FormHelperTest.php 305 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use ArrayObject;
  18. use Cake\Collection\Collection;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Exception\CakeException;
  21. use Cake\Form\Form;
  22. use Cake\Http\ServerRequest;
  23. use Cake\I18n\Date;
  24. use Cake\I18n\DateTime;
  25. use Cake\ORM\Entity;
  26. use Cake\ORM\Table;
  27. use Cake\Routing\Router;
  28. use Cake\TestSuite\TestCase;
  29. use Cake\Utility\Security;
  30. use Cake\Validation\Validator;
  31. use Cake\View\Form\EntityContext;
  32. use Cake\View\Helper\FormHelper;
  33. use Cake\View\View;
  34. use Cake\View\Widget\WidgetLocator;
  35. use InvalidArgumentException;
  36. use ReflectionProperty;
  37. use TestApp\Model\Entity\Article;
  38. use TestApp\Model\Table\ContactsTable;
  39. use TestApp\Model\Table\ValidateUsersTable;
  40. /**
  41. * FormHelperTest class
  42. *
  43. * @property \Cake\View\Helper\FormHelper $Form
  44. * @property \Cake\View\View $View
  45. */
  46. class FormHelperTest extends TestCase
  47. {
  48. /**
  49. * Fixtures to be used
  50. *
  51. * @var array<string>
  52. */
  53. protected array $fixtures = ['core.Articles', 'core.Comments'];
  54. /**
  55. * @var array
  56. */
  57. protected $article = [];
  58. /**
  59. * @var string
  60. */
  61. protected $url;
  62. /**
  63. * @var \Cake\View\Helper\FormHelper
  64. */
  65. protected $Form;
  66. /**
  67. * @var \Cake\View\View
  68. */
  69. protected $View;
  70. /**
  71. * setUp method
  72. */
  73. public function setUp(): void
  74. {
  75. parent::setUp();
  76. Configure::write('Config.language', 'eng');
  77. Configure::write('App.base', '');
  78. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  79. $request = new ServerRequest([
  80. 'webroot' => '',
  81. 'base' => '',
  82. 'url' => '/articles/add',
  83. 'params' => [
  84. 'controller' => 'Articles',
  85. 'action' => 'add',
  86. 'plugin' => null,
  87. ],
  88. ]);
  89. $this->View = new View($request);
  90. Router::reload();
  91. Router::setRequest($request);
  92. $this->url = '/articles/add';
  93. $this->Form = new FormHelper($this->View);
  94. $this->article = [
  95. 'schema' => [
  96. 'id' => ['type' => 'integer'],
  97. 'author_id' => ['type' => 'integer', 'null' => true],
  98. 'title' => ['type' => 'string', 'null' => true],
  99. 'body' => 'text',
  100. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  101. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  102. ],
  103. 'required' => [
  104. 'author_id' => true,
  105. 'title' => true,
  106. ],
  107. ];
  108. Security::setSalt('foo!');
  109. $builder = Router::createRouteBuilder('/');
  110. $builder->connect('/{controller}', ['action' => 'index']);
  111. $builder->connect('/{controller}/{action}/*');
  112. }
  113. /**
  114. * tearDown method
  115. */
  116. public function tearDown(): void
  117. {
  118. parent::tearDown();
  119. unset($this->Form, $this->View);
  120. }
  121. /**
  122. * Test construct() with the templates option.
  123. */
  124. public function testConstructTemplatesFile(): void
  125. {
  126. $helper = new FormHelper($this->View, [
  127. 'templates' => 'htmlhelper_tags',
  128. ]);
  129. $result = $helper->control('name');
  130. $this->assertStringContainsString('<input', $result);
  131. }
  132. /**
  133. * Test that when specifying custom widgets the config array for that widget
  134. * is overwritten instead of merged.
  135. */
  136. public function testConstructWithWidgets(): void
  137. {
  138. $config = [
  139. 'widgets' => [
  140. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  141. ],
  142. ];
  143. $helper = new FormHelper($this->View, $config);
  144. $locator = $helper->getWidgetLocator();
  145. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  146. }
  147. /**
  148. * Test that when specifying custom widgets config file and it should be
  149. * added to widgets array. WidgetLocator will load widgets in constructor.
  150. */
  151. public function testConstructWithWidgetsConfig(): void
  152. {
  153. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  154. $locator = $helper->getWidgetLocator();
  155. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  156. }
  157. /**
  158. * Test setting the widget locator
  159. */
  160. public function testSetAndGetWidgetLocator(): void
  161. {
  162. $helper = new FormHelper($this->View);
  163. $locator = new WidgetLocator($helper->templater(), $this->View);
  164. $helper->setWidgetLocator($locator);
  165. $this->assertSame($locator, $helper->getWidgetLocator());
  166. }
  167. /**
  168. * Test overridding grouped input types which controls generation of "for"
  169. * attribute of labels.
  170. */
  171. public function testConstructWithGroupedInputTypes(): void
  172. {
  173. $helper = new FormHelper($this->View, [
  174. 'groupedInputTypes' => ['radio'],
  175. ]);
  176. $result = $helper->control('when', ['type' => 'datetime-local']);
  177. $this->assertStringContainsString('<label for="when">When</label>', $result);
  178. }
  179. /**
  180. * Test registering a new widget class and rendering it.
  181. */
  182. public function testAddWidgetAndRenderWidget(): void
  183. {
  184. $data = [
  185. 'val' => 1,
  186. ];
  187. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  188. $this->Form->addWidget('test', $mock);
  189. $mock->expects($this->once())
  190. ->method('render')
  191. ->with($data)
  192. ->will($this->returnValue('HTML'));
  193. $result = $this->Form->widget('test', $data);
  194. $this->assertSame('HTML', $result);
  195. }
  196. /**
  197. * Test that secureFields() of widget is called after calling render(),
  198. * not before.
  199. */
  200. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  201. {
  202. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  203. 'unlockedFields' => [],
  204. ]));
  205. $data = [
  206. 'val' => 1,
  207. 'name' => 'test',
  208. ];
  209. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  210. $this->Form->addWidget('test', $mock);
  211. $mock->expects($this->once())
  212. ->method('render')
  213. ->with($data)
  214. ->will($this->returnValue('HTML'));
  215. $mock->expects($this->once())
  216. ->method('secureFields')
  217. ->with($data)
  218. ->will($this->returnValue(['test']));
  219. $this->Form->create();
  220. $result = $this->Form->widget('test', $data + ['secure' => true]);
  221. $this->assertSame('HTML', $result);
  222. }
  223. /**
  224. * Test that empty string is not added to secure fields list when
  225. * rendering input widget without name.
  226. */
  227. public function testRenderingWidgetWithEmptyName(): void
  228. {
  229. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  230. $this->Form->create();
  231. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  232. $this->assertSame('<select name=""></select>', $result);
  233. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  234. $this->assertEquals([], $result);
  235. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  236. $this->assertSame('<select name="0"></select>', $result);
  237. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  238. $this->assertEquals(['0'], $result);
  239. }
  240. /**
  241. * Test adding a new context class.
  242. */
  243. public function testAddContextProvider(): void
  244. {
  245. $context = 'My data';
  246. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  247. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  248. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  249. $this->assertSame($context, $data['entity']);
  250. return $stub;
  251. });
  252. $this->Form->create($context);
  253. $result = $this->Form->context();
  254. $this->assertSame($stub, $result);
  255. }
  256. /**
  257. * Test replacing a context class.
  258. */
  259. public function testAddContextProviderReplace(): void
  260. {
  261. $entity = new Article();
  262. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  263. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  264. return $stub;
  265. });
  266. $this->Form->create($entity);
  267. $result = $this->Form->context();
  268. $this->assertSame($stub, $result);
  269. }
  270. /**
  271. * Test overriding a context class.
  272. */
  273. public function testAddContextProviderAdd(): void
  274. {
  275. $entity = new Article();
  276. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  277. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  278. if ($data['entity'] instanceof Entity) {
  279. return $stub;
  280. }
  281. });
  282. $this->Form->create($entity);
  283. $result = $this->Form->context();
  284. $this->assertSame($stub, $result);
  285. }
  286. /**
  287. * Provides context options for create().
  288. *
  289. * @return array
  290. */
  291. public function contextSelectionProvider(): array
  292. {
  293. $entity = new Article();
  294. $collection = new Collection([$entity]);
  295. $emptyCollection = new Collection([]);
  296. $arrayObject = new ArrayObject([]);
  297. $data = [
  298. 'schema' => [
  299. 'title' => ['type' => 'string'],
  300. ],
  301. ];
  302. $form = new Form();
  303. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  304. return [
  305. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  306. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  307. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  308. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  309. 'form' => [$form, 'Cake\View\Form\FormContext'],
  310. 'none' => [null, 'Cake\View\Form\NullContext'],
  311. 'custom' => [$custom, $custom::class],
  312. ];
  313. }
  314. /**
  315. * Test default context selection in create()
  316. *
  317. * @dataProvider contextSelectionProvider
  318. * @param mixed $data
  319. */
  320. public function testCreateContextSelectionBuiltIn($data, string $class): void
  321. {
  322. $this->Form->create($data);
  323. $this->assertInstanceOf($class, $this->Form->context());
  324. }
  325. /**
  326. * Data provider for type option.
  327. *
  328. * @return array
  329. */
  330. public static function requestTypeProvider(): array
  331. {
  332. return [
  333. // type, method, override
  334. ['post', 'post', 'POST'],
  335. ['put', 'post', 'PUT'],
  336. ['patch', 'post', 'PATCH'],
  337. ['delete', 'post', 'DELETE'],
  338. ];
  339. }
  340. /**
  341. * Test creating file forms.
  342. */
  343. public function testCreateFile(): void
  344. {
  345. $encoding = strtolower(Configure::read('App.encoding'));
  346. $result = $this->Form->create(null, ['type' => 'file']);
  347. $expected = [
  348. 'form' => [
  349. 'method' => 'post', 'action' => '/articles/add',
  350. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  351. ],
  352. 'div' => ['style' => 'display:none;'],
  353. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  354. '/div',
  355. ];
  356. $this->assertHtml($expected, $result);
  357. }
  358. /**
  359. * Test creating GET forms.
  360. */
  361. public function testCreateGet(): void
  362. {
  363. $encoding = strtolower(Configure::read('App.encoding'));
  364. $result = $this->Form->create(null, ['type' => 'get']);
  365. $expected = ['form' => [
  366. 'method' => 'get', 'action' => '/articles/add',
  367. 'accept-charset' => $encoding,
  368. ]];
  369. $this->assertHtml($expected, $result);
  370. }
  371. /**
  372. * Test explicit method/enctype options.
  373. *
  374. * Explicit method overwrites inferred method from 'type'
  375. */
  376. public function testCreateExplicitMethodEnctype(): void
  377. {
  378. $encoding = strtolower(Configure::read('App.encoding'));
  379. $result = $this->Form->create(null, [
  380. 'type' => 'get',
  381. 'method' => 'put',
  382. 'enctype' => 'multipart/form-data',
  383. ]);
  384. $expected = ['form' => [
  385. 'method' => 'put',
  386. 'action' => '/articles/add',
  387. 'enctype' => 'multipart/form-data',
  388. 'accept-charset' => $encoding,
  389. ]];
  390. $this->assertHtml($expected, $result);
  391. }
  392. /**
  393. * Test create() with the templates option.
  394. */
  395. public function testCreateTemplatesArray(): void
  396. {
  397. $result = $this->Form->create($this->article, [
  398. 'templates' => [
  399. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  400. ],
  401. ]);
  402. $expected = [
  403. 'form' => [
  404. 'class' => 'form-horizontal',
  405. 'method' => 'post',
  406. 'action' => '/articles/add',
  407. 'accept-charset' => 'utf-8',
  408. ],
  409. ];
  410. $this->assertHtml($expected, $result);
  411. }
  412. /**
  413. * Test create() with the templates option.
  414. */
  415. public function testCreateTemplatesFile(): void
  416. {
  417. $result = $this->Form->create($this->article, [
  418. 'templates' => 'htmlhelper_tags',
  419. ]);
  420. $expected = [
  421. 'start form',
  422. ];
  423. $this->assertHtml($expected, $result);
  424. }
  425. /**
  426. * Test that create() and end() restore templates.
  427. */
  428. public function testCreateEndRestoreTemplates(): void
  429. {
  430. $this->Form->create($this->article, [
  431. 'templates' => ['input' => 'custom input element'],
  432. ]);
  433. $this->Form->end();
  434. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  435. }
  436. /**
  437. * Test using template vars in various templates used by control() method.
  438. */
  439. public function testControlTemplateVars(): void
  440. {
  441. $result = $this->Form->control('text', [
  442. 'templates' => [
  443. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  444. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  445. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  446. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  447. ],
  448. 'templateVars' => [
  449. 'forinput' => 'in-input',
  450. 'forlabel' => 'in-label',
  451. 'forgroup' => 'in-group',
  452. 'forcontainer' => 'in-container',
  453. ],
  454. ]);
  455. $expected = [
  456. 'div' => ['class'],
  457. 'label' => ['for'],
  458. 'Text in-label',
  459. '/label',
  460. 'in-group',
  461. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  462. 'in-container',
  463. '/div',
  464. ];
  465. $this->assertHtml($expected, $result);
  466. }
  467. /**
  468. * Test ensuring template variables work in template files loaded
  469. * during control().
  470. */
  471. public function testControlTemplatesFromFile(): void
  472. {
  473. $result = $this->Form->control('title', [
  474. 'templates' => 'test_templates',
  475. 'templateVars' => [
  476. 'forcontainer' => 'container-data',
  477. ],
  478. ]);
  479. $expected = [
  480. 'div' => ['class'],
  481. 'label' => ['for'],
  482. 'Title',
  483. '/label',
  484. 'input' => ['name', 'type' => 'text', 'id'],
  485. 'container-data',
  486. '/div',
  487. ];
  488. $this->assertHtml($expected, $result);
  489. }
  490. /**
  491. * Test using template vars in inputSubmit and submitContainer template.
  492. */
  493. public function testSubmitTemplateVars(): void
  494. {
  495. $this->Form->setTemplates([
  496. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  497. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  498. ]);
  499. $result = $this->Form->submit('Submit', [
  500. 'templateVars' => [
  501. 'forinput' => 'in-input',
  502. 'forcontainer' => 'in-container',
  503. ],
  504. ]);
  505. $expected = [
  506. 'div' => ['class'],
  507. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  508. 'in-container',
  509. '/div',
  510. ];
  511. $this->assertHtml($expected, $result);
  512. }
  513. /**
  514. * test the create() method
  515. *
  516. * @dataProvider requestTypeProvider
  517. */
  518. public function testCreateTypeOptions(string $type, string $method, string $override): void
  519. {
  520. $encoding = strtolower(Configure::read('App.encoding'));
  521. $result = $this->Form->create(null, ['type' => $type]);
  522. $expected = [
  523. 'form' => [
  524. 'method' => $method, 'action' => '/articles/add',
  525. 'accept-charset' => $encoding,
  526. ],
  527. ];
  528. $extra = [
  529. 'div' => ['style' => 'display:none;'],
  530. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  531. '/div',
  532. ];
  533. if ($type !== 'post') {
  534. $expected = array_merge($expected, $extra);
  535. }
  536. $this->assertHtml($expected, $result);
  537. }
  538. /**
  539. * Test using template vars in Create (formStart template)
  540. */
  541. public function testCreateTemplateVars(): void
  542. {
  543. $result = $this->Form->create($this->article, [
  544. 'templates' => [
  545. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  546. ],
  547. 'templateVars' => ['header' => 'headertext'],
  548. ]);
  549. $expected = [
  550. 'h4' => ['class'],
  551. 'headertext',
  552. '/h4',
  553. 'form' => [
  554. 'method' => 'post',
  555. 'action' => '/articles/add',
  556. 'accept-charset' => 'utf-8',
  557. ],
  558. ];
  559. $this->assertHtml($expected, $result);
  560. }
  561. /**
  562. * Test opening a form for an update operation.
  563. */
  564. public function testCreateUpdateForm(): void
  565. {
  566. $encoding = strtolower(Configure::read('App.encoding'));
  567. $this->View->setRequest($this->View->getRequest()
  568. ->withRequestTarget('/articles/edit/1')
  569. ->withParam('action', 'edit'));
  570. $this->article['defaults']['id'] = 1;
  571. $result = $this->Form->create($this->article);
  572. $expected = [
  573. 'form' => [
  574. 'method' => 'post',
  575. 'action' => '/articles/edit/1',
  576. 'accept-charset' => $encoding,
  577. ],
  578. 'div' => ['style' => 'display:none;'],
  579. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  580. '/div',
  581. ];
  582. $this->assertHtml($expected, $result);
  583. }
  584. /**
  585. * test create() with automatic url generation
  586. */
  587. public function testCreateAutoUrl(): void
  588. {
  589. $encoding = strtolower(Configure::read('App.encoding'));
  590. $this->View->setRequest($this->View->getRequest()
  591. ->withRequestTarget('/articles/delete/10')
  592. ->withParam('action', 'delete'));
  593. $result = $this->Form->create($this->article);
  594. $expected = [
  595. 'form' => [
  596. 'method' => 'post', 'action' => '/articles/delete/10',
  597. 'accept-charset' => $encoding,
  598. ],
  599. ];
  600. $this->assertHtml($expected, $result);
  601. $this->article['defaults'] = ['id' => 1];
  602. $this->View->setRequest($this->View->getRequest()
  603. ->withRequestTarget('/Articles/edit/1')
  604. ->withParam('action', 'delete'));
  605. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  606. $expected = [
  607. 'form' => [
  608. 'method' => 'post',
  609. 'action' => '/Articles/edit/1',
  610. 'accept-charset' => $encoding,
  611. ],
  612. 'div' => ['style' => 'display:none;'],
  613. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  614. '/div',
  615. ];
  616. $this->assertHtml($expected, $result);
  617. $this->View->setRequest($this->View->getRequest()
  618. ->withParam('action', 'add'));
  619. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  620. $expected = [
  621. 'form' => [
  622. 'method' => 'post',
  623. 'action' => '/Articles/publish/1',
  624. 'accept-charset' => $encoding,
  625. ],
  626. 'div' => ['style' => 'display:none;'],
  627. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  628. '/div',
  629. ];
  630. $this->assertHtml($expected, $result);
  631. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  632. $expected = [
  633. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  634. 'div' => ['style' => 'display:none;'],
  635. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  636. '/div',
  637. ];
  638. $this->assertHtml($expected, $result);
  639. $this->View->setRequest($this->View->getRequest()
  640. ->withParam('controller', 'Pages'));
  641. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  642. $expected = [
  643. 'form' => [
  644. 'method' => 'post', 'action' => '/Pages/signup/1',
  645. 'accept-charset' => $encoding,
  646. ],
  647. 'div' => ['style' => 'display:none;'],
  648. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  649. '/div',
  650. ];
  651. $this->assertHtml($expected, $result);
  652. }
  653. /**
  654. * Test create() with no URL (no "action" attribute for <form> tag)
  655. */
  656. public function testCreateNoUrl(): void
  657. {
  658. $result = $this->Form->create(null, ['url' => false]);
  659. $expected = [
  660. 'form' => [
  661. 'method' => 'post',
  662. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  663. ],
  664. ];
  665. $this->assertHtml($expected, $result);
  666. }
  667. /**
  668. * test create() with a custom route
  669. */
  670. public function testCreateCustomRoute(): void
  671. {
  672. $builder = Router::createRouteBuilder('/');
  673. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  674. $encoding = strtolower(Configure::read('App.encoding'));
  675. $this->View->setRequest($this->View->getRequest()
  676. ->withParam('controller', 'Users'));
  677. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  678. $expected = [
  679. 'form' => [
  680. 'method' => 'post', 'action' => '/login',
  681. 'accept-charset' => $encoding,
  682. ],
  683. ];
  684. $this->assertHtml($expected, $result);
  685. $builder->connect(
  686. '/new-article',
  687. ['controller' => 'Articles', 'action' => 'myAction'],
  688. ['_name' => 'my-route']
  689. );
  690. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  691. $expected = [
  692. 'form' => [
  693. 'method' => 'post', 'action' => '/new-article',
  694. 'accept-charset' => $encoding,
  695. ],
  696. ];
  697. $this->assertHtml($expected, $result);
  698. }
  699. /**
  700. * test automatic accept-charset overriding
  701. */
  702. public function testCreateWithAcceptCharset(): void
  703. {
  704. $result = $this->Form->create(
  705. $this->article,
  706. [
  707. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  708. ]
  709. );
  710. $expected = [
  711. 'form' => [
  712. 'method' => 'post', 'action' => '/Articles',
  713. 'accept-charset' => 'iso-8859-1',
  714. ],
  715. ];
  716. $this->assertHtml($expected, $result);
  717. }
  718. /**
  719. * Test base form URL when 'url' param is passed with multiple parameters (&)
  720. */
  721. public function testCreateQueryStringRequest(): void
  722. {
  723. $encoding = strtolower(Configure::read('App.encoding'));
  724. $result = $this->Form->create($this->article, [
  725. 'type' => 'post',
  726. 'escape' => false,
  727. 'url' => [
  728. 'controller' => 'Controller',
  729. 'action' => 'action',
  730. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  731. ],
  732. ]);
  733. $expected = [
  734. 'form' => [
  735. 'method' => 'post',
  736. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  737. 'accept-charset' => $encoding,
  738. ],
  739. ];
  740. $this->assertHtml($expected, $result);
  741. $result = $this->Form->create($this->article, [
  742. 'type' => 'post',
  743. 'url' => [
  744. 'controller' => 'Controller',
  745. 'action' => 'action',
  746. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  747. ],
  748. ]);
  749. $this->assertHtml($expected, $result);
  750. }
  751. /**
  752. * test that create() doesn't cause errors by multiple id's being in the primary key
  753. * as could happen with multiple select or checkboxes.
  754. */
  755. public function testCreateWithMultipleIdInData(): void
  756. {
  757. $encoding = strtolower(Configure::read('App.encoding'));
  758. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  759. $result = $this->Form->create($this->article);
  760. $expected = [
  761. 'form' => [
  762. 'method' => 'post',
  763. 'action' => '/articles/add',
  764. 'accept-charset' => $encoding,
  765. ],
  766. ];
  767. $this->assertHtml($expected, $result);
  768. }
  769. /**
  770. * test that create() doesn't add in extra passed params.
  771. */
  772. public function testCreatePassedArgs(): void
  773. {
  774. $encoding = strtolower(Configure::read('App.encoding'));
  775. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  776. $result = $this->Form->create($this->article, [
  777. 'type' => 'post',
  778. 'escape' => false,
  779. 'url' => [
  780. 'action' => 'edit',
  781. 'myparam',
  782. ],
  783. ]);
  784. $expected = [
  785. 'form' => [
  786. 'method' => 'post',
  787. 'action' => '/Articles/edit/myparam',
  788. 'accept-charset' => $encoding,
  789. ],
  790. ];
  791. $this->assertHtml($expected, $result);
  792. }
  793. /**
  794. * test creating a get form, and get form inputs.
  795. */
  796. public function testGetFormCreate(): void
  797. {
  798. $encoding = strtolower(Configure::read('App.encoding'));
  799. $result = $this->Form->create($this->article, ['type' => 'get']);
  800. $expected = ['form' => [
  801. 'method' => 'get', 'action' => '/articles/add',
  802. 'accept-charset' => $encoding,
  803. ]];
  804. $this->assertHtml($expected, $result);
  805. $result = $this->Form->text('title');
  806. $expected = ['input' => [
  807. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  808. ]];
  809. $this->assertHtml($expected, $result);
  810. $result = $this->Form->password('password');
  811. $expected = ['input' => [
  812. 'name' => 'password', 'type' => 'password',
  813. ]];
  814. $this->assertHtml($expected, $result);
  815. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  816. $result = $this->Form->text('user_form');
  817. $expected = ['input' => [
  818. 'name' => 'user_form', 'type' => 'text',
  819. ]];
  820. $this->assertHtml($expected, $result);
  821. }
  822. /**
  823. * test get form, and inputs when the model param is false
  824. */
  825. public function testGetFormWithFalseModel(): void
  826. {
  827. $encoding = strtolower(Configure::read('App.encoding'));
  828. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  829. $result = $this->Form->create(null, [
  830. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  831. ]);
  832. $expected = ['form' => [
  833. 'method' => 'get', 'action' => '/ContactTest/add',
  834. 'accept-charset' => $encoding,
  835. ]];
  836. $this->assertHtml($expected, $result);
  837. $result = $this->Form->text('reason');
  838. $expected = [
  839. 'input' => ['type' => 'text', 'name' => 'reason'],
  840. ];
  841. $this->assertHtml($expected, $result);
  842. }
  843. /**
  844. * testFormCreateWithSecurity method
  845. *
  846. * Test form->create() with security key.
  847. */
  848. public function testCreateWithSecurity(): void
  849. {
  850. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  851. $encoding = strtolower(Configure::read('App.encoding'));
  852. $result = $this->Form->create($this->article, [
  853. 'url' => '/articles/publish',
  854. ]);
  855. $expected = [
  856. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  857. 'div' => ['style' => 'display:none;'],
  858. ['input' => [
  859. 'type' => 'hidden',
  860. 'name' => '_csrfToken',
  861. 'value' => 'testKey',
  862. ]],
  863. '/div',
  864. ];
  865. $this->assertHtml($expected, $result);
  866. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  867. $expected['form']['id'] = 'MyForm';
  868. $this->assertHtml($expected, $result);
  869. }
  870. /**
  871. * testFormCreateGetNoSecurity method
  872. *
  873. * Test form->create() with no security key as its a get form
  874. */
  875. public function testCreateEndGetNoSecurity(): void
  876. {
  877. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  878. $article = new Article();
  879. $result = $this->Form->create($article, [
  880. 'type' => 'get',
  881. 'url' => '/contacts/add',
  882. ]);
  883. $this->assertStringNotContainsString('testKey', $result);
  884. $result = $this->Form->end();
  885. $this->assertStringNotContainsString('testKey', $result);
  886. }
  887. /**
  888. * Tests form hash generation with model-less data
  889. */
  890. public function testValidateHashNoModel(): void
  891. {
  892. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  893. $fields = ['anything'];
  894. $this->Form->create();
  895. $result = $this->Form->secure($fields);
  896. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  897. $this->assertStringContainsString($hash, $result);
  898. }
  899. /**
  900. * Tests that hidden fields generated for checkboxes don't get locked
  901. */
  902. public function testNoCheckboxLocking(): void
  903. {
  904. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  905. $this->Form->create();
  906. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  907. $this->Form->checkbox('check', ['value' => '1']);
  908. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  909. }
  910. /**
  911. * testFormSecurityFields method
  912. *
  913. * Test generation of secure form hash generation.
  914. */
  915. public function testFormSecurityFields(): void
  916. {
  917. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  918. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  919. $this->Form->create();
  920. $result = $this->Form->secure($fields);
  921. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  922. $hash .= ':' . 'Model.valid';
  923. $hash = urlencode($hash);
  924. $tokenDebug = urlencode(json_encode([
  925. $this->url,
  926. $fields,
  927. [],
  928. ]));
  929. $expected = [
  930. 'div' => ['style' => 'display:none;'],
  931. ['input' => [
  932. 'type' => 'hidden',
  933. 'name' => '_Token[fields]',
  934. 'value' => $hash,
  935. ]],
  936. ['input' => [
  937. 'type' => 'hidden',
  938. 'name' => '_Token[unlocked]',
  939. 'value' => '',
  940. ]],
  941. ['input' => [
  942. 'type' => 'hidden',
  943. 'name' => '_Token[debug]',
  944. 'value' => $tokenDebug,
  945. ]],
  946. '/div',
  947. ];
  948. $this->assertHtml($expected, $result);
  949. }
  950. /**
  951. * testFormSecurityFields method
  952. *
  953. * Test debug token is not generated if debug is false
  954. */
  955. public function testFormSecurityFieldsNoDebugMode(): void
  956. {
  957. Configure::write('debug', false);
  958. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  959. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  960. $this->Form->create();
  961. $result = $this->Form->secure($fields);
  962. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  963. $hash .= ':' . 'Model.valid';
  964. $hash = urlencode($hash);
  965. $expected = [
  966. 'div' => ['style' => 'display:none;'],
  967. ['input' => [
  968. 'type' => 'hidden',
  969. 'name' => '_Token[fields]',
  970. 'value' => $hash,
  971. ]],
  972. ['input' => [
  973. 'type' => 'hidden',
  974. 'name' => '_Token[unlocked]',
  975. 'value' => '',
  976. ]],
  977. '/div',
  978. ];
  979. $this->assertHtml($expected, $result);
  980. }
  981. /**
  982. * Tests correct generation of number fields for smallint
  983. */
  984. public function testTextFieldGenerationForSmallint(): void
  985. {
  986. $this->article['schema'] = [
  987. 'foo' => [
  988. 'type' => 'smallinteger',
  989. 'null' => false,
  990. 'default' => null,
  991. 'length' => 10,
  992. ],
  993. ];
  994. $this->Form->create($this->article);
  995. $result = $this->Form->control('foo');
  996. $this->assertStringContainsString('class="input number"', $result);
  997. $this->assertStringContainsString('type="number"', $result);
  998. }
  999. /**
  1000. * Tests correct generation of number fields for tinyint
  1001. */
  1002. public function testTextFieldGenerationForTinyint(): void
  1003. {
  1004. $this->article['schema'] = [
  1005. 'foo' => [
  1006. 'type' => 'tinyinteger',
  1007. 'null' => false,
  1008. 'default' => null,
  1009. 'length' => 10,
  1010. ],
  1011. ];
  1012. $this->Form->create($this->article);
  1013. $result = $this->Form->control('foo');
  1014. $this->assertStringContainsString('class="input number"', $result);
  1015. $this->assertStringContainsString('type="number"', $result);
  1016. }
  1017. /**
  1018. * Tests correct generation of number fields for double and float fields
  1019. */
  1020. public function testTextFieldGenerationForFloats(): void
  1021. {
  1022. $this->article['schema'] = [
  1023. 'foo' => [
  1024. 'type' => 'float',
  1025. 'null' => false,
  1026. 'default' => null,
  1027. 'length' => 10,
  1028. ],
  1029. ];
  1030. $this->Form->create($this->article);
  1031. $result = $this->Form->control('foo');
  1032. $expected = [
  1033. 'div' => ['class' => 'input number'],
  1034. 'label' => ['for' => 'foo'],
  1035. 'Foo',
  1036. '/label',
  1037. ['input' => [
  1038. 'type' => 'number',
  1039. 'name' => 'foo',
  1040. 'id' => 'foo',
  1041. 'step' => 'any',
  1042. ]],
  1043. '/div',
  1044. ];
  1045. $this->assertHtml($expected, $result);
  1046. $result = $this->Form->control('foo', ['step' => 0.5]);
  1047. $expected = [
  1048. 'div' => ['class' => 'input number'],
  1049. 'label' => ['for' => 'foo'],
  1050. 'Foo',
  1051. '/label',
  1052. ['input' => [
  1053. 'type' => 'number',
  1054. 'name' => 'foo',
  1055. 'id' => 'foo',
  1056. 'step' => '0.5',
  1057. ]],
  1058. '/div',
  1059. ];
  1060. $this->assertHtml($expected, $result);
  1061. }
  1062. /**
  1063. * Tests correct generation of number fields for integer fields
  1064. */
  1065. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1066. {
  1067. $this->getTableLocator()->get('Contacts', [
  1068. 'className' => ContactsTable::class,
  1069. ]);
  1070. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1071. $result = $this->Form->control('age');
  1072. $expected = [
  1073. 'div' => ['class' => 'input number'],
  1074. 'label' => ['for' => 'age'],
  1075. 'Age',
  1076. '/label',
  1077. ['input' => [
  1078. 'type' => 'number', 'name' => 'age',
  1079. 'id' => 'age',
  1080. ]],
  1081. '/div',
  1082. ];
  1083. $this->assertHtml($expected, $result);
  1084. }
  1085. /**
  1086. * Tests correct generation of file upload fields for binary fields
  1087. */
  1088. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1089. {
  1090. $table = $this->getTableLocator()->get('Contacts', [
  1091. 'className' => ContactsTable::class,
  1092. ]);
  1093. $table->setSchema(['foo' => [
  1094. 'type' => 'binary',
  1095. 'null' => false,
  1096. 'default' => null,
  1097. 'length' => 1024,
  1098. ]]);
  1099. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1100. $result = $this->Form->control('foo');
  1101. $expected = [
  1102. 'div' => ['class' => 'input file'],
  1103. 'label' => ['for' => 'foo'],
  1104. 'Foo',
  1105. '/label',
  1106. ['input' => [
  1107. 'type' => 'file', 'name' => 'foo',
  1108. 'id' => 'foo',
  1109. ]],
  1110. '/div',
  1111. ];
  1112. $this->assertHtml($expected, $result);
  1113. }
  1114. /**
  1115. * testFormSecurityMultipleFields method
  1116. *
  1117. * Test secure() with multiple row form. Ensure hash is correct.
  1118. */
  1119. public function testFormSecurityMultipleFields(): void
  1120. {
  1121. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1122. $this->Form->create();
  1123. $fields = [
  1124. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1125. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1126. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1127. ];
  1128. $result = $this->Form->secure($fields);
  1129. $sortedFields = [
  1130. 'Model.0.password',
  1131. 'Model.0.username',
  1132. 'Model.1.password',
  1133. 'Model.1.username',
  1134. 'Model.0.hidden' => 'value',
  1135. 'Model.0.valid' => '0',
  1136. 'Model.1.hidden' => 'value',
  1137. 'Model.1.valid' => '0',
  1138. ];
  1139. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1140. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1141. $hash = urlencode($hash);
  1142. $tokenDebug = urlencode(json_encode([
  1143. $this->url,
  1144. $fields,
  1145. [],
  1146. ]));
  1147. $expected = [
  1148. 'div' => ['style' => 'display:none;'],
  1149. ['input' => [
  1150. 'type' => 'hidden',
  1151. 'name' => '_Token[fields]',
  1152. 'value' => $hash,
  1153. ]],
  1154. ['input' => [
  1155. 'type' => 'hidden',
  1156. 'name' => '_Token[unlocked]',
  1157. 'value' => '',
  1158. ]],
  1159. ['input' => [
  1160. 'type' => 'hidden',
  1161. 'name' => '_Token[debug]',
  1162. 'value' => $tokenDebug,
  1163. ]],
  1164. '/div',
  1165. ];
  1166. $this->assertHtml($expected, $result);
  1167. }
  1168. /**
  1169. * testFormSecurityMultipleSubmitButtons
  1170. *
  1171. * test form submit generation and ensure that _Token is only created on end()
  1172. */
  1173. public function testFormSecurityMultipleSubmitButtons(): void
  1174. {
  1175. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1176. $this->Form->create($this->article);
  1177. $this->Form->text('Address.title');
  1178. $this->Form->text('Address.first_name');
  1179. $result = $this->Form->submit('Save', ['name' => 'save']);
  1180. $expected = [
  1181. 'div' => ['class' => 'submit'],
  1182. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1183. '/div',
  1184. ];
  1185. $this->assertHtml($expected, $result);
  1186. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1187. $expected = [
  1188. 'div' => ['class' => 'submit'],
  1189. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1190. '/div',
  1191. ];
  1192. $this->assertHtml($expected, $result);
  1193. $result = $this->Form->end();
  1194. $tokenDebug = urlencode(json_encode([
  1195. '/articles/add',
  1196. [
  1197. 'Address.title',
  1198. 'Address.first_name',
  1199. ],
  1200. ['save', 'cancel'],
  1201. ]));
  1202. $expected = [
  1203. 'div' => ['style' => 'display:none;'],
  1204. ['input' => [
  1205. 'type' => 'hidden',
  1206. 'name' => '_Token[fields]',
  1207. 'value',
  1208. ]],
  1209. ['input' => [
  1210. 'type' => 'hidden',
  1211. 'name' => '_Token[unlocked]',
  1212. 'value' => 'cancel%7Csave',
  1213. ]],
  1214. ['input' => [
  1215. 'type' => 'hidden',
  1216. 'name' => '_Token[debug]',
  1217. 'value' => $tokenDebug,
  1218. ]],
  1219. '/div',
  1220. ];
  1221. $this->assertHtml($expected, $result);
  1222. }
  1223. /**
  1224. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1225. */
  1226. public function testSecurityButtonNestedNamed(): void
  1227. {
  1228. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1229. $this->Form->create();
  1230. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1231. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1232. $this->assertEquals(['Address.button'], $result);
  1233. }
  1234. /**
  1235. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1236. */
  1237. public function testSecuritySubmitNestedNamed(): void
  1238. {
  1239. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1240. $this->Form->create($this->article);
  1241. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1242. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1243. $this->assertEquals(['Address.button'], $result);
  1244. }
  1245. /**
  1246. * Test that the correct fields are unlocked for image submits with no names.
  1247. */
  1248. public function testSecuritySubmitImageNoName(): void
  1249. {
  1250. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1251. $this->Form->create();
  1252. $result = $this->Form->submit('save.png');
  1253. $expected = [
  1254. 'div' => ['class' => 'submit'],
  1255. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1256. '/div',
  1257. ];
  1258. $this->assertHtml($expected, $result);
  1259. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1260. $this->assertEquals(['x', 'y'], $result);
  1261. }
  1262. /**
  1263. * Test that the correct fields are unlocked for image submits with names.
  1264. */
  1265. public function testSecuritySubmitImageName(): void
  1266. {
  1267. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1268. $this->Form->create();
  1269. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1270. $expected = [
  1271. 'div' => ['class' => 'submit'],
  1272. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1273. '/div',
  1274. ];
  1275. $this->assertHtml($expected, $result);
  1276. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1277. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1278. }
  1279. /**
  1280. * testFormSecurityMultipleControlFields method
  1281. *
  1282. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1283. */
  1284. public function testFormSecurityMultipleControlFields(): void
  1285. {
  1286. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1287. $this->Form->create();
  1288. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1289. $this->Form->control('Addresses.0.title');
  1290. $this->Form->control('Addresses.0.first_name');
  1291. $this->Form->control('Addresses.0.last_name');
  1292. $this->Form->control('Addresses.0.address');
  1293. $this->Form->control('Addresses.0.city');
  1294. $this->Form->control('Addresses.0.phone');
  1295. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1296. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1297. $this->Form->control('Addresses.1.title');
  1298. $this->Form->control('Addresses.1.first_name');
  1299. $this->Form->control('Addresses.1.last_name');
  1300. $this->Form->control('Addresses.1.address');
  1301. $this->Form->control('Addresses.1.city');
  1302. $this->Form->control('Addresses.1.phone');
  1303. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1304. $result = $this->Form->secure();
  1305. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1306. $tokenDebug = urlencode(json_encode([
  1307. '/articles/add',
  1308. [
  1309. 'Addresses.0.id' => '123456',
  1310. 'Addresses.0.title',
  1311. 'Addresses.0.first_name',
  1312. 'Addresses.0.last_name',
  1313. 'Addresses.0.address',
  1314. 'Addresses.0.city',
  1315. 'Addresses.0.phone',
  1316. 'Addresses.0.primary',
  1317. 'Addresses.1.id' => '654321',
  1318. 'Addresses.1.title',
  1319. 'Addresses.1.first_name',
  1320. 'Addresses.1.last_name',
  1321. 'Addresses.1.address',
  1322. 'Addresses.1.city',
  1323. 'Addresses.1.phone',
  1324. 'Addresses.1.primary',
  1325. ],
  1326. [],
  1327. ]));
  1328. $expected = [
  1329. 'div' => ['style' => 'display:none;'],
  1330. ['input' => [
  1331. 'type' => 'hidden',
  1332. 'name' => '_Token[fields]',
  1333. 'value' => $hash,
  1334. ]],
  1335. ['input' => [
  1336. 'type' => 'hidden',
  1337. 'name' => '_Token[unlocked]',
  1338. 'value' => '',
  1339. ]],
  1340. ['input' => [
  1341. 'type' => 'hidden',
  1342. 'name' => '_Token[debug]',
  1343. 'value' => $tokenDebug,
  1344. ]],
  1345. '/div',
  1346. ];
  1347. $this->assertHtml($expected, $result);
  1348. }
  1349. /**
  1350. * testFormSecurityArrayFields method
  1351. *
  1352. * Test form security with Model.field.0 style inputs.
  1353. */
  1354. public function testFormSecurityArrayFields(): void
  1355. {
  1356. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1357. $this->Form->create();
  1358. $this->Form->text('Address.primary.1');
  1359. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1360. $this->assertSame('Address.primary', $result[0]);
  1361. $this->Form->text('Address.secondary.1.0');
  1362. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1363. $this->assertSame('Address.secondary', $result[1]);
  1364. }
  1365. /**
  1366. * testFormSecurityMultipleControlDisabledFields method
  1367. *
  1368. * Test secure form generation with multiple records and disabled fields.
  1369. */
  1370. public function testFormSecurityMultipleControlDisabledFields(): void
  1371. {
  1372. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1373. 'unlockedFields' => ['first_name', 'address'],
  1374. ]));
  1375. $this->Form->create();
  1376. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1377. $this->Form->text('Addresses.0.title');
  1378. $this->Form->text('Addresses.0.first_name');
  1379. $this->Form->text('Addresses.0.last_name');
  1380. $this->Form->text('Addresses.0.address');
  1381. $this->Form->text('Addresses.0.city');
  1382. $this->Form->text('Addresses.0.phone');
  1383. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1384. $this->Form->text('Addresses.1.title');
  1385. $this->Form->text('Addresses.1.first_name');
  1386. $this->Form->text('Addresses.1.last_name');
  1387. $this->Form->text('Addresses.1.address');
  1388. $this->Form->text('Addresses.1.city');
  1389. $this->Form->text('Addresses.1.phone');
  1390. $result = $this->Form->secure();
  1391. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1392. $tokenDebug = urlencode(json_encode([
  1393. '/articles/add',
  1394. [
  1395. 'Addresses.0.id' => '123456',
  1396. 'Addresses.0.title',
  1397. 'Addresses.0.last_name',
  1398. 'Addresses.0.city',
  1399. 'Addresses.0.phone',
  1400. 'Addresses.1.id' => '654321',
  1401. 'Addresses.1.title',
  1402. 'Addresses.1.last_name',
  1403. 'Addresses.1.city',
  1404. 'Addresses.1.phone',
  1405. ],
  1406. [
  1407. 'first_name',
  1408. 'address',
  1409. ],
  1410. ]));
  1411. $expected = [
  1412. 'div' => ['style' => 'display:none;'],
  1413. ['input' => [
  1414. 'type' => 'hidden',
  1415. 'name' => '_Token[fields]',
  1416. 'value' => $hash,
  1417. ]],
  1418. ['input' => [
  1419. 'type' => 'hidden',
  1420. 'name' => '_Token[unlocked]',
  1421. 'value' => 'address%7Cfirst_name',
  1422. ]],
  1423. ['input' => [
  1424. 'type' => 'hidden',
  1425. 'name' => '_Token[debug]',
  1426. 'value' => $tokenDebug,
  1427. ]],
  1428. '/div',
  1429. ];
  1430. $this->assertHtml($expected, $result);
  1431. }
  1432. /**
  1433. * testFormSecurityControlDisabledFields method
  1434. *
  1435. * Test single record form with disabled fields.
  1436. */
  1437. public function testFormSecurityControlUnlockedFields(): void
  1438. {
  1439. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1440. 'unlockedFields' => ['first_name', 'address'],
  1441. ]));
  1442. $this->Form->create();
  1443. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1444. $this->assertEquals(
  1445. $this->View->getRequest()->getAttribute('formTokenData'),
  1446. ['unlockedFields' => $result]
  1447. );
  1448. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1449. $this->Form->text('Addresses.title');
  1450. $this->Form->text('Addresses.first_name');
  1451. $this->Form->text('Addresses.last_name');
  1452. $this->Form->text('Addresses.address');
  1453. $this->Form->text('Addresses.city');
  1454. $this->Form->text('Addresses.phone');
  1455. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1456. $expected = [
  1457. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1458. 'Addresses.city', 'Addresses.phone',
  1459. ];
  1460. $this->assertEquals($expected, $result);
  1461. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1462. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1463. $tokenDebug = urlencode(json_encode([
  1464. '/articles/add',
  1465. [
  1466. 'Addresses.id' => '123456',
  1467. 'Addresses.title',
  1468. 'Addresses.last_name',
  1469. 'Addresses.city',
  1470. 'Addresses.phone',
  1471. ],
  1472. [
  1473. 'first_name',
  1474. 'address',
  1475. ],
  1476. ]));
  1477. $expected = [
  1478. 'div' => ['style' => 'display:none;'],
  1479. ['input' => [
  1480. 'type' => 'hidden',
  1481. 'name' => '_Token[fields]',
  1482. 'value' => $hash,
  1483. 'data-foo' => 'bar',
  1484. ]],
  1485. ['input' => [
  1486. 'type' => 'hidden',
  1487. 'name' => '_Token[unlocked]',
  1488. 'value' => 'address%7Cfirst_name',
  1489. 'data-foo' => 'bar',
  1490. ]],
  1491. ['input' => [
  1492. 'type' => 'hidden', 'name' => '_Token[debug]',
  1493. 'value' => $tokenDebug,
  1494. 'data-foo' => 'bar',
  1495. ]],
  1496. '/div',
  1497. ];
  1498. $this->assertHtml($expected, $result);
  1499. }
  1500. /**
  1501. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1502. *
  1503. * Test single record form with debugSecurity param.
  1504. */
  1505. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1506. {
  1507. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1508. 'unlockedFields' => ['first_name', 'address'],
  1509. ]));
  1510. $this->Form->create();
  1511. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1512. $this->assertEquals(
  1513. $this->View->getRequest()->getAttribute('formTokenData'),
  1514. ['unlockedFields' => $result]
  1515. );
  1516. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1517. $this->Form->text('Addresses.title');
  1518. $this->Form->text('Addresses.first_name');
  1519. $this->Form->text('Addresses.last_name');
  1520. $this->Form->text('Addresses.address');
  1521. $this->Form->text('Addresses.city');
  1522. $this->Form->text('Addresses.phone');
  1523. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1524. $expected = [
  1525. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1526. 'Addresses.city', 'Addresses.phone',
  1527. ];
  1528. $this->assertEquals($expected, $result);
  1529. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1530. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1531. $tokenDebug = urlencode(json_encode([
  1532. '/articles/add',
  1533. [
  1534. 'Addresses.id' => '123456',
  1535. 'Addresses.title',
  1536. 'Addresses.last_name',
  1537. 'Addresses.city',
  1538. 'Addresses.phone',
  1539. ],
  1540. [
  1541. 'first_name',
  1542. 'address',
  1543. ],
  1544. ]));
  1545. $expected = [
  1546. 'div' => ['style' => 'display:none;'],
  1547. ['input' => [
  1548. 'type' => 'hidden',
  1549. 'name' => '_Token[fields]',
  1550. 'value' => $hash,
  1551. 'data-foo' => 'bar',
  1552. ]],
  1553. ['input' => [
  1554. 'type' => 'hidden',
  1555. 'name' => '_Token[unlocked]',
  1556. 'value' => 'address%7Cfirst_name',
  1557. 'data-foo' => 'bar',
  1558. ]],
  1559. ['input' => [
  1560. 'type' => 'hidden', 'name' => '_Token[debug]',
  1561. 'value' => $tokenDebug,
  1562. 'data-foo' => 'bar',
  1563. ]],
  1564. '/div',
  1565. ];
  1566. $this->assertHtml($expected, $result);
  1567. }
  1568. /**
  1569. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1570. *
  1571. * Debug is false, debugSecurity is true -> no debug
  1572. */
  1573. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1574. {
  1575. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1576. 'unlockedFields' => ['first_name', 'address'],
  1577. ]));
  1578. $this->Form->create();
  1579. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1580. $this->assertEquals(
  1581. $this->View->getRequest()->getAttribute('formTokenData'),
  1582. ['unlockedFields' => $result]
  1583. );
  1584. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1585. $this->Form->text('Addresses.title');
  1586. $this->Form->text('Addresses.first_name');
  1587. $this->Form->text('Addresses.last_name');
  1588. $this->Form->text('Addresses.address');
  1589. $this->Form->text('Addresses.city');
  1590. $this->Form->text('Addresses.phone');
  1591. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1592. $expected = [
  1593. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1594. 'Addresses.city', 'Addresses.phone',
  1595. ];
  1596. $this->assertEquals($expected, $result);
  1597. Configure::write('debug', false);
  1598. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1599. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1600. $expected = [
  1601. 'div' => ['style' => 'display:none;'],
  1602. ['input' => [
  1603. 'type' => 'hidden',
  1604. 'name' => '_Token[fields]',
  1605. 'value' => $hash,
  1606. 'data-foo' => 'bar',
  1607. ]],
  1608. ['input' => [
  1609. 'type' => 'hidden',
  1610. 'name' => '_Token[unlocked]',
  1611. 'value' => 'address%7Cfirst_name',
  1612. 'data-foo' => 'bar',
  1613. ]],
  1614. '/div',
  1615. ];
  1616. $this->assertHtml($expected, $result);
  1617. }
  1618. /**
  1619. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1620. *
  1621. * Test single record form with debugSecurity param.
  1622. */
  1623. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1624. {
  1625. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1626. 'unlockedFields' => ['first_name', 'address'],
  1627. ]));
  1628. $this->Form->create();
  1629. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1630. $this->assertEquals(
  1631. $this->View->getRequest()->getAttribute('formTokenData'),
  1632. ['unlockedFields' => $result]
  1633. );
  1634. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1635. $this->Form->text('Addresses.title');
  1636. $this->Form->text('Addresses.first_name');
  1637. $this->Form->text('Addresses.last_name');
  1638. $this->Form->text('Addresses.address');
  1639. $this->Form->text('Addresses.city');
  1640. $this->Form->text('Addresses.phone');
  1641. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1642. $expected = [
  1643. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1644. 'Addresses.city', 'Addresses.phone',
  1645. ];
  1646. $this->assertEquals($expected, $result);
  1647. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1648. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  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. '/div',
  1664. ];
  1665. $this->assertHtml($expected, $result);
  1666. }
  1667. /**
  1668. * testFormSecureWithCustomNameAttribute method
  1669. *
  1670. * Test securing inputs with custom name attributes.
  1671. */
  1672. public function testFormSecureWithCustomNameAttribute(): void
  1673. {
  1674. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1675. $this->Form->create();
  1676. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1677. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1678. $this->assertSame('User.custom', $result[0]);
  1679. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1680. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1681. $this->assertSame('User.custom.another.value', $result[1]);
  1682. }
  1683. /**
  1684. * testFormSecuredControl method
  1685. *
  1686. * Test generation of entire secure form, assertions made on control() output.
  1687. */
  1688. public function testFormSecuredControl(): void
  1689. {
  1690. $this->View->setRequest($this->View->getRequest()
  1691. ->withAttribute('formTokenData', [])
  1692. ->withAttribute('csrfToken', 'testKey'));
  1693. $this->article['schema'] = [
  1694. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1695. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1696. ];
  1697. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1698. $encoding = strtolower(Configure::read('App.encoding'));
  1699. $expected = [
  1700. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1701. 'div' => ['style' => 'display:none;'],
  1702. ['input' => [
  1703. 'type' => 'hidden',
  1704. 'name' => '_csrfToken',
  1705. 'value' => 'testKey',
  1706. ]],
  1707. '/div',
  1708. ];
  1709. $this->assertHtml($expected, $result);
  1710. $result = $this->Form->control('ratio');
  1711. $expected = [
  1712. 'div' => ['class'],
  1713. 'label' => ['for'],
  1714. 'Ratio',
  1715. '/label',
  1716. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1717. '/div',
  1718. ];
  1719. $this->assertHtml($expected, $result);
  1720. $result = $this->Form->control('population');
  1721. $expected = [
  1722. 'div' => ['class'],
  1723. 'label' => ['for'],
  1724. 'Population',
  1725. '/label',
  1726. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1727. '/div',
  1728. ];
  1729. $this->assertHtml($expected, $result);
  1730. $result = $this->Form->control('published', ['type' => 'text']);
  1731. $expected = [
  1732. 'div' => ['class' => 'input text'],
  1733. 'label' => ['for' => 'published'],
  1734. 'Published',
  1735. '/label',
  1736. ['input' => [
  1737. 'type' => 'text',
  1738. 'name' => 'published',
  1739. 'id' => 'published',
  1740. ]],
  1741. '/div',
  1742. ];
  1743. $this->assertHtml($expected, $result);
  1744. $result = $this->Form->control('other', ['type' => 'text']);
  1745. $expected = [
  1746. 'div' => ['class' => 'input text'],
  1747. 'label' => ['for' => 'other'],
  1748. 'Other',
  1749. '/label',
  1750. ['input' => [
  1751. 'type' => 'text',
  1752. 'name' => 'other',
  1753. 'id',
  1754. ]],
  1755. '/div',
  1756. ];
  1757. $this->assertHtml($expected, $result);
  1758. $result = $this->Form->hidden('stuff');
  1759. $expected = [
  1760. 'input' => [
  1761. 'type' => 'hidden',
  1762. 'name' => 'stuff',
  1763. ],
  1764. ];
  1765. $this->assertHtml($expected, $result);
  1766. $result = $this->Form->hidden('hidden', ['value' => false]);
  1767. $expected = ['input' => [
  1768. 'type' => 'hidden',
  1769. 'name' => 'hidden',
  1770. 'value' => '0',
  1771. ]];
  1772. $this->assertHtml($expected, $result);
  1773. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1774. $expected = [
  1775. 'div' => ['class' => 'input checkbox'],
  1776. ['input' => [
  1777. 'type' => 'hidden',
  1778. 'name' => 'something',
  1779. 'value' => '0',
  1780. ]],
  1781. 'label' => ['for' => 'something'],
  1782. ['input' => [
  1783. 'type' => 'checkbox',
  1784. 'name' => 'something',
  1785. 'value' => '1',
  1786. 'id' => 'something',
  1787. ]],
  1788. 'Something',
  1789. '/label',
  1790. '/div',
  1791. ];
  1792. $this->assertHtml($expected, $result);
  1793. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1794. $expectedFields = [
  1795. 'ratio',
  1796. 'population',
  1797. 'published',
  1798. 'other',
  1799. 'stuff' => '',
  1800. 'hidden' => '0',
  1801. 'something',
  1802. ];
  1803. $this->assertEquals($expectedFields, $result);
  1804. $result = $this->Form->secure();
  1805. $tokenDebug = urlencode(json_encode([
  1806. '/articles/add',
  1807. $expectedFields,
  1808. [],
  1809. ]));
  1810. $expected = [
  1811. 'div' => ['style' => 'display:none;'],
  1812. ['input' => [
  1813. 'type' => 'hidden',
  1814. 'name' => '_Token[fields]',
  1815. 'value',
  1816. ]],
  1817. ['input' => [
  1818. 'type' => 'hidden',
  1819. 'name' => '_Token[unlocked]',
  1820. 'value' => '',
  1821. ]],
  1822. ['input' => [
  1823. 'type' => 'hidden', 'name' => '_Token[debug]',
  1824. 'value' => $tokenDebug,
  1825. ]],
  1826. '/div',
  1827. ];
  1828. $this->assertHtml($expected, $result);
  1829. $data = [
  1830. 'ratio' => '',
  1831. 'population' => '',
  1832. 'published' => '',
  1833. 'other' => '',
  1834. 'stuff' => '',
  1835. 'hidden' => '0',
  1836. 'something' => '',
  1837. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1838. ];
  1839. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1840. }
  1841. /**
  1842. * testSecuredControlCustomName method
  1843. *
  1844. * Test secured inputs with custom names.
  1845. */
  1846. public function testSecuredControlCustomName(): void
  1847. {
  1848. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1849. $this->Form->create();
  1850. $this->Form->text('text_input', [
  1851. 'name' => 'Option[General.default_role]',
  1852. ]);
  1853. $expected = ['Option.General.default_role'];
  1854. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1855. $this->assertEquals($expected, $result);
  1856. $this->Form->select('select_box', [1, 2], [
  1857. 'name' => 'Option[General.select_role]',
  1858. ]);
  1859. $expected[] = 'Option.General.select_role';
  1860. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1861. $this->assertEquals($expected, $result);
  1862. $this->Form->text('other.things[]');
  1863. $expected[] = 'other.things';
  1864. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1865. $this->assertEquals($expected, $result);
  1866. }
  1867. /**
  1868. * testSecuredControlDuplicate method
  1869. *
  1870. * Test that a hidden field followed by a visible field
  1871. * undoes the hidden field locking.
  1872. */
  1873. public function testSecuredControlDuplicate(): void
  1874. {
  1875. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1876. $this->Form->create();
  1877. $this->Form->control('text_val', [
  1878. 'type' => 'hidden',
  1879. 'value' => 'some text',
  1880. ]);
  1881. $expected = ['text_val' => 'some text'];
  1882. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1883. $this->assertEquals($expected, $result);
  1884. $this->Form->control('text_val', [
  1885. 'type' => 'text',
  1886. ]);
  1887. $expected = ['text_val'];
  1888. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1889. $this->assertEquals($expected, $result);
  1890. }
  1891. /**
  1892. * testFormSecuredFileControl method
  1893. *
  1894. * Tests that the correct keys are added to the field hash index.
  1895. */
  1896. public function testFormSecuredFileControl(): void
  1897. {
  1898. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1899. $this->Form->create();
  1900. $this->Form->file('Attachment.file');
  1901. $expected = ['Attachment.file'];
  1902. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1903. $this->assertEquals($expected, $result);
  1904. }
  1905. /**
  1906. * testFormSecuredMultipleSelect method
  1907. *
  1908. * Test that multiple selects keys are added to field hash.
  1909. */
  1910. public function testFormSecuredMultipleSelect(): void
  1911. {
  1912. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1913. $this->Form->create();
  1914. $options = ['1' => 'one', '2' => 'two'];
  1915. $this->Form->select('Model.select', $options);
  1916. $expected = ['Model.select'];
  1917. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1918. $this->assertEquals($expected, $result);
  1919. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1920. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1921. $this->assertEquals($expected, $result);
  1922. }
  1923. /**
  1924. * testFormSecuredRadio method
  1925. */
  1926. public function testFormSecuredRadio(): void
  1927. {
  1928. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1929. $this->Form->create();
  1930. $options = ['1' => 'option1', '2' => 'option2'];
  1931. $this->Form->radio('Test.test', $options);
  1932. $expected = ['Test.test'];
  1933. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1934. $this->assertEquals($expected, $result);
  1935. $this->Form->radio('Test.all', $options, [
  1936. 'disabled' => ['option1', 'option2'],
  1937. ]);
  1938. $expected = ['Test.test', 'Test.all' => ''];
  1939. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1940. $this->assertEquals($expected, $result);
  1941. $this->Form->radio('Test.some', $options, [
  1942. 'disabled' => ['option1'],
  1943. ]);
  1944. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1945. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1946. $this->assertEquals($expected, $result);
  1947. }
  1948. /**
  1949. * testFormSecuredAndDisabled method
  1950. *
  1951. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1952. * hashing.
  1953. */
  1954. public function testFormSecuredAndDisabled(): void
  1955. {
  1956. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1957. $this->Form->create();
  1958. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1959. $this->Form->text('Model.text', ['disabled' => true]);
  1960. $this->Form->password('Model.text', ['disabled' => true]);
  1961. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1962. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1963. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1964. $this->Form->year('Model.year', ['disabled' => true]);
  1965. $this->Form->month('Model.month', ['disabled' => true]);
  1966. $this->Form->day('Model.day', ['disabled' => true]);
  1967. $this->Form->hour('Model.hour', ['disabled' => true]);
  1968. $this->Form->minute('Model.minute', ['disabled' => true]);
  1969. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  1970. $expected = [
  1971. 'Model.radio' => '',
  1972. ];
  1973. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1974. $this->assertEquals($expected, $result);
  1975. }
  1976. /**
  1977. * testUnlockFieldAddsToList method
  1978. *
  1979. * Test disableField.
  1980. */
  1981. public function testUnlockFieldAddsToList(): void
  1982. {
  1983. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1984. 'unlockedFields' => [],
  1985. ]));
  1986. $this->Form->create();
  1987. $this->Form->unlockField('Contact.name');
  1988. $this->Form->text('Contact.name');
  1989. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1990. $this->assertEquals([], $result);
  1991. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1992. $this->assertEquals(['Contact.name'], $result);
  1993. }
  1994. /**
  1995. * testUnlockFieldRemovingFromFields method
  1996. *
  1997. * Test unlockField removing from fields array.
  1998. */
  1999. public function testUnlockFieldRemovingFromFields(): void
  2000. {
  2001. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2002. 'unlockedFields' => [],
  2003. ]));
  2004. $this->Form->create($this->article);
  2005. $this->Form->hidden('Article.id', ['value' => 1]);
  2006. $this->Form->text('Article.title');
  2007. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2008. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2009. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2010. $this->Form->unlockField('Article.title');
  2011. $this->Form->unlockField('Article.id');
  2012. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2013. $this->assertEquals([], $result);
  2014. }
  2015. /**
  2016. * testResetUnlockFields method
  2017. *
  2018. * Test reset unlockFields, when create new form.
  2019. */
  2020. public function testResetUnlockFields(): void
  2021. {
  2022. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2023. 'key' => 'testKey',
  2024. 'unlockedFields' => [],
  2025. ]));
  2026. $this->Form->create();
  2027. $this->Form->unlockField('Contact.id');
  2028. $this->Form->hidden('Contact.id', ['value' => 1]);
  2029. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2030. $this->assertEmpty($result, 'Field should be unlocked');
  2031. $this->Form->end();
  2032. $this->Form->create();
  2033. $this->Form->hidden('Contact.id', ['value' => 1]);
  2034. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2035. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2036. }
  2037. /**
  2038. * testSecuredFormUrlIgnoresHost method
  2039. *
  2040. * Test that only the path + query elements of a form's URL show up in their hash.
  2041. */
  2042. public function testSecuredFormUrlIgnoresHost(): void
  2043. {
  2044. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2045. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2046. $this->Form->create($this->article, [
  2047. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2048. ]);
  2049. $result = $this->Form->secure();
  2050. $this->assertStringContainsString($expected, $result);
  2051. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2052. $result = $this->Form->secure();
  2053. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2054. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2055. $result = $this->Form->secure();
  2056. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2057. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2058. $result = $this->Form->secure();
  2059. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2060. }
  2061. /**
  2062. * testSecuredFormUrlHasHtmlAndIdentifier method
  2063. *
  2064. * Test that URL, HTML and identifier show up in their hashes.
  2065. */
  2066. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2067. {
  2068. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2069. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2070. $this->Form->create($this->article, [
  2071. 'url' => [
  2072. 'controller' => 'articles',
  2073. 'action' => 'view',
  2074. '?' => [
  2075. 'page' => 1,
  2076. 'limit' => 10,
  2077. 'html' => '<>"',
  2078. ],
  2079. '#' => 'result',
  2080. ],
  2081. ]);
  2082. $result = $this->Form->secure();
  2083. $this->assertStringContainsString($expected, $result);
  2084. $this->Form->create($this->article, [
  2085. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2086. ]);
  2087. $result = $this->Form->secure();
  2088. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2089. $this->Form->create($this->article, [
  2090. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2091. ]);
  2092. $result = $this->Form->secure();
  2093. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2094. }
  2095. /**
  2096. * testErrorMessageDisplay method
  2097. *
  2098. * Test error message display.
  2099. */
  2100. public function testErrorMessageDisplay(): void
  2101. {
  2102. $this->article['errors'] = [
  2103. 'Article' => [
  2104. 'title' => 'error message',
  2105. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2106. ],
  2107. ];
  2108. $this->Form->create($this->article);
  2109. $result = $this->Form->control('Article.title');
  2110. $expected = [
  2111. 'div' => ['class' => 'input text error'],
  2112. 'label' => ['for' => 'article-title'],
  2113. 'Title',
  2114. '/label',
  2115. 'input' => [
  2116. 'type' => 'text',
  2117. 'name' => 'Article[title]',
  2118. 'id' => 'article-title',
  2119. 'class' => 'form-error',
  2120. 'aria-invalid' => 'true',
  2121. 'aria-describedby' => 'article-title-error',
  2122. ],
  2123. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2124. 'error message',
  2125. '/div',
  2126. '/div',
  2127. ];
  2128. $this->assertHtml($expected, $result);
  2129. $result = $this->Form->control('Article.title', [
  2130. 'templates' => [
  2131. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2132. ],
  2133. ]);
  2134. $expected = [
  2135. 'div' => ['class' => 'input text error'],
  2136. 'label' => ['for' => 'article-title'],
  2137. 'Title',
  2138. '/label',
  2139. 'input' => [
  2140. 'type' => 'text',
  2141. 'name' => 'Article[title]',
  2142. 'id' => 'article-title',
  2143. 'class' => 'form-error',
  2144. // No aria-describedby because error template is custom
  2145. 'aria-invalid' => 'true',
  2146. ],
  2147. '/div',
  2148. ];
  2149. $this->assertHtml($expected, $result);
  2150. $result = $this->Form->control('Article.content');
  2151. $expected = [
  2152. 'div' => ['class' => 'input text error'],
  2153. 'label' => ['for' => 'article-content'],
  2154. 'Content',
  2155. '/label',
  2156. 'input' => [
  2157. 'type' => 'text',
  2158. 'name' => 'Article[content]',
  2159. 'id' => 'article-content',
  2160. 'class' => 'form-error',
  2161. 'aria-invalid' => 'true',
  2162. 'aria-describedby' => 'article-content-error',
  2163. ],
  2164. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2165. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2166. '/div',
  2167. '/div',
  2168. ];
  2169. $this->assertHtml($expected, $result);
  2170. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2171. $expected = [
  2172. 'div' => ['class' => 'input text error'],
  2173. 'label' => ['for' => 'article-content'],
  2174. 'Content',
  2175. '/label',
  2176. 'input' => [
  2177. 'type' => 'text',
  2178. 'name' => 'Article[content]',
  2179. 'id' => 'article-content',
  2180. 'class' => 'form-error',
  2181. 'aria-invalid' => 'true',
  2182. 'aria-describedby' => 'article-content-error',
  2183. ],
  2184. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2185. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2186. '/div',
  2187. '/div',
  2188. ];
  2189. $this->assertHtml($expected, $result);
  2190. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2191. $expected = [
  2192. 'div' => ['class' => 'input text error'],
  2193. 'label' => ['for' => 'article-content'],
  2194. 'Content',
  2195. '/label',
  2196. 'input' => [
  2197. 'type' => 'text',
  2198. 'name' => 'Article[content]',
  2199. 'id' => 'article-content',
  2200. 'class' => 'form-error',
  2201. 'aria-invalid' => 'true',
  2202. 'aria-describedby' => 'article-content-error',
  2203. ],
  2204. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2205. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2206. '/div',
  2207. '/div',
  2208. ];
  2209. $this->assertHtml($expected, $result);
  2210. }
  2211. /**
  2212. * testEmptyErrorValidation method
  2213. *
  2214. * Test validation errors, when validation message is an empty string.
  2215. */
  2216. public function testEmptyErrorValidation(): void
  2217. {
  2218. $this->article['errors'] = [
  2219. 'Article' => ['title' => ''],
  2220. ];
  2221. $this->Form->create($this->article);
  2222. $result = $this->Form->control('Article.title');
  2223. $expected = [
  2224. 'div' => ['class' => 'input text error'],
  2225. 'label' => ['for' => 'article-title'],
  2226. 'Title',
  2227. '/label',
  2228. 'input' => [
  2229. 'type' => 'text',
  2230. 'name' => 'Article[title]',
  2231. 'id' => 'article-title',
  2232. 'class' => 'form-error',
  2233. 'aria-invalid' => 'true',
  2234. 'aria-describedby' => 'article-title-error',
  2235. ],
  2236. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2237. [],
  2238. '/div',
  2239. '/div',
  2240. ];
  2241. $this->assertHtml($expected, $result);
  2242. }
  2243. /**
  2244. * testEmptyControlErrorValidation method
  2245. *
  2246. * Test validation errors, when calling control() overriding validation message by an empty string.
  2247. */
  2248. public function testEmptyControlErrorValidation(): void
  2249. {
  2250. $this->article['errors'] = [
  2251. 'Article' => ['title' => 'error message'],
  2252. ];
  2253. $this->Form->create($this->article);
  2254. $result = $this->Form->control('Article.title', ['error' => '']);
  2255. $expected = [
  2256. 'div' => ['class' => 'input text error'],
  2257. 'label' => ['for' => 'article-title'],
  2258. 'Title',
  2259. '/label',
  2260. 'input' => [
  2261. 'aria-invalid' => 'true',
  2262. 'aria-describedby' => 'article-title-error',
  2263. 'type' => 'text',
  2264. 'name' => 'Article[title]',
  2265. 'id' => 'article-title',
  2266. 'class' => 'form-error',
  2267. ],
  2268. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2269. [],
  2270. '/div',
  2271. '/div',
  2272. ];
  2273. $this->assertHtml($expected, $result);
  2274. }
  2275. /**
  2276. * testControlErrorMessage method
  2277. *
  2278. * Test validation errors, when calling control() overriding validation messages.
  2279. */
  2280. public function testControlErrorMessage(): void
  2281. {
  2282. $this->article['errors'] = [
  2283. 'title' => ['error message'],
  2284. ];
  2285. $this->Form->create($this->article);
  2286. $result = $this->Form->control('title', [
  2287. 'error' => 'Custom error!',
  2288. ]);
  2289. $expected = [
  2290. 'div' => ['class' => 'input text required error'],
  2291. 'label' => ['for' => 'title'],
  2292. 'Title',
  2293. '/label',
  2294. 'input' => [
  2295. 'type' => 'text',
  2296. 'name' => 'title',
  2297. 'id' => 'title',
  2298. 'class' => 'form-error',
  2299. 'required' => 'required',
  2300. 'data-validity-message' => 'This field cannot be left empty',
  2301. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2302. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2303. 'aria-required' => 'true',
  2304. 'aria-invalid' => 'true',
  2305. 'aria-describedby' => 'title-error',
  2306. ],
  2307. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2308. 'Custom error!',
  2309. '/div',
  2310. '/div',
  2311. ];
  2312. $this->assertHtml($expected, $result);
  2313. $result = $this->Form->control('title', [
  2314. 'error' => ['error message' => 'Custom error!'],
  2315. ]);
  2316. $expected = [
  2317. 'div' => ['class' => 'input text required error'],
  2318. 'label' => ['for' => 'title'],
  2319. 'Title',
  2320. '/label',
  2321. 'input' => [
  2322. 'type' => 'text',
  2323. 'name' => 'title',
  2324. 'id' => 'title',
  2325. 'aria-required' => 'true',
  2326. 'aria-invalid' => 'true',
  2327. 'aria-describedby' => 'title-error',
  2328. 'class' => 'form-error',
  2329. 'required' => 'required',
  2330. 'data-validity-message' => 'This field cannot be left empty',
  2331. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2332. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2333. ],
  2334. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2335. 'Custom error!',
  2336. '/div',
  2337. '/div',
  2338. ];
  2339. $this->assertHtml($expected, $result);
  2340. }
  2341. /**
  2342. * testFormValidationAssociated method
  2343. *
  2344. * Tests displaying errors for nested entities.
  2345. */
  2346. public function testFormValidationAssociated(): void
  2347. {
  2348. $nested = new Entity(['foo' => 'bar']);
  2349. $nested->setError('foo', ['not a valid bar']);
  2350. $entity = new Entity(['nested' => $nested]);
  2351. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2352. $result = $this->Form->error('nested.foo');
  2353. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2354. }
  2355. /**
  2356. * testFormValidationAssociatedSecondLevel method
  2357. *
  2358. * Test form error display with associated model.
  2359. */
  2360. public function testFormValidationAssociatedSecondLevel(): void
  2361. {
  2362. $inner = new Entity(['bar' => 'baz']);
  2363. $nested = new Entity(['foo' => $inner]);
  2364. $entity = new Entity(['nested' => $nested]);
  2365. $inner->setError('bar', ['not a valid one']);
  2366. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2367. $result = $this->Form->error('nested.foo.bar');
  2368. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2369. }
  2370. /**
  2371. * testFormValidationMultiRecord method
  2372. *
  2373. * Test form error display with multiple records.
  2374. */
  2375. public function testFormValidationMultiRecord(): void
  2376. {
  2377. $one = new Entity();
  2378. $two = new Entity();
  2379. $this->getTableLocator()->get('Contacts', [
  2380. 'className' => ContactsTable::class,
  2381. ]);
  2382. $one->set('email', '');
  2383. $one->setError('email', ['invalid email']);
  2384. $two->set('name', '');
  2385. $two->setError('name', ['This is wrong']);
  2386. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2387. $result = $this->Form->control('0.email');
  2388. $expected = [
  2389. 'div' => ['class' => 'input email error'],
  2390. 'label' => ['for' => '0-email'],
  2391. 'Email',
  2392. '/label',
  2393. 'input' => [
  2394. 'type' => 'email',
  2395. 'name' => '0[email]',
  2396. 'id' => '0-email',
  2397. 'class' => 'form-error',
  2398. 'maxlength' => 255,
  2399. 'value' => '',
  2400. 'aria-invalid' => 'true',
  2401. 'aria-describedby' => '0-email-error',
  2402. ],
  2403. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2404. 'invalid email',
  2405. '/div',
  2406. '/div',
  2407. ];
  2408. $this->assertHtml($expected, $result);
  2409. $result = $this->Form->control('1.name');
  2410. $expected = [
  2411. 'div' => ['class' => 'input text error'],
  2412. 'label' => ['for' => '1-name'],
  2413. 'Name',
  2414. '/label',
  2415. 'input' => [
  2416. 'type' => 'text',
  2417. 'name' => '1[name]',
  2418. 'id' => '1-name',
  2419. 'class' => 'form-error',
  2420. 'maxlength' => 255,
  2421. 'value' => '',
  2422. 'aria-invalid' => 'true',
  2423. 'aria-describedby' => '1-name-error',
  2424. ],
  2425. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2426. 'This is wrong',
  2427. '/div',
  2428. '/div',
  2429. ];
  2430. $this->assertHtml($expected, $result);
  2431. }
  2432. /**
  2433. * testControl method
  2434. *
  2435. * Test various incarnations of control().
  2436. */
  2437. public function testControl(): void
  2438. {
  2439. $this->getTableLocator()->get('ValidateUsers', [
  2440. 'className' => ValidateUsersTable::class,
  2441. ]);
  2442. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2443. $result = $this->Form->control('ValidateUsers.balance');
  2444. $expected = [
  2445. 'div' => ['class'],
  2446. 'label' => ['for'],
  2447. 'Balance',
  2448. '/label',
  2449. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2450. '/div',
  2451. ];
  2452. $this->assertHtml($expected, $result);
  2453. $result = $this->Form->control('ValidateUser.cost_decimal');
  2454. $expected = [
  2455. 'div' => ['class'],
  2456. 'label' => ['for'],
  2457. 'Cost Decimal',
  2458. '/label',
  2459. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2460. '/div',
  2461. ];
  2462. $this->assertHtml($expected, $result);
  2463. $result = $this->Form->control('ValidateUser.null_decimal');
  2464. $expected = [
  2465. 'div' => ['class'],
  2466. 'label' => ['for'],
  2467. 'Null Decimal',
  2468. '/label',
  2469. 'input' => ['name', 'type' => 'number', 'id'],
  2470. '/div',
  2471. ];
  2472. $this->assertHtml($expected, $result);
  2473. }
  2474. /**
  2475. * testControlCustomization method
  2476. *
  2477. * Tests the input method and passing custom options.
  2478. */
  2479. public function testControlCustomization(): void
  2480. {
  2481. $this->getTableLocator()->get('Contacts', [
  2482. 'className' => ContactsTable::class,
  2483. ]);
  2484. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2485. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2486. $expected = [
  2487. 'div' => ['class' => 'input email'],
  2488. 'label' => ['for' => 'custom'],
  2489. 'Email',
  2490. '/label',
  2491. ['input' => [
  2492. 'type' => 'email', 'name' => 'Contact[email]',
  2493. 'id' => 'custom', 'maxlength' => 255,
  2494. ]],
  2495. '/div',
  2496. ];
  2497. $this->assertHtml($expected, $result);
  2498. $result = $this->Form->control('Contact.email', [
  2499. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2500. ]);
  2501. $expected = [
  2502. '<div',
  2503. 'label' => ['for' => 'contact-email'],
  2504. 'Email',
  2505. '/label',
  2506. ['input' => [
  2507. 'type' => 'email', 'name' => 'Contact[email]',
  2508. 'id' => 'contact-email', 'maxlength' => 255,
  2509. ]],
  2510. '/div',
  2511. ];
  2512. $this->assertHtml($expected, $result);
  2513. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2514. $expected = [
  2515. 'div' => ['class' => 'input text'],
  2516. 'label' => ['for' => 'contact-email'],
  2517. 'Email',
  2518. '/label',
  2519. ['input' => [
  2520. 'type' => 'text', 'name' => 'Contact[email]',
  2521. 'id' => 'contact-email', 'maxlength' => '255',
  2522. ]],
  2523. '/div',
  2524. ];
  2525. $this->assertHtml($expected, $result);
  2526. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2527. $expected = [
  2528. 'div' => ['class' => 'input text'],
  2529. 'label' => ['for' => 'contact-5-email'],
  2530. 'Email',
  2531. '/label',
  2532. ['input' => [
  2533. 'type' => 'text', 'name' => 'Contact[5][email]',
  2534. 'id' => 'contact-5-email', 'maxlength' => '255',
  2535. ]],
  2536. '/div',
  2537. ];
  2538. $this->assertHtml($expected, $result);
  2539. $result = $this->Form->control('Contact.password');
  2540. $expected = [
  2541. 'div' => ['class' => 'input password'],
  2542. 'label' => ['for' => 'contact-password'],
  2543. 'Password',
  2544. '/label',
  2545. ['input' => [
  2546. 'type' => 'password', 'name' => 'Contact[password]',
  2547. 'id' => 'contact-password',
  2548. ]],
  2549. '/div',
  2550. ];
  2551. $this->assertHtml($expected, $result);
  2552. $result = $this->Form->control('Contact.email', [
  2553. 'type' => 'file', 'class' => 'textbox',
  2554. ]);
  2555. $expected = [
  2556. 'div' => ['class' => 'input file'],
  2557. 'label' => ['for' => 'contact-email'],
  2558. 'Email',
  2559. '/label',
  2560. ['input' => [
  2561. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2562. 'id' => 'contact-email',
  2563. ]],
  2564. '/div',
  2565. ];
  2566. $this->assertHtml($expected, $result);
  2567. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2568. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2569. $result = $this->Form->control('phone');
  2570. $expected = [
  2571. 'div' => ['class' => 'input tel'],
  2572. 'label' => ['for' => 'phone'],
  2573. 'Phone',
  2574. '/label',
  2575. ['input' => [
  2576. 'type' => 'tel', 'name' => 'phone',
  2577. 'value' => 'Hello &amp; World &gt; weird chars',
  2578. 'id' => 'phone', 'maxlength' => 255,
  2579. ]],
  2580. '/div',
  2581. ];
  2582. $this->assertHtml($expected, $result);
  2583. $this->View->setRequest(
  2584. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2585. );
  2586. $this->Form->create();
  2587. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2588. $expected = [
  2589. 'div' => ['class' => 'input text'],
  2590. 'label' => ['for' => 'myId'],
  2591. 'Field',
  2592. '/label',
  2593. 'input' => [
  2594. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2595. 'value' => 'My value', 'id' => 'myId',
  2596. ],
  2597. '/div',
  2598. ];
  2599. $this->assertHtml($expected, $result);
  2600. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2601. $this->Form->create();
  2602. $entity->setError('field', 'Badness!');
  2603. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2604. $result = $this->Form->control('field');
  2605. $expected = [
  2606. 'div' => ['class' => 'input text error'],
  2607. 'label' => ['for' => 'field'],
  2608. 'Field',
  2609. '/label',
  2610. 'input' => [
  2611. 'type' => 'text',
  2612. 'name' => 'field',
  2613. 'id' => 'field',
  2614. 'class' => 'form-error',
  2615. 'aria-invalid' => 'true',
  2616. 'aria-describedby' => 'field-error',
  2617. ],
  2618. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2619. 'Badness!',
  2620. '/div',
  2621. '/div',
  2622. ];
  2623. $this->assertHtml($expected, $result);
  2624. $result = $this->Form->control('field', [
  2625. 'templates' => [
  2626. 'inputContainerError' => '{{content}}{{error}}',
  2627. 'error' => '<span class="error-message">{{content}}</span>',
  2628. ],
  2629. ]);
  2630. $expected = [
  2631. 'label' => ['for' => 'field'],
  2632. 'Field',
  2633. '/label',
  2634. 'input' => [
  2635. 'type' => 'text',
  2636. 'name' => 'field',
  2637. 'id' => 'field',
  2638. 'class' => 'form-error',
  2639. // No aria-describedby because error template is custom
  2640. 'aria-invalid' => 'true',
  2641. ],
  2642. ['span' => ['class' => 'error-message']],
  2643. 'Badness!',
  2644. '/span',
  2645. ];
  2646. $this->assertHtml($expected, $result);
  2647. $entity->setError('field', ['minLength'], true);
  2648. $result = $this->Form->control('field', [
  2649. 'error' => [
  2650. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2651. 'maxLength' => 'login too large',
  2652. ],
  2653. ]);
  2654. $expected = [
  2655. 'div' => ['class' => 'input text error'],
  2656. 'label' => ['for' => 'field'],
  2657. 'Field',
  2658. '/label',
  2659. 'input' => [
  2660. 'type' => 'text',
  2661. 'name' => 'field',
  2662. 'id' => 'field',
  2663. 'class' => 'form-error',
  2664. 'aria-invalid' => 'true',
  2665. 'aria-describedby' => 'field-error',
  2666. ],
  2667. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2668. 'Le login doit contenir au moins 2 caractères',
  2669. '/div',
  2670. '/div',
  2671. ];
  2672. $this->assertHtml($expected, $result);
  2673. $entity->setError('field', ['maxLength'], true);
  2674. $result = $this->Form->control('field', [
  2675. 'error' => [
  2676. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2677. 'maxLength' => 'login too large',
  2678. ],
  2679. ]);
  2680. $expected = [
  2681. 'div' => ['class' => 'input text error'],
  2682. 'label' => ['for' => 'field'],
  2683. 'Field',
  2684. '/label',
  2685. 'input' => [
  2686. 'type' => 'text',
  2687. 'name' => 'field',
  2688. 'id' => 'field',
  2689. 'class' => 'form-error',
  2690. 'aria-invalid' => 'true',
  2691. 'aria-describedby' => 'field-error',
  2692. ],
  2693. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2694. 'login too large',
  2695. '/div',
  2696. '/div',
  2697. ];
  2698. $this->assertHtml($expected, $result);
  2699. }
  2700. /**
  2701. * testControlWithTemplateFile method
  2702. *
  2703. * Test that control() accepts a template file.
  2704. */
  2705. public function testControlWithTemplateFile(): void
  2706. {
  2707. $result = $this->Form->control('field', [
  2708. 'templates' => 'htmlhelper_tags',
  2709. ]);
  2710. $expected = [
  2711. 'label' => ['for' => 'field'],
  2712. 'Field',
  2713. '/label',
  2714. 'input' => [
  2715. 'type' => 'text', 'name' => 'field',
  2716. 'id' => 'field',
  2717. ],
  2718. ];
  2719. $this->assertHtml($expected, $result);
  2720. }
  2721. /**
  2722. * testNestedControlsEndWithBrackets method
  2723. *
  2724. * Test that nested inputs end with brackets.
  2725. */
  2726. public function testNestedControlsEndWithBrackets(): void
  2727. {
  2728. $result = $this->Form->text('nested.text[]');
  2729. $expected = [
  2730. 'input' => [
  2731. 'type' => 'text', 'name' => 'nested[text][]',
  2732. ],
  2733. ];
  2734. $this->assertHtml($expected, $result);
  2735. $result = $this->Form->file('nested.file[]');
  2736. $expected = [
  2737. 'input' => [
  2738. 'type' => 'file', 'name' => 'nested[file][]',
  2739. ],
  2740. ];
  2741. $this->assertHtml($expected, $result);
  2742. }
  2743. /**
  2744. * testCreateIdPrefix method
  2745. *
  2746. * Test id prefix.
  2747. */
  2748. public function testCreateIdPrefix(): void
  2749. {
  2750. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2751. $result = $this->Form->control('field');
  2752. $expected = [
  2753. 'div' => ['class' => 'input text'],
  2754. 'label' => ['for' => 'prefix-field'],
  2755. 'Field',
  2756. '/label',
  2757. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2758. '/div',
  2759. ];
  2760. $this->assertHtml($expected, $result);
  2761. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2762. $expected = [
  2763. 'div' => ['class' => 'input text'],
  2764. 'label' => ['for' => 'custom-id'],
  2765. 'Field',
  2766. '/label',
  2767. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2768. '/div',
  2769. ];
  2770. $this->assertHtml($expected, $result);
  2771. $result = $this->Form->radio('Model.field', ['option A']);
  2772. $expected = [
  2773. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2774. 'label' => ['for' => 'prefix-model-field-0'],
  2775. ['input' => [
  2776. 'type' => 'radio',
  2777. 'name' => 'Model[field]',
  2778. 'value' => '0',
  2779. 'id' => 'prefix-model-field-0',
  2780. ]],
  2781. 'option A',
  2782. '/label',
  2783. ];
  2784. $this->assertHtml($expected, $result);
  2785. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2786. $expected = [
  2787. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2788. 'label' => ['for' => 'prefix-model-field-0'],
  2789. ['input' => [
  2790. 'type' => 'radio',
  2791. 'name' => 'Model[field]',
  2792. 'value' => '0',
  2793. 'id' => 'prefix-model-field-0',
  2794. ]],
  2795. 'option A',
  2796. '/label',
  2797. ];
  2798. $this->assertHtml($expected, $result);
  2799. $result = $this->Form->select(
  2800. 'Model.multi_field',
  2801. ['first'],
  2802. ['multiple' => 'checkbox']
  2803. );
  2804. $expected = [
  2805. 'input' => [
  2806. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2807. ],
  2808. ['div' => ['class' => 'checkbox']],
  2809. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2810. ['input' => [
  2811. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2812. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2813. ]],
  2814. 'first',
  2815. '/label',
  2816. '/div',
  2817. ];
  2818. $this->assertHtml($expected, $result);
  2819. $this->Form->end();
  2820. $result = $this->Form->control('field');
  2821. $expected = [
  2822. 'div' => ['class' => 'input text'],
  2823. 'label' => ['for' => 'field'],
  2824. 'Field',
  2825. '/label',
  2826. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2827. '/div',
  2828. ];
  2829. $this->assertHtml($expected, $result);
  2830. }
  2831. /**
  2832. * testControlZero method
  2833. *
  2834. * Test that inputs with 0 can be created.
  2835. */
  2836. public function testControlZero(): void
  2837. {
  2838. $this->getTableLocator()->get('Contacts', [
  2839. 'className' => ContactsTable::class,
  2840. ]);
  2841. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2842. $result = $this->Form->control('0');
  2843. $expected = [
  2844. 'div' => ['class' => 'input text'],
  2845. 'label' => ['for' => '0'], '/label',
  2846. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2847. '/div',
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. }
  2851. /**
  2852. * testControlCheckbox method
  2853. *
  2854. * Test control() with checkbox creation.
  2855. */
  2856. public function testControlCheckbox(): void
  2857. {
  2858. $articles = $this->getTableLocator()->get('Articles');
  2859. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2860. $article = $articles->newEmptyEntity();
  2861. $this->Form->create($article);
  2862. $result = $this->Form->control('Articles.active');
  2863. $expected = [
  2864. 'div' => ['class' => 'input checkbox'],
  2865. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2866. 'label' => ['for' => 'articles-active'],
  2867. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2868. 'Active',
  2869. '/label',
  2870. '/div',
  2871. ];
  2872. $this->assertHtml($expected, $result);
  2873. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2874. $expected = [
  2875. 'div' => ['class' => 'input checkbox'],
  2876. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2877. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2878. '/div',
  2879. ];
  2880. $this->assertHtml($expected, $result);
  2881. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2882. $expected = [
  2883. 'div' => ['class' => 'input checkbox'],
  2884. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2885. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2886. '/div',
  2887. ];
  2888. $this->assertHtml($expected, $result);
  2889. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2890. $expected = [
  2891. 'div' => ['class' => 'input checkbox'],
  2892. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2893. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2894. '/div',
  2895. ];
  2896. $this->assertHtml($expected, $result);
  2897. $result = $this->Form->control('Articles.disabled', [
  2898. 'label' => 'Disabled',
  2899. 'type' => 'checkbox',
  2900. 'data-foo' => 'disabled',
  2901. ]);
  2902. $expected = [
  2903. 'div' => ['class' => 'input checkbox'],
  2904. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2905. 'label' => ['for' => 'articles-disabled'],
  2906. ['input' => [
  2907. 'type' => 'checkbox',
  2908. 'name' => 'Articles[disabled]',
  2909. 'value' => '1',
  2910. 'id' => 'articles-disabled',
  2911. 'data-foo' => 'disabled',
  2912. ]],
  2913. 'Disabled',
  2914. '/label',
  2915. '/div',
  2916. ];
  2917. $this->assertHtml($expected, $result);
  2918. $result = $this->Form->control('Articles.confirm', [
  2919. 'label' => 'Confirm <b>me</b>!',
  2920. 'type' => 'checkbox',
  2921. 'escape' => false,
  2922. ]);
  2923. $expected = [
  2924. 'div' => ['class' => 'input checkbox'],
  2925. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2926. 'label' => ['for' => 'articles-confirm'],
  2927. ['input' => [
  2928. 'type' => 'checkbox',
  2929. 'name' => 'Articles[confirm]',
  2930. 'value' => '1',
  2931. 'id' => 'articles-confirm',
  2932. ]],
  2933. 'Confirm <b>me</b>!',
  2934. '/label',
  2935. '/div',
  2936. ];
  2937. $this->assertHtml($expected, $result);
  2938. }
  2939. /**
  2940. * testControlHidden method
  2941. *
  2942. * Test that control() does not create wrapping div and label tag for hidden fields.
  2943. */
  2944. public function testControlHidden(): void
  2945. {
  2946. $this->getTableLocator()->get('ValidateUsers', [
  2947. 'className' => ValidateUsersTable::class,
  2948. ]);
  2949. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2950. $result = $this->Form->control('ValidateUser.id');
  2951. $expected = [
  2952. 'input' => ['name', 'type' => 'hidden', 'id'],
  2953. ];
  2954. $this->assertHtml($expected, $result);
  2955. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  2956. $expected = [
  2957. 'input' => ['name', 'type' => 'hidden', 'id'],
  2958. ];
  2959. $this->assertHtml($expected, $result);
  2960. }
  2961. /**
  2962. * testControlDatetime method
  2963. *
  2964. * Test form->control() with datetime.
  2965. */
  2966. public function testControlDatetime(): void
  2967. {
  2968. $result = $this->Form->control('prueba', [
  2969. 'type' => 'datetime',
  2970. 'value' => new DateTime('2019-09-27 02:52:43'),
  2971. ]);
  2972. $expected = [
  2973. 'div' => ['class' => 'input datetime'],
  2974. 'label' => ['for' => 'prueba'],
  2975. 'Prueba',
  2976. '/label',
  2977. 'input' => [
  2978. 'name' => 'prueba',
  2979. 'id' => 'prueba',
  2980. 'type' => 'datetime-local',
  2981. 'value' => '2019-09-27T02:52:43',
  2982. 'step' => '1',
  2983. ],
  2984. '/div',
  2985. ];
  2986. $this->assertHtml($expected, $result);
  2987. }
  2988. /**
  2989. * testControlDatetimeIdPrefix method
  2990. *
  2991. * Test form->control() with datetime with id prefix.
  2992. */
  2993. public function testControlDatetimeIdPrefix(): void
  2994. {
  2995. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2996. $result = $this->Form->control('prueba', [
  2997. 'type' => 'datetime',
  2998. ]);
  2999. $expected = [
  3000. 'div' => ['class' => 'input datetime'],
  3001. 'label' => ['for' => 'prefix-prueba'],
  3002. 'Prueba',
  3003. '/label',
  3004. 'input' => [
  3005. 'name' => 'prueba',
  3006. 'id' => 'prefix-prueba',
  3007. 'type' => 'datetime-local',
  3008. 'value' => '',
  3009. 'step' => '1',
  3010. ],
  3011. '/div',
  3012. ];
  3013. $this->assertHtml($expected, $result);
  3014. }
  3015. /**
  3016. * testControlDatetimeStep method
  3017. *
  3018. * Test form->control() with datetime with custom step size.
  3019. */
  3020. public function testControlDatetimeStep(): void
  3021. {
  3022. $result = $this->Form->control('prueba', [
  3023. 'type' => 'datetime',
  3024. 'value' => new DateTime('2019-09-27 02:52:43'),
  3025. 'step' => '0.5',
  3026. ]);
  3027. $expected = [
  3028. 'div' => ['class' => 'input datetime'],
  3029. 'label' => ['for' => 'prueba'],
  3030. 'Prueba',
  3031. '/label',
  3032. 'input' => [
  3033. 'name' => 'prueba',
  3034. 'id' => 'prueba',
  3035. 'type' => 'datetime-local',
  3036. 'value' => '2019-09-27T02:52:43.000',
  3037. 'step' => '0.5',
  3038. ],
  3039. '/div',
  3040. ];
  3041. $this->assertHtml($expected, $result);
  3042. }
  3043. /**
  3044. * testControlCheckboxWithDisabledElements method
  3045. *
  3046. * Test generating checkboxes with disabled elements.
  3047. */
  3048. public function testControlCheckboxWithDisabledElements(): void
  3049. {
  3050. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3051. $result = $this->Form->control('Contact.multiple', [
  3052. 'multiple' => 'checkbox',
  3053. 'disabled' => 'disabled',
  3054. 'options' => $options,
  3055. ]);
  3056. $expected = [
  3057. ['div' => ['class' => 'input select']],
  3058. ['label' => ['for' => 'contact-multiple']],
  3059. 'Multiple',
  3060. '/label',
  3061. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3062. ['div' => ['class' => 'checkbox']],
  3063. ['label' => ['for' => 'contact-multiple-1']],
  3064. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3065. 'One',
  3066. '/label',
  3067. '/div',
  3068. ['div' => ['class' => 'checkbox']],
  3069. ['label' => ['for' => 'contact-multiple-2']],
  3070. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3071. 'Two',
  3072. '/label',
  3073. '/div',
  3074. ['div' => ['class' => 'checkbox']],
  3075. ['label' => ['for' => 'contact-multiple-3']],
  3076. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3077. 'Three',
  3078. '/label',
  3079. '/div',
  3080. '/div',
  3081. ];
  3082. $this->assertHtml($expected, $result);
  3083. // make sure 50 does only disable 50, and not 50f5c0cf
  3084. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3085. $disabled = [50];
  3086. $expected = [
  3087. ['div' => ['class' => 'input select']],
  3088. ['label' => ['for' => 'contact-multiple']],
  3089. 'Multiple',
  3090. '/label',
  3091. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3092. ['div' => ['class' => 'checkbox']],
  3093. ['label' => ['for' => 'contact-multiple-50']],
  3094. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3095. 'Fifty',
  3096. '/label',
  3097. '/div',
  3098. ['div' => ['class' => 'checkbox']],
  3099. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3100. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3101. 'Stringy',
  3102. '/label',
  3103. '/div',
  3104. '/div',
  3105. ];
  3106. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3107. $this->assertHtml($expected, $result);
  3108. }
  3109. /**
  3110. * testControlWithLeadingInteger method
  3111. *
  3112. * Test input name with leading integer, ensure attributes are generated correctly.
  3113. */
  3114. public function testControlWithLeadingInteger(): void
  3115. {
  3116. $result = $this->Form->text('0.Node.title');
  3117. $expected = [
  3118. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3119. ];
  3120. $this->assertHtml($expected, $result);
  3121. }
  3122. /**
  3123. * testControlSelectType method
  3124. *
  3125. * Test form->control() with select type inputs.
  3126. */
  3127. public function testControlSelectType(): void
  3128. {
  3129. $result = $this->Form->control(
  3130. 'email',
  3131. [
  3132. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3133. );
  3134. $expected = [
  3135. 'div' => ['class' => 'input select'],
  3136. 'label' => ['for' => 'email'],
  3137. 'Email',
  3138. '/label',
  3139. ['select' => ['name' => 'email', 'id' => 'email']],
  3140. ['option' => ['value' => '']],
  3141. '/option',
  3142. ['option' => ['value' => 'è']],
  3143. 'Firést',
  3144. '/option',
  3145. ['option' => ['value' => 'é']],
  3146. 'Secoènd',
  3147. '/option',
  3148. '/select',
  3149. '/div',
  3150. ];
  3151. $this->assertHtml($expected, $result);
  3152. $result = $this->Form->control(
  3153. 'email',
  3154. [
  3155. 'options' => ['First', 'Second'], 'empty' => true]
  3156. );
  3157. $expected = [
  3158. 'div' => ['class' => 'input select'],
  3159. 'label' => ['for' => 'email'],
  3160. 'Email',
  3161. '/label',
  3162. ['select' => ['name' => 'email', 'id' => 'email']],
  3163. ['option' => ['value' => '']],
  3164. '/option',
  3165. ['option' => ['value' => '0']],
  3166. 'First',
  3167. '/option',
  3168. ['option' => ['value' => '1']],
  3169. 'Second',
  3170. '/option',
  3171. '/select',
  3172. '/div',
  3173. ];
  3174. $this->assertHtml($expected, $result);
  3175. $result = $this->Form->control('email', [
  3176. 'type' => 'select',
  3177. 'options' => new ArrayObject(['First', 'Second']),
  3178. 'empty' => true,
  3179. ]);
  3180. $this->assertHtml($expected, $result);
  3181. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3182. $this->View->setRequest(
  3183. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3184. );
  3185. $this->Form->create();
  3186. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3187. $expected = [
  3188. 'div' => ['class' => 'input select'],
  3189. 'label' => ['for' => 'model-user-id'],
  3190. 'User',
  3191. '/label',
  3192. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3193. ['option' => ['value' => '']],
  3194. '/option',
  3195. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3196. 'good',
  3197. '/option',
  3198. ['option' => ['value' => 'other']],
  3199. 'bad',
  3200. '/option',
  3201. '/select',
  3202. '/div',
  3203. ];
  3204. $this->assertHtml($expected, $result);
  3205. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3206. $this->View->setRequest(
  3207. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3208. );
  3209. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3210. $expected = [
  3211. 'div' => ['class' => 'input select'],
  3212. 'label' => ['for' => 'thing-user-id'],
  3213. 'User',
  3214. '/label',
  3215. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3216. ['option' => ['value' => '']],
  3217. 'Some Empty',
  3218. '/option',
  3219. ['option' => ['value' => 'value']],
  3220. 'good',
  3221. '/option',
  3222. ['option' => ['value' => 'other']],
  3223. 'bad',
  3224. '/option',
  3225. '/select',
  3226. '/div',
  3227. ];
  3228. $this->assertHtml($expected, $result);
  3229. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3230. $this->View->setRequest(
  3231. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3232. );
  3233. $this->Form->create();
  3234. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3235. $expected = [
  3236. 'div' => ['class' => 'input select'],
  3237. 'label' => ['for' => 'thing-user-id'],
  3238. 'User',
  3239. '/label',
  3240. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3241. ['option' => ['value' => '']],
  3242. 'Some Empty',
  3243. '/option',
  3244. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3245. 'good',
  3246. '/option',
  3247. ['option' => ['value' => 'other']],
  3248. 'bad',
  3249. '/option',
  3250. '/select',
  3251. '/div',
  3252. ];
  3253. $this->assertHtml($expected, $result);
  3254. $result = $this->Form->control('Publisher.id', [
  3255. 'label' => 'Publisher',
  3256. 'type' => 'select',
  3257. 'multiple' => 'checkbox',
  3258. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3259. ]);
  3260. $expected = [
  3261. ['div' => ['class' => 'input select']],
  3262. ['label' => ['for' => 'publisher-id']],
  3263. 'Publisher',
  3264. '/label',
  3265. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3266. ['div' => ['class' => 'checkbox']],
  3267. ['label' => ['for' => 'publisher-id-value-1']],
  3268. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3269. 'Label 1',
  3270. '/label',
  3271. '/div',
  3272. ['div' => ['class' => 'checkbox']],
  3273. ['label' => ['for' => 'publisher-id-value-2']],
  3274. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3275. 'Label 2',
  3276. '/label',
  3277. '/div',
  3278. '/div',
  3279. ];
  3280. $this->assertHtml($expected, $result);
  3281. }
  3282. /**
  3283. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3284. *
  3285. * Test that control() and a non standard primary key makes a hidden input by default.
  3286. */
  3287. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3288. {
  3289. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3290. $this->Form->create($this->article);
  3291. $result = $this->Form->control('title');
  3292. $expected = [
  3293. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3294. ];
  3295. $this->assertHtml($expected, $result);
  3296. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3297. $this->Form->create($this->article);
  3298. $result = $this->Form->control('title');
  3299. $expected = [
  3300. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3301. ];
  3302. $this->assertHtml($expected, $result);
  3303. $result = $this->Form->control('body');
  3304. $expected = [
  3305. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3306. ];
  3307. $this->assertHtml($expected, $result);
  3308. }
  3309. /**
  3310. * testControlOverridingMagicSelectType method
  3311. *
  3312. * Test that overriding the magic select type widget is possible.
  3313. */
  3314. public function testControlOverridingMagicSelectType(): void
  3315. {
  3316. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3317. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3318. $expected = [
  3319. 'div' => ['class' => 'input text'],
  3320. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3321. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3322. '/div',
  3323. ];
  3324. $this->assertHtml($expected, $result);
  3325. //Check that magic types still work for plural/singular vars
  3326. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3327. $result = $this->Form->control('Model.type');
  3328. $expected = [
  3329. 'div' => ['class' => 'input select'],
  3330. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3331. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3332. ['option' => ['value' => 'value']], 'good', '/option',
  3333. ['option' => ['value' => 'other']], 'bad', '/option',
  3334. '/select',
  3335. '/div',
  3336. ];
  3337. $this->assertHtml($expected, $result);
  3338. }
  3339. /**
  3340. * testControlMagicTypeDoesNotOverride method
  3341. *
  3342. * Test that inferred types do not override developer input.
  3343. */
  3344. public function testControlMagicTypeDoesNotOverride(): void
  3345. {
  3346. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3347. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3348. $expected = [
  3349. 'div' => ['class' => 'input checkbox'],
  3350. ['input' => [
  3351. 'type' => 'hidden',
  3352. 'name' => 'Model[user]',
  3353. 'value' => 0,
  3354. ]],
  3355. 'label' => ['for' => 'model-user'],
  3356. ['input' => [
  3357. 'name' => 'Model[user]',
  3358. 'type' => 'checkbox',
  3359. 'id' => 'model-user',
  3360. 'value' => 1,
  3361. ]],
  3362. 'User',
  3363. '/label',
  3364. '/div',
  3365. ];
  3366. $this->assertHtml($expected, $result);
  3367. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3368. $options = [
  3369. 1 => 'blue',
  3370. 2 => 'red',
  3371. ];
  3372. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3373. $expected = [
  3374. 'div' => ['class' => 'input select'],
  3375. 'label' => ['for' => 'tags-ids'],
  3376. 'Tags',
  3377. '/label',
  3378. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3379. ['div' => ['class' => 'checkbox']],
  3380. ['label' => ['for' => 'tags-ids-1']],
  3381. ['input' => [
  3382. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3383. 'value' => '1', 'name' => 'tags[_ids][]',
  3384. ]],
  3385. 'blue',
  3386. '/label',
  3387. '/div',
  3388. ['div' => ['class' => 'checkbox']],
  3389. ['label' => ['for' => 'tags-ids-2']],
  3390. ['input' => [
  3391. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3392. 'value' => '2', 'name' => 'tags[_ids][]',
  3393. ]],
  3394. 'red',
  3395. '/label',
  3396. '/div',
  3397. '/div',
  3398. ];
  3399. $this->assertHtml($expected, $result);
  3400. }
  3401. /**
  3402. * testControlMagicSelectForTypeNumber method
  3403. *
  3404. * Test that magic control() selects are created for type=number.
  3405. */
  3406. public function testControlMagicSelectForTypeNumber(): void
  3407. {
  3408. $this->getTableLocator()->get('ValidateUsers', [
  3409. 'className' => ValidateUsersTable::class,
  3410. ]);
  3411. $entity = new Entity(['balance' => 1]);
  3412. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3413. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3414. $result = $this->Form->control('balance');
  3415. $expected = [
  3416. 'div' => ['class' => 'input select'],
  3417. 'label' => ['for' => 'balance'],
  3418. 'Balance',
  3419. '/label',
  3420. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3421. ['option' => ['value' => '0']],
  3422. 'nothing',
  3423. '/option',
  3424. ['option' => ['value' => '1', 'selected' => 'selected']],
  3425. 'some',
  3426. '/option',
  3427. ['option' => ['value' => '100']],
  3428. 'a lot',
  3429. '/option',
  3430. '/select',
  3431. '/div',
  3432. ];
  3433. $this->assertHtml($expected, $result);
  3434. }
  3435. /**
  3436. * testInvalidControlTypeOption method
  3437. *
  3438. * Test invalid 'input' type option to control() function.
  3439. */
  3440. public function testInvalidControlTypeOption(): void
  3441. {
  3442. $this->expectException(InvalidArgumentException::class);
  3443. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3444. $this->Form->control('text', ['type' => 'input']);
  3445. }
  3446. /**
  3447. * testControlMagicSelectChangeToRadio method
  3448. *
  3449. * Test that magic control() selects can easily be converted into radio types without error.
  3450. */
  3451. public function testControlMagicSelectChangeToRadio(): void
  3452. {
  3453. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3454. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3455. $this->assertStringContainsString('input type="radio"', $result);
  3456. }
  3457. /**
  3458. * testFormControlSubmit method
  3459. *
  3460. * Test correct results for form::control() and type submit.
  3461. */
  3462. public function testFormControlSubmit(): void
  3463. {
  3464. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3465. $expected = [
  3466. 'div' => ['class' => 'submit'],
  3467. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3468. '/div',
  3469. ];
  3470. $this->assertHtml($expected, $result);
  3471. }
  3472. /**
  3473. * testFormControls method
  3474. *
  3475. * Test correct results from Form::controls().
  3476. */
  3477. public function testFormControlsLegendFieldset(): void
  3478. {
  3479. $this->Form->create($this->article);
  3480. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3481. $expected = [
  3482. '<fieldset',
  3483. '<legend',
  3484. 'The Legend',
  3485. '/legend',
  3486. '*/fieldset',
  3487. ];
  3488. $this->assertHtml($expected, $result);
  3489. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3490. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3491. $this->assertStringContainsString('<fieldset>', $result);
  3492. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3493. $this->assertStringNotContainsString('<legend>', $result);
  3494. $this->assertStringNotContainsString('<fieldset>', $result);
  3495. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3496. $this->assertStringNotContainsString('<legend>', $result);
  3497. $this->assertStringNotContainsString('<fieldset>', $result);
  3498. $this->Form->create($this->article);
  3499. $this->View->setRequest($this->View->getRequest()
  3500. ->withParam('prefix', 'admin')
  3501. ->withParam('action', 'admin_edit')
  3502. ->withParam('controller', 'articles'));
  3503. $result = $this->Form->allControls();
  3504. $expected = [
  3505. '<fieldset',
  3506. '<legend',
  3507. 'New Article',
  3508. '/legend',
  3509. '*/fieldset',
  3510. ];
  3511. $this->assertHtml($expected, $result);
  3512. $this->Form->create($this->article);
  3513. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3514. $expected = [
  3515. '<fieldset',
  3516. '<legend',
  3517. 'The Legend',
  3518. '/legend',
  3519. '*/fieldset',
  3520. ];
  3521. $this->assertHtml($expected, $result);
  3522. $this->Form->create($this->article);
  3523. $result = $this->Form->allControls([], [
  3524. 'fieldset' => [
  3525. 'class' => 'some-class some-other-class',
  3526. 'disabled' => true,
  3527. 'data-param' => 'a-param',
  3528. ],
  3529. 'legend' => 'The Legend',
  3530. ]);
  3531. $expected = [
  3532. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3533. '<legend',
  3534. 'The Legend',
  3535. '/legend',
  3536. '*/fieldset',
  3537. ];
  3538. $this->assertHtml($expected, $result);
  3539. }
  3540. /**
  3541. * testFormControls method
  3542. *
  3543. * Test the controls() method.
  3544. */
  3545. public function testFormControls(): void
  3546. {
  3547. $this->Form->create($this->article);
  3548. $result = $this->Form->allControls();
  3549. $expected = [
  3550. '<fieldset',
  3551. '<legend', 'New Article', '/legend',
  3552. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3553. ['div' => ['class' => 'input select required']],
  3554. '*/div',
  3555. ['div' => ['class' => 'input text required']],
  3556. '*/div',
  3557. ['div' => ['class' => 'input text']],
  3558. '*/div',
  3559. ['div' => ['class' => 'input text']],
  3560. '*/div',
  3561. '/fieldset',
  3562. ];
  3563. $this->assertHtml($expected, $result);
  3564. $result = $this->Form->allControls([
  3565. 'published' => ['type' => 'boolean'],
  3566. ]);
  3567. $expected = [
  3568. '<fieldset',
  3569. '<legend', 'New Article', '/legend',
  3570. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3571. ['div' => ['class' => 'input select required']],
  3572. '*/div',
  3573. ['div' => ['class' => 'input text required']],
  3574. '*/div',
  3575. ['div' => ['class' => 'input text']],
  3576. '*/div',
  3577. ['div' => ['class' => 'input boolean']],
  3578. '*/div',
  3579. '/fieldset',
  3580. ];
  3581. $this->assertHtml($expected, $result);
  3582. $this->Form->create($this->article);
  3583. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3584. $expected = [
  3585. 'fieldset' => [],
  3586. 'legend' => [],
  3587. 'Hello',
  3588. '/legend',
  3589. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3590. ['div' => ['class' => 'input select required']],
  3591. '*/div',
  3592. ['div' => ['class' => 'input text required']],
  3593. '*/div',
  3594. ['div' => ['class' => 'input text']],
  3595. '*/div',
  3596. ['div' => ['class' => 'input text']],
  3597. '*/div',
  3598. '/fieldset',
  3599. ];
  3600. $this->assertHtml($expected, $result);
  3601. $this->Form->create();
  3602. $expected = [
  3603. 'fieldset' => [],
  3604. ['div' => ['class' => 'input text']],
  3605. 'label' => ['for' => 'foo'],
  3606. 'Foo',
  3607. '/label',
  3608. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3609. '*/div',
  3610. '/fieldset',
  3611. ];
  3612. $result = $this->Form->allControls(
  3613. ['foo' => ['type' => 'text']],
  3614. ['legend' => false]
  3615. );
  3616. $this->assertHtml($expected, $result);
  3617. }
  3618. /**
  3619. * testFormControlsBlacklist method
  3620. */
  3621. public function testFormControlsBlacklist(): void
  3622. {
  3623. $this->Form->create($this->article);
  3624. $result = $this->Form->allControls([
  3625. 'id' => false,
  3626. ]);
  3627. $expected = [
  3628. '<fieldset',
  3629. '<legend', 'New Article', '/legend',
  3630. ['div' => ['class' => 'input select required']],
  3631. '*/div',
  3632. ['div' => ['class' => 'input text required']],
  3633. '*/div',
  3634. ['div' => ['class' => 'input text']],
  3635. '*/div',
  3636. ['div' => ['class' => 'input text']],
  3637. '*/div',
  3638. '/fieldset',
  3639. ];
  3640. $this->assertHtml($expected, $result);
  3641. $this->Form->create($this->article);
  3642. $result = $this->Form->allControls([
  3643. 'id' => [],
  3644. ]);
  3645. $expected = [
  3646. '<fieldset',
  3647. '<legend', 'New Article', '/legend',
  3648. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3649. ['div' => ['class' => 'input select required']],
  3650. '*/div',
  3651. ['div' => ['class' => 'input text required']],
  3652. '*/div',
  3653. ['div' => ['class' => 'input text']],
  3654. '*/div',
  3655. ['div' => ['class' => 'input text']],
  3656. '*/div',
  3657. '/fieldset',
  3658. ];
  3659. $this->assertHtml($expected, $result, true);
  3660. }
  3661. /**
  3662. * testSelectAsCheckbox method
  3663. *
  3664. * Test multi-select widget with checkbox formatting.
  3665. */
  3666. public function testSelectAsCheckbox(): void
  3667. {
  3668. $result = $this->Form->select(
  3669. 'Model.multi_field',
  3670. ['first', 'second', 'third'],
  3671. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3672. );
  3673. $expected = [
  3674. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3675. ['div' => ['class' => 'checkbox']],
  3676. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3677. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3678. 'first',
  3679. '/label',
  3680. '/div',
  3681. ['div' => ['class' => 'checkbox']],
  3682. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3683. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3684. 'second',
  3685. '/label',
  3686. '/div',
  3687. ['div' => ['class' => 'checkbox']],
  3688. ['label' => ['for' => 'model-multi-field-2']],
  3689. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3690. 'third',
  3691. '/label',
  3692. '/div',
  3693. ];
  3694. $this->assertHtml($expected, $result);
  3695. $result = $this->Form->select(
  3696. 'Model.multi_field',
  3697. ['1/2' => 'half'],
  3698. ['multiple' => 'checkbox']
  3699. );
  3700. $expected = [
  3701. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3702. ['div' => ['class' => 'checkbox']],
  3703. ['label' => ['for' => 'model-multi-field-1-2']],
  3704. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3705. 'half',
  3706. '/label',
  3707. '/div',
  3708. ];
  3709. $this->assertHtml($expected, $result);
  3710. }
  3711. /**
  3712. * testLabel method
  3713. *
  3714. * Test label generation.
  3715. */
  3716. public function testLabel(): void
  3717. {
  3718. $result = $this->Form->label('Person.name');
  3719. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3720. $this->assertHtml($expected, $result);
  3721. $result = $this->Form->label('Person.name');
  3722. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3723. $this->assertHtml($expected, $result);
  3724. $result = $this->Form->label('Person.first_name');
  3725. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3726. $this->assertHtml($expected, $result);
  3727. $result = $this->Form->label('Person.first_name', 'Your first name');
  3728. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3729. $this->assertHtml($expected, $result);
  3730. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3731. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3732. $this->assertHtml($expected, $result);
  3733. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3734. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3735. $this->assertHtml($expected, $result);
  3736. $result = $this->Form->label('Person.first_name', '');
  3737. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3738. $this->assertHtml($expected, $result);
  3739. $result = $this->Form->label('Person.2.name', '');
  3740. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3741. $this->assertHtml($expected, $result);
  3742. }
  3743. /**
  3744. * testLabelContainControl method
  3745. *
  3746. * Test that label() can accept an input with the correct template vars.
  3747. */
  3748. public function testLabelContainControl(): void
  3749. {
  3750. $this->Form->setTemplates([
  3751. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3752. ]);
  3753. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3754. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3755. ]);
  3756. $expected = [
  3757. 'label' => ['for' => 'person-accept-terms'],
  3758. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3759. 'Accept',
  3760. '/label',
  3761. ];
  3762. $this->assertHtml($expected, $result);
  3763. }
  3764. /**
  3765. * testTextbox method
  3766. *
  3767. * Test textbox element generation.
  3768. */
  3769. public function testTextbox(): void
  3770. {
  3771. $result = $this->Form->text('Model.field');
  3772. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3773. $this->assertHtml($expected, $result);
  3774. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3775. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3776. $this->assertHtml($expected, $result);
  3777. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3778. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3779. $this->assertHtml($expected, $result);
  3780. }
  3781. /**
  3782. * testTextBoxDataAndError method
  3783. *
  3784. * Test that text() hooks up with request data and error fields.
  3785. */
  3786. public function testTextBoxDataAndError(): void
  3787. {
  3788. $this->article['errors'] = [
  3789. 'Contact' => ['text' => 'wrong'],
  3790. ];
  3791. $this->View->setRequest($this->View->getRequest()
  3792. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3793. ->withData('Contact.text', 'test'));
  3794. $this->Form->create($this->article);
  3795. $result = $this->Form->text('Model.text');
  3796. $expected = [
  3797. 'input' => [
  3798. 'type' => 'text',
  3799. 'name' => 'Model[text]',
  3800. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3801. ],
  3802. ];
  3803. $this->assertHtml($expected, $result);
  3804. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3805. $expected = [
  3806. 'input' => [
  3807. 'type' => 'text',
  3808. 'name' => 'Contact[text]',
  3809. 'value' => 'test',
  3810. 'id' => 'theID',
  3811. 'class' => 'form-error',
  3812. ],
  3813. ];
  3814. $this->assertHtml($expected, $result);
  3815. }
  3816. /**
  3817. * testDefaultValue method
  3818. *
  3819. * Test default value setting.
  3820. */
  3821. public function testTextDefaultValue(): void
  3822. {
  3823. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3824. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3825. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3826. $this->assertHtml($expected, $result);
  3827. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3828. $this->Form->create();
  3829. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3830. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3831. $this->assertHtml($expected, $result);
  3832. $Articles = $this->getTableLocator()->get('Articles');
  3833. $title = $Articles->getSchema()->getColumn('title');
  3834. $Articles->getSchema()->addColumn(
  3835. 'title',
  3836. ['default' => 'default title', 'length' => 255] + $title
  3837. );
  3838. $entity = $Articles->newEmptyEntity();
  3839. $this->Form->create($entity);
  3840. // Get default value from schema
  3841. $result = $this->Form->text('title');
  3842. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3843. $this->assertHtml($expected, $result);
  3844. // Don't get value from schema
  3845. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3846. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3847. $this->assertHtml($expected, $result);
  3848. // Custom default value overrides default value from schema
  3849. $result = $this->Form->text('title', ['default' => 'override default']);
  3850. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3851. $this->assertHtml($expected, $result);
  3852. // Default value from schema is used only for new entities.
  3853. $entity->setNew(false);
  3854. $result = $this->Form->text('title');
  3855. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3856. $this->assertHtml($expected, $result);
  3857. }
  3858. /**
  3859. * testError method
  3860. *
  3861. * Test field error generation.
  3862. */
  3863. public function testError(): void
  3864. {
  3865. $this->article['errors'] = [
  3866. 'Article' => ['field' => 'email'],
  3867. ];
  3868. $this->Form->create($this->article);
  3869. $result = $this->Form->error('Article.field');
  3870. $expected = [
  3871. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3872. 'email',
  3873. '/div',
  3874. ];
  3875. $this->assertHtml($expected, $result);
  3876. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3877. $expected = [
  3878. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3879. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3880. '/div',
  3881. ];
  3882. $this->assertHtml($expected, $result);
  3883. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3884. $expected = [
  3885. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3886. '<strong', 'Badness!', '/strong',
  3887. '/div',
  3888. ];
  3889. $this->assertHtml($expected, $result);
  3890. }
  3891. /**
  3892. * testErrorRuleName method
  3893. *
  3894. * Test error translation can use rule names for translating.
  3895. */
  3896. public function testErrorRuleName(): void
  3897. {
  3898. $this->article['errors'] = [
  3899. 'Article' => [
  3900. 'field' => ['email' => 'Your email was not good'],
  3901. ],
  3902. ];
  3903. $this->Form->create($this->article);
  3904. $result = $this->Form->error('Article.field');
  3905. $expected = [
  3906. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3907. 'Your email was not good',
  3908. '/div',
  3909. ];
  3910. $this->assertHtml($expected, $result);
  3911. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3912. $expected = [
  3913. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3914. 'Email in use',
  3915. '/div',
  3916. ];
  3917. $this->assertHtml($expected, $result);
  3918. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3919. $expected = [
  3920. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3921. 'Email in use',
  3922. '/div',
  3923. ];
  3924. $this->assertHtml($expected, $result);
  3925. $result = $this->Form->error('Article.field', [
  3926. 'email' => 'Key is preferred',
  3927. 'Your email was not good' => 'Email in use',
  3928. ]);
  3929. $expected = [
  3930. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3931. 'Key is preferred',
  3932. '/div',
  3933. ];
  3934. $this->assertHtml($expected, $result);
  3935. }
  3936. /**
  3937. * testErrorMessages method
  3938. *
  3939. * Test error with nested lists.
  3940. */
  3941. public function testErrorMessages(): void
  3942. {
  3943. $this->article['errors'] = [
  3944. 'Article' => ['field' => 'email'],
  3945. ];
  3946. $this->Form->create($this->article);
  3947. $result = $this->Form->error('Article.field', [
  3948. 'email' => 'No good!',
  3949. ]);
  3950. $expected = [
  3951. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  3952. 'No good!',
  3953. '/div',
  3954. ];
  3955. $this->assertHtml($expected, $result);
  3956. }
  3957. /**
  3958. * testErrorMultipleMessages method
  3959. *
  3960. * Test error() with multiple messages.
  3961. */
  3962. public function testErrorMultipleMessages(): void
  3963. {
  3964. $this->article['errors'] = [
  3965. 'field' => ['notBlank', 'email', 'Something else'],
  3966. ];
  3967. $this->Form->create($this->article);
  3968. $result = $this->Form->error('field', [
  3969. 'notBlank' => 'Cannot be empty',
  3970. 'email' => 'No good!',
  3971. ]);
  3972. $expected = [
  3973. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  3974. 'ul' => [],
  3975. '<li', 'Cannot be empty', '/li',
  3976. '<li', 'No good!', '/li',
  3977. '<li', 'Something else', '/li',
  3978. '/ul',
  3979. '/div',
  3980. ];
  3981. $this->assertHtml($expected, $result);
  3982. }
  3983. /**
  3984. * testPassword method
  3985. *
  3986. * Test password element generation.
  3987. */
  3988. public function testPassword(): void
  3989. {
  3990. $this->article['errors'] = [
  3991. 'Contact' => [
  3992. 'passwd' => 1,
  3993. ],
  3994. ];
  3995. $this->Form->create($this->article);
  3996. $result = $this->Form->password('Contact.field');
  3997. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  3998. $this->assertHtml($expected, $result);
  3999. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4000. $this->Form->create($this->article);
  4001. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4002. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4003. $this->assertHtml($expected, $result);
  4004. }
  4005. /**
  4006. * testRadio method
  4007. *
  4008. * Test radio element set generation.
  4009. */
  4010. public function testRadio(): void
  4011. {
  4012. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4013. $expected = [
  4014. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4015. ['label' => ['for' => 'model-field-0']],
  4016. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4017. 'option A',
  4018. '/label',
  4019. ['label' => ['for' => 'model-field-1']],
  4020. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4021. 'option B',
  4022. '/label',
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4026. $this->assertHtml($expected, $result);
  4027. $result = $this->Form->radio(
  4028. 'Employee.vegetarian',
  4029. ['yes' => 'Yes', 'no' => 'No'],
  4030. ['form' => 'my-form', 'id' => 'id-veg']
  4031. );
  4032. $expected = [
  4033. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4034. ['label' => ['for' => 'id-veg-yes']],
  4035. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4036. 'Yes',
  4037. '/label',
  4038. ['label' => ['for' => 'id-veg-no']],
  4039. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4040. 'No',
  4041. '/label',
  4042. ];
  4043. $this->assertHtml($expected, $result);
  4044. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4045. $expected = [
  4046. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4047. ['label' => ['for' => 'model-custom-0']],
  4048. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4049. 'option A',
  4050. '/label',
  4051. ['label' => ['for' => 'model-custom-1']],
  4052. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4053. 'option B',
  4054. '/label',
  4055. ];
  4056. $this->assertHtml($expected, $result);
  4057. $result = $this->Form->radio(
  4058. 'Employee.gender',
  4059. [
  4060. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4061. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4062. ]
  4063. );
  4064. $expected = [
  4065. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4066. ['label' => ['for' => 'employee-gender-male']],
  4067. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4068. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4069. 'Male',
  4070. '/label',
  4071. ['label' => ['for' => 'employee-gender-female']],
  4072. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4073. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4074. 'Female',
  4075. '/label',
  4076. ];
  4077. $this->assertHtml($expected, $result);
  4078. }
  4079. /**
  4080. * Test radio with complex options and empty disabled data.
  4081. */
  4082. public function testRadioComplexDisabled(): void
  4083. {
  4084. $options = [
  4085. ['value' => 'r', 'text' => 'red'],
  4086. ['value' => 'b', 'text' => 'blue'],
  4087. ];
  4088. $attrs = ['disabled' => []];
  4089. $result = $this->Form->radio('Model.field', $options, $attrs);
  4090. $expected = [
  4091. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4092. ['label' => ['for' => 'model-field-r']],
  4093. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4094. 'red',
  4095. '/label',
  4096. ['label' => ['for' => 'model-field-b']],
  4097. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4098. 'blue',
  4099. '/label',
  4100. ];
  4101. $this->assertHtml($expected, $result);
  4102. $attrs = ['disabled' => ['r']];
  4103. $result = $this->Form->radio('Model.field', $options, $attrs);
  4104. $this->assertStringContainsString('disabled="disabled"', $result);
  4105. }
  4106. /**
  4107. * testRadioDefaultValue method
  4108. *
  4109. * Test default value setting on radio() method.
  4110. */
  4111. public function testRadioDefaultValue(): void
  4112. {
  4113. $Articles = $this->getTableLocator()->get('Articles');
  4114. $title = $Articles->getSchema()->getColumn('title');
  4115. $Articles->getSchema()->addColumn(
  4116. 'title',
  4117. ['default' => '1'] + $title
  4118. );
  4119. $this->Form->create($Articles->newEmptyEntity());
  4120. $result = $this->Form->radio('title', ['option A', 'option B']);
  4121. $expected = [
  4122. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4123. ['label' => ['for' => 'title-0']],
  4124. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4125. 'option A',
  4126. '/label',
  4127. ['label' => ['for' => 'title-1']],
  4128. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4129. 'option B',
  4130. '/label',
  4131. ];
  4132. $this->assertHtml($expected, $result);
  4133. }
  4134. /**
  4135. * Test setting a hiddenField value on radio buttons.
  4136. */
  4137. public function testRadioHiddenFieldValue(): void
  4138. {
  4139. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4140. $expected = [
  4141. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4142. 'label' => ['for' => 'title-0'],
  4143. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4144. 'option A',
  4145. '/label',
  4146. ];
  4147. $this->assertHtml($expected, $result);
  4148. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4149. $expected = [
  4150. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4151. 'label' => ['for' => 'title-0'],
  4152. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4153. 'option A',
  4154. '/label',
  4155. ];
  4156. $this->assertHtml($expected, $result);
  4157. }
  4158. /**
  4159. * testControlRadio method
  4160. *
  4161. * Test that input works with radio types.
  4162. */
  4163. public function testControlRadio(): void
  4164. {
  4165. $result = $this->Form->control('test', [
  4166. 'type' => 'radio',
  4167. 'options' => ['A', 'B'],
  4168. ]);
  4169. $expected = [
  4170. ['div' => ['class' => 'input radio']],
  4171. '<label',
  4172. 'Test',
  4173. '/label',
  4174. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4175. ['label' => ['for' => 'test-0']],
  4176. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4177. 'A',
  4178. '/label',
  4179. ['label' => ['for' => 'test-1']],
  4180. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4181. 'B',
  4182. '/label',
  4183. '/div',
  4184. ];
  4185. $this->assertHtml($expected, $result);
  4186. $result = $this->Form->control('test', [
  4187. 'type' => 'radio',
  4188. 'options' => ['A', 'B'],
  4189. 'value' => '0',
  4190. ]);
  4191. $expected = [
  4192. ['div' => ['class' => 'input radio']],
  4193. '<label',
  4194. 'Test',
  4195. '/label',
  4196. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4197. ['label' => ['for' => 'test-0']],
  4198. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4199. 'A',
  4200. '/label',
  4201. ['label' => ['for' => 'test-1']],
  4202. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4203. 'B',
  4204. '/label',
  4205. '/div',
  4206. ];
  4207. $this->assertHtml($expected, $result);
  4208. $result = $this->Form->control('test', [
  4209. 'type' => 'radio',
  4210. 'options' => ['A', 'B'],
  4211. 'label' => false,
  4212. ]);
  4213. $expected = [
  4214. ['div' => ['class' => 'input radio']],
  4215. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4216. ['label' => ['for' => 'test-0']],
  4217. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4218. 'A',
  4219. '/label',
  4220. ['label' => ['for' => 'test-1']],
  4221. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4222. 'B',
  4223. '/label',
  4224. '/div',
  4225. ];
  4226. $this->assertHtml($expected, $result);
  4227. $result = $this->Form->control('accept', [
  4228. 'type' => 'radio',
  4229. 'options' => [
  4230. 1 => 'positive',
  4231. -1 => 'negative',
  4232. ],
  4233. 'label' => false,
  4234. ]);
  4235. $expected = [
  4236. ['div' => ['class' => 'input radio']],
  4237. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4238. ['label' => ['for' => 'accept-1']],
  4239. ['input' => [
  4240. 'type' => 'radio',
  4241. 'name' => 'accept',
  4242. 'value' => '1',
  4243. 'id' => 'accept-1',
  4244. ]],
  4245. 'positive',
  4246. '/label',
  4247. ['label' => ['for' => 'accept--1']],
  4248. ['input' => [
  4249. 'type' => 'radio',
  4250. 'name' => 'accept',
  4251. 'value' => '-1',
  4252. 'id' => 'accept--1',
  4253. ]],
  4254. 'negative',
  4255. '/label',
  4256. '/div',
  4257. ];
  4258. $this->assertHtml($expected, $result);
  4259. }
  4260. /**
  4261. * testRadioNoLabel method
  4262. *
  4263. * Test that radio() works with label = false.
  4264. */
  4265. public function testRadioNoLabel(): void
  4266. {
  4267. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4268. $expected = [
  4269. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4270. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4271. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4272. ];
  4273. $this->assertHtml($expected, $result);
  4274. }
  4275. /**
  4276. * testRadioControlInsideLabel method
  4277. *
  4278. * Test generating radio input inside label ala twitter bootstrap.
  4279. */
  4280. public function testRadioControlInsideLabel(): void
  4281. {
  4282. $this->Form->setTemplates([
  4283. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4284. 'radioWrapper' => '{{label}}',
  4285. ]);
  4286. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4287. // phpcs:disable
  4288. $expected = [
  4289. ['input' => [
  4290. 'type' => 'hidden',
  4291. 'name' => 'Model[field]',
  4292. 'value' => '',
  4293. 'id' => 'model-field'
  4294. ]],
  4295. ['label' => ['for' => 'model-field-0']],
  4296. ['input' => [
  4297. 'type' => 'radio',
  4298. 'name' => 'Model[field]',
  4299. 'value' => '0',
  4300. 'id' => 'model-field-0'
  4301. ]],
  4302. 'option A',
  4303. '/label',
  4304. ['label' => ['for' => 'model-field-1']],
  4305. ['input' => [
  4306. 'type' => 'radio',
  4307. 'name' => 'Model[field]',
  4308. 'value' => '1',
  4309. 'id' => 'model-field-1'
  4310. ]],
  4311. 'option B',
  4312. '/label',
  4313. ];
  4314. // phpcs:enable
  4315. $this->assertHtml($expected, $result);
  4316. }
  4317. /**
  4318. * testRadioHiddenControlDisabling method
  4319. *
  4320. * Test disabling the hidden input for radio buttons.
  4321. */
  4322. public function testRadioHiddenControlDisabling(): void
  4323. {
  4324. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4325. $expected = [
  4326. 'label' => ['for' => 'model-1-field-0'],
  4327. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4328. 'option A',
  4329. '/label',
  4330. ];
  4331. $this->assertHtml($expected, $result);
  4332. }
  4333. /**
  4334. * testRadioOutOfRange method
  4335. *
  4336. * Test radio element set generation.
  4337. */
  4338. public function testRadioOutOfRange(): void
  4339. {
  4340. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4341. $expected = [
  4342. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4343. 'label' => ['for' => 'model-field-v'],
  4344. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4345. 'value',
  4346. '/label',
  4347. ];
  4348. $this->assertHtml($expected, $result);
  4349. }
  4350. /**
  4351. * testSelect method
  4352. *
  4353. * Test select element generation.
  4354. */
  4355. public function testSelect(): void
  4356. {
  4357. $result = $this->Form->select('Model.field', []);
  4358. $expected = [
  4359. 'select' => ['name' => 'Model[field]'],
  4360. '/select',
  4361. ];
  4362. $this->assertHtml($expected, $result);
  4363. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4364. $this->Form->create();
  4365. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4366. $expected = [
  4367. 'select' => ['name' => 'Model[field]'],
  4368. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4369. 'good',
  4370. '/option',
  4371. ['option' => ['value' => 'other']],
  4372. 'bad',
  4373. '/option',
  4374. '/select',
  4375. ];
  4376. $this->assertHtml($expected, $result);
  4377. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4378. $this->assertHtml($expected, $result);
  4379. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4380. $this->Form->create();
  4381. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4382. $expected = [
  4383. 'select' => ['name' => 'Model[field]'],
  4384. ['option' => ['value' => 'value']],
  4385. 'good',
  4386. '/option',
  4387. ['option' => ['value' => 'other']],
  4388. 'bad',
  4389. '/option',
  4390. '/select',
  4391. ];
  4392. $this->assertHtml($expected, $result);
  4393. $options = [
  4394. ['value' => 'first', 'text' => 'First'],
  4395. ['value' => 'first', 'text' => 'Another First'],
  4396. ];
  4397. $result = $this->Form->select(
  4398. 'Model.field',
  4399. $options,
  4400. ['escape' => false, 'empty' => false]
  4401. );
  4402. $expected = [
  4403. 'select' => ['name' => 'Model[field]'],
  4404. ['option' => ['value' => 'first']],
  4405. 'First',
  4406. '/option',
  4407. ['option' => ['value' => 'first']],
  4408. 'Another First',
  4409. '/option',
  4410. '/select',
  4411. ];
  4412. $this->assertHtml($expected, $result);
  4413. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4414. $this->Form->create();
  4415. $result = $this->Form->select(
  4416. 'Model.contact_id',
  4417. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4418. ['escape' => false, 'empty' => 'pick something']
  4419. );
  4420. $expected = [
  4421. 'select' => ['name' => 'Model[contact_id]'],
  4422. ['option' => ['value' => '']], 'pick something', '/option',
  4423. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4424. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4425. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4426. '/select',
  4427. ];
  4428. $this->assertHtml($expected, $result);
  4429. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4430. $this->Form->create();
  4431. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4432. $expected = [
  4433. 'select' => ['name' => 'Model[field]'],
  4434. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4435. ['option' => ['value' => '1']], 'Yes', '/option',
  4436. '/select',
  4437. ];
  4438. $this->assertHtml($expected, $result);
  4439. }
  4440. /**
  4441. * testSelectEscapeHtml method
  4442. *
  4443. * Test that select() escapes HTML.
  4444. */
  4445. public function testSelectEscapeHtml(): void
  4446. {
  4447. $result = $this->Form->select(
  4448. 'Model.field',
  4449. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4450. ['empty' => false]
  4451. );
  4452. $expected = [
  4453. 'select' => ['name' => 'Model[field]'],
  4454. ['option' => ['value' => 'first']],
  4455. 'first &quot;html&quot; &lt;chars&gt;',
  4456. '/option',
  4457. ['option' => ['value' => 'second']],
  4458. 'value',
  4459. '/option',
  4460. '/select',
  4461. ];
  4462. $this->assertHtml($expected, $result);
  4463. $result = $this->Form->select(
  4464. 'Model.field',
  4465. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4466. ['escape' => false, 'empty' => false]
  4467. );
  4468. $expected = [
  4469. 'select' => ['name' => 'Model[field]'],
  4470. ['option' => ['value' => 'first']],
  4471. 'first "html" <chars>',
  4472. '/option',
  4473. ['option' => ['value' => 'second']],
  4474. 'value',
  4475. '/option',
  4476. '/select',
  4477. ];
  4478. $this->assertHtml($expected, $result);
  4479. }
  4480. /**
  4481. * testSelectRequired method
  4482. *
  4483. * Test select() with required and disabled attributes.
  4484. */
  4485. public function testSelectRequired(): void
  4486. {
  4487. $this->article['required'] = [
  4488. 'user_id' => true,
  4489. ];
  4490. $this->Form->create($this->article);
  4491. $result = $this->Form->select('user_id', ['option A']);
  4492. $expected = [
  4493. 'select' => [
  4494. 'name' => 'user_id',
  4495. 'required' => 'required',
  4496. ],
  4497. ['option' => ['value' => '0']], 'option A', '/option',
  4498. '/select',
  4499. ];
  4500. $this->assertHtml($expected, $result);
  4501. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4502. $expected = [
  4503. 'select' => [
  4504. 'name' => 'user_id',
  4505. 'disabled' => 'disabled',
  4506. ],
  4507. ['option' => ['value' => '0']], 'option A', '/option',
  4508. '/select',
  4509. ];
  4510. $this->assertHtml($expected, $result);
  4511. }
  4512. public function testSelectEmptyWithRequiredFalse(): void
  4513. {
  4514. $Articles = $this->getTableLocator()->get('Articles');
  4515. $validator = $Articles->getValidator('default');
  4516. $validator->allowEmptyString('user_id');
  4517. $Articles->setValidator('default', $validator);
  4518. $entity = $Articles->newEmptyEntity();
  4519. $this->Form->create($entity);
  4520. $result = $this->Form->select('user_id', ['option A']);
  4521. $expected = [
  4522. 'select' => [
  4523. 'name' => 'user_id',
  4524. ],
  4525. ['option' => ['value' => '']], '/option',
  4526. ['option' => ['value' => '0']], 'option A', '/option',
  4527. '/select',
  4528. ];
  4529. $this->assertHtml($expected, $result);
  4530. }
  4531. /**
  4532. * testNestedSelect method
  4533. *
  4534. * Test select element generation with optgroups.
  4535. */
  4536. public function testNestedSelect(): void
  4537. {
  4538. $result = $this->Form->select(
  4539. 'Model.field',
  4540. [1 => 'One', 2 => 'Two', 'Three' => [
  4541. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4542. ]],
  4543. ['empty' => false]
  4544. );
  4545. $expected = [
  4546. 'select' => ['name' => 'Model[field]'],
  4547. ['option' => ['value' => 1]],
  4548. 'One',
  4549. '/option',
  4550. ['option' => ['value' => 2]],
  4551. 'Two',
  4552. '/option',
  4553. ['optgroup' => ['label' => 'Three']],
  4554. ['option' => ['value' => 3]],
  4555. 'Three',
  4556. '/option',
  4557. ['option' => ['value' => 4]],
  4558. 'Four',
  4559. '/option',
  4560. ['option' => ['value' => 5]],
  4561. 'Five',
  4562. '/option',
  4563. '/optgroup',
  4564. '/select',
  4565. ];
  4566. $this->assertHtml($expected, $result);
  4567. }
  4568. /**
  4569. * testSelectMultiple method
  4570. *
  4571. * Test generation of multiple select elements.
  4572. */
  4573. public function testSelectMultiple(): void
  4574. {
  4575. $options = ['first', 'second', 'third'];
  4576. $result = $this->Form->select(
  4577. 'Model.multi_field',
  4578. $options,
  4579. ['form' => 'my-form', 'multiple' => true]
  4580. );
  4581. $expected = [
  4582. 'input' => [
  4583. 'type' => 'hidden',
  4584. 'name' => 'Model[multi_field]',
  4585. 'value' => '',
  4586. 'form' => 'my-form',
  4587. ],
  4588. 'select' => [
  4589. 'name' => 'Model[multi_field][]',
  4590. 'multiple' => 'multiple',
  4591. 'form' => 'my-form',
  4592. ],
  4593. ['option' => ['value' => '0']],
  4594. 'first',
  4595. '/option',
  4596. ['option' => ['value' => '1']],
  4597. 'second',
  4598. '/option',
  4599. ['option' => ['value' => '2']],
  4600. 'third',
  4601. '/option',
  4602. '/select',
  4603. ];
  4604. $this->assertHtml($expected, $result);
  4605. $result = $this->Form->select(
  4606. 'Model.multi_field',
  4607. $options,
  4608. ['multiple' => 'multiple', 'form' => 'my-form']
  4609. );
  4610. $this->assertHtml($expected, $result);
  4611. $result = $this->Form->select(
  4612. 'Model.multi_field',
  4613. $options,
  4614. ['form' => 'my-form', 'multiple' => false]
  4615. );
  4616. $this->assertStringNotContainsString('multiple', $result);
  4617. }
  4618. /**
  4619. * testCheckboxZeroValue method
  4620. *
  4621. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4622. */
  4623. public function testCheckboxZeroValue(): void
  4624. {
  4625. $result = $this->Form->control('User.get_spam', [
  4626. 'type' => 'checkbox',
  4627. 'value' => '0',
  4628. 'hiddenField' => '1',
  4629. ]);
  4630. $expected = [
  4631. 'div' => ['class' => 'input checkbox'],
  4632. 'label' => ['for' => 'user-get-spam'],
  4633. ['input' => [
  4634. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4635. 'value' => '1',
  4636. ]],
  4637. ['input' => [
  4638. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4639. 'value' => '0', 'id' => 'user-get-spam',
  4640. ]],
  4641. 'Get Spam',
  4642. '/label',
  4643. '/div',
  4644. ];
  4645. $this->assertHtml($expected, $result);
  4646. $result = $this->Form->control('User.get_spam', [
  4647. 'type' => 'checkbox',
  4648. 'value' => '0',
  4649. 'hiddenField' => '',
  4650. ]);
  4651. $expected = [
  4652. 'div' => ['class' => 'input checkbox'],
  4653. 'label' => ['for' => 'user-get-spam'],
  4654. ['input' => [
  4655. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4656. 'value' => '',
  4657. ]],
  4658. ['input' => [
  4659. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4660. 'value' => '0', 'id' => 'user-get-spam',
  4661. ]],
  4662. 'Get Spam',
  4663. '/label',
  4664. '/div',
  4665. ];
  4666. $this->assertHtml($expected, $result);
  4667. }
  4668. /**
  4669. * testHabtmSelectBox method
  4670. *
  4671. * Test generation of habtm select boxes.
  4672. */
  4673. public function testHabtmSelectBox(): void
  4674. {
  4675. $options = [
  4676. 1 => 'blue',
  4677. 2 => 'red',
  4678. 3 => 'green',
  4679. ];
  4680. $tags = [
  4681. new Entity(['id' => 1, 'name' => 'blue']),
  4682. new Entity(['id' => 3, 'name' => 'green']),
  4683. ];
  4684. $article = new Article(['tags' => $tags]);
  4685. $this->Form->create($article);
  4686. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4687. $expected = [
  4688. 'div' => ['class' => 'input select'],
  4689. 'label' => ['for' => 'tags-ids'],
  4690. 'Tags',
  4691. '/label',
  4692. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4693. 'select' => [
  4694. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4695. 'multiple' => 'multiple',
  4696. ],
  4697. ['option' => ['value' => '1', 'selected' => 'selected']],
  4698. 'blue',
  4699. '/option',
  4700. ['option' => ['value' => '2']],
  4701. 'red',
  4702. '/option',
  4703. ['option' => ['value' => '3', 'selected' => 'selected']],
  4704. 'green',
  4705. '/option',
  4706. '/select',
  4707. '/div',
  4708. ];
  4709. $this->assertHtml($expected, $result);
  4710. // make sure only 50 is selected, and not 50f5c0cf
  4711. $options = [
  4712. '1' => 'blue',
  4713. '50f5c0cf' => 'red',
  4714. '50' => 'green',
  4715. ];
  4716. $tags = [
  4717. new Entity(['id' => 1, 'name' => 'blue']),
  4718. new Entity(['id' => 50, 'name' => 'green']),
  4719. ];
  4720. $article = new Article(['tags' => $tags]);
  4721. $this->Form->create($article);
  4722. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4723. $expected = [
  4724. 'div' => ['class' => 'input select'],
  4725. 'label' => ['for' => 'tags-ids'],
  4726. 'Tags',
  4727. '/label',
  4728. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4729. 'select' => [
  4730. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4731. 'multiple' => 'multiple',
  4732. ],
  4733. ['option' => ['value' => '1', 'selected' => 'selected']],
  4734. 'blue',
  4735. '/option',
  4736. ['option' => ['value' => '50f5c0cf']],
  4737. 'red',
  4738. '/option',
  4739. ['option' => ['value' => '50', 'selected' => 'selected']],
  4740. 'green',
  4741. '/option',
  4742. '/select',
  4743. '/div',
  4744. ];
  4745. $this->assertHtml($expected, $result);
  4746. $spacecraft = [
  4747. 1 => 'Orion',
  4748. 2 => 'Helios',
  4749. ];
  4750. $this->View->set('spacecraft', $spacecraft);
  4751. $this->Form->create();
  4752. $result = $this->Form->control('spacecraft._ids');
  4753. $expected = [
  4754. 'div' => ['class' => 'input select'],
  4755. 'label' => ['for' => 'spacecraft-ids'],
  4756. 'Spacecraft',
  4757. '/label',
  4758. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4759. 'select' => [
  4760. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4761. 'multiple' => 'multiple',
  4762. ],
  4763. ['option' => ['value' => '1']],
  4764. 'Orion',
  4765. '/option',
  4766. ['option' => ['value' => '2']],
  4767. 'Helios',
  4768. '/option',
  4769. '/select',
  4770. '/div',
  4771. ];
  4772. $this->assertHtml($expected, $result);
  4773. }
  4774. /**
  4775. * testErrorsForBelongsToManySelect method
  4776. *
  4777. * Tests that errors for belongsToMany select fields are being
  4778. * picked up properly.
  4779. */
  4780. public function testErrorsForBelongsToManySelect(): void
  4781. {
  4782. $spacecraft = [
  4783. 1 => 'Orion',
  4784. 2 => 'Helios',
  4785. ];
  4786. $this->View->set('spacecraft', $spacecraft);
  4787. $article = new Article();
  4788. $article->setError('spacecraft', ['Invalid']);
  4789. $this->Form->create($article);
  4790. $result = $this->Form->control('spacecraft._ids');
  4791. $expected = [
  4792. ['div' => ['class' => 'input select error']],
  4793. 'label' => ['for' => 'spacecraft-ids'],
  4794. 'Spacecraft',
  4795. '/label',
  4796. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4797. 'select' => [
  4798. 'name' => 'spacecraft[_ids][]',
  4799. 'id' => 'spacecraft-ids',
  4800. 'multiple' => 'multiple',
  4801. ],
  4802. ['option' => ['value' => '1']],
  4803. 'Orion',
  4804. '/option',
  4805. ['option' => ['value' => '2']],
  4806. 'Helios',
  4807. '/option',
  4808. '/select',
  4809. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4810. 'Invalid',
  4811. '/div',
  4812. '/div',
  4813. ];
  4814. $this->assertHtml($expected, $result);
  4815. }
  4816. /**
  4817. * testSelectMultipleCheckboxes method
  4818. *
  4819. * Test generation of multi select elements in checkbox format.
  4820. */
  4821. public function testSelectMultipleCheckboxes(): void
  4822. {
  4823. $result = $this->Form->select(
  4824. 'Model.multi_field',
  4825. ['first', 'second', 'third'],
  4826. ['multiple' => 'checkbox']
  4827. );
  4828. $expected = [
  4829. 'input' => [
  4830. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4831. ],
  4832. ['div' => ['class' => 'checkbox']],
  4833. ['label' => ['for' => 'model-multi-field-0']],
  4834. ['input' => [
  4835. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4836. 'value' => '0', 'id' => 'model-multi-field-0',
  4837. ]],
  4838. 'first',
  4839. '/label',
  4840. '/div',
  4841. ['div' => ['class' => 'checkbox']],
  4842. ['label' => ['for' => 'model-multi-field-1']],
  4843. ['input' => [
  4844. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4845. 'value' => '1', 'id' => 'model-multi-field-1',
  4846. ]],
  4847. 'second',
  4848. '/label',
  4849. '/div',
  4850. ['div' => ['class' => 'checkbox']],
  4851. ['label' => ['for' => 'model-multi-field-2']],
  4852. ['input' => [
  4853. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4854. 'value' => '2', 'id' => 'model-multi-field-2',
  4855. ]],
  4856. 'third',
  4857. '/label',
  4858. '/div',
  4859. ];
  4860. $this->assertHtml($expected, $result);
  4861. $result = $this->Form->select(
  4862. 'Model.multi_field',
  4863. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4864. ['multiple' => 'checkbox']
  4865. );
  4866. $expected = [
  4867. 'input' => [
  4868. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4869. ],
  4870. ['div' => ['class' => 'checkbox']],
  4871. ['label' => ['for' => 'model-multi-field-a+']],
  4872. ['input' => [
  4873. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4874. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  4875. ]],
  4876. 'first',
  4877. '/label',
  4878. '/div',
  4879. ['div' => ['class' => 'checkbox']],
  4880. ['label' => ['for' => 'model-multi-field-a++']],
  4881. ['input' => [
  4882. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4883. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  4884. ]],
  4885. 'second',
  4886. '/label',
  4887. '/div',
  4888. ['div' => ['class' => 'checkbox']],
  4889. ['label' => ['for' => 'model-multi-field-a+++']],
  4890. ['input' => [
  4891. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4892. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  4893. ]],
  4894. 'third',
  4895. '/label',
  4896. '/div',
  4897. ];
  4898. $this->assertHtml($expected, $result);
  4899. $result = $this->Form->select(
  4900. 'Model.multi_field',
  4901. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4902. ['multiple' => 'checkbox']
  4903. );
  4904. $expected = [
  4905. 'input' => [
  4906. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4907. ],
  4908. ['div' => ['class' => 'checkbox']],
  4909. ['label' => ['for' => 'model-multi-field-a-b']],
  4910. ['input' => [
  4911. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4912. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  4913. ]],
  4914. 'first',
  4915. '/label',
  4916. '/div',
  4917. ['div' => ['class' => 'checkbox']],
  4918. ['label' => ['for' => 'model-multi-field-a-b1']],
  4919. ['input' => [
  4920. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4921. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  4922. ]],
  4923. 'second',
  4924. '/label',
  4925. '/div',
  4926. ['div' => ['class' => 'checkbox']],
  4927. ['label' => ['for' => 'model-multi-field-a-b2']],
  4928. ['input' => [
  4929. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4930. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  4931. ]],
  4932. 'third',
  4933. '/label',
  4934. '/div',
  4935. ];
  4936. $this->assertHtml($expected, $result);
  4937. }
  4938. /**
  4939. * testSelectMultipleCheckboxRequestData method
  4940. *
  4941. * Ensure that multiCheckbox reads from the request data.
  4942. */
  4943. public function testSelectMultipleCheckboxRequestData(): void
  4944. {
  4945. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  4946. $result = $this->Form->select(
  4947. 'Model.tags',
  4948. ['1' => 'first', 'Array' => 'Array'],
  4949. ['multiple' => 'checkbox']
  4950. );
  4951. $expected = [
  4952. 'input' => [
  4953. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  4954. ],
  4955. ['div' => ['class' => 'checkbox']],
  4956. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4957. ['input' => [
  4958. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4959. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  4960. ]],
  4961. 'first',
  4962. '/label',
  4963. '/div',
  4964. ['div' => ['class' => 'checkbox']],
  4965. ['label' => ['for' => 'model-tags-array']],
  4966. ['input' => [
  4967. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4968. 'value' => 'Array', 'id' => 'model-tags-array',
  4969. ]],
  4970. 'Array',
  4971. '/label',
  4972. '/div',
  4973. ];
  4974. $this->assertHtml($expected, $result);
  4975. }
  4976. /**
  4977. * testSelectMultipleCheckboxSecurity method
  4978. *
  4979. * Checks the security hash array generated for multiple-input checkbox elements.
  4980. */
  4981. public function testSelectMultipleCheckboxSecurity(): void
  4982. {
  4983. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  4984. $this->Form->create();
  4985. $this->Form->select(
  4986. 'Model.multi_field',
  4987. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4988. ['multiple' => 'checkbox']
  4989. );
  4990. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  4991. $this->assertEquals(['Model.multi_field'], $fields);
  4992. $result = $this->Form->secure();
  4993. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  4994. $hash = urlencode($hash . ':');
  4995. $this->assertStringContainsString('"' . $hash . '"', $result);
  4996. }
  4997. /**
  4998. * testSelectMultipleSecureWithNoOptions method
  4999. *
  5000. * Multiple select elements should always be secured as they always participate
  5001. * in the POST data.
  5002. */
  5003. public function testSelectMultipleSecureWithNoOptions(): void
  5004. {
  5005. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5006. $this->Form->create();
  5007. $this->Form->select(
  5008. 'Model.select',
  5009. [],
  5010. ['multiple' => true]
  5011. );
  5012. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5013. $this->assertEquals(['Model.select'], $result);
  5014. }
  5015. /**
  5016. * testSelectNoSecureWithNoOptions method
  5017. *
  5018. * When a select box has no options it should not be added to the fields list
  5019. * as it always fail post validation.
  5020. */
  5021. public function testSelectNoSecureWithNoOptions(): void
  5022. {
  5023. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5024. $this->Form->create();
  5025. $this->Form->select(
  5026. 'Model.select',
  5027. []
  5028. );
  5029. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5030. $this->assertEquals([], $result);
  5031. $this->Form->select(
  5032. 'Model.user_id',
  5033. [],
  5034. ['empty' => true]
  5035. );
  5036. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5037. $this->assertEquals(['Model.user_id'], $result);
  5038. }
  5039. /**
  5040. * testControlMultipleCheckboxes method
  5041. *
  5042. * Test control() resulting in multi select elements being generated.
  5043. */
  5044. public function testControlMultipleCheckboxes(): void
  5045. {
  5046. $result = $this->Form->control('Model.multi_field', [
  5047. 'options' => ['first', 'second', 'third'],
  5048. 'multiple' => 'checkbox',
  5049. ]);
  5050. $expected = [
  5051. ['div' => ['class' => 'input select']],
  5052. ['label' => ['for' => 'model-multi-field']],
  5053. 'Multi Field',
  5054. '/label',
  5055. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5056. ['div' => ['class' => 'checkbox']],
  5057. ['label' => ['for' => 'model-multi-field-0']],
  5058. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5059. 'first',
  5060. '/label',
  5061. '/div',
  5062. ['div' => ['class' => 'checkbox']],
  5063. ['label' => ['for' => 'model-multi-field-1']],
  5064. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5065. 'second',
  5066. '/label',
  5067. '/div',
  5068. ['div' => ['class' => 'checkbox']],
  5069. ['label' => ['for' => 'model-multi-field-2']],
  5070. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5071. 'third',
  5072. '/label',
  5073. '/div',
  5074. '/div',
  5075. ];
  5076. $this->assertHtml($expected, $result);
  5077. $result = $this->Form->control('Model.multi_field', [
  5078. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5079. 'multiple' => 'checkbox',
  5080. ]);
  5081. $expected = [
  5082. ['div' => ['class' => 'input select']],
  5083. ['label' => ['for' => 'model-multi-field']],
  5084. 'Multi Field',
  5085. '/label',
  5086. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5087. ['div' => ['class' => 'checkbox']],
  5088. ['label' => ['for' => 'model-multi-field-a']],
  5089. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5090. 'first',
  5091. '/label',
  5092. '/div',
  5093. ['div' => ['class' => 'checkbox']],
  5094. ['label' => ['for' => 'model-multi-field-b']],
  5095. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5096. 'second',
  5097. '/label',
  5098. '/div',
  5099. ['div' => ['class' => 'checkbox']],
  5100. ['label' => ['for' => 'model-multi-field-c']],
  5101. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5102. 'third',
  5103. '/label',
  5104. '/div',
  5105. '/div',
  5106. ];
  5107. $this->assertHtml($expected, $result);
  5108. }
  5109. /**
  5110. * testSelectHiddenFieldOmission method
  5111. *
  5112. * Test that select() with 'hiddenField' => false omits the hidden field.
  5113. */
  5114. public function testSelectHiddenFieldOmission(): void
  5115. {
  5116. $result = $this->Form->select(
  5117. 'Model.multi_field',
  5118. ['first', 'second'],
  5119. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5120. );
  5121. $this->assertStringNotContainsString('type="hidden"', $result);
  5122. }
  5123. /**
  5124. * testSelectCheckboxMultipleOverrideName method
  5125. *
  5126. * Test that select() with multiple = checkbox works with overriding name attribute.
  5127. */
  5128. public function testSelectCheckboxMultipleOverrideName(): void
  5129. {
  5130. $result = $this->Form->select('category', ['1', '2'], [
  5131. 'multiple' => 'checkbox',
  5132. 'name' => 'fish',
  5133. ]);
  5134. $expected = [
  5135. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5136. ['div' => ['class' => 'checkbox']],
  5137. ['label' => ['for' => 'fish-0']],
  5138. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5139. '1',
  5140. '/label',
  5141. '/div',
  5142. ['div' => ['class' => 'checkbox']],
  5143. ['label' => ['for' => 'fish-1']],
  5144. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5145. '2',
  5146. '/label',
  5147. '/div',
  5148. ];
  5149. $this->assertHtml($expected, $result);
  5150. $result = $this->Form->multiCheckbox(
  5151. 'category',
  5152. new Collection(['1', '2']),
  5153. ['name' => 'fish']
  5154. );
  5155. $this->assertHtml($expected, $result);
  5156. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5157. 'name' => 'fish',
  5158. ]);
  5159. $this->assertHtml($expected, $result);
  5160. }
  5161. /**
  5162. * testSelectCheckboxMultipleOverrideName method
  5163. *
  5164. * Test that select() with multiple = checkbox works with overriding name attribute.
  5165. */
  5166. public function testSelectCheckboxMultipleCustomId(): void
  5167. {
  5168. $result = $this->Form->select('category', ['1', '2'], [
  5169. 'multiple' => 'checkbox',
  5170. 'id' => 'cat',
  5171. ]);
  5172. $expected = [
  5173. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5174. ['div' => ['class' => 'checkbox']],
  5175. ['label' => ['for' => 'cat-0']],
  5176. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5177. '1',
  5178. '/label',
  5179. '/div',
  5180. ['div' => ['class' => 'checkbox']],
  5181. ['label' => ['for' => 'cat-1']],
  5182. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5183. '2',
  5184. '/label',
  5185. '/div',
  5186. ];
  5187. $this->assertHtml($expected, $result);
  5188. $result = $this->Form->multiCheckbox(
  5189. 'category',
  5190. ['1', '2'],
  5191. ['id' => 'cat']
  5192. );
  5193. $this->assertHtml($expected, $result);
  5194. }
  5195. /**
  5196. * testControlMultiCheckbox method
  5197. *
  5198. * Test that control() works with multicheckbox.
  5199. */
  5200. public function testControlMultiCheckbox(): void
  5201. {
  5202. $result = $this->Form->control('category', [
  5203. 'type' => 'multicheckbox',
  5204. 'options' => ['1', '2'],
  5205. ]);
  5206. $expected = [
  5207. ['div' => ['class' => 'input multicheckbox']],
  5208. '<label',
  5209. 'Category',
  5210. '/label',
  5211. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5212. ['div' => ['class' => 'checkbox']],
  5213. ['label' => ['for' => 'category-0']],
  5214. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5215. '1',
  5216. '/label',
  5217. '/div',
  5218. ['div' => ['class' => 'checkbox']],
  5219. ['label' => ['for' => 'category-1']],
  5220. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5221. '2',
  5222. '/label',
  5223. '/div',
  5224. '/div',
  5225. ];
  5226. $this->assertHtml($expected, $result);
  5227. }
  5228. /**
  5229. * testCheckbox method
  5230. *
  5231. * Test generation of checkboxes.
  5232. */
  5233. public function testCheckbox(): void
  5234. {
  5235. $result = $this->Form->checkbox('Model.field');
  5236. $expected = [
  5237. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5238. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5239. ];
  5240. $this->assertHtml($expected, $result);
  5241. $result = $this->Form->checkbox('Model.field', [
  5242. 'id' => 'theID',
  5243. 'value' => 'myvalue',
  5244. 'form' => 'my-form',
  5245. ]);
  5246. $expected = [
  5247. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5248. ['input' => [
  5249. 'type' => 'checkbox', 'name' => 'Model[field]',
  5250. 'value' => 'myvalue', 'id' => 'theID',
  5251. 'form' => 'my-form',
  5252. ]],
  5253. ];
  5254. $this->assertHtml($expected, $result);
  5255. }
  5256. /**
  5257. * testCheckboxDefaultValue method
  5258. *
  5259. * Test default value setting on checkbox() method.
  5260. */
  5261. public function testCheckboxDefaultValue(): void
  5262. {
  5263. $this->View->setRequest($this->View->getRequest()->withData('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. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5268. $this->Form->create();
  5269. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5270. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5271. $this->assertHtml($expected, $result);
  5272. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5273. $this->Form->create();
  5274. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5275. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5276. $this->assertHtml($expected, $result);
  5277. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5278. $this->Form->create();
  5279. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5280. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5281. $this->assertHtml($expected, $result);
  5282. $Articles = $this->getTableLocator()->get('Articles');
  5283. $Articles->getSchema()->addColumn(
  5284. 'published',
  5285. ['type' => 'boolean', 'null' => false, 'default' => true]
  5286. );
  5287. $this->Form->create($Articles->newEmptyEntity());
  5288. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5289. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5290. $this->assertHtml($expected, $result);
  5291. }
  5292. /**
  5293. * testCheckboxCheckedAndError method
  5294. *
  5295. * Test checkbox being checked or having errors.
  5296. */
  5297. public function testCheckboxCheckedAndError(): void
  5298. {
  5299. $this->article['errors'] = [
  5300. 'published' => true,
  5301. ];
  5302. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5303. $this->Form->create($this->article);
  5304. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5305. $expected = [
  5306. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5307. ['input' => [
  5308. 'type' => 'checkbox',
  5309. 'name' => 'published',
  5310. 'value' => 'myvalue',
  5311. 'id' => 'theID',
  5312. 'checked' => 'checked',
  5313. 'class' => 'form-error',
  5314. ]],
  5315. ];
  5316. $this->assertHtml($expected, $result);
  5317. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5318. $this->Form->create($this->article);
  5319. $result = $this->Form->checkbox('published');
  5320. $expected = [
  5321. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5322. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5323. ];
  5324. $this->assertHtml($expected, $result);
  5325. }
  5326. /**
  5327. * testCheckboxCustomNameAttribute method
  5328. *
  5329. * Test checkbox() with a custom name attribute.
  5330. */
  5331. public function testCheckboxCustomNameAttribute(): void
  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. public function testCheckboxHiddenField(): void
  5347. {
  5348. $result = $this->Form->checkbox('UserForm.something', [
  5349. 'hiddenField' => false,
  5350. ]);
  5351. $expected = [
  5352. 'input' => [
  5353. 'type' => 'checkbox',
  5354. 'name' => 'UserForm[something]',
  5355. 'value' => '1',
  5356. ],
  5357. ];
  5358. $this->assertHtml($expected, $result);
  5359. $result = $this->Form->checkbox('UserForm.something', [
  5360. 'value' => 'Y',
  5361. 'hiddenField' => '',
  5362. ]);
  5363. $expected = [
  5364. ['input' => [
  5365. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5366. 'value' => '',
  5367. ]],
  5368. ['input' => [
  5369. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5370. 'value' => 'Y',
  5371. ]],
  5372. ];
  5373. $this->assertHtml($expected, $result);
  5374. $result = $this->Form->checkbox('UserForm.something', [
  5375. 'value' => 'Y',
  5376. 'hiddenField' => 'N',
  5377. ]);
  5378. $expected = [
  5379. ['input' => [
  5380. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5381. 'value' => 'N',
  5382. ]],
  5383. ['input' => [
  5384. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5385. 'value' => 'Y',
  5386. ]],
  5387. ];
  5388. $this->assertHtml($expected, $result);
  5389. }
  5390. /**
  5391. * testTime method
  5392. *
  5393. * Test the time type.
  5394. */
  5395. public function testTime(): void
  5396. {
  5397. $result = $this->Form->time('start_time', [
  5398. 'value' => '2014-03-08 16:30:00',
  5399. ]);
  5400. $expected = [
  5401. 'input' => [
  5402. 'type' => 'time',
  5403. 'name' => 'start_time',
  5404. 'value' => '16:30:00',
  5405. 'step' => '1',
  5406. ],
  5407. ];
  5408. $this->assertHtml($expected, $result);
  5409. }
  5410. /**
  5411. * testDate method
  5412. *
  5413. * Test the date type.
  5414. */
  5415. public function testDate(): void
  5416. {
  5417. $result = $this->Form->date('start_day', [
  5418. 'value' => '2014-03-08',
  5419. ]);
  5420. $expected = [
  5421. 'input' => [
  5422. 'type' => 'date',
  5423. 'name' => 'start_day',
  5424. 'value' => '2014-03-08',
  5425. ],
  5426. ];
  5427. $this->assertHtml($expected, $result);
  5428. $result = $this->Form->date('start_day', [
  5429. 'value' => new Date('2014-03-08'),
  5430. ]);
  5431. $this->assertHtml($expected, $result);
  5432. }
  5433. /**
  5434. * testDateTime method
  5435. */
  5436. public function testDateTime(): void
  5437. {
  5438. $result = $this->Form->dateTime('date', ['default' => true]);
  5439. $expected = [
  5440. 'input' => [
  5441. 'type' => 'datetime-local',
  5442. 'name' => 'date',
  5443. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5444. 'step' => '1',
  5445. ],
  5446. ];
  5447. $this->assertHtml($expected, $result);
  5448. }
  5449. /**
  5450. * testDateTimeSecured method
  5451. *
  5452. * Test that datetime fields are added to protected fields list.
  5453. */
  5454. public function testDateTimeSecured(): void
  5455. {
  5456. $this->View->setRequest(
  5457. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5458. );
  5459. $this->Form->create();
  5460. $this->Form->dateTime('date');
  5461. $expected = ['date'];
  5462. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5463. $this->assertEquals($expected, $result);
  5464. $this->Form->date('published');
  5465. $expected = ['date', 'published'];
  5466. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5467. $this->assertEquals($expected, $result);
  5468. }
  5469. /**
  5470. * testDateTimeSecuredDisabled method
  5471. *
  5472. * Test that datetime fields are added to protected fields list.
  5473. */
  5474. public function testDateTimeSecuredDisabled(): void
  5475. {
  5476. $this->View->setRequest(
  5477. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5478. );
  5479. $this->Form->create();
  5480. $this->Form->dateTime('date', ['secure' => false]);
  5481. $expected = [];
  5482. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5483. $this->assertEquals($expected, $result);
  5484. $this->Form->date('published', ['secure' => false]);
  5485. $expected = [];
  5486. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5487. $this->assertEquals($expected, $result);
  5488. }
  5489. /**
  5490. * testDatetimeWithDefault method
  5491. *
  5492. * Test that datetime() and default values work.
  5493. */
  5494. public function testDatetimeWithDefault(): void
  5495. {
  5496. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5497. $expected = [
  5498. 'input' => [
  5499. 'type' => 'datetime-local',
  5500. 'name' => 'updated',
  5501. 'value' => '2009-06-01T11:15:30',
  5502. 'step' => '1',
  5503. ],
  5504. ];
  5505. $this->assertHtml($expected, $result);
  5506. $result = $this->Form->dateTime('updated', [
  5507. 'default' => '2009-06-01 11:15:30',
  5508. ]);
  5509. $expected = [
  5510. 'input' => [
  5511. 'type' => 'datetime-local',
  5512. 'name' => 'updated',
  5513. 'value' => '2009-06-01T11:15:30',
  5514. 'step' => '1',
  5515. ],
  5516. ];
  5517. $this->assertHtml($expected, $result);
  5518. }
  5519. /**
  5520. * testMonth method
  5521. *
  5522. * Test generation of a month input.
  5523. */
  5524. public function testMonth(): void
  5525. {
  5526. $result = $this->Form->month('field', ['value' => '']);
  5527. $expected = [
  5528. 'input' => [
  5529. 'type' => 'month',
  5530. 'name' => 'field',
  5531. 'value' => '',
  5532. ],
  5533. ];
  5534. $this->assertHtml($expected, $result);
  5535. $this->View->setRequest(
  5536. $this->View->getRequest()->withData('release', '2050-02-10')
  5537. );
  5538. $this->Form->create();
  5539. $result = $this->Form->month('release');
  5540. $expected = [
  5541. 'input' => [
  5542. 'type' => 'month',
  5543. 'name' => 'release',
  5544. 'value' => '2050-02',
  5545. ],
  5546. ];
  5547. $this->assertHtml($expected, $result);
  5548. $this->View->setRequest(
  5549. $this->View->getRequest()->withData('release', '2050-03')
  5550. );
  5551. $this->Form->create();
  5552. $result = $this->Form->month('release');
  5553. $expected = [
  5554. 'input' => [
  5555. 'type' => 'month',
  5556. 'name' => 'release',
  5557. 'value' => '2050-03',
  5558. ],
  5559. ];
  5560. $this->assertHtml($expected, $result);
  5561. }
  5562. /**
  5563. * testYear method
  5564. *
  5565. * Test generation of a year input.
  5566. */
  5567. public function testYear(): void
  5568. {
  5569. $this->View->setRequest(
  5570. $this->View->getRequest()->withData('published', '2006')
  5571. );
  5572. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5573. $expected = [
  5574. ['select' => ['name' => 'field']],
  5575. ['option' => ['selected' => 'selected', 'value' => '']],
  5576. '/option',
  5577. ['option' => ['value' => '2007']],
  5578. '2007',
  5579. '/option',
  5580. ['option' => ['value' => '2006']],
  5581. '2006',
  5582. '/option',
  5583. '/select',
  5584. ];
  5585. $this->assertHtml($expected, $result);
  5586. $result = $this->Form->year('field', [
  5587. 'value' => '',
  5588. 'min' => 2006,
  5589. 'max' => 2007,
  5590. 'order' => 'asc',
  5591. ]);
  5592. $expected = [
  5593. ['select' => ['name' => 'field']],
  5594. ['option' => ['selected' => 'selected', 'value' => '']],
  5595. '/option',
  5596. ['option' => ['value' => '2006']],
  5597. '2006',
  5598. '/option',
  5599. ['option' => ['value' => '2007']],
  5600. '2007',
  5601. '/option',
  5602. '/select',
  5603. ];
  5604. $this->assertHtml($expected, $result);
  5605. $result = $this->Form->year('published', [
  5606. 'empty' => false,
  5607. 'min' => 2006,
  5608. 'max' => 2007,
  5609. ]);
  5610. $expected = [
  5611. ['select' => ['name' => 'published']],
  5612. ['option' => ['value' => '2007']],
  5613. '2007',
  5614. '/option',
  5615. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5616. '2006',
  5617. '/option',
  5618. '/select',
  5619. ];
  5620. $this->assertHtml($expected, $result);
  5621. $result = $this->Form->year('published', [
  5622. 'empty' => false,
  5623. 'value' => new Date('2008-01-12'),
  5624. 'min' => 2007,
  5625. 'max' => 2009,
  5626. ]);
  5627. $expected = [
  5628. ['select' => ['name' => 'published']],
  5629. ['option' => ['value' => '2009']],
  5630. '2009',
  5631. '/option',
  5632. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5633. '2008',
  5634. '/option',
  5635. ['option' => ['value' => '2007']],
  5636. '2007',
  5637. '/option',
  5638. '/select',
  5639. ];
  5640. $this->assertHtml($expected, $result);
  5641. $result = $this->Form->year('published', [
  5642. 'empty' => 'Published on',
  5643. ]);
  5644. $this->assertStringContainsString('Published on', $result);
  5645. }
  5646. /**
  5647. * testControlYearPreEpoch method
  5648. *
  5649. * Test minYear being prior to the unix epoch.
  5650. */
  5651. public function testControlYearPreEpoch(): void
  5652. {
  5653. $start = date('Y') - 80;
  5654. $end = date('Y') - 18;
  5655. $result = $this->Form->control('birth_year', [
  5656. 'type' => 'year',
  5657. 'label' => 'Birth Year',
  5658. 'min' => $start,
  5659. 'max' => $end,
  5660. ]);
  5661. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5662. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5663. $this->assertStringNotContainsString('value="00">00', $result);
  5664. }
  5665. /**
  5666. * test control() datetime & required attributes
  5667. */
  5668. public function testControlDatetimeRequired(): void
  5669. {
  5670. $result = $this->Form->control('birthday', [
  5671. 'type' => 'date',
  5672. 'required' => true,
  5673. ]);
  5674. $this->assertStringContainsString(
  5675. '<input type="date" name="birthday" required="required"',
  5676. $result
  5677. );
  5678. }
  5679. /**
  5680. * testYearAutoExpandRange method
  5681. */
  5682. public function testYearAutoExpandRange(): void
  5683. {
  5684. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5685. $result = $this->Form->year('birthday');
  5686. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5687. $result = $matches[1];
  5688. $expected = range(date('Y') + 5, 1930);
  5689. $this->assertEquals($expected, $result);
  5690. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5691. $this->Form->create();
  5692. $result = $this->Form->year('release');
  5693. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5694. $result = $matches[1];
  5695. $expected = range(2050, date('Y') - 5);
  5696. $this->assertEquals($expected, $result);
  5697. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5698. $this->Form->create();
  5699. $result = $this->Form->year('release', [
  5700. 'min' => 1890,
  5701. 'max' => 1900,
  5702. ]);
  5703. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5704. $result = $matches[1];
  5705. $expected = range(1900, 1881);
  5706. $this->assertEquals($expected, $result);
  5707. }
  5708. /**
  5709. * test control placeholder + label
  5710. */
  5711. public function testControlLabelAndPlaceholder(): void
  5712. {
  5713. $this->Form->create($this->article);
  5714. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5715. $expected = [
  5716. 'div' => ['class' => 'input text required'],
  5717. 'label' => ['for' => 'title'],
  5718. 'Title',
  5719. '/label',
  5720. 'input' => [
  5721. 'aria-required' => 'true',
  5722. 'type' => 'text',
  5723. 'required' => 'required',
  5724. 'placeholder' => 'Add title',
  5725. 'id' => 'title',
  5726. 'name' => 'title',
  5727. 'data-validity-message' => 'This field cannot be left empty',
  5728. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5729. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5730. ],
  5731. '/div',
  5732. ];
  5733. $this->assertHtml($expected, $result);
  5734. }
  5735. /**
  5736. * testControlLabelFalse method
  5737. *
  5738. * Test the label option being set to false.
  5739. */
  5740. public function testControlLabelFalse(): void
  5741. {
  5742. $this->Form->create($this->article);
  5743. $result = $this->Form->control('title', ['label' => false]);
  5744. $expected = [
  5745. 'div' => ['class' => 'input text required'],
  5746. 'input' => [
  5747. 'aria-required' => 'true',
  5748. 'type' => 'text',
  5749. 'required' => 'required',
  5750. 'id' => 'title',
  5751. 'name' => 'title',
  5752. 'data-validity-message' => 'This field cannot be left empty',
  5753. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5754. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5755. ],
  5756. '/div',
  5757. ];
  5758. $this->assertHtml($expected, $result);
  5759. $this->Form->create($this->article);
  5760. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5761. $expected['input'] += [
  5762. 'placeholder' => 'Add title',
  5763. 'aria-label' => 'Add title',
  5764. ];
  5765. $this->assertHtml($expected, $result);
  5766. }
  5767. /**
  5768. * testTextArea method
  5769. *
  5770. * Test generation of a textarea input.
  5771. */
  5772. public function testTextArea(): void
  5773. {
  5774. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5775. $result = $this->Form->textarea('field');
  5776. $expected = [
  5777. 'textarea' => ['name' => 'field', 'rows' => 5],
  5778. 'some test data',
  5779. '/textarea',
  5780. ];
  5781. $this->assertHtml($expected, $result);
  5782. $result = $this->Form->textarea('user.bio');
  5783. $expected = [
  5784. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5785. '/textarea',
  5786. ];
  5787. $this->assertHtml($expected, $result);
  5788. $this->View->setRequest($this->View->getRequest()
  5789. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5790. $this->Form->create();
  5791. $result = $this->Form->textarea('field');
  5792. $expected = [
  5793. 'textarea' => ['name' => 'field', 'rows' => 5],
  5794. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5795. '/textarea',
  5796. ];
  5797. $this->assertHtml($expected, $result);
  5798. $this->View->setRequest($this->View->getRequest()->withData(
  5799. 'Model.field',
  5800. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5801. ));
  5802. $this->Form->create();
  5803. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5804. $expected = [
  5805. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5806. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5807. '/textarea',
  5808. ];
  5809. $this->assertHtml($expected, $result);
  5810. $result = $this->Form->textarea('0.OtherModel.field');
  5811. $expected = [
  5812. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5813. '/textarea',
  5814. ];
  5815. $this->assertHtml($expected, $result);
  5816. }
  5817. /**
  5818. * testTextAreaWithStupidCharacters method
  5819. *
  5820. * Test text area with non-ascii characters.
  5821. */
  5822. public function testTextAreaWithStupidCharacters(): void
  5823. {
  5824. $result = $this->Form->textarea('Post.content', [
  5825. 'value' => 'GREAT®',
  5826. 'rows' => '15',
  5827. 'cols' => '75',
  5828. ]);
  5829. $expected = [
  5830. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5831. 'GREAT®',
  5832. '/textarea',
  5833. ];
  5834. $this->assertHtml($expected, $result);
  5835. }
  5836. /**
  5837. * testTextAreaMaxLength method
  5838. *
  5839. * Test textareas maxlength read from schema.
  5840. */
  5841. public function testTextAreaMaxLength(): void
  5842. {
  5843. $this->Form->create([
  5844. 'schema' => [
  5845. 'stuff' => ['type' => 'string', 'length' => 10],
  5846. ],
  5847. ]);
  5848. $result = $this->Form->control('other', ['type' => 'textarea']);
  5849. $expected = [
  5850. 'div' => ['class' => 'input textarea'],
  5851. 'label' => ['for' => 'other'],
  5852. 'Other',
  5853. '/label',
  5854. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5855. '/textarea',
  5856. '/div',
  5857. ];
  5858. $this->assertHtml($expected, $result);
  5859. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5860. $expected = [
  5861. 'div' => ['class' => 'input textarea'],
  5862. 'label' => ['for' => 'stuff'],
  5863. 'Stuff',
  5864. '/label',
  5865. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5866. '/textarea',
  5867. '/div',
  5868. ];
  5869. $this->assertHtml($expected, $result);
  5870. }
  5871. /**
  5872. * testHiddenField method
  5873. *
  5874. * Test generation of a hidden input.
  5875. */
  5876. public function testHidden(): void
  5877. {
  5878. $this->article['errors'] = [
  5879. 'field' => true,
  5880. ];
  5881. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5882. $this->Form->create($this->article);
  5883. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5884. $expected = [
  5885. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5886. $this->assertHtml($expected, $result);
  5887. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5888. $expected = [
  5889. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5890. ];
  5891. $this->assertHtml($expected, $result);
  5892. }
  5893. /**
  5894. * Test hidden() with various boolean values.
  5895. */
  5896. public function testHiddenBooleanValues(): void
  5897. {
  5898. $this->Form->create($this->article);
  5899. $result = $this->Form->hidden('field', ['value' => null]);
  5900. $expected = [
  5901. 'input' => ['type' => 'hidden', 'name' => 'field'],
  5902. ];
  5903. $this->assertHtml($expected, $result);
  5904. $result = $this->Form->hidden('field', ['value' => true]);
  5905. $expected = [
  5906. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  5907. ];
  5908. $this->assertHtml($expected, $result);
  5909. $result = $this->Form->hidden('field', ['value' => false]);
  5910. $expected = [
  5911. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  5912. ];
  5913. $this->assertHtml($expected, $result);
  5914. }
  5915. /**
  5916. * testFileUploadField method
  5917. *
  5918. * Test generation of a file upload input.
  5919. */
  5920. public function testFileUploadField(): void
  5921. {
  5922. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5923. $result = $this->Form->file('Model.upload');
  5924. $this->assertHtml($expected, $result);
  5925. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5926. 'name' => '', 'type' => '', 'tmp_name' => '',
  5927. 'error' => 4, 'size' => 0,
  5928. ]));
  5929. $result = $this->Form->file('Model.upload');
  5930. $this->assertHtml($expected, $result);
  5931. $this->View->setRequest(
  5932. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  5933. );
  5934. $result = $this->Form->file('Model.upload');
  5935. $this->assertHtml($expected, $result);
  5936. }
  5937. /**
  5938. * testFileUploadOnOtherModel method
  5939. *
  5940. * Test File upload input on a model not used in create().
  5941. */
  5942. public function testFileUploadOnOtherModel(): void
  5943. {
  5944. $this->Form->create($this->article, ['type' => 'file']);
  5945. $result = $this->Form->file('ValidateProfile.city');
  5946. $expected = [
  5947. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  5948. ];
  5949. $this->assertHtml($expected, $result);
  5950. }
  5951. /**
  5952. * testButton method
  5953. *
  5954. * Test generation of a form button.
  5955. */
  5956. public function testButton(): void
  5957. {
  5958. $result = $this->Form->button('Hi');
  5959. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5960. $this->assertHtml($expected, $result);
  5961. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  5962. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  5963. $this->assertHtml($expected, $result);
  5964. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  5965. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  5966. $this->assertHtml($expected, $result);
  5967. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  5968. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  5969. $this->assertHtml($expected, $result);
  5970. $result = $this->Form->button('No type', ['type' => false]);
  5971. $expected = ['button' => [], 'No type', '/button'];
  5972. $this->assertHtml($expected, $result);
  5973. $result = $this->Form->button('Upload Text', [
  5974. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5975. 'escape' => false,
  5976. ]);
  5977. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  5978. }
  5979. /**
  5980. * testButtonUnlockedByDefault method
  5981. *
  5982. * Test that button() makes unlocked fields by default.
  5983. */
  5984. public function testButtonUnlockedByDefault(): void
  5985. {
  5986. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5987. $this->Form->create();
  5988. $this->Form->button('Save', ['name' => 'save']);
  5989. $this->Form->button('Clear');
  5990. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  5991. $this->assertEquals(['save'], $result);
  5992. }
  5993. /**
  5994. * Test generation of a form button with confirm message.
  5995. */
  5996. public function testButtonWithConfirm(): void
  5997. {
  5998. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  5999. $expected = ['button' => [
  6000. 'type' => 'submit',
  6001. 'data-confirm-message' => 'Confirm me!',
  6002. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6003. ], 'Hi', '/button'];
  6004. $this->assertHtml($expected, $result);
  6005. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6006. $expected = ['button' => [
  6007. 'type' => 'submit',
  6008. 'data-confirm-message' => 'Confirm "me"!',
  6009. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6010. ], 'Hi', '/button'];
  6011. $this->assertHtml($expected, $result);
  6012. }
  6013. /**
  6014. * testPostButton method
  6015. */
  6016. public function testPostButton(): void
  6017. {
  6018. $result = $this->Form->postButton('Hi', '/controller/action');
  6019. $expected = [
  6020. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6021. 'button' => ['type' => 'submit'],
  6022. 'Hi',
  6023. '/button',
  6024. '/form',
  6025. ];
  6026. $this->assertHtml($expected, $result);
  6027. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6028. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6029. }
  6030. /**
  6031. * testPostButtonMethodType method
  6032. */
  6033. public function testPostButtonMethodType(): void
  6034. {
  6035. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6036. $expected = [
  6037. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6038. 'div' => ['style' => 'display:none;'],
  6039. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6040. '/div',
  6041. 'button' => ['type' => 'submit'],
  6042. 'Hi',
  6043. '/button',
  6044. '/form',
  6045. ];
  6046. $this->assertHtml($expected, $result);
  6047. }
  6048. /**
  6049. * testPostButtonFormOptions method
  6050. */
  6051. public function testPostButtonFormOptions(): void
  6052. {
  6053. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6054. $expected = [
  6055. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6056. 'button' => ['type' => 'submit'],
  6057. 'Hi',
  6058. '/button',
  6059. '/form',
  6060. ];
  6061. $this->assertHtml($expected, $result);
  6062. }
  6063. /**
  6064. * testPostButtonNestedData method
  6065. *
  6066. * Test using postButton with N dimensional data.
  6067. */
  6068. public function testPostButtonNestedData(): void
  6069. {
  6070. $data = [
  6071. 'one' => [
  6072. 'two' => [
  6073. 3, 4, 5,
  6074. ],
  6075. ],
  6076. ];
  6077. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6078. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6079. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6080. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6081. }
  6082. /**
  6083. * testSecurePostButton method
  6084. *
  6085. * Test that postButton adds _Token fields.
  6086. */
  6087. public function testSecurePostButton(): void
  6088. {
  6089. $this->View->setRequest($this->View->getRequest()
  6090. ->withAttribute('csrfToken', 'testkey')
  6091. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6092. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6093. $tokenDebug = urlencode(json_encode([
  6094. '/posts/delete/1',
  6095. [],
  6096. [],
  6097. ]));
  6098. $expected = [
  6099. 'form' => [
  6100. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6101. ],
  6102. ['div' => ['style' => 'display:none;']],
  6103. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6104. '/div',
  6105. 'button' => ['type' => 'submit'],
  6106. 'Delete',
  6107. '/button',
  6108. ['div' => ['style' => 'display:none;']],
  6109. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6110. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6111. ['input' => [
  6112. 'type' => 'hidden',
  6113. 'name' => '_Token[debug]',
  6114. 'value' => $tokenDebug,
  6115. ]],
  6116. '/div',
  6117. '/form',
  6118. ];
  6119. $this->assertHtml($expected, $result);
  6120. }
  6121. /**
  6122. * testPostLink method
  6123. */
  6124. public function testPostLink(): void
  6125. {
  6126. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6127. $expected = [
  6128. 'form' => [
  6129. 'method' => 'post', 'action' => '/posts/delete/1',
  6130. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6131. ],
  6132. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6133. '/form',
  6134. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6135. 'Delete',
  6136. '/a',
  6137. ];
  6138. $this->assertHtml($expected, $result);
  6139. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6140. $expected = [
  6141. 'form' => [
  6142. 'method' => 'post', 'action' => '/posts/delete/1',
  6143. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6144. ],
  6145. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6146. '/form',
  6147. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6148. 'Delete',
  6149. '/a',
  6150. ];
  6151. $this->assertHtml($expected, $result);
  6152. $result = $this->Form->postLink(
  6153. 'Delete',
  6154. '/posts/delete/1',
  6155. ['target' => '_blank', 'class' => 'btn btn-danger']
  6156. );
  6157. $expected = [
  6158. 'form' => [
  6159. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6160. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6161. ],
  6162. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6163. '/form',
  6164. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6165. 'Delete',
  6166. '/a',
  6167. ];
  6168. $this->assertHtml($expected, $result);
  6169. }
  6170. /**
  6171. * testPostLinkWithConfirm method
  6172. *
  6173. * Test the confirm option for postLink().
  6174. */
  6175. public function testPostLinkWithConfirm(): void
  6176. {
  6177. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6178. $expected = [
  6179. 'form' => [
  6180. 'method' => 'post', 'action' => '/posts/delete/1',
  6181. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6182. ],
  6183. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6184. '/form',
  6185. 'a' => [
  6186. 'href' => '#',
  6187. 'data-confirm-message' => 'Confirm?',
  6188. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6189. ],
  6190. 'Delete',
  6191. '/a',
  6192. ];
  6193. $this->assertHtml($expected, $result);
  6194. $result = $this->Form->postLink(
  6195. 'Delete',
  6196. '/posts/delete/1',
  6197. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6198. );
  6199. $expected = [
  6200. 'form' => [
  6201. 'method' => 'post', 'action' => '/posts/delete/1',
  6202. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6203. ],
  6204. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6205. '/form',
  6206. 'a' => [
  6207. 'href' => '#',
  6208. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6209. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6210. ],
  6211. 'Delete',
  6212. '/a',
  6213. ];
  6214. $this->assertHtml($expected, $result);
  6215. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6216. $result = $this->Form->postLink(
  6217. 'Delete',
  6218. '/posts/delete/1',
  6219. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6220. );
  6221. $expected = [
  6222. 'form' => [
  6223. 'method' => 'post', 'action' => '/posts/delete/1',
  6224. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6225. ],
  6226. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6227. '/form',
  6228. 'a' => [
  6229. 'href' => '#',
  6230. 'data-confirm-message' => 'Confirm this deletion?',
  6231. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6232. ],
  6233. 'Delete',
  6234. '/a',
  6235. ];
  6236. $this->assertHtml($expected, $result);
  6237. }
  6238. /**
  6239. * testPostLinkWithQuery method
  6240. *
  6241. * Test postLink() with query string args.
  6242. */
  6243. public function testPostLinkWithQuery(): void
  6244. {
  6245. $result = $this->Form->postLink(
  6246. 'Delete',
  6247. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6248. );
  6249. $expected = [
  6250. 'form' => [
  6251. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6252. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6253. ],
  6254. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6255. '/form',
  6256. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6257. 'Delete',
  6258. '/a',
  6259. ];
  6260. $this->assertHtml($expected, $result);
  6261. }
  6262. /**
  6263. * testPostLinkWithData method
  6264. *
  6265. * Test postLink with additional data.
  6266. */
  6267. public function testPostLinkWithData(): void
  6268. {
  6269. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6270. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6271. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6272. $this->Form->create($entity);
  6273. $this->Form->end();
  6274. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6275. $this->assertStringContainsString(
  6276. '<input type="hidden" name="name" value="show"',
  6277. $result,
  6278. 'should not contain entity data.'
  6279. );
  6280. }
  6281. /**
  6282. * testPostLinkSecurityHash method
  6283. *
  6284. * Test that security hashes for postLink include the url.
  6285. */
  6286. public function testPostLinkSecurityHash(): void
  6287. {
  6288. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6289. $hash .= '%3Aid';
  6290. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6291. $result = $this->Form->postLink(
  6292. 'Delete',
  6293. '/posts/delete/1',
  6294. ['data' => ['id' => 1]]
  6295. );
  6296. $tokenDebug = urlencode(json_encode([
  6297. '/posts/delete/1',
  6298. [
  6299. 'id' => 1,
  6300. ],
  6301. [],
  6302. ]));
  6303. $expected = [
  6304. 'form' => [
  6305. 'method' => 'post', 'action' => '/posts/delete/1',
  6306. 'name', 'style' => 'display:none;',
  6307. ],
  6308. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6309. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6310. 'div' => ['style' => 'display:none;'],
  6311. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6312. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6313. ['input' => [
  6314. 'type' => 'hidden',
  6315. 'name' => '_Token[debug]',
  6316. 'value' => $tokenDebug,
  6317. ]],
  6318. '/div',
  6319. '/form',
  6320. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6321. 'Delete',
  6322. '/a',
  6323. ];
  6324. $this->assertHtml($expected, $result);
  6325. }
  6326. /**
  6327. * testPostLinkSecurityHashBlockMode method
  6328. *
  6329. * Test that postLink doesn't modify the fields in the containing form.
  6330. *
  6331. * postLink() calls inside open forms should not modify the field list
  6332. * for the form.
  6333. */
  6334. public function testPostLinkSecurityHashBlockMode(): void
  6335. {
  6336. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6337. $hash .= '%3A';
  6338. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6339. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6340. $this->Form->control('title');
  6341. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6342. $result = $this->View->fetch('postLink');
  6343. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6344. $this->assertEquals(['title'], $fields);
  6345. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6346. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6347. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6348. }
  6349. /**
  6350. * testPostLinkSecurityHashNoDebugMode method
  6351. *
  6352. * Test that security does not include debug token if debug is false.
  6353. */
  6354. public function testPostLinkSecurityHashNoDebugMode(): void
  6355. {
  6356. Configure::write('debug', false);
  6357. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6358. $hash .= '%3Aid';
  6359. $this->View->setRequest($this->View->getRequest()
  6360. ->withAttribute('formTokenData', ['key' => 'test']));
  6361. $result = $this->Form->postLink(
  6362. 'Delete',
  6363. '/posts/delete/1',
  6364. ['data' => ['id' => 1]]
  6365. );
  6366. $expected = [
  6367. 'form' => [
  6368. 'method' => 'post', 'action' => '/posts/delete/1',
  6369. 'name', 'style' => 'display:none;',
  6370. ],
  6371. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6372. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6373. 'div' => ['style' => 'display:none;'],
  6374. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6375. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6376. '/div',
  6377. '/form',
  6378. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6379. 'Delete',
  6380. '/a',
  6381. ];
  6382. $this->assertHtml($expected, $result);
  6383. }
  6384. /**
  6385. * testPostLinkNestedData method
  6386. *
  6387. * Test using postLink with N dimensional data.
  6388. */
  6389. public function testPostLinkNestedData(): void
  6390. {
  6391. $data = [
  6392. 'one' => [
  6393. 'two' => [
  6394. 3, 4, 5,
  6395. ],
  6396. ],
  6397. ];
  6398. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6399. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6400. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6401. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6402. }
  6403. /**
  6404. * testPostLinkAfterGetForm method
  6405. *
  6406. * Test creating postLinks after a GET form.
  6407. */
  6408. public function testPostLinkAfterGetForm(): void
  6409. {
  6410. $this->View->setRequest($this->View->getRequest()
  6411. ->withAttribute('csrfToken', 'testkey')
  6412. ->withAttribute('formTokenData', []));
  6413. $this->Form->create($this->article, ['type' => 'get']);
  6414. $this->Form->end();
  6415. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6416. $tokenDebug = urlencode(json_encode([
  6417. '/posts/delete/1',
  6418. [],
  6419. [],
  6420. ]));
  6421. $expected = [
  6422. 'form' => [
  6423. 'method' => 'post', 'action' => '/posts/delete/1',
  6424. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6425. ],
  6426. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6427. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6428. 'div' => ['style' => 'display:none;'],
  6429. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6430. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6431. ['input' => [
  6432. 'type' => 'hidden', 'name' => '_Token[debug]',
  6433. 'value' => $tokenDebug,
  6434. ]],
  6435. '/div',
  6436. '/form',
  6437. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6438. 'Delete',
  6439. '/a',
  6440. ];
  6441. $this->assertHtml($expected, $result);
  6442. }
  6443. /**
  6444. * testPostLinkFormBuffer method
  6445. *
  6446. * Test that postLink adds form tags to view block.
  6447. */
  6448. public function testPostLinkFormBuffer(): void
  6449. {
  6450. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6451. $expected = [
  6452. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6453. 'Delete',
  6454. '/a',
  6455. ];
  6456. $this->assertHtml($expected, $result);
  6457. $result = $this->View->fetch('postLink');
  6458. $expected = [
  6459. 'form' => [
  6460. 'method' => 'post', 'action' => '/posts/delete/1',
  6461. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6462. ],
  6463. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6464. '/form',
  6465. ];
  6466. $this->assertHtml($expected, $result);
  6467. $result = $this->Form->postLink(
  6468. 'Delete',
  6469. '/posts/delete/2',
  6470. ['block' => true, 'method' => 'DELETE']
  6471. );
  6472. $expected = [
  6473. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6474. 'Delete',
  6475. '/a',
  6476. ];
  6477. $this->assertHtml($expected, $result);
  6478. $result = $this->View->fetch('postLink');
  6479. $expected = [
  6480. 'form' => [
  6481. 'method' => 'post', 'action' => '/posts/delete/1',
  6482. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6483. ],
  6484. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6485. '/form',
  6486. [
  6487. 'form' => [
  6488. 'method' => 'post', 'action' => '/posts/delete/2',
  6489. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6490. ],
  6491. ],
  6492. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6493. '/form',
  6494. ];
  6495. $this->assertHtml($expected, $result);
  6496. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6497. $expected = [
  6498. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6499. 'Delete',
  6500. '/a',
  6501. ];
  6502. $this->assertHtml($expected, $result);
  6503. $result = $this->View->fetch('foobar');
  6504. $expected = [
  6505. 'form' => [
  6506. 'method' => 'post', 'action' => '/posts/delete/1',
  6507. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6508. ],
  6509. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6510. '/form',
  6511. ];
  6512. $this->assertHtml($expected, $result);
  6513. }
  6514. /**
  6515. * testSubmitButton method
  6516. */
  6517. public function testSubmitButton(): void
  6518. {
  6519. $result = $this->Form->submit('');
  6520. $expected = [
  6521. 'div' => ['class' => 'submit'],
  6522. 'input' => ['type' => 'submit', 'value' => ''],
  6523. '/div',
  6524. ];
  6525. $this->assertHtml($expected, $result);
  6526. $result = $this->Form->submit('Test Submit');
  6527. $expected = [
  6528. 'div' => ['class' => 'submit'],
  6529. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6530. '/div',
  6531. ];
  6532. $this->assertHtml($expected, $result);
  6533. $result = $this->Form->submit('Next >');
  6534. $expected = [
  6535. 'div' => ['class' => 'submit'],
  6536. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6537. '/div',
  6538. ];
  6539. $this->assertHtml($expected, $result);
  6540. $result = $this->Form->submit('Next >', ['escape' => false]);
  6541. $expected = [
  6542. 'div' => ['class' => 'submit'],
  6543. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6544. '/div',
  6545. ];
  6546. $this->assertHtml($expected, $result);
  6547. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6548. $expected = [
  6549. 'div' => ['class' => 'submit'],
  6550. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6551. '/div',
  6552. ];
  6553. $this->assertHtml($expected, $result);
  6554. }
  6555. /**
  6556. * testSubmitImage method
  6557. *
  6558. * Test image submit types.
  6559. */
  6560. public function testSubmitImage(): void
  6561. {
  6562. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6563. $expected = [
  6564. 'div' => ['class' => 'submit'],
  6565. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6566. '/div',
  6567. ];
  6568. $this->assertHtml($expected, $result);
  6569. $result = $this->Form->submit('/relative/cake.power.gif');
  6570. $expected = [
  6571. 'div' => ['class' => 'submit'],
  6572. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6573. '/div',
  6574. ];
  6575. $this->assertHtml($expected, $result);
  6576. $result = $this->Form->submit('cake.power.gif');
  6577. $expected = [
  6578. 'div' => ['class' => 'submit'],
  6579. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6580. '/div',
  6581. ];
  6582. $this->assertHtml($expected, $result);
  6583. $result = $this->Form->submit('Not.an.image');
  6584. $expected = [
  6585. 'div' => ['class' => 'submit'],
  6586. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6587. '/div',
  6588. ];
  6589. $this->assertHtml($expected, $result);
  6590. }
  6591. /**
  6592. * testSubmitUnlockedByDefault method
  6593. *
  6594. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6595. * be submitted at a time.
  6596. */
  6597. public function testSubmitUnlockedByDefault(): void
  6598. {
  6599. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6600. $this->Form->create();
  6601. $this->Form->submit('Go go');
  6602. $this->Form->submit('Save', ['name' => 'save']);
  6603. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6604. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6605. }
  6606. /**
  6607. * testSubmitImageTimestamp method
  6608. *
  6609. * Test submit image with timestamps.
  6610. */
  6611. public function testSubmitImageTimestamp(): void
  6612. {
  6613. Configure::write('Asset.timestamp', 'force');
  6614. $result = $this->Form->submit('cake.power.gif');
  6615. $expected = [
  6616. 'div' => ['class' => 'submit'],
  6617. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6618. '/div',
  6619. ];
  6620. $this->assertHtml($expected, $result);
  6621. }
  6622. /**
  6623. * testDateTimeWithGetForms method
  6624. *
  6625. * Test that datetime() works with GET style forms.
  6626. */
  6627. public function testDateTimeWithGetForms(): void
  6628. {
  6629. $this->Form->create($this->article, ['type' => 'get']);
  6630. $result = $this->Form->datetime('created');
  6631. $expected = [
  6632. 'input' => [
  6633. 'type' => 'datetime-local',
  6634. 'name' => 'created',
  6635. 'value' => '',
  6636. 'step' => '1',
  6637. ],
  6638. ];
  6639. $this->assertHtml($expected, $result);
  6640. $result = $this->Form->datetime('created', ['default' => true]);
  6641. $expected = [
  6642. 'input' => [
  6643. 'type' => 'datetime-local',
  6644. 'name' => 'created',
  6645. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6646. 'step' => '1',
  6647. ],
  6648. ];
  6649. $this->assertHtml($expected, $result);
  6650. }
  6651. /**
  6652. * Provides fractional schema types
  6653. *
  6654. * @return array
  6655. */
  6656. public function fractionalTypeProvider(): array
  6657. {
  6658. return [
  6659. ['datetimefractional'],
  6660. ['timestampfractional'],
  6661. ['timestamptimezone'],
  6662. ];
  6663. }
  6664. /**
  6665. * testDateTimeWithFractional method
  6666. *
  6667. * Test that datetime() works with datetimefractional.
  6668. *
  6669. * @dataProvider fractionalTypeProvider
  6670. */
  6671. public function testDateTimeWithFractional(string $type): void
  6672. {
  6673. $this->Form->create([
  6674. 'schema' => [
  6675. 'created' => ['type' => $type],
  6676. ],
  6677. ]);
  6678. $result = $this->Form->datetime('created', [
  6679. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6680. ]);
  6681. $expected = [
  6682. 'input' => [
  6683. 'type' => 'datetime-local',
  6684. 'name' => 'created',
  6685. 'value' => '2019-09-27T02:52:43.123',
  6686. 'step' => '0.001',
  6687. ],
  6688. ];
  6689. $this->assertHtml($expected, $result);
  6690. }
  6691. /**
  6692. * testControlWithFractional method
  6693. *
  6694. * Test that control() works with datetimefractional.
  6695. *
  6696. * @dataProvider fractionalTypeProvider
  6697. */
  6698. public function testControlWithFractional(string $type): void
  6699. {
  6700. $this->Form->create([
  6701. 'schema' => [
  6702. 'created' => ['type' => $type],
  6703. ],
  6704. ]);
  6705. $result = $this->Form->control('created', [
  6706. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6707. ]);
  6708. $expected = [
  6709. 'div' => ['class' => 'input datetime'],
  6710. 'label' => ['for' => 'created'],
  6711. 'Created',
  6712. '/label',
  6713. 'input' => [
  6714. 'type' => 'datetime-local',
  6715. 'name' => 'created',
  6716. 'id' => 'created',
  6717. 'value' => '2019-09-27T02:52:43.123',
  6718. 'step' => '0.001',
  6719. ],
  6720. '/div',
  6721. ];
  6722. $this->assertHtml($expected, $result);
  6723. }
  6724. /**
  6725. * testForMagicControlNonExistentNotValidated method
  6726. */
  6727. public function testForMagicControlNonExistentNotValidated(): void
  6728. {
  6729. $this->Form->create($this->article);
  6730. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6731. $result = $this->Form->control('nonexistent_not_validated');
  6732. $expected = [
  6733. 'label' => ['for' => 'nonexistent-not-validated'],
  6734. 'Nonexistent Not Validated',
  6735. '/label',
  6736. 'input' => [
  6737. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6738. 'id' => 'nonexistent-not-validated',
  6739. ],
  6740. ];
  6741. $this->assertHtml($expected, $result);
  6742. $result = $this->Form->control('nonexistent_not_validated', [
  6743. 'val' => 'my value',
  6744. ]);
  6745. $expected = [
  6746. 'label' => ['for' => 'nonexistent-not-validated'],
  6747. 'Nonexistent Not Validated',
  6748. '/label',
  6749. 'input' => [
  6750. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6751. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6752. ],
  6753. ];
  6754. $this->assertHtml($expected, $result);
  6755. $this->View->setRequest(
  6756. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6757. );
  6758. $this->Form->create($this->article);
  6759. $result = $this->Form->control('nonexistent_not_validated');
  6760. $expected = [
  6761. 'label' => ['for' => 'nonexistent-not-validated'],
  6762. 'Nonexistent Not Validated',
  6763. '/label',
  6764. 'input' => [
  6765. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6766. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6767. ],
  6768. ];
  6769. $this->assertHtml($expected, $result);
  6770. }
  6771. /**
  6772. * testFormMagicControlLabel method
  6773. */
  6774. public function testFormMagicControlLabel(): void
  6775. {
  6776. $this->getTableLocator()->get('Contacts', [
  6777. 'className' => ContactsTable::class,
  6778. ]);
  6779. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6780. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6781. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6782. $expected = [
  6783. 'label' => ['for' => 'contacts-name'],
  6784. 'My label',
  6785. '/label',
  6786. 'input' => [
  6787. 'type' => 'text',
  6788. 'name' => 'Contacts[name]',
  6789. 'id' => 'contacts-name',
  6790. 'maxlength' => '255',
  6791. ],
  6792. ];
  6793. $this->assertHtml($expected, $result);
  6794. $result = $this->Form->control('name', [
  6795. 'label' => ['class' => 'mandatory'],
  6796. ]);
  6797. $expected = [
  6798. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6799. 'Name',
  6800. '/label',
  6801. 'input' => [
  6802. 'type' => 'text', 'name' => 'name',
  6803. 'id' => 'name', 'maxlength' => '255',
  6804. ],
  6805. ];
  6806. $this->assertHtml($expected, $result);
  6807. $result = $this->Form->control('name', [
  6808. 'div' => false,
  6809. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6810. ]);
  6811. $expected = [
  6812. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6813. 'My label',
  6814. '/label',
  6815. 'input' => [
  6816. 'type' => 'text', 'name' => 'name',
  6817. 'id' => 'name', 'maxlength' => '255',
  6818. ],
  6819. ];
  6820. $this->assertHtml($expected, $result);
  6821. $result = $this->Form->control('Contact.name', [
  6822. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6823. ]);
  6824. $expected = [
  6825. 'label' => ['for' => 'my_id'],
  6826. 'Name',
  6827. '/label',
  6828. 'input' => [
  6829. 'type' => 'text', 'name' => 'Contact[name]',
  6830. 'id' => 'my_id', 'maxlength' => '255',
  6831. ],
  6832. ];
  6833. $this->assertHtml($expected, $result);
  6834. $result = $this->Form->control('1.id');
  6835. $expected = ['input' => [
  6836. 'type' => 'hidden', 'name' => '1[id]',
  6837. 'id' => '1-id',
  6838. ]];
  6839. $this->assertHtml($expected, $result);
  6840. $result = $this->Form->control('1.name');
  6841. $expected = [
  6842. 'label' => ['for' => '1-name'],
  6843. 'Name',
  6844. '/label',
  6845. 'input' => [
  6846. 'type' => 'text', 'name' => '1[name]',
  6847. 'id' => '1-name', 'maxlength' => '255',
  6848. ],
  6849. ];
  6850. $this->assertHtml($expected, $result);
  6851. }
  6852. /**
  6853. * testFormEnd method
  6854. */
  6855. public function testFormEnd(): void
  6856. {
  6857. $this->assertSame('</form>', $this->Form->end());
  6858. }
  6859. /**
  6860. * testMultiRecordForm method
  6861. *
  6862. * Test the generation of fields for a multi record form.
  6863. */
  6864. public function testMultiRecordForm(): void
  6865. {
  6866. $articles = $this->getTableLocator()->get('Articles');
  6867. $articles->hasMany('Comments');
  6868. $comment = new Entity(['comment' => 'Value']);
  6869. $article = new Article(['comments' => [$comment]]);
  6870. $this->Form->create([$article]);
  6871. $result = $this->Form->control('0.comments.1.comment');
  6872. // phpcs:disable
  6873. $expected = [
  6874. 'div' => ['class' => 'input textarea'],
  6875. 'label' => ['for' => '0-comments-1-comment'],
  6876. 'Comment',
  6877. '/label',
  6878. 'textarea' => [
  6879. 'name',
  6880. 'id' => '0-comments-1-comment',
  6881. 'rows' => 5
  6882. ],
  6883. '/textarea',
  6884. '/div'
  6885. ];
  6886. // phpcs:enable
  6887. $this->assertHtml($expected, $result);
  6888. $result = $this->Form->control('0.comments.0.comment');
  6889. // phpcs:disable
  6890. $expected = [
  6891. 'div' => ['class' => 'input textarea'],
  6892. 'label' => ['for' => '0-comments-0-comment'],
  6893. 'Comment',
  6894. '/label',
  6895. 'textarea' => [
  6896. 'name',
  6897. 'id' => '0-comments-0-comment',
  6898. 'rows' => 5
  6899. ],
  6900. 'Value',
  6901. '/textarea',
  6902. '/div'
  6903. ];
  6904. // phpcs:enable
  6905. $this->assertHtml($expected, $result);
  6906. $comment->setError('comment', ['Not valid']);
  6907. $result = $this->Form->control('0.comments.0.comment');
  6908. // phpcs:disable
  6909. $expected = [
  6910. 'div' => ['class' => 'input textarea error'],
  6911. 'label' => ['for' => '0-comments-0-comment'],
  6912. 'Comment',
  6913. '/label',
  6914. 'textarea' => [
  6915. 'name',
  6916. 'class' => 'form-error',
  6917. 'id' => '0-comments-0-comment',
  6918. 'aria-invalid' => 'true',
  6919. 'aria-describedby' => '0-comments-0-comment-error',
  6920. 'rows' => 5
  6921. ],
  6922. 'Value',
  6923. '/textarea',
  6924. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  6925. 'Not valid',
  6926. '/div',
  6927. '/div'
  6928. ];
  6929. // phpcs:enable
  6930. $this->assertHtml($expected, $result);
  6931. $this->getTableLocator()->get('Comments')
  6932. ->getValidator('default')
  6933. ->allowEmptyString('comment', null, false);
  6934. $result = $this->Form->control('0.comments.1.comment');
  6935. // phpcs:disable
  6936. $expected = [
  6937. 'div' => ['class' => 'input textarea required'],
  6938. 'label' => ['for' => '0-comments-1-comment'],
  6939. 'Comment',
  6940. '/label',
  6941. 'textarea' => [
  6942. 'name',
  6943. 'aria-required' => 'true',
  6944. 'required' => 'required',
  6945. 'id' => '0-comments-1-comment',
  6946. 'rows' => 5,
  6947. 'data-validity-message' => 'This field cannot be left empty',
  6948. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  6949. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  6950. ],
  6951. '/textarea',
  6952. '/div'
  6953. ];
  6954. // phpcs:enable
  6955. $this->assertHtml($expected, $result);
  6956. }
  6957. /**
  6958. * testHtml5Controls method
  6959. *
  6960. * Test that some html5 inputs + FormHelper::__call() work.
  6961. */
  6962. public function testHtml5Controls(): void
  6963. {
  6964. $result = $this->Form->email('User.email');
  6965. $expected = [
  6966. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  6967. ];
  6968. $this->assertHtml($expected, $result);
  6969. $result = $this->Form->search('User.query');
  6970. $expected = [
  6971. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  6972. ];
  6973. $this->assertHtml($expected, $result);
  6974. $result = $this->Form->search('User.query', ['value' => 'test']);
  6975. $expected = [
  6976. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  6977. ];
  6978. $this->assertHtml($expected, $result);
  6979. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  6980. $expected = [
  6981. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  6982. ];
  6983. $this->assertHtml($expected, $result);
  6984. }
  6985. /**
  6986. * testHtml5ControlWithControl method
  6987. *
  6988. * Test accessing html5 inputs through control().
  6989. */
  6990. public function testHtml5ControlWithControl(): void
  6991. {
  6992. $this->Form->create();
  6993. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6994. $result = $this->Form->control('website', [
  6995. 'type' => 'url',
  6996. 'val' => 'http://domain.tld',
  6997. 'label' => false,
  6998. ]);
  6999. $expected = [
  7000. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7001. ];
  7002. $this->assertHtml($expected, $result);
  7003. }
  7004. /**
  7005. * testHtml5ControlException method
  7006. *
  7007. * Test errors when field name is missing.
  7008. */
  7009. public function testHtml5ControlException(): void
  7010. {
  7011. $this->expectException(CakeException::class);
  7012. $this->Form->email();
  7013. }
  7014. /**
  7015. * tests fields that are required use custom validation messages
  7016. */
  7017. public function testHtml5ErrorMessage(): void
  7018. {
  7019. $this->Form->setConfig('autoSetCustomValidity', true);
  7020. $validator = (new Validator())
  7021. ->notEmptyString('email', 'Custom error message')
  7022. ->requirePresence('password')
  7023. ->alphaNumeric('password')
  7024. ->notBlank('phone');
  7025. $table = $this->getTableLocator()->get('Contacts', [
  7026. 'className' => ContactsTable::class,
  7027. ]);
  7028. $table->setValidator('default', $validator);
  7029. $contact = new Entity();
  7030. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7031. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7032. $result = $this->Form->control('password');
  7033. $expected = [
  7034. 'label' => ['for' => 'password'],
  7035. 'Password',
  7036. '/label',
  7037. 'input' => [
  7038. 'aria-required' => 'true',
  7039. 'id' => 'password',
  7040. 'name' => 'password',
  7041. 'type' => 'password',
  7042. 'value' => '',
  7043. 'required' => 'required',
  7044. 'data-validity-message' => 'This field cannot be left empty',
  7045. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7046. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7047. ],
  7048. ];
  7049. $this->assertHtml($expected, $result);
  7050. $result = $this->Form->control('phone');
  7051. $expected = [
  7052. 'label' => ['for' => 'phone'],
  7053. 'Phone',
  7054. '/label',
  7055. 'input' => [
  7056. 'aria-required' => 'true',
  7057. 'id' => 'phone',
  7058. 'name' => 'phone',
  7059. 'type' => 'tel',
  7060. 'value' => '',
  7061. 'maxlength' => 255,
  7062. 'required' => 'required',
  7063. 'data-validity-message' => 'This field cannot be left empty',
  7064. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7065. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7066. ],
  7067. ];
  7068. $this->assertHtml($expected, $result);
  7069. $result = $this->Form->control('email');
  7070. $expected = [
  7071. 'label' => ['for' => 'email'],
  7072. 'Email',
  7073. '/label',
  7074. 'input' => [
  7075. 'aria-required' => 'true',
  7076. 'id' => 'email',
  7077. 'name' => 'email',
  7078. 'type' => 'email',
  7079. 'value' => '',
  7080. 'maxlength' => 255,
  7081. 'required' => 'required',
  7082. 'data-validity-message' => 'Custom error message',
  7083. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7084. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7085. ],
  7086. ];
  7087. $this->assertHtml($expected, $result);
  7088. }
  7089. /**
  7090. * tests that custom validation messages are in templateVars
  7091. */
  7092. public function testHtml5ErrorMessageInTemplateVars(): void
  7093. {
  7094. $validator = (new Validator())
  7095. ->notEmptyString('email', 'Custom error "message" & entities')
  7096. ->requirePresence('password')
  7097. ->alphaNumeric('password')
  7098. ->notBlank('phone');
  7099. $table = $this->getTableLocator()->get('Contacts', [
  7100. 'className' => ContactsTable::class,
  7101. ]);
  7102. $table->setValidator('default', $validator);
  7103. $contact = new Entity();
  7104. $this->Form->setConfig('autoSetCustomValidity', false);
  7105. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7106. $this->Form->setTemplates([
  7107. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7108. 'inputContainer' => '{{content}}',
  7109. ]);
  7110. $result = $this->Form->control('password');
  7111. $expected = [
  7112. 'label' => ['for' => 'password'],
  7113. 'Password',
  7114. '/label',
  7115. 'input' => [
  7116. 'aria-required' => 'true',
  7117. 'id' => 'password',
  7118. 'name' => 'password',
  7119. 'type' => 'password',
  7120. 'value' => '',
  7121. 'required' => 'required',
  7122. 'data-message' => 'This field cannot be left empty',
  7123. ],
  7124. ];
  7125. $this->assertHtml($expected, $result);
  7126. $result = $this->Form->control('phone');
  7127. $expected = [
  7128. 'label' => ['for' => 'phone'],
  7129. 'Phone',
  7130. '/label',
  7131. 'input' => [
  7132. 'aria-required' => 'true',
  7133. 'id' => 'phone',
  7134. 'name' => 'phone',
  7135. 'type' => 'tel',
  7136. 'value' => '',
  7137. 'maxlength' => 255,
  7138. 'required' => 'required',
  7139. 'data-message' => 'This field cannot be left empty',
  7140. ],
  7141. ];
  7142. $this->assertHtml($expected, $result);
  7143. $result = $this->Form->control('email', [
  7144. 'templateVars' => [
  7145. 'custom' => 'data-custom="1"',
  7146. ],
  7147. ]);
  7148. $expected = [
  7149. 'label' => ['for' => 'email'],
  7150. 'Email',
  7151. '/label',
  7152. 'input' => [
  7153. 'aria-required' => 'true',
  7154. 'id' => 'email',
  7155. 'name' => 'email',
  7156. 'type' => 'email',
  7157. 'value' => '',
  7158. 'maxlength' => 255,
  7159. 'required' => 'required',
  7160. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7161. 'data-custom' => '1',
  7162. ],
  7163. ];
  7164. $this->assertHtml($expected, $result);
  7165. $this->Form->setConfig('autoSetCustomValidity', true);
  7166. }
  7167. /**
  7168. * testRequiredAttribute method
  7169. *
  7170. * Tests that formhelper sets required attributes.
  7171. */
  7172. public function testRequiredAttribute(): void
  7173. {
  7174. $this->article['required'] = [
  7175. 'title' => true,
  7176. 'body' => false,
  7177. ];
  7178. $this->Form->create($this->article);
  7179. $result = $this->Form->control('title');
  7180. $expected = [
  7181. 'div' => ['class' => 'input text required'],
  7182. 'label' => ['for' => 'title'],
  7183. 'Title',
  7184. '/label',
  7185. 'input' => [
  7186. 'type' => 'text',
  7187. 'name' => 'title',
  7188. 'id' => 'title',
  7189. 'aria-required' => 'true',
  7190. 'required' => 'required',
  7191. 'data-validity-message' => 'This field cannot be left empty',
  7192. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7193. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7194. ],
  7195. '/div',
  7196. ];
  7197. $this->assertHtml($expected, $result);
  7198. $result = $this->Form->control('title', ['required' => false]);
  7199. $this->assertStringNotContainsString('required', $result);
  7200. $result = $this->Form->control('body');
  7201. $expected = [
  7202. 'div' => ['class' => 'input text'],
  7203. 'label' => ['for' => 'body'],
  7204. 'Body',
  7205. '/label',
  7206. 'input' => [
  7207. 'type' => 'text',
  7208. 'name' => 'body',
  7209. 'id' => 'body',
  7210. ],
  7211. '/div',
  7212. ];
  7213. $this->assertHtml($expected, $result);
  7214. $result = $this->Form->control('body', ['required' => true]);
  7215. $this->assertStringContainsString('required', $result);
  7216. }
  7217. /**
  7218. * testControlsNotNested method
  7219. *
  7220. * Tests that it is possible to put inputs outside of the label.
  7221. */
  7222. public function testControlsNotNested(): void
  7223. {
  7224. $this->Form->setTemplates([
  7225. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7226. 'formGroup' => '{{input}}{{label}}',
  7227. ]);
  7228. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7229. $expected = [
  7230. 'div' => ['class' => 'input checkbox'],
  7231. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7232. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7233. 'label' => ['for' => 'foo'],
  7234. 'Foo',
  7235. '/label',
  7236. '/div',
  7237. ];
  7238. $this->assertHtml($expected, $result);
  7239. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7240. $expected = [
  7241. 'div' => ['class' => 'input checkbox'],
  7242. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7243. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7244. '/div',
  7245. ];
  7246. $this->assertHtml($expected, $result);
  7247. $result = $this->Form->control('confirm', [
  7248. 'type' => 'radio',
  7249. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7250. ]);
  7251. $expected = [
  7252. 'div' => ['class' => 'input radio'],
  7253. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7254. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7255. ['label' => ['for' => 'confirm-y']],
  7256. 'Yes',
  7257. '/label',
  7258. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7259. ['label' => ['for' => 'confirm-n']],
  7260. 'No',
  7261. '/label',
  7262. '<label',
  7263. 'Confirm',
  7264. '/label',
  7265. '/div',
  7266. ];
  7267. $this->assertHtml($expected, $result);
  7268. $result = $this->Form->select('category', ['1', '2'], [
  7269. 'multiple' => 'checkbox',
  7270. 'name' => 'fish',
  7271. ]);
  7272. $expected = [
  7273. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7274. ['div' => ['class' => 'checkbox']],
  7275. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7276. ['label' => ['for' => 'fish-0']],
  7277. '1',
  7278. '/label',
  7279. '/div',
  7280. ['div' => ['class' => 'checkbox']],
  7281. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7282. ['label' => ['for' => 'fish-1']],
  7283. '2',
  7284. '/label',
  7285. '/div',
  7286. ];
  7287. $this->assertHtml($expected, $result);
  7288. }
  7289. /**
  7290. * testControlContainerTemplates method
  7291. *
  7292. * Test that *Container templates are used by input.
  7293. */
  7294. public function testControlContainerTemplates(): void
  7295. {
  7296. $this->Form->setTemplates([
  7297. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7298. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7299. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7300. ]);
  7301. $this->article['errors'] = [
  7302. 'Article' => ['published' => 'error message'],
  7303. ];
  7304. $this->Form->create($this->article);
  7305. $result = $this->Form->control('accept', [
  7306. 'type' => 'checkbox',
  7307. ]);
  7308. $expected = [
  7309. 'div' => ['class' => 'check'],
  7310. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7311. 'label' => ['for' => 'accept'],
  7312. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7313. 'Accept',
  7314. '/label',
  7315. '/div',
  7316. ];
  7317. $this->assertHtml($expected, $result);
  7318. $result = $this->Form->control('accept', [
  7319. 'type' => 'radio',
  7320. 'options' => ['Y', 'N'],
  7321. ]);
  7322. $this->assertStringContainsString('<div class="rad">', $result);
  7323. $result = $this->Form->control('Article.published', [
  7324. 'type' => 'radio',
  7325. 'options' => ['Y', 'N'],
  7326. ]);
  7327. $this->assertStringContainsString('<div class="rad err">', $result);
  7328. }
  7329. /**
  7330. * testFormGroupTemplates method
  7331. *
  7332. * Test that *Container templates are used by input.
  7333. */
  7334. public function testFormGroupTemplates(): void
  7335. {
  7336. $this->Form->setTemplates([
  7337. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7338. ]);
  7339. $this->Form->create($this->article);
  7340. $result = $this->Form->control('accept', [
  7341. 'type' => 'radio',
  7342. 'options' => ['Y', 'N'],
  7343. ]);
  7344. $this->assertStringContainsString('<div class="radio">', $result);
  7345. }
  7346. /**
  7347. * testResetTemplates method
  7348. *
  7349. * Test resetting templates.
  7350. */
  7351. public function testResetTemplates(): void
  7352. {
  7353. $this->Form->setTemplates(['input' => '<input/>']);
  7354. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7355. $this->Form->resetTemplates();
  7356. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7357. }
  7358. /**
  7359. * testContext method
  7360. *
  7361. * Test the context method.
  7362. */
  7363. public function testContext(): void
  7364. {
  7365. $result = $this->Form->context();
  7366. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7367. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7368. $this->assertSame($mock, $this->Form->context($mock));
  7369. $this->assertSame($mock, $this->Form->context());
  7370. }
  7371. /**
  7372. * testAutoDomId method
  7373. */
  7374. public function testAutoDomId(): void
  7375. {
  7376. $result = $this->Form->text('field', ['id' => true]);
  7377. $expected = [
  7378. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7379. ];
  7380. $this->assertHtml($expected, $result);
  7381. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7382. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7383. $expected = [
  7384. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7385. ['label' => ['for' => 'field-0']],
  7386. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7387. 'option A',
  7388. '/label',
  7389. ['label' => ['for' => 'field-1']],
  7390. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7391. 'option B',
  7392. '/label',
  7393. ];
  7394. $this->assertHtml($expected, $result);
  7395. $result = $this->Form->select(
  7396. 'multi_field',
  7397. ['first', 'second'],
  7398. ['multiple' => 'checkbox', 'id' => true]
  7399. );
  7400. $expected = [
  7401. 'input' => [
  7402. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7403. ],
  7404. ['div' => ['class' => 'checkbox']],
  7405. ['label' => ['for' => 'multi-field-0']],
  7406. ['input' => [
  7407. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7408. 'value' => '0', 'id' => 'multi-field-0',
  7409. ]],
  7410. 'first',
  7411. '/label',
  7412. '/div',
  7413. ['div' => ['class' => 'checkbox']],
  7414. ['label' => ['for' => 'multi-field-1']],
  7415. ['input' => [
  7416. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7417. 'value' => '1', 'id' => 'multi-field-1',
  7418. ]],
  7419. 'second',
  7420. '/label',
  7421. '/div',
  7422. ];
  7423. $this->assertHtml($expected, $result);
  7424. }
  7425. /**
  7426. * Test the basic setters and getters for value sources
  7427. */
  7428. public function testFormValueSourcesSettersGetters(): void
  7429. {
  7430. $this->View->setRequest($this->View->getRequest()
  7431. ->withData('id', '1')
  7432. ->withQueryParams(['id' => '2']));
  7433. $expected = ['data', 'context'];
  7434. $result = $this->Form->getValueSources();
  7435. $this->assertEquals($expected, $result);
  7436. $this->Form->setValueSources(['context']);
  7437. $result = $this->Form->getSourceValue('id');
  7438. $this->assertNull($result);
  7439. $this->Form->setValueSources('query');
  7440. $expected = ['query'];
  7441. $result = $this->Form->getValueSources();
  7442. $this->assertEquals($expected, $result);
  7443. $expected = '2';
  7444. $result = $this->Form->getSourceValue('id');
  7445. $this->assertSame($expected, $result);
  7446. $this->Form->setValueSources(['data']);
  7447. $expected = '1';
  7448. $result = $this->Form->getSourceValue('id');
  7449. $this->assertSame($expected, $result);
  7450. $this->Form->setValueSources(['query', 'data']);
  7451. $expected = '2';
  7452. $result = $this->Form->getSourceValue('id');
  7453. $this->assertSame($expected, $result);
  7454. }
  7455. public function testValueSourcesValidation(): void
  7456. {
  7457. $this->expectException(InvalidArgumentException::class);
  7458. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7459. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7460. }
  7461. /**
  7462. * Tests the different input rendering values based on sources values switching
  7463. */
  7464. public function testFormValueSourcesSingleSwitchRendering(): void
  7465. {
  7466. $articles = $this->getTableLocator()->get('Articles');
  7467. $article = new Article();
  7468. $articles->patchEntity($article, ['id' => '3']);
  7469. $this->Form->create($article);
  7470. $this->Form->setValueSources(['context']);
  7471. $result = $this->Form->control('id');
  7472. $expected = [
  7473. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7474. ];
  7475. $this->assertHtml($expected, $result);
  7476. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7477. $this->Form->setValueSources(['query']);
  7478. $result = $this->Form->control('id');
  7479. $expected = [
  7480. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7481. ];
  7482. $this->assertHtml($expected, $result);
  7483. $this->View->setRequest($this->View->getRequest()
  7484. ->withQueryParams(['id' => '5a'])
  7485. ->withData('id', '5b'));
  7486. $this->Form->setValueSources(['context']);
  7487. $this->Form->create($article);
  7488. $result = $this->Form->control('id');
  7489. $expected = [
  7490. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7491. ];
  7492. $this->assertHtml($expected, $result);
  7493. $this->Form->setValueSources(['data']);
  7494. $this->Form->create($article);
  7495. $result = $this->Form->control('id');
  7496. $expected = [
  7497. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7498. ];
  7499. $this->assertHtml($expected, $result);
  7500. $this->Form->setValueSources(['query']);
  7501. $result = $this->Form->control('id');
  7502. $expected = [
  7503. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7504. ];
  7505. $this->assertHtml($expected, $result);
  7506. }
  7507. /**
  7508. * Tests the different input rendering values based on sources values switching while supplying
  7509. * an entity (base context) and multiple sources (such as data, query)
  7510. */
  7511. public function testFormValueSourcesListSwitchRendering(): void
  7512. {
  7513. $articles = $this->getTableLocator()->get('Articles');
  7514. $article = new Article();
  7515. $articles->patchEntity($article, ['id' => '3']);
  7516. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7517. $this->Form->create($article);
  7518. $this->Form->setValueSources(['context', 'query']);
  7519. $result = $this->Form->control('id');
  7520. $expected = [
  7521. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7522. ];
  7523. $this->assertHtml($expected, $result);
  7524. $this->Form->setValueSources(['query', 'context']);
  7525. $result = $this->Form->control('id');
  7526. $expected = [
  7527. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7528. ];
  7529. $this->assertHtml($expected, $result);
  7530. $this->Form->setValueSources(['data', 'query', 'context']);
  7531. $result = $this->Form->control('id');
  7532. $expected = [
  7533. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7534. ];
  7535. $this->assertHtml($expected, $result);
  7536. $this->View->setRequest($this->View->getRequest()
  7537. ->withData('id', '8')
  7538. ->withQueryParams(['id' => '9']));
  7539. $this->Form->setValueSources(['data', 'query', 'context']);
  7540. $result = $this->Form->control('id');
  7541. $expected = [
  7542. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7543. ];
  7544. $this->assertHtml($expected, $result);
  7545. }
  7546. /**
  7547. * Test the different form input renderings based on values sources switchings through form options
  7548. */
  7549. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7550. {
  7551. $articles = $this->getTableLocator()->get('Articles');
  7552. $article = new Article();
  7553. $articles->patchEntity($article, ['id' => '3']);
  7554. $this->View->setRequest(
  7555. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7556. );
  7557. $this->Form->create($article, ['valueSources' => 'query']);
  7558. $result = $this->Form->control('id');
  7559. $expected = [
  7560. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7561. ];
  7562. $this->assertHtml($expected, $result);
  7563. $result = $this->Form->getSourceValue('id');
  7564. $this->assertSame('5', $result);
  7565. $this->Form->setValueSources(['context']);
  7566. $this->Form->create($article, ['valueSources' => 'query']);
  7567. $result = $this->Form->control('id');
  7568. $expected = [
  7569. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7570. ];
  7571. $this->assertHtml($expected, $result);
  7572. $result = $this->Form->getSourceValue('id');
  7573. $this->assertSame('5', $result);
  7574. $this->Form->setValueSources(['query']);
  7575. $this->Form->create($article, ['valueSources' => 'data']);
  7576. $result = $this->Form->control('id');
  7577. $expected = [
  7578. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7579. ];
  7580. $this->assertHtml($expected, $result);
  7581. $result = $this->Form->getSourceValue('id');
  7582. $this->assertSame('4', $result);
  7583. $this->Form->setValueSources(['query']);
  7584. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7585. $result = $this->Form->control('id');
  7586. $expected = [
  7587. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7588. ];
  7589. $this->assertHtml($expected, $result);
  7590. $result = $this->Form->getSourceValue('id');
  7591. $this->assertSame(3, $result);
  7592. }
  7593. /**
  7594. * Test the different form input renderings based on values sources switchings through form options
  7595. */
  7596. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7597. {
  7598. $articles = $this->getTableLocator()->get('Articles');
  7599. $article = new Article();
  7600. $articles->patchEntity($article, ['id' => '3']);
  7601. $this->View->setRequest(
  7602. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7603. );
  7604. $this->Form->setValueSources(['context'])
  7605. ->create($article, ['valueSources' => ['query', 'data']]);
  7606. $result = $this->Form->control('id');
  7607. $expected = [
  7608. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7609. ];
  7610. $this->assertHtml($expected, $result);
  7611. $result = $this->Form->getSourceValue('id');
  7612. $this->assertSame('11', $result);
  7613. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7614. $this->Form->setValueSources(['context'])
  7615. ->create($article, ['valueSources' => ['query', 'data']]);
  7616. $result = $this->Form->control('id');
  7617. $expected = [
  7618. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7619. ];
  7620. $this->assertHtml($expected, $result);
  7621. $result = $this->Form->getSourceValue('id');
  7622. $this->assertSame('10', $result);
  7623. }
  7624. /**
  7625. * Test the different form values sources resetting through From::end();
  7626. */
  7627. public function testFormValueSourcesResetViaEnd(): void
  7628. {
  7629. $expected = ['data', 'context'];
  7630. $result = $this->Form->getValueSources();
  7631. $this->assertEquals($expected, $result);
  7632. $expected = ['query', 'context', 'data'];
  7633. $this->Form->setValueSources(['query', 'context', 'data']);
  7634. $result = $this->Form->getValueSources();
  7635. $this->assertEquals($expected, $result);
  7636. $this->Form->create();
  7637. $result = $this->Form->getValueSources();
  7638. $this->assertEquals($expected, $result);
  7639. $this->Form->end();
  7640. $result = $this->Form->getValueSources();
  7641. $this->assertEquals(['data', 'context'], $result);
  7642. }
  7643. /**
  7644. * Test sources values defaults handling
  7645. */
  7646. public function testFormValueSourcesDefaults(): void
  7647. {
  7648. $this->View->setRequest(
  7649. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7650. );
  7651. $this->Form->create();
  7652. $result = $this->Form->password('password');
  7653. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7654. $this->assertHtml($expected, $result);
  7655. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7656. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7657. $this->assertHtml($expected, $result);
  7658. $this->Form->setValueSources('query');
  7659. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7660. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7661. $this->assertHtml($expected, $result);
  7662. $this->Form->setValueSources('data');
  7663. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7664. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7665. $this->assertHtml($expected, $result);
  7666. }
  7667. /**
  7668. * Test sources values schema defaults handling
  7669. */
  7670. public function testSourcesValueDoesntExistPassThrough(): void
  7671. {
  7672. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7673. $articles = $this->getTableLocator()->get('Articles');
  7674. $entity = $articles->newEmptyEntity();
  7675. $this->Form->create($entity);
  7676. $this->Form->setValueSources(['query', 'context']);
  7677. $result = $this->Form->getSourceValue('category');
  7678. $this->assertSame('sesame-cookies', $result);
  7679. $this->Form->setValueSources(['context', 'query']);
  7680. $result = $this->Form->getSourceValue('category');
  7681. $this->assertSame('sesame-cookies', $result);
  7682. }
  7683. /**
  7684. * testNestedLabelInput method
  7685. *
  7686. * Test the `nestedInput` parameter
  7687. */
  7688. public function testNestedLabelInput(): void
  7689. {
  7690. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7691. $expected = [
  7692. 'div' => ['class' => 'input text'],
  7693. 'label' => ['for' => 'foo'],
  7694. ['input' => [
  7695. 'type' => 'text',
  7696. 'name' => 'foo',
  7697. 'id' => 'foo',
  7698. ]],
  7699. 'Foo',
  7700. '/label',
  7701. '/div',
  7702. ];
  7703. $this->assertHtml($expected, $result);
  7704. }
  7705. /**
  7706. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7707. *
  7708. * This test makes sure `false` excludes the label from the render
  7709. */
  7710. public function testControlLabelManipulationDisableLabels(): void
  7711. {
  7712. $result = $this->Form->control('test', [
  7713. 'type' => 'radio',
  7714. 'options' => ['A', 'B'],
  7715. 'labelOptions' => false,
  7716. ]);
  7717. $expected = [
  7718. ['div' => ['class' => 'input radio']],
  7719. '<label',
  7720. 'Test',
  7721. '/label',
  7722. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7723. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7724. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7725. '/div',
  7726. ];
  7727. $this->assertHtml($expected, $result);
  7728. $result = $this->Form->control('checkbox1', [
  7729. 'label' => 'My checkboxes',
  7730. 'multiple' => 'checkbox',
  7731. 'type' => 'select',
  7732. 'options' => [
  7733. ['text' => 'First Checkbox', 'value' => 1],
  7734. ['text' => 'Second Checkbox', 'value' => 2],
  7735. ],
  7736. 'labelOptions' => false,
  7737. ]);
  7738. $expected = [
  7739. ['div' => ['class' => 'input select']],
  7740. ['label' => ['for' => 'checkbox1']],
  7741. 'My checkboxes',
  7742. '/label',
  7743. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7744. ['div' => ['class' => 'checkbox']],
  7745. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7746. '/div',
  7747. ['div' => ['class' => 'checkbox']],
  7748. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7749. '/div',
  7750. '/div',
  7751. ];
  7752. $this->assertHtml($expected, $result);
  7753. }
  7754. /**
  7755. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7756. *
  7757. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7758. * added to the class if checked.
  7759. *
  7760. * Also checks to make sure any custom attributes are rendered correctly
  7761. */
  7762. public function testControlLabelManipulationRadios(): void
  7763. {
  7764. $result = $this->Form->control('test', [
  7765. 'type' => 'radio',
  7766. 'options' => ['A', 'B'],
  7767. 'labelOptions' => ['class' => 'custom-class'],
  7768. ]);
  7769. $expected = [
  7770. ['div' => ['class' => 'input radio']],
  7771. '<label',
  7772. 'Test',
  7773. '/label',
  7774. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7775. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7776. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7777. 'A',
  7778. '/label',
  7779. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7780. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7781. 'B',
  7782. '/label',
  7783. '/div',
  7784. ];
  7785. $this->assertHtml($expected, $result);
  7786. $result = $this->Form->control('test', [
  7787. 'type' => 'radio',
  7788. 'options' => ['A', 'B'],
  7789. 'value' => 1,
  7790. 'labelOptions' => ['class' => 'custom-class'],
  7791. ]);
  7792. $expected = [
  7793. ['div' => ['class' => 'input radio']],
  7794. '<label',
  7795. 'Test',
  7796. '/label',
  7797. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7798. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7799. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7800. 'A',
  7801. '/label',
  7802. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7803. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7804. 'B',
  7805. '/label',
  7806. '/div',
  7807. ];
  7808. $this->assertHtml($expected, $result);
  7809. $result = $this->Form->control('test', [
  7810. 'type' => 'radio',
  7811. 'options' => ['A', 'B'],
  7812. 'value' => 1,
  7813. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7814. ]);
  7815. $expected = [
  7816. ['div' => ['class' => 'input radio']],
  7817. '<label',
  7818. 'Test',
  7819. '/label',
  7820. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7821. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7822. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7823. 'A',
  7824. '/label',
  7825. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7826. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7827. 'B',
  7828. '/label',
  7829. '/div',
  7830. ];
  7831. $this->assertHtml($expected, $result);
  7832. $result = $this->Form->radio('test', ['A', 'B'], [
  7833. 'label' => [
  7834. 'class' => ['custom-class', 'another-class'],
  7835. 'data-name' => 'bob',
  7836. ],
  7837. 'value' => 1,
  7838. ]);
  7839. $expected = [
  7840. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  7841. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7842. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7843. 'A',
  7844. '/label',
  7845. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7846. ['input' => [
  7847. 'type' => 'radio',
  7848. 'name' => 'test',
  7849. 'value' => '1',
  7850. 'id' => 'test-1',
  7851. 'checked' => 'checked',
  7852. ]],
  7853. 'B',
  7854. '/label',
  7855. ];
  7856. $this->assertHtml($expected, $result);
  7857. }
  7858. /**
  7859. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7860. *
  7861. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7862. * added to the class if checked.
  7863. *
  7864. * Also checks to make sure any custom attributes are rendered correctly
  7865. */
  7866. public function testControlLabelManipulationCheckboxes(): void
  7867. {
  7868. $result = $this->Form->control('checkbox1', [
  7869. 'label' => 'My checkboxes',
  7870. 'multiple' => 'checkbox',
  7871. 'type' => 'select',
  7872. 'options' => [
  7873. ['text' => 'First Checkbox', 'value' => 1],
  7874. ['text' => 'Second Checkbox', 'value' => 2],
  7875. ],
  7876. 'labelOptions' => ['class' => 'custom-class'],
  7877. 'value' => ['1'],
  7878. ]);
  7879. $expected = [
  7880. ['div' => ['class' => 'input select']],
  7881. ['label' => ['for' => 'checkbox1']],
  7882. 'My checkboxes',
  7883. '/label',
  7884. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7885. ['div' => ['class' => 'checkbox']],
  7886. ['label' => [
  7887. 'class' => 'custom-class selected',
  7888. 'for' => 'checkbox1-1',
  7889. ]],
  7890. ['input' => [
  7891. 'type' => 'checkbox',
  7892. 'name' => 'checkbox1[]',
  7893. 'value' => '1',
  7894. 'id' => 'checkbox1-1',
  7895. 'checked' => 'checked',
  7896. ]],
  7897. 'First Checkbox',
  7898. '/label',
  7899. '/div',
  7900. ['div' => ['class' => 'checkbox']],
  7901. ['label' => [
  7902. 'class' => 'custom-class',
  7903. 'for' => 'checkbox1-2',
  7904. ]],
  7905. ['input' => [
  7906. 'type' => 'checkbox',
  7907. 'name' => 'checkbox1[]',
  7908. 'value' => '2',
  7909. 'id' => 'checkbox1-2',
  7910. ]],
  7911. 'Second Checkbox',
  7912. '/label',
  7913. '/div',
  7914. '/div',
  7915. ];
  7916. $this->assertHtml($expected, $result);
  7917. $result = $this->Form->control('checkbox1', [
  7918. 'label' => 'My checkboxes',
  7919. 'multiple' => 'checkbox',
  7920. 'type' => 'select',
  7921. 'options' => [
  7922. ['text' => 'First Checkbox', 'value' => 1],
  7923. ['text' => 'Second Checkbox', 'value' => 2],
  7924. ],
  7925. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  7926. 'value' => ['1'],
  7927. ]);
  7928. $expected = [
  7929. ['div' => ['class' => 'input select']],
  7930. ['label' => ['for' => 'checkbox1']],
  7931. 'My checkboxes',
  7932. '/label',
  7933. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7934. ['div' => ['class' => 'checkbox']],
  7935. ['label' => [
  7936. 'class' => 'custom-class another-class selected',
  7937. 'data-name' => 'bob',
  7938. 'for' => 'checkbox1-1',
  7939. ]],
  7940. ['input' => [
  7941. 'type' => 'checkbox',
  7942. 'name' => 'checkbox1[]',
  7943. 'value' => '1',
  7944. 'id' => 'checkbox1-1',
  7945. 'checked' => 'checked',
  7946. ]],
  7947. 'First Checkbox',
  7948. '/label',
  7949. '/div',
  7950. ['div' => ['class' => 'checkbox']],
  7951. ['label' => [
  7952. 'class' => 'custom-class another-class',
  7953. 'data-name' => 'bob',
  7954. 'for' => 'checkbox1-2',
  7955. ]],
  7956. ['input' => [
  7957. 'type' => 'checkbox',
  7958. 'name' => 'checkbox1[]',
  7959. 'value' => '2',
  7960. 'id' => 'checkbox1-2',
  7961. ]],
  7962. 'Second Checkbox',
  7963. '/label',
  7964. '/div',
  7965. '/div',
  7966. ];
  7967. $this->assertHtml($expected, $result);
  7968. }
  7969. /**
  7970. * testControlMaxLengthArrayContext method
  7971. *
  7972. * Test control() with maxlength attribute in Array Context.
  7973. */
  7974. public function testControlMaxLengthArrayContext(): void
  7975. {
  7976. $this->article['schema'] = [
  7977. 'title' => ['length' => 10],
  7978. ];
  7979. $this->Form->create($this->article);
  7980. $result = $this->Form->control('title');
  7981. $expected = [
  7982. 'div' => ['class'],
  7983. 'label' => ['for'],
  7984. 'Title',
  7985. '/label',
  7986. 'input' => [
  7987. 'aria-required' => 'true',
  7988. 'id',
  7989. 'name' => 'title',
  7990. 'type' => 'text',
  7991. 'required' => 'required',
  7992. 'maxlength' => 10,
  7993. 'data-validity-message' => 'This field cannot be left empty',
  7994. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7995. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7996. ],
  7997. '/div',
  7998. ];
  7999. $this->assertHtml($expected, $result);
  8000. }
  8001. /**
  8002. * testControlMaxLengthEntityContext method
  8003. *
  8004. * Test control() with maxlength attribute in Entity Context.
  8005. */
  8006. public function testControlMaxLengthEntityContext(): void
  8007. {
  8008. $this->article['schema']['title']['length'] = 45;
  8009. $validator = new Validator();
  8010. $validator->maxLength('title', 10);
  8011. $article = new EntityContext(
  8012. [
  8013. 'entity' => new Entity($this->article),
  8014. 'table' => new Table([
  8015. 'alias' => 'Articles',
  8016. 'schema' => $this->article['schema'],
  8017. 'validator' => $validator,
  8018. ]),
  8019. ]
  8020. );
  8021. $this->Form->create($article);
  8022. $result = $this->Form->control('title');
  8023. $expected = [
  8024. 'div' => ['class'],
  8025. 'label' => ['for'],
  8026. 'Title',
  8027. '/label',
  8028. 'input' => [
  8029. 'aria-required' => 'true',
  8030. 'id',
  8031. 'name' => 'title',
  8032. 'type' => 'text',
  8033. 'required' => 'required',
  8034. 'maxlength' => 10,
  8035. 'data-validity-message' => 'This field cannot be left empty',
  8036. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8037. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8038. ],
  8039. '/div',
  8040. ];
  8041. $this->assertHtml($expected, $result);
  8042. $this->article['schema']['title']['length'] = 45;
  8043. $validator = new Validator();
  8044. $validator->maxLength('title', 55);
  8045. $article = new EntityContext(
  8046. [
  8047. 'entity' => new Entity($this->article),
  8048. 'table' => new Table([
  8049. 'schema' => $this->article['schema'],
  8050. 'validator' => $validator,
  8051. 'alias' => 'Articles',
  8052. ]),
  8053. ]
  8054. );
  8055. $this->Form->create($article);
  8056. $result = $this->Form->control('title');
  8057. $expected = [
  8058. 'div' => ['class'],
  8059. 'label' => ['for'],
  8060. 'Title',
  8061. '/label',
  8062. 'input' => [
  8063. 'aria-required' => 'true',
  8064. 'id',
  8065. 'name' => 'title',
  8066. 'type' => 'text',
  8067. 'required' => 'required',
  8068. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8069. 'data-validity-message' => 'This field cannot be left empty',
  8070. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8071. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8072. ],
  8073. '/div',
  8074. ];
  8075. $this->assertHtml($expected, $result);
  8076. $this->article['schema']['title']['length'] = 45;
  8077. $validator = new Validator();
  8078. $validator->maxLength('title', 55);
  8079. $article = new EntityContext(
  8080. [
  8081. 'entity' => new Entity($this->article),
  8082. 'table' => new Table([
  8083. 'schema' => $this->article['schema'],
  8084. 'validator' => $validator,
  8085. 'alias' => 'Articles',
  8086. ]),
  8087. ]
  8088. );
  8089. $this->Form->create($article);
  8090. $result = $this->Form->control('title', ['maxlength' => 10]);
  8091. $expected = [
  8092. 'div' => ['class'],
  8093. 'label' => ['for'],
  8094. 'Title',
  8095. '/label',
  8096. 'input' => [
  8097. 'aria-required' => 'true',
  8098. 'id',
  8099. 'name' => 'title',
  8100. 'type' => 'text',
  8101. 'required' => 'required',
  8102. 'maxlength' => 10, // Length set in options should take highest precedence.
  8103. 'data-validity-message' => 'This field cannot be left empty',
  8104. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8105. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8106. ],
  8107. '/div',
  8108. ];
  8109. $this->assertHtml($expected, $result);
  8110. }
  8111. /**
  8112. * testControlMinMaxLengthEntityContext method
  8113. *
  8114. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8115. */
  8116. public function testControlMinMaxLengthEntityContext(): void
  8117. {
  8118. $validator = new Validator();
  8119. $validator->maxLength('title', 10);
  8120. $article = new EntityContext(
  8121. [
  8122. 'entity' => new Entity($this->article),
  8123. 'table' => new Table([
  8124. 'alias' => 'Articles',
  8125. 'schema' => $this->article['schema'],
  8126. 'validator' => $validator,
  8127. ]),
  8128. ]
  8129. );
  8130. $this->Form->create($article);
  8131. $result = $this->Form->control('title');
  8132. $expected = [
  8133. 'div' => ['class'],
  8134. 'label' => ['for'],
  8135. 'Title',
  8136. '/label',
  8137. 'input' => [
  8138. 'aria-required' => 'true',
  8139. 'id',
  8140. 'name' => 'title',
  8141. 'type' => 'text',
  8142. 'required' => 'required',
  8143. 'maxlength' => 10,
  8144. 'data-validity-message' => 'This field cannot be left empty',
  8145. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8146. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8147. ],
  8148. '/div',
  8149. ];
  8150. $this->assertHtml($expected, $result);
  8151. }
  8152. /**
  8153. * testControlMaxLengthFormContext method
  8154. *
  8155. * Test control() with maxlength attribute in Form Context.
  8156. */
  8157. public function testControlMaxLengthFormContext(): void
  8158. {
  8159. $validator = new Validator();
  8160. $validator->maxLength('title', 10);
  8161. $form = new Form();
  8162. $form->setValidator('default', $validator);
  8163. $this->Form->create($form);
  8164. $result = $this->Form->control('title');
  8165. $expected = [
  8166. 'div' => ['class'],
  8167. 'label' => ['for'],
  8168. 'Title',
  8169. '/label',
  8170. 'input' => [
  8171. 'aria-required' => 'true',
  8172. 'id',
  8173. 'name' => 'title',
  8174. 'type' => 'text',
  8175. 'required' => 'required',
  8176. 'maxlength' => 10,
  8177. 'data-validity-message' => 'This field cannot be left empty',
  8178. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8179. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8180. ],
  8181. '/div',
  8182. ];
  8183. $this->assertHtml($expected, $result);
  8184. }
  8185. }