FormHelperTest.php 286 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773
  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. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property FormHelper $Form
  103. */
  104. class FormHelperTest extends TestCase
  105. {
  106. /**
  107. * Fixtures to be used
  108. *
  109. * @var array
  110. */
  111. public $fixtures = ['core.articles', 'core.comments'];
  112. /**
  113. * Do not load the fixtures by default
  114. *
  115. * @var bool
  116. */
  117. public $autoFixtures = false;
  118. /**
  119. * setUp method
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. Configure::write('Config.language', 'eng');
  127. Configure::write('App.base', '');
  128. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  129. Configure::delete('Asset');
  130. $this->View = new View();
  131. $this->Form = new FormHelper($this->View);
  132. $request = new Request('articles/add');
  133. $request->here = '/articles/add';
  134. $request['controller'] = 'articles';
  135. $request['action'] = 'add';
  136. $request->webroot = '';
  137. $request->base = '';
  138. $this->Form->Url->request = $this->Form->request = $request;
  139. $this->dateRegex = [
  140. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  142. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  144. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  145. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  146. ];
  147. $this->article = [
  148. 'schema' => [
  149. 'id' => ['type' => 'integer'],
  150. 'author_id' => ['type' => 'integer', 'null' => true],
  151. 'title' => ['type' => 'string', 'null' => true],
  152. 'body' => 'text',
  153. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  154. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  155. ],
  156. 'required' => [
  157. 'author_id' => true,
  158. 'title' => true,
  159. ]
  160. ];
  161. Security::salt('foo!');
  162. Router::connect('/:controller', ['action' => 'index']);
  163. Router::connect('/:controller/:action/*');
  164. }
  165. /**
  166. * tearDown method
  167. *
  168. * @return void
  169. */
  170. public function tearDown()
  171. {
  172. parent::tearDown();
  173. unset($this->Form, $this->Controller, $this->View);
  174. TableRegistry::clear();
  175. }
  176. /**
  177. * Test construct() with the templates option.
  178. *
  179. * @return void
  180. */
  181. public function testConstructTemplatesFile()
  182. {
  183. $helper = new FormHelper($this->View, [
  184. 'templates' => 'htmlhelper_tags'
  185. ]);
  186. $result = $helper->input('name');
  187. $this->assertContains('<input', $result);
  188. }
  189. /**
  190. * Test that when specifying custom widgets the config array for that widget
  191. * is overwritten instead of merged.
  192. *
  193. * @return void
  194. */
  195. public function testConstructWithWidgets()
  196. {
  197. $config = [
  198. 'widgets' => [
  199. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  200. ]
  201. ];
  202. $helper = new FormHelper($this->View, $config);
  203. $registry = $helper->widgetRegistry();
  204. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  205. }
  206. /**
  207. * Test that when specifying custom widgets config file and it should be
  208. * added to widgets array. WidgetRegistry will load widgets in constructor.
  209. *
  210. * @return void
  211. */
  212. public function testConstructWithWidgetsConfig()
  213. {
  214. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  215. $registry = $helper->widgetRegistry();
  216. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  217. }
  218. /**
  219. * Test registering a new widget class and rendering it.
  220. *
  221. * @return void
  222. */
  223. public function testAddWidgetAndRenderWidget()
  224. {
  225. $data = [
  226. 'val' => 1
  227. ];
  228. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  229. $this->assertNull($this->Form->addWidget('test', $mock));
  230. $mock->expects($this->once())
  231. ->method('render')
  232. ->with($data)
  233. ->will($this->returnValue('HTML'));
  234. $result = $this->Form->widget('test', $data);
  235. $this->assertEquals('HTML', $result);
  236. }
  237. /**
  238. * Test that secureFields() of widget is called after calling render(),
  239. * not before.
  240. *
  241. * @return void
  242. */
  243. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  244. {
  245. $data = [
  246. 'val' => 1,
  247. 'name' => 'test'
  248. ];
  249. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  250. $this->assertNull($this->Form->addWidget('test', $mock));
  251. $mock->expects($this->at(0))
  252. ->method('render')
  253. ->with($data)
  254. ->will($this->returnValue('HTML'));
  255. $mock->expects($this->at(1))
  256. ->method('secureFields')
  257. ->with($data)
  258. ->will($this->returnValue(['test']));
  259. $result = $this->Form->widget('test', $data + ['secure' => true]);
  260. $this->assertEquals('HTML', $result);
  261. }
  262. /**
  263. * Test that empty string is not added to secure fields list when
  264. * rendering input widget without name.
  265. *
  266. * @return void
  267. */
  268. public function testRenderingWidgetWithEmptyName()
  269. {
  270. $this->assertEquals([], $this->Form->fields);
  271. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  272. $this->assertEquals('<select name=""></select>', $result);
  273. $this->assertEquals([], $this->Form->fields);
  274. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  275. $this->assertEquals('<select name="0"></select>', $result);
  276. $this->assertEquals(['0'], $this->Form->fields);
  277. }
  278. /**
  279. * Test registering an invalid widget class.
  280. *
  281. * @expectedException \RuntimeException
  282. * @return void
  283. */
  284. public function testAddWidgetInvalid()
  285. {
  286. $mock = new \StdClass();
  287. $this->Form->addWidget('test', $mock);
  288. $this->Form->widget('test');
  289. }
  290. /**
  291. * Test adding a new context class.
  292. *
  293. * @return void
  294. */
  295. public function testAddContextProvider()
  296. {
  297. $context = 'My data';
  298. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  299. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  300. $this->assertInstanceOf('Cake\Network\Request', $request);
  301. $this->assertEquals($context, $data['entity']);
  302. return $stub;
  303. });
  304. $this->Form->create($context);
  305. $result = $this->Form->context();
  306. $this->assertSame($stub, $result);
  307. }
  308. /**
  309. * Test replacing a context class.
  310. *
  311. * @return void
  312. */
  313. public function testAddContextProviderReplace()
  314. {
  315. $entity = new Article();
  316. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  317. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  318. return $stub;
  319. });
  320. $this->Form->create($entity);
  321. $result = $this->Form->context();
  322. $this->assertSame($stub, $result);
  323. }
  324. /**
  325. * Test overriding a context class.
  326. *
  327. * @return void
  328. */
  329. public function testAddContextProviderAdd()
  330. {
  331. $entity = new Article();
  332. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  333. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  334. if ($data['entity'] instanceof Entity) {
  335. return $stub;
  336. }
  337. });
  338. $this->Form->create($entity);
  339. $result = $this->Form->context();
  340. $this->assertSame($stub, $result);
  341. }
  342. /**
  343. * Test adding an invalid context class.
  344. *
  345. * @expectedException \RuntimeException
  346. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  347. * @return void
  348. */
  349. public function testAddContextProviderInvalid()
  350. {
  351. $context = 'My data';
  352. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  353. return new \StdClass();
  354. });
  355. $this->Form->create($context);
  356. }
  357. /**
  358. * Provides context options for create().
  359. *
  360. * @return array
  361. */
  362. public function contextSelectionProvider()
  363. {
  364. $entity = new Article();
  365. $collection = new Collection([$entity]);
  366. $emptyCollection = new Collection([]);
  367. $arrayObject = new \ArrayObject([]);
  368. $data = [
  369. 'schema' => [
  370. 'title' => ['type' => 'string']
  371. ]
  372. ];
  373. $form = new Form();
  374. return [
  375. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  376. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  377. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  378. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  379. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  380. 'form' => [$form, 'Cake\View\Form\FormContext'],
  381. 'none' => [null, 'Cake\View\Form\NullContext'],
  382. 'false' => [false, 'Cake\View\Form\NullContext'],
  383. ];
  384. }
  385. /**
  386. * Test default context selection in create()
  387. *
  388. * @dataProvider contextSelectionProvider
  389. * @return void
  390. */
  391. public function testCreateContextSelectionBuiltIn($data, $class)
  392. {
  393. $this->loadFixtures('Articles');
  394. $this->Form->create($data);
  395. $this->assertInstanceOf($class, $this->Form->context());
  396. }
  397. /**
  398. * Data provider for type option.
  399. *
  400. * @return array
  401. */
  402. public static function requestTypeProvider()
  403. {
  404. return [
  405. // type, method, override
  406. ['post', 'post', 'POST'],
  407. ['put', 'post', 'PUT'],
  408. ['patch', 'post', 'PATCH'],
  409. ['delete', 'post', 'DELETE'],
  410. ];
  411. }
  412. /**
  413. * Test creating file forms.
  414. *
  415. * @return void
  416. */
  417. public function testCreateFile()
  418. {
  419. $encoding = strtolower(Configure::read('App.encoding'));
  420. $result = $this->Form->create(false, ['type' => 'file']);
  421. $expected = [
  422. 'form' => [
  423. 'method' => 'post', 'action' => '/articles/add',
  424. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  425. ],
  426. 'div' => ['style' => 'display:none;'],
  427. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  428. '/div'
  429. ];
  430. $this->assertHtml($expected, $result);
  431. }
  432. /**
  433. * Test creating GET forms.
  434. *
  435. * @return void
  436. */
  437. public function testCreateGet()
  438. {
  439. $encoding = strtolower(Configure::read('App.encoding'));
  440. $result = $this->Form->create(false, ['type' => 'get']);
  441. $expected = ['form' => [
  442. 'method' => 'get', 'action' => '/articles/add',
  443. 'accept-charset' => $encoding
  444. ]];
  445. $this->assertHtml($expected, $result);
  446. }
  447. /**
  448. * Test explicit method/enctype options.
  449. *
  450. * Explicit method overwrites inferred method from 'type'
  451. *
  452. * @return void
  453. */
  454. public function testCreateExplicitMethodEnctype()
  455. {
  456. $encoding = strtolower(Configure::read('App.encoding'));
  457. $result = $this->Form->create(false, [
  458. 'type' => 'get',
  459. 'method' => 'put',
  460. 'enctype' => 'multipart/form-data'
  461. ]);
  462. $expected = ['form' => [
  463. 'method' => 'put',
  464. 'action' => '/articles/add',
  465. 'enctype' => 'multipart/form-data',
  466. 'accept-charset' => $encoding
  467. ]];
  468. $this->assertHtml($expected, $result);
  469. }
  470. /**
  471. * Test create() with the templates option.
  472. *
  473. * @return void
  474. */
  475. public function testCreateTemplatesArray()
  476. {
  477. $result = $this->Form->create($this->article, [
  478. 'templates' => [
  479. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  480. ]
  481. ]);
  482. $expected = [
  483. 'form' => [
  484. 'class' => 'form-horizontal',
  485. 'method' => 'post',
  486. 'action' => '/articles/add',
  487. 'accept-charset' => 'utf-8'
  488. ]
  489. ];
  490. $this->assertHtml($expected, $result);
  491. }
  492. /**
  493. * Test create() with the templates option.
  494. *
  495. * @return void
  496. */
  497. public function testCreateTemplatesFile()
  498. {
  499. $result = $this->Form->create($this->article, [
  500. 'templates' => 'htmlhelper_tags',
  501. ]);
  502. $expected = [
  503. 'start form',
  504. 'div' => ['class' => 'hidden'],
  505. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  506. '/div'
  507. ];
  508. $this->assertHtml($expected, $result);
  509. }
  510. /**
  511. * Test that create() and end() restore templates.
  512. *
  513. * @return void
  514. */
  515. public function testCreateEndRestoreTemplates()
  516. {
  517. $this->Form->create($this->article, [
  518. 'templates' => ['input' => 'custom input element']
  519. ]);
  520. $this->Form->end();
  521. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  522. }
  523. /**
  524. * Test using template vars in various templates used by input() method.
  525. *
  526. * @return void
  527. */
  528. public function testInputTemplateVars()
  529. {
  530. $result = $this->Form->input('text', [
  531. 'templates' => [
  532. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  533. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  534. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  535. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  536. ],
  537. 'templateVars' => [
  538. 'forinput' => 'in-input',
  539. 'forlabel' => 'in-label',
  540. 'forgroup' => 'in-group',
  541. 'forcontainer' => 'in-container'
  542. ]
  543. ]);
  544. $expected = [
  545. 'div' => ['class'],
  546. 'label' => ['for'],
  547. 'Text in-label',
  548. '/label',
  549. 'in-group',
  550. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  551. 'in-container',
  552. '/div',
  553. ];
  554. $this->assertHtml($expected, $result);
  555. }
  556. /**
  557. * Test ensuring template variables work in template files loaded
  558. * during input().
  559. *
  560. * @return void
  561. */
  562. public function testInputTemplatesFromFile()
  563. {
  564. $result = $this->Form->input('title', [
  565. 'templates' => 'test_templates',
  566. 'templateVars' => [
  567. 'forcontainer' => 'container-data'
  568. ]
  569. ]);
  570. $expected = [
  571. 'div' => ['class'],
  572. 'label' => ['for'],
  573. 'Title',
  574. '/label',
  575. 'input' => ['name', 'type' => 'text', 'id'],
  576. 'container-data',
  577. '/div',
  578. ];
  579. $this->assertHtml($expected, $result);
  580. }
  581. /**
  582. * Test using template vars in inputSubmit and submitContainer template.
  583. *
  584. * @return void
  585. */
  586. public function testSubmitTemplateVars()
  587. {
  588. $this->Form->templates([
  589. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  590. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  591. ]);
  592. $result = $this->Form->submit('Submit', [
  593. 'templateVars' => [
  594. 'forinput' => 'in-input',
  595. 'forcontainer' => 'in-container'
  596. ]
  597. ]);
  598. $expected = [
  599. 'div' => ['class'],
  600. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  601. 'in-container',
  602. '/div',
  603. ];
  604. $this->assertHtml($expected, $result);
  605. }
  606. /**
  607. * test the create() method
  608. *
  609. * @dataProvider requestTypeProvider
  610. * @return void
  611. */
  612. public function testCreateTypeOptions($type, $method, $override)
  613. {
  614. $encoding = strtolower(Configure::read('App.encoding'));
  615. $result = $this->Form->create(false, ['type' => $type]);
  616. $expected = [
  617. 'form' => [
  618. 'method' => $method, 'action' => '/articles/add',
  619. 'accept-charset' => $encoding
  620. ],
  621. 'div' => ['style' => 'display:none;'],
  622. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  623. '/div'
  624. ];
  625. $this->assertHtml($expected, $result);
  626. }
  627. /**
  628. * Test using template vars in Create (formStart template)
  629. *
  630. * @return void
  631. */
  632. public function testCreateTemplateVars()
  633. {
  634. $result = $this->Form->create($this->article, [
  635. 'templates' => [
  636. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  637. ],
  638. 'templateVars' => ['header' => 'headertext']
  639. ]);
  640. $expected = [
  641. 'h4' => ['class'],
  642. 'headertext',
  643. '/h4',
  644. 'form' => [
  645. 'method' => 'post',
  646. 'action' => '/articles/add',
  647. 'accept-charset' => 'utf-8'
  648. ]
  649. ];
  650. $this->assertHtml($expected, $result);
  651. }
  652. /**
  653. * Test opening a form for an update operation.
  654. *
  655. * @return void
  656. */
  657. public function testCreateUpdateForm()
  658. {
  659. $encoding = strtolower(Configure::read('App.encoding'));
  660. $this->Form->request->here = '/articles/edit/1';
  661. $this->Form->request['action'] = 'edit';
  662. $this->article['defaults']['id'] = 1;
  663. $result = $this->Form->create($this->article);
  664. $expected = [
  665. 'form' => [
  666. 'method' => 'post', 'action' => '/articles/edit/1',
  667. 'accept-charset' => $encoding
  668. ],
  669. 'div' => ['style' => 'display:none;'],
  670. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  671. '/div'
  672. ];
  673. $this->assertHtml($expected, $result);
  674. }
  675. /**
  676. * test create() with automatic url generation
  677. *
  678. * @return void
  679. */
  680. public function testCreateAutoUrl()
  681. {
  682. $encoding = strtolower(Configure::read('App.encoding'));
  683. $this->Form->request['action'] = 'delete';
  684. $this->Form->request->here = '/articles/delete/10';
  685. $this->Form->request->base = '';
  686. $result = $this->Form->create($this->article);
  687. $expected = [
  688. 'form' => [
  689. 'method' => 'post', 'action' => '/articles/delete/10',
  690. 'accept-charset' => $encoding
  691. ],
  692. 'div' => ['style' => 'display:none;'],
  693. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  694. '/div'
  695. ];
  696. $this->assertHtml($expected, $result);
  697. $this->article['defaults'] = ['id' => 1];
  698. $this->Form->request->here = '/articles/edit/1';
  699. $this->Form->request['action'] = 'delete';
  700. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  701. $expected = [
  702. 'form' => [
  703. 'method' => 'post',
  704. 'action' => '/articles/edit/1',
  705. 'accept-charset' => $encoding
  706. ],
  707. 'div' => ['style' => 'display:none;'],
  708. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  709. '/div'
  710. ];
  711. $this->assertHtml($expected, $result);
  712. $this->Form->request['action'] = 'add';
  713. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  714. $expected = [
  715. 'form' => [
  716. 'method' => 'post',
  717. 'action' => '/articles/publish/1',
  718. 'accept-charset' => $encoding
  719. ],
  720. 'div' => ['style' => 'display:none;'],
  721. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  722. '/div'
  723. ];
  724. $this->assertHtml($expected, $result);
  725. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  726. $expected = [
  727. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  728. 'div' => ['style' => 'display:none;'],
  729. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  730. '/div'
  731. ];
  732. $this->assertHtml($expected, $result);
  733. $this->Form->request['controller'] = 'Pages';
  734. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  735. $expected = [
  736. 'form' => [
  737. 'method' => 'post', 'action' => '/Pages/signup/1',
  738. 'accept-charset' => $encoding
  739. ],
  740. 'div' => ['style' => 'display:none;'],
  741. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  742. '/div'
  743. ];
  744. $this->assertHtml($expected, $result);
  745. }
  746. /**
  747. * Test create() with no URL (no "action" attribute for <form> tag)
  748. *
  749. * @return void
  750. */
  751. public function testCreateNoUrl()
  752. {
  753. $result = $this->Form->create(false, ['url' => false]);
  754. $expected = [
  755. 'form' => [
  756. 'method' => 'post',
  757. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  758. ],
  759. 'div' => ['style' => 'display:none;'],
  760. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  761. '/div'
  762. ];
  763. $this->assertHtml($expected, $result);
  764. }
  765. /**
  766. * test create() with a custom route
  767. *
  768. * @return void
  769. */
  770. public function testCreateCustomRoute()
  771. {
  772. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  773. $encoding = strtolower(Configure::read('App.encoding'));
  774. $this->Form->request['controller'] = 'users';
  775. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  776. $expected = [
  777. 'form' => [
  778. 'method' => 'post', 'action' => '/login',
  779. 'accept-charset' => $encoding
  780. ],
  781. 'div' => ['style' => 'display:none;'],
  782. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  783. '/div'
  784. ];
  785. $this->assertHtml($expected, $result);
  786. Router::connect(
  787. '/new-article',
  788. ['controller' => 'articles', 'action' => 'myaction'],
  789. ['_name' => 'my-route']
  790. );
  791. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  792. $expected = [
  793. 'form' => [
  794. 'method' => 'post', 'action' => '/new-article',
  795. 'accept-charset' => $encoding,
  796. ],
  797. 'div' => ['style' => 'display:none;'],
  798. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  799. '/div'
  800. ];
  801. $this->assertHtml($expected, $result);
  802. }
  803. /**
  804. * test automatic accept-charset overriding
  805. *
  806. * @return void
  807. */
  808. public function testCreateWithAcceptCharset()
  809. {
  810. $result = $this->Form->create(
  811. $this->article,
  812. [
  813. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  814. ]
  815. );
  816. $expected = [
  817. 'form' => [
  818. 'method' => 'post', 'action' => '/articles',
  819. 'accept-charset' => 'iso-8859-1'
  820. ],
  821. 'div' => ['style' => 'display:none;'],
  822. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  823. '/div'
  824. ];
  825. $this->assertHtml($expected, $result);
  826. }
  827. /**
  828. * Test base form URL when url param is passed with multiple parameters (&)
  829. *
  830. */
  831. public function testCreateQueryStringRequest()
  832. {
  833. $encoding = strtolower(Configure::read('App.encoding'));
  834. $result = $this->Form->create($this->article, [
  835. 'type' => 'post',
  836. 'escape' => false,
  837. 'url' => [
  838. 'controller' => 'controller',
  839. 'action' => 'action',
  840. '?' => ['param1' => 'value1', 'param2' => 'value2']
  841. ]
  842. ]);
  843. $expected = [
  844. 'form' => [
  845. 'method' => 'post',
  846. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  847. 'accept-charset' => $encoding
  848. ],
  849. 'div' => ['style' => 'display:none;'],
  850. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  851. '/div'
  852. ];
  853. $this->assertHtml($expected, $result);
  854. $result = $this->Form->create($this->article, [
  855. 'type' => 'post',
  856. 'url' => [
  857. 'controller' => 'controller',
  858. 'action' => 'action',
  859. '?' => ['param1' => 'value1', 'param2' => 'value2']
  860. ]
  861. ]);
  862. $this->assertHtml($expected, $result);
  863. }
  864. /**
  865. * test that create() doesn't cause errors by multiple id's being in the primary key
  866. * as could happen with multiple select or checkboxes.
  867. *
  868. * @return void
  869. */
  870. public function testCreateWithMultipleIdInData()
  871. {
  872. $encoding = strtolower(Configure::read('App.encoding'));
  873. $this->Form->request->data['Article']['id'] = [1, 2];
  874. $result = $this->Form->create($this->article);
  875. $expected = [
  876. 'form' => [
  877. 'method' => 'post',
  878. 'action' => '/articles/add',
  879. 'accept-charset' => $encoding
  880. ],
  881. 'div' => ['style' => 'display:none;'],
  882. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  883. '/div'
  884. ];
  885. $this->assertHtml($expected, $result);
  886. }
  887. /**
  888. * test that create() doesn't add in extra passed params.
  889. *
  890. * @return void
  891. */
  892. public function testCreatePassedArgs()
  893. {
  894. $encoding = strtolower(Configure::read('App.encoding'));
  895. $this->Form->request->data['Article']['id'] = 1;
  896. $result = $this->Form->create($this->article, [
  897. 'type' => 'post',
  898. 'escape' => false,
  899. 'url' => [
  900. 'action' => 'edit',
  901. 'myparam'
  902. ]
  903. ]);
  904. $expected = [
  905. 'form' => [
  906. 'method' => 'post',
  907. 'action' => '/articles/edit/myparam',
  908. 'accept-charset' => $encoding
  909. ],
  910. 'div' => ['style' => 'display:none;'],
  911. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  912. '/div'
  913. ];
  914. $this->assertHtml($expected, $result);
  915. }
  916. /**
  917. * test creating a get form, and get form inputs.
  918. *
  919. * @return void
  920. */
  921. public function testGetFormCreate()
  922. {
  923. $encoding = strtolower(Configure::read('App.encoding'));
  924. $result = $this->Form->create($this->article, ['type' => 'get']);
  925. $expected = ['form' => [
  926. 'method' => 'get', 'action' => '/articles/add',
  927. 'accept-charset' => $encoding
  928. ]];
  929. $this->assertHtml($expected, $result);
  930. $result = $this->Form->text('title');
  931. $expected = ['input' => [
  932. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  933. ]];
  934. $this->assertHtml($expected, $result);
  935. $result = $this->Form->password('password');
  936. $expected = ['input' => [
  937. 'name' => 'password', 'type' => 'password'
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  941. $result = $this->Form->text('user_form');
  942. $expected = ['input' => [
  943. 'name' => 'user_form', 'type' => 'text'
  944. ]];
  945. $this->assertHtml($expected, $result);
  946. }
  947. /**
  948. * test get form, and inputs when the model param is false
  949. *
  950. * @return void
  951. */
  952. public function testGetFormWithFalseModel()
  953. {
  954. $encoding = strtolower(Configure::read('App.encoding'));
  955. $this->Form->request['controller'] = 'contact_test';
  956. $result = $this->Form->create(false, [
  957. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  958. ]);
  959. $expected = ['form' => [
  960. 'method' => 'get', 'action' => '/contact_test/add',
  961. 'accept-charset' => $encoding
  962. ]];
  963. $this->assertHtml($expected, $result);
  964. $result = $this->Form->text('reason');
  965. $expected = [
  966. 'input' => ['type' => 'text', 'name' => 'reason']
  967. ];
  968. $this->assertHtml($expected, $result);
  969. }
  970. /**
  971. * testFormCreateWithSecurity method
  972. *
  973. * Test form->create() with security key.
  974. *
  975. * @return void
  976. */
  977. public function testCreateWithSecurity()
  978. {
  979. $this->Form->request->params['_csrfToken'] = 'testKey';
  980. $encoding = strtolower(Configure::read('App.encoding'));
  981. $result = $this->Form->create($this->article, [
  982. 'url' => '/articles/publish',
  983. ]);
  984. $expected = [
  985. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  986. 'div' => ['style' => 'display:none;'],
  987. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  988. ['input' => [
  989. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  990. ]],
  991. '/div'
  992. ];
  993. $this->assertHtml($expected, $result);
  994. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  995. $expected['form']['id'] = 'MyForm';
  996. $this->assertHtml($expected, $result);
  997. }
  998. /**
  999. * testFormCreateGetNoSecurity method
  1000. *
  1001. * Test form->create() with no security key as its a get form
  1002. *
  1003. * @return void
  1004. */
  1005. public function testCreateEndGetNoSecurity()
  1006. {
  1007. $this->Form->request->params['_csrfToken'] = 'testKey';
  1008. $article = new Article();
  1009. $result = $this->Form->create($article, [
  1010. 'type' => 'get',
  1011. 'url' => '/contacts/add'
  1012. ]);
  1013. $this->assertNotContains('testKey', $result);
  1014. $result = $this->Form->end();
  1015. $this->assertNotContains('testKey', $result);
  1016. }
  1017. /**
  1018. * test that create() clears the fields property so it starts fresh
  1019. *
  1020. * @return void
  1021. */
  1022. public function testCreateClearingFields()
  1023. {
  1024. $this->Form->fields = ['model_id'];
  1025. $this->Form->create($this->article);
  1026. $this->assertEquals([], $this->Form->fields);
  1027. }
  1028. /**
  1029. * Tests form hash generation with model-less data
  1030. *
  1031. * @return void
  1032. */
  1033. public function testValidateHashNoModel()
  1034. {
  1035. $this->Form->request->params['_Token'] = 'foo';
  1036. $result = $this->Form->secure(['anything']);
  1037. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1038. }
  1039. /**
  1040. * Tests that hidden fields generated for checkboxes don't get locked
  1041. *
  1042. * @return void
  1043. */
  1044. public function testNoCheckboxLocking()
  1045. {
  1046. $this->Form->request->params['_Token'] = 'foo';
  1047. $this->assertSame([], $this->Form->fields);
  1048. $this->Form->checkbox('check', ['value' => '1']);
  1049. $this->assertSame($this->Form->fields, ['check']);
  1050. }
  1051. /**
  1052. * testFormSecurityFields method
  1053. *
  1054. * Test generation of secure form hash generation.
  1055. *
  1056. * @return void
  1057. */
  1058. public function testFormSecurityFields()
  1059. {
  1060. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1061. $this->Form->request->params['_Token'] = 'testKey';
  1062. $result = $this->Form->secure($fields);
  1063. $hash = Security::hash(serialize($fields) . Security::salt());
  1064. $hash .= ':' . 'Model.valid';
  1065. $hash = urlencode($hash);
  1066. $tokenDebug = urlencode(json_encode([
  1067. '',
  1068. $fields,
  1069. []
  1070. ]));
  1071. $expected = [
  1072. 'div' => ['style' => 'display:none;'],
  1073. ['input' => [
  1074. 'type' => 'hidden',
  1075. 'name' => '_Token[fields]',
  1076. 'value' => $hash
  1077. ]],
  1078. ['input' => [
  1079. 'type' => 'hidden',
  1080. 'name' => '_Token[unlocked]',
  1081. 'value' => '',
  1082. ]],
  1083. ['input' => [
  1084. 'type' => 'hidden',
  1085. 'name' => '_Token[debug]',
  1086. 'value' => $tokenDebug
  1087. ]],
  1088. '/div'
  1089. ];
  1090. $this->assertHtml($expected, $result);
  1091. }
  1092. /**
  1093. * testFormSecurityFields method
  1094. *
  1095. * Test debug token is not generated if debug is false
  1096. *
  1097. * @return void
  1098. */
  1099. public function testFormSecurityFieldsNoDebugMode()
  1100. {
  1101. Configure::write('debug', false);
  1102. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1103. $this->Form->request->params['_Token'] = 'testKey';
  1104. $result = $this->Form->secure($fields);
  1105. $hash = Security::hash(serialize($fields) . Security::salt());
  1106. $hash .= ':' . 'Model.valid';
  1107. $hash = urlencode($hash);
  1108. $expected = [
  1109. 'div' => ['style' => 'display:none;'],
  1110. ['input' => [
  1111. 'type' => 'hidden',
  1112. 'name' => '_Token[fields]',
  1113. 'value' => $hash
  1114. ]],
  1115. ['input' => [
  1116. 'type' => 'hidden',
  1117. 'name' => '_Token[unlocked]',
  1118. 'value' => '',
  1119. ]],
  1120. '/div'
  1121. ];
  1122. $this->assertHtml($expected, $result);
  1123. }
  1124. /**
  1125. * Tests correct generation of number fields for double and float fields
  1126. *
  1127. * @return void
  1128. */
  1129. public function testTextFieldGenerationForFloats()
  1130. {
  1131. $this->article['schema'] = [
  1132. 'foo' => [
  1133. 'type' => 'float',
  1134. 'null' => false,
  1135. 'default' => null,
  1136. 'length' => 10
  1137. ]
  1138. ];
  1139. $this->Form->create($this->article);
  1140. $result = $this->Form->input('foo');
  1141. $expected = [
  1142. 'div' => ['class' => 'input number'],
  1143. 'label' => ['for' => 'foo'],
  1144. 'Foo',
  1145. '/label',
  1146. ['input' => [
  1147. 'type' => 'number',
  1148. 'name' => 'foo',
  1149. 'id' => 'foo',
  1150. 'step' => 'any'
  1151. ]],
  1152. '/div'
  1153. ];
  1154. $this->assertHtml($expected, $result);
  1155. $result = $this->Form->input('foo', ['step' => 0.5]);
  1156. $expected = [
  1157. 'div' => ['class' => 'input number'],
  1158. 'label' => ['for' => 'foo'],
  1159. 'Foo',
  1160. '/label',
  1161. ['input' => [
  1162. 'type' => 'number',
  1163. 'name' => 'foo',
  1164. 'id' => 'foo',
  1165. 'step' => '0.5'
  1166. ]],
  1167. '/div'
  1168. ];
  1169. $this->assertHtml($expected, $result);
  1170. }
  1171. /**
  1172. * Tests correct generation of number fields for integer fields
  1173. *
  1174. * @return void
  1175. */
  1176. public function testTextFieldTypeNumberGenerationForIntegers()
  1177. {
  1178. TableRegistry::get('Contacts', [
  1179. 'className' => __NAMESPACE__ . '\ContactsTable'
  1180. ]);
  1181. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1182. $result = $this->Form->input('age');
  1183. $expected = [
  1184. 'div' => ['class' => 'input number'],
  1185. 'label' => ['for' => 'age'],
  1186. 'Age',
  1187. '/label',
  1188. ['input' => [
  1189. 'type' => 'number', 'name' => 'age',
  1190. 'id' => 'age'
  1191. ]],
  1192. '/div'
  1193. ];
  1194. $this->assertHtml($expected, $result);
  1195. }
  1196. /**
  1197. * Tests correct generation of file upload fields for binary fields
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFileUploadFieldTypeGenerationForBinaries()
  1202. {
  1203. $table = TableRegistry::get('Contacts', [
  1204. 'className' => __NAMESPACE__ . '\ContactsTable'
  1205. ]);
  1206. $table->schema(['foo' => [
  1207. 'type' => 'binary',
  1208. 'null' => false,
  1209. 'default' => null,
  1210. 'length' => 1024
  1211. ]]);
  1212. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1213. $result = $this->Form->input('foo');
  1214. $expected = [
  1215. 'div' => ['class' => 'input file'],
  1216. 'label' => ['for' => 'foo'],
  1217. 'Foo',
  1218. '/label',
  1219. ['input' => [
  1220. 'type' => 'file', 'name' => 'foo',
  1221. 'id' => 'foo'
  1222. ]],
  1223. '/div'
  1224. ];
  1225. $this->assertHtml($expected, $result);
  1226. }
  1227. /**
  1228. * testFormSecurityMultipleFields method
  1229. *
  1230. * Test secure() with multiple row form. Ensure hash is correct.
  1231. *
  1232. * @return void
  1233. */
  1234. public function testFormSecurityMultipleFields()
  1235. {
  1236. $this->Form->request->params['_Token'] = 'foo';
  1237. $fields = [
  1238. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1239. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1240. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1241. ];
  1242. $result = $this->Form->secure($fields);
  1243. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1244. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1245. $tokenDebug = urlencode(json_encode([
  1246. '',
  1247. $fields,
  1248. []
  1249. ]));
  1250. $expected = [
  1251. 'div' => ['style' => 'display:none;'],
  1252. ['input' => [
  1253. 'type' => 'hidden', 'name' => '_Token[fields]',
  1254. 'value' => $hash
  1255. ]],
  1256. ['input' => [
  1257. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1258. 'value' => ''
  1259. ]],
  1260. ['input' => [
  1261. 'type' => 'hidden', 'name' => '_Token[debug]',
  1262. 'value' => $tokenDebug,
  1263. ]],
  1264. '/div'
  1265. ];
  1266. $this->assertHtml($expected, $result);
  1267. }
  1268. /**
  1269. * testFormSecurityMultipleSubmitButtons
  1270. *
  1271. * test form submit generation and ensure that _Token is only created on end()
  1272. *
  1273. * @return void
  1274. */
  1275. public function testFormSecurityMultipleSubmitButtons()
  1276. {
  1277. $this->Form->request->params['_Token'] = 'testKey';
  1278. $this->Form->create($this->article);
  1279. $this->Form->text('Address.title');
  1280. $this->Form->text('Address.first_name');
  1281. $result = $this->Form->submit('Save', ['name' => 'save']);
  1282. $expected = [
  1283. 'div' => ['class' => 'submit'],
  1284. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1285. '/div',
  1286. ];
  1287. $this->assertHtml($expected, $result);
  1288. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1289. $expected = [
  1290. 'div' => ['class' => 'submit'],
  1291. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1292. '/div',
  1293. ];
  1294. $this->assertHtml($expected, $result);
  1295. $result = $this->Form->end();
  1296. $tokenDebug = urlencode(json_encode([
  1297. '/articles/add',
  1298. [
  1299. 'Address.title',
  1300. 'Address.first_name',
  1301. ],
  1302. ['save', 'cancel']
  1303. ]));
  1304. $expected = [
  1305. 'div' => ['style' => 'display:none;'],
  1306. ['input' => [
  1307. 'type' => 'hidden',
  1308. 'name' => '_Token[fields]',
  1309. 'value'
  1310. ]],
  1311. ['input' => [
  1312. 'type' => 'hidden',
  1313. 'name' => '_Token[unlocked]',
  1314. 'value' => 'cancel%7Csave'
  1315. ]],
  1316. ['input' => [
  1317. 'type' => 'hidden', 'name' => '_Token[debug]',
  1318. 'value' => $tokenDebug,
  1319. ]],
  1320. '/div'
  1321. ];
  1322. $this->assertHtml($expected, $result);
  1323. }
  1324. /**
  1325. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1326. *
  1327. * @return void
  1328. */
  1329. public function testSecurityButtonNestedNamed()
  1330. {
  1331. $key = 'testKey';
  1332. $this->Form->request->params['_csrfToken'] = $key;
  1333. $this->Form->create('Addresses');
  1334. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1335. $result = $this->Form->unlockField();
  1336. $this->assertEquals(['Address.button'], $result);
  1337. }
  1338. /**
  1339. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1340. *
  1341. * @return void
  1342. */
  1343. public function testSecuritySubmitNestedNamed()
  1344. {
  1345. $this->Form->request->params['_Token'] = 'testKey';
  1346. $this->Form->create($this->article);
  1347. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1348. $result = $this->Form->unlockField();
  1349. $this->assertEquals(['Address.button'], $result);
  1350. }
  1351. /**
  1352. * Test that the correct fields are unlocked for image submits with no names.
  1353. *
  1354. * @return void
  1355. */
  1356. public function testSecuritySubmitImageNoName()
  1357. {
  1358. $this->Form->request->params['_Token'] = 'testKey';
  1359. $this->Form->create(false);
  1360. $result = $this->Form->submit('save.png');
  1361. $expected = [
  1362. 'div' => ['class' => 'submit'],
  1363. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1364. '/div'
  1365. ];
  1366. $this->assertHtml($expected, $result);
  1367. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1368. }
  1369. /**
  1370. * Test that the correct fields are unlocked for image submits with names.
  1371. *
  1372. * @return void
  1373. */
  1374. public function testSecuritySubmitImageName()
  1375. {
  1376. $this->Form->request->params['_Token'] = 'testKey';
  1377. $this->Form->create(null);
  1378. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1379. $expected = [
  1380. 'div' => ['class' => 'submit'],
  1381. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1382. '/div'
  1383. ];
  1384. $this->assertHtml($expected, $result);
  1385. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1386. }
  1387. /**
  1388. * testFormSecurityMultipleInputFields method
  1389. *
  1390. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1391. *
  1392. * @return void
  1393. */
  1394. public function testFormSecurityMultipleInputFields()
  1395. {
  1396. $this->Form->request->params['_Token'] = 'testKey';
  1397. $this->Form->create();
  1398. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1399. $this->Form->input('Addresses.0.title');
  1400. $this->Form->input('Addresses.0.first_name');
  1401. $this->Form->input('Addresses.0.last_name');
  1402. $this->Form->input('Addresses.0.address');
  1403. $this->Form->input('Addresses.0.city');
  1404. $this->Form->input('Addresses.0.phone');
  1405. $this->Form->input('Addresses.0.primary', ['type' => 'checkbox']);
  1406. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1407. $this->Form->input('Addresses.1.title');
  1408. $this->Form->input('Addresses.1.first_name');
  1409. $this->Form->input('Addresses.1.last_name');
  1410. $this->Form->input('Addresses.1.address');
  1411. $this->Form->input('Addresses.1.city');
  1412. $this->Form->input('Addresses.1.phone');
  1413. $this->Form->input('Addresses.1.primary', ['type' => 'checkbox']);
  1414. $result = $this->Form->secure($this->Form->fields);
  1415. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1416. $tokenDebug = urlencode(json_encode([
  1417. '/articles/add',
  1418. [
  1419. 'Addresses.0.id' => '123456',
  1420. 'Addresses.0.title',
  1421. 'Addresses.0.first_name',
  1422. 'Addresses.0.last_name',
  1423. 'Addresses.0.address',
  1424. 'Addresses.0.city',
  1425. 'Addresses.0.phone',
  1426. 'Addresses.0.primary',
  1427. 'Addresses.1.id' => '654321',
  1428. 'Addresses.1.title',
  1429. 'Addresses.1.first_name',
  1430. 'Addresses.1.last_name',
  1431. 'Addresses.1.address',
  1432. 'Addresses.1.city',
  1433. 'Addresses.1.phone',
  1434. 'Addresses.1.primary',
  1435. ],
  1436. []
  1437. ]));
  1438. $expected = [
  1439. 'div' => ['style' => 'display:none;'],
  1440. ['input' => [
  1441. 'type' => 'hidden', 'name' => '_Token[fields]',
  1442. 'value' => $hash
  1443. ]],
  1444. ['input' => [
  1445. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1446. 'value' => ''
  1447. ]],
  1448. ['input' => [
  1449. 'type' => 'hidden', 'name' => '_Token[debug]',
  1450. 'value' => $tokenDebug,
  1451. ]],
  1452. '/div'
  1453. ];
  1454. $this->assertHtml($expected, $result);
  1455. }
  1456. /**
  1457. * testFormSecurityArrayFields method
  1458. *
  1459. * Test form security with Model.field.0 style inputs.
  1460. *
  1461. * @return void
  1462. */
  1463. public function testFormSecurityArrayFields()
  1464. {
  1465. $this->Form->request->params['_Token'] = 'testKey';
  1466. $this->Form->create();
  1467. $this->Form->text('Address.primary.1');
  1468. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1469. $this->Form->text('Address.secondary.1.0');
  1470. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1471. }
  1472. /**
  1473. * testFormSecurityMultipleInputDisabledFields method
  1474. *
  1475. * Test secure form generation with multiple records and disabled fields.
  1476. *
  1477. * @return void
  1478. */
  1479. public function testFormSecurityMultipleInputDisabledFields()
  1480. {
  1481. $this->Form->request->params['_Token'] = [
  1482. 'unlockedFields' => ['first_name', 'address']
  1483. ];
  1484. $this->Form->create();
  1485. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1486. $this->Form->text('Addresses.0.title');
  1487. $this->Form->text('Addresses.0.first_name');
  1488. $this->Form->text('Addresses.0.last_name');
  1489. $this->Form->text('Addresses.0.address');
  1490. $this->Form->text('Addresses.0.city');
  1491. $this->Form->text('Addresses.0.phone');
  1492. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1493. $this->Form->text('Addresses.1.title');
  1494. $this->Form->text('Addresses.1.first_name');
  1495. $this->Form->text('Addresses.1.last_name');
  1496. $this->Form->text('Addresses.1.address');
  1497. $this->Form->text('Addresses.1.city');
  1498. $this->Form->text('Addresses.1.phone');
  1499. $result = $this->Form->secure($this->Form->fields);
  1500. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1501. $tokenDebug = urlencode(json_encode([
  1502. '/articles/add',
  1503. [
  1504. 'Addresses.0.id' => '123456',
  1505. 'Addresses.0.title',
  1506. 'Addresses.0.last_name',
  1507. 'Addresses.0.city',
  1508. 'Addresses.0.phone',
  1509. 'Addresses.1.id' => '654321',
  1510. 'Addresses.1.title',
  1511. 'Addresses.1.last_name',
  1512. 'Addresses.1.city',
  1513. 'Addresses.1.phone'
  1514. ],
  1515. [
  1516. 'first_name',
  1517. 'address'
  1518. ]
  1519. ]));
  1520. $expected = [
  1521. 'div' => ['style' => 'display:none;'],
  1522. ['input' => [
  1523. 'type' => 'hidden',
  1524. 'name' => '_Token[fields]',
  1525. 'value' => $hash
  1526. ]],
  1527. ['input' => [
  1528. 'type' => 'hidden',
  1529. 'name' => '_Token[unlocked]',
  1530. 'value' => 'address%7Cfirst_name',
  1531. ]],
  1532. ['input' => [
  1533. 'type' => 'hidden', 'name' => '_Token[debug]',
  1534. 'value' => $tokenDebug,
  1535. ]],
  1536. '/div'
  1537. ];
  1538. $this->assertHtml($expected, $result);
  1539. }
  1540. /**
  1541. * testFormSecurityInputDisabledFields method
  1542. *
  1543. * Test single record form with disabled fields.
  1544. *
  1545. * @return void
  1546. */
  1547. public function testFormSecurityInputUnlockedFields()
  1548. {
  1549. $this->Form->request['_Token'] = [
  1550. 'unlockedFields' => ['first_name', 'address']
  1551. ];
  1552. $this->Form->create();
  1553. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1554. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1555. $this->Form->text('Addresses.title');
  1556. $this->Form->text('Addresses.first_name');
  1557. $this->Form->text('Addresses.last_name');
  1558. $this->Form->text('Addresses.address');
  1559. $this->Form->text('Addresses.city');
  1560. $this->Form->text('Addresses.phone');
  1561. $result = $this->Form->fields;
  1562. $expected = [
  1563. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1564. 'Addresses.city', 'Addresses.phone'
  1565. ];
  1566. $this->assertEquals($expected, $result);
  1567. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1568. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1569. $tokenDebug = urlencode(json_encode([
  1570. '/articles/add',
  1571. [
  1572. 'Addresses.id' => '123456',
  1573. 'Addresses.title',
  1574. 'Addresses.last_name',
  1575. 'Addresses.city',
  1576. 'Addresses.phone'
  1577. ],
  1578. [
  1579. 'first_name',
  1580. 'address'
  1581. ]
  1582. ]));
  1583. $expected = [
  1584. 'div' => ['style' => 'display:none;'],
  1585. ['input' => [
  1586. 'type' => 'hidden',
  1587. 'name' => '_Token[fields]',
  1588. 'value' => $hash,
  1589. 'data-foo' => 'bar',
  1590. ]],
  1591. ['input' => [
  1592. 'type' => 'hidden',
  1593. 'name' => '_Token[unlocked]',
  1594. 'value' => 'address%7Cfirst_name',
  1595. 'data-foo' => 'bar',
  1596. ]],
  1597. ['input' => [
  1598. 'type' => 'hidden', 'name' => '_Token[debug]',
  1599. 'value' => $tokenDebug,
  1600. 'data-foo' => 'bar'
  1601. ]],
  1602. '/div'
  1603. ];
  1604. $this->assertHtml($expected, $result);
  1605. }
  1606. /**
  1607. * testFormSecurityInputUnlockedFieldsDebugSecurityTrue method
  1608. *
  1609. * Test single record form with debugSecurity param.
  1610. *
  1611. * @return void
  1612. */
  1613. public function testFormSecurityInputUnlockedFieldsDebugSecurityTrue()
  1614. {
  1615. $this->Form->request['_Token'] = [
  1616. 'unlockedFields' => ['first_name', 'address']
  1617. ];
  1618. $this->Form->create();
  1619. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1620. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1621. $this->Form->text('Addresses.title');
  1622. $this->Form->text('Addresses.first_name');
  1623. $this->Form->text('Addresses.last_name');
  1624. $this->Form->text('Addresses.address');
  1625. $this->Form->text('Addresses.city');
  1626. $this->Form->text('Addresses.phone');
  1627. $result = $this->Form->fields;
  1628. $expected = [
  1629. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1630. 'Addresses.city', 'Addresses.phone'
  1631. ];
  1632. $this->assertEquals($expected, $result);
  1633. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1634. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1635. $tokenDebug = urlencode(json_encode([
  1636. '/articles/add',
  1637. [
  1638. 'Addresses.id' => '123456',
  1639. 'Addresses.title',
  1640. 'Addresses.last_name',
  1641. 'Addresses.city',
  1642. 'Addresses.phone'
  1643. ],
  1644. [
  1645. 'first_name',
  1646. 'address'
  1647. ]
  1648. ]));
  1649. $expected = [
  1650. 'div' => ['style' => 'display:none;'],
  1651. ['input' => [
  1652. 'type' => 'hidden',
  1653. 'name' => '_Token[fields]',
  1654. 'value' => $hash,
  1655. 'data-foo' => 'bar',
  1656. ]],
  1657. ['input' => [
  1658. 'type' => 'hidden',
  1659. 'name' => '_Token[unlocked]',
  1660. 'value' => 'address%7Cfirst_name',
  1661. 'data-foo' => 'bar',
  1662. ]],
  1663. ['input' => [
  1664. 'type' => 'hidden', 'name' => '_Token[debug]',
  1665. 'value' => $tokenDebug,
  1666. 'data-foo' => 'bar'
  1667. ]],
  1668. '/div'
  1669. ];
  1670. $this->assertHtml($expected, $result);
  1671. }
  1672. /**
  1673. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1674. *
  1675. * Debug is false, debugSecurity is true -> no debug
  1676. *
  1677. * @return void
  1678. */
  1679. public function testFormSecurityInputUnlockedFieldsDebugSecurityDebugFalse()
  1680. {
  1681. $this->Form->request['_Token'] = [
  1682. 'unlockedFields' => ['first_name', 'address']
  1683. ];
  1684. $this->Form->create();
  1685. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1686. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1687. $this->Form->text('Addresses.title');
  1688. $this->Form->text('Addresses.first_name');
  1689. $this->Form->text('Addresses.last_name');
  1690. $this->Form->text('Addresses.address');
  1691. $this->Form->text('Addresses.city');
  1692. $this->Form->text('Addresses.phone');
  1693. $result = $this->Form->fields;
  1694. $expected = [
  1695. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1696. 'Addresses.city', 'Addresses.phone'
  1697. ];
  1698. $this->assertEquals($expected, $result);
  1699. Configure::write('debug', false);
  1700. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1701. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1702. $expected = [
  1703. 'div' => ['style' => 'display:none;'],
  1704. ['input' => [
  1705. 'type' => 'hidden',
  1706. 'name' => '_Token[fields]',
  1707. 'value' => $hash,
  1708. 'data-foo' => 'bar',
  1709. ]],
  1710. ['input' => [
  1711. 'type' => 'hidden',
  1712. 'name' => '_Token[unlocked]',
  1713. 'value' => 'address%7Cfirst_name',
  1714. 'data-foo' => 'bar',
  1715. ]],
  1716. '/div'
  1717. ];
  1718. $this->assertHtml($expected, $result);
  1719. }
  1720. /**
  1721. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1722. *
  1723. * Test single record form with debugSecurity param.
  1724. *
  1725. * @return void
  1726. */
  1727. public function testFormSecurityInputUnlockedFieldsDebugSecurityFalse()
  1728. {
  1729. $this->Form->request['_Token'] = [
  1730. 'unlockedFields' => ['first_name', 'address']
  1731. ];
  1732. $this->Form->create();
  1733. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1734. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1735. $this->Form->text('Addresses.title');
  1736. $this->Form->text('Addresses.first_name');
  1737. $this->Form->text('Addresses.last_name');
  1738. $this->Form->text('Addresses.address');
  1739. $this->Form->text('Addresses.city');
  1740. $this->Form->text('Addresses.phone');
  1741. $result = $this->Form->fields;
  1742. $expected = [
  1743. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1744. 'Addresses.city', 'Addresses.phone'
  1745. ];
  1746. $this->assertEquals($expected, $result);
  1747. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1748. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1749. $expected = [
  1750. 'div' => ['style' => 'display:none;'],
  1751. ['input' => [
  1752. 'type' => 'hidden',
  1753. 'name' => '_Token[fields]',
  1754. 'value' => $hash,
  1755. 'data-foo' => 'bar',
  1756. ]],
  1757. ['input' => [
  1758. 'type' => 'hidden',
  1759. 'name' => '_Token[unlocked]',
  1760. 'value' => 'address%7Cfirst_name',
  1761. 'data-foo' => 'bar',
  1762. ]],
  1763. '/div'
  1764. ];
  1765. $this->assertHtml($expected, $result);
  1766. }
  1767. /**
  1768. * testFormSecureWithCustomNameAttribute method
  1769. *
  1770. * Test securing inputs with custom name attributes.
  1771. *
  1772. * @return void
  1773. */
  1774. public function testFormSecureWithCustomNameAttribute()
  1775. {
  1776. $this->Form->request->params['_Token'] = 'testKey';
  1777. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1778. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1779. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1780. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1781. }
  1782. /**
  1783. * testFormSecuredInput method
  1784. *
  1785. * Test generation of entire secure form, assertions made on input() output.
  1786. *
  1787. * @return void
  1788. */
  1789. public function testFormSecuredInput()
  1790. {
  1791. $this->Form->request->params['_csrfToken'] = 'testKey';
  1792. $this->Form->request->params['_Token'] = 'stuff';
  1793. $this->article['schema'] = [
  1794. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1795. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1796. ];
  1797. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1798. $encoding = strtolower(Configure::read('App.encoding'));
  1799. $expected = [
  1800. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1801. 'div' => ['style' => 'display:none;'],
  1802. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1803. ['input' => [
  1804. 'type' => 'hidden',
  1805. 'name' => '_csrfToken',
  1806. 'value' => 'testKey'
  1807. ]],
  1808. '/div'
  1809. ];
  1810. $this->assertHtml($expected, $result);
  1811. $result = $this->Form->input('ratio');
  1812. $expected = [
  1813. 'div' => ['class'],
  1814. 'label' => ['for'],
  1815. 'Ratio',
  1816. '/label',
  1817. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1818. '/div',
  1819. ];
  1820. $this->assertHtml($expected, $result);
  1821. $result = $this->Form->input('population');
  1822. $expected = [
  1823. 'div' => ['class'],
  1824. 'label' => ['for'],
  1825. 'Population',
  1826. '/label',
  1827. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1828. '/div',
  1829. ];
  1830. $this->assertHtml($expected, $result);
  1831. $result = $this->Form->input('published', ['type' => 'text']);
  1832. $expected = [
  1833. 'div' => ['class' => 'input text'],
  1834. 'label' => ['for' => 'published'],
  1835. 'Published',
  1836. '/label',
  1837. ['input' => [
  1838. 'type' => 'text',
  1839. 'name' => 'published',
  1840. 'id' => 'published'
  1841. ]],
  1842. '/div'
  1843. ];
  1844. $this->assertHtml($expected, $result);
  1845. $result = $this->Form->input('other', ['type' => 'text']);
  1846. $expected = [
  1847. 'div' => ['class' => 'input text'],
  1848. 'label' => ['for' => 'other'],
  1849. 'Other',
  1850. '/label',
  1851. ['input' => [
  1852. 'type' => 'text',
  1853. 'name' => 'other',
  1854. 'id',
  1855. ]],
  1856. '/div'
  1857. ];
  1858. $this->assertHtml($expected, $result);
  1859. $result = $this->Form->hidden('stuff');
  1860. $expected = [
  1861. 'input' => [
  1862. 'type' => 'hidden',
  1863. 'name' => 'stuff'
  1864. ]
  1865. ];
  1866. $this->assertHtml($expected, $result);
  1867. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1868. $expected = ['input' => [
  1869. 'type' => 'hidden',
  1870. 'name' => 'hidden',
  1871. 'value' => '0'
  1872. ]];
  1873. $this->assertHtml($expected, $result);
  1874. $result = $this->Form->input('something', ['type' => 'checkbox']);
  1875. $expected = [
  1876. 'div' => ['class' => 'input checkbox'],
  1877. ['input' => [
  1878. 'type' => 'hidden',
  1879. 'name' => 'something',
  1880. 'value' => '0'
  1881. ]],
  1882. 'label' => ['for' => 'something'],
  1883. ['input' => [
  1884. 'type' => 'checkbox',
  1885. 'name' => 'something',
  1886. 'value' => '1',
  1887. 'id' => 'something'
  1888. ]],
  1889. 'Something',
  1890. '/label',
  1891. '/div'
  1892. ];
  1893. $this->assertHtml($expected, $result);
  1894. $result = $this->Form->fields;
  1895. $expectedFields = [
  1896. 'ratio',
  1897. 'population',
  1898. 'published',
  1899. 'other',
  1900. 'stuff' => '',
  1901. 'hidden' => '0',
  1902. 'something'
  1903. ];
  1904. $this->assertEquals($expectedFields, $result);
  1905. $result = $this->Form->secure($this->Form->fields);
  1906. $tokenDebug = urlencode(json_encode([
  1907. '/articles/add',
  1908. $expectedFields,
  1909. []
  1910. ]));
  1911. $expected = [
  1912. 'div' => ['style' => 'display:none;'],
  1913. ['input' => [
  1914. 'type' => 'hidden',
  1915. 'name' => '_Token[fields]',
  1916. 'value'
  1917. ]],
  1918. ['input' => [
  1919. 'type' => 'hidden',
  1920. 'name' => '_Token[unlocked]',
  1921. 'value' => ''
  1922. ]],
  1923. ['input' => [
  1924. 'type' => 'hidden', 'name' => '_Token[debug]',
  1925. 'value' => $tokenDebug,
  1926. ]],
  1927. '/div'
  1928. ];
  1929. $this->assertHtml($expected, $result);
  1930. }
  1931. /**
  1932. * testSecuredInputCustomName method
  1933. *
  1934. * Test secured inputs with custom names.
  1935. *
  1936. * @return void
  1937. */
  1938. public function testSecuredInputCustomName()
  1939. {
  1940. $this->Form->request->params['_Token'] = 'testKey';
  1941. $this->assertEquals([], $this->Form->fields);
  1942. $this->Form->text('text_input', [
  1943. 'name' => 'Option[General.default_role]',
  1944. ]);
  1945. $expected = ['Option.General.default_role'];
  1946. $this->assertEquals($expected, $this->Form->fields);
  1947. $this->Form->select('select_box', [1, 2], [
  1948. 'name' => 'Option[General.select_role]',
  1949. ]);
  1950. $expected[] = 'Option.General.select_role';
  1951. $this->assertEquals($expected, $this->Form->fields);
  1952. $this->Form->text('other.things[]');
  1953. $expected[] = 'other.things';
  1954. $this->assertEquals($expected, $this->Form->fields);
  1955. }
  1956. /**
  1957. * testSecuredInputDuplicate method
  1958. *
  1959. * Test that a hidden field followed by a visible field
  1960. * undoes the hidden field locking.
  1961. *
  1962. * @return void
  1963. */
  1964. public function testSecuredInputDuplicate()
  1965. {
  1966. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1967. $this->assertEquals([], $this->Form->fields);
  1968. $this->Form->input('text_val', [
  1969. 'type' => 'hidden',
  1970. 'value' => 'some text',
  1971. ]);
  1972. $expected = ['text_val' => 'some text'];
  1973. $this->assertEquals($expected, $this->Form->fields);
  1974. $this->Form->input('text_val', [
  1975. 'type' => 'text',
  1976. ]);
  1977. $expected = ['text_val'];
  1978. $this->assertEquals($expected, $this->Form->fields);
  1979. }
  1980. /**
  1981. * testFormSecuredFileInput method
  1982. *
  1983. * Tests that the correct keys are added to the field hash index.
  1984. *
  1985. * @return void
  1986. */
  1987. public function testFormSecuredFileInput()
  1988. {
  1989. $this->assertEquals([], $this->Form->fields);
  1990. $this->Form->file('Attachment.file');
  1991. $expected = [
  1992. 'Attachment.file.name', 'Attachment.file.type',
  1993. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1994. 'Attachment.file.size'
  1995. ];
  1996. $this->assertEquals($expected, $this->Form->fields);
  1997. }
  1998. /**
  1999. * testFormSecuredMultipleSelect method
  2000. *
  2001. * Test that multiple selects keys are added to field hash.
  2002. *
  2003. * @return void
  2004. */
  2005. public function testFormSecuredMultipleSelect()
  2006. {
  2007. $this->Form->request->params['_csrfToken'] = 'testKey';
  2008. $this->assertEquals([], $this->Form->fields);
  2009. $options = ['1' => 'one', '2' => 'two'];
  2010. $this->Form->select('Model.select', $options);
  2011. $expected = ['Model.select'];
  2012. $this->assertEquals($expected, $this->Form->fields);
  2013. $this->Form->fields = [];
  2014. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2015. $this->assertEquals($expected, $this->Form->fields);
  2016. }
  2017. /**
  2018. * testFormSecuredRadio method
  2019. *
  2020. * @return void
  2021. */
  2022. public function testFormSecuredRadio()
  2023. {
  2024. $this->Form->request->params['_Token'] = 'testKey';
  2025. $this->assertEquals([], $this->Form->fields);
  2026. $options = ['1' => 'option1', '2' => 'option2'];
  2027. $this->Form->radio('Test.test', $options);
  2028. $expected = ['Test.test'];
  2029. $this->assertEquals($expected, $this->Form->fields);
  2030. $this->Form->radio('Test.all', $options, [
  2031. 'disabled' => ['option1', 'option2']
  2032. ]);
  2033. $expected = ['Test.test', 'Test.all' => ''];
  2034. $this->assertEquals($expected, $this->Form->fields);
  2035. $this->Form->radio('Test.some', $options, [
  2036. 'disabled' => ['option1']
  2037. ]);
  2038. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2039. $this->assertEquals($expected, $this->Form->fields);
  2040. }
  2041. /**
  2042. * testFormSecuredAndDisabledNotAssoc method
  2043. *
  2044. * Test that when disabled is in a list based attribute array it works.
  2045. *
  2046. * @return void
  2047. */
  2048. public function testFormSecuredAndDisabledNotAssoc()
  2049. {
  2050. $this->Form->request->params['_Token'] = 'testKey';
  2051. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2052. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2053. $this->Form->text('Model.text', ['disabled']);
  2054. $this->Form->textarea('Model.textarea', ['disabled']);
  2055. $this->Form->password('Model.password', ['disabled']);
  2056. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2057. $expected = [
  2058. 'Model.radio' => ''
  2059. ];
  2060. $this->assertEquals($expected, $this->Form->fields);
  2061. }
  2062. /**
  2063. * testFormSecuredAndDisabled method
  2064. *
  2065. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2066. * hashing.
  2067. *
  2068. * @return void
  2069. */
  2070. public function testFormSecuredAndDisabled()
  2071. {
  2072. $this->Form->request->params['_Token'] = 'testKey';
  2073. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2074. $this->Form->text('Model.text', ['disabled' => true]);
  2075. $this->Form->password('Model.text', ['disabled' => true]);
  2076. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2077. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2078. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2079. $this->Form->year('Model.year', ['disabled' => true]);
  2080. $this->Form->month('Model.month', ['disabled' => true]);
  2081. $this->Form->day('Model.day', ['disabled' => true]);
  2082. $this->Form->hour('Model.hour', ['disabled' => true]);
  2083. $this->Form->minute('Model.minute', ['disabled' => true]);
  2084. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2085. $expected = [
  2086. 'Model.radio' => ''
  2087. ];
  2088. $this->assertEquals($expected, $this->Form->fields);
  2089. }
  2090. /**
  2091. * testDisableSecurityUsingForm method
  2092. *
  2093. * @return void
  2094. */
  2095. public function testDisableSecurityUsingForm()
  2096. {
  2097. $this->Form->request['_Token'] = [
  2098. 'disabledFields' => []
  2099. ];
  2100. $this->Form->create();
  2101. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2102. $this->Form->text('Addresses.title');
  2103. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2104. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2105. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2106. $result = $this->Form->fields;
  2107. $expected = [
  2108. 'Addresses.id' => '123456', 'Addresses.title',
  2109. ];
  2110. $this->assertEquals($expected, $result);
  2111. }
  2112. /**
  2113. * testUnlockFieldAddsToList method
  2114. *
  2115. * Test disableField.
  2116. *
  2117. * @return void
  2118. */
  2119. public function testUnlockFieldAddsToList()
  2120. {
  2121. $this->Form->request['_Token'] = [
  2122. 'unlockedFields' => []
  2123. ];
  2124. $this->Form->unlockField('Contact.name');
  2125. $this->Form->text('Contact.name');
  2126. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2127. $this->assertEquals([], $this->Form->fields);
  2128. }
  2129. /**
  2130. * testUnlockFieldRemovingFromFields method
  2131. *
  2132. * Test unlockField removing from fields array.
  2133. *
  2134. * @return void
  2135. */
  2136. public function testUnlockFieldRemovingFromFields()
  2137. {
  2138. $this->Form->request['_Token'] = [
  2139. 'unlockedFields' => []
  2140. ];
  2141. $this->Form->create($this->article);
  2142. $this->Form->hidden('Article.id', ['value' => 1]);
  2143. $this->Form->text('Article.title');
  2144. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2145. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2146. $this->Form->unlockField('Article.title');
  2147. $this->Form->unlockField('Article.id');
  2148. $this->assertEquals([], $this->Form->fields);
  2149. }
  2150. /**
  2151. * testResetUnlockFields method
  2152. *
  2153. * Test reset unlockFields, when create new form.
  2154. *
  2155. * @return void
  2156. */
  2157. public function testResetUnlockFields()
  2158. {
  2159. $this->Form->request['_Token'] = [
  2160. 'key' => 'testKey',
  2161. 'unlockedFields' => []
  2162. ];
  2163. $this->Form->unlockField('Contact.id');
  2164. $this->Form->create('Contact');
  2165. $this->Form->hidden('Contact.id', ['value' => 1]);
  2166. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2167. $this->Form->end();
  2168. $this->Form->create('Contact');
  2169. $this->Form->hidden('Contact.id', ['value' => 1]);
  2170. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2171. }
  2172. /**
  2173. * testSecuredFormUrlIgnoresHost method
  2174. *
  2175. * Test that only the path + query elements of a form's URL show up in their hash.
  2176. *
  2177. * @return void
  2178. */
  2179. public function testSecuredFormUrlIgnoresHost()
  2180. {
  2181. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2182. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2183. $this->Form->create($this->article, [
  2184. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2185. ]);
  2186. $result = $this->Form->secure();
  2187. $this->assertContains($expected, $result);
  2188. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2189. $result = $this->Form->secure();
  2190. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2191. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2192. $result = $this->Form->secure();
  2193. $this->assertContains($expected, $result, 'URL path + query should work.');
  2194. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2195. $result = $this->Form->secure();
  2196. $this->assertNotContains($expected, $result, 'URL is different');
  2197. }
  2198. /**
  2199. * testSecuredFormUrlHasHtmlAndIdentifier method
  2200. *
  2201. * Test that URL, HTML and identifier show up in their hashes.
  2202. *
  2203. * @return void
  2204. */
  2205. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2206. {
  2207. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2208. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2209. $this->Form->create($this->article, [
  2210. 'url' => [
  2211. 'controller' => 'articles',
  2212. 'action' => 'view',
  2213. '?' => [
  2214. 'page' => 1,
  2215. 'limit' => 10,
  2216. 'html' => '<>"',
  2217. ],
  2218. '#' => 'result',
  2219. ],
  2220. ]);
  2221. $result = $this->Form->secure();
  2222. $this->assertContains($expected, $result);
  2223. $this->Form->create($this->article, [
  2224. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2225. ]);
  2226. $result = $this->Form->secure();
  2227. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2228. $this->Form->create($this->article, [
  2229. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2230. ]);
  2231. $result = $this->Form->secure();
  2232. $this->assertContains($expected, $result, 'URL path + query should work.');
  2233. }
  2234. /**
  2235. * testErrorMessageDisplay method
  2236. *
  2237. * Test error message display.
  2238. *
  2239. * @return void
  2240. */
  2241. public function testErrorMessageDisplay()
  2242. {
  2243. $this->article['errors'] = [
  2244. 'Article' => [
  2245. 'title' => 'error message',
  2246. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2247. ]
  2248. ];
  2249. $this->Form->create($this->article);
  2250. $result = $this->Form->input('Article.title');
  2251. $expected = [
  2252. 'div' => ['class' => 'input text error'],
  2253. 'label' => ['for' => 'article-title'],
  2254. 'Title',
  2255. '/label',
  2256. 'input' => [
  2257. 'type' => 'text', 'name' => 'Article[title]',
  2258. 'id' => 'article-title', 'class' => 'form-error'
  2259. ],
  2260. ['div' => ['class' => 'error-message']],
  2261. 'error message',
  2262. '/div',
  2263. '/div'
  2264. ];
  2265. $this->assertHtml($expected, $result);
  2266. $result = $this->Form->input('Article.title', [
  2267. 'templates' => [
  2268. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2269. ]
  2270. ]);
  2271. $expected = [
  2272. 'div' => ['class' => 'input text error'],
  2273. 'label' => ['for' => 'article-title'],
  2274. 'Title',
  2275. '/label',
  2276. 'input' => [
  2277. 'type' => 'text', 'name' => 'Article[title]',
  2278. 'id' => 'article-title', 'class' => 'form-error'
  2279. ],
  2280. '/div'
  2281. ];
  2282. $this->assertHtml($expected, $result);
  2283. $result = $this->Form->input('Article.content');
  2284. $expected = [
  2285. 'div' => ['class' => 'input text error'],
  2286. 'label' => ['for' => 'article-content'],
  2287. 'Content',
  2288. '/label',
  2289. 'input' => [
  2290. 'type' => 'text', 'name' => 'Article[content]',
  2291. 'id' => 'article-content', 'class' => 'form-error'
  2292. ],
  2293. ['div' => ['class' => 'error-message']],
  2294. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2295. '/div',
  2296. '/div'
  2297. ];
  2298. $this->assertHtml($expected, $result);
  2299. $result = $this->Form->input('Article.content', ['error' => ['escape' => true]]);
  2300. $expected = [
  2301. 'div' => ['class' => 'input text error'],
  2302. 'label' => ['for' => 'article-content'],
  2303. 'Content',
  2304. '/label',
  2305. 'input' => [
  2306. 'type' => 'text', 'name' => 'Article[content]',
  2307. 'id' => 'article-content', 'class' => 'form-error'
  2308. ],
  2309. ['div' => ['class' => 'error-message']],
  2310. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2311. '/div',
  2312. '/div'
  2313. ];
  2314. $this->assertHtml($expected, $result);
  2315. $result = $this->Form->input('Article.content', ['error' => ['escape' => false]]);
  2316. $expected = [
  2317. 'div' => ['class' => 'input text error'],
  2318. 'label' => ['for' => 'article-content'],
  2319. 'Content',
  2320. '/label',
  2321. 'input' => [
  2322. 'type' => 'text', 'name' => 'Article[content]',
  2323. 'id' => 'article-content', 'class' => 'form-error'
  2324. ],
  2325. ['div' => ['class' => 'error-message']],
  2326. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2327. '/div',
  2328. '/div'
  2329. ];
  2330. $this->assertHtml($expected, $result);
  2331. }
  2332. /**
  2333. * testEmptyErrorValidation method
  2334. *
  2335. * Test validation errors, when validation message is an empty string.
  2336. *
  2337. * @return void
  2338. */
  2339. public function testEmptyErrorValidation()
  2340. {
  2341. $this->article['errors'] = [
  2342. 'Article' => ['title' => '']
  2343. ];
  2344. $this->Form->create($this->article);
  2345. $result = $this->Form->input('Article.title');
  2346. $expected = [
  2347. 'div' => ['class' => 'input text error'],
  2348. 'label' => ['for' => 'article-title'],
  2349. 'Title',
  2350. '/label',
  2351. 'input' => [
  2352. 'type' => 'text', 'name' => 'Article[title]',
  2353. 'id' => 'article-title', 'class' => 'form-error'
  2354. ],
  2355. ['div' => ['class' => 'error-message']],
  2356. [],
  2357. '/div',
  2358. '/div'
  2359. ];
  2360. $this->assertHtml($expected, $result);
  2361. }
  2362. /**
  2363. * testEmptyInputErrorValidation method
  2364. *
  2365. * Test validation errors, when calling input() overriding validation message by an empty string.
  2366. *
  2367. * @return void
  2368. */
  2369. public function testEmptyInputErrorValidation()
  2370. {
  2371. $this->article['errors'] = [
  2372. 'Article' => ['title' => 'error message']
  2373. ];
  2374. $this->Form->create($this->article);
  2375. $result = $this->Form->input('Article.title', ['error' => '']);
  2376. $expected = [
  2377. 'div' => ['class' => 'input text error'],
  2378. 'label' => ['for' => 'article-title'],
  2379. 'Title',
  2380. '/label',
  2381. 'input' => [
  2382. 'type' => 'text', 'name' => 'Article[title]',
  2383. 'id' => 'article-title', 'class' => 'form-error'
  2384. ],
  2385. ['div' => ['class' => 'error-message']],
  2386. [],
  2387. '/div',
  2388. '/div'
  2389. ];
  2390. $this->assertHtml($expected, $result);
  2391. }
  2392. /**
  2393. * testInputErrorMessage method
  2394. *
  2395. * Test validation errors, when calling input() overriding validation messages.
  2396. *
  2397. * @return void
  2398. */
  2399. public function testInputErrorMessage()
  2400. {
  2401. $this->article['errors'] = [
  2402. 'title' => ['error message']
  2403. ];
  2404. $this->Form->create($this->article);
  2405. $result = $this->Form->input('title', [
  2406. 'error' => 'Custom error!'
  2407. ]);
  2408. $expected = [
  2409. 'div' => ['class' => 'input text required error'],
  2410. 'label' => ['for' => 'title'],
  2411. 'Title',
  2412. '/label',
  2413. 'input' => [
  2414. 'type' => 'text', 'name' => 'title',
  2415. 'id' => 'title', 'class' => 'form-error',
  2416. 'required' => 'required',
  2417. ],
  2418. ['div' => ['class' => 'error-message']],
  2419. 'Custom error!',
  2420. '/div',
  2421. '/div'
  2422. ];
  2423. $this->assertHtml($expected, $result);
  2424. $result = $this->Form->input('title', [
  2425. 'error' => ['error message' => 'Custom error!']
  2426. ]);
  2427. $expected = [
  2428. 'div' => ['class' => 'input text required error'],
  2429. 'label' => ['for' => 'title'],
  2430. 'Title',
  2431. '/label',
  2432. 'input' => [
  2433. 'type' => 'text',
  2434. 'name' => 'title',
  2435. 'id' => 'title',
  2436. 'class' => 'form-error',
  2437. 'required' => 'required'
  2438. ],
  2439. ['div' => ['class' => 'error-message']],
  2440. 'Custom error!',
  2441. '/div',
  2442. '/div'
  2443. ];
  2444. $this->assertHtml($expected, $result);
  2445. }
  2446. /**
  2447. * testFormValidationAssociated method
  2448. *
  2449. * Tests displaying errors for nested entities.
  2450. *
  2451. * @return void
  2452. */
  2453. public function testFormValidationAssociated()
  2454. {
  2455. $nested = new Entity(['foo' => 'bar']);
  2456. $nested->errors('foo', ['not a valid bar']);
  2457. $entity = new Entity(['nested' => $nested]);
  2458. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2459. $result = $this->Form->error('nested.foo');
  2460. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2461. }
  2462. /**
  2463. * testFormValidationAssociatedSecondLevel method
  2464. *
  2465. * Test form error display with associated model.
  2466. *
  2467. * @return void
  2468. */
  2469. public function testFormValidationAssociatedSecondLevel()
  2470. {
  2471. $inner = new Entity(['bar' => 'baz']);
  2472. $nested = new Entity(['foo' => $inner]);
  2473. $entity = new Entity(['nested' => $nested]);
  2474. $inner->errors('bar', ['not a valid one']);
  2475. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2476. $result = $this->Form->error('nested.foo.bar');
  2477. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2478. }
  2479. /**
  2480. * testFormValidationMultiRecord method
  2481. *
  2482. * Test form error display with multiple records.
  2483. *
  2484. * @return void
  2485. */
  2486. public function testFormValidationMultiRecord()
  2487. {
  2488. $one = new Entity();
  2489. $two = new Entity();
  2490. TableRegistry::get('Contacts', [
  2491. 'className' => __NAMESPACE__ . '\ContactsTable'
  2492. ]);
  2493. $one->set('email', '');
  2494. $one->errors('email', ['invalid email']);
  2495. $two->set('name', '');
  2496. $two->errors('name', ['This is wrong']);
  2497. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2498. $result = $this->Form->input('0.email');
  2499. $expected = [
  2500. 'div' => ['class' => 'input email error'],
  2501. 'label' => ['for' => '0-email'],
  2502. 'Email',
  2503. '/label',
  2504. 'input' => [
  2505. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2506. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2507. ],
  2508. ['div' => ['class' => 'error-message']],
  2509. 'invalid email',
  2510. '/div',
  2511. '/div'
  2512. ];
  2513. $this->assertHtml($expected, $result);
  2514. $result = $this->Form->input('1.name');
  2515. $expected = [
  2516. 'div' => ['class' => 'input text error'],
  2517. 'label' => ['for' => '1-name'],
  2518. 'Name',
  2519. '/label',
  2520. 'input' => [
  2521. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2522. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2523. ],
  2524. ['div' => ['class' => 'error-message']],
  2525. 'This is wrong',
  2526. '/div',
  2527. '/div'
  2528. ];
  2529. $this->assertHtml($expected, $result);
  2530. }
  2531. /**
  2532. * testInput method
  2533. *
  2534. * Test various incarnations of input().
  2535. *
  2536. * @return void
  2537. */
  2538. public function testInput()
  2539. {
  2540. TableRegistry::get('ValidateUsers', [
  2541. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2542. ]);
  2543. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2544. $result = $this->Form->input('ValidateUsers.balance');
  2545. $expected = [
  2546. 'div' => ['class'],
  2547. 'label' => ['for'],
  2548. 'Balance',
  2549. '/label',
  2550. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2551. '/div',
  2552. ];
  2553. $this->assertHtml($expected, $result);
  2554. $result = $this->Form->input('ValidateUser.cost_decimal');
  2555. $expected = [
  2556. 'div' => ['class'],
  2557. 'label' => ['for'],
  2558. 'Cost Decimal',
  2559. '/label',
  2560. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2561. '/div',
  2562. ];
  2563. $this->assertHtml($expected, $result);
  2564. $result = $this->Form->input('ValidateUser.null_decimal');
  2565. $expected = [
  2566. 'div' => ['class'],
  2567. 'label' => ['for'],
  2568. 'Null Decimal',
  2569. '/label',
  2570. 'input' => ['name', 'type' => 'number', 'id'],
  2571. '/div',
  2572. ];
  2573. $this->assertHtml($expected, $result);
  2574. }
  2575. /**
  2576. * testInputCustomization method
  2577. *
  2578. * Tests the input method and passing custom options.
  2579. *
  2580. * @return void
  2581. */
  2582. public function testInputCustomization()
  2583. {
  2584. TableRegistry::get('Contacts', [
  2585. 'className' => __NAMESPACE__ . '\ContactsTable'
  2586. ]);
  2587. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2588. $result = $this->Form->input('Contact.email', ['id' => 'custom']);
  2589. $expected = [
  2590. 'div' => ['class' => 'input email'],
  2591. 'label' => ['for' => 'custom'],
  2592. 'Email',
  2593. '/label',
  2594. ['input' => [
  2595. 'type' => 'email', 'name' => 'Contact[email]',
  2596. 'id' => 'custom', 'maxlength' => 255
  2597. ]],
  2598. '/div'
  2599. ];
  2600. $this->assertHtml($expected, $result);
  2601. $result = $this->Form->input('Contact.email', [
  2602. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2603. ]);
  2604. $expected = [
  2605. '<div',
  2606. 'label' => ['for' => 'contact-email'],
  2607. 'Email',
  2608. '/label',
  2609. ['input' => [
  2610. 'type' => 'email', 'name' => 'Contact[email]',
  2611. 'id' => 'contact-email', 'maxlength' => 255
  2612. ]],
  2613. '/div'
  2614. ];
  2615. $this->assertHtml($expected, $result);
  2616. $result = $this->Form->input('Contact.email', ['type' => 'text']);
  2617. $expected = [
  2618. 'div' => ['class' => 'input text'],
  2619. 'label' => ['for' => 'contact-email'],
  2620. 'Email',
  2621. '/label',
  2622. ['input' => [
  2623. 'type' => 'text', 'name' => 'Contact[email]',
  2624. 'id' => 'contact-email', 'maxlength' => '255'
  2625. ]],
  2626. '/div'
  2627. ];
  2628. $this->assertHtml($expected, $result);
  2629. $result = $this->Form->input('Contact.5.email', ['type' => 'text']);
  2630. $expected = [
  2631. 'div' => ['class' => 'input text'],
  2632. 'label' => ['for' => 'contact-5-email'],
  2633. 'Email',
  2634. '/label',
  2635. ['input' => [
  2636. 'type' => 'text', 'name' => 'Contact[5][email]',
  2637. 'id' => 'contact-5-email', 'maxlength' => '255'
  2638. ]],
  2639. '/div'
  2640. ];
  2641. $this->assertHtml($expected, $result);
  2642. $result = $this->Form->input('Contact.password');
  2643. $expected = [
  2644. 'div' => ['class' => 'input password'],
  2645. 'label' => ['for' => 'contact-password'],
  2646. 'Password',
  2647. '/label',
  2648. ['input' => [
  2649. 'type' => 'password', 'name' => 'Contact[password]',
  2650. 'id' => 'contact-password'
  2651. ]],
  2652. '/div'
  2653. ];
  2654. $this->assertHtml($expected, $result);
  2655. $result = $this->Form->input('Contact.email', [
  2656. 'type' => 'file', 'class' => 'textbox'
  2657. ]);
  2658. $expected = [
  2659. 'div' => ['class' => 'input file'],
  2660. 'label' => ['for' => 'contact-email'],
  2661. 'Email',
  2662. '/label',
  2663. ['input' => [
  2664. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2665. 'id' => 'contact-email'
  2666. ]],
  2667. '/div'
  2668. ];
  2669. $this->assertHtml($expected, $result);
  2670. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2671. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2672. $result = $this->Form->input('phone');
  2673. $expected = [
  2674. 'div' => ['class' => 'input tel'],
  2675. 'label' => ['for' => 'phone'],
  2676. 'Phone',
  2677. '/label',
  2678. ['input' => [
  2679. 'type' => 'tel', 'name' => 'phone',
  2680. 'value' => 'Hello &amp; World &gt; weird chars',
  2681. 'id' => 'phone', 'maxlength' => 255
  2682. ]],
  2683. '/div'
  2684. ];
  2685. $this->assertHtml($expected, $result);
  2686. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2687. $this->Form->create();
  2688. $result = $this->Form->input('Model.0.OtherModel.field', ['id' => 'myId']);
  2689. $expected = [
  2690. 'div' => ['class' => 'input text'],
  2691. 'label' => ['for' => 'myId'],
  2692. 'Field',
  2693. '/label',
  2694. 'input' => [
  2695. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2696. 'value' => 'My value', 'id' => 'myId'
  2697. ],
  2698. '/div'
  2699. ];
  2700. $this->assertHtml($expected, $result);
  2701. $this->Form->request->data = [];
  2702. $entity->errors('field', 'Badness!');
  2703. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2704. $result = $this->Form->input('field');
  2705. $expected = [
  2706. 'div' => ['class' => 'input text error'],
  2707. 'label' => ['for' => 'field'],
  2708. 'Field',
  2709. '/label',
  2710. 'input' => [
  2711. 'type' => 'text', 'name' => 'field',
  2712. 'id' => 'field', 'class' => 'form-error'
  2713. ],
  2714. ['div' => ['class' => 'error-message']],
  2715. 'Badness!',
  2716. '/div',
  2717. '/div'
  2718. ];
  2719. $this->assertHtml($expected, $result);
  2720. $result = $this->Form->input('field', [
  2721. 'templates' => [
  2722. 'inputContainerError' => '{{content}}{{error}}',
  2723. 'error' => '<span class="error-message">{{content}}</span>'
  2724. ]
  2725. ]);
  2726. $expected = [
  2727. 'label' => ['for' => 'field'],
  2728. 'Field',
  2729. '/label',
  2730. 'input' => [
  2731. 'type' => 'text', 'name' => 'field',
  2732. 'id' => 'field', 'class' => 'form-error'
  2733. ],
  2734. ['span' => ['class' => 'error-message']],
  2735. 'Badness!',
  2736. '/span'
  2737. ];
  2738. $this->assertHtml($expected, $result);
  2739. $entity->errors('field', ['minLength'], true);
  2740. $result = $this->Form->input('field', [
  2741. 'error' => [
  2742. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2743. 'maxLength' => 'login too large'
  2744. ]
  2745. ]);
  2746. $expected = [
  2747. 'div' => ['class' => 'input text error'],
  2748. 'label' => ['for' => 'field'],
  2749. 'Field',
  2750. '/label',
  2751. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2752. ['div' => ['class' => 'error-message']],
  2753. 'Le login doit contenir au moins 2 caractères',
  2754. '/div',
  2755. '/div'
  2756. ];
  2757. $this->assertHtml($expected, $result);
  2758. $entity->errors('field', ['maxLength'], true);
  2759. $result = $this->Form->input('field', [
  2760. 'error' => [
  2761. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2762. 'maxLength' => 'login too large',
  2763. ]
  2764. ]);
  2765. $expected = [
  2766. 'div' => ['class' => 'input text error'],
  2767. 'label' => ['for' => 'field'],
  2768. 'Field',
  2769. '/label',
  2770. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2771. ['div' => ['class' => 'error-message']],
  2772. 'login too large',
  2773. '/div',
  2774. '/div'
  2775. ];
  2776. $this->assertHtml($expected, $result);
  2777. }
  2778. /**
  2779. * testInputWithTemplateFile method
  2780. *
  2781. * Test that input() accepts a template file.
  2782. *
  2783. * @return void
  2784. */
  2785. public function testInputWithTemplateFile()
  2786. {
  2787. $result = $this->Form->input('field', [
  2788. 'templates' => 'htmlhelper_tags'
  2789. ]);
  2790. $expected = [
  2791. 'label' => ['for' => 'field'],
  2792. 'Field',
  2793. '/label',
  2794. 'input' => [
  2795. 'type' => 'text', 'name' => 'field',
  2796. 'id' => 'field'
  2797. ],
  2798. ];
  2799. $this->assertHtml($expected, $result);
  2800. }
  2801. /**
  2802. * testNestedInputsEndWithBrackets method
  2803. *
  2804. * Test that nested inputs end with brackets.
  2805. *
  2806. * @return void
  2807. */
  2808. public function testNestedInputsEndWithBrackets()
  2809. {
  2810. $result = $this->Form->text('nested.text[]');
  2811. $expected = [
  2812. 'input' => [
  2813. 'type' => 'text', 'name' => 'nested[text][]'
  2814. ],
  2815. ];
  2816. $this->assertHtml($expected, $result);
  2817. $result = $this->Form->file('nested.file[]');
  2818. $expected = [
  2819. 'input' => [
  2820. 'type' => 'file', 'name' => 'nested[file][]'
  2821. ],
  2822. ];
  2823. $this->assertHtml($expected, $result);
  2824. }
  2825. /**
  2826. * testCreateIdPrefix method
  2827. *
  2828. * Test id prefix.
  2829. *
  2830. * @return void
  2831. */
  2832. public function testCreateIdPrefix()
  2833. {
  2834. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2835. $result = $this->Form->input('field');
  2836. $expected = [
  2837. 'div' => ['class' => 'input text'],
  2838. 'label' => ['for' => 'prefix-field'],
  2839. 'Field',
  2840. '/label',
  2841. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2842. '/div'
  2843. ];
  2844. $this->assertHtml($expected, $result);
  2845. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2846. $expected = [
  2847. 'div' => ['class' => 'input text'],
  2848. 'label' => ['for' => 'custom-id'],
  2849. 'Field',
  2850. '/label',
  2851. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2852. '/div'
  2853. ];
  2854. $this->assertHtml($expected, $result);
  2855. $result = $this->Form->radio('Model.field', ['option A']);
  2856. $expected = [
  2857. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2858. 'label' => ['for' => 'prefix-model-field-0'],
  2859. ['input' => [
  2860. 'type' => 'radio',
  2861. 'name' => 'Model[field]',
  2862. 'value' => '0',
  2863. 'id' => 'prefix-model-field-0'
  2864. ]],
  2865. 'option A',
  2866. '/label'
  2867. ];
  2868. $this->assertHtml($expected, $result);
  2869. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2870. $expected = [
  2871. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2872. 'label' => ['for' => 'prefix-model-field-0'],
  2873. ['input' => [
  2874. 'type' => 'radio',
  2875. 'name' => 'Model[field]',
  2876. 'value' => '0',
  2877. 'id' => 'prefix-model-field-0'
  2878. ]],
  2879. 'option A',
  2880. '/label'
  2881. ];
  2882. $this->assertHtml($expected, $result);
  2883. $result = $this->Form->select(
  2884. 'Model.multi_field',
  2885. ['first'],
  2886. ['multiple' => 'checkbox']
  2887. );
  2888. $expected = [
  2889. 'input' => [
  2890. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2891. ],
  2892. ['div' => ['class' => 'checkbox']],
  2893. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2894. ['input' => [
  2895. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2896. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2897. ]],
  2898. 'first',
  2899. '/label',
  2900. '/div',
  2901. ];
  2902. $this->assertHtml($expected, $result);
  2903. $this->Form->end();
  2904. $result = $this->Form->input('field');
  2905. $expected = [
  2906. 'div' => ['class' => 'input text'],
  2907. 'label' => ['for' => 'field'],
  2908. 'Field',
  2909. '/label',
  2910. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2911. '/div'
  2912. ];
  2913. $this->assertHtml($expected, $result);
  2914. }
  2915. /**
  2916. * testInputZero method
  2917. *
  2918. * Test that inputs with 0 can be created.
  2919. *
  2920. * @return void
  2921. */
  2922. public function testInputZero()
  2923. {
  2924. TableRegistry::get('Contacts', [
  2925. 'className' => __NAMESPACE__ . '\ContactsTable'
  2926. ]);
  2927. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2928. $result = $this->Form->input('0');
  2929. $expected = [
  2930. 'div' => ['class' => 'input text'],
  2931. 'label' => ['for' => '0'], '/label',
  2932. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2933. '/div'
  2934. ];
  2935. $this->assertHtml($expected, $result);
  2936. }
  2937. /**
  2938. * testInputCheckbox method
  2939. *
  2940. * Test input() with checkbox creation.
  2941. *
  2942. * @return void
  2943. */
  2944. public function testInputCheckbox()
  2945. {
  2946. $result = $this->Form->input('User.active', ['label' => false, 'checked' => true]);
  2947. $expected = [
  2948. 'div' => ['class' => 'input checkbox'],
  2949. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2950. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2951. '/div'
  2952. ];
  2953. $this->assertHtml($expected, $result);
  2954. $result = $this->Form->input('User.active', ['label' => false, 'checked' => 1]);
  2955. $expected = [
  2956. 'div' => ['class' => 'input checkbox'],
  2957. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2958. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2959. '/div'
  2960. ];
  2961. $this->assertHtml($expected, $result);
  2962. $result = $this->Form->input('User.active', ['label' => false, 'checked' => '1']);
  2963. $expected = [
  2964. 'div' => ['class' => 'input checkbox'],
  2965. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2966. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2967. '/div'
  2968. ];
  2969. $this->assertHtml($expected, $result);
  2970. $result = $this->Form->input('User.disabled', [
  2971. 'label' => 'Disabled',
  2972. 'type' => 'checkbox',
  2973. 'data-foo' => 'disabled'
  2974. ]);
  2975. $expected = [
  2976. 'div' => ['class' => 'input checkbox'],
  2977. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2978. 'label' => ['for' => 'user-disabled'],
  2979. ['input' => [
  2980. 'type' => 'checkbox',
  2981. 'name' => 'User[disabled]',
  2982. 'value' => '1',
  2983. 'id' => 'user-disabled',
  2984. 'data-foo' => 'disabled'
  2985. ]],
  2986. 'Disabled',
  2987. '/label',
  2988. '/div'
  2989. ];
  2990. $this->assertHtml($expected, $result);
  2991. $result = $this->Form->input('User.confirm', [
  2992. 'label' => 'Confirm <b>me</b>!',
  2993. 'type' => 'checkbox',
  2994. 'escape' => false
  2995. ]);
  2996. $expected = [
  2997. 'div' => ['class' => 'input checkbox'],
  2998. 'input' => ['type' => 'hidden', 'name' => 'User[confirm]', 'value' => '0'],
  2999. 'label' => ['for' => 'user-confirm'],
  3000. ['input' => [
  3001. 'type' => 'checkbox',
  3002. 'name' => 'User[confirm]',
  3003. 'value' => '1',
  3004. 'id' => 'user-confirm',
  3005. ]],
  3006. 'Confirm <b>me</b>!',
  3007. '/label',
  3008. '/div'
  3009. ];
  3010. $this->assertHtml($expected, $result);
  3011. }
  3012. /**
  3013. * testInputHidden method
  3014. *
  3015. * Test that input() does not create wrapping div and label tag for hidden fields.
  3016. *
  3017. * @return void
  3018. */
  3019. public function testInputHidden()
  3020. {
  3021. TableRegistry::get('ValidateUsers', [
  3022. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3023. ]);
  3024. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3025. $result = $this->Form->input('ValidateUser.id');
  3026. $expected = [
  3027. 'input' => ['name', 'type' => 'hidden', 'id']
  3028. ];
  3029. $this->assertHtml($expected, $result);
  3030. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  3031. $expected = [
  3032. 'input' => ['name', 'type' => 'hidden', 'id']
  3033. ];
  3034. $this->assertHtml($expected, $result);
  3035. }
  3036. /**
  3037. * testInputDatetime method
  3038. *
  3039. * Test form->input() with datetime.
  3040. *
  3041. * @return void
  3042. */
  3043. public function testInputDatetime()
  3044. {
  3045. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3046. ->setMethods(['datetime'])
  3047. ->setConstructorArgs([new View()])
  3048. ->getMock();
  3049. $this->Form->expects($this->once())->method('datetime')
  3050. ->with('prueba', [
  3051. 'type' => 'datetime',
  3052. 'timeFormat' => 24,
  3053. 'minYear' => 2008,
  3054. 'maxYear' => 2011,
  3055. 'interval' => 15,
  3056. 'options' => null,
  3057. 'empty' => false,
  3058. 'id' => 'prueba',
  3059. 'required' => false,
  3060. 'templateVars' => []
  3061. ])
  3062. ->will($this->returnValue('This is it!'));
  3063. $result = $this->Form->input('prueba', [
  3064. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3065. 'maxYear' => 2011, 'interval' => 15
  3066. ]);
  3067. $expected = [
  3068. 'div' => ['class' => 'input datetime'],
  3069. '<label',
  3070. 'Prueba',
  3071. '/label',
  3072. 'This is it!',
  3073. '/div'
  3074. ];
  3075. $this->assertHtml($expected, $result);
  3076. }
  3077. /**
  3078. * testInputDatetimeIdPrefix method
  3079. *
  3080. * Test form->input() with datetime with id prefix.
  3081. *
  3082. * @return void
  3083. */
  3084. public function testInputDatetimeIdPrefix()
  3085. {
  3086. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3087. ->setMethods(['datetime'])
  3088. ->setConstructorArgs([new View()])
  3089. ->getMock();
  3090. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3091. $this->Form->expects($this->once())->method('datetime')
  3092. ->with('prueba', [
  3093. 'type' => 'datetime',
  3094. 'timeFormat' => 24,
  3095. 'minYear' => 2008,
  3096. 'maxYear' => 2011,
  3097. 'interval' => 15,
  3098. 'options' => null,
  3099. 'empty' => false,
  3100. 'id' => 'prefix-prueba',
  3101. 'required' => false,
  3102. 'templateVars' => []
  3103. ])
  3104. ->will($this->returnValue('This is it!'));
  3105. $result = $this->Form->input('prueba', [
  3106. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3107. 'maxYear' => 2011, 'interval' => 15
  3108. ]);
  3109. $expected = [
  3110. 'div' => ['class' => 'input datetime'],
  3111. '<label',
  3112. 'Prueba',
  3113. '/label',
  3114. 'This is it!',
  3115. '/div'
  3116. ];
  3117. $this->assertHtml($expected, $result);
  3118. }
  3119. /**
  3120. * testInputCheckboxWithDisabledElements method
  3121. *
  3122. * Test generating checkboxes with disabled elements.
  3123. *
  3124. * @return void
  3125. */
  3126. public function testInputCheckboxWithDisabledElements()
  3127. {
  3128. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3129. $result = $this->Form->input('Contact.multiple', [
  3130. 'multiple' => 'checkbox',
  3131. 'disabled' => 'disabled',
  3132. 'options' => $options
  3133. ]);
  3134. $expected = [
  3135. ['div' => ['class' => 'input select']],
  3136. ['label' => ['for' => "contact-multiple"]],
  3137. 'Multiple',
  3138. '/label',
  3139. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3140. ['div' => ['class' => 'checkbox']],
  3141. ['label' => ['for' => "contact-multiple-1"]],
  3142. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3143. 'One',
  3144. '/label',
  3145. '/div',
  3146. ['div' => ['class' => 'checkbox']],
  3147. ['label' => ['for' => "contact-multiple-2"]],
  3148. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3149. 'Two',
  3150. '/label',
  3151. '/div',
  3152. ['div' => ['class' => 'checkbox']],
  3153. ['label' => ['for' => "contact-multiple-3"]],
  3154. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3155. 'Three',
  3156. '/label',
  3157. '/div',
  3158. '/div'
  3159. ];
  3160. $this->assertHtml($expected, $result);
  3161. // make sure 50 does only disable 50, and not 50f5c0cf
  3162. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3163. $disabled = [50];
  3164. $expected = [
  3165. ['div' => ['class' => 'input select']],
  3166. ['label' => ['for' => "contact-multiple"]],
  3167. 'Multiple',
  3168. '/label',
  3169. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3170. ['div' => ['class' => 'checkbox']],
  3171. ['label' => ['for' => "contact-multiple-50"]],
  3172. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3173. 'Fifty',
  3174. '/label',
  3175. '/div',
  3176. ['div' => ['class' => 'checkbox']],
  3177. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3178. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3179. 'Stringy',
  3180. '/label',
  3181. '/div',
  3182. '/div'
  3183. ];
  3184. $result = $this->Form->input('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3185. $this->assertHtml($expected, $result);
  3186. }
  3187. /**
  3188. * testInputWithLeadingInteger method
  3189. *
  3190. * Test input name with leading integer, ensure attributes are generated correctly.
  3191. *
  3192. * @return void
  3193. */
  3194. public function testInputWithLeadingInteger()
  3195. {
  3196. $result = $this->Form->text('0.Node.title');
  3197. $expected = [
  3198. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3199. ];
  3200. $this->assertHtml($expected, $result);
  3201. }
  3202. /**
  3203. * testInputSelectType method
  3204. *
  3205. * Test form->input() with select type inputs.
  3206. *
  3207. * @return void
  3208. */
  3209. public function testInputSelectType()
  3210. {
  3211. $result = $this->Form->input(
  3212. 'email',
  3213. [
  3214. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3215. );
  3216. $expected = [
  3217. 'div' => ['class' => 'input select'],
  3218. 'label' => ['for' => 'email'],
  3219. 'Email',
  3220. '/label',
  3221. ['select' => ['name' => 'email', 'id' => 'email']],
  3222. ['option' => ['value' => '']],
  3223. '/option',
  3224. ['option' => ['value' => 'è']],
  3225. 'Firést',
  3226. '/option',
  3227. ['option' => ['value' => 'é']],
  3228. 'Secoènd',
  3229. '/option',
  3230. '/select',
  3231. '/div'
  3232. ];
  3233. $this->assertHtml($expected, $result);
  3234. $result = $this->Form->input(
  3235. 'email',
  3236. [
  3237. 'options' => ['First', 'Second'], 'empty' => true]
  3238. );
  3239. $expected = [
  3240. 'div' => ['class' => 'input select'],
  3241. 'label' => ['for' => 'email'],
  3242. 'Email',
  3243. '/label',
  3244. ['select' => ['name' => 'email', 'id' => 'email']],
  3245. ['option' => ['value' => '']],
  3246. '/option',
  3247. ['option' => ['value' => '0']],
  3248. 'First',
  3249. '/option',
  3250. ['option' => ['value' => '1']],
  3251. 'Second',
  3252. '/option',
  3253. '/select',
  3254. '/div'
  3255. ];
  3256. $this->assertHtml($expected, $result);
  3257. $result = $this->Form->input('email', [
  3258. 'type' => 'select',
  3259. 'options' => new \ArrayObject(['First', 'Second']),
  3260. 'empty' => true
  3261. ]);
  3262. $this->assertHtml($expected, $result);
  3263. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3264. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3265. $result = $this->Form->input('Model.user_id', ['empty' => true]);
  3266. $expected = [
  3267. 'div' => ['class' => 'input select'],
  3268. 'label' => ['for' => 'model-user-id'],
  3269. 'User',
  3270. '/label',
  3271. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3272. ['option' => ['value' => '']],
  3273. '/option',
  3274. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3275. 'good',
  3276. '/option',
  3277. ['option' => ['value' => 'other']],
  3278. 'bad',
  3279. '/option',
  3280. '/select',
  3281. '/div'
  3282. ];
  3283. $this->assertHtml($expected, $result);
  3284. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3285. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3286. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3287. $expected = [
  3288. 'div' => ['class' => 'input select'],
  3289. 'label' => ['for' => 'thing-user-id'],
  3290. 'User',
  3291. '/label',
  3292. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3293. ['option' => ['value' => '']],
  3294. 'Some Empty',
  3295. '/option',
  3296. ['option' => ['value' => 'value']],
  3297. 'good',
  3298. '/option',
  3299. ['option' => ['value' => 'other']],
  3300. 'bad',
  3301. '/option',
  3302. '/select',
  3303. '/div'
  3304. ];
  3305. $this->assertHtml($expected, $result);
  3306. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3307. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3308. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3309. $expected = [
  3310. 'div' => ['class' => 'input select'],
  3311. 'label' => ['for' => 'thing-user-id'],
  3312. 'User',
  3313. '/label',
  3314. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3315. ['option' => ['value' => '']],
  3316. 'Some Empty',
  3317. '/option',
  3318. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3319. 'good',
  3320. '/option',
  3321. ['option' => ['value' => 'other']],
  3322. 'bad',
  3323. '/option',
  3324. '/select',
  3325. '/div'
  3326. ];
  3327. $this->assertHtml($expected, $result);
  3328. $this->Form->data = [];
  3329. $result = $this->Form->input('Publisher.id', [
  3330. 'label' => 'Publisher',
  3331. 'type' => 'select',
  3332. 'multiple' => 'checkbox',
  3333. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3334. ]);
  3335. $expected = [
  3336. ['div' => ['class' => 'input select']],
  3337. ['label' => ['for' => 'publisher-id']],
  3338. 'Publisher',
  3339. '/label',
  3340. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3341. ['div' => ['class' => 'checkbox']],
  3342. ['label' => ['for' => 'publisher-id-value-1']],
  3343. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3344. 'Label 1',
  3345. '/label',
  3346. '/div',
  3347. ['div' => ['class' => 'checkbox']],
  3348. ['label' => ['for' => 'publisher-id-value-2']],
  3349. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3350. 'Label 2',
  3351. '/label',
  3352. '/div',
  3353. '/div'
  3354. ];
  3355. $this->assertHtml($expected, $result);
  3356. }
  3357. /**
  3358. * testInputWithNonStandardPrimaryKeyMakesHidden method
  3359. *
  3360. * Test that input() and a non standard primary key makes a hidden input by default.
  3361. *
  3362. * @return void
  3363. */
  3364. public function testInputWithNonStandardPrimaryKeyMakesHidden()
  3365. {
  3366. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3367. $this->Form->create($this->article);
  3368. $result = $this->Form->input('title');
  3369. $expected = [
  3370. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3371. ];
  3372. $this->assertHtml($expected, $result);
  3373. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3374. $this->Form->create($this->article);
  3375. $result = $this->Form->input('title');
  3376. $expected = [
  3377. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3378. ];
  3379. $this->assertHtml($expected, $result);
  3380. $result = $this->Form->input('body');
  3381. $expected = [
  3382. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3383. ];
  3384. $this->assertHtml($expected, $result);
  3385. }
  3386. /**
  3387. * testInputOverridingMagicSelectType method
  3388. *
  3389. * Test that overriding the magic select type widget is possible.
  3390. *
  3391. * @return void
  3392. */
  3393. public function testInputOverridingMagicSelectType()
  3394. {
  3395. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3396. $result = $this->Form->input('Model.user_id', ['type' => 'text']);
  3397. $expected = [
  3398. 'div' => ['class' => 'input text'],
  3399. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3400. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3401. '/div'
  3402. ];
  3403. $this->assertHtml($expected, $result);
  3404. //Check that magic types still work for plural/singular vars
  3405. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3406. $result = $this->Form->input('Model.type');
  3407. $expected = [
  3408. 'div' => ['class' => 'input select'],
  3409. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3410. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3411. ['option' => ['value' => 'value']], 'good', '/option',
  3412. ['option' => ['value' => 'other']], 'bad', '/option',
  3413. '/select',
  3414. '/div'
  3415. ];
  3416. $this->assertHtml($expected, $result);
  3417. }
  3418. /**
  3419. * testInputMagicTypeDoesNotOverride method
  3420. *
  3421. * Test that inferred types do not override developer input.
  3422. *
  3423. * @return void
  3424. */
  3425. public function testInputMagicTypeDoesNotOverride()
  3426. {
  3427. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3428. $result = $this->Form->input('Model.user', ['type' => 'checkbox']);
  3429. $expected = [
  3430. 'div' => ['class' => 'input checkbox'],
  3431. ['input' => [
  3432. 'type' => 'hidden',
  3433. 'name' => 'Model[user]',
  3434. 'value' => 0,
  3435. ]],
  3436. 'label' => ['for' => 'model-user'],
  3437. ['input' => [
  3438. 'name' => 'Model[user]',
  3439. 'type' => 'checkbox',
  3440. 'id' => 'model-user',
  3441. 'value' => 1
  3442. ]],
  3443. 'User',
  3444. '/label',
  3445. '/div'
  3446. ];
  3447. $this->assertHtml($expected, $result);
  3448. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3449. $options = [
  3450. 1 => 'blue',
  3451. 2 => 'red'
  3452. ];
  3453. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3454. $expected = [
  3455. 'div' => ['class' => 'input select'],
  3456. 'label' => ['for' => 'tags-ids'],
  3457. 'Tags',
  3458. '/label',
  3459. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3460. ['div' => ['class' => 'checkbox']],
  3461. ['label' => ['for' => 'tags-ids-1']],
  3462. ['input' => [
  3463. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3464. 'value' => '1', 'name' => 'tags[_ids][]'
  3465. ]],
  3466. 'blue',
  3467. '/label',
  3468. '/div',
  3469. ['div' => ['class' => 'checkbox']],
  3470. ['label' => ['for' => 'tags-ids-2']],
  3471. ['input' => [
  3472. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3473. 'value' => '2', 'name' => 'tags[_ids][]'
  3474. ]],
  3475. 'red',
  3476. '/label',
  3477. '/div',
  3478. '/div'
  3479. ];
  3480. $this->assertHtml($expected, $result);
  3481. }
  3482. /**
  3483. * testInputMagicSelectForTypeNumber method
  3484. *
  3485. * Test that magic input() selects are created for type=number.
  3486. *
  3487. * @return void
  3488. */
  3489. public function testInputMagicSelectForTypeNumber()
  3490. {
  3491. TableRegistry::get('ValidateUsers', [
  3492. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3493. ]);
  3494. $entity = new Entity(['balance' => 1]);
  3495. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3496. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3497. $result = $this->Form->input('balance');
  3498. $expected = [
  3499. 'div' => ['class' => 'input select'],
  3500. 'label' => ['for' => 'balance'],
  3501. 'Balance',
  3502. '/label',
  3503. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3504. ['option' => ['value' => '0']],
  3505. 'nothing',
  3506. '/option',
  3507. ['option' => ['value' => '1', 'selected' => 'selected']],
  3508. 'some',
  3509. '/option',
  3510. ['option' => ['value' => '100']],
  3511. 'a lot',
  3512. '/option',
  3513. '/select',
  3514. '/div'
  3515. ];
  3516. $this->assertHtml($expected, $result);
  3517. }
  3518. /**
  3519. * testInvalidInputTypeOption method
  3520. *
  3521. * Test invalid 'input' type option to input() function.
  3522. *
  3523. * @expectedException \RuntimeException
  3524. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3525. * @return void
  3526. */
  3527. public function testInvalidInputTypeOption()
  3528. {
  3529. $this->Form->input('text', ['type' => 'input']);
  3530. }
  3531. /**
  3532. * testInputMagicSelectChangeToRadio method
  3533. *
  3534. * Test that magic input() selects can easily be converted into radio types without error.
  3535. *
  3536. * @return void
  3537. */
  3538. public function testInputMagicSelectChangeToRadio()
  3539. {
  3540. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3541. $result = $this->Form->input('Model.user_id', ['type' => 'radio']);
  3542. $this->assertContains('input type="radio"', $result);
  3543. }
  3544. /**
  3545. * testFormInputSubmit method
  3546. *
  3547. * Test correct results for form::input() and type submit.
  3548. *
  3549. * @return void
  3550. */
  3551. public function testFormInputSubmit()
  3552. {
  3553. $result = $this->Form->input('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3554. $expected = [
  3555. 'div' => ['class' => 'submit'],
  3556. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3557. '/div'
  3558. ];
  3559. $this->assertHtml($expected, $result);
  3560. }
  3561. /**
  3562. * testFormInputs method
  3563. *
  3564. * Test correct results from form::inputs().
  3565. *
  3566. * @return void
  3567. */
  3568. public function testFormInputsLegendFieldset()
  3569. {
  3570. $this->Form->create($this->article);
  3571. $result = $this->Form->allInputs([], ['legend' => 'The Legend']);
  3572. $expected = [
  3573. '<fieldset',
  3574. '<legend',
  3575. 'The Legend',
  3576. '/legend',
  3577. '*/fieldset',
  3578. ];
  3579. $this->assertHtml($expected, $result);
  3580. $result = $this->Form->allInputs([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3581. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3582. $this->assertContains('<fieldset>', $result);
  3583. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => false]);
  3584. $this->assertNotContains('<legend>', $result);
  3585. $this->assertNotContains('<fieldset>', $result);
  3586. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => 'Hello']);
  3587. $this->assertNotContains('<legend>', $result);
  3588. $this->assertNotContains('<fieldset>', $result);
  3589. $this->Form->create($this->article);
  3590. $this->Form->request->params['prefix'] = 'admin';
  3591. $this->Form->request->params['action'] = 'admin_edit';
  3592. $this->Form->request->params['controller'] = 'articles';
  3593. $result = $this->Form->allInputs();
  3594. $expected = [
  3595. '<fieldset',
  3596. '<legend',
  3597. 'New Article',
  3598. '/legend',
  3599. '*/fieldset',
  3600. ];
  3601. $this->assertHtml($expected, $result);
  3602. $this->Form->create($this->article);
  3603. $result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
  3604. $expected = [
  3605. '<fieldset',
  3606. '<legend',
  3607. 'The Legend',
  3608. '/legend',
  3609. '*/fieldset',
  3610. ];
  3611. $this->assertHtml($expected, $result);
  3612. $this->Form->create($this->article);
  3613. $result = $this->Form->allInputs([], [
  3614. 'fieldset' => [
  3615. 'class' => 'some-class some-other-class',
  3616. 'disabled' => true,
  3617. 'data-param' => 'a-param'
  3618. ],
  3619. 'legend' => 'The Legend'
  3620. ]);
  3621. $expected = [
  3622. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3623. '<legend',
  3624. 'The Legend',
  3625. '/legend',
  3626. '*/fieldset',
  3627. ];
  3628. $this->assertHtml($expected, $result);
  3629. }
  3630. /**
  3631. * testFormInputs method
  3632. *
  3633. * Test the inputs() method.
  3634. *
  3635. * @return void
  3636. */
  3637. public function testFormInputs()
  3638. {
  3639. $this->Form->create($this->article);
  3640. $result = $this->Form->allInputs();
  3641. $expected = [
  3642. '<fieldset',
  3643. '<legend', 'New Article', '/legend',
  3644. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3645. ['div' => ['class' => 'input select required']],
  3646. '*/div',
  3647. ['div' => ['class' => 'input text required']],
  3648. '*/div',
  3649. ['div' => ['class' => 'input text']],
  3650. '*/div',
  3651. ['div' => ['class' => 'input text']],
  3652. '*/div',
  3653. '/fieldset',
  3654. ];
  3655. $this->assertHtml($expected, $result);
  3656. $result = $this->Form->allInputs([
  3657. 'published' => ['type' => 'boolean']
  3658. ]);
  3659. $expected = [
  3660. '<fieldset',
  3661. '<legend', 'New Article', '/legend',
  3662. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3663. ['div' => ['class' => 'input select required']],
  3664. '*/div',
  3665. ['div' => ['class' => 'input text required']],
  3666. '*/div',
  3667. ['div' => ['class' => 'input text']],
  3668. '*/div',
  3669. ['div' => ['class' => 'input boolean']],
  3670. '*/div',
  3671. '/fieldset',
  3672. ];
  3673. $this->assertHtml($expected, $result);
  3674. $this->Form->create($this->article);
  3675. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  3676. $expected = [
  3677. 'fieldset' => [],
  3678. 'legend' => [],
  3679. 'Hello',
  3680. '/legend',
  3681. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3682. ['div' => ['class' => 'input select required']],
  3683. '*/div',
  3684. ['div' => ['class' => 'input text required']],
  3685. '*/div',
  3686. ['div' => ['class' => 'input text']],
  3687. '*/div',
  3688. ['div' => ['class' => 'input text']],
  3689. '*/div',
  3690. '/fieldset'
  3691. ];
  3692. $this->assertHtml($expected, $result);
  3693. $this->Form->create(false);
  3694. $expected = [
  3695. 'fieldset' => [],
  3696. ['div' => ['class' => 'input text']],
  3697. 'label' => ['for' => 'foo'],
  3698. 'Foo',
  3699. '/label',
  3700. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3701. '*/div',
  3702. '/fieldset'
  3703. ];
  3704. $result = $this->Form->allInputs(
  3705. ['foo' => ['type' => 'text']],
  3706. ['legend' => false]
  3707. );
  3708. $this->assertHtml($expected, $result);
  3709. }
  3710. /**
  3711. * testFormInputsBlacklist method
  3712. *
  3713. * @return void
  3714. */
  3715. public function testFormInputsBlacklist()
  3716. {
  3717. $this->Form->create($this->article);
  3718. $result = $this->Form->allInputs([
  3719. 'id' => false
  3720. ]);
  3721. $expected = [
  3722. '<fieldset',
  3723. '<legend', 'New Article', '/legend',
  3724. ['div' => ['class' => 'input select required']],
  3725. '*/div',
  3726. ['div' => ['class' => 'input text required']],
  3727. '*/div',
  3728. ['div' => ['class' => 'input text']],
  3729. '*/div',
  3730. ['div' => ['class' => 'input text']],
  3731. '*/div',
  3732. '/fieldset',
  3733. ];
  3734. $this->assertHtml($expected, $result);
  3735. $this->Form->create($this->article);
  3736. $result = $this->Form->allInputs([
  3737. 'id' => []
  3738. ]);
  3739. $expected = [
  3740. '<fieldset',
  3741. '<legend', 'New Article', '/legend',
  3742. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3743. ['div' => ['class' => 'input select required']],
  3744. '*/div',
  3745. ['div' => ['class' => 'input text required']],
  3746. '*/div',
  3747. ['div' => ['class' => 'input text']],
  3748. '*/div',
  3749. ['div' => ['class' => 'input text']],
  3750. '*/div',
  3751. '/fieldset',
  3752. ];
  3753. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3754. }
  3755. /**
  3756. * testSelectAsCheckbox method
  3757. *
  3758. * Test multi-select widget with checkbox formatting.
  3759. *
  3760. * @return void
  3761. */
  3762. public function testSelectAsCheckbox()
  3763. {
  3764. $result = $this->Form->select(
  3765. 'Model.multi_field',
  3766. ['first', 'second', 'third'],
  3767. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3768. );
  3769. $expected = [
  3770. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3771. ['div' => ['class' => 'checkbox']],
  3772. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3773. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3774. 'first',
  3775. '/label',
  3776. '/div',
  3777. ['div' => ['class' => 'checkbox']],
  3778. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3779. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3780. 'second',
  3781. '/label',
  3782. '/div',
  3783. ['div' => ['class' => 'checkbox']],
  3784. ['label' => ['for' => 'model-multi-field-2']],
  3785. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3786. 'third',
  3787. '/label',
  3788. '/div',
  3789. ];
  3790. $this->assertHtml($expected, $result);
  3791. $result = $this->Form->select(
  3792. 'Model.multi_field',
  3793. ['1/2' => 'half'],
  3794. ['multiple' => 'checkbox']
  3795. );
  3796. $expected = [
  3797. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3798. ['div' => ['class' => 'checkbox']],
  3799. ['label' => ['for' => 'model-multi-field-1-2']],
  3800. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3801. 'half',
  3802. '/label',
  3803. '/div',
  3804. ];
  3805. $this->assertHtml($expected, $result);
  3806. }
  3807. /**
  3808. * testLabel method
  3809. *
  3810. * Test label generation.
  3811. *
  3812. * @return void
  3813. */
  3814. public function testLabel()
  3815. {
  3816. $result = $this->Form->label('Person.name');
  3817. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3818. $this->assertHtml($expected, $result);
  3819. $result = $this->Form->label('Person.name');
  3820. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3821. $this->assertHtml($expected, $result);
  3822. $result = $this->Form->label('Person.first_name');
  3823. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3824. $this->assertHtml($expected, $result);
  3825. $result = $this->Form->label('Person.first_name', 'Your first name');
  3826. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3827. $this->assertHtml($expected, $result);
  3828. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3829. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3830. $this->assertHtml($expected, $result);
  3831. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3832. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3833. $this->assertHtml($expected, $result);
  3834. $result = $this->Form->label('Person.first_name', '');
  3835. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3836. $this->assertHtml($expected, $result);
  3837. $result = $this->Form->label('Person.2.name', '');
  3838. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3839. $this->assertHtml($expected, $result);
  3840. }
  3841. /**
  3842. * testLabelContainInput method
  3843. *
  3844. * Test that label() can accept an input with the correct template vars.
  3845. *
  3846. * @return void
  3847. */
  3848. public function testLabelContainInput()
  3849. {
  3850. $this->Form->templates([
  3851. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3852. ]);
  3853. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3854. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3855. ]);
  3856. $expected = [
  3857. 'label' => ['for' => 'person-accept-terms'],
  3858. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3859. 'Accept',
  3860. '/label',
  3861. ];
  3862. $this->assertHtml($expected, $result);
  3863. }
  3864. /**
  3865. * testTextbox method
  3866. *
  3867. * Test textbox element generation.
  3868. *
  3869. * @return void
  3870. */
  3871. public function testTextbox()
  3872. {
  3873. $result = $this->Form->text('Model.field');
  3874. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3875. $this->assertHtml($expected, $result);
  3876. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3877. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3878. $this->assertHtml($expected, $result);
  3879. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3880. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3881. $this->assertHtml($expected, $result);
  3882. }
  3883. /**
  3884. * testTextBoxDataAndError method
  3885. *
  3886. * Test that text() hooks up with request data and error fields.
  3887. *
  3888. * @return void
  3889. */
  3890. public function testTextBoxDataAndError()
  3891. {
  3892. $this->article['errors'] = [
  3893. 'Contact' => ['text' => 'wrong']
  3894. ];
  3895. $this->Form->create($this->article);
  3896. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3897. $result = $this->Form->text('Model.text');
  3898. $expected = [
  3899. 'input' => [
  3900. 'type' => 'text',
  3901. 'name' => 'Model[text]',
  3902. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3903. ]
  3904. ];
  3905. $this->assertHtml($expected, $result);
  3906. $this->Form->request->data['Contact']['text'] = 'test';
  3907. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3908. $expected = [
  3909. 'input' => [
  3910. 'type' => 'text',
  3911. 'name' => 'Contact[text]',
  3912. 'value' => 'test',
  3913. 'id' => 'theID',
  3914. 'class' => 'form-error'
  3915. ]
  3916. ];
  3917. $this->assertHtml($expected, $result);
  3918. }
  3919. /**
  3920. * testDefaultValue method
  3921. *
  3922. * Test default value setting.
  3923. *
  3924. * @return void
  3925. */
  3926. public function testTextDefaultValue()
  3927. {
  3928. $this->Form->request->data['Model']['field'] = 'test';
  3929. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3930. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3931. $this->assertHtml($expected, $result);
  3932. unset($this->Form->request->data['Model']['field']);
  3933. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3934. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3935. $this->assertHtml($expected, $result);
  3936. $Articles = TableRegistry::get('Articles');
  3937. $title = $Articles->schema()->column('title');
  3938. $Articles->schema()->addColumn(
  3939. 'title',
  3940. ['default' => 'default title'] + $title
  3941. );
  3942. $entity = $Articles->newEntity();
  3943. $this->Form->create($entity);
  3944. // Get default value from schema
  3945. $result = $this->Form->text('title');
  3946. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3947. $this->assertHtml($expected, $result);
  3948. // Don't get value from schema
  3949. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3950. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3951. $this->assertHtml($expected, $result);
  3952. // Custom default value overrides default value from schema
  3953. $result = $this->Form->text('title', ['default' => 'override default']);
  3954. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3955. $this->assertHtml($expected, $result);
  3956. // Default value from schema is used only for new entities.
  3957. $entity->isNew(false);
  3958. $result = $this->Form->text('title');
  3959. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3960. $this->assertHtml($expected, $result);
  3961. }
  3962. /**
  3963. * testError method
  3964. *
  3965. * Test field error generation.
  3966. *
  3967. * @return void
  3968. */
  3969. public function testError()
  3970. {
  3971. $this->article['errors'] = [
  3972. 'Article' => ['field' => 'email']
  3973. ];
  3974. $this->Form->create($this->article);
  3975. $result = $this->Form->error('Article.field');
  3976. $expected = [
  3977. ['div' => ['class' => 'error-message']],
  3978. 'email',
  3979. '/div',
  3980. ];
  3981. $this->assertHtml($expected, $result);
  3982. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3983. $expected = [
  3984. ['div' => ['class' => 'error-message']],
  3985. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3986. '/div',
  3987. ];
  3988. $this->assertHtml($expected, $result);
  3989. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3990. $expected = [
  3991. ['div' => ['class' => 'error-message']],
  3992. '<strong', 'Badness!', '/strong',
  3993. '/div',
  3994. ];
  3995. $this->assertHtml($expected, $result);
  3996. }
  3997. /**
  3998. * testErrorRuleName method
  3999. *
  4000. * Test error translation can use rule names for translating.
  4001. *
  4002. * @return void
  4003. */
  4004. public function testErrorRuleName()
  4005. {
  4006. $this->article['errors'] = [
  4007. 'Article' => [
  4008. 'field' => ['email' => 'Your email was not good']
  4009. ]
  4010. ];
  4011. $this->Form->create($this->article);
  4012. $result = $this->Form->error('Article.field');
  4013. $expected = [
  4014. ['div' => ['class' => 'error-message']],
  4015. 'Your email was not good',
  4016. '/div',
  4017. ];
  4018. $this->assertHtml($expected, $result);
  4019. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4020. $expected = [
  4021. ['div' => ['class' => 'error-message']],
  4022. 'Email in use',
  4023. '/div',
  4024. ];
  4025. $this->assertHtml($expected, $result);
  4026. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4027. $expected = [
  4028. ['div' => ['class' => 'error-message']],
  4029. 'Email in use',
  4030. '/div',
  4031. ];
  4032. $this->assertHtml($expected, $result);
  4033. $result = $this->Form->error('Article.field', [
  4034. 'email' => 'Key is preferred',
  4035. 'Your email was not good' => 'Email in use'
  4036. ]);
  4037. $expected = [
  4038. ['div' => ['class' => 'error-message']],
  4039. 'Key is preferred',
  4040. '/div',
  4041. ];
  4042. $this->assertHtml($expected, $result);
  4043. }
  4044. /**
  4045. * testErrorMessages method
  4046. *
  4047. * Test error with nested lists.
  4048. *
  4049. * @return void
  4050. */
  4051. public function testErrorMessages()
  4052. {
  4053. $this->article['errors'] = [
  4054. 'Article' => ['field' => 'email']
  4055. ];
  4056. $this->Form->create($this->article);
  4057. $result = $this->Form->error('Article.field', [
  4058. 'email' => 'No good!'
  4059. ]);
  4060. $expected = [
  4061. 'div' => ['class' => 'error-message'],
  4062. 'No good!',
  4063. '/div'
  4064. ];
  4065. $this->assertHtml($expected, $result);
  4066. }
  4067. /**
  4068. * testErrorMultipleMessages method
  4069. *
  4070. * Test error() with multiple messages.
  4071. *
  4072. * @return void
  4073. */
  4074. public function testErrorMultipleMessages()
  4075. {
  4076. $this->article['errors'] = [
  4077. 'field' => ['notBlank', 'email', 'Something else']
  4078. ];
  4079. $this->Form->create($this->article);
  4080. $result = $this->Form->error('field', [
  4081. 'notBlank' => 'Cannot be empty',
  4082. 'email' => 'No good!'
  4083. ]);
  4084. $expected = [
  4085. 'div' => ['class' => 'error-message'],
  4086. 'ul' => [],
  4087. '<li', 'Cannot be empty', '/li',
  4088. '<li', 'No good!', '/li',
  4089. '<li', 'Something else', '/li',
  4090. '/ul',
  4091. '/div'
  4092. ];
  4093. $this->assertHtml($expected, $result);
  4094. }
  4095. /**
  4096. * testPassword method
  4097. *
  4098. * Test password element generation.
  4099. *
  4100. * @return void
  4101. */
  4102. public function testPassword()
  4103. {
  4104. $this->article['errors'] = [
  4105. 'Contact' => [
  4106. 'passwd' => 1
  4107. ]
  4108. ];
  4109. $this->Form->create($this->article);
  4110. $result = $this->Form->password('Contact.field');
  4111. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4112. $this->assertHtml($expected, $result);
  4113. $this->Form->request->data['Contact']['passwd'] = 'test';
  4114. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4115. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4116. $this->assertHtml($expected, $result);
  4117. }
  4118. /**
  4119. * testRadio method
  4120. *
  4121. * Test radio element set generation.
  4122. *
  4123. * @return void
  4124. */
  4125. public function testRadio()
  4126. {
  4127. $result = $this->Form->radio('Model.field', ['option A']);
  4128. $expected = [
  4129. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4130. 'label' => ['for' => 'model-field-0'],
  4131. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4132. 'option A',
  4133. '/label'
  4134. ];
  4135. $this->assertHtml($expected, $result);
  4136. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4137. $this->assertHtml($expected, $result);
  4138. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4139. $expected = [
  4140. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4141. ['label' => ['for' => 'model-field-0']],
  4142. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4143. 'option A',
  4144. '/label',
  4145. ['label' => ['for' => 'model-field-1']],
  4146. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4147. 'option B',
  4148. '/label',
  4149. ];
  4150. $this->assertHtml($expected, $result);
  4151. $result = $this->Form->radio(
  4152. 'Employee.gender',
  4153. ['male' => 'Male', 'female' => 'Female'],
  4154. ['form' => 'my-form']
  4155. );
  4156. $expected = [
  4157. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4158. ['label' => ['for' => 'employee-gender-male']],
  4159. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4160. 'Male',
  4161. '/label',
  4162. ['label' => ['for' => 'employee-gender-female']],
  4163. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4164. 'Female',
  4165. '/label',
  4166. ];
  4167. $this->assertHtml($expected, $result);
  4168. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4169. $expected = [
  4170. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4171. ['label' => ['for' => 'model-custom-0']],
  4172. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4173. 'option A',
  4174. '/label',
  4175. ['label' => ['for' => 'model-custom-1']],
  4176. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4177. 'option B',
  4178. '/label',
  4179. ];
  4180. $this->assertHtml($expected, $result);
  4181. $result = $this->Form->radio(
  4182. 'Employee.gender',
  4183. [
  4184. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4185. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4186. ]
  4187. );
  4188. $expected = [
  4189. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4190. ['label' => ['for' => 'employee-gender-male']],
  4191. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4192. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4193. 'Male',
  4194. '/label',
  4195. ['label' => ['for' => 'employee-gender-female']],
  4196. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4197. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4198. 'Female',
  4199. '/label',
  4200. ];
  4201. $this->assertHtml($expected, $result);
  4202. }
  4203. /**
  4204. * testRadioDefaultValue method
  4205. *
  4206. * Test default value setting on radio() method.
  4207. *
  4208. * @return void
  4209. */
  4210. public function testRadioDefaultValue()
  4211. {
  4212. $Articles = TableRegistry::get('Articles');
  4213. $title = $Articles->schema()->column('title');
  4214. $Articles->schema()->addColumn(
  4215. 'title',
  4216. ['default' => '1'] + $title
  4217. );
  4218. $this->Form->create($Articles->newEntity());
  4219. $result = $this->Form->radio('title', ['option A', 'option B']);
  4220. $expected = [
  4221. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4222. ['label' => ['for' => 'title-0']],
  4223. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4224. 'option A',
  4225. '/label',
  4226. ['label' => ['for' => 'title-1']],
  4227. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4228. 'option B',
  4229. '/label',
  4230. ];
  4231. $this->assertHtml($expected, $result);
  4232. }
  4233. /**
  4234. * testInputRadio method
  4235. *
  4236. * Test that input works with radio types.
  4237. *
  4238. * @return void
  4239. */
  4240. public function testInputRadio()
  4241. {
  4242. $result = $this->Form->input('test', [
  4243. 'type' => 'radio',
  4244. 'options' => ['A', 'B'],
  4245. ]);
  4246. $expected = [
  4247. ['div' => ['class' => 'input radio']],
  4248. '<label',
  4249. 'Test',
  4250. '/label',
  4251. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4252. ['label' => ['for' => 'test-0']],
  4253. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4254. 'A',
  4255. '/label',
  4256. ['label' => ['for' => 'test-1']],
  4257. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4258. 'B',
  4259. '/label',
  4260. '/div',
  4261. ];
  4262. $this->assertHtml($expected, $result);
  4263. $result = $this->Form->input('test', [
  4264. 'type' => 'radio',
  4265. 'options' => ['A', 'B'],
  4266. 'value' => '0'
  4267. ]);
  4268. $expected = [
  4269. ['div' => ['class' => 'input radio']],
  4270. '<label',
  4271. 'Test',
  4272. '/label',
  4273. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4274. ['label' => ['for' => 'test-0']],
  4275. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4276. 'A',
  4277. '/label',
  4278. ['label' => ['for' => 'test-1']],
  4279. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4280. 'B',
  4281. '/label',
  4282. '/div',
  4283. ];
  4284. $this->assertHtml($expected, $result);
  4285. $result = $this->Form->input('test', [
  4286. 'type' => 'radio',
  4287. 'options' => ['A', 'B'],
  4288. 'label' => false
  4289. ]);
  4290. $expected = [
  4291. ['div' => ['class' => 'input radio']],
  4292. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4293. ['label' => ['for' => 'test-0']],
  4294. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4295. 'A',
  4296. '/label',
  4297. ['label' => ['for' => 'test-1']],
  4298. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4299. 'B',
  4300. '/label',
  4301. '/div',
  4302. ];
  4303. $this->assertHtml($expected, $result);
  4304. }
  4305. /**
  4306. * testRadioNoLabel method
  4307. *
  4308. * Test that radio() works with label = false.
  4309. *
  4310. * @return void
  4311. */
  4312. public function testRadioNoLabel()
  4313. {
  4314. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4315. $expected = [
  4316. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4317. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4318. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4319. ];
  4320. $this->assertHtml($expected, $result);
  4321. }
  4322. /**
  4323. * testRadioInputInsideLabel method
  4324. *
  4325. * Test generating radio input inside label ala twitter bootstrap.
  4326. *
  4327. * @return void
  4328. */
  4329. public function testRadioInputInsideLabel()
  4330. {
  4331. $this->Form->templates([
  4332. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4333. 'radioWrapper' => '{{label}}'
  4334. ]);
  4335. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4336. //@codingStandardsIgnoreStart
  4337. $expected = [
  4338. ['input' => [
  4339. 'type' => 'hidden',
  4340. 'name' => 'Model[field]',
  4341. 'value' => ''
  4342. ]],
  4343. ['label' => ['for' => 'model-field-0']],
  4344. ['input' => [
  4345. 'type' => 'radio',
  4346. 'name' => 'Model[field]',
  4347. 'value' => '0',
  4348. 'id' => 'model-field-0'
  4349. ]],
  4350. 'option A',
  4351. '/label',
  4352. ['label' => ['for' => 'model-field-1']],
  4353. ['input' => [
  4354. 'type' => 'radio',
  4355. 'name' => 'Model[field]',
  4356. 'value' => '1',
  4357. 'id' => 'model-field-1'
  4358. ]],
  4359. 'option B',
  4360. '/label',
  4361. ];
  4362. //@codingStandardsIgnoreEnd
  4363. $this->assertHtml($expected, $result);
  4364. }
  4365. /**
  4366. * testRadioHiddenInputDisabling method
  4367. *
  4368. * Test disabling the hidden input for radio buttons.
  4369. *
  4370. * @return void
  4371. */
  4372. public function testRadioHiddenInputDisabling()
  4373. {
  4374. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4375. $expected = [
  4376. 'label' => ['for' => 'model-1-field-0'],
  4377. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4378. 'option A',
  4379. '/label'
  4380. ];
  4381. $this->assertHtml($expected, $result);
  4382. }
  4383. /**
  4384. * testRadioOutOfRange method
  4385. *
  4386. * Test radio element set generation.
  4387. *
  4388. * @return void
  4389. */
  4390. public function testRadioOutOfRange()
  4391. {
  4392. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4393. $expected = [
  4394. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4395. 'label' => ['for' => 'model-field-v'],
  4396. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4397. 'value',
  4398. '/label'
  4399. ];
  4400. $this->assertHtml($expected, $result);
  4401. }
  4402. /**
  4403. * testSelect method
  4404. *
  4405. * Test select element generation.
  4406. *
  4407. * @return void
  4408. */
  4409. public function testSelect()
  4410. {
  4411. $result = $this->Form->select('Model.field', []);
  4412. $expected = [
  4413. 'select' => ['name' => 'Model[field]'],
  4414. '/select'
  4415. ];
  4416. $this->assertHtml($expected, $result);
  4417. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4418. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4419. $expected = [
  4420. 'select' => ['name' => 'Model[field]'],
  4421. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4422. 'good',
  4423. '/option',
  4424. ['option' => ['value' => 'other']],
  4425. 'bad',
  4426. '/option',
  4427. '/select'
  4428. ];
  4429. $this->assertHtml($expected, $result);
  4430. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4431. $this->assertHtml($expected, $result);
  4432. $this->Form->request->data = [];
  4433. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4434. $expected = [
  4435. 'select' => ['name' => 'Model[field]'],
  4436. ['option' => ['value' => 'value']],
  4437. 'good',
  4438. '/option',
  4439. ['option' => ['value' => 'other']],
  4440. 'bad',
  4441. '/option',
  4442. '/select'
  4443. ];
  4444. $this->assertHtml($expected, $result);
  4445. $options = [
  4446. ['value' => 'first', 'text' => 'First'],
  4447. ['value' => 'first', 'text' => 'Another First'],
  4448. ];
  4449. $result = $this->Form->select(
  4450. 'Model.field',
  4451. $options,
  4452. ['escape' => false, 'empty' => false]
  4453. );
  4454. $expected = [
  4455. 'select' => ['name' => 'Model[field]'],
  4456. ['option' => ['value' => 'first']],
  4457. 'First',
  4458. '/option',
  4459. ['option' => ['value' => 'first']],
  4460. 'Another First',
  4461. '/option',
  4462. '/select'
  4463. ];
  4464. $this->assertHtml($expected, $result);
  4465. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4466. $result = $this->Form->select(
  4467. 'Model.contact_id',
  4468. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4469. ['escape' => false, 'empty' => 'pick something']
  4470. );
  4471. $expected = [
  4472. 'select' => ['name' => 'Model[contact_id]'],
  4473. ['option' => ['value' => '']], 'pick something', '/option',
  4474. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4475. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4476. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4477. '/select'
  4478. ];
  4479. $this->assertHtml($expected, $result);
  4480. $this->Form->request->data['Model']['field'] = 0;
  4481. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4482. $expected = [
  4483. 'select' => ['name' => 'Model[field]'],
  4484. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4485. ['option' => ['value' => '1']], 'Yes', '/option',
  4486. '/select'
  4487. ];
  4488. $this->assertHtml($expected, $result);
  4489. }
  4490. /**
  4491. * testSelectEscapeHtml method
  4492. *
  4493. * Test that select() escapes HTML.
  4494. *
  4495. * @return void
  4496. */
  4497. public function testSelectEscapeHtml()
  4498. {
  4499. $result = $this->Form->select(
  4500. 'Model.field',
  4501. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4502. ['empty' => false]
  4503. );
  4504. $expected = [
  4505. 'select' => ['name' => 'Model[field]'],
  4506. ['option' => ['value' => 'first']],
  4507. 'first &quot;html&quot; &lt;chars&gt;',
  4508. '/option',
  4509. ['option' => ['value' => 'second']],
  4510. 'value',
  4511. '/option',
  4512. '/select'
  4513. ];
  4514. $this->assertHtml($expected, $result);
  4515. $result = $this->Form->select(
  4516. 'Model.field',
  4517. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4518. ['escape' => false, 'empty' => false]
  4519. );
  4520. $expected = [
  4521. 'select' => ['name' => 'Model[field]'],
  4522. ['option' => ['value' => 'first']],
  4523. 'first "html" <chars>',
  4524. '/option',
  4525. ['option' => ['value' => 'second']],
  4526. 'value',
  4527. '/option',
  4528. '/select'
  4529. ];
  4530. $this->assertHtml($expected, $result);
  4531. }
  4532. /**
  4533. * testSelectRequired method
  4534. *
  4535. * Test select() with required and disabled attributes.
  4536. *
  4537. * @return void
  4538. */
  4539. public function testSelectRequired()
  4540. {
  4541. $this->article['required'] = [
  4542. 'user_id' => true
  4543. ];
  4544. $this->Form->create($this->article);
  4545. $result = $this->Form->select('user_id', ['option A']);
  4546. $expected = [
  4547. 'select' => [
  4548. 'name' => 'user_id',
  4549. 'required' => 'required'
  4550. ],
  4551. ['option' => ['value' => '0']], 'option A', '/option',
  4552. '/select'
  4553. ];
  4554. $this->assertHtml($expected, $result);
  4555. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4556. $expected = [
  4557. 'select' => [
  4558. 'name' => 'user_id',
  4559. 'disabled' => 'disabled'
  4560. ],
  4561. ['option' => ['value' => '0']], 'option A', '/option',
  4562. '/select'
  4563. ];
  4564. $this->assertHtml($expected, $result);
  4565. }
  4566. /**
  4567. * testNestedSelect method
  4568. *
  4569. * Test select element generation with optgroups.
  4570. *
  4571. * @return void
  4572. */
  4573. public function testNestedSelect()
  4574. {
  4575. $result = $this->Form->select(
  4576. 'Model.field',
  4577. [1 => 'One', 2 => 'Two', 'Three' => [
  4578. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4579. ]],
  4580. ['empty' => false]
  4581. );
  4582. $expected = [
  4583. 'select' => ['name' => 'Model[field]'],
  4584. ['option' => ['value' => 1]],
  4585. 'One',
  4586. '/option',
  4587. ['option' => ['value' => 2]],
  4588. 'Two',
  4589. '/option',
  4590. ['optgroup' => ['label' => 'Three']],
  4591. ['option' => ['value' => 3]],
  4592. 'Three',
  4593. '/option',
  4594. ['option' => ['value' => 4]],
  4595. 'Four',
  4596. '/option',
  4597. ['option' => ['value' => 5]],
  4598. 'Five',
  4599. '/option',
  4600. '/optgroup',
  4601. '/select'
  4602. ];
  4603. $this->assertHtml($expected, $result);
  4604. }
  4605. /**
  4606. * testSelectMultiple method
  4607. *
  4608. * Test generation of multiple select elements.
  4609. *
  4610. * @return void
  4611. */
  4612. public function testSelectMultiple()
  4613. {
  4614. $options = ['first', 'second', 'third'];
  4615. $result = $this->Form->select(
  4616. 'Model.multi_field',
  4617. $options,
  4618. ['form' => 'my-form', 'multiple' => true]
  4619. );
  4620. $expected = [
  4621. 'input' => [
  4622. 'type' => 'hidden',
  4623. 'name' => 'Model[multi_field]',
  4624. 'value' => '',
  4625. 'form' => 'my-form',
  4626. ],
  4627. 'select' => [
  4628. 'name' => 'Model[multi_field][]',
  4629. 'multiple' => 'multiple',
  4630. 'form' => 'my-form',
  4631. ],
  4632. ['option' => ['value' => '0']],
  4633. 'first',
  4634. '/option',
  4635. ['option' => ['value' => '1']],
  4636. 'second',
  4637. '/option',
  4638. ['option' => ['value' => '2']],
  4639. 'third',
  4640. '/option',
  4641. '/select'
  4642. ];
  4643. $this->assertHtml($expected, $result);
  4644. $result = $this->Form->select(
  4645. 'Model.multi_field',
  4646. $options,
  4647. ['multiple' => 'multiple', 'form' => 'my-form']
  4648. );
  4649. $this->assertHtml($expected, $result);
  4650. }
  4651. /**
  4652. * testCheckboxZeroValue method
  4653. *
  4654. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4655. *
  4656. * @return void
  4657. */
  4658. public function testCheckboxZeroValue()
  4659. {
  4660. $result = $this->Form->input('User.get_spam', [
  4661. 'type' => 'checkbox',
  4662. 'value' => '0',
  4663. 'hiddenField' => '1',
  4664. ]);
  4665. $expected = [
  4666. 'div' => ['class' => 'input checkbox'],
  4667. 'label' => ['for' => 'user-get-spam'],
  4668. ['input' => [
  4669. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4670. 'value' => '1'
  4671. ]],
  4672. ['input' => [
  4673. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4674. 'value' => '0', 'id' => 'user-get-spam'
  4675. ]],
  4676. 'Get Spam',
  4677. '/label',
  4678. '/div'
  4679. ];
  4680. $this->assertHtml($expected, $result);
  4681. }
  4682. /**
  4683. * testHabtmSelectBox method
  4684. *
  4685. * Test generation of habtm select boxes.
  4686. *
  4687. * @return void
  4688. */
  4689. public function testHabtmSelectBox()
  4690. {
  4691. $this->loadFixtures('Articles');
  4692. $options = [
  4693. 1 => 'blue',
  4694. 2 => 'red',
  4695. 3 => 'green'
  4696. ];
  4697. $tags = [
  4698. new Entity(['id' => 1, 'name' => 'blue']),
  4699. new Entity(['id' => 3, 'name' => 'green'])
  4700. ];
  4701. $article = new Article(['tags' => $tags]);
  4702. $this->Form->create($article);
  4703. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4704. $expected = [
  4705. 'div' => ['class' => 'input select'],
  4706. 'label' => ['for' => 'tags-ids'],
  4707. 'Tags',
  4708. '/label',
  4709. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4710. 'select' => [
  4711. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4712. 'multiple' => 'multiple'
  4713. ],
  4714. ['option' => ['value' => '1', 'selected' => 'selected']],
  4715. 'blue',
  4716. '/option',
  4717. ['option' => ['value' => '2']],
  4718. 'red',
  4719. '/option',
  4720. ['option' => ['value' => '3', 'selected' => 'selected']],
  4721. 'green',
  4722. '/option',
  4723. '/select',
  4724. '/div'
  4725. ];
  4726. $this->assertHtml($expected, $result);
  4727. // make sure only 50 is selected, and not 50f5c0cf
  4728. $options = [
  4729. '1' => 'blue',
  4730. '50f5c0cf' => 'red',
  4731. '50' => 'green'
  4732. ];
  4733. $tags = [
  4734. new Entity(['id' => 1, 'name' => 'blue']),
  4735. new Entity(['id' => 50, 'name' => 'green'])
  4736. ];
  4737. $article = new Article(['tags' => $tags]);
  4738. $this->Form->create($article);
  4739. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4740. $expected = [
  4741. 'div' => ['class' => 'input select'],
  4742. 'label' => ['for' => 'tags-ids'],
  4743. 'Tags',
  4744. '/label',
  4745. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4746. 'select' => [
  4747. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4748. 'multiple' => 'multiple'
  4749. ],
  4750. ['option' => ['value' => '1', 'selected' => 'selected']],
  4751. 'blue',
  4752. '/option',
  4753. ['option' => ['value' => '50f5c0cf']],
  4754. 'red',
  4755. '/option',
  4756. ['option' => ['value' => '50', 'selected' => 'selected']],
  4757. 'green',
  4758. '/option',
  4759. '/select',
  4760. '/div'
  4761. ];
  4762. $this->assertHtml($expected, $result);
  4763. $spacecraft = [
  4764. 1 => 'Orion',
  4765. 2 => 'Helios'
  4766. ];
  4767. $this->View->viewVars['spacecraft'] = $spacecraft;
  4768. $this->Form->create();
  4769. $result = $this->Form->input('spacecraft._ids');
  4770. $expected = [
  4771. 'div' => ['class' => 'input select'],
  4772. 'label' => ['for' => 'spacecraft-ids'],
  4773. 'Spacecraft',
  4774. '/label',
  4775. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4776. 'select' => [
  4777. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4778. 'multiple' => 'multiple'
  4779. ],
  4780. ['option' => ['value' => '1']],
  4781. 'Orion',
  4782. '/option',
  4783. ['option' => ['value' => '2']],
  4784. 'Helios',
  4785. '/option',
  4786. '/select',
  4787. '/div'
  4788. ];
  4789. $this->assertHtml($expected, $result);
  4790. }
  4791. /**
  4792. * testErrorsForBelongsToManySelect method
  4793. *
  4794. * Tests that errors for belongsToMany select fields are being
  4795. * picked up properly.
  4796. *
  4797. * @return void
  4798. */
  4799. public function testErrorsForBelongsToManySelect()
  4800. {
  4801. $spacecraft = [
  4802. 1 => 'Orion',
  4803. 2 => 'Helios'
  4804. ];
  4805. $this->View->viewVars['spacecraft'] = $spacecraft;
  4806. $article = new Article();
  4807. $article->errors('spacecraft', ['Invalid']);
  4808. $this->Form->create($article);
  4809. $result = $this->Form->input('spacecraft._ids');
  4810. $expected = [
  4811. ['div' => ['class' => 'input select error']],
  4812. 'label' => ['for' => 'spacecraft-ids'],
  4813. 'Spacecraft',
  4814. '/label',
  4815. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4816. 'select' => [
  4817. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4818. 'multiple' => 'multiple'
  4819. ],
  4820. ['option' => ['value' => '1']],
  4821. 'Orion',
  4822. '/option',
  4823. ['option' => ['value' => '2']],
  4824. 'Helios',
  4825. '/option',
  4826. '/select',
  4827. ['div' => ['class' => 'error-message']],
  4828. 'Invalid',
  4829. '/div',
  4830. '/div'
  4831. ];
  4832. $this->assertHtml($expected, $result);
  4833. }
  4834. /**
  4835. * testSelectMultipleCheckboxes method
  4836. *
  4837. * Test generation of multi select elements in checkbox format.
  4838. *
  4839. * @return void
  4840. */
  4841. public function testSelectMultipleCheckboxes()
  4842. {
  4843. $result = $this->Form->select(
  4844. 'Model.multi_field',
  4845. ['first', 'second', 'third'],
  4846. ['multiple' => 'checkbox']
  4847. );
  4848. $expected = [
  4849. 'input' => [
  4850. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4851. ],
  4852. ['div' => ['class' => 'checkbox']],
  4853. ['label' => ['for' => 'model-multi-field-0']],
  4854. ['input' => [
  4855. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4856. 'value' => '0', 'id' => 'model-multi-field-0'
  4857. ]],
  4858. 'first',
  4859. '/label',
  4860. '/div',
  4861. ['div' => ['class' => 'checkbox']],
  4862. ['label' => ['for' => 'model-multi-field-1']],
  4863. ['input' => [
  4864. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4865. 'value' => '1', 'id' => 'model-multi-field-1'
  4866. ]],
  4867. 'second',
  4868. '/label',
  4869. '/div',
  4870. ['div' => ['class' => 'checkbox']],
  4871. ['label' => ['for' => 'model-multi-field-2']],
  4872. ['input' => [
  4873. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4874. 'value' => '2', 'id' => 'model-multi-field-2'
  4875. ]],
  4876. 'third',
  4877. '/label',
  4878. '/div'
  4879. ];
  4880. $this->assertHtml($expected, $result);
  4881. $result = $this->Form->select(
  4882. 'Model.multi_field',
  4883. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4884. ['multiple' => 'checkbox']
  4885. );
  4886. $expected = [
  4887. 'input' => [
  4888. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4889. ],
  4890. ['div' => ['class' => 'checkbox']],
  4891. ['label' => ['for' => 'model-multi-field-a+']],
  4892. ['input' => [
  4893. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4894. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4895. ]],
  4896. 'first',
  4897. '/label',
  4898. '/div',
  4899. ['div' => ['class' => 'checkbox']],
  4900. ['label' => ['for' => 'model-multi-field-a++']],
  4901. ['input' => [
  4902. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4903. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4904. ]],
  4905. 'second',
  4906. '/label',
  4907. '/div',
  4908. ['div' => ['class' => 'checkbox']],
  4909. ['label' => ['for' => 'model-multi-field-a+++']],
  4910. ['input' => [
  4911. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4912. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4913. ]],
  4914. 'third',
  4915. '/label',
  4916. '/div'
  4917. ];
  4918. $this->assertHtml($expected, $result);
  4919. $result = $this->Form->select(
  4920. 'Model.multi_field',
  4921. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4922. ['multiple' => 'checkbox']
  4923. );
  4924. $expected = [
  4925. 'input' => [
  4926. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4927. ],
  4928. ['div' => ['class' => 'checkbox']],
  4929. ['label' => ['for' => 'model-multi-field-a-b']],
  4930. ['input' => [
  4931. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4932. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4933. ]],
  4934. 'first',
  4935. '/label',
  4936. '/div',
  4937. ['div' => ['class' => 'checkbox']],
  4938. ['label' => ['for' => 'model-multi-field-a-b1']],
  4939. ['input' => [
  4940. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4941. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4942. ]],
  4943. 'second',
  4944. '/label',
  4945. '/div',
  4946. ['div' => ['class' => 'checkbox']],
  4947. ['label' => ['for' => 'model-multi-field-a-b2']],
  4948. ['input' => [
  4949. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4950. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4951. ]],
  4952. 'third',
  4953. '/label',
  4954. '/div'
  4955. ];
  4956. $this->assertHtml($expected, $result);
  4957. }
  4958. /**
  4959. * testSelectMultipleCheckboxRequestData method
  4960. *
  4961. * Ensure that multiCheckbox reads from the request data.
  4962. *
  4963. * @return void
  4964. */
  4965. public function testSelectMultipleCheckboxRequestData()
  4966. {
  4967. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4968. $result = $this->Form->select(
  4969. 'Model.tags',
  4970. ['1' => 'first', 'Array' => 'Array'],
  4971. ['multiple' => 'checkbox']
  4972. );
  4973. $expected = [
  4974. 'input' => [
  4975. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4976. ],
  4977. ['div' => ['class' => 'checkbox']],
  4978. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4979. ['input' => [
  4980. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4981. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4982. ]],
  4983. 'first',
  4984. '/label',
  4985. '/div',
  4986. ['div' => ['class' => 'checkbox']],
  4987. ['label' => ['for' => 'model-tags-array']],
  4988. ['input' => [
  4989. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4990. 'value' => 'Array', 'id' => 'model-tags-array'
  4991. ]],
  4992. 'Array',
  4993. '/label',
  4994. '/div'
  4995. ];
  4996. $this->assertHtml($expected, $result);
  4997. }
  4998. /**
  4999. * testSelectMultipleCheckboxSecurity method
  5000. *
  5001. * Checks the security hash array generated for multiple-input checkbox elements.
  5002. *
  5003. * @return void
  5004. */
  5005. public function testSelectMultipleCheckboxSecurity()
  5006. {
  5007. $this->Form->request->params['_Token'] = 'testKey';
  5008. $this->assertEquals([], $this->Form->fields);
  5009. $this->Form->select(
  5010. 'Model.multi_field',
  5011. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5012. ['multiple' => 'checkbox']
  5013. );
  5014. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5015. $result = $this->Form->secure($this->Form->fields);
  5016. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5017. $this->assertRegExp('/"' . $key . '"/', $result);
  5018. }
  5019. /**
  5020. * testSelectMultipleSecureWithNoOptions method
  5021. *
  5022. * Multiple select elements should always be secured as they always participate
  5023. * in the POST data.
  5024. *
  5025. * @return void
  5026. */
  5027. public function testSelectMultipleSecureWithNoOptions()
  5028. {
  5029. $this->assertEquals([], $this->Form->fields);
  5030. $this->Form->select(
  5031. 'Model.select',
  5032. [],
  5033. ['multiple' => true]
  5034. );
  5035. $this->assertEquals(['Model.select'], $this->Form->fields);
  5036. }
  5037. /**
  5038. * testSelectNoSecureWithNoOptions method
  5039. *
  5040. * When a select box has no options it should not be added to the fields list
  5041. * as it always fail post validation.
  5042. *
  5043. * @return void
  5044. */
  5045. public function testSelectNoSecureWithNoOptions()
  5046. {
  5047. $this->Form->request->params['_Token'] = 'testkey';
  5048. $this->assertEquals([], $this->Form->fields);
  5049. $this->Form->select(
  5050. 'Model.select',
  5051. []
  5052. );
  5053. $this->assertEquals([], $this->Form->fields);
  5054. $this->Form->select(
  5055. 'Model.user_id',
  5056. [],
  5057. ['empty' => true]
  5058. );
  5059. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5060. }
  5061. /**
  5062. * testInputMultipleCheckboxes method
  5063. *
  5064. * Test input() resulting in multi select elements being generated.
  5065. *
  5066. * @return void
  5067. */
  5068. public function testInputMultipleCheckboxes()
  5069. {
  5070. $result = $this->Form->input('Model.multi_field', [
  5071. 'options' => ['first', 'second', 'third'],
  5072. 'multiple' => 'checkbox'
  5073. ]);
  5074. $expected = [
  5075. ['div' => ['class' => 'input select']],
  5076. ['label' => ['for' => 'model-multi-field']],
  5077. 'Multi Field',
  5078. '/label',
  5079. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5080. ['div' => ['class' => 'checkbox']],
  5081. ['label' => ['for' => 'model-multi-field-0']],
  5082. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5083. 'first',
  5084. '/label',
  5085. '/div',
  5086. ['div' => ['class' => 'checkbox']],
  5087. ['label' => ['for' => 'model-multi-field-1']],
  5088. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5089. 'second',
  5090. '/label',
  5091. '/div',
  5092. ['div' => ['class' => 'checkbox']],
  5093. ['label' => ['for' => 'model-multi-field-2']],
  5094. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5095. 'third',
  5096. '/label',
  5097. '/div',
  5098. '/div'
  5099. ];
  5100. $this->assertHtml($expected, $result);
  5101. $result = $this->Form->input('Model.multi_field', [
  5102. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5103. 'multiple' => 'checkbox'
  5104. ]);
  5105. $expected = [
  5106. ['div' => ['class' => 'input select']],
  5107. ['label' => ['for' => 'model-multi-field']],
  5108. 'Multi Field',
  5109. '/label',
  5110. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5111. ['div' => ['class' => 'checkbox']],
  5112. ['label' => ['for' => 'model-multi-field-a']],
  5113. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5114. 'first',
  5115. '/label',
  5116. '/div',
  5117. ['div' => ['class' => 'checkbox']],
  5118. ['label' => ['for' => 'model-multi-field-b']],
  5119. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5120. 'second',
  5121. '/label',
  5122. '/div',
  5123. ['div' => ['class' => 'checkbox']],
  5124. ['label' => ['for' => 'model-multi-field-c']],
  5125. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5126. 'third',
  5127. '/label',
  5128. '/div',
  5129. '/div'
  5130. ];
  5131. $this->assertHtml($expected, $result);
  5132. }
  5133. /**
  5134. * testSelectHiddenFieldOmission method
  5135. *
  5136. * Test that select() with 'hiddenField' => false omits the hidden field.
  5137. *
  5138. * @return void
  5139. */
  5140. public function testSelectHiddenFieldOmission()
  5141. {
  5142. $result = $this->Form->select(
  5143. 'Model.multi_field',
  5144. ['first', 'second'],
  5145. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5146. );
  5147. $this->assertNotContains('type="hidden"', $result);
  5148. }
  5149. /**
  5150. * testSelectCheckboxMultipleOverrideName method
  5151. *
  5152. * Test that select() with multiple = checkbox works with overriding name attribute.
  5153. *
  5154. * @return void
  5155. */
  5156. public function testSelectCheckboxMultipleOverrideName()
  5157. {
  5158. $result = $this->Form->select('category', ['1', '2'], [
  5159. 'multiple' => 'checkbox',
  5160. 'name' => 'fish',
  5161. ]);
  5162. $expected = [
  5163. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5164. ['div' => ['class' => 'checkbox']],
  5165. ['label' => ['for' => 'fish-0']],
  5166. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5167. '1',
  5168. '/label',
  5169. '/div',
  5170. ['div' => ['class' => 'checkbox']],
  5171. ['label' => ['for' => 'fish-1']],
  5172. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5173. '2',
  5174. '/label',
  5175. '/div'
  5176. ];
  5177. $this->assertHtml($expected, $result);
  5178. $result = $this->Form->multiCheckbox(
  5179. 'category',
  5180. new Collection(['1', '2']),
  5181. ['name' => 'fish']
  5182. );
  5183. $this->assertHtml($expected, $result);
  5184. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5185. 'name' => 'fish',
  5186. ]);
  5187. $this->assertHtml($expected, $result);
  5188. }
  5189. /**
  5190. * testInputMultiCheckbox method
  5191. *
  5192. * Test that input() works with multicheckbox.
  5193. *
  5194. * @return void
  5195. */
  5196. public function testInputMultiCheckbox()
  5197. {
  5198. $result = $this->Form->input('category', [
  5199. 'type' => 'multicheckbox',
  5200. 'options' => ['1', '2'],
  5201. ]);
  5202. $expected = [
  5203. ['div' => ['class' => 'input multicheckbox']],
  5204. '<label',
  5205. 'Category',
  5206. '/label',
  5207. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5208. ['div' => ['class' => 'checkbox']],
  5209. ['label' => ['for' => 'category-0']],
  5210. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5211. '1',
  5212. '/label',
  5213. '/div',
  5214. ['div' => ['class' => 'checkbox']],
  5215. ['label' => ['for' => 'category-1']],
  5216. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5217. '2',
  5218. '/label',
  5219. '/div',
  5220. '/div',
  5221. ];
  5222. $this->assertHtml($expected, $result);
  5223. }
  5224. /**
  5225. * testCheckbox method
  5226. *
  5227. * Test generation of checkboxes.
  5228. *
  5229. * @return void
  5230. */
  5231. public function testCheckbox()
  5232. {
  5233. $result = $this->Form->checkbox('Model.field');
  5234. $expected = [
  5235. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5236. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5237. ];
  5238. $this->assertHtml($expected, $result);
  5239. $result = $this->Form->checkbox('Model.field', [
  5240. 'id' => 'theID',
  5241. 'value' => 'myvalue',
  5242. 'form' => 'my-form',
  5243. ]);
  5244. $expected = [
  5245. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5246. ['input' => [
  5247. 'type' => 'checkbox', 'name' => 'Model[field]',
  5248. 'value' => 'myvalue', 'id' => 'theID',
  5249. 'form' => 'my-form',
  5250. ]]
  5251. ];
  5252. $this->assertHtml($expected, $result);
  5253. }
  5254. /**
  5255. * testCheckboxDefaultValue method
  5256. *
  5257. * Test default value setting on checkbox() method.
  5258. *
  5259. * @return void
  5260. */
  5261. public function testCheckboxDefaultValue()
  5262. {
  5263. $this->Form->request->data['Model']['field'] = false;
  5264. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5265. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5266. $this->assertHtml($expected, $result);
  5267. unset($this->Form->request->data['Model']['field']);
  5268. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5269. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5270. $this->assertHtml($expected, $result);
  5271. $this->Form->request->data['Model']['field'] = true;
  5272. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5273. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5274. $this->assertHtml($expected, $result);
  5275. unset($this->Form->request->data['Model']['field']);
  5276. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5277. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5278. $this->assertHtml($expected, $result);
  5279. $Articles = TableRegistry::get('Articles');
  5280. $Articles->schema()->addColumn(
  5281. 'published',
  5282. ['type' => 'boolean', 'null' => false, 'default' => true]
  5283. );
  5284. $this->Form->create($Articles->newEntity());
  5285. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5286. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5287. $this->assertHtml($expected, $result);
  5288. }
  5289. /**
  5290. * testCheckboxCheckedAndError method
  5291. *
  5292. * Test checkbox being checked or having errors.
  5293. *
  5294. * @return void
  5295. */
  5296. public function testCheckboxCheckedAndError()
  5297. {
  5298. $this->article['errors'] = [
  5299. 'published' => true
  5300. ];
  5301. $this->Form->request->data['published'] = 'myvalue';
  5302. $this->Form->create($this->article);
  5303. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5304. $expected = [
  5305. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5306. ['input' => [
  5307. 'type' => 'checkbox',
  5308. 'name' => 'published',
  5309. 'value' => 'myvalue',
  5310. 'id' => 'theID',
  5311. 'checked' => 'checked',
  5312. 'class' => 'form-error'
  5313. ]]
  5314. ];
  5315. $this->assertHtml($expected, $result);
  5316. $this->Form->request->data['published'] = '';
  5317. $result = $this->Form->checkbox('published');
  5318. $expected = [
  5319. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5320. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5321. ];
  5322. $this->assertHtml($expected, $result);
  5323. }
  5324. /**
  5325. * testCheckboxCustomNameAttribute method
  5326. *
  5327. * Test checkbox() with a custom name attribute.
  5328. *
  5329. * @return void
  5330. */
  5331. public function testCheckboxCustomNameAttribute()
  5332. {
  5333. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5334. $expected = [
  5335. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5336. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5337. ];
  5338. $this->assertHtml($expected, $result);
  5339. }
  5340. /**
  5341. * testCheckboxHiddenField method
  5342. *
  5343. * Test that the hidden input for checkboxes can be omitted or set to a
  5344. * specific value.
  5345. *
  5346. * @return void
  5347. */
  5348. public function testCheckboxHiddenField()
  5349. {
  5350. $result = $this->Form->checkbox('UserForm.something', [
  5351. 'hiddenField' => false
  5352. ]);
  5353. $expected = [
  5354. 'input' => [
  5355. 'type' => 'checkbox',
  5356. 'name' => 'UserForm[something]',
  5357. 'value' => '1'
  5358. ],
  5359. ];
  5360. $this->assertHtml($expected, $result);
  5361. $result = $this->Form->checkbox('UserForm.something', [
  5362. 'value' => 'Y',
  5363. 'hiddenField' => 'N',
  5364. ]);
  5365. $expected = [
  5366. ['input' => [
  5367. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5368. 'value' => 'N'
  5369. ]],
  5370. ['input' => [
  5371. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5372. 'value' => 'Y'
  5373. ]],
  5374. ];
  5375. $this->assertHtml($expected, $result);
  5376. }
  5377. /**
  5378. * testTime method
  5379. *
  5380. * Test the time type.
  5381. *
  5382. * @return void
  5383. */
  5384. public function testTime()
  5385. {
  5386. $result = $this->Form->time('start_time', [
  5387. 'timeFormat' => 12,
  5388. 'interval' => 5,
  5389. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5390. ]);
  5391. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5392. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5393. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5394. $this->assertNotContains('year', $result);
  5395. $this->assertNotContains('month', $result);
  5396. $this->assertNotContains('day', $result);
  5397. $result = $this->Form->time('start_time', [
  5398. 'timeFormat' => 12,
  5399. 'interval' => 5,
  5400. 'value' => '2014-03-08 16:30:00'
  5401. ]);
  5402. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5403. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5404. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5405. $this->assertNotContains('year', $result);
  5406. $this->assertNotContains('month', $result);
  5407. $this->assertNotContains('day', $result);
  5408. }
  5409. /**
  5410. * testTimeFormat24NoMeridian method
  5411. *
  5412. * Ensure that timeFormat=24 has no merdian.
  5413. *
  5414. * @return void.
  5415. */
  5416. public function testTimeFormat24NoMeridian()
  5417. {
  5418. $result = $this->Form->time('start_time', [
  5419. 'timeFormat' => 24,
  5420. 'interval' => 5,
  5421. 'value' => '2014-03-08 16:30:00'
  5422. ]);
  5423. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5424. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5425. $this->assertNotContains('meridian', $result);
  5426. $this->assertNotContains('pm', $result);
  5427. $this->assertNotContains('year', $result);
  5428. $this->assertNotContains('month', $result);
  5429. $this->assertNotContains('day', $result);
  5430. }
  5431. /**
  5432. * testDate method
  5433. *
  5434. * Test the date type.
  5435. *
  5436. * @return void
  5437. */
  5438. public function testDate()
  5439. {
  5440. $result = $this->Form->date('start_day', [
  5441. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5442. ]);
  5443. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5444. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5445. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5446. $this->assertNotContains('hour', $result);
  5447. $this->assertNotContains('minute', $result);
  5448. $this->assertNotContains('second', $result);
  5449. $this->assertNotContains('meridian', $result);
  5450. }
  5451. /**
  5452. * testDateTime method
  5453. *
  5454. * Test generation of date/time select elements.
  5455. *
  5456. * @return void
  5457. */
  5458. public function testDateTime()
  5459. {
  5460. extract($this->dateRegex);
  5461. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5462. $now = strtotime('now');
  5463. $expected = [
  5464. ['select' => ['name' => 'Contact[date][year]']],
  5465. ['option' => ['value' => '']],
  5466. '/option',
  5467. $yearsRegex,
  5468. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5469. date('Y', $now),
  5470. '/option',
  5471. '*/select',
  5472. ['select' => ['name' => 'Contact[date][month]']],
  5473. ['option' => ['value' => '']],
  5474. '/option',
  5475. $monthsRegex,
  5476. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5477. date('F', $now),
  5478. '/option',
  5479. '*/select',
  5480. ['select' => ['name' => 'Contact[date][day]']],
  5481. ['option' => ['value' => '']],
  5482. '/option',
  5483. $daysRegex,
  5484. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5485. date('j', $now),
  5486. '/option',
  5487. '*/select',
  5488. ['select' => ['name' => 'Contact[date][hour]']],
  5489. ['option' => ['value' => '']],
  5490. '/option',
  5491. $hoursRegex,
  5492. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5493. date('G', $now),
  5494. '/option',
  5495. '*/select',
  5496. ['select' => ['name' => 'Contact[date][minute]']],
  5497. ['option' => ['value' => '']],
  5498. '/option',
  5499. $minutesRegex,
  5500. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5501. date('i', $now),
  5502. '/option',
  5503. '*/select',
  5504. ];
  5505. $this->assertHtml($expected, $result);
  5506. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5507. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5508. $now = strtotime('now');
  5509. $expected = [
  5510. ['select' => ['name' => 'Contact[date][year]']],
  5511. $yearsRegex,
  5512. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5513. date('Y', $now),
  5514. '/option',
  5515. '*/select',
  5516. ['select' => ['name' => 'Contact[date][month]']],
  5517. $monthsRegex,
  5518. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5519. date('F', $now),
  5520. '/option',
  5521. '*/select',
  5522. ['select' => ['name' => 'Contact[date][day]']],
  5523. $daysRegex,
  5524. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5525. date('j', $now),
  5526. '/option',
  5527. '*/select',
  5528. ['select' => ['name' => 'Contact[date][hour]']],
  5529. $hoursRegex,
  5530. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5531. date('G', $now),
  5532. '/option',
  5533. '*/select',
  5534. ['select' => ['name' => 'Contact[date][minute]']],
  5535. $minutesRegex,
  5536. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5537. date('i', $now),
  5538. '/option',
  5539. '*/select',
  5540. ];
  5541. $this->assertHtml($expected, $result);
  5542. }
  5543. /**
  5544. * testDateTimeSecured method
  5545. *
  5546. * Test that datetime fields are added to protected fields list.
  5547. *
  5548. * @return void
  5549. */
  5550. public function testDateTimeSecured()
  5551. {
  5552. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5553. $this->Form->dateTime('Contact.date');
  5554. $expected = [
  5555. 'Contact.date.year',
  5556. 'Contact.date.month',
  5557. 'Contact.date.day',
  5558. 'Contact.date.hour',
  5559. 'Contact.date.minute',
  5560. ];
  5561. $this->assertEquals($expected, $this->Form->fields);
  5562. $this->Form->fields = [];
  5563. $this->Form->date('Contact.published');
  5564. $expected = [
  5565. 'Contact.published.year',
  5566. 'Contact.published.month',
  5567. 'Contact.published.day',
  5568. ];
  5569. $this->assertEquals($expected, $this->Form->fields);
  5570. }
  5571. /**
  5572. * testDateTimeSecuredDisabled method
  5573. *
  5574. * Test that datetime fields are added to protected fields list.
  5575. *
  5576. * @return void
  5577. */
  5578. public function testDateTimeSecuredDisabled()
  5579. {
  5580. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5581. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5582. $expected = [];
  5583. $this->assertEquals($expected, $this->Form->fields);
  5584. $this->Form->fields = [];
  5585. $this->Form->date('Contact.published', ['secure' => false]);
  5586. $expected = [];
  5587. $this->assertEquals($expected, $this->Form->fields);
  5588. }
  5589. /**
  5590. * testDatetimeEmpty method
  5591. *
  5592. * Test empty defaulting to true for datetime.
  5593. *
  5594. * @return void
  5595. */
  5596. public function testDatetimeEmpty()
  5597. {
  5598. extract($this->dateRegex);
  5599. $result = $this->Form->dateTime('Contact.date', [
  5600. 'timeFormat' => 12,
  5601. 'empty' => true,
  5602. 'default' => true
  5603. ]);
  5604. $expected = [
  5605. ['select' => ['name' => 'Contact[date][year]']],
  5606. $yearsRegex,
  5607. ['option' => ['value' => '']],
  5608. '/option',
  5609. '*/select',
  5610. ['select' => ['name' => 'Contact[date][month]']],
  5611. $monthsRegex,
  5612. ['option' => ['value' => '']],
  5613. '/option',
  5614. '*/select',
  5615. ['select' => ['name' => 'Contact[date][day]']],
  5616. $daysRegex,
  5617. ['option' => ['value' => '']],
  5618. '/option',
  5619. '*/select',
  5620. ['select' => ['name' => 'Contact[date][hour]']],
  5621. $hoursRegex,
  5622. ['option' => ['value' => '']],
  5623. '/option',
  5624. '*/select',
  5625. ['select' => ['name' => 'Contact[date][minute]']],
  5626. $minutesRegex,
  5627. ['option' => ['value' => '']],
  5628. '/option',
  5629. '*/select',
  5630. ['select' => ['name' => 'Contact[date][meridian]']],
  5631. $meridianRegex,
  5632. ['option' => ['value' => '']],
  5633. '/option',
  5634. '*/select'
  5635. ];
  5636. $this->assertHtml($expected, $result);
  5637. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5638. }
  5639. /**
  5640. * testDatetimeMinuteInterval method
  5641. *
  5642. * Test datetime with interval option.
  5643. *
  5644. * @return void
  5645. */
  5646. public function testDatetimeMinuteInterval()
  5647. {
  5648. extract($this->dateRegex);
  5649. $result = $this->Form->dateTime('Contact.date', [
  5650. 'interval' => 5,
  5651. 'value' => ''
  5652. ]);
  5653. $expected = [
  5654. ['select' => ['name' => 'Contact[date][year]']],
  5655. $yearsRegex,
  5656. ['option' => ['selected' => 'selected', 'value' => '']],
  5657. '/option',
  5658. '*/select',
  5659. ['select' => ['name' => 'Contact[date][month]']],
  5660. $monthsRegex,
  5661. ['option' => ['selected' => 'selected', 'value' => '']],
  5662. '/option',
  5663. '*/select',
  5664. ['select' => ['name' => 'Contact[date][day]']],
  5665. $daysRegex,
  5666. ['option' => ['selected' => 'selected', 'value' => '']],
  5667. '/option',
  5668. '*/select',
  5669. ['select' => ['name' => 'Contact[date][hour]']],
  5670. $hoursRegex,
  5671. ['option' => ['selected' => 'selected', 'value' => '']],
  5672. '/option',
  5673. '*/select',
  5674. ['select' => ['name' => 'Contact[date][minute]']],
  5675. $minutesRegex,
  5676. ['option' => ['selected' => 'selected', 'value' => '']],
  5677. '/option',
  5678. ['option' => ['value' => '00']],
  5679. '00',
  5680. '/option',
  5681. ['option' => ['value' => '05']],
  5682. '05',
  5683. '/option',
  5684. ['option' => ['value' => '10']],
  5685. '10',
  5686. '/option',
  5687. '*/select',
  5688. ];
  5689. $this->assertHtml($expected, $result);
  5690. }
  5691. /**
  5692. * testDateTimeRounding method
  5693. *
  5694. * Test dateTime with rounding.
  5695. *
  5696. * @return void
  5697. */
  5698. public function testDateTimeRounding()
  5699. {
  5700. $this->Form->request->data['Contact'] = [
  5701. 'date' => [
  5702. 'day' => '13',
  5703. 'month' => '12',
  5704. 'year' => '2010',
  5705. 'hour' => '04',
  5706. 'minute' => '19',
  5707. 'meridian' => 'AM'
  5708. ]
  5709. ];
  5710. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5711. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5712. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5713. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5714. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5715. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5716. }
  5717. /**
  5718. * testDatetimeWithDefault method
  5719. *
  5720. * Test that datetime() and default values work.
  5721. *
  5722. * @return void
  5723. */
  5724. public function testDatetimeWithDefault()
  5725. {
  5726. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5727. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5728. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5729. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5730. $result = $this->Form->dateTime('Contact.updated', [
  5731. 'default' => '2009-06-01 11:15:30'
  5732. ]);
  5733. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5734. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5735. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5736. }
  5737. /**
  5738. * testDateTimeAllZeros method
  5739. *
  5740. * Test datetime() with all zeros.
  5741. *
  5742. * @return void
  5743. */
  5744. public function testDateTimeAllZeros()
  5745. {
  5746. $result = $this->Form->dateTime('Contact.date', [
  5747. 'timeFormat' => false,
  5748. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5749. 'value' => '0000-00-00'
  5750. ]);
  5751. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5752. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5753. }
  5754. /**
  5755. * testDateTimeEmptyAsArray method
  5756. *
  5757. * @return void
  5758. */
  5759. public function testDateTimeEmptyAsArray()
  5760. {
  5761. $result = $this->Form->dateTime('Contact.date', [
  5762. 'empty' => [
  5763. 'day' => 'DAY',
  5764. 'month' => 'MONTH',
  5765. 'year' => 'YEAR',
  5766. 'hour' => 'HOUR',
  5767. 'minute' => 'MINUTE',
  5768. 'meridian' => false
  5769. ],
  5770. 'default' => true
  5771. ]);
  5772. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5773. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5774. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5775. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5776. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5777. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5778. $result = $this->Form->dateTime('Contact.date', [
  5779. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5780. 'default' => true
  5781. ]);
  5782. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5783. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5784. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5785. }
  5786. /**
  5787. * testFormDateTimeMulti method
  5788. *
  5789. * Test multiple datetime element generation.
  5790. *
  5791. * @return void
  5792. */
  5793. public function testFormDateTimeMulti()
  5794. {
  5795. extract($this->dateRegex);
  5796. $result = $this->Form->dateTime('Contact.1.updated');
  5797. $this->assertContains('Contact[1][updated][month]', $result);
  5798. $this->assertContains('Contact[1][updated][day]', $result);
  5799. $this->assertContains('Contact[1][updated][year]', $result);
  5800. $this->assertContains('Contact[1][updated][hour]', $result);
  5801. $this->assertContains('Contact[1][updated][minute]', $result);
  5802. }
  5803. /**
  5804. * testDateTimeLabelIdMatchesFirstInput method
  5805. *
  5806. * When changing the date format, the label should always focus the first select box when
  5807. * clicked.
  5808. *
  5809. * @return void
  5810. */
  5811. public function testDateTimeLabelIdMatchesFirstInput()
  5812. {
  5813. $result = $this->Form->input('Model.date', ['type' => 'date']);
  5814. $this->assertContains('<label>Date</label>', $result);
  5815. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5816. $this->assertContains('<label>Date</label>', $result);
  5817. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5818. $this->assertContains('<label>Date</label>', $result);
  5819. }
  5820. /**
  5821. * testDateTimeSecondOptions method
  5822. *
  5823. * Test datetime second=true.
  5824. *
  5825. * @return void
  5826. */
  5827. public function testDateTimeSecondOptions()
  5828. {
  5829. $result = $this->Form->dateTime('updated', ['second' => true]);
  5830. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5831. $result = $this->Form->dateTime('updated', ['second' => []]);
  5832. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5833. $result = $this->Form->dateTime('updated', ['second' => null]);
  5834. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5835. $result = $this->Form->dateTime('updated', ['second' => false]);
  5836. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5837. }
  5838. /**
  5839. * testMonth method
  5840. *
  5841. * Test generation of a month input.
  5842. *
  5843. * @return void
  5844. */
  5845. public function testMonth()
  5846. {
  5847. $result = $this->Form->month('Model.field', ['value' => '']);
  5848. $expected = [
  5849. ['select' => ['name' => 'Model[field][month]']],
  5850. ['option' => ['value' => '', 'selected' => 'selected']],
  5851. '/option',
  5852. ['option' => ['value' => '01']],
  5853. date('F', strtotime('2008-01-01 00:00:00')),
  5854. '/option',
  5855. ['option' => ['value' => '02']],
  5856. date('F', strtotime('2008-02-01 00:00:00')),
  5857. '/option',
  5858. '*/select',
  5859. ];
  5860. $this->assertHtml($expected, $result);
  5861. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5862. $expected = [
  5863. ['select' => ['name' => 'Model[field][month]']],
  5864. ['option' => ['selected' => 'selected', 'value' => '']],
  5865. '/option',
  5866. ['option' => ['value' => '01']],
  5867. date('F', strtotime('2008-01-01 00:00:00')),
  5868. '/option',
  5869. ['option' => ['value' => '02']],
  5870. date('F', strtotime('2008-02-01 00:00:00')),
  5871. '/option',
  5872. '*/select',
  5873. ];
  5874. $this->assertHtml($expected, $result);
  5875. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5876. $expected = [
  5877. ['select' => ['name' => 'Model[field][month]']],
  5878. ['option' => ['selected' => 'selected', 'value' => '']],
  5879. '/option',
  5880. ['option' => ['value' => '01']],
  5881. '1',
  5882. '/option',
  5883. ['option' => ['value' => '02']],
  5884. '2',
  5885. '/option',
  5886. '*/select',
  5887. ];
  5888. $this->assertHtml($expected, $result);
  5889. $monthNames = [
  5890. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5891. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5892. ];
  5893. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5894. $expected = [
  5895. ['select' => ['name' => 'Model[field][month]']],
  5896. ['option' => ['value' => '']],
  5897. '/option',
  5898. ['option' => ['value' => '01', 'selected' => 'selected']],
  5899. 'Jan',
  5900. '/option',
  5901. ['option' => ['value' => '02']],
  5902. 'Feb',
  5903. '/option',
  5904. '*/select',
  5905. ];
  5906. $this->assertHtml($expected, $result);
  5907. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5908. $result = $this->Form->month('Project.release');
  5909. $expected = [
  5910. ['select' => ['name' => 'Project[release][month]']],
  5911. ['option' => ['value' => '']],
  5912. '/option',
  5913. ['option' => ['value' => '01']],
  5914. 'January',
  5915. '/option',
  5916. ['option' => ['value' => '02', 'selected' => 'selected']],
  5917. 'February',
  5918. '/option',
  5919. '*/select',
  5920. ];
  5921. $this->assertHtml($expected, $result);
  5922. $result = $this->Form->month('Contact.published', [
  5923. 'empty' => 'Published on',
  5924. ]);
  5925. $this->assertContains('Published on', $result);
  5926. }
  5927. /**
  5928. * testDay method
  5929. *
  5930. * Test generation of a day input.
  5931. *
  5932. * @return void
  5933. */
  5934. public function testDay()
  5935. {
  5936. extract($this->dateRegex);
  5937. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5938. $expected = [
  5939. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5940. ['option' => ['selected' => 'selected', 'value' => '']],
  5941. '/option',
  5942. ['option' => ['value' => '01']],
  5943. '1',
  5944. '/option',
  5945. ['option' => ['value' => '02']],
  5946. '2',
  5947. '/option',
  5948. $daysRegex,
  5949. '/select',
  5950. ];
  5951. $this->assertHtml($expected, $result);
  5952. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5953. $result = $this->Form->day('Model.field');
  5954. $expected = [
  5955. ['select' => ['name' => 'Model[field][day]']],
  5956. ['option' => ['value' => '']],
  5957. '/option',
  5958. ['option' => ['value' => '01']],
  5959. '1',
  5960. '/option',
  5961. ['option' => ['value' => '02']],
  5962. '2',
  5963. '/option',
  5964. $daysRegex,
  5965. ['option' => ['value' => '10', 'selected' => 'selected']],
  5966. '10',
  5967. '/option',
  5968. $daysRegex,
  5969. '/select',
  5970. ];
  5971. $this->assertHtml($expected, $result);
  5972. $this->Form->request->data['Model']['field'] = '';
  5973. $result = $this->Form->day('Model.field', ['value' => '10']);
  5974. $expected = [
  5975. ['select' => ['name' => 'Model[field][day]']],
  5976. ['option' => ['value' => '']],
  5977. '/option',
  5978. ['option' => ['value' => '01']],
  5979. '1',
  5980. '/option',
  5981. ['option' => ['value' => '02']],
  5982. '2',
  5983. '/option',
  5984. $daysRegex,
  5985. ['option' => ['value' => '10', 'selected' => 'selected']],
  5986. '10',
  5987. '/option',
  5988. $daysRegex,
  5989. '/select',
  5990. ];
  5991. $this->assertHtml($expected, $result);
  5992. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5993. $result = $this->Form->day('Project.release');
  5994. $expected = [
  5995. ['select' => ['name' => 'Project[release][day]']],
  5996. ['option' => ['value' => '']],
  5997. '/option',
  5998. ['option' => ['value' => '01']],
  5999. '1',
  6000. '/option',
  6001. ['option' => ['value' => '02']],
  6002. '2',
  6003. '/option',
  6004. $daysRegex,
  6005. ['option' => ['value' => '10', 'selected' => 'selected']],
  6006. '10',
  6007. '/option',
  6008. $daysRegex,
  6009. '/select',
  6010. ];
  6011. $this->assertHtml($expected, $result);
  6012. $result = $this->Form->day('Contact.published', [
  6013. 'empty' => 'Published on',
  6014. ]);
  6015. $this->assertContains('Published on', $result);
  6016. }
  6017. /**
  6018. * testMinute method
  6019. *
  6020. * Test generation of a minute input.
  6021. *
  6022. * @return void
  6023. */
  6024. public function testMinute()
  6025. {
  6026. extract($this->dateRegex);
  6027. $result = $this->Form->minute('Model.field', ['value' => '']);
  6028. $expected = [
  6029. ['select' => ['name' => 'Model[field][minute]']],
  6030. ['option' => ['selected' => 'selected', 'value' => '']],
  6031. '/option',
  6032. ['option' => ['value' => '00']],
  6033. '00',
  6034. '/option',
  6035. ['option' => ['value' => '01']],
  6036. '01',
  6037. '/option',
  6038. ['option' => ['value' => '02']],
  6039. '02',
  6040. '/option',
  6041. $minutesRegex,
  6042. '/select',
  6043. ];
  6044. $this->assertHtml($expected, $result);
  6045. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6046. $result = $this->Form->minute('Model.field');
  6047. $expected = [
  6048. ['select' => ['name' => 'Model[field][minute]']],
  6049. ['option' => ['value' => '']],
  6050. '/option',
  6051. ['option' => ['value' => '00']],
  6052. '00',
  6053. '/option',
  6054. ['option' => ['value' => '01']],
  6055. '01',
  6056. '/option',
  6057. ['option' => ['value' => '02']],
  6058. '02',
  6059. '/option',
  6060. $minutesRegex,
  6061. ['option' => ['value' => '12', 'selected' => 'selected']],
  6062. '12',
  6063. '/option',
  6064. $minutesRegex,
  6065. '/select',
  6066. ];
  6067. $this->assertHtml($expected, $result);
  6068. $this->Form->request->data['Model']['field'] = '';
  6069. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6070. $expected = [
  6071. ['select' => ['name' => 'Model[field][minute]']],
  6072. ['option' => ['selected' => 'selected', 'value' => '']],
  6073. '/option',
  6074. ['option' => ['value' => '00']],
  6075. '00',
  6076. '/option',
  6077. ['option' => ['value' => '05']],
  6078. '05',
  6079. '/option',
  6080. ['option' => ['value' => '10']],
  6081. '10',
  6082. '/option',
  6083. $minutesRegex,
  6084. '/select',
  6085. ];
  6086. $this->assertHtml($expected, $result);
  6087. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6088. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6089. $expected = [
  6090. ['select' => ['name' => 'Model[field][minute]']],
  6091. ['option' => ['value' => '']],
  6092. '/option',
  6093. ['option' => ['value' => '00']],
  6094. '00',
  6095. '/option',
  6096. ['option' => ['value' => '05']],
  6097. '05',
  6098. '/option',
  6099. ['option' => ['value' => '10', 'selected' => 'selected']],
  6100. '10',
  6101. '/option',
  6102. $minutesRegex,
  6103. '/select',
  6104. ];
  6105. $this->assertHtml($expected, $result);
  6106. }
  6107. /**
  6108. * testMeridian method
  6109. *
  6110. * Test generating an input for the meridian.
  6111. *
  6112. * @return void
  6113. */
  6114. public function testMeridian()
  6115. {
  6116. extract($this->dateRegex);
  6117. $now = new \DateTime();
  6118. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6119. $expected = [
  6120. ['select' => ['name' => 'Model[field][meridian]']],
  6121. ['option' => ['value' => '']],
  6122. '/option',
  6123. $meridianRegex,
  6124. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6125. $now->format('a'),
  6126. '/option',
  6127. '*/select'
  6128. ];
  6129. $this->assertHtml($expected, $result);
  6130. }
  6131. /**
  6132. * testHour method
  6133. *
  6134. * Test generation of an hour input.
  6135. *
  6136. * @return void
  6137. */
  6138. public function testHour()
  6139. {
  6140. extract($this->dateRegex);
  6141. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6142. $expected = [
  6143. ['select' => ['name' => 'Model[field][hour]']],
  6144. ['option' => ['selected' => 'selected', 'value' => '']],
  6145. '/option',
  6146. ['option' => ['value' => '01']],
  6147. '1',
  6148. '/option',
  6149. ['option' => ['value' => '02']],
  6150. '2',
  6151. '/option',
  6152. $hoursRegex,
  6153. '/select',
  6154. ];
  6155. $this->assertHtml($expected, $result);
  6156. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6157. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6158. $expected = [
  6159. ['select' => ['name' => 'Model[field][hour]']],
  6160. ['option' => ['value' => '']],
  6161. '/option',
  6162. ['option' => ['value' => '01']],
  6163. '1',
  6164. '/option',
  6165. ['option' => ['value' => '02']],
  6166. '2',
  6167. '/option',
  6168. $hoursRegex,
  6169. ['option' => ['value' => '12', 'selected' => 'selected']],
  6170. '12',
  6171. '/option',
  6172. '/select',
  6173. ];
  6174. $this->assertHtml($expected, $result);
  6175. $this->Form->request->data['Model']['field'] = '';
  6176. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6177. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6178. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6179. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6180. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6181. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6182. $expected = [
  6183. ['select' => ['name' => 'Model[field][hour]']],
  6184. ['option' => ['value' => '']],
  6185. '/option',
  6186. ['option' => ['value' => '00', 'selected' => 'selected']],
  6187. '0',
  6188. '/option',
  6189. ['option' => ['value' => '01']],
  6190. '1',
  6191. '/option',
  6192. ['option' => ['value' => '02']],
  6193. '2',
  6194. '/option',
  6195. $hoursRegex,
  6196. '/select',
  6197. ];
  6198. $this->assertHtml($expected, $result);
  6199. unset($this->Form->request->data['Model']['field']);
  6200. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6201. $thisHour = date('H');
  6202. $optValue = date('G');
  6203. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6204. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6205. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6206. $expected = [
  6207. ['select' => ['name' => 'Model[field][hour]']],
  6208. ['option' => ['value' => '']],
  6209. '/option',
  6210. ['option' => ['value' => '00']],
  6211. '0',
  6212. '/option',
  6213. ['option' => ['value' => '01', 'selected' => 'selected']],
  6214. '1',
  6215. '/option',
  6216. ['option' => ['value' => '02']],
  6217. '2',
  6218. '/option',
  6219. $hoursRegex,
  6220. '/select',
  6221. ];
  6222. $this->assertHtml($expected, $result);
  6223. }
  6224. /**
  6225. * testYear method
  6226. *
  6227. * Test generation of a year input.
  6228. *
  6229. * @return void
  6230. */
  6231. public function testYear()
  6232. {
  6233. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6234. $expected = [
  6235. ['select' => ['name' => 'Model[field][year]']],
  6236. ['option' => ['selected' => 'selected', 'value' => '']],
  6237. '/option',
  6238. ['option' => ['value' => '2007']],
  6239. '2007',
  6240. '/option',
  6241. ['option' => ['value' => '2006']],
  6242. '2006',
  6243. '/option',
  6244. '/select',
  6245. ];
  6246. $this->assertHtml($expected, $result);
  6247. $result = $this->Form->year('Model.field', [
  6248. 'value' => '',
  6249. 'minYear' => 2006,
  6250. 'maxYear' => 2007,
  6251. 'orderYear' => 'asc'
  6252. ]);
  6253. $expected = [
  6254. ['select' => ['name' => 'Model[field][year]']],
  6255. ['option' => ['selected' => 'selected', 'value' => '']],
  6256. '/option',
  6257. ['option' => ['value' => '2006']],
  6258. '2006',
  6259. '/option',
  6260. ['option' => ['value' => '2007']],
  6261. '2007',
  6262. '/option',
  6263. '/select',
  6264. ];
  6265. $this->assertHtml($expected, $result);
  6266. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6267. $result = $this->Form->year('Contact.published', [
  6268. 'empty' => false,
  6269. 'minYear' => 2006,
  6270. 'maxYear' => 2007,
  6271. ]);
  6272. $expected = [
  6273. ['select' => ['name' => 'Contact[published][year]']],
  6274. ['option' => ['value' => '2007']],
  6275. '2007',
  6276. '/option',
  6277. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6278. '2006',
  6279. '/option',
  6280. '/select',
  6281. ];
  6282. $this->assertHtml($expected, $result);
  6283. $result = $this->Form->year('Contact.published', [
  6284. 'empty' => 'Published on',
  6285. ]);
  6286. $this->assertContains('Published on', $result);
  6287. }
  6288. /**
  6289. * testInputDatetimePreEpoch method
  6290. *
  6291. * Test minYear being prior to the unix epoch.
  6292. *
  6293. * @return void
  6294. */
  6295. public function testInputDatetimePreEpoch()
  6296. {
  6297. $start = date('Y') - 80;
  6298. $end = date('Y') - 18;
  6299. $result = $this->Form->input('birth_year', [
  6300. 'type' => 'date',
  6301. 'label' => 'Birth Year',
  6302. 'minYear' => $start,
  6303. 'maxYear' => $end,
  6304. 'month' => false,
  6305. 'day' => false,
  6306. ]);
  6307. $this->assertContains('value="' . $start . '">' . $start, $result);
  6308. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6309. $this->assertNotContains('value="00">00', $result);
  6310. }
  6311. /**
  6312. * testYearAutoExpandRange method
  6313. *
  6314. * @return void
  6315. */
  6316. public function testYearAutoExpandRange()
  6317. {
  6318. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6319. $result = $this->Form->year('User.birthday');
  6320. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6321. $result = $matches[1];
  6322. $expected = range(date('Y') + 5, 1930);
  6323. $this->assertEquals($expected, $result);
  6324. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6325. $result = $this->Form->year('Project.release');
  6326. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6327. $result = $matches[1];
  6328. $expected = range(2050, date('Y') - 5);
  6329. $this->assertEquals($expected, $result);
  6330. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6331. $result = $this->Form->year('Project.release', [
  6332. 'minYear' => 1890,
  6333. 'maxYear' => 1900
  6334. ]);
  6335. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6336. $result = $matches[1];
  6337. $expected = range(1900, 1881);
  6338. $this->assertEquals($expected, $result);
  6339. }
  6340. /**
  6341. * testInputDate method
  6342. *
  6343. * Test that input() accepts the type of date and passes options in.
  6344. *
  6345. * @return void
  6346. */
  6347. public function testInputDate()
  6348. {
  6349. $this->Form->request->data = [
  6350. 'month_year' => ['month' => date('m')],
  6351. ];
  6352. $this->Form->create($this->article);
  6353. $result = $this->Form->input('month_year', [
  6354. 'label' => false,
  6355. 'type' => 'date',
  6356. 'minYear' => 2006,
  6357. 'maxYear' => 2008
  6358. ]);
  6359. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6360. $this->assertNotContains('value="2008" selected="selected"', $result);
  6361. }
  6362. /**
  6363. * testInputLabelFalse method
  6364. *
  6365. * Test the label option being set to false.
  6366. *
  6367. * @return void
  6368. */
  6369. public function testInputLabelFalse()
  6370. {
  6371. $this->Form->create($this->article);
  6372. $result = $this->Form->input('title', ['label' => false]);
  6373. $expected = [
  6374. 'div' => ['class' => 'input text required'],
  6375. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6376. '/div'
  6377. ];
  6378. $this->assertHtml($expected, $result);
  6379. }
  6380. /**
  6381. * testInputDateMaxYear method
  6382. *
  6383. * Let's say we want to only allow users born from 2006 to 2008 to register
  6384. * This being the first signup page, we still don't have any data.
  6385. *
  6386. * @return void
  6387. */
  6388. public function testInputDateMaxYear()
  6389. {
  6390. $this->Form->request->data = [];
  6391. $this->Form->create($this->article);
  6392. $result = $this->Form->input('birthday', [
  6393. 'label' => false,
  6394. 'type' => 'date',
  6395. 'minYear' => 2006,
  6396. 'maxYear' => 2008,
  6397. 'default' => true
  6398. ]);
  6399. $this->assertContains('value="2008" selected="selected"', $result);
  6400. $this->assertContains('value="2006"', $result);
  6401. $this->assertNotContains('value="2005"', $result);
  6402. $this->assertNotContains('value="2009"', $result);
  6403. }
  6404. /**
  6405. * testTextArea method
  6406. *
  6407. * Test generation of a textarea input.
  6408. *
  6409. * @return void
  6410. */
  6411. public function testTextArea()
  6412. {
  6413. $this->Form->request->data = ['field' => 'some test data'];
  6414. $result = $this->Form->textarea('field');
  6415. $expected = [
  6416. 'textarea' => ['name' => 'field', 'rows' => 5],
  6417. 'some test data',
  6418. '/textarea',
  6419. ];
  6420. $this->assertHtml($expected, $result);
  6421. $result = $this->Form->textarea('user.bio');
  6422. $expected = [
  6423. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6424. '/textarea',
  6425. ];
  6426. $this->assertHtml($expected, $result);
  6427. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6428. $result = $this->Form->textarea('field');
  6429. $expected = [
  6430. 'textarea' => ['name' => 'field', 'rows' => 5],
  6431. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6432. '/textarea',
  6433. ];
  6434. $this->assertHtml($expected, $result);
  6435. $this->Form->request->data = [
  6436. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6437. ];
  6438. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6439. $expected = [
  6440. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6441. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6442. '/textarea',
  6443. ];
  6444. $this->assertHtml($expected, $result);
  6445. $result = $this->Form->textarea('0.OtherModel.field');
  6446. $expected = [
  6447. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6448. '/textarea'
  6449. ];
  6450. $this->assertHtml($expected, $result);
  6451. }
  6452. /**
  6453. * testTextAreaWithStupidCharacters method
  6454. *
  6455. * Test text area with non-ascii characters.
  6456. *
  6457. * @return void
  6458. */
  6459. public function testTextAreaWithStupidCharacters()
  6460. {
  6461. $result = $this->Form->textarea('Post.content', [
  6462. 'value' => "GREAT®",
  6463. 'rows' => '15',
  6464. 'cols' => '75'
  6465. ]);
  6466. $expected = [
  6467. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6468. 'GREAT®',
  6469. '/textarea',
  6470. ];
  6471. $this->assertHtml($expected, $result);
  6472. }
  6473. /**
  6474. * testTextAreaMaxLength method
  6475. *
  6476. * Test textareas maxlength read from schema.
  6477. *
  6478. * @return void
  6479. */
  6480. public function testTextAreaMaxLength()
  6481. {
  6482. $this->Form->create([
  6483. 'schema' => [
  6484. 'stuff' => ['type' => 'string', 'length' => 10],
  6485. ]
  6486. ]);
  6487. $result = $this->Form->input('other', ['type' => 'textarea']);
  6488. $expected = [
  6489. 'div' => ['class' => 'input textarea'],
  6490. 'label' => ['for' => 'other'],
  6491. 'Other',
  6492. '/label',
  6493. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6494. '/textarea',
  6495. '/div'
  6496. ];
  6497. $this->assertHtml($expected, $result);
  6498. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  6499. $expected = [
  6500. 'div' => ['class' => 'input textarea'],
  6501. 'label' => ['for' => 'stuff'],
  6502. 'Stuff',
  6503. '/label',
  6504. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6505. '/textarea',
  6506. '/div'
  6507. ];
  6508. $this->assertHtml($expected, $result);
  6509. }
  6510. /**
  6511. * testHiddenField method
  6512. *
  6513. * Test generation of a hidden input.
  6514. *
  6515. * @return void
  6516. */
  6517. public function testHiddenField()
  6518. {
  6519. $this->article['errors'] = [
  6520. 'field' => true
  6521. ];
  6522. $this->Form->request->data['field'] = 'test';
  6523. $this->Form->create($this->article);
  6524. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6525. $expected = [
  6526. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6527. $this->assertHtml($expected, $result);
  6528. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6529. $expected = [
  6530. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6531. ];
  6532. $this->assertHtml($expected, $result);
  6533. }
  6534. /**
  6535. * testFileUploadField method
  6536. *
  6537. * Test generation of a file upload input.
  6538. *
  6539. * @return void
  6540. */
  6541. public function testFileUploadField()
  6542. {
  6543. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6544. $result = $this->Form->file('Model.upload');
  6545. $this->assertHtml($expected, $result);
  6546. $this->Form->request->data['Model']['upload'] = [
  6547. 'name' => '', 'type' => '', 'tmp_name' => '',
  6548. 'error' => 4, 'size' => 0
  6549. ];
  6550. $result = $this->Form->file('Model.upload');
  6551. $this->assertHtml($expected, $result);
  6552. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6553. $result = $this->Form->file('Model.upload');
  6554. $this->assertHtml($expected, $result);
  6555. }
  6556. /**
  6557. * testFileUploadOnOtherModel method
  6558. *
  6559. * Test File upload input on a model not used in create().
  6560. *
  6561. * @return void
  6562. */
  6563. public function testFileUploadOnOtherModel()
  6564. {
  6565. $this->Form->create($this->article, ['type' => 'file']);
  6566. $result = $this->Form->file('ValidateProfile.city');
  6567. $expected = [
  6568. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6569. ];
  6570. $this->assertHtml($expected, $result);
  6571. }
  6572. /**
  6573. * testButton method
  6574. *
  6575. * Test generation of a form button.
  6576. *
  6577. * @return void
  6578. */
  6579. public function testButton()
  6580. {
  6581. $result = $this->Form->button('Hi');
  6582. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6583. $this->assertHtml($expected, $result);
  6584. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6585. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6586. $this->assertHtml($expected, $result);
  6587. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6588. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6589. $this->assertHtml($expected, $result);
  6590. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6591. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6592. $this->assertHtml($expected, $result);
  6593. $result = $this->Form->button('No type', ['type' => false]);
  6594. $expected = ['button' => [], 'No type', '/button'];
  6595. $this->assertHtml($expected, $result);
  6596. $result = $this->Form->button('Upload Text', [
  6597. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6598. 'escape' => false
  6599. ]);
  6600. $this->assertNotRegExp('/\&039/', $result);
  6601. }
  6602. /**
  6603. * testButtonUnlockedByDefault method
  6604. *
  6605. * Test that button() makes unlocked fields by default.
  6606. *
  6607. * @return void
  6608. */
  6609. public function testButtonUnlockedByDefault()
  6610. {
  6611. $this->Form->request->params['_csrfToken'] = 'secured';
  6612. $this->Form->button('Save', ['name' => 'save']);
  6613. $this->Form->button('Clear');
  6614. $result = $this->Form->unlockField();
  6615. $this->assertEquals(['save'], $result);
  6616. }
  6617. /**
  6618. * testPostButton method
  6619. *
  6620. * @return void
  6621. */
  6622. public function testPostButton()
  6623. {
  6624. $result = $this->Form->postButton('Hi', '/controller/action');
  6625. $expected = [
  6626. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6627. 'div' => ['style' => 'display:none;'],
  6628. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6629. '/div',
  6630. 'button' => ['type' => 'submit'],
  6631. 'Hi',
  6632. '/button',
  6633. '/form'
  6634. ];
  6635. $this->assertHtml($expected, $result);
  6636. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6637. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6638. }
  6639. /**
  6640. * testPostButtonMethodType method
  6641. *
  6642. * @return void
  6643. */
  6644. public function testPostButtonMethodType()
  6645. {
  6646. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6647. $expected = [
  6648. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6649. 'div' => ['style' => 'display:none;'],
  6650. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6651. '/div',
  6652. 'button' => ['type' => 'submit'],
  6653. 'Hi',
  6654. '/button',
  6655. '/form'
  6656. ];
  6657. $this->assertHtml($expected, $result);
  6658. }
  6659. /**
  6660. * testPostButtonFormOptions method
  6661. *
  6662. * @return void
  6663. */
  6664. public function testPostButtonFormOptions()
  6665. {
  6666. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6667. $expected = [
  6668. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6669. 'div' => ['style' => 'display:none;'],
  6670. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6671. '/div',
  6672. 'button' => ['type' => 'submit'],
  6673. 'Hi',
  6674. '/button',
  6675. '/form'
  6676. ];
  6677. $this->assertHtml($expected, $result);
  6678. }
  6679. /**
  6680. * testPostButtonNestedData method
  6681. *
  6682. * Test using postButton with N dimensional data.
  6683. *
  6684. * @return void
  6685. */
  6686. public function testPostButtonNestedData()
  6687. {
  6688. $data = [
  6689. 'one' => [
  6690. 'two' => [
  6691. 3, 4, 5
  6692. ]
  6693. ]
  6694. ];
  6695. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6696. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6697. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6698. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6699. }
  6700. /**
  6701. * testSecurePostButton method
  6702. *
  6703. * Test that postButton adds _Token fields.
  6704. *
  6705. * @return void
  6706. */
  6707. public function testSecurePostButton()
  6708. {
  6709. $this->Form->request->params['_csrfToken'] = 'testkey';
  6710. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6711. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6712. $tokenDebug = urlencode(json_encode([
  6713. '/posts/delete/1',
  6714. [],
  6715. []
  6716. ]));
  6717. $expected = [
  6718. 'form' => [
  6719. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6720. ],
  6721. ['div' => ['style' => 'display:none;']],
  6722. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6723. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6724. '/div',
  6725. 'button' => ['type' => 'submit'],
  6726. 'Delete',
  6727. '/button',
  6728. ['div' => ['style' => 'display:none;']],
  6729. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6730. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6731. ['input' => [
  6732. 'type' => 'hidden', 'name' => '_Token[debug]',
  6733. 'value' => $tokenDebug,
  6734. ]],
  6735. '/div',
  6736. '/form',
  6737. ];
  6738. $this->assertHtml($expected, $result);
  6739. }
  6740. /**
  6741. * testPostLink method
  6742. *
  6743. * @return void
  6744. */
  6745. public function testPostLink()
  6746. {
  6747. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6748. $expected = [
  6749. 'form' => [
  6750. 'method' => 'post', 'action' => '/posts/delete/1',
  6751. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6752. ],
  6753. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6754. '/form',
  6755. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6756. 'Delete',
  6757. '/a'
  6758. ];
  6759. $this->assertHtml($expected, $result);
  6760. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6761. $expected = [
  6762. 'form' => [
  6763. 'method' => 'post', 'action' => '/posts/delete/1',
  6764. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6765. ],
  6766. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6767. '/form',
  6768. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6769. 'Delete',
  6770. '/a'
  6771. ];
  6772. $this->assertHtml($expected, $result);
  6773. $result = $this->Form->postLink(
  6774. 'Delete',
  6775. '/posts/delete/1',
  6776. ['target' => '_blank', 'class' => 'btn btn-danger']
  6777. );
  6778. $expected = [
  6779. 'form' => [
  6780. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6781. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6782. ],
  6783. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6784. '/form',
  6785. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6786. 'Delete',
  6787. '/a'
  6788. ];
  6789. $this->assertHtml($expected, $result);
  6790. }
  6791. /**
  6792. * testPostLinkWithConfirm method
  6793. *
  6794. * Test the confirm option for postLink().
  6795. *
  6796. * @return void
  6797. */
  6798. public function testPostLinkWithConfirm()
  6799. {
  6800. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6801. $expected = [
  6802. 'form' => [
  6803. 'method' => 'post', 'action' => '/posts/delete/1',
  6804. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6805. ],
  6806. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6807. '/form',
  6808. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6809. 'Delete',
  6810. '/a'
  6811. ];
  6812. $this->assertHtml($expected, $result);
  6813. $result = $this->Form->postLink(
  6814. 'Delete',
  6815. '/posts/delete/1',
  6816. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6817. );
  6818. $expected = [
  6819. 'form' => [
  6820. 'method' => 'post', 'action' => '/posts/delete/1',
  6821. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6822. ],
  6823. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6824. '/form',
  6825. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\nthis \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6826. 'Delete',
  6827. '/a'
  6828. ];
  6829. $this->assertHtml($expected, $result);
  6830. }
  6831. /**
  6832. * testPostLinkWithQuery method
  6833. *
  6834. * Test postLink() with query string args.
  6835. *
  6836. * @return void
  6837. */
  6838. public function testPostLinkWithQuery()
  6839. {
  6840. $result = $this->Form->postLink(
  6841. 'Delete',
  6842. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6843. );
  6844. $expected = [
  6845. 'form' => [
  6846. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6847. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6848. ],
  6849. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6850. '/form',
  6851. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6852. 'Delete',
  6853. '/a'
  6854. ];
  6855. $this->assertHtml($expected, $result);
  6856. }
  6857. /**
  6858. * testPostLinkWithData method
  6859. *
  6860. * Test postLink with additional data.
  6861. *
  6862. * @return void
  6863. */
  6864. public function testPostLinkWithData()
  6865. {
  6866. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6867. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6868. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6869. $this->Form->create($entity);
  6870. $this->Form->end();
  6871. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6872. $this->assertContains(
  6873. '<input type="hidden" name="name" value="show"',
  6874. $result,
  6875. 'should not contain entity data.'
  6876. );
  6877. }
  6878. /**
  6879. * testPostLinkSecurityHash method
  6880. *
  6881. * Test that security hashes for postLink include the url.
  6882. *
  6883. * @return void
  6884. */
  6885. public function testPostLinkSecurityHash()
  6886. {
  6887. $hash = Security::hash(
  6888. '/posts/delete/1' .
  6889. serialize(['id' => '1']) .
  6890. '' .
  6891. Security::salt()
  6892. );
  6893. $hash .= '%3Aid';
  6894. $this->Form->request->params['_Token']['key'] = 'test';
  6895. $result = $this->Form->postLink(
  6896. 'Delete',
  6897. '/posts/delete/1',
  6898. ['data' => ['id' => 1]]
  6899. );
  6900. $tokenDebug = urlencode(json_encode([
  6901. '/posts/delete/1',
  6902. [
  6903. 'id' => 1
  6904. ],
  6905. []
  6906. ]));
  6907. $expected = [
  6908. 'form' => [
  6909. 'method' => 'post', 'action' => '/posts/delete/1',
  6910. 'name', 'style' => 'display:none;'
  6911. ],
  6912. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6913. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6914. 'div' => ['style' => 'display:none;'],
  6915. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6916. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6917. ['input' => [
  6918. 'type' => 'hidden', 'name' => '_Token[debug]',
  6919. 'value' => $tokenDebug,
  6920. ]],
  6921. '/div',
  6922. '/form',
  6923. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6924. 'Delete',
  6925. '/a'
  6926. ];
  6927. $this->assertHtml($expected, $result);
  6928. }
  6929. /**
  6930. * testPostLinkSecurityHashBlockMode method
  6931. *
  6932. * Test that postLink doesn't modify the fields in the containing form.
  6933. *
  6934. * postLink() calls inside open forms should not modify the field list
  6935. * for the form.
  6936. *
  6937. * @return void
  6938. */
  6939. public function testPostLinkSecurityHashBlockMode()
  6940. {
  6941. $hash = Security::hash(
  6942. '/posts/delete/1' .
  6943. serialize([]) .
  6944. '' .
  6945. Security::salt()
  6946. );
  6947. $hash .= '%3A';
  6948. $this->Form->request->params['_Token']['key'] = 'test';
  6949. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6950. $this->Form->input('title');
  6951. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6952. $result = $this->View->fetch('postLink');
  6953. $this->assertEquals(['title'], $this->Form->fields);
  6954. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6955. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6956. }
  6957. /**
  6958. * testPostLinkSecurityHashNoDebugMode method
  6959. *
  6960. * Test that security does not include debug token if debug is false.
  6961. *
  6962. * @return void
  6963. */
  6964. public function testPostLinkSecurityHashNoDebugMode()
  6965. {
  6966. Configure::write('debug', false);
  6967. $hash = Security::hash(
  6968. '/posts/delete/1' .
  6969. serialize(['id' => '1']) .
  6970. '' .
  6971. Security::salt()
  6972. );
  6973. $hash .= '%3Aid';
  6974. $this->Form->request->params['_Token']['key'] = 'test';
  6975. $result = $this->Form->postLink(
  6976. 'Delete',
  6977. '/posts/delete/1',
  6978. ['data' => ['id' => 1]]
  6979. );
  6980. $expected = [
  6981. 'form' => [
  6982. 'method' => 'post', 'action' => '/posts/delete/1',
  6983. 'name', 'style' => 'display:none;'
  6984. ],
  6985. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6986. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6987. 'div' => ['style' => 'display:none;'],
  6988. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6989. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6990. '/div',
  6991. '/form',
  6992. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6993. 'Delete',
  6994. '/a'
  6995. ];
  6996. $this->assertHtml($expected, $result);
  6997. }
  6998. /**
  6999. * testPostLinkNestedData method
  7000. *
  7001. * Test using postLink with N dimensional data.
  7002. *
  7003. * @return void
  7004. */
  7005. public function testPostLinkNestedData()
  7006. {
  7007. $data = [
  7008. 'one' => [
  7009. 'two' => [
  7010. 3, 4, 5
  7011. ]
  7012. ]
  7013. ];
  7014. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7015. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7016. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7017. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7018. }
  7019. /**
  7020. * testPostLinkAfterGetForm method
  7021. *
  7022. * Test creating postLinks after a GET form.
  7023. *
  7024. * @return void
  7025. */
  7026. public function testPostLinkAfterGetForm()
  7027. {
  7028. $this->Form->request->params['_csrfToken'] = 'testkey';
  7029. $this->Form->request->params['_Token'] = 'val';
  7030. $this->Form->create($this->article, ['type' => 'get']);
  7031. $this->Form->end();
  7032. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7033. $tokenDebug = urlencode(json_encode([
  7034. '/posts/delete/1',
  7035. [],
  7036. []
  7037. ]));
  7038. $expected = [
  7039. 'form' => [
  7040. 'method' => 'post', 'action' => '/posts/delete/1',
  7041. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7042. ],
  7043. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7044. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  7045. 'div' => ['style' => 'display:none;'],
  7046. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  7047. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  7048. ['input' => [
  7049. 'type' => 'hidden', 'name' => '_Token[debug]',
  7050. 'value' => $tokenDebug,
  7051. ]],
  7052. '/div',
  7053. '/form',
  7054. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7055. 'Delete',
  7056. '/a'
  7057. ];
  7058. $this->assertHtml($expected, $result);
  7059. }
  7060. /**
  7061. * testPostLinkFormBuffer method
  7062. *
  7063. * Test that postLink adds form tags to view block.
  7064. *
  7065. * @return void
  7066. */
  7067. public function testPostLinkFormBuffer()
  7068. {
  7069. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7070. $expected = [
  7071. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7072. 'Delete',
  7073. '/a'
  7074. ];
  7075. $this->assertHtml($expected, $result);
  7076. $result = $this->View->fetch('postLink');
  7077. $expected = [
  7078. 'form' => [
  7079. 'method' => 'post', 'action' => '/posts/delete/1',
  7080. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7081. ],
  7082. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7083. '/form'
  7084. ];
  7085. $this->assertHtml($expected, $result);
  7086. $result = $this->Form->postLink(
  7087. 'Delete',
  7088. '/posts/delete/2',
  7089. ['block' => true, 'method' => 'DELETE']
  7090. );
  7091. $expected = [
  7092. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7093. 'Delete',
  7094. '/a'
  7095. ];
  7096. $this->assertHtml($expected, $result);
  7097. $result = $this->View->fetch('postLink');
  7098. $expected = [
  7099. 'form' => [
  7100. 'method' => 'post', 'action' => '/posts/delete/1',
  7101. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7102. ],
  7103. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7104. '/form',
  7105. [
  7106. 'form' => [
  7107. 'method' => 'post', 'action' => '/posts/delete/2',
  7108. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7109. ],
  7110. ],
  7111. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7112. '/form'
  7113. ];
  7114. $this->assertHtml($expected, $result);
  7115. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7116. $expected = [
  7117. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7118. 'Delete',
  7119. '/a'
  7120. ];
  7121. $this->assertHtml($expected, $result);
  7122. $result = $this->View->fetch('foobar');
  7123. $expected = [
  7124. 'form' => [
  7125. 'method' => 'post', 'action' => '/posts/delete/1',
  7126. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7127. ],
  7128. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7129. '/form'
  7130. ];
  7131. $this->assertHtml($expected, $result);
  7132. }
  7133. /**
  7134. * testSubmitButton method
  7135. *
  7136. * @return void
  7137. */
  7138. public function testSubmitButton()
  7139. {
  7140. $result = $this->Form->submit('');
  7141. $expected = [
  7142. 'div' => ['class' => 'submit'],
  7143. 'input' => ['type' => 'submit', 'value' => ''],
  7144. '/div'
  7145. ];
  7146. $this->assertHtml($expected, $result);
  7147. $result = $this->Form->submit('Test Submit');
  7148. $expected = [
  7149. 'div' => ['class' => 'submit'],
  7150. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7151. '/div'
  7152. ];
  7153. $this->assertHtml($expected, $result);
  7154. $result = $this->Form->submit('Next >');
  7155. $expected = [
  7156. 'div' => ['class' => 'submit'],
  7157. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7158. '/div'
  7159. ];
  7160. $this->assertHtml($expected, $result);
  7161. $result = $this->Form->submit('Next >', ['escape' => false]);
  7162. $expected = [
  7163. 'div' => ['class' => 'submit'],
  7164. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7165. '/div'
  7166. ];
  7167. $this->assertHtml($expected, $result);
  7168. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7169. $expected = [
  7170. 'div' => ['class' => 'submit'],
  7171. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7172. '/div'
  7173. ];
  7174. $this->assertHtml($expected, $result);
  7175. }
  7176. /**
  7177. * testSubmitImage method
  7178. *
  7179. * Test image submit types.
  7180. *
  7181. * @return void
  7182. */
  7183. public function testSubmitImage()
  7184. {
  7185. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7186. $expected = [
  7187. 'div' => ['class' => 'submit'],
  7188. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7189. '/div'
  7190. ];
  7191. $this->assertHtml($expected, $result);
  7192. $result = $this->Form->submit('/relative/cake.power.gif');
  7193. $expected = [
  7194. 'div' => ['class' => 'submit'],
  7195. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7196. '/div'
  7197. ];
  7198. $this->assertHtml($expected, $result);
  7199. $result = $this->Form->submit('cake.power.gif');
  7200. $expected = [
  7201. 'div' => ['class' => 'submit'],
  7202. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7203. '/div'
  7204. ];
  7205. $this->assertHtml($expected, $result);
  7206. $result = $this->Form->submit('Not.an.image');
  7207. $expected = [
  7208. 'div' => ['class' => 'submit'],
  7209. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7210. '/div'
  7211. ];
  7212. $this->assertHtml($expected, $result);
  7213. }
  7214. /**
  7215. * testSubmitUnlockedByDefault method
  7216. *
  7217. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7218. * be submitted at a time.
  7219. *
  7220. * @return void
  7221. */
  7222. public function testSubmitUnlockedByDefault()
  7223. {
  7224. $this->Form->request->params['_Token'] = 'secured';
  7225. $this->Form->submit('Go go');
  7226. $this->Form->submit('Save', ['name' => 'save']);
  7227. $result = $this->Form->unlockField();
  7228. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7229. }
  7230. /**
  7231. * testSubmitImageTimestamp method
  7232. *
  7233. * Test submit image with timestamps.
  7234. *
  7235. * @return void
  7236. */
  7237. public function testSubmitImageTimestamp()
  7238. {
  7239. Configure::write('Asset.timestamp', 'force');
  7240. $result = $this->Form->submit('cake.power.gif');
  7241. $expected = [
  7242. 'div' => ['class' => 'submit'],
  7243. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7244. '/div'
  7245. ];
  7246. $this->assertHtml($expected, $result);
  7247. }
  7248. /**
  7249. * testDateTimeWithGetForms method
  7250. *
  7251. * Test that datetime() works with GET style forms.
  7252. *
  7253. * @return void
  7254. */
  7255. public function testDateTimeWithGetForms()
  7256. {
  7257. extract($this->dateRegex);
  7258. $this->Form->create($this->article, ['type' => 'get']);
  7259. $result = $this->Form->datetime('created');
  7260. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7261. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7262. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7263. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7264. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7265. }
  7266. /**
  7267. * testForMagicInputNonExistingNorValidated method
  7268. *
  7269. * @return void
  7270. */
  7271. public function testForMagicInputNonExistingNorValidated()
  7272. {
  7273. $this->Form->create($this->article);
  7274. $this->Form->templates(['inputContainer' => '{{content}}']);
  7275. $result = $this->Form->input('non_existing_nor_validated');
  7276. $expected = [
  7277. 'label' => ['for' => 'non-existing-nor-validated'],
  7278. 'Non Existing Nor Validated',
  7279. '/label',
  7280. 'input' => [
  7281. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7282. 'id' => 'non-existing-nor-validated'
  7283. ]
  7284. ];
  7285. $this->assertHtml($expected, $result);
  7286. $result = $this->Form->input('non_existing_nor_validated', [
  7287. 'val' => 'my value'
  7288. ]);
  7289. $expected = [
  7290. 'label' => ['for' => 'non-existing-nor-validated'],
  7291. 'Non Existing Nor Validated',
  7292. '/label',
  7293. 'input' => [
  7294. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7295. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7296. ]
  7297. ];
  7298. $this->assertHtml($expected, $result);
  7299. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7300. $result = $this->Form->input('non_existing_nor_validated');
  7301. $expected = [
  7302. 'label' => ['for' => 'non-existing-nor-validated'],
  7303. 'Non Existing Nor Validated',
  7304. '/label',
  7305. 'input' => [
  7306. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7307. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7308. ]
  7309. ];
  7310. $this->assertHtml($expected, $result);
  7311. }
  7312. /**
  7313. * testFormMagicInputLabel method
  7314. *
  7315. * @return void
  7316. */
  7317. public function testFormMagicInputLabel()
  7318. {
  7319. TableRegistry::get('Contacts', [
  7320. 'className' => __NAMESPACE__ . '\ContactsTable'
  7321. ]);
  7322. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7323. $this->Form->templates(['inputContainer' => '{{content}}']);
  7324. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  7325. $expected = [
  7326. 'label' => ['for' => 'contacts-name'],
  7327. 'My label',
  7328. '/label',
  7329. 'input' => [
  7330. 'type' => 'text', 'name' => 'Contacts[name]',
  7331. 'id' => 'contacts-name', 'maxlength' => '255'
  7332. ]
  7333. ];
  7334. $this->assertHtml($expected, $result);
  7335. $result = $this->Form->input('name', [
  7336. 'label' => ['class' => 'mandatory']
  7337. ]);
  7338. $expected = [
  7339. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7340. 'Name',
  7341. '/label',
  7342. 'input' => [
  7343. 'type' => 'text', 'name' => 'name',
  7344. 'id' => 'name', 'maxlength' => '255'
  7345. ]
  7346. ];
  7347. $this->assertHtml($expected, $result);
  7348. $result = $this->Form->input('name', [
  7349. 'div' => false,
  7350. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7351. ]);
  7352. $expected = [
  7353. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7354. 'My label',
  7355. '/label',
  7356. 'input' => [
  7357. 'type' => 'text', 'name' => 'name',
  7358. 'id' => 'name', 'maxlength' => '255'
  7359. ]
  7360. ];
  7361. $this->assertHtml($expected, $result);
  7362. $result = $this->Form->input('Contact.name', [
  7363. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7364. ]);
  7365. $expected = [
  7366. 'label' => ['for' => 'my_id'],
  7367. 'Name',
  7368. '/label',
  7369. 'input' => [
  7370. 'type' => 'text', 'name' => 'Contact[name]',
  7371. 'id' => 'my_id', 'maxlength' => '255'
  7372. ]
  7373. ];
  7374. $this->assertHtml($expected, $result);
  7375. $result = $this->Form->input('1.id');
  7376. $expected = ['input' => [
  7377. 'type' => 'hidden', 'name' => '1[id]',
  7378. 'id' => '1-id'
  7379. ]];
  7380. $this->assertHtml($expected, $result);
  7381. $result = $this->Form->input("1.name");
  7382. $expected = [
  7383. 'label' => ['for' => '1-name'],
  7384. 'Name',
  7385. '/label',
  7386. 'input' => [
  7387. 'type' => 'text', 'name' => '1[name]',
  7388. 'id' => '1-name', 'maxlength' => '255'
  7389. ]
  7390. ];
  7391. $this->assertHtml($expected, $result);
  7392. }
  7393. /**
  7394. * testFormEnd method
  7395. *
  7396. * @return void
  7397. */
  7398. public function testFormEnd()
  7399. {
  7400. $this->assertEquals('</form>', $this->Form->end());
  7401. }
  7402. /**
  7403. * testMultiRecordForm method
  7404. *
  7405. * Test the generation of fields for a multi record form.
  7406. *
  7407. * @return void
  7408. */
  7409. public function testMultiRecordForm()
  7410. {
  7411. $this->loadFixtures('Articles', 'Comments');
  7412. $articles = TableRegistry::get('Articles');
  7413. $articles->hasMany('Comments');
  7414. $comment = new Entity(['comment' => 'Value']);
  7415. $article = new Article(['comments' => [$comment]]);
  7416. $this->Form->create([$article]);
  7417. $result = $this->Form->input('0.comments.1.comment');
  7418. //@codingStandardsIgnoreStart
  7419. $expected = [
  7420. 'div' => ['class' => 'input textarea'],
  7421. 'label' => ['for' => '0-comments-1-comment'],
  7422. 'Comment',
  7423. '/label',
  7424. 'textarea' => [
  7425. 'name',
  7426. 'id' => '0-comments-1-comment',
  7427. 'rows' => 5
  7428. ],
  7429. '/textarea',
  7430. '/div'
  7431. ];
  7432. //@codingStandardsIgnoreEnd
  7433. $this->assertHtml($expected, $result);
  7434. $result = $this->Form->input('0.comments.0.comment');
  7435. //@codingStandardsIgnoreStart
  7436. $expected = [
  7437. 'div' => ['class' => 'input textarea'],
  7438. 'label' => ['for' => '0-comments-0-comment'],
  7439. 'Comment',
  7440. '/label',
  7441. 'textarea' => [
  7442. 'name',
  7443. 'id' => '0-comments-0-comment',
  7444. 'rows' => 5
  7445. ],
  7446. 'Value',
  7447. '/textarea',
  7448. '/div'
  7449. ];
  7450. //@codingStandardsIgnoreEnd
  7451. $this->assertHtml($expected, $result);
  7452. $comment->errors('comment', ['Not valid']);
  7453. $result = $this->Form->input('0.comments.0.comment');
  7454. //@codingStandardsIgnoreStart
  7455. $expected = [
  7456. 'div' => ['class' => 'input textarea error'],
  7457. 'label' => ['for' => '0-comments-0-comment'],
  7458. 'Comment',
  7459. '/label',
  7460. 'textarea' => [
  7461. 'name',
  7462. 'class' => 'form-error',
  7463. 'id' => '0-comments-0-comment',
  7464. 'rows' => 5
  7465. ],
  7466. 'Value',
  7467. '/textarea',
  7468. ['div' => ['class' => 'error-message']],
  7469. 'Not valid',
  7470. '/div',
  7471. '/div'
  7472. ];
  7473. //@codingStandardsIgnoreEnd
  7474. $this->assertHtml($expected, $result);
  7475. TableRegistry::get('Comments')
  7476. ->validator('default')
  7477. ->allowEmpty('comment', false);
  7478. $result = $this->Form->input('0.comments.1.comment');
  7479. //@codingStandardsIgnoreStart
  7480. $expected = [
  7481. 'div' => ['class' => 'input textarea required'],
  7482. 'label' => ['for' => '0-comments-1-comment'],
  7483. 'Comment',
  7484. '/label',
  7485. 'textarea' => [
  7486. 'name',
  7487. 'required' => 'required',
  7488. 'id' => '0-comments-1-comment',
  7489. 'rows' => 5
  7490. ],
  7491. '/textarea',
  7492. '/div'
  7493. ];
  7494. //@codingStandardsIgnoreEnd
  7495. $this->assertHtml($expected, $result);
  7496. }
  7497. /**
  7498. * testHtml5Inputs method
  7499. *
  7500. * Test that some html5 inputs + FormHelper::__call() work.
  7501. *
  7502. * @return void
  7503. */
  7504. public function testHtml5Inputs()
  7505. {
  7506. $result = $this->Form->email('User.email');
  7507. $expected = [
  7508. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7509. ];
  7510. $this->assertHtml($expected, $result);
  7511. $result = $this->Form->search('User.query');
  7512. $expected = [
  7513. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7514. ];
  7515. $this->assertHtml($expected, $result);
  7516. $result = $this->Form->search('User.query', ['value' => 'test']);
  7517. $expected = [
  7518. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7519. ];
  7520. $this->assertHtml($expected, $result);
  7521. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7522. $expected = [
  7523. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7524. ];
  7525. $this->assertHtml($expected, $result);
  7526. }
  7527. /**
  7528. * testHtml5InputWithInput method
  7529. *
  7530. * Test accessing html5 inputs through input().
  7531. *
  7532. * @return void
  7533. */
  7534. public function testHtml5InputWithInput()
  7535. {
  7536. $this->Form->create();
  7537. $this->Form->templates(['inputContainer' => '{{content}}']);
  7538. $result = $this->Form->input('website', [
  7539. 'type' => 'url',
  7540. 'val' => 'http://domain.tld',
  7541. 'label' => false
  7542. ]);
  7543. $expected = [
  7544. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7545. ];
  7546. $this->assertHtml($expected, $result);
  7547. }
  7548. /**
  7549. * testHtml5InputException method
  7550. *
  7551. * Test errors when field name is missing.
  7552. *
  7553. * @expectedException \Cake\Core\Exception\Exception
  7554. * @return void
  7555. */
  7556. public function testHtml5InputException()
  7557. {
  7558. $this->Form->email();
  7559. }
  7560. /**
  7561. * testRequiredAttribute method
  7562. *
  7563. * Tests that formhelper sets required attributes.
  7564. *
  7565. * @return void
  7566. */
  7567. public function testRequiredAttribute()
  7568. {
  7569. $this->article['required'] = [
  7570. 'title' => true,
  7571. 'body' => false,
  7572. ];
  7573. $this->Form->create($this->article);
  7574. $result = $this->Form->input('title');
  7575. $expected = [
  7576. 'div' => ['class' => 'input text required'],
  7577. 'label' => ['for' => 'title'],
  7578. 'Title',
  7579. '/label',
  7580. 'input' => [
  7581. 'type' => 'text',
  7582. 'name' => 'title',
  7583. 'id' => 'title',
  7584. 'required' => 'required',
  7585. ],
  7586. '/div'
  7587. ];
  7588. $this->assertHtml($expected, $result);
  7589. $result = $this->Form->input('title', ['required' => false]);
  7590. $this->assertNotContains('required', $result);
  7591. $result = $this->Form->input('body');
  7592. $expected = [
  7593. 'div' => ['class' => 'input text'],
  7594. 'label' => ['for' => 'body'],
  7595. 'Body',
  7596. '/label',
  7597. 'input' => [
  7598. 'type' => 'text',
  7599. 'name' => 'body',
  7600. 'id' => 'body',
  7601. ],
  7602. '/div'
  7603. ];
  7604. $this->assertHtml($expected, $result);
  7605. $result = $this->Form->input('body', ['required' => true]);
  7606. $this->assertContains('required', $result);
  7607. }
  7608. /**
  7609. * testInputsNotNested method
  7610. *
  7611. * Tests that it is possible to put inputs outside of the label.
  7612. *
  7613. * @return void
  7614. */
  7615. public function testInputsNotNested()
  7616. {
  7617. $this->Form->templates([
  7618. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7619. 'formGroup' => '{{input}}{{label}}',
  7620. ]);
  7621. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  7622. $expected = [
  7623. 'div' => ['class' => 'input checkbox'],
  7624. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7625. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7626. 'label' => ['for' => 'foo'],
  7627. 'Foo',
  7628. '/label',
  7629. '/div'
  7630. ];
  7631. $this->assertHtml($expected, $result);
  7632. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  7633. $expected = [
  7634. 'div' => ['class' => 'input checkbox'],
  7635. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7636. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7637. '/div'
  7638. ];
  7639. $this->assertHtml($expected, $result);
  7640. $result = $this->Form->input('confirm', [
  7641. 'type' => 'radio',
  7642. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7643. ]);
  7644. $expected = [
  7645. 'div' => ['class' => 'input radio'],
  7646. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7647. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7648. ['label' => ['for' => 'confirm-y']],
  7649. 'Yes',
  7650. '/label',
  7651. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7652. ['label' => ['for' => 'confirm-n']],
  7653. 'No',
  7654. '/label',
  7655. '<label',
  7656. 'Confirm',
  7657. '/label',
  7658. '/div',
  7659. ];
  7660. $this->assertHtml($expected, $result);
  7661. $result = $this->Form->select('category', ['1', '2'], [
  7662. 'multiple' => 'checkbox',
  7663. 'name' => 'fish',
  7664. ]);
  7665. $expected = [
  7666. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7667. ['div' => ['class' => 'checkbox']],
  7668. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7669. ['label' => ['for' => 'fish-0']],
  7670. '1',
  7671. '/label',
  7672. '/div',
  7673. ['div' => ['class' => 'checkbox']],
  7674. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7675. ['label' => ['for' => 'fish-1']],
  7676. '2',
  7677. '/label',
  7678. '/div'
  7679. ];
  7680. $this->assertHtml($expected, $result);
  7681. }
  7682. /**
  7683. * testInputContainerTemplates method
  7684. *
  7685. * Test that *Container templates are used by input.
  7686. *
  7687. * @return void
  7688. */
  7689. public function testInputContainerTemplates()
  7690. {
  7691. $this->Form->templates([
  7692. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7693. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7694. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7695. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7696. ]);
  7697. $this->article['errors'] = [
  7698. 'Article' => ['published' => 'error message']
  7699. ];
  7700. $this->Form->create($this->article);
  7701. $result = $this->Form->input('accept', [
  7702. 'type' => 'checkbox'
  7703. ]);
  7704. $expected = [
  7705. 'div' => ['class' => 'check'],
  7706. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7707. 'label' => ['for' => 'accept'],
  7708. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7709. 'Accept',
  7710. '/label',
  7711. '/div'
  7712. ];
  7713. $this->assertHtml($expected, $result);
  7714. $result = $this->Form->input('accept', [
  7715. 'type' => 'radio',
  7716. 'options' => ['Y', 'N']
  7717. ]);
  7718. $this->assertContains('<div class="rad">', $result);
  7719. $result = $this->Form->input('Article.published', [
  7720. 'type' => 'radio',
  7721. 'options' => ['Y', 'N']
  7722. ]);
  7723. $this->assertContains('<div class="rad err">', $result);
  7724. $result = $this->Form->input('Article.created', [
  7725. 'type' => 'datetime',
  7726. ]);
  7727. $this->assertContains('<div class="dt">', $result);
  7728. }
  7729. /**
  7730. * testFormGroupTemplates method
  7731. *
  7732. * Test that *Container templates are used by input.
  7733. *
  7734. * @return void
  7735. */
  7736. public function testFormGroupTemplates()
  7737. {
  7738. $this->Form->templates([
  7739. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7740. ]);
  7741. $this->Form->create($this->article);
  7742. $result = $this->Form->input('accept', [
  7743. 'type' => 'radio',
  7744. 'options' => ['Y', 'N']
  7745. ]);
  7746. $this->assertContains('<div class="radio">', $result);
  7747. }
  7748. /**
  7749. * testResetTemplates method
  7750. *
  7751. * Test resetting templates.
  7752. *
  7753. * @return void
  7754. */
  7755. public function testResetTemplates()
  7756. {
  7757. $this->Form->templates(['input' => '<input/>']);
  7758. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7759. $this->assertNull($this->Form->resetTemplates());
  7760. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7761. }
  7762. /**
  7763. * testContext method
  7764. *
  7765. * Test the context method.
  7766. *
  7767. * @return void
  7768. */
  7769. public function testContext()
  7770. {
  7771. $result = $this->Form->context();
  7772. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7773. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7774. $this->assertSame($mock, $this->Form->context($mock));
  7775. $this->assertSame($mock, $this->Form->context());
  7776. }
  7777. /**
  7778. * testAutoDomId method
  7779. *
  7780. * @return void
  7781. */
  7782. public function testAutoDomId()
  7783. {
  7784. $result = $this->Form->text('field', ['id' => true]);
  7785. $expected = [
  7786. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7787. ];
  7788. $this->assertHtml($expected, $result);
  7789. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7790. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7791. $expected = [
  7792. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7793. ['label' => ['for' => 'field-0']],
  7794. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7795. 'option A',
  7796. '/label',
  7797. ['label' => ['for' => 'field-1']],
  7798. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7799. 'option B',
  7800. '/label',
  7801. ];
  7802. $this->assertHtml($expected, $result);
  7803. $result = $this->Form->select(
  7804. 'multi_field',
  7805. ['first', 'second'],
  7806. ['multiple' => 'checkbox', 'id' => true]
  7807. );
  7808. $expected = [
  7809. 'input' => [
  7810. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7811. ],
  7812. ['div' => ['class' => 'checkbox']],
  7813. ['label' => ['for' => 'multi-field-0']],
  7814. ['input' => [
  7815. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7816. 'value' => '0', 'id' => 'multi-field-0'
  7817. ]],
  7818. 'first',
  7819. '/label',
  7820. '/div',
  7821. ['div' => ['class' => 'checkbox']],
  7822. ['label' => ['for' => 'multi-field-1']],
  7823. ['input' => [
  7824. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7825. 'value' => '1', 'id' => 'multi-field-1'
  7826. ]],
  7827. 'second',
  7828. '/label',
  7829. '/div',
  7830. ];
  7831. $this->assertHtml($expected, $result);
  7832. }
  7833. /**
  7834. * testNestedLabelInput method
  7835. *
  7836. * Test the `nestedInput` parameter
  7837. *
  7838. * @return void
  7839. */
  7840. public function testNestedLabelInput()
  7841. {
  7842. $result = $this->Form->input('foo', ['nestedInput' => true]);
  7843. $expected = [
  7844. 'div' => ['class' => 'input text'],
  7845. 'label' => ['for' => 'foo'],
  7846. ['input' => [
  7847. 'type' => 'text',
  7848. 'name' => 'foo',
  7849. 'id' => 'foo'
  7850. ]],
  7851. 'Foo',
  7852. '/label',
  7853. '/div'
  7854. ];
  7855. $this->assertHtml($expected, $result);
  7856. }
  7857. /**
  7858. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7859. *
  7860. * This test makes sure `false` excludes the label from the render
  7861. *
  7862. */
  7863. public function testInputLabelManipulationDisableLabels()
  7864. {
  7865. $result = $this->Form->input('test', [
  7866. 'type' => 'radio',
  7867. 'options' => ['A', 'B'],
  7868. 'labelOptions' => false
  7869. ]);
  7870. $expected = [
  7871. ['div' => ['class' => 'input radio']],
  7872. '<label',
  7873. 'Test',
  7874. '/label',
  7875. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7876. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7877. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7878. '/div',
  7879. ];
  7880. $this->assertHtml($expected, $result);
  7881. $result = $this->Form->input('checkbox1', [
  7882. 'label' => 'My checkboxes',
  7883. 'multiple' => 'checkbox',
  7884. 'type' => 'select',
  7885. 'options' => [
  7886. ['text' => 'First Checkbox', 'value' => 1],
  7887. ['text' => 'Second Checkbox', 'value' => 2]
  7888. ],
  7889. 'labelOptions' => false
  7890. ]);
  7891. $expected = [
  7892. ['div' => ['class' => 'input select']],
  7893. ['label' => ['for' => 'checkbox1']],
  7894. 'My checkboxes',
  7895. '/label',
  7896. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7897. ['div' => ['class' => 'checkbox']],
  7898. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7899. '/div',
  7900. ['div' => ['class' => 'checkbox']],
  7901. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7902. '/div',
  7903. '/div'
  7904. ];
  7905. $this->assertHtml($expected, $result);
  7906. }
  7907. /**
  7908. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7909. *
  7910. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7911. * added to the class if checked.
  7912. *
  7913. * Also checks to make sure any custom attributes are rendered correctly
  7914. */
  7915. public function testInputLabelManipulationRadios()
  7916. {
  7917. $result = $this->Form->input('test', [
  7918. 'type' => 'radio',
  7919. 'options' => ['A', 'B'],
  7920. 'labelOptions' => ['class' => 'custom-class']
  7921. ]);
  7922. $expected = [
  7923. ['div' => ['class' => 'input radio']],
  7924. '<label',
  7925. 'Test',
  7926. '/label',
  7927. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7928. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7929. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7930. 'A',
  7931. '/label',
  7932. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7933. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7934. 'B',
  7935. '/label',
  7936. '/div',
  7937. ];
  7938. $this->assertHtml($expected, $result);
  7939. $result = $this->Form->input('test', [
  7940. 'type' => 'radio',
  7941. 'options' => ['A', 'B'],
  7942. 'value' => 1,
  7943. 'labelOptions' => ['class' => 'custom-class']
  7944. ]);
  7945. $expected = [
  7946. ['div' => ['class' => 'input radio']],
  7947. '<label',
  7948. 'Test',
  7949. '/label',
  7950. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7951. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7952. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7953. 'A',
  7954. '/label',
  7955. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7956. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7957. 'B',
  7958. '/label',
  7959. '/div',
  7960. ];
  7961. $this->assertHtml($expected, $result);
  7962. $result = $this->Form->input('test', [
  7963. 'type' => 'radio',
  7964. 'options' => ['A', 'B'],
  7965. 'value' => 1,
  7966. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  7967. ]);
  7968. $expected = [
  7969. ['div' => ['class' => 'input radio']],
  7970. '<label',
  7971. 'Test',
  7972. '/label',
  7973. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7974. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7975. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7976. 'A',
  7977. '/label',
  7978. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7979. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7980. 'B',
  7981. '/label',
  7982. '/div',
  7983. ];
  7984. $this->assertHtml($expected, $result);
  7985. $result = $this->Form->radio('test', ['A', 'B'], [
  7986. 'label' => [
  7987. 'class' => ['custom-class', 'another-class'],
  7988. 'data-name' => 'bob'
  7989. ],
  7990. 'value' => 1
  7991. ]);
  7992. $expected = [
  7993. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  7994. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7995. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7996. 'A',
  7997. '/label',
  7998. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7999. ['input' => [
  8000. 'type' => 'radio',
  8001. 'name' => 'test',
  8002. 'value' => '1',
  8003. 'id' => 'test-1',
  8004. 'checked' => 'checked'
  8005. ]],
  8006. 'B',
  8007. '/label',
  8008. ];
  8009. $this->assertHtml($expected, $result);
  8010. }
  8011. /**
  8012. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8013. *
  8014. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8015. * added to the class if checked.
  8016. *
  8017. * Also checks to make sure any custom attributes are rendered correctly
  8018. */
  8019. public function testInputLabelManipulationCheckboxes()
  8020. {
  8021. $result = $this->Form->input('checkbox1', [
  8022. 'label' => 'My checkboxes',
  8023. 'multiple' => 'checkbox',
  8024. 'type' => 'select',
  8025. 'options' => [
  8026. ['text' => 'First Checkbox', 'value' => 1],
  8027. ['text' => 'Second Checkbox', 'value' => 2]
  8028. ],
  8029. 'labelOptions' => ['class' => 'custom-class'],
  8030. 'value' => ['1']
  8031. ]);
  8032. $expected = [
  8033. ['div' => ['class' => 'input select']],
  8034. ['label' => ['for' => 'checkbox1']],
  8035. 'My checkboxes',
  8036. '/label',
  8037. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8038. ['div' => ['class' => 'checkbox']],
  8039. ['label' => [
  8040. 'class' => 'custom-class selected',
  8041. 'for' => 'checkbox1-1'
  8042. ]],
  8043. ['input' => [
  8044. 'type' => 'checkbox',
  8045. 'name' => 'checkbox1[]',
  8046. 'value' => '1',
  8047. 'id' => 'checkbox1-1',
  8048. 'checked' => 'checked'
  8049. ]],
  8050. 'First Checkbox',
  8051. '/label',
  8052. '/div',
  8053. ['div' => ['class' => 'checkbox']],
  8054. ['label' => [
  8055. 'class' => 'custom-class',
  8056. 'for' => 'checkbox1-2'
  8057. ]],
  8058. ['input' => [
  8059. 'type' => 'checkbox',
  8060. 'name' => 'checkbox1[]',
  8061. 'value' => '2',
  8062. 'id' => 'checkbox1-2'
  8063. ]],
  8064. 'Second Checkbox',
  8065. '/label',
  8066. '/div',
  8067. '/div'
  8068. ];
  8069. $this->assertHtml($expected, $result);
  8070. $result = $this->Form->input('checkbox1', [
  8071. 'label' => 'My checkboxes',
  8072. 'multiple' => 'checkbox',
  8073. 'type' => 'select',
  8074. 'options' => [
  8075. ['text' => 'First Checkbox', 'value' => 1],
  8076. ['text' => 'Second Checkbox', 'value' => 2]
  8077. ],
  8078. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8079. 'value' => ['1']
  8080. ]);
  8081. $expected = [
  8082. ['div' => ['class' => 'input select']],
  8083. ['label' => ['for' => 'checkbox1']],
  8084. 'My checkboxes',
  8085. '/label',
  8086. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8087. ['div' => ['class' => 'checkbox']],
  8088. ['label' => [
  8089. 'class' => 'custom-class another-class selected',
  8090. 'data-name' => 'bob',
  8091. 'for' => 'checkbox1-1'
  8092. ]],
  8093. ['input' => [
  8094. 'type' => 'checkbox',
  8095. 'name' => 'checkbox1[]',
  8096. 'value' => '1',
  8097. 'id' => 'checkbox1-1',
  8098. 'checked' => 'checked'
  8099. ]],
  8100. 'First Checkbox',
  8101. '/label',
  8102. '/div',
  8103. ['div' => ['class' => 'checkbox']],
  8104. ['label' => [
  8105. 'class' => 'custom-class another-class',
  8106. 'data-name' => 'bob',
  8107. 'for' => 'checkbox1-2'
  8108. ]],
  8109. ['input' => [
  8110. 'type' => 'checkbox',
  8111. 'name' => 'checkbox1[]',
  8112. 'value' => '2',
  8113. 'id' => 'checkbox1-2'
  8114. ]],
  8115. 'Second Checkbox',
  8116. '/label',
  8117. '/div',
  8118. '/div'
  8119. ];
  8120. $this->assertHtml($expected, $result);
  8121. }
  8122. }