FormHelperTest.php 313 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127
  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\Chronos\ChronosTime;
  19. use Cake\Collection\Collection;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Exception\CakeException;
  22. use Cake\Database\Type\EnumType;
  23. use Cake\Form\Form;
  24. use Cake\Http\ServerRequest;
  25. use Cake\I18n\Date;
  26. use Cake\I18n\DateTime;
  27. use Cake\ORM\Entity;
  28. use Cake\ORM\Table;
  29. use Cake\Routing\Router;
  30. use Cake\TestSuite\TestCase;
  31. use Cake\Utility\Security;
  32. use Cake\Validation\Validator;
  33. use Cake\View\Form\ArrayContext;
  34. use Cake\View\Form\ContextInterface;
  35. use Cake\View\Form\EntityContext;
  36. use Cake\View\Form\FormContext;
  37. use Cake\View\Form\NullContext;
  38. use Cake\View\Helper\FormHelper;
  39. use Cake\View\View;
  40. use Cake\View\Widget\LabelWidget;
  41. use Cake\View\Widget\WidgetInterface;
  42. use Cake\View\Widget\WidgetLocator;
  43. use InvalidArgumentException;
  44. use PHPUnit\Framework\Attributes\DataProvider;
  45. use ReflectionProperty;
  46. use TestApp\Model\Entity\Article;
  47. use TestApp\Model\Enum\ArticleStatus;
  48. use TestApp\Model\Enum\ArticleStatusLabel;
  49. use TestApp\Model\Enum\ArticleStatusLabelInterface;
  50. use TestApp\Model\Enum\Priority;
  51. use TestApp\Model\Table\ContactsTable;
  52. use TestApp\Model\Table\ValidateUsersTable;
  53. use TestApp\View\Form\StubContext;
  54. /**
  55. * FormHelperTest class
  56. *
  57. * @property \Cake\View\Helper\FormHelper $Form
  58. * @property \Cake\View\View $View
  59. */
  60. class FormHelperTest extends TestCase
  61. {
  62. /**
  63. * Fixtures to be used
  64. *
  65. * @var list<string>
  66. */
  67. protected array $fixtures = ['core.Articles', 'core.Comments'];
  68. /**
  69. * @var array
  70. */
  71. protected $article = [];
  72. /**
  73. * @var string
  74. */
  75. protected $url;
  76. /**
  77. * @var \Cake\View\Helper\FormHelper
  78. */
  79. protected $Form;
  80. /**
  81. * @var \Cake\View\View
  82. */
  83. protected $View;
  84. /**
  85. * setUp method
  86. */
  87. public function setUp(): void
  88. {
  89. parent::setUp();
  90. Configure::write('Config.language', 'eng');
  91. Configure::write('App.base', '');
  92. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  93. $request = new ServerRequest([
  94. 'webroot' => '',
  95. 'base' => '',
  96. 'url' => '/articles/add',
  97. 'params' => [
  98. 'controller' => 'Articles',
  99. 'action' => 'add',
  100. 'plugin' => null,
  101. ],
  102. ]);
  103. $this->View = new View($request);
  104. Router::reload();
  105. Router::setRequest($request);
  106. $this->url = '/articles/add';
  107. $this->Form = new FormHelper($this->View);
  108. $this->article = [
  109. 'schema' => [
  110. 'id' => ['type' => 'integer'],
  111. 'author_id' => ['type' => 'integer', 'null' => true],
  112. 'title' => ['type' => 'string', 'null' => true],
  113. 'body' => 'text',
  114. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  115. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  116. ],
  117. 'required' => [
  118. 'author_id' => true,
  119. 'title' => true,
  120. ],
  121. ];
  122. Security::setSalt('foo!');
  123. $builder = Router::createRouteBuilder('/');
  124. $builder->connect('/{controller}', ['action' => 'index']);
  125. $builder->connect('/{controller}/{action}/*');
  126. }
  127. /**
  128. * tearDown method
  129. */
  130. public function tearDown(): void
  131. {
  132. parent::tearDown();
  133. unset($this->Form, $this->View);
  134. }
  135. /**
  136. * Test construct() with the templates option.
  137. */
  138. public function testConstructTemplatesFile(): void
  139. {
  140. $helper = new FormHelper($this->View, [
  141. 'templates' => 'htmlhelper_tags',
  142. ]);
  143. $result = $helper->control('name');
  144. $this->assertStringContainsString('<input', $result);
  145. }
  146. /**
  147. * Test that when specifying custom widgets the config array for that widget
  148. * is overwritten instead of merged.
  149. */
  150. public function testConstructWithWidgets(): void
  151. {
  152. $config = [
  153. 'widgets' => [
  154. 'datetime' => [LabelWidget::class, 'select'],
  155. ],
  156. ];
  157. $helper = new FormHelper($this->View, $config);
  158. $locator = $helper->getWidgetLocator();
  159. $this->assertInstanceOf(LabelWidget::class, $locator->get('datetime'));
  160. }
  161. /**
  162. * Test that when specifying custom widgets config file and it should be
  163. * added to widgets array. WidgetLocator will load widgets in constructor.
  164. */
  165. public function testConstructWithWidgetsConfig(): void
  166. {
  167. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  168. $locator = $helper->getWidgetLocator();
  169. $this->assertInstanceOf(LabelWidget::class, $locator->get('text'));
  170. }
  171. /**
  172. * Test setting the widget locator
  173. */
  174. public function testSetAndGetWidgetLocator(): void
  175. {
  176. $helper = new FormHelper($this->View);
  177. $locator = new WidgetLocator($helper->templater(), $this->View);
  178. $helper->setWidgetLocator($locator);
  179. $this->assertSame($locator, $helper->getWidgetLocator());
  180. }
  181. /**
  182. * Test overridding grouped input types which controls generation of "for"
  183. * attribute of labels.
  184. */
  185. public function testConstructWithGroupedInputTypes(): void
  186. {
  187. $helper = new FormHelper($this->View, [
  188. 'groupedInputTypes' => ['radio'],
  189. ]);
  190. $result = $helper->control('when', ['type' => 'datetime-local']);
  191. $this->assertStringContainsString('<label for="when">When</label>', $result);
  192. }
  193. /**
  194. * Test registering a new widget class and rendering it.
  195. */
  196. public function testAddWidgetAndRenderWidget(): void
  197. {
  198. $widget = new class implements WidgetInterface
  199. {
  200. public function render(array $data, ContextInterface $context): string
  201. {
  202. return 'HTML';
  203. }
  204. public function secureFields(array $data): array
  205. {
  206. return ['val'];
  207. }
  208. };
  209. $this->Form->addWidget('test', $widget);
  210. $result = $this->Form->widget('test', ['val' => 1]);
  211. $this->assertSame('HTML', $result);
  212. }
  213. /**
  214. * Test that secureFields() of widget is called after calling render(),
  215. * not before.
  216. */
  217. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  218. {
  219. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  220. 'unlockedFields' => [],
  221. ]));
  222. $widget = new class implements WidgetInterface
  223. {
  224. public function render(array $data, ContextInterface $context): string
  225. {
  226. return 'HTML';
  227. }
  228. public function secureFields(array $data): array
  229. {
  230. return ['test'];
  231. }
  232. };
  233. $this->Form->addWidget('test', $widget);
  234. $this->Form->create();
  235. $result = $this->Form->widget('test', ['val' => 1, 'name' => 'test', 'secure' => true]);
  236. $this->assertSame('HTML', $result);
  237. }
  238. /**
  239. * Test that empty string is not added to secure fields list when
  240. * rendering input widget without name.
  241. */
  242. public function testRenderingWidgetWithEmptyName(): void
  243. {
  244. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  245. $this->Form->create();
  246. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  247. $this->assertSame('<select name=""></select>', $result);
  248. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  249. $this->assertEquals([], $result);
  250. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  251. $this->assertSame('<select name="0"></select>', $result);
  252. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  253. $this->assertEquals(['0'], $result);
  254. }
  255. /**
  256. * Test adding a new context class.
  257. */
  258. public function testAddContextProvider(): void
  259. {
  260. $context = 'My data';
  261. $stub = new StubContext();
  262. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  263. $this->assertInstanceOf(ServerRequest::class, $request);
  264. $this->assertSame($context, $data['entity']);
  265. return $stub;
  266. });
  267. $this->Form->create($context);
  268. $result = $this->Form->context();
  269. $this->assertSame($stub, $result);
  270. }
  271. /**
  272. * Test replacing a context class.
  273. */
  274. public function testAddContextProviderReplace(): void
  275. {
  276. $entity = new Article();
  277. $stub = new StubContext();
  278. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  279. return $stub;
  280. });
  281. $this->Form->create($entity);
  282. $result = $this->Form->context();
  283. $this->assertSame($stub, $result);
  284. }
  285. /**
  286. * Test overriding a context class.
  287. */
  288. public function testAddContextProviderAdd(): void
  289. {
  290. $entity = new Article();
  291. $stub = new StubContext();
  292. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  293. if ($data['entity'] instanceof Entity) {
  294. return $stub;
  295. }
  296. });
  297. $this->Form->create($entity);
  298. $result = $this->Form->context();
  299. $this->assertSame($stub, $result);
  300. }
  301. /**
  302. * Provides context options for create().
  303. *
  304. * @return array
  305. */
  306. public static function contextSelectionProvider(): array
  307. {
  308. $entity = new Article();
  309. $collection = new Collection([$entity]);
  310. $emptyCollection = new Collection([]);
  311. $data = [
  312. 'schema' => [
  313. 'title' => ['type' => 'string'],
  314. ],
  315. ];
  316. $form = new Form();
  317. $custom = new StubContext();
  318. return [
  319. 'entity' => [$entity, EntityContext::class],
  320. 'collection' => [$collection, EntityContext::class],
  321. 'empty_collection' => [$emptyCollection, NullContext::class],
  322. 'array' => [$data, ArrayContext::class],
  323. 'form' => [$form, FormContext::class],
  324. 'none' => [null, NullContext::class],
  325. 'custom' => [$custom, $custom::class],
  326. ];
  327. }
  328. /**
  329. * Test default context selection in create()
  330. *
  331. * @param mixed $data
  332. */
  333. #[DataProvider('contextSelectionProvider')]
  334. public function testCreateContextSelectionBuiltIn($data, string $class): void
  335. {
  336. $this->Form->create($data);
  337. $this->assertInstanceOf($class, $this->Form->context());
  338. }
  339. /**
  340. * Data provider for type option.
  341. *
  342. * @return array
  343. */
  344. public static function requestTypeProvider(): array
  345. {
  346. return [
  347. // type, method, override
  348. ['post', 'post', 'POST'],
  349. ['put', 'post', 'PUT'],
  350. ['patch', 'post', 'PATCH'],
  351. ['delete', 'post', 'DELETE'],
  352. ];
  353. }
  354. /**
  355. * Test creating file forms.
  356. */
  357. public function testCreateFile(): void
  358. {
  359. $encoding = strtolower(Configure::read('App.encoding'));
  360. $result = $this->Form->create(null, ['type' => 'file']);
  361. $expected = [
  362. 'form' => [
  363. 'method' => 'post', 'action' => '/articles/add',
  364. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  365. ],
  366. 'div' => ['style' => 'display:none;'],
  367. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  368. '/div',
  369. ];
  370. $this->assertHtml($expected, $result);
  371. }
  372. /**
  373. * Test creating GET forms.
  374. */
  375. public function testCreateGet(): void
  376. {
  377. $encoding = strtolower(Configure::read('App.encoding'));
  378. $result = $this->Form->create(null, ['type' => 'get']);
  379. $expected = ['form' => [
  380. 'method' => 'get', 'action' => '/articles/add',
  381. 'accept-charset' => $encoding,
  382. ]];
  383. $this->assertHtml($expected, $result);
  384. }
  385. /**
  386. * Test explicit method/enctype options.
  387. *
  388. * Explicit method overwrites inferred method from 'type'
  389. */
  390. public function testCreateExplicitMethodEnctype(): void
  391. {
  392. $encoding = strtolower(Configure::read('App.encoding'));
  393. $result = $this->Form->create(null, [
  394. 'type' => 'get',
  395. 'method' => 'put',
  396. 'enctype' => 'multipart/form-data',
  397. ]);
  398. $expected = ['form' => [
  399. 'method' => 'put',
  400. 'action' => '/articles/add',
  401. 'enctype' => 'multipart/form-data',
  402. 'accept-charset' => $encoding,
  403. ]];
  404. $this->assertHtml($expected, $result);
  405. }
  406. /**
  407. * Test create() with the templates option.
  408. */
  409. public function testCreateTemplatesArray(): void
  410. {
  411. $result = $this->Form->create($this->article, [
  412. 'templates' => [
  413. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  414. ],
  415. ]);
  416. $expected = [
  417. 'form' => [
  418. 'class' => 'form-horizontal',
  419. 'method' => 'post',
  420. 'action' => '/articles/add',
  421. 'accept-charset' => 'utf-8',
  422. ],
  423. ];
  424. $this->assertHtml($expected, $result);
  425. }
  426. /**
  427. * Test create() with the templates option.
  428. */
  429. public function testCreateTemplatesFile(): void
  430. {
  431. $result = $this->Form->create($this->article, [
  432. 'templates' => 'htmlhelper_tags',
  433. ]);
  434. $expected = [
  435. 'start form',
  436. ];
  437. $this->assertHtml($expected, $result);
  438. }
  439. /**
  440. * Test that create() and end() restore templates.
  441. */
  442. public function testCreateEndRestoreTemplates(): void
  443. {
  444. $this->Form->create($this->article, [
  445. 'templates' => ['input' => 'custom input element'],
  446. ]);
  447. $this->Form->end();
  448. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  449. }
  450. /**
  451. * Test create() with the templates option.
  452. */
  453. public function testCreateTemplatesRequiredClass(): void
  454. {
  455. $this->Form->create($this->article, [
  456. 'templates' => [
  457. 'requiredClass' => 'is-required',
  458. ],
  459. ]);
  460. $result = $this->Form->control('title');
  461. $expected = [
  462. 'div' => ['class' => 'input text is-required'],
  463. 'label' => ['for' => 'title'],
  464. 'Title',
  465. '/label',
  466. 'input' => [
  467. 'type' => 'text',
  468. 'name' => 'title',
  469. 'id' => 'title',
  470. 'required' => 'required',
  471. 'data-validity-message' => 'This field cannot be left empty',
  472. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  473. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  474. ],
  475. '/div',
  476. ];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test using template vars in various templates used by control() method.
  481. */
  482. public function testControlTemplateVars(): void
  483. {
  484. $result = $this->Form->control('text', [
  485. 'templates' => [
  486. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  487. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  488. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  489. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  490. ],
  491. 'templateVars' => [
  492. 'forinput' => 'in-input',
  493. 'forlabel' => 'in-label',
  494. 'forgroup' => 'in-group',
  495. 'forcontainer' => 'in-container',
  496. ],
  497. ]);
  498. $expected = [
  499. 'div' => ['class'],
  500. 'label' => ['for'],
  501. 'Text in-label',
  502. '/label',
  503. 'in-group',
  504. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  505. 'in-container',
  506. '/div',
  507. ];
  508. $this->assertHtml($expected, $result);
  509. }
  510. /**
  511. * Test ensuring template variables work in template files loaded
  512. * during control().
  513. */
  514. public function testControlTemplatesFromFile(): void
  515. {
  516. $result = $this->Form->control('title', [
  517. 'templates' => 'test_templates',
  518. 'templateVars' => [
  519. 'forcontainer' => 'container-data',
  520. ],
  521. ]);
  522. $expected = [
  523. 'div' => ['class'],
  524. 'label' => ['for'],
  525. 'Title',
  526. '/label',
  527. 'input' => ['name', 'type' => 'text', 'id'],
  528. 'container-data',
  529. '/div',
  530. ];
  531. $this->assertHtml($expected, $result);
  532. }
  533. /**
  534. * Test using template vars in inputSubmit and submitContainer template.
  535. */
  536. public function testSubmitTemplateVars(): void
  537. {
  538. $this->Form->setTemplates([
  539. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  540. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  541. ]);
  542. $result = $this->Form->submit('Submit', [
  543. 'templateVars' => [
  544. 'forinput' => 'in-input',
  545. 'forcontainer' => 'in-container',
  546. ],
  547. ]);
  548. $expected = [
  549. 'div' => ['class'],
  550. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  551. 'in-container',
  552. '/div',
  553. ];
  554. $this->assertHtml($expected, $result);
  555. }
  556. /**
  557. * test the create() method
  558. */
  559. #[DataProvider('requestTypeProvider')]
  560. public function testCreateTypeOptions(string $type, string $method, string $override): void
  561. {
  562. $encoding = strtolower(Configure::read('App.encoding'));
  563. $result = $this->Form->create(null, ['type' => $type]);
  564. $expected = [
  565. 'form' => [
  566. 'method' => $method, 'action' => '/articles/add',
  567. 'accept-charset' => $encoding,
  568. ],
  569. ];
  570. $extra = [
  571. 'div' => ['style' => 'display:none;'],
  572. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  573. '/div',
  574. ];
  575. if ($type !== 'post') {
  576. $expected = array_merge($expected, $extra);
  577. }
  578. $this->assertHtml($expected, $result);
  579. }
  580. /**
  581. * Test using template vars in Create (formStart template)
  582. */
  583. public function testCreateTemplateVars(): void
  584. {
  585. $result = $this->Form->create($this->article, [
  586. 'templates' => [
  587. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  588. ],
  589. 'templateVars' => ['header' => 'headertext'],
  590. ]);
  591. $expected = [
  592. 'h4' => ['class'],
  593. 'headertext',
  594. '/h4',
  595. 'form' => [
  596. 'method' => 'post',
  597. 'action' => '/articles/add',
  598. 'accept-charset' => 'utf-8',
  599. ],
  600. ];
  601. $this->assertHtml($expected, $result);
  602. }
  603. /**
  604. * Test opening a form for an update operation.
  605. */
  606. public function testCreateUpdateForm(): void
  607. {
  608. $encoding = strtolower(Configure::read('App.encoding'));
  609. $this->View->setRequest($this->View->getRequest()
  610. ->withRequestTarget('/articles/edit/1')
  611. ->withParam('action', 'edit'));
  612. $this->article['defaults']['id'] = 1;
  613. $result = $this->Form->create($this->article);
  614. $expected = [
  615. 'form' => [
  616. 'method' => 'post',
  617. 'action' => '/articles/edit/1',
  618. 'accept-charset' => $encoding,
  619. ],
  620. 'div' => ['style' => 'display:none;'],
  621. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  622. '/div',
  623. ];
  624. $this->assertHtml($expected, $result);
  625. }
  626. /**
  627. * test create() with automatic url generation
  628. */
  629. public function testCreateAutoUrl(): void
  630. {
  631. $encoding = strtolower(Configure::read('App.encoding'));
  632. $this->View->setRequest($this->View->getRequest()
  633. ->withRequestTarget('/articles/delete/10')
  634. ->withParam('action', 'delete'));
  635. $result = $this->Form->create($this->article);
  636. $expected = [
  637. 'form' => [
  638. 'method' => 'post', 'action' => '/articles/delete/10',
  639. 'accept-charset' => $encoding,
  640. ],
  641. ];
  642. $this->assertHtml($expected, $result);
  643. $this->article['defaults'] = ['id' => 1];
  644. $this->View->setRequest($this->View->getRequest()
  645. ->withRequestTarget('/Articles/edit/1')
  646. ->withParam('action', 'delete'));
  647. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  648. $expected = [
  649. 'form' => [
  650. 'method' => 'post',
  651. 'action' => '/Articles/edit/1',
  652. 'accept-charset' => $encoding,
  653. ],
  654. 'div' => ['style' => 'display:none;'],
  655. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  656. '/div',
  657. ];
  658. $this->assertHtml($expected, $result);
  659. $this->View->setRequest($this->View->getRequest()
  660. ->withParam('action', 'add'));
  661. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  662. $expected = [
  663. 'form' => [
  664. 'method' => 'post',
  665. 'action' => '/Articles/publish/1',
  666. 'accept-charset' => $encoding,
  667. ],
  668. 'div' => ['style' => 'display:none;'],
  669. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  670. '/div',
  671. ];
  672. $this->assertHtml($expected, $result);
  673. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  674. $expected = [
  675. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  676. 'div' => ['style' => 'display:none;'],
  677. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  678. '/div',
  679. ];
  680. $this->assertHtml($expected, $result);
  681. $this->View->setRequest($this->View->getRequest()
  682. ->withParam('controller', 'Pages'));
  683. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  684. $expected = [
  685. 'form' => [
  686. 'method' => 'post', 'action' => '/Pages/signup/1',
  687. 'accept-charset' => $encoding,
  688. ],
  689. 'div' => ['style' => 'display:none;'],
  690. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  691. '/div',
  692. ];
  693. $this->assertHtml($expected, $result);
  694. }
  695. /**
  696. * Test create() with no URL (no "action" attribute for <form> tag)
  697. */
  698. public function testCreateNoUrl(): void
  699. {
  700. $result = $this->Form->create(null, ['url' => false]);
  701. $expected = [
  702. 'form' => [
  703. 'method' => 'post',
  704. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  705. ],
  706. ];
  707. $this->assertHtml($expected, $result);
  708. }
  709. /**
  710. * test create() with a custom route
  711. */
  712. public function testCreateCustomRoute(): void
  713. {
  714. $builder = Router::createRouteBuilder('/');
  715. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  716. $encoding = strtolower(Configure::read('App.encoding'));
  717. $this->View->setRequest($this->View->getRequest()
  718. ->withParam('controller', 'Users'));
  719. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  720. $expected = [
  721. 'form' => [
  722. 'method' => 'post', 'action' => '/login',
  723. 'accept-charset' => $encoding,
  724. ],
  725. ];
  726. $this->assertHtml($expected, $result);
  727. $builder->connect(
  728. '/new-article',
  729. ['controller' => 'Articles', 'action' => 'myAction'],
  730. ['_name' => 'my-route']
  731. );
  732. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  733. $expected = [
  734. 'form' => [
  735. 'method' => 'post', 'action' => '/new-article',
  736. 'accept-charset' => $encoding,
  737. ],
  738. ];
  739. $this->assertHtml($expected, $result);
  740. }
  741. /**
  742. * test automatic accept-charset overriding
  743. */
  744. public function testCreateWithAcceptCharset(): void
  745. {
  746. $result = $this->Form->create(
  747. $this->article,
  748. [
  749. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  750. ]
  751. );
  752. $expected = [
  753. 'form' => [
  754. 'method' => 'post', 'action' => '/Articles',
  755. 'accept-charset' => 'iso-8859-1',
  756. ],
  757. ];
  758. $this->assertHtml($expected, $result);
  759. }
  760. /**
  761. * Test base form URL when 'url' param is passed with multiple parameters (&)
  762. */
  763. public function testCreateQueryStringRequest(): void
  764. {
  765. $encoding = strtolower(Configure::read('App.encoding'));
  766. $result = $this->Form->create($this->article, [
  767. 'type' => 'post',
  768. 'escape' => false,
  769. 'url' => [
  770. 'controller' => 'Controller',
  771. 'action' => 'action',
  772. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  773. ],
  774. ]);
  775. $expected = [
  776. 'form' => [
  777. 'method' => 'post',
  778. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  779. 'accept-charset' => $encoding,
  780. ],
  781. ];
  782. $this->assertHtml($expected, $result);
  783. $result = $this->Form->create($this->article, [
  784. 'type' => 'post',
  785. 'url' => [
  786. 'controller' => 'Controller',
  787. 'action' => 'action',
  788. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  789. ],
  790. ]);
  791. $this->assertHtml($expected, $result);
  792. }
  793. /**
  794. * test that create() doesn't cause errors by multiple id's being in the primary key
  795. * as could happen with multiple select or checkboxes.
  796. */
  797. public function testCreateWithMultipleIdInData(): void
  798. {
  799. $encoding = strtolower(Configure::read('App.encoding'));
  800. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  801. $result = $this->Form->create($this->article);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post',
  805. 'action' => '/articles/add',
  806. 'accept-charset' => $encoding,
  807. ],
  808. ];
  809. $this->assertHtml($expected, $result);
  810. }
  811. /**
  812. * test that create() doesn't add in extra passed params.
  813. */
  814. public function testCreatePassedArgs(): void
  815. {
  816. $encoding = strtolower(Configure::read('App.encoding'));
  817. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  818. $result = $this->Form->create($this->article, [
  819. 'type' => 'post',
  820. 'escape' => false,
  821. 'url' => [
  822. 'action' => 'edit',
  823. 'myparam',
  824. ],
  825. ]);
  826. $expected = [
  827. 'form' => [
  828. 'method' => 'post',
  829. 'action' => '/Articles/edit/myparam',
  830. 'accept-charset' => $encoding,
  831. ],
  832. ];
  833. $this->assertHtml($expected, $result);
  834. }
  835. /**
  836. * test creating a get form, and get form inputs.
  837. */
  838. public function testGetFormCreate(): void
  839. {
  840. $encoding = strtolower(Configure::read('App.encoding'));
  841. $result = $this->Form->create($this->article, ['type' => 'get']);
  842. $expected = ['form' => [
  843. 'method' => 'get', 'action' => '/articles/add',
  844. 'accept-charset' => $encoding,
  845. ]];
  846. $this->assertHtml($expected, $result);
  847. $result = $this->Form->text('title');
  848. $expected = ['input' => [
  849. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  850. ]];
  851. $this->assertHtml($expected, $result);
  852. $result = $this->Form->password('password');
  853. $expected = ['input' => [
  854. 'name' => 'password', 'type' => 'password',
  855. ]];
  856. $this->assertHtml($expected, $result);
  857. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  858. $result = $this->Form->text('user_form');
  859. $expected = ['input' => [
  860. 'name' => 'user_form', 'type' => 'text',
  861. ]];
  862. $this->assertHtml($expected, $result);
  863. }
  864. /**
  865. * test get form, and inputs when the model param is false
  866. */
  867. public function testGetFormWithFalseModel(): void
  868. {
  869. $encoding = strtolower(Configure::read('App.encoding'));
  870. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  871. $result = $this->Form->create(null, [
  872. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  873. ]);
  874. $expected = ['form' => [
  875. 'method' => 'get', 'action' => '/ContactTest/add',
  876. 'accept-charset' => $encoding,
  877. ]];
  878. $this->assertHtml($expected, $result);
  879. $result = $this->Form->text('reason');
  880. $expected = [
  881. 'input' => ['type' => 'text', 'name' => 'reason'],
  882. ];
  883. $this->assertHtml($expected, $result);
  884. }
  885. /**
  886. * testFormCreateWithSecurity method
  887. *
  888. * Test form->create() with security key.
  889. */
  890. public function testCreateWithSecurity(): void
  891. {
  892. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  893. $encoding = strtolower(Configure::read('App.encoding'));
  894. $result = $this->Form->create($this->article, [
  895. 'url' => '/articles/publish',
  896. ]);
  897. $expected = [
  898. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  899. 'div' => ['style' => 'display:none;'],
  900. ['input' => [
  901. 'type' => 'hidden',
  902. 'name' => '_csrfToken',
  903. 'value' => 'testKey',
  904. ]],
  905. '/div',
  906. ];
  907. $this->assertHtml($expected, $result);
  908. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  909. $expected['form']['id'] = 'MyForm';
  910. $this->assertHtml($expected, $result);
  911. }
  912. /**
  913. * testFormCreateGetNoSecurity method
  914. *
  915. * Test form->create() with no security key as its a get form
  916. */
  917. public function testCreateEndGetNoSecurity(): void
  918. {
  919. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  920. $article = new Article();
  921. $result = $this->Form->create($article, [
  922. 'type' => 'get',
  923. 'url' => '/contacts/add',
  924. ]);
  925. $this->assertStringNotContainsString('testKey', $result);
  926. $result = $this->Form->end();
  927. $this->assertStringNotContainsString('testKey', $result);
  928. }
  929. /**
  930. * Tests form hash generation with model-less data
  931. */
  932. public function testValidateHashNoModel(): void
  933. {
  934. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  935. $fields = ['anything'];
  936. $this->Form->create();
  937. $result = $this->Form->secure($fields);
  938. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  939. $this->assertStringContainsString($hash, $result);
  940. }
  941. /**
  942. * Tests that hidden fields generated for checkboxes don't get locked
  943. */
  944. public function testNoCheckboxLocking(): void
  945. {
  946. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  947. $this->Form->create();
  948. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  949. $this->Form->checkbox('check', ['value' => '1']);
  950. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  951. }
  952. /**
  953. * testFormSecurityFields method
  954. *
  955. * Test generation of secure form hash generation.
  956. */
  957. public function testFormSecurityFields(): void
  958. {
  959. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  960. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  961. $this->Form->create();
  962. $result = $this->Form->secure($fields);
  963. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  964. $hash .= ':' . 'Model.valid';
  965. $hash = urlencode($hash);
  966. $tokenDebug = urlencode(json_encode([
  967. $this->url,
  968. $fields,
  969. [],
  970. ]));
  971. $expected = [
  972. 'div' => ['style' => 'display:none;'],
  973. ['input' => [
  974. 'type' => 'hidden',
  975. 'name' => '_Token[fields]',
  976. 'value' => $hash,
  977. ]],
  978. ['input' => [
  979. 'type' => 'hidden',
  980. 'name' => '_Token[unlocked]',
  981. 'value' => '',
  982. ]],
  983. ['input' => [
  984. 'type' => 'hidden',
  985. 'name' => '_Token[debug]',
  986. 'value' => $tokenDebug,
  987. ]],
  988. '/div',
  989. ];
  990. $this->assertHtml($expected, $result);
  991. }
  992. /**
  993. * testFormSecurityFields method
  994. *
  995. * Test debug token is not generated if debug is false
  996. */
  997. public function testFormSecurityFieldsNoDebugMode(): void
  998. {
  999. Configure::write('debug', false);
  1000. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1001. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1002. $this->Form->create();
  1003. $result = $this->Form->secure($fields);
  1004. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1005. $hash .= ':' . 'Model.valid';
  1006. $hash = urlencode($hash);
  1007. $expected = [
  1008. 'div' => ['style' => 'display:none;'],
  1009. ['input' => [
  1010. 'type' => 'hidden',
  1011. 'name' => '_Token[fields]',
  1012. 'value' => $hash,
  1013. ]],
  1014. ['input' => [
  1015. 'type' => 'hidden',
  1016. 'name' => '_Token[unlocked]',
  1017. 'value' => '',
  1018. ]],
  1019. '/div',
  1020. ];
  1021. $this->assertHtml($expected, $result);
  1022. }
  1023. /**
  1024. * Tests correct generation of number fields for smallint
  1025. */
  1026. public function testTextFieldGenerationForSmallint(): void
  1027. {
  1028. $this->article['schema'] = [
  1029. 'foo' => [
  1030. 'type' => 'smallinteger',
  1031. 'null' => false,
  1032. 'default' => null,
  1033. 'length' => 10,
  1034. ],
  1035. ];
  1036. $this->Form->create($this->article);
  1037. $result = $this->Form->control('foo');
  1038. $this->assertStringContainsString('class="input number"', $result);
  1039. $this->assertStringContainsString('type="number"', $result);
  1040. }
  1041. /**
  1042. * Tests correct generation of number fields for tinyint
  1043. */
  1044. public function testTextFieldGenerationForTinyint(): void
  1045. {
  1046. $this->article['schema'] = [
  1047. 'foo' => [
  1048. 'type' => 'tinyinteger',
  1049. 'null' => false,
  1050. 'default' => null,
  1051. 'length' => 10,
  1052. ],
  1053. ];
  1054. $this->Form->create($this->article);
  1055. $result = $this->Form->control('foo');
  1056. $this->assertStringContainsString('class="input number"', $result);
  1057. $this->assertStringContainsString('type="number"', $result);
  1058. }
  1059. /**
  1060. * Tests correct generation of number fields for double and float fields
  1061. */
  1062. public function testTextFieldGenerationForFloats(): void
  1063. {
  1064. $this->article['schema'] = [
  1065. 'foo' => [
  1066. 'type' => 'float',
  1067. 'null' => false,
  1068. 'default' => null,
  1069. 'length' => 10,
  1070. ],
  1071. ];
  1072. $this->Form->create($this->article);
  1073. $result = $this->Form->control('foo');
  1074. $expected = [
  1075. 'div' => ['class' => 'input number'],
  1076. 'label' => ['for' => 'foo'],
  1077. 'Foo',
  1078. '/label',
  1079. ['input' => [
  1080. 'type' => 'number',
  1081. 'name' => 'foo',
  1082. 'id' => 'foo',
  1083. 'step' => 'any',
  1084. ]],
  1085. '/div',
  1086. ];
  1087. $this->assertHtml($expected, $result);
  1088. $result = $this->Form->control('foo', ['step' => 0.5]);
  1089. $expected = [
  1090. 'div' => ['class' => 'input number'],
  1091. 'label' => ['for' => 'foo'],
  1092. 'Foo',
  1093. '/label',
  1094. ['input' => [
  1095. 'type' => 'number',
  1096. 'name' => 'foo',
  1097. 'id' => 'foo',
  1098. 'step' => '0.5',
  1099. ]],
  1100. '/div',
  1101. ];
  1102. $this->assertHtml($expected, $result);
  1103. }
  1104. /**
  1105. * Tests correct generation of number fields for integer fields
  1106. */
  1107. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1108. {
  1109. $this->getTableLocator()->get('Contacts', [
  1110. 'className' => ContactsTable::class,
  1111. ]);
  1112. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1113. $result = $this->Form->control('age');
  1114. $expected = [
  1115. 'div' => ['class' => 'input number'],
  1116. 'label' => ['for' => 'age'],
  1117. 'Age',
  1118. '/label',
  1119. ['input' => [
  1120. 'type' => 'number', 'name' => 'age',
  1121. 'id' => 'age',
  1122. ]],
  1123. '/div',
  1124. ];
  1125. $this->assertHtml($expected, $result);
  1126. }
  1127. /**
  1128. * Tests correct generation of file upload fields for binary fields
  1129. */
  1130. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1131. {
  1132. $table = $this->getTableLocator()->get('Contacts', [
  1133. 'className' => ContactsTable::class,
  1134. ]);
  1135. $table->setSchema(['foo' => [
  1136. 'type' => 'binary',
  1137. 'null' => false,
  1138. 'default' => null,
  1139. 'length' => 1024,
  1140. ]]);
  1141. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1142. $result = $this->Form->control('foo');
  1143. $expected = [
  1144. 'div' => ['class' => 'input file'],
  1145. 'label' => ['for' => 'foo'],
  1146. 'Foo',
  1147. '/label',
  1148. ['input' => [
  1149. 'type' => 'file', 'name' => 'foo',
  1150. 'id' => 'foo',
  1151. ]],
  1152. '/div',
  1153. ];
  1154. $this->assertHtml($expected, $result);
  1155. }
  1156. /**
  1157. * testFormSecurityMultipleFields method
  1158. *
  1159. * Test secure() with multiple row form. Ensure hash is correct.
  1160. */
  1161. public function testFormSecurityMultipleFields(): void
  1162. {
  1163. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1164. $this->Form->create();
  1165. $fields = [
  1166. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1167. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1168. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1169. ];
  1170. $result = $this->Form->secure($fields);
  1171. $sortedFields = [
  1172. 'Model.0.password',
  1173. 'Model.0.username',
  1174. 'Model.1.password',
  1175. 'Model.1.username',
  1176. 'Model.0.hidden' => 'value',
  1177. 'Model.0.valid' => '0',
  1178. 'Model.1.hidden' => 'value',
  1179. 'Model.1.valid' => '0',
  1180. ];
  1181. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1182. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1183. $hash = urlencode($hash);
  1184. $tokenDebug = urlencode(json_encode([
  1185. $this->url,
  1186. $fields,
  1187. [],
  1188. ]));
  1189. $expected = [
  1190. 'div' => ['style' => 'display:none;'],
  1191. ['input' => [
  1192. 'type' => 'hidden',
  1193. 'name' => '_Token[fields]',
  1194. 'value' => $hash,
  1195. ]],
  1196. ['input' => [
  1197. 'type' => 'hidden',
  1198. 'name' => '_Token[unlocked]',
  1199. 'value' => '',
  1200. ]],
  1201. ['input' => [
  1202. 'type' => 'hidden',
  1203. 'name' => '_Token[debug]',
  1204. 'value' => $tokenDebug,
  1205. ]],
  1206. '/div',
  1207. ];
  1208. $this->assertHtml($expected, $result);
  1209. }
  1210. /**
  1211. * testFormSecurityMultipleSubmitButtons
  1212. *
  1213. * test form submit generation and ensure that _Token is only created on end()
  1214. */
  1215. public function testFormSecurityMultipleSubmitButtons(): void
  1216. {
  1217. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1218. $this->Form->create($this->article);
  1219. $this->Form->text('Address.title');
  1220. $this->Form->text('Address.first_name');
  1221. $result = $this->Form->submit('Save', ['name' => 'save']);
  1222. $expected = [
  1223. 'div' => ['class' => 'submit'],
  1224. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1225. '/div',
  1226. ];
  1227. $this->assertHtml($expected, $result);
  1228. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1229. $expected = [
  1230. 'div' => ['class' => 'submit'],
  1231. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1232. '/div',
  1233. ];
  1234. $this->assertHtml($expected, $result);
  1235. $result = $this->Form->end();
  1236. $tokenDebug = urlencode(json_encode([
  1237. '/articles/add',
  1238. [
  1239. 'Address.title',
  1240. 'Address.first_name',
  1241. ],
  1242. ['save', 'cancel'],
  1243. ]));
  1244. $expected = [
  1245. 'div' => ['style' => 'display:none;'],
  1246. ['input' => [
  1247. 'type' => 'hidden',
  1248. 'name' => '_Token[fields]',
  1249. 'value',
  1250. ]],
  1251. ['input' => [
  1252. 'type' => 'hidden',
  1253. 'name' => '_Token[unlocked]',
  1254. 'value' => 'cancel%7Csave',
  1255. ]],
  1256. ['input' => [
  1257. 'type' => 'hidden',
  1258. 'name' => '_Token[debug]',
  1259. 'value' => $tokenDebug,
  1260. ]],
  1261. '/div',
  1262. ];
  1263. $this->assertHtml($expected, $result);
  1264. }
  1265. /**
  1266. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1267. */
  1268. public function testSecurityButtonNestedNamed(): void
  1269. {
  1270. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1271. $this->Form->create();
  1272. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1273. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1274. $this->assertEquals(['Address.button'], $result);
  1275. }
  1276. /**
  1277. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1278. */
  1279. public function testSecuritySubmitNestedNamed(): void
  1280. {
  1281. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1282. $this->Form->create($this->article);
  1283. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1284. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1285. $this->assertEquals(['Address.button'], $result);
  1286. }
  1287. /**
  1288. * Test that the correct fields are unlocked for image submits with no names.
  1289. */
  1290. public function testSecuritySubmitImageNoName(): void
  1291. {
  1292. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1293. $this->Form->create();
  1294. $result = $this->Form->submit('save.png');
  1295. $expected = [
  1296. 'div' => ['class' => 'submit'],
  1297. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1298. '/div',
  1299. ];
  1300. $this->assertHtml($expected, $result);
  1301. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1302. $this->assertEquals(['x', 'y'], $result);
  1303. }
  1304. /**
  1305. * Test that the correct fields are unlocked for image submits with names.
  1306. */
  1307. public function testSecuritySubmitImageName(): void
  1308. {
  1309. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1310. $this->Form->create();
  1311. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1312. $expected = [
  1313. 'div' => ['class' => 'submit'],
  1314. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1315. '/div',
  1316. ];
  1317. $this->assertHtml($expected, $result);
  1318. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1319. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1320. }
  1321. /**
  1322. * testFormSecurityMultipleControlFields method
  1323. *
  1324. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1325. */
  1326. public function testFormSecurityMultipleControlFields(): void
  1327. {
  1328. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1329. $this->Form->create();
  1330. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1331. $this->Form->control('Addresses.0.title');
  1332. $this->Form->control('Addresses.0.first_name');
  1333. $this->Form->control('Addresses.0.last_name');
  1334. $this->Form->control('Addresses.0.address');
  1335. $this->Form->control('Addresses.0.city');
  1336. $this->Form->control('Addresses.0.phone');
  1337. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1338. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1339. $this->Form->control('Addresses.1.title');
  1340. $this->Form->control('Addresses.1.first_name');
  1341. $this->Form->control('Addresses.1.last_name');
  1342. $this->Form->control('Addresses.1.address');
  1343. $this->Form->control('Addresses.1.city');
  1344. $this->Form->control('Addresses.1.phone');
  1345. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1346. $result = $this->Form->secure();
  1347. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1348. $tokenDebug = urlencode(json_encode([
  1349. '/articles/add',
  1350. [
  1351. 'Addresses.0.id' => '123456',
  1352. 'Addresses.0.title',
  1353. 'Addresses.0.first_name',
  1354. 'Addresses.0.last_name',
  1355. 'Addresses.0.address',
  1356. 'Addresses.0.city',
  1357. 'Addresses.0.phone',
  1358. 'Addresses.0.primary',
  1359. 'Addresses.1.id' => '654321',
  1360. 'Addresses.1.title',
  1361. 'Addresses.1.first_name',
  1362. 'Addresses.1.last_name',
  1363. 'Addresses.1.address',
  1364. 'Addresses.1.city',
  1365. 'Addresses.1.phone',
  1366. 'Addresses.1.primary',
  1367. ],
  1368. [],
  1369. ]));
  1370. $expected = [
  1371. 'div' => ['style' => 'display:none;'],
  1372. ['input' => [
  1373. 'type' => 'hidden',
  1374. 'name' => '_Token[fields]',
  1375. 'value' => $hash,
  1376. ]],
  1377. ['input' => [
  1378. 'type' => 'hidden',
  1379. 'name' => '_Token[unlocked]',
  1380. 'value' => '',
  1381. ]],
  1382. ['input' => [
  1383. 'type' => 'hidden',
  1384. 'name' => '_Token[debug]',
  1385. 'value' => $tokenDebug,
  1386. ]],
  1387. '/div',
  1388. ];
  1389. $this->assertHtml($expected, $result);
  1390. }
  1391. /**
  1392. * testFormSecurityArrayFields method
  1393. *
  1394. * Test form security with Model.field.0 style inputs.
  1395. */
  1396. public function testFormSecurityArrayFields(): void
  1397. {
  1398. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1399. $this->Form->create();
  1400. $this->Form->text('Address.primary.1');
  1401. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1402. $this->assertSame('Address.primary', $result[0]);
  1403. $this->Form->text('Address.secondary.1.0');
  1404. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1405. $this->assertSame('Address.secondary', $result[1]);
  1406. }
  1407. /**
  1408. * testFormSecurityMultipleControlDisabledFields method
  1409. *
  1410. * Test secure form generation with multiple records and disabled fields.
  1411. */
  1412. public function testFormSecurityMultipleControlDisabledFields(): void
  1413. {
  1414. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1415. 'unlockedFields' => ['first_name', 'address'],
  1416. ]));
  1417. $this->Form->create();
  1418. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1419. $this->Form->text('Addresses.0.title');
  1420. $this->Form->text('Addresses.0.first_name');
  1421. $this->Form->text('Addresses.0.last_name');
  1422. $this->Form->text('Addresses.0.address');
  1423. $this->Form->text('Addresses.0.city');
  1424. $this->Form->text('Addresses.0.phone');
  1425. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1426. $this->Form->text('Addresses.1.title');
  1427. $this->Form->text('Addresses.1.first_name');
  1428. $this->Form->text('Addresses.1.last_name');
  1429. $this->Form->text('Addresses.1.address');
  1430. $this->Form->text('Addresses.1.city');
  1431. $this->Form->text('Addresses.1.phone');
  1432. $result = $this->Form->secure();
  1433. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1434. $tokenDebug = urlencode(json_encode([
  1435. '/articles/add',
  1436. [
  1437. 'Addresses.0.id' => '123456',
  1438. 'Addresses.0.title',
  1439. 'Addresses.0.last_name',
  1440. 'Addresses.0.city',
  1441. 'Addresses.0.phone',
  1442. 'Addresses.1.id' => '654321',
  1443. 'Addresses.1.title',
  1444. 'Addresses.1.last_name',
  1445. 'Addresses.1.city',
  1446. 'Addresses.1.phone',
  1447. ],
  1448. [
  1449. 'first_name',
  1450. 'address',
  1451. ],
  1452. ]));
  1453. $expected = [
  1454. 'div' => ['style' => 'display:none;'],
  1455. ['input' => [
  1456. 'type' => 'hidden',
  1457. 'name' => '_Token[fields]',
  1458. 'value' => $hash,
  1459. ]],
  1460. ['input' => [
  1461. 'type' => 'hidden',
  1462. 'name' => '_Token[unlocked]',
  1463. 'value' => 'address%7Cfirst_name',
  1464. ]],
  1465. ['input' => [
  1466. 'type' => 'hidden',
  1467. 'name' => '_Token[debug]',
  1468. 'value' => $tokenDebug,
  1469. ]],
  1470. '/div',
  1471. ];
  1472. $this->assertHtml($expected, $result);
  1473. }
  1474. /**
  1475. * testFormSecurityControlDisabledFields method
  1476. *
  1477. * Test single record form with disabled fields.
  1478. */
  1479. public function testFormSecurityControlUnlockedFields(): void
  1480. {
  1481. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1482. 'unlockedFields' => ['first_name', 'address'],
  1483. ]));
  1484. $this->Form->create();
  1485. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1486. $this->assertEquals(
  1487. $this->View->getRequest()->getAttribute('formTokenData'),
  1488. ['unlockedFields' => $result]
  1489. );
  1490. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1491. $this->Form->text('Addresses.title');
  1492. $this->Form->text('Addresses.first_name');
  1493. $this->Form->text('Addresses.last_name');
  1494. $this->Form->text('Addresses.address');
  1495. $this->Form->text('Addresses.city');
  1496. $this->Form->text('Addresses.phone');
  1497. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1498. $expected = [
  1499. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1500. 'Addresses.city', 'Addresses.phone',
  1501. ];
  1502. $this->assertEquals($expected, $result);
  1503. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1504. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1505. $tokenDebug = urlencode(json_encode([
  1506. '/articles/add',
  1507. [
  1508. 'Addresses.id' => '123456',
  1509. 'Addresses.title',
  1510. 'Addresses.last_name',
  1511. 'Addresses.city',
  1512. 'Addresses.phone',
  1513. ],
  1514. [
  1515. 'first_name',
  1516. 'address',
  1517. ],
  1518. ]));
  1519. $expected = [
  1520. 'div' => ['style' => 'display:none;'],
  1521. ['input' => [
  1522. 'type' => 'hidden',
  1523. 'name' => '_Token[fields]',
  1524. 'value' => $hash,
  1525. 'data-foo' => 'bar',
  1526. ]],
  1527. ['input' => [
  1528. 'type' => 'hidden',
  1529. 'name' => '_Token[unlocked]',
  1530. 'value' => 'address%7Cfirst_name',
  1531. 'data-foo' => 'bar',
  1532. ]],
  1533. ['input' => [
  1534. 'type' => 'hidden', 'name' => '_Token[debug]',
  1535. 'value' => $tokenDebug,
  1536. 'data-foo' => 'bar',
  1537. ]],
  1538. '/div',
  1539. ];
  1540. $this->assertHtml($expected, $result);
  1541. }
  1542. /**
  1543. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1544. *
  1545. * Test single record form with debugSecurity param.
  1546. */
  1547. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1548. {
  1549. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1550. 'unlockedFields' => ['first_name', 'address'],
  1551. ]));
  1552. $this->Form->create();
  1553. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1554. $this->assertEquals(
  1555. $this->View->getRequest()->getAttribute('formTokenData'),
  1556. ['unlockedFields' => $result]
  1557. );
  1558. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1559. $this->Form->text('Addresses.title');
  1560. $this->Form->text('Addresses.first_name');
  1561. $this->Form->text('Addresses.last_name');
  1562. $this->Form->text('Addresses.address');
  1563. $this->Form->text('Addresses.city');
  1564. $this->Form->text('Addresses.phone');
  1565. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1566. $expected = [
  1567. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1568. 'Addresses.city', 'Addresses.phone',
  1569. ];
  1570. $this->assertEquals($expected, $result);
  1571. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1572. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1573. $tokenDebug = urlencode(json_encode([
  1574. '/articles/add',
  1575. [
  1576. 'Addresses.id' => '123456',
  1577. 'Addresses.title',
  1578. 'Addresses.last_name',
  1579. 'Addresses.city',
  1580. 'Addresses.phone',
  1581. ],
  1582. [
  1583. 'first_name',
  1584. 'address',
  1585. ],
  1586. ]));
  1587. $expected = [
  1588. 'div' => ['style' => 'display:none;'],
  1589. ['input' => [
  1590. 'type' => 'hidden',
  1591. 'name' => '_Token[fields]',
  1592. 'value' => $hash,
  1593. 'data-foo' => 'bar',
  1594. ]],
  1595. ['input' => [
  1596. 'type' => 'hidden',
  1597. 'name' => '_Token[unlocked]',
  1598. 'value' => 'address%7Cfirst_name',
  1599. 'data-foo' => 'bar',
  1600. ]],
  1601. ['input' => [
  1602. 'type' => 'hidden', 'name' => '_Token[debug]',
  1603. 'value' => $tokenDebug,
  1604. 'data-foo' => 'bar',
  1605. ]],
  1606. '/div',
  1607. ];
  1608. $this->assertHtml($expected, $result);
  1609. }
  1610. /**
  1611. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1612. *
  1613. * Debug is false, debugSecurity is true -> no debug
  1614. */
  1615. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1616. {
  1617. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1618. 'unlockedFields' => ['first_name', 'address'],
  1619. ]));
  1620. $this->Form->create();
  1621. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1622. $this->assertEquals(
  1623. $this->View->getRequest()->getAttribute('formTokenData'),
  1624. ['unlockedFields' => $result]
  1625. );
  1626. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1627. $this->Form->text('Addresses.title');
  1628. $this->Form->text('Addresses.first_name');
  1629. $this->Form->text('Addresses.last_name');
  1630. $this->Form->text('Addresses.address');
  1631. $this->Form->text('Addresses.city');
  1632. $this->Form->text('Addresses.phone');
  1633. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1634. $expected = [
  1635. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1636. 'Addresses.city', 'Addresses.phone',
  1637. ];
  1638. $this->assertEquals($expected, $result);
  1639. Configure::write('debug', false);
  1640. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1641. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1642. $expected = [
  1643. 'div' => ['style' => 'display:none;'],
  1644. ['input' => [
  1645. 'type' => 'hidden',
  1646. 'name' => '_Token[fields]',
  1647. 'value' => $hash,
  1648. 'data-foo' => 'bar',
  1649. ]],
  1650. ['input' => [
  1651. 'type' => 'hidden',
  1652. 'name' => '_Token[unlocked]',
  1653. 'value' => 'address%7Cfirst_name',
  1654. 'data-foo' => 'bar',
  1655. ]],
  1656. '/div',
  1657. ];
  1658. $this->assertHtml($expected, $result);
  1659. }
  1660. /**
  1661. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1662. *
  1663. * Test single record form with debugSecurity param.
  1664. */
  1665. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1666. {
  1667. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1668. 'unlockedFields' => ['first_name', 'address'],
  1669. ]));
  1670. $this->Form->create();
  1671. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1672. $this->assertEquals(
  1673. $this->View->getRequest()->getAttribute('formTokenData'),
  1674. ['unlockedFields' => $result]
  1675. );
  1676. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1677. $this->Form->text('Addresses.title');
  1678. $this->Form->text('Addresses.first_name');
  1679. $this->Form->text('Addresses.last_name');
  1680. $this->Form->text('Addresses.address');
  1681. $this->Form->text('Addresses.city');
  1682. $this->Form->text('Addresses.phone');
  1683. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1684. $expected = [
  1685. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1686. 'Addresses.city', 'Addresses.phone',
  1687. ];
  1688. $this->assertEquals($expected, $result);
  1689. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1690. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1691. $expected = [
  1692. 'div' => ['style' => 'display:none;'],
  1693. ['input' => [
  1694. 'type' => 'hidden',
  1695. 'name' => '_Token[fields]',
  1696. 'value' => $hash,
  1697. 'data-foo' => 'bar',
  1698. ]],
  1699. ['input' => [
  1700. 'type' => 'hidden',
  1701. 'name' => '_Token[unlocked]',
  1702. 'value' => 'address%7Cfirst_name',
  1703. 'data-foo' => 'bar',
  1704. ]],
  1705. '/div',
  1706. ];
  1707. $this->assertHtml($expected, $result);
  1708. }
  1709. /**
  1710. * testFormSecureWithCustomNameAttribute method
  1711. *
  1712. * Test securing inputs with custom name attributes.
  1713. */
  1714. public function testFormSecureWithCustomNameAttribute(): void
  1715. {
  1716. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1717. $this->Form->create();
  1718. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1719. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1720. $this->assertSame('User.custom', $result[0]);
  1721. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1722. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1723. $this->assertSame('User.custom.another.value', $result[1]);
  1724. }
  1725. /**
  1726. * testFormSecuredControl method
  1727. *
  1728. * Test generation of entire secure form, assertions made on control() output.
  1729. */
  1730. public function testFormSecuredControl(): void
  1731. {
  1732. $this->View->setRequest($this->View->getRequest()
  1733. ->withAttribute('formTokenData', [])
  1734. ->withAttribute('csrfToken', 'testKey'));
  1735. $this->article['schema'] = [
  1736. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1737. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1738. ];
  1739. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1740. $encoding = strtolower(Configure::read('App.encoding'));
  1741. $expected = [
  1742. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1743. 'div' => ['style' => 'display:none;'],
  1744. ['input' => [
  1745. 'type' => 'hidden',
  1746. 'name' => '_csrfToken',
  1747. 'value' => 'testKey',
  1748. ]],
  1749. '/div',
  1750. ];
  1751. $this->assertHtml($expected, $result);
  1752. $result = $this->Form->control('ratio');
  1753. $expected = [
  1754. 'div' => ['class'],
  1755. 'label' => ['for'],
  1756. 'Ratio',
  1757. '/label',
  1758. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1759. '/div',
  1760. ];
  1761. $this->assertHtml($expected, $result);
  1762. $result = $this->Form->control('population');
  1763. $expected = [
  1764. 'div' => ['class'],
  1765. 'label' => ['for'],
  1766. 'Population',
  1767. '/label',
  1768. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1769. '/div',
  1770. ];
  1771. $this->assertHtml($expected, $result);
  1772. $result = $this->Form->control('published', ['type' => 'text']);
  1773. $expected = [
  1774. 'div' => ['class' => 'input text'],
  1775. 'label' => ['for' => 'published'],
  1776. 'Published',
  1777. '/label',
  1778. ['input' => [
  1779. 'type' => 'text',
  1780. 'name' => 'published',
  1781. 'id' => 'published',
  1782. ]],
  1783. '/div',
  1784. ];
  1785. $this->assertHtml($expected, $result);
  1786. $result = $this->Form->control('other', ['type' => 'text']);
  1787. $expected = [
  1788. 'div' => ['class' => 'input text'],
  1789. 'label' => ['for' => 'other'],
  1790. 'Other',
  1791. '/label',
  1792. ['input' => [
  1793. 'type' => 'text',
  1794. 'name' => 'other',
  1795. 'id',
  1796. ]],
  1797. '/div',
  1798. ];
  1799. $this->assertHtml($expected, $result);
  1800. $result = $this->Form->hidden('stuff');
  1801. $expected = [
  1802. 'input' => [
  1803. 'type' => 'hidden',
  1804. 'name' => 'stuff',
  1805. ],
  1806. ];
  1807. $this->assertHtml($expected, $result);
  1808. $result = $this->Form->hidden('hidden', ['value' => false]);
  1809. $expected = ['input' => [
  1810. 'type' => 'hidden',
  1811. 'name' => 'hidden',
  1812. 'value' => '0',
  1813. ]];
  1814. $this->assertHtml($expected, $result);
  1815. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1816. $expected = [
  1817. 'div' => ['class' => 'input checkbox'],
  1818. ['input' => [
  1819. 'type' => 'hidden',
  1820. 'name' => 'something',
  1821. 'value' => '0',
  1822. ]],
  1823. 'label' => ['for' => 'something'],
  1824. ['input' => [
  1825. 'type' => 'checkbox',
  1826. 'name' => 'something',
  1827. 'value' => '1',
  1828. 'id' => 'something',
  1829. ]],
  1830. 'Something',
  1831. '/label',
  1832. '/div',
  1833. ];
  1834. $this->assertHtml($expected, $result);
  1835. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1836. $expectedFields = [
  1837. 'ratio',
  1838. 'population',
  1839. 'published',
  1840. 'other',
  1841. 'stuff' => '',
  1842. 'hidden' => '0',
  1843. 'something',
  1844. ];
  1845. $this->assertEquals($expectedFields, $result);
  1846. $result = $this->Form->secure();
  1847. $tokenDebug = urlencode(json_encode([
  1848. '/articles/add',
  1849. $expectedFields,
  1850. [],
  1851. ]));
  1852. $expected = [
  1853. 'div' => ['style' => 'display:none;'],
  1854. ['input' => [
  1855. 'type' => 'hidden',
  1856. 'name' => '_Token[fields]',
  1857. 'value',
  1858. ]],
  1859. ['input' => [
  1860. 'type' => 'hidden',
  1861. 'name' => '_Token[unlocked]',
  1862. 'value' => '',
  1863. ]],
  1864. ['input' => [
  1865. 'type' => 'hidden', 'name' => '_Token[debug]',
  1866. 'value' => $tokenDebug,
  1867. ]],
  1868. '/div',
  1869. ];
  1870. $this->assertHtml($expected, $result);
  1871. $data = [
  1872. 'ratio' => '',
  1873. 'population' => '',
  1874. 'published' => '',
  1875. 'other' => '',
  1876. 'stuff' => '',
  1877. 'hidden' => '0',
  1878. 'something' => '',
  1879. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1880. ];
  1881. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1882. }
  1883. /**
  1884. * testSecuredControlCustomName method
  1885. *
  1886. * Test secured inputs with custom names.
  1887. */
  1888. public function testSecuredControlCustomName(): void
  1889. {
  1890. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1891. $this->Form->create();
  1892. $this->Form->text('text_input', [
  1893. 'name' => 'Option[General.default_role]',
  1894. ]);
  1895. $expected = ['Option.General.default_role'];
  1896. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1897. $this->assertEquals($expected, $result);
  1898. $this->Form->select('select_box', [1, 2], [
  1899. 'name' => 'Option[General.select_role]',
  1900. ]);
  1901. $expected[] = 'Option.General.select_role';
  1902. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1903. $this->assertEquals($expected, $result);
  1904. $this->Form->text('other.things[]');
  1905. $expected[] = 'other.things';
  1906. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1907. $this->assertEquals($expected, $result);
  1908. }
  1909. /**
  1910. * testSecuredControlDuplicate method
  1911. *
  1912. * Test that a hidden field followed by a visible field
  1913. * undoes the hidden field locking.
  1914. */
  1915. public function testSecuredControlDuplicate(): void
  1916. {
  1917. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1918. $this->Form->create();
  1919. $this->Form->control('text_val', [
  1920. 'type' => 'hidden',
  1921. 'value' => 'some text',
  1922. ]);
  1923. $expected = ['text_val' => 'some text'];
  1924. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1925. $this->assertEquals($expected, $result);
  1926. $this->Form->control('text_val', [
  1927. 'type' => 'text',
  1928. ]);
  1929. $expected = ['text_val'];
  1930. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1931. $this->assertEquals($expected, $result);
  1932. }
  1933. /**
  1934. * testFormSecuredFileControl method
  1935. *
  1936. * Tests that the correct keys are added to the field hash index.
  1937. */
  1938. public function testFormSecuredFileControl(): void
  1939. {
  1940. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1941. $this->Form->create();
  1942. $this->Form->file('Attachment.file');
  1943. $expected = ['Attachment.file'];
  1944. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1945. $this->assertEquals($expected, $result);
  1946. }
  1947. /**
  1948. * testFormSecuredMultipleSelect method
  1949. *
  1950. * Test that multiple selects keys are added to field hash.
  1951. */
  1952. public function testFormSecuredMultipleSelect(): void
  1953. {
  1954. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1955. $this->Form->create();
  1956. $options = ['1' => 'one', '2' => 'two'];
  1957. $this->Form->select('Model.select', $options);
  1958. $expected = ['Model.select'];
  1959. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1960. $this->assertEquals($expected, $result);
  1961. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1962. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1963. $this->assertEquals($expected, $result);
  1964. }
  1965. /**
  1966. * testFormSecuredRadio method
  1967. */
  1968. public function testFormSecuredRadio(): void
  1969. {
  1970. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1971. $this->Form->create();
  1972. $options = ['1' => 'option1', '2' => 'option2'];
  1973. $this->Form->radio('Test.test', $options);
  1974. $expected = ['Test.test'];
  1975. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1976. $this->assertEquals($expected, $result);
  1977. $this->Form->radio('Test.all', $options, [
  1978. 'disabled' => ['option1', 'option2'],
  1979. ]);
  1980. $expected = ['Test.test', 'Test.all' => ''];
  1981. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1982. $this->assertEquals($expected, $result);
  1983. $this->Form->radio('Test.some', $options, [
  1984. 'disabled' => ['option1'],
  1985. ]);
  1986. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1987. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1988. $this->assertEquals($expected, $result);
  1989. }
  1990. /**
  1991. * testFormSecuredAndDisabled method
  1992. *
  1993. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1994. * hashing.
  1995. */
  1996. public function testFormSecuredAndDisabled(): void
  1997. {
  1998. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1999. $this->Form->create();
  2000. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2001. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2002. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2003. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2004. $this->Form->year('Model.year', ['disabled' => true]);
  2005. $this->Form->month('Model.month', ['disabled' => true]);
  2006. $this->Form->text('Model.text', ['disabled' => true]);
  2007. $this->Form->password('Model.text', ['disabled' => true]);
  2008. $this->Form->day('Model.day', ['disabled' => true]);
  2009. $this->Form->hour('Model.hour', ['disabled' => true]);
  2010. $this->Form->minute('Model.minute', ['disabled' => true]);
  2011. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2012. $expected = [
  2013. 'Model.radio' => '',
  2014. ];
  2015. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2016. $this->assertEquals($expected, $result);
  2017. }
  2018. /**
  2019. * testUnlockFieldAddsToList method
  2020. *
  2021. * Test disableField.
  2022. */
  2023. public function testUnlockFieldAddsToList(): void
  2024. {
  2025. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2026. 'unlockedFields' => [],
  2027. ]));
  2028. $this->Form->create();
  2029. $this->Form->unlockField('Contact.name');
  2030. $this->Form->text('Contact.name');
  2031. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2032. $this->assertEquals([], $result);
  2033. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2034. $this->assertEquals(['Contact.name'], $result);
  2035. }
  2036. /**
  2037. * testUnlockFieldRemovingFromFields method
  2038. *
  2039. * Test unlockField removing from fields array.
  2040. */
  2041. public function testUnlockFieldRemovingFromFields(): void
  2042. {
  2043. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2044. 'unlockedFields' => [],
  2045. ]));
  2046. $this->Form->create($this->article);
  2047. $this->Form->hidden('Article.id', ['value' => 1]);
  2048. $this->Form->text('Article.title');
  2049. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2050. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2051. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2052. $this->Form->unlockField('Article.title');
  2053. $this->Form->unlockField('Article.id');
  2054. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2055. $this->assertEquals([], $result);
  2056. }
  2057. /**
  2058. * testResetUnlockFields method
  2059. *
  2060. * Test reset unlockFields, when create new form.
  2061. */
  2062. public function testResetUnlockFields(): void
  2063. {
  2064. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2065. 'key' => 'testKey',
  2066. 'unlockedFields' => [],
  2067. ]));
  2068. $this->Form->create();
  2069. $this->Form->unlockField('Contact.id');
  2070. $this->Form->hidden('Contact.id', ['value' => 1]);
  2071. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2072. $this->assertEmpty($result, 'Field should be unlocked');
  2073. $this->Form->end();
  2074. $this->Form->create();
  2075. $this->Form->hidden('Contact.id', ['value' => 1]);
  2076. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2077. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2078. }
  2079. /**
  2080. * testUnlockFieldWithFormTokenData method
  2081. *
  2082. * Test that unlockField() becomes a no-op and does not throw an exception
  2083. * when called without `formTokenData` being present in the request.
  2084. *
  2085. * @return void
  2086. */
  2087. public function testUnlockFieldWithFormTokenData(): void
  2088. {
  2089. $this->Form->create();
  2090. $result = $this->Form->unlockField('foo');
  2091. $this->assertSame($this->Form, $result);
  2092. }
  2093. /**
  2094. * testSecuredFormUrlIgnoresHost method
  2095. *
  2096. * Test that only the path + query elements of a form's URL show up in their hash.
  2097. */
  2098. public function testSecuredFormUrlIgnoresHost(): void
  2099. {
  2100. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2101. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2102. $this->Form->create($this->article, [
  2103. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2104. ]);
  2105. $result = $this->Form->secure();
  2106. $this->assertStringContainsString($expected, $result);
  2107. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2108. $result = $this->Form->secure();
  2109. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2110. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2111. $result = $this->Form->secure();
  2112. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2113. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2114. $result = $this->Form->secure();
  2115. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2116. }
  2117. /**
  2118. * testSecuredFormUrlHasHtmlAndIdentifier method
  2119. *
  2120. * Test that URL, HTML and identifier show up in their hashes.
  2121. */
  2122. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2123. {
  2124. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2125. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2126. $this->Form->create($this->article, [
  2127. 'url' => [
  2128. 'controller' => 'articles',
  2129. 'action' => 'view',
  2130. '?' => [
  2131. 'page' => 1,
  2132. 'limit' => 10,
  2133. 'html' => '<>"',
  2134. ],
  2135. '#' => 'result',
  2136. ],
  2137. ]);
  2138. $result = $this->Form->secure();
  2139. $this->assertStringContainsString($expected, $result);
  2140. $this->Form->create($this->article, [
  2141. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2142. ]);
  2143. $result = $this->Form->secure();
  2144. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2145. $this->Form->create($this->article, [
  2146. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2147. ]);
  2148. $result = $this->Form->secure();
  2149. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2150. }
  2151. /**
  2152. * testErrorMessageDisplay method
  2153. *
  2154. * Test error message display.
  2155. */
  2156. public function testErrorMessageDisplay(): void
  2157. {
  2158. $this->article['errors'] = [
  2159. 'Article' => [
  2160. 'title' => 'error message',
  2161. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2162. ],
  2163. ];
  2164. $this->Form->create($this->article);
  2165. $result = $this->Form->control('Article.title');
  2166. $expected = [
  2167. 'div' => ['class' => 'input text error'],
  2168. 'label' => ['for' => 'article-title'],
  2169. 'Title',
  2170. '/label',
  2171. 'input' => [
  2172. 'type' => 'text',
  2173. 'name' => 'Article[title]',
  2174. 'id' => 'article-title',
  2175. 'class' => 'form-error',
  2176. 'aria-invalid' => 'true',
  2177. 'aria-describedby' => 'article-title-error',
  2178. ],
  2179. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2180. 'error message',
  2181. '/div',
  2182. '/div',
  2183. ];
  2184. $this->assertHtml($expected, $result);
  2185. $result = $this->Form->control('Article.title', [
  2186. 'templates' => [
  2187. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2188. ],
  2189. ]);
  2190. $expected = [
  2191. 'div' => ['class' => 'input text error'],
  2192. 'label' => ['for' => 'article-title'],
  2193. 'Title',
  2194. '/label',
  2195. 'input' => [
  2196. 'type' => 'text',
  2197. 'name' => 'Article[title]',
  2198. 'id' => 'article-title',
  2199. 'class' => 'form-error',
  2200. // No aria-describedby because error template is custom
  2201. 'aria-invalid' => 'true',
  2202. ],
  2203. '/div',
  2204. ];
  2205. $this->assertHtml($expected, $result);
  2206. $result = $this->Form->control('Article.content');
  2207. $expected = [
  2208. 'div' => ['class' => 'input text error'],
  2209. 'label' => ['for' => 'article-content'],
  2210. 'Content',
  2211. '/label',
  2212. 'input' => [
  2213. 'type' => 'text',
  2214. 'name' => 'Article[content]',
  2215. 'id' => 'article-content',
  2216. 'class' => 'form-error',
  2217. 'aria-invalid' => 'true',
  2218. 'aria-describedby' => 'article-content-error',
  2219. ],
  2220. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2221. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2222. '/div',
  2223. '/div',
  2224. ];
  2225. $this->assertHtml($expected, $result);
  2226. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2227. $expected = [
  2228. 'div' => ['class' => 'input text error'],
  2229. 'label' => ['for' => 'article-content'],
  2230. 'Content',
  2231. '/label',
  2232. 'input' => [
  2233. 'type' => 'text',
  2234. 'name' => 'Article[content]',
  2235. 'id' => 'article-content',
  2236. 'class' => 'form-error',
  2237. 'aria-invalid' => 'true',
  2238. 'aria-describedby' => 'article-content-error',
  2239. ],
  2240. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2241. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2242. '/div',
  2243. '/div',
  2244. ];
  2245. $this->assertHtml($expected, $result);
  2246. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2247. $expected = [
  2248. 'div' => ['class' => 'input text error'],
  2249. 'label' => ['for' => 'article-content'],
  2250. 'Content',
  2251. '/label',
  2252. 'input' => [
  2253. 'type' => 'text',
  2254. 'name' => 'Article[content]',
  2255. 'id' => 'article-content',
  2256. 'class' => 'form-error',
  2257. 'aria-invalid' => 'true',
  2258. 'aria-describedby' => 'article-content-error',
  2259. ],
  2260. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2261. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2262. '/div',
  2263. '/div',
  2264. ];
  2265. $this->assertHtml($expected, $result);
  2266. }
  2267. /**
  2268. * testEmptyErrorValidation method
  2269. *
  2270. * Test validation errors, when validation message is an empty string.
  2271. */
  2272. public function testEmptyErrorValidation(): void
  2273. {
  2274. $this->article['errors'] = [
  2275. 'Article' => ['title' => ''],
  2276. ];
  2277. $this->Form->create($this->article);
  2278. $result = $this->Form->control('Article.title');
  2279. $expected = [
  2280. 'div' => ['class' => 'input text error'],
  2281. 'label' => ['for' => 'article-title'],
  2282. 'Title',
  2283. '/label',
  2284. 'input' => [
  2285. 'type' => 'text',
  2286. 'name' => 'Article[title]',
  2287. 'id' => 'article-title',
  2288. 'class' => 'form-error',
  2289. 'aria-invalid' => 'true',
  2290. 'aria-describedby' => 'article-title-error',
  2291. ],
  2292. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2293. [],
  2294. '/div',
  2295. '/div',
  2296. ];
  2297. $this->assertHtml($expected, $result);
  2298. }
  2299. /**
  2300. * testEmptyControlErrorValidation method
  2301. *
  2302. * Test validation errors, when calling control() overriding validation message by an empty string.
  2303. */
  2304. public function testEmptyControlErrorValidation(): void
  2305. {
  2306. $this->article['errors'] = [
  2307. 'Article' => ['title' => 'error message'],
  2308. ];
  2309. $this->Form->create($this->article);
  2310. $result = $this->Form->control('Article.title', ['error' => '']);
  2311. $expected = [
  2312. 'div' => ['class' => 'input text error'],
  2313. 'label' => ['for' => 'article-title'],
  2314. 'Title',
  2315. '/label',
  2316. 'input' => [
  2317. 'aria-invalid' => 'true',
  2318. 'aria-describedby' => 'article-title-error',
  2319. 'type' => 'text',
  2320. 'name' => 'Article[title]',
  2321. 'id' => 'article-title',
  2322. 'class' => 'form-error',
  2323. ],
  2324. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2325. [],
  2326. '/div',
  2327. '/div',
  2328. ];
  2329. $this->assertHtml($expected, $result);
  2330. }
  2331. /**
  2332. * testControlErrorMessage method
  2333. *
  2334. * Test validation errors, when calling control() overriding validation messages.
  2335. */
  2336. public function testControlErrorMessage(): void
  2337. {
  2338. $this->article['errors'] = [
  2339. 'title' => ['error message'],
  2340. ];
  2341. $this->Form->create($this->article);
  2342. $result = $this->Form->control('title', [
  2343. 'error' => 'Custom error!',
  2344. ]);
  2345. $expected = [
  2346. 'div' => ['class' => 'input text required error'],
  2347. 'label' => ['for' => 'title'],
  2348. 'Title',
  2349. '/label',
  2350. 'input' => [
  2351. 'type' => 'text',
  2352. 'name' => 'title',
  2353. 'id' => 'title',
  2354. 'class' => 'form-error',
  2355. 'required' => 'required',
  2356. 'data-validity-message' => 'This field cannot be left empty',
  2357. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2358. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2359. 'aria-invalid' => 'true',
  2360. 'aria-describedby' => 'title-error',
  2361. ],
  2362. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2363. 'Custom error!',
  2364. '/div',
  2365. '/div',
  2366. ];
  2367. $this->assertHtml($expected, $result);
  2368. $result = $this->Form->control('title', [
  2369. 'error' => ['error message' => 'Custom error!'],
  2370. ]);
  2371. $expected = [
  2372. 'div' => ['class' => 'input text required error'],
  2373. 'label' => ['for' => 'title'],
  2374. 'Title',
  2375. '/label',
  2376. 'input' => [
  2377. 'type' => 'text',
  2378. 'name' => 'title',
  2379. 'id' => 'title',
  2380. 'aria-invalid' => 'true',
  2381. 'aria-describedby' => 'title-error',
  2382. 'class' => 'form-error',
  2383. 'required' => 'required',
  2384. 'data-validity-message' => 'This field cannot be left empty',
  2385. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2386. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2387. ],
  2388. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2389. 'Custom error!',
  2390. '/div',
  2391. '/div',
  2392. ];
  2393. $this->assertHtml($expected, $result);
  2394. }
  2395. /**
  2396. * testFormValidationAssociated method
  2397. *
  2398. * Tests displaying errors for nested entities.
  2399. */
  2400. public function testFormValidationAssociated(): void
  2401. {
  2402. $nested = new Entity(['foo' => 'bar']);
  2403. $nested->setError('foo', ['not a valid bar']);
  2404. $entity = new Entity(['nested' => $nested]);
  2405. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2406. $result = $this->Form->error('nested.foo');
  2407. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2408. }
  2409. /**
  2410. * testFormValidationAssociatedSecondLevel method
  2411. *
  2412. * Test form error display with associated model.
  2413. */
  2414. public function testFormValidationAssociatedSecondLevel(): void
  2415. {
  2416. $inner = new Entity(['bar' => 'baz']);
  2417. $nested = new Entity(['foo' => $inner]);
  2418. $entity = new Entity(['nested' => $nested]);
  2419. $inner->setError('bar', ['not a valid one']);
  2420. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2421. $result = $this->Form->error('nested.foo.bar');
  2422. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2423. }
  2424. /**
  2425. * testFormValidationMultiRecord method
  2426. *
  2427. * Test form error display with multiple records.
  2428. */
  2429. public function testFormValidationMultiRecord(): void
  2430. {
  2431. $one = new Entity();
  2432. $two = new Entity();
  2433. $this->getTableLocator()->get('Contacts', [
  2434. 'className' => ContactsTable::class,
  2435. ]);
  2436. $one->set('email', '');
  2437. $one->setError('email', ['invalid email']);
  2438. $two->set('name', '');
  2439. $two->setError('name', ['This is wrong']);
  2440. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2441. $result = $this->Form->control('0.email');
  2442. $expected = [
  2443. 'div' => ['class' => 'input email error'],
  2444. 'label' => ['for' => '0-email'],
  2445. 'Email',
  2446. '/label',
  2447. 'input' => [
  2448. 'type' => 'email',
  2449. 'name' => '0[email]',
  2450. 'id' => '0-email',
  2451. 'class' => 'form-error',
  2452. 'maxlength' => 255,
  2453. 'value' => '',
  2454. 'aria-invalid' => 'true',
  2455. 'aria-describedby' => '0-email-error',
  2456. ],
  2457. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2458. 'invalid email',
  2459. '/div',
  2460. '/div',
  2461. ];
  2462. $this->assertHtml($expected, $result);
  2463. $result = $this->Form->control('1.name');
  2464. $expected = [
  2465. 'div' => ['class' => 'input text error'],
  2466. 'label' => ['for' => '1-name'],
  2467. 'Name',
  2468. '/label',
  2469. 'input' => [
  2470. 'type' => 'text',
  2471. 'name' => '1[name]',
  2472. 'id' => '1-name',
  2473. 'class' => 'form-error',
  2474. 'maxlength' => 255,
  2475. 'value' => '',
  2476. 'aria-invalid' => 'true',
  2477. 'aria-describedby' => '1-name-error',
  2478. ],
  2479. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2480. 'This is wrong',
  2481. '/div',
  2482. '/div',
  2483. ];
  2484. $this->assertHtml($expected, $result);
  2485. }
  2486. /**
  2487. * testControl method
  2488. *
  2489. * Test various incarnations of control().
  2490. */
  2491. public function testControl(): void
  2492. {
  2493. $this->getTableLocator()->get('ValidateUsers', [
  2494. 'className' => ValidateUsersTable::class,
  2495. ]);
  2496. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2497. $result = $this->Form->control('ValidateUsers.balance');
  2498. $expected = [
  2499. 'div' => ['class'],
  2500. 'label' => ['for'],
  2501. 'Balance',
  2502. '/label',
  2503. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2504. '/div',
  2505. ];
  2506. $this->assertHtml($expected, $result);
  2507. $result = $this->Form->control('ValidateUser.cost_decimal');
  2508. $expected = [
  2509. 'div' => ['class'],
  2510. 'label' => ['for'],
  2511. 'Cost Decimal',
  2512. '/label',
  2513. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2514. '/div',
  2515. ];
  2516. $this->assertHtml($expected, $result);
  2517. $result = $this->Form->control('ValidateUser.null_decimal');
  2518. $expected = [
  2519. 'div' => ['class'],
  2520. 'label' => ['for'],
  2521. 'Null Decimal',
  2522. '/label',
  2523. 'input' => ['name', 'type' => 'number', 'id'],
  2524. '/div',
  2525. ];
  2526. $this->assertHtml($expected, $result);
  2527. }
  2528. /**
  2529. * testControlCustomization method
  2530. *
  2531. * Tests the input method and passing custom options.
  2532. */
  2533. public function testControlCustomization(): void
  2534. {
  2535. $this->getTableLocator()->get('Contacts', [
  2536. 'className' => ContactsTable::class,
  2537. ]);
  2538. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2539. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2540. $expected = [
  2541. 'div' => ['class' => 'input email'],
  2542. 'label' => ['for' => 'custom'],
  2543. 'Email',
  2544. '/label',
  2545. ['input' => [
  2546. 'type' => 'email', 'name' => 'Contact[email]',
  2547. 'id' => 'custom', 'maxlength' => 255,
  2548. ]],
  2549. '/div',
  2550. ];
  2551. $this->assertHtml($expected, $result);
  2552. $result = $this->Form->control('Contact.email', [
  2553. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2554. ]);
  2555. $expected = [
  2556. '<div',
  2557. 'label' => ['for' => 'contact-email'],
  2558. 'Email',
  2559. '/label',
  2560. ['input' => [
  2561. 'type' => 'email', 'name' => 'Contact[email]',
  2562. 'id' => 'contact-email', 'maxlength' => 255,
  2563. ]],
  2564. '/div',
  2565. ];
  2566. $this->assertHtml($expected, $result);
  2567. $result = $this->Form->control('Contact.email', [
  2568. 'templates' => [
  2569. 'formGroup' => '{{input}}',
  2570. 'inputContainer' => '<div><div>{{label}}</div>{{content}}</div>',
  2571. ],
  2572. ]);
  2573. $expected = [
  2574. '<div',
  2575. '<div',
  2576. 'label' => ['for' => 'contact-email'],
  2577. 'Email',
  2578. '/label',
  2579. '/div',
  2580. ['input' => [
  2581. 'type' => 'email', 'name' => 'Contact[email]',
  2582. 'id' => 'contact-email', 'maxlength' => 255,
  2583. ]],
  2584. '/div',
  2585. ];
  2586. $this->assertHtml($expected, $result);
  2587. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2588. $expected = [
  2589. 'div' => ['class' => 'input text'],
  2590. 'label' => ['for' => 'contact-email'],
  2591. 'Email',
  2592. '/label',
  2593. ['input' => [
  2594. 'type' => 'text', 'name' => 'Contact[email]',
  2595. 'id' => 'contact-email', 'maxlength' => '255',
  2596. ]],
  2597. '/div',
  2598. ];
  2599. $this->assertHtml($expected, $result);
  2600. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2601. $expected = [
  2602. 'div' => ['class' => 'input text'],
  2603. 'label' => ['for' => 'contact-5-email'],
  2604. 'Email',
  2605. '/label',
  2606. ['input' => [
  2607. 'type' => 'text', 'name' => 'Contact[5][email]',
  2608. 'id' => 'contact-5-email', 'maxlength' => '255',
  2609. ]],
  2610. '/div',
  2611. ];
  2612. $this->assertHtml($expected, $result);
  2613. $result = $this->Form->control('Contact.password');
  2614. $expected = [
  2615. 'div' => ['class' => 'input password'],
  2616. 'label' => ['for' => 'contact-password'],
  2617. 'Password',
  2618. '/label',
  2619. ['input' => [
  2620. 'type' => 'password', 'name' => 'Contact[password]',
  2621. 'id' => 'contact-password',
  2622. ]],
  2623. '/div',
  2624. ];
  2625. $this->assertHtml($expected, $result);
  2626. $result = $this->Form->control('Contact.email', [
  2627. 'type' => 'file', 'class' => 'textbox',
  2628. ]);
  2629. $expected = [
  2630. 'div' => ['class' => 'input file'],
  2631. 'label' => ['for' => 'contact-email'],
  2632. 'Email',
  2633. '/label',
  2634. ['input' => [
  2635. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2636. 'id' => 'contact-email',
  2637. ]],
  2638. '/div',
  2639. ];
  2640. $this->assertHtml($expected, $result);
  2641. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2642. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2643. $result = $this->Form->control('phone');
  2644. $expected = [
  2645. 'div' => ['class' => 'input tel'],
  2646. 'label' => ['for' => 'phone'],
  2647. 'Phone',
  2648. '/label',
  2649. ['input' => [
  2650. 'type' => 'tel', 'name' => 'phone',
  2651. 'value' => 'Hello &amp; World &gt; weird chars',
  2652. 'id' => 'phone', 'maxlength' => 255,
  2653. ]],
  2654. '/div',
  2655. ];
  2656. $this->assertHtml($expected, $result);
  2657. $this->View->setRequest(
  2658. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2659. );
  2660. $this->Form->create();
  2661. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2662. $expected = [
  2663. 'div' => ['class' => 'input text'],
  2664. 'label' => ['for' => 'myId'],
  2665. 'Field',
  2666. '/label',
  2667. 'input' => [
  2668. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2669. 'value' => 'My value', 'id' => 'myId',
  2670. ],
  2671. '/div',
  2672. ];
  2673. $this->assertHtml($expected, $result);
  2674. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2675. $this->Form->create();
  2676. $entity->setError('field', 'Badness!');
  2677. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2678. $result = $this->Form->control('field');
  2679. $expected = [
  2680. 'div' => ['class' => 'input text error'],
  2681. 'label' => ['for' => 'field'],
  2682. 'Field',
  2683. '/label',
  2684. 'input' => [
  2685. 'type' => 'text',
  2686. 'name' => 'field',
  2687. 'id' => 'field',
  2688. 'class' => 'form-error',
  2689. 'aria-invalid' => 'true',
  2690. 'aria-describedby' => 'field-error',
  2691. ],
  2692. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2693. 'Badness!',
  2694. '/div',
  2695. '/div',
  2696. ];
  2697. $this->assertHtml($expected, $result);
  2698. $result = $this->Form->control('field', [
  2699. 'templates' => [
  2700. 'inputContainerError' => '{{content}}{{error}}',
  2701. 'error' => '<span class="error-message">{{content}}</span>',
  2702. ],
  2703. ]);
  2704. $expected = [
  2705. 'label' => ['for' => 'field'],
  2706. 'Field',
  2707. '/label',
  2708. 'input' => [
  2709. 'type' => 'text',
  2710. 'name' => 'field',
  2711. 'id' => 'field',
  2712. 'class' => 'form-error',
  2713. // No aria-describedby because error template is custom
  2714. 'aria-invalid' => 'true',
  2715. ],
  2716. ['span' => ['class' => 'error-message']],
  2717. 'Badness!',
  2718. '/span',
  2719. ];
  2720. $this->assertHtml($expected, $result);
  2721. $entity->setError('field', ['minLength'], true);
  2722. $result = $this->Form->control('field', [
  2723. 'error' => [
  2724. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2725. 'maxLength' => 'login too large',
  2726. ],
  2727. ]);
  2728. $expected = [
  2729. 'div' => ['class' => 'input text error'],
  2730. 'label' => ['for' => 'field'],
  2731. 'Field',
  2732. '/label',
  2733. 'input' => [
  2734. 'type' => 'text',
  2735. 'name' => 'field',
  2736. 'id' => 'field',
  2737. 'class' => 'form-error',
  2738. 'aria-invalid' => 'true',
  2739. 'aria-describedby' => 'field-error',
  2740. ],
  2741. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2742. 'Le login doit contenir au moins 2 caractères',
  2743. '/div',
  2744. '/div',
  2745. ];
  2746. $this->assertHtml($expected, $result);
  2747. $entity->setError('field', ['maxLength'], true);
  2748. $result = $this->Form->control('field', [
  2749. 'error' => [
  2750. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2751. 'maxLength' => 'login too large',
  2752. ],
  2753. ]);
  2754. $expected = [
  2755. 'div' => ['class' => 'input text error'],
  2756. 'label' => ['for' => 'field'],
  2757. 'Field',
  2758. '/label',
  2759. 'input' => [
  2760. 'type' => 'text',
  2761. 'name' => 'field',
  2762. 'id' => 'field',
  2763. 'class' => 'form-error',
  2764. 'aria-invalid' => 'true',
  2765. 'aria-describedby' => 'field-error',
  2766. ],
  2767. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2768. 'login too large',
  2769. '/div',
  2770. '/div',
  2771. ];
  2772. $this->assertHtml($expected, $result);
  2773. }
  2774. /**
  2775. * testControlWithTemplateFile method
  2776. *
  2777. * Test that control() accepts a template file.
  2778. */
  2779. public function testControlWithTemplateFile(): void
  2780. {
  2781. $result = $this->Form->control('field', [
  2782. 'templates' => 'htmlhelper_tags',
  2783. ]);
  2784. $expected = [
  2785. 'label' => ['for' => 'field'],
  2786. 'Field',
  2787. '/label',
  2788. 'input' => [
  2789. 'type' => 'text', 'name' => 'field',
  2790. 'id' => 'field',
  2791. ],
  2792. ];
  2793. $this->assertHtml($expected, $result);
  2794. }
  2795. /**
  2796. * testNestedControlsEndWithBrackets method
  2797. *
  2798. * Test that nested inputs end with brackets.
  2799. */
  2800. public function testNestedControlsEndWithBrackets(): void
  2801. {
  2802. $result = $this->Form->text('nested.text[]');
  2803. $expected = [
  2804. 'input' => [
  2805. 'type' => 'text', 'name' => 'nested[text][]',
  2806. ],
  2807. ];
  2808. $this->assertHtml($expected, $result);
  2809. $result = $this->Form->file('nested.file[]');
  2810. $expected = [
  2811. 'input' => [
  2812. 'type' => 'file', 'name' => 'nested[file][]',
  2813. ],
  2814. ];
  2815. $this->assertHtml($expected, $result);
  2816. }
  2817. /**
  2818. * testCreateIdPrefix method
  2819. *
  2820. * Test id prefix.
  2821. */
  2822. public function testCreateIdPrefix(): void
  2823. {
  2824. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2825. $result = $this->Form->control('field');
  2826. $expected = [
  2827. 'div' => ['class' => 'input text'],
  2828. 'label' => ['for' => 'prefix-field'],
  2829. 'Field',
  2830. '/label',
  2831. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2832. '/div',
  2833. ];
  2834. $this->assertHtml($expected, $result);
  2835. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2836. $expected = [
  2837. 'div' => ['class' => 'input text'],
  2838. 'label' => ['for' => 'custom-id'],
  2839. 'Field',
  2840. '/label',
  2841. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2842. '/div',
  2843. ];
  2844. $this->assertHtml($expected, $result);
  2845. $result = $this->Form->radio('Model.field', ['option A']);
  2846. $expected = [
  2847. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2848. 'label' => ['for' => 'prefix-model-field-0'],
  2849. ['input' => [
  2850. 'type' => 'radio',
  2851. 'name' => 'Model[field]',
  2852. 'value' => '0',
  2853. 'id' => 'prefix-model-field-0',
  2854. ]],
  2855. 'option A',
  2856. '/label',
  2857. ];
  2858. $this->assertHtml($expected, $result);
  2859. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2860. $expected = [
  2861. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2862. 'label' => ['for' => 'prefix-model-field-0'],
  2863. ['input' => [
  2864. 'type' => 'radio',
  2865. 'name' => 'Model[field]',
  2866. 'value' => '0',
  2867. 'id' => 'prefix-model-field-0',
  2868. ]],
  2869. 'option A',
  2870. '/label',
  2871. ];
  2872. $this->assertHtml($expected, $result);
  2873. $result = $this->Form->select(
  2874. 'Model.multi_field',
  2875. ['first'],
  2876. ['multiple' => 'checkbox']
  2877. );
  2878. $expected = [
  2879. 'input' => [
  2880. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2881. ],
  2882. ['div' => ['class' => 'checkbox']],
  2883. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2884. ['input' => [
  2885. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2886. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2887. ]],
  2888. 'first',
  2889. '/label',
  2890. '/div',
  2891. ];
  2892. $this->assertHtml($expected, $result);
  2893. $this->Form->end();
  2894. $result = $this->Form->control('field');
  2895. $expected = [
  2896. 'div' => ['class' => 'input text'],
  2897. 'label' => ['for' => 'field'],
  2898. 'Field',
  2899. '/label',
  2900. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2901. '/div',
  2902. ];
  2903. $this->assertHtml($expected, $result);
  2904. }
  2905. /**
  2906. * testControlZero method
  2907. *
  2908. * Test that inputs with 0 can be created.
  2909. */
  2910. public function testControlZero(): void
  2911. {
  2912. $this->getTableLocator()->get('Contacts', [
  2913. 'className' => ContactsTable::class,
  2914. ]);
  2915. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2916. $result = $this->Form->control('0');
  2917. $expected = [
  2918. 'div' => ['class' => 'input text'],
  2919. 'label' => ['for' => '0'], '/label',
  2920. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2921. '/div',
  2922. ];
  2923. $this->assertHtml($expected, $result);
  2924. }
  2925. /**
  2926. * testControlCheckbox method
  2927. *
  2928. * Test control() with checkbox creation.
  2929. */
  2930. public function testControlCheckbox(): void
  2931. {
  2932. $articles = $this->getTableLocator()->get('Articles');
  2933. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2934. $article = $articles->newEmptyEntity();
  2935. $this->Form->create($article);
  2936. $result = $this->Form->control('Articles.active');
  2937. $expected = [
  2938. 'div' => ['class' => 'input checkbox'],
  2939. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2940. 'label' => ['for' => 'articles-active'],
  2941. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2942. 'Active',
  2943. '/label',
  2944. '/div',
  2945. ];
  2946. $this->assertHtml($expected, $result);
  2947. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2948. $expected = [
  2949. 'div' => ['class' => 'input checkbox'],
  2950. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2951. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2952. '/div',
  2953. ];
  2954. $this->assertHtml($expected, $result);
  2955. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2956. $expected = [
  2957. 'div' => ['class' => 'input checkbox'],
  2958. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2959. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2960. '/div',
  2961. ];
  2962. $this->assertHtml($expected, $result);
  2963. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2964. $expected = [
  2965. 'div' => ['class' => 'input checkbox'],
  2966. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2967. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2968. '/div',
  2969. ];
  2970. $this->assertHtml($expected, $result);
  2971. $result = $this->Form->control('Articles.disabled', [
  2972. 'label' => 'Disabled',
  2973. 'type' => 'checkbox',
  2974. 'data-foo' => 'disabled',
  2975. ]);
  2976. $expected = [
  2977. 'div' => ['class' => 'input checkbox'],
  2978. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2979. 'label' => ['for' => 'articles-disabled'],
  2980. ['input' => [
  2981. 'type' => 'checkbox',
  2982. 'name' => 'Articles[disabled]',
  2983. 'value' => '1',
  2984. 'id' => 'articles-disabled',
  2985. 'data-foo' => 'disabled',
  2986. ]],
  2987. 'Disabled',
  2988. '/label',
  2989. '/div',
  2990. ];
  2991. $this->assertHtml($expected, $result);
  2992. $result = $this->Form->control('Articles.confirm', [
  2993. 'label' => 'Confirm <b>me</b>!',
  2994. 'type' => 'checkbox',
  2995. 'escape' => false,
  2996. ]);
  2997. $expected = [
  2998. 'div' => ['class' => 'input checkbox'],
  2999. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3000. 'label' => ['for' => 'articles-confirm'],
  3001. ['input' => [
  3002. 'type' => 'checkbox',
  3003. 'name' => 'Articles[confirm]',
  3004. 'value' => '1',
  3005. 'id' => 'articles-confirm',
  3006. ]],
  3007. 'Confirm <b>me</b>!',
  3008. '/label',
  3009. '/div',
  3010. ];
  3011. $this->assertHtml($expected, $result);
  3012. }
  3013. /**
  3014. * testControlHidden method
  3015. *
  3016. * Test that control() does not create wrapping div and label tag for hidden fields.
  3017. */
  3018. public function testControlHidden(): void
  3019. {
  3020. $this->getTableLocator()->get('ValidateUsers', [
  3021. 'className' => ValidateUsersTable::class,
  3022. ]);
  3023. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3024. $result = $this->Form->control('ValidateUser.id');
  3025. $expected = [
  3026. 'input' => ['name', 'type' => 'hidden', 'id'],
  3027. ];
  3028. $this->assertHtml($expected, $result);
  3029. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3030. $expected = [
  3031. 'input' => ['name', 'type' => 'hidden', 'id'],
  3032. ];
  3033. $this->assertHtml($expected, $result);
  3034. }
  3035. /**
  3036. * testControlDatetime method
  3037. *
  3038. * Test form->control() with datetime.
  3039. */
  3040. public function testControlDatetime(): void
  3041. {
  3042. $result = $this->Form->control('prueba', [
  3043. 'type' => 'datetime',
  3044. 'value' => new DateTime('2019-09-27 02:52:43'),
  3045. ]);
  3046. $expected = [
  3047. 'div' => ['class' => 'input datetime'],
  3048. 'label' => ['for' => 'prueba'],
  3049. 'Prueba',
  3050. '/label',
  3051. 'input' => [
  3052. 'name' => 'prueba',
  3053. 'id' => 'prueba',
  3054. 'type' => 'datetime-local',
  3055. 'value' => '2019-09-27T02:52:43',
  3056. 'step' => '1',
  3057. ],
  3058. '/div',
  3059. ];
  3060. $this->assertHtml($expected, $result);
  3061. }
  3062. /**
  3063. * testControlDatetimeIdPrefix method
  3064. *
  3065. * Test form->control() with datetime with id prefix.
  3066. */
  3067. public function testControlDatetimeIdPrefix(): void
  3068. {
  3069. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3070. $result = $this->Form->control('prueba', [
  3071. 'type' => 'datetime',
  3072. ]);
  3073. $expected = [
  3074. 'div' => ['class' => 'input datetime'],
  3075. 'label' => ['for' => 'prefix-prueba'],
  3076. 'Prueba',
  3077. '/label',
  3078. 'input' => [
  3079. 'name' => 'prueba',
  3080. 'id' => 'prefix-prueba',
  3081. 'type' => 'datetime-local',
  3082. 'value' => '',
  3083. 'step' => '1',
  3084. ],
  3085. '/div',
  3086. ];
  3087. $this->assertHtml($expected, $result);
  3088. }
  3089. /**
  3090. * testControlDatetimeStep method
  3091. *
  3092. * Test form->control() with datetime with custom step size.
  3093. */
  3094. public function testControlDatetimeStep(): void
  3095. {
  3096. $result = $this->Form->control('prueba', [
  3097. 'type' => 'datetime',
  3098. 'value' => new DateTime('2019-09-27 02:52:43'),
  3099. 'step' => '0.5',
  3100. ]);
  3101. $expected = [
  3102. 'div' => ['class' => 'input datetime'],
  3103. 'label' => ['for' => 'prueba'],
  3104. 'Prueba',
  3105. '/label',
  3106. 'input' => [
  3107. 'name' => 'prueba',
  3108. 'id' => 'prueba',
  3109. 'type' => 'datetime-local',
  3110. 'value' => '2019-09-27T02:52:43.000',
  3111. 'step' => '0.5',
  3112. ],
  3113. '/div',
  3114. ];
  3115. $this->assertHtml($expected, $result);
  3116. }
  3117. /**
  3118. * testControlCheckboxWithDisabledElements method
  3119. *
  3120. * Test generating checkboxes with disabled elements.
  3121. */
  3122. public function testControlCheckboxWithDisabledElements(): void
  3123. {
  3124. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3125. $result = $this->Form->control('Contact.multiple', [
  3126. 'multiple' => 'checkbox',
  3127. 'disabled' => 'disabled',
  3128. 'options' => $options,
  3129. ]);
  3130. $expected = [
  3131. ['div' => ['class' => 'input select']],
  3132. ['label' => ['for' => 'contact-multiple']],
  3133. 'Multiple',
  3134. '/label',
  3135. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3136. ['div' => ['class' => 'checkbox']],
  3137. ['label' => ['for' => 'contact-multiple-1']],
  3138. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3139. 'One',
  3140. '/label',
  3141. '/div',
  3142. ['div' => ['class' => 'checkbox']],
  3143. ['label' => ['for' => 'contact-multiple-2']],
  3144. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3145. 'Two',
  3146. '/label',
  3147. '/div',
  3148. ['div' => ['class' => 'checkbox']],
  3149. ['label' => ['for' => 'contact-multiple-3']],
  3150. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3151. 'Three',
  3152. '/label',
  3153. '/div',
  3154. '/div',
  3155. ];
  3156. $this->assertHtml($expected, $result);
  3157. // make sure 50 does only disable 50, and not 50f5c0cf
  3158. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3159. $disabled = [50];
  3160. $expected = [
  3161. ['div' => ['class' => 'input select']],
  3162. ['label' => ['for' => 'contact-multiple']],
  3163. 'Multiple',
  3164. '/label',
  3165. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3166. ['div' => ['class' => 'checkbox']],
  3167. ['label' => ['for' => 'contact-multiple-50']],
  3168. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3169. 'Fifty',
  3170. '/label',
  3171. '/div',
  3172. ['div' => ['class' => 'checkbox']],
  3173. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3174. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3175. 'Stringy',
  3176. '/label',
  3177. '/div',
  3178. '/div',
  3179. ];
  3180. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3181. $this->assertHtml($expected, $result);
  3182. }
  3183. /**
  3184. * testControlWithLeadingInteger method
  3185. *
  3186. * Test input name with leading integer, ensure attributes are generated correctly.
  3187. */
  3188. public function testControlWithLeadingInteger(): void
  3189. {
  3190. $result = $this->Form->text('0.Node.title');
  3191. $expected = [
  3192. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3193. ];
  3194. $this->assertHtml($expected, $result);
  3195. }
  3196. /**
  3197. * testControlSelectType method
  3198. *
  3199. * Test form->control() with select type inputs.
  3200. */
  3201. public function testControlSelectType(): void
  3202. {
  3203. $result = $this->Form->control(
  3204. 'email',
  3205. [
  3206. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3207. );
  3208. $expected = [
  3209. 'div' => ['class' => 'input select'],
  3210. 'label' => ['for' => 'email'],
  3211. 'Email',
  3212. '/label',
  3213. ['select' => ['name' => 'email', 'id' => 'email']],
  3214. ['option' => ['value' => '']],
  3215. '/option',
  3216. ['option' => ['value' => 'è']],
  3217. 'Firést',
  3218. '/option',
  3219. ['option' => ['value' => 'é']],
  3220. 'Secoènd',
  3221. '/option',
  3222. '/select',
  3223. '/div',
  3224. ];
  3225. $this->assertHtml($expected, $result);
  3226. $result = $this->Form->control(
  3227. 'email',
  3228. [
  3229. 'options' => ['First', 'Second'], 'empty' => true]
  3230. );
  3231. $expected = [
  3232. 'div' => ['class' => 'input select'],
  3233. 'label' => ['for' => 'email'],
  3234. 'Email',
  3235. '/label',
  3236. ['select' => ['name' => 'email', 'id' => 'email']],
  3237. ['option' => ['value' => '']],
  3238. '/option',
  3239. ['option' => ['value' => '0']],
  3240. 'First',
  3241. '/option',
  3242. ['option' => ['value' => '1']],
  3243. 'Second',
  3244. '/option',
  3245. '/select',
  3246. '/div',
  3247. ];
  3248. $this->assertHtml($expected, $result);
  3249. $result = $this->Form->control('email', [
  3250. 'type' => 'select',
  3251. 'options' => new ArrayObject(['First', 'Second']),
  3252. 'empty' => true,
  3253. ]);
  3254. $this->assertHtml($expected, $result);
  3255. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3256. $this->View->setRequest(
  3257. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3258. );
  3259. $this->Form->create();
  3260. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3261. $expected = [
  3262. 'div' => ['class' => 'input select'],
  3263. 'label' => ['for' => 'model-user-id'],
  3264. 'User',
  3265. '/label',
  3266. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3267. ['option' => ['value' => '']],
  3268. '/option',
  3269. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3270. 'good',
  3271. '/option',
  3272. ['option' => ['value' => 'other']],
  3273. 'bad',
  3274. '/option',
  3275. '/select',
  3276. '/div',
  3277. ];
  3278. $this->assertHtml($expected, $result);
  3279. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3280. $this->View->setRequest(
  3281. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3282. );
  3283. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3284. $expected = [
  3285. 'div' => ['class' => 'input select'],
  3286. 'label' => ['for' => 'thing-user-id'],
  3287. 'User',
  3288. '/label',
  3289. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3290. ['option' => ['value' => '']],
  3291. 'Some Empty',
  3292. '/option',
  3293. ['option' => ['value' => 'value']],
  3294. 'good',
  3295. '/option',
  3296. ['option' => ['value' => 'other']],
  3297. 'bad',
  3298. '/option',
  3299. '/select',
  3300. '/div',
  3301. ];
  3302. $this->assertHtml($expected, $result);
  3303. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3304. $this->View->setRequest(
  3305. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3306. );
  3307. $this->Form->create();
  3308. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3309. $expected = [
  3310. 'div' => ['class' => 'input select'],
  3311. 'label' => ['for' => 'thing-user-id'],
  3312. 'User',
  3313. '/label',
  3314. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3315. ['option' => ['value' => '']],
  3316. 'Some Empty',
  3317. '/option',
  3318. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3319. 'good',
  3320. '/option',
  3321. ['option' => ['value' => 'other']],
  3322. 'bad',
  3323. '/option',
  3324. '/select',
  3325. '/div',
  3326. ];
  3327. $this->assertHtml($expected, $result);
  3328. $result = $this->Form->control('Publisher.id', [
  3329. 'label' => 'Publisher',
  3330. 'type' => 'select',
  3331. 'multiple' => 'checkbox',
  3332. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3333. ]);
  3334. $expected = [
  3335. ['div' => ['class' => 'input select']],
  3336. ['label' => ['for' => 'publisher-id']],
  3337. 'Publisher',
  3338. '/label',
  3339. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3340. ['div' => ['class' => 'checkbox']],
  3341. ['label' => ['for' => 'publisher-id-value-1']],
  3342. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3343. 'Label 1',
  3344. '/label',
  3345. '/div',
  3346. ['div' => ['class' => 'checkbox']],
  3347. ['label' => ['for' => 'publisher-id-value-2']],
  3348. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3349. 'Label 2',
  3350. '/label',
  3351. '/div',
  3352. '/div',
  3353. ];
  3354. $this->assertHtml($expected, $result);
  3355. $articlesTable = $this->getTableLocator()->get('Articles');
  3356. $articlesTable->getSchema()->setColumnType(
  3357. 'published',
  3358. EnumType::from(ArticleStatus::class)
  3359. );
  3360. $this->Form->create($articlesTable->newEmptyEntity());
  3361. $result = $this->Form->control('published');
  3362. $expected = [
  3363. 'div' => ['class' => 'input select'],
  3364. 'label' => ['for' => 'published'],
  3365. 'Published',
  3366. '/label',
  3367. 'select' => ['name' => 'published', 'id' => 'published'],
  3368. ['option' => ['value' => 'Y']],
  3369. 'Published',
  3370. '/option',
  3371. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3372. 'Unpublished',
  3373. '/option',
  3374. '/select',
  3375. '/div',
  3376. ];
  3377. $this->assertHtml($expected, $result);
  3378. $articlesTable->getSchema()->setColumnType(
  3379. 'published',
  3380. EnumType::from(ArticleStatusLabel::class)
  3381. );
  3382. $this->Form->create($articlesTable->newEmptyEntity());
  3383. $result = $this->Form->control('published');
  3384. $expected = [
  3385. 'div' => ['class' => 'input select'],
  3386. 'label' => ['for' => 'published'],
  3387. 'Published',
  3388. '/label',
  3389. 'select' => ['name' => 'published', 'id' => 'published'],
  3390. ['option' => ['value' => 'Y']],
  3391. 'Is Published',
  3392. '/option',
  3393. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3394. 'Is Unpublished',
  3395. '/option',
  3396. '/select',
  3397. '/div',
  3398. ];
  3399. $this->assertHtml($expected, $result);
  3400. $articlesTable->getSchema()->setColumnType(
  3401. 'published',
  3402. EnumType::from(ArticleStatusLabelInterface::class)
  3403. );
  3404. $this->Form->create($articlesTable->newEmptyEntity());
  3405. $result = $this->Form->control('published');
  3406. $this->assertHtml($expected, $result);
  3407. $articlePriosTable = $this->getTableLocator()->get('ArticlePrios');
  3408. $articlePriosTable->getSchema()->setColumnType(
  3409. 'priority',
  3410. EnumType::from(Priority::class)
  3411. );
  3412. $this->Form->create($articlePriosTable->newEmptyEntity());
  3413. // Select empty by default
  3414. $result = $this->Form->control('priority', ['empty' => ['' => ' - please select - '], 'default' => '']);
  3415. $expected = [
  3416. 'div' => ['class' => 'input select'],
  3417. 'label' => ['for' => 'priority'],
  3418. 'Priority',
  3419. '/label',
  3420. 'select' => ['name' => 'priority', 'id' => 'priority'],
  3421. ['option' => ['value' => '', 'selected' => 'selected']],
  3422. ' - please select - ',
  3423. '/option',
  3424. ['option' => ['value' => '1']],
  3425. 'Is Low',
  3426. '/option',
  3427. ['option' => ['value' => '2']],
  3428. 'Is Medium',
  3429. '/option',
  3430. ['option' => ['value' => '3']],
  3431. 'Is High',
  3432. '/option',
  3433. '/select',
  3434. '/div',
  3435. ];
  3436. $this->assertHtml($expected, $result);
  3437. }
  3438. /**
  3439. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3440. *
  3441. * Test that control() and a non standard primary key makes a hidden input by default.
  3442. */
  3443. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3444. {
  3445. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3446. $this->Form->create($this->article);
  3447. $result = $this->Form->control('title');
  3448. $expected = [
  3449. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3450. ];
  3451. $this->assertHtml($expected, $result);
  3452. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3453. $this->Form->create($this->article);
  3454. $result = $this->Form->control('title');
  3455. $expected = [
  3456. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3457. ];
  3458. $this->assertHtml($expected, $result);
  3459. $result = $this->Form->control('body');
  3460. $expected = [
  3461. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3462. ];
  3463. $this->assertHtml($expected, $result);
  3464. }
  3465. /**
  3466. * testControlOverridingMagicSelectType method
  3467. *
  3468. * Test that overriding the magic select type widget is possible.
  3469. */
  3470. public function testControlOverridingMagicSelectType(): void
  3471. {
  3472. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3473. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3474. $expected = [
  3475. 'div' => ['class' => 'input text'],
  3476. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3477. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3478. '/div',
  3479. ];
  3480. $this->assertHtml($expected, $result);
  3481. //Check that magic types still work for plural/singular vars
  3482. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3483. $result = $this->Form->control('Model.type');
  3484. $expected = [
  3485. 'div' => ['class' => 'input select'],
  3486. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3487. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3488. ['option' => ['value' => 'value']], 'good', '/option',
  3489. ['option' => ['value' => 'other']], 'bad', '/option',
  3490. '/select',
  3491. '/div',
  3492. ];
  3493. $this->assertHtml($expected, $result);
  3494. }
  3495. /**
  3496. * testControlMagicTypeDoesNotOverride method
  3497. *
  3498. * Test that inferred types do not override developer input.
  3499. */
  3500. public function testControlMagicTypeDoesNotOverride(): void
  3501. {
  3502. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3503. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3504. $expected = [
  3505. 'div' => ['class' => 'input checkbox'],
  3506. ['input' => [
  3507. 'type' => 'hidden',
  3508. 'name' => 'Model[user]',
  3509. 'value' => 0,
  3510. ]],
  3511. 'label' => ['for' => 'model-user'],
  3512. ['input' => [
  3513. 'name' => 'Model[user]',
  3514. 'type' => 'checkbox',
  3515. 'id' => 'model-user',
  3516. 'value' => 1,
  3517. ]],
  3518. 'User',
  3519. '/label',
  3520. '/div',
  3521. ];
  3522. $this->assertHtml($expected, $result);
  3523. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3524. $options = [
  3525. 1 => 'blue',
  3526. 2 => 'red',
  3527. ];
  3528. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3529. $expected = [
  3530. 'div' => ['class' => 'input select'],
  3531. 'label' => ['for' => 'tags-ids'],
  3532. 'Tags',
  3533. '/label',
  3534. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3535. ['div' => ['class' => 'checkbox']],
  3536. ['label' => ['for' => 'tags-ids-1']],
  3537. ['input' => [
  3538. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3539. 'value' => '1', 'name' => 'tags[_ids][]',
  3540. ]],
  3541. 'blue',
  3542. '/label',
  3543. '/div',
  3544. ['div' => ['class' => 'checkbox']],
  3545. ['label' => ['for' => 'tags-ids-2']],
  3546. ['input' => [
  3547. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3548. 'value' => '2', 'name' => 'tags[_ids][]',
  3549. ]],
  3550. 'red',
  3551. '/label',
  3552. '/div',
  3553. '/div',
  3554. ];
  3555. $this->assertHtml($expected, $result);
  3556. }
  3557. /**
  3558. * testControlMagicSelectForTypeNumber method
  3559. *
  3560. * Test that magic control() selects are created for type=number.
  3561. */
  3562. public function testControlMagicSelectForTypeNumber(): void
  3563. {
  3564. $this->getTableLocator()->get('ValidateUsers', [
  3565. 'className' => ValidateUsersTable::class,
  3566. ]);
  3567. $entity = new Entity(['balance' => 1]);
  3568. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3569. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3570. $result = $this->Form->control('balance');
  3571. $expected = [
  3572. 'div' => ['class' => 'input select'],
  3573. 'label' => ['for' => 'balance'],
  3574. 'Balance',
  3575. '/label',
  3576. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3577. ['option' => ['value' => '0']],
  3578. 'nothing',
  3579. '/option',
  3580. ['option' => ['value' => '1', 'selected' => 'selected']],
  3581. 'some',
  3582. '/option',
  3583. ['option' => ['value' => '100']],
  3584. 'a lot',
  3585. '/option',
  3586. '/select',
  3587. '/div',
  3588. ];
  3589. $this->assertHtml($expected, $result);
  3590. }
  3591. /**
  3592. * testInvalidControlTypeOption method
  3593. *
  3594. * Test invalid 'input' type option to control() function.
  3595. */
  3596. public function testInvalidControlTypeOption(): void
  3597. {
  3598. $this->expectException(InvalidArgumentException::class);
  3599. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3600. $this->Form->control('text', ['type' => 'input']);
  3601. }
  3602. /**
  3603. * testControlMagicSelectChangeToRadio method
  3604. *
  3605. * Test that magic control() selects can easily be converted into radio types without error.
  3606. */
  3607. public function testControlMagicSelectChangeToRadio(): void
  3608. {
  3609. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3610. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3611. $this->assertStringContainsString('input type="radio"', $result);
  3612. }
  3613. /**
  3614. * testFormControlSubmit method
  3615. *
  3616. * Test correct results for form::control() and type submit.
  3617. */
  3618. public function testFormControlSubmit(): void
  3619. {
  3620. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3621. $expected = [
  3622. 'div' => ['class' => 'submit'],
  3623. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3624. '/div',
  3625. ];
  3626. $this->assertHtml($expected, $result);
  3627. }
  3628. /**
  3629. * testFormControls method
  3630. *
  3631. * Test correct results from Form::controls().
  3632. */
  3633. public function testFormControlsLegendFieldset(): void
  3634. {
  3635. $this->Form->create($this->article);
  3636. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3637. $expected = [
  3638. '<fieldset',
  3639. '<legend',
  3640. 'The Legend',
  3641. '/legend',
  3642. '*/fieldset',
  3643. ];
  3644. $this->assertHtml($expected, $result);
  3645. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3646. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3647. $this->assertStringContainsString('<fieldset>', $result);
  3648. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3649. $this->assertStringNotContainsString('<legend>', $result);
  3650. $this->assertStringNotContainsString('<fieldset>', $result);
  3651. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3652. $this->assertStringNotContainsString('<legend>', $result);
  3653. $this->assertStringNotContainsString('<fieldset>', $result);
  3654. $this->Form->create($this->article);
  3655. $this->View->setRequest($this->View->getRequest()
  3656. ->withParam('prefix', 'admin')
  3657. ->withParam('action', 'admin_edit')
  3658. ->withParam('controller', 'articles'));
  3659. $result = $this->Form->allControls();
  3660. $expected = [
  3661. '<fieldset',
  3662. '<legend',
  3663. 'New Article',
  3664. '/legend',
  3665. '*/fieldset',
  3666. ];
  3667. $this->assertHtml($expected, $result);
  3668. $this->Form->create($this->article);
  3669. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3670. $expected = [
  3671. '<fieldset',
  3672. '<legend',
  3673. 'The Legend',
  3674. '/legend',
  3675. '*/fieldset',
  3676. ];
  3677. $this->assertHtml($expected, $result);
  3678. $this->Form->create($this->article);
  3679. $result = $this->Form->allControls([], [
  3680. 'fieldset' => [
  3681. 'class' => 'some-class some-other-class',
  3682. 'disabled' => true,
  3683. 'data-param' => 'a-param',
  3684. ],
  3685. 'legend' => 'The Legend',
  3686. ]);
  3687. $expected = [
  3688. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3689. '<legend',
  3690. 'The Legend',
  3691. '/legend',
  3692. '*/fieldset',
  3693. ];
  3694. $this->assertHtml($expected, $result);
  3695. }
  3696. /**
  3697. * testFormControls method
  3698. *
  3699. * Test the controls() method.
  3700. */
  3701. public function testFormControls(): void
  3702. {
  3703. $this->Form->create($this->article);
  3704. $result = $this->Form->allControls();
  3705. $expected = [
  3706. '<fieldset',
  3707. '<legend', 'New Article', '/legend',
  3708. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3709. ['div' => ['class' => 'input select required']],
  3710. '*/div',
  3711. ['div' => ['class' => 'input text required']],
  3712. '*/div',
  3713. ['div' => ['class' => 'input text']],
  3714. '*/div',
  3715. ['div' => ['class' => 'input text']],
  3716. '*/div',
  3717. '/fieldset',
  3718. ];
  3719. $this->assertHtml($expected, $result);
  3720. $result = $this->Form->allControls([
  3721. 'published' => ['type' => 'boolean'],
  3722. ]);
  3723. $expected = [
  3724. '<fieldset',
  3725. '<legend', 'New Article', '/legend',
  3726. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3727. ['div' => ['class' => 'input select required']],
  3728. '*/div',
  3729. ['div' => ['class' => 'input text required']],
  3730. '*/div',
  3731. ['div' => ['class' => 'input text']],
  3732. '*/div',
  3733. ['div' => ['class' => 'input boolean']],
  3734. '*/div',
  3735. '/fieldset',
  3736. ];
  3737. $this->assertHtml($expected, $result);
  3738. $this->Form->create($this->article);
  3739. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3740. $expected = [
  3741. 'fieldset' => [],
  3742. 'legend' => [],
  3743. 'Hello',
  3744. '/legend',
  3745. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3746. ['div' => ['class' => 'input select required']],
  3747. '*/div',
  3748. ['div' => ['class' => 'input text required']],
  3749. '*/div',
  3750. ['div' => ['class' => 'input text']],
  3751. '*/div',
  3752. ['div' => ['class' => 'input text']],
  3753. '*/div',
  3754. '/fieldset',
  3755. ];
  3756. $this->assertHtml($expected, $result);
  3757. $this->Form->create();
  3758. $expected = [
  3759. 'fieldset' => [],
  3760. ['div' => ['class' => 'input text']],
  3761. 'label' => ['for' => 'foo'],
  3762. 'Foo',
  3763. '/label',
  3764. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3765. '*/div',
  3766. '/fieldset',
  3767. ];
  3768. $result = $this->Form->allControls(
  3769. ['foo' => ['type' => 'text']],
  3770. ['legend' => false]
  3771. );
  3772. $this->assertHtml($expected, $result);
  3773. }
  3774. /**
  3775. * testFormControlsBlacklist method
  3776. */
  3777. public function testFormControlsBlacklist(): void
  3778. {
  3779. $this->Form->create($this->article);
  3780. $result = $this->Form->allControls([
  3781. 'id' => false,
  3782. ]);
  3783. $expected = [
  3784. '<fieldset',
  3785. '<legend', 'New Article', '/legend',
  3786. ['div' => ['class' => 'input select required']],
  3787. '*/div',
  3788. ['div' => ['class' => 'input text required']],
  3789. '*/div',
  3790. ['div' => ['class' => 'input text']],
  3791. '*/div',
  3792. ['div' => ['class' => 'input text']],
  3793. '*/div',
  3794. '/fieldset',
  3795. ];
  3796. $this->assertHtml($expected, $result);
  3797. $this->Form->create($this->article);
  3798. $result = $this->Form->allControls([
  3799. 'id' => [],
  3800. ]);
  3801. $expected = [
  3802. '<fieldset',
  3803. '<legend', 'New Article', '/legend',
  3804. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3805. ['div' => ['class' => 'input select required']],
  3806. '*/div',
  3807. ['div' => ['class' => 'input text required']],
  3808. '*/div',
  3809. ['div' => ['class' => 'input text']],
  3810. '*/div',
  3811. ['div' => ['class' => 'input text']],
  3812. '*/div',
  3813. '/fieldset',
  3814. ];
  3815. $this->assertHtml($expected, $result, true);
  3816. }
  3817. /**
  3818. * testSelectAsCheckbox method
  3819. *
  3820. * Test multi-select widget with checkbox formatting.
  3821. */
  3822. public function testSelectAsCheckbox(): void
  3823. {
  3824. $result = $this->Form->select(
  3825. 'Model.multi_field',
  3826. ['first', 'second', 'third'],
  3827. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3828. );
  3829. $expected = [
  3830. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3831. ['div' => ['class' => 'checkbox']],
  3832. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3833. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3834. 'first',
  3835. '/label',
  3836. '/div',
  3837. ['div' => ['class' => 'checkbox']],
  3838. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3839. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3840. 'second',
  3841. '/label',
  3842. '/div',
  3843. ['div' => ['class' => 'checkbox']],
  3844. ['label' => ['for' => 'model-multi-field-2']],
  3845. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3846. 'third',
  3847. '/label',
  3848. '/div',
  3849. ];
  3850. $this->assertHtml($expected, $result);
  3851. $result = $this->Form->select(
  3852. 'Model.multi_field',
  3853. ['1/2' => 'half'],
  3854. ['multiple' => 'checkbox']
  3855. );
  3856. $expected = [
  3857. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3858. ['div' => ['class' => 'checkbox']],
  3859. ['label' => ['for' => 'model-multi-field-1-2']],
  3860. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3861. 'half',
  3862. '/label',
  3863. '/div',
  3864. ];
  3865. $this->assertHtml($expected, $result);
  3866. }
  3867. /**
  3868. * testLabel method
  3869. *
  3870. * Test label generation.
  3871. */
  3872. public function testLabel(): void
  3873. {
  3874. $result = $this->Form->label('Person.name');
  3875. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3876. $this->assertHtml($expected, $result);
  3877. $result = $this->Form->label('Person.name');
  3878. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3879. $this->assertHtml($expected, $result);
  3880. $result = $this->Form->label('Person.first_name');
  3881. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3882. $this->assertHtml($expected, $result);
  3883. $result = $this->Form->label('Person.first_name', 'Your first name');
  3884. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3885. $this->assertHtml($expected, $result);
  3886. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3887. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3888. $this->assertHtml($expected, $result);
  3889. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3890. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3891. $this->assertHtml($expected, $result);
  3892. $result = $this->Form->label('Person.first_name', '');
  3893. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3894. $this->assertHtml($expected, $result);
  3895. $result = $this->Form->label('Person.2.name', '');
  3896. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3897. $this->assertHtml($expected, $result);
  3898. }
  3899. /**
  3900. * testLabelContainControl method
  3901. *
  3902. * Test that label() can accept an input with the correct template vars.
  3903. */
  3904. public function testLabelContainControl(): void
  3905. {
  3906. $this->Form->setTemplates([
  3907. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3908. ]);
  3909. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3910. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3911. ]);
  3912. $expected = [
  3913. 'label' => ['for' => 'person-accept-terms'],
  3914. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3915. 'Accept',
  3916. '/label',
  3917. ];
  3918. $this->assertHtml($expected, $result);
  3919. }
  3920. /**
  3921. * testTextbox method
  3922. *
  3923. * Test textbox element generation.
  3924. */
  3925. public function testTextbox(): void
  3926. {
  3927. $result = $this->Form->text('Model.field');
  3928. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3929. $this->assertHtml($expected, $result);
  3930. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3931. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3932. $this->assertHtml($expected, $result);
  3933. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3934. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3935. $this->assertHtml($expected, $result);
  3936. }
  3937. /**
  3938. * testTextBoxDataAndError method
  3939. *
  3940. * Test that text() hooks up with request data and error fields.
  3941. */
  3942. public function testTextBoxDataAndError(): void
  3943. {
  3944. $this->article['errors'] = [
  3945. 'Contact' => ['text' => 'wrong'],
  3946. ];
  3947. $this->View->setRequest($this->View->getRequest()
  3948. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3949. ->withData('Contact.text', 'test'));
  3950. $this->Form->create($this->article);
  3951. $result = $this->Form->text('Model.text');
  3952. $expected = [
  3953. 'input' => [
  3954. 'type' => 'text',
  3955. 'name' => 'Model[text]',
  3956. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3957. ],
  3958. ];
  3959. $this->assertHtml($expected, $result);
  3960. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3961. $expected = [
  3962. 'input' => [
  3963. 'type' => 'text',
  3964. 'name' => 'Contact[text]',
  3965. 'value' => 'test',
  3966. 'id' => 'theID',
  3967. 'class' => 'form-error',
  3968. ],
  3969. ];
  3970. $this->assertHtml($expected, $result);
  3971. }
  3972. /**
  3973. * testDefaultValue method
  3974. *
  3975. * Test default value setting.
  3976. */
  3977. public function testTextDefaultValue(): void
  3978. {
  3979. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3980. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3981. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3982. $this->assertHtml($expected, $result);
  3983. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3984. $this->Form->create();
  3985. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3986. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3987. $this->assertHtml($expected, $result);
  3988. $Articles = $this->getTableLocator()->get('Articles');
  3989. $title = $Articles->getSchema()->getColumn('title');
  3990. $Articles->getSchema()->addColumn(
  3991. 'title',
  3992. ['default' => 'default title', 'length' => 255] + $title
  3993. );
  3994. $entity = $Articles->newEmptyEntity();
  3995. $this->Form->create($entity);
  3996. // Get default value from schema
  3997. $result = $this->Form->text('title');
  3998. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3999. $this->assertHtml($expected, $result);
  4000. // Don't get value from schema
  4001. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4002. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  4003. $this->assertHtml($expected, $result);
  4004. // Custom default value overrides default value from schema
  4005. $result = $this->Form->text('title', ['default' => 'override default']);
  4006. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  4007. $this->assertHtml($expected, $result);
  4008. // Default value from schema is used only for new entities.
  4009. $entity->setNew(false);
  4010. $result = $this->Form->text('title');
  4011. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  4012. $this->assertHtml($expected, $result);
  4013. }
  4014. /**
  4015. * testError method
  4016. *
  4017. * Test field error generation.
  4018. */
  4019. public function testError(): void
  4020. {
  4021. $this->article['errors'] = [
  4022. 'Article' => ['field' => 'email'],
  4023. ];
  4024. $this->Form->create($this->article);
  4025. $result = $this->Form->error('Article.field');
  4026. $expected = [
  4027. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4028. 'email',
  4029. '/div',
  4030. ];
  4031. $this->assertHtml($expected, $result);
  4032. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4033. $expected = [
  4034. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4035. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4036. '/div',
  4037. ];
  4038. $this->assertHtml($expected, $result);
  4039. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4040. $expected = [
  4041. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4042. '<strong', 'Badness!', '/strong',
  4043. '/div',
  4044. ];
  4045. $this->assertHtml($expected, $result);
  4046. }
  4047. /**
  4048. * testErrorRuleName method
  4049. *
  4050. * Test error translation can use rule names for translating.
  4051. */
  4052. public function testErrorRuleName(): void
  4053. {
  4054. $this->article['errors'] = [
  4055. 'Article' => [
  4056. 'field' => ['email' => 'Your email was not good'],
  4057. ],
  4058. ];
  4059. $this->Form->create($this->article);
  4060. $result = $this->Form->error('Article.field');
  4061. $expected = [
  4062. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4063. 'Your email was not good',
  4064. '/div',
  4065. ];
  4066. $this->assertHtml($expected, $result);
  4067. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4068. $expected = [
  4069. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4070. 'Email in use',
  4071. '/div',
  4072. ];
  4073. $this->assertHtml($expected, $result);
  4074. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4075. $expected = [
  4076. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4077. 'Email in use',
  4078. '/div',
  4079. ];
  4080. $this->assertHtml($expected, $result);
  4081. $result = $this->Form->error('Article.field', [
  4082. 'email' => 'Key is preferred',
  4083. 'Your email was not good' => 'Email in use',
  4084. ]);
  4085. $expected = [
  4086. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4087. 'Key is preferred',
  4088. '/div',
  4089. ];
  4090. $this->assertHtml($expected, $result);
  4091. }
  4092. /**
  4093. * testErrorMessages method
  4094. *
  4095. * Test error with nested lists.
  4096. */
  4097. public function testErrorMessages(): void
  4098. {
  4099. $this->article['errors'] = [
  4100. 'Article' => ['field' => 'email'],
  4101. ];
  4102. $this->Form->create($this->article);
  4103. $result = $this->Form->error('Article.field', [
  4104. 'email' => 'No good!',
  4105. ]);
  4106. $expected = [
  4107. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4108. 'No good!',
  4109. '/div',
  4110. ];
  4111. $this->assertHtml($expected, $result);
  4112. }
  4113. /**
  4114. * testErrorMultipleMessages method
  4115. *
  4116. * Test error() with multiple messages.
  4117. */
  4118. public function testErrorMultipleMessages(): void
  4119. {
  4120. $this->article['errors'] = [
  4121. 'field' => ['notBlank', 'email', 'Something else'],
  4122. ];
  4123. $this->Form->create($this->article);
  4124. $result = $this->Form->error('field', [
  4125. 'notBlank' => 'Cannot be empty',
  4126. 'email' => 'No good!',
  4127. ]);
  4128. $expected = [
  4129. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4130. 'ul' => [],
  4131. '<li', 'Cannot be empty', '/li',
  4132. '<li', 'No good!', '/li',
  4133. '<li', 'Something else', '/li',
  4134. '/ul',
  4135. '/div',
  4136. ];
  4137. $this->assertHtml($expected, $result);
  4138. }
  4139. /**
  4140. * testPassword method
  4141. *
  4142. * Test password element generation.
  4143. */
  4144. public function testPassword(): void
  4145. {
  4146. $this->article['errors'] = [
  4147. 'Contact' => [
  4148. 'passwd' => 1,
  4149. ],
  4150. ];
  4151. $this->Form->create($this->article);
  4152. $result = $this->Form->password('Contact.field');
  4153. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4154. $this->assertHtml($expected, $result);
  4155. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4156. $this->Form->create($this->article);
  4157. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4158. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4159. $this->assertHtml($expected, $result);
  4160. }
  4161. /**
  4162. * testRadio method
  4163. *
  4164. * Test radio element set generation.
  4165. */
  4166. public function testRadio(): void
  4167. {
  4168. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4169. $expected = [
  4170. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4171. ['label' => ['for' => 'model-field-0']],
  4172. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4173. 'option A',
  4174. '/label',
  4175. ['label' => ['for' => 'model-field-1']],
  4176. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4177. 'option B',
  4178. '/label',
  4179. ];
  4180. $this->assertHtml($expected, $result);
  4181. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4182. $this->assertHtml($expected, $result);
  4183. $result = $this->Form->radio(
  4184. 'Employee.vegetarian',
  4185. ['yes' => 'Yes', 'no' => 'No'],
  4186. ['form' => 'my-form', 'id' => 'id-veg']
  4187. );
  4188. $expected = [
  4189. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4190. ['label' => ['for' => 'id-veg-yes']],
  4191. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4192. 'Yes',
  4193. '/label',
  4194. ['label' => ['for' => 'id-veg-no']],
  4195. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4196. 'No',
  4197. '/label',
  4198. ];
  4199. $this->assertHtml($expected, $result);
  4200. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4201. $expected = [
  4202. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4203. ['label' => ['for' => 'model-custom-0']],
  4204. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4205. 'option A',
  4206. '/label',
  4207. ['label' => ['for' => 'model-custom-1']],
  4208. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4209. 'option B',
  4210. '/label',
  4211. ];
  4212. $this->assertHtml($expected, $result);
  4213. $result = $this->Form->radio(
  4214. 'Employee.gender',
  4215. [
  4216. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4217. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4218. ]
  4219. );
  4220. $expected = [
  4221. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4222. ['label' => ['for' => 'employee-gender-male']],
  4223. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4224. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4225. 'Male',
  4226. '/label',
  4227. ['label' => ['for' => 'employee-gender-female']],
  4228. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4229. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4230. 'Female',
  4231. '/label',
  4232. ];
  4233. $this->assertHtml($expected, $result);
  4234. $article = new Article([
  4235. 'status' => ArticleStatus::Unpublished,
  4236. ]);
  4237. $this->Form->create($article);
  4238. $result = $this->Form->radio('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4239. $expected = [
  4240. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => '', 'id' => 'status'],
  4241. ['label' => ['for' => 'status-y']],
  4242. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'Y', 'id' => 'status-y']],
  4243. 'Published',
  4244. '/label',
  4245. ['label' => ['for' => 'status-n']],
  4246. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'N', 'id' => 'status-n', 'checked' => 'checked']],
  4247. 'Unpublished',
  4248. '/label',
  4249. ];
  4250. $this->assertHtml($expected, $result);
  4251. }
  4252. /**
  4253. * Test radio with complex options and empty disabled data.
  4254. */
  4255. public function testRadioComplexDisabled(): void
  4256. {
  4257. $options = [
  4258. ['value' => 'r', 'text' => 'red'],
  4259. ['value' => 'b', 'text' => 'blue'],
  4260. ];
  4261. $attrs = ['disabled' => []];
  4262. $result = $this->Form->radio('Model.field', $options, $attrs);
  4263. $expected = [
  4264. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4265. ['label' => ['for' => 'model-field-r']],
  4266. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4267. 'red',
  4268. '/label',
  4269. ['label' => ['for' => 'model-field-b']],
  4270. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4271. 'blue',
  4272. '/label',
  4273. ];
  4274. $this->assertHtml($expected, $result);
  4275. $attrs = ['disabled' => ['r']];
  4276. $result = $this->Form->radio('Model.field', $options, $attrs);
  4277. $this->assertStringContainsString('disabled="disabled"', $result);
  4278. }
  4279. /**
  4280. * testRadioDefaultValue method
  4281. *
  4282. * Test default value setting on radio() method.
  4283. */
  4284. public function testRadioDefaultValue(): void
  4285. {
  4286. $Articles = $this->getTableLocator()->get('Articles');
  4287. $title = $Articles->getSchema()->getColumn('title');
  4288. $Articles->getSchema()->addColumn(
  4289. 'title',
  4290. ['default' => '1'] + $title
  4291. );
  4292. $this->Form->create($Articles->newEmptyEntity());
  4293. $result = $this->Form->radio('title', ['option A', 'option B']);
  4294. $expected = [
  4295. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4296. ['label' => ['for' => 'title-0']],
  4297. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4298. 'option A',
  4299. '/label',
  4300. ['label' => ['for' => 'title-1']],
  4301. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4302. 'option B',
  4303. '/label',
  4304. ];
  4305. $this->assertHtml($expected, $result);
  4306. }
  4307. /**
  4308. * Test setting a hiddenField value on radio buttons.
  4309. */
  4310. public function testRadioHiddenFieldValue(): void
  4311. {
  4312. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4313. $expected = [
  4314. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4315. 'label' => ['for' => 'title-0'],
  4316. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4317. 'option A',
  4318. '/label',
  4319. ];
  4320. $this->assertHtml($expected, $result);
  4321. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4322. $expected = [
  4323. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4324. 'label' => ['for' => 'title-0'],
  4325. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4326. 'option A',
  4327. '/label',
  4328. ];
  4329. $this->assertHtml($expected, $result);
  4330. }
  4331. /**
  4332. * testControlRadio method
  4333. *
  4334. * Test that input works with radio types.
  4335. */
  4336. public function testControlRadio(): void
  4337. {
  4338. $result = $this->Form->control('test', [
  4339. 'type' => 'radio',
  4340. 'options' => ['A', 'B'],
  4341. ]);
  4342. $expected = [
  4343. ['div' => ['class' => 'input radio']],
  4344. '<label',
  4345. 'Test',
  4346. '/label',
  4347. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4348. ['label' => ['for' => 'test-0']],
  4349. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4350. 'A',
  4351. '/label',
  4352. ['label' => ['for' => 'test-1']],
  4353. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4354. 'B',
  4355. '/label',
  4356. '/div',
  4357. ];
  4358. $this->assertHtml($expected, $result);
  4359. $result = $this->Form->control('test', [
  4360. 'type' => 'radio',
  4361. 'options' => ['A', 'B'],
  4362. 'value' => '0',
  4363. ]);
  4364. $expected = [
  4365. ['div' => ['class' => 'input radio']],
  4366. '<label',
  4367. 'Test',
  4368. '/label',
  4369. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4370. ['label' => ['for' => 'test-0']],
  4371. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4372. 'A',
  4373. '/label',
  4374. ['label' => ['for' => 'test-1']],
  4375. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4376. 'B',
  4377. '/label',
  4378. '/div',
  4379. ];
  4380. $this->assertHtml($expected, $result);
  4381. $result = $this->Form->control('test', [
  4382. 'type' => 'radio',
  4383. 'options' => ['A', 'B'],
  4384. 'label' => false,
  4385. ]);
  4386. $expected = [
  4387. ['div' => ['class' => 'input radio']],
  4388. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4389. ['label' => ['for' => 'test-0']],
  4390. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4391. 'A',
  4392. '/label',
  4393. ['label' => ['for' => 'test-1']],
  4394. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4395. 'B',
  4396. '/label',
  4397. '/div',
  4398. ];
  4399. $this->assertHtml($expected, $result);
  4400. $result = $this->Form->control('accept', [
  4401. 'type' => 'radio',
  4402. 'options' => [
  4403. 1 => 'positive',
  4404. -1 => 'negative',
  4405. ],
  4406. 'label' => false,
  4407. ]);
  4408. $expected = [
  4409. ['div' => ['class' => 'input radio']],
  4410. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4411. ['label' => ['for' => 'accept-1']],
  4412. ['input' => [
  4413. 'type' => 'radio',
  4414. 'name' => 'accept',
  4415. 'value' => '1',
  4416. 'id' => 'accept-1',
  4417. ]],
  4418. 'positive',
  4419. '/label',
  4420. ['label' => ['for' => 'accept--1']],
  4421. ['input' => [
  4422. 'type' => 'radio',
  4423. 'name' => 'accept',
  4424. 'value' => '-1',
  4425. 'id' => 'accept--1',
  4426. ]],
  4427. 'negative',
  4428. '/label',
  4429. '/div',
  4430. ];
  4431. $this->assertHtml($expected, $result);
  4432. $articlesTable = $this->getTableLocator()->get('Articles');
  4433. $articlesTable->getSchema()->setColumnType(
  4434. 'published',
  4435. EnumType::from(ArticleStatus::class)
  4436. );
  4437. $this->Form->create($articlesTable->newEmptyEntity());
  4438. $result = $this->Form->control('published', ['type' => 'radio', 'label' => false,]);
  4439. $expected = [
  4440. ['div' => ['class' => 'input radio']],
  4441. 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => '', 'id' => 'published'],
  4442. ['label' => ['for' => 'published-y']],
  4443. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'Y', 'id' => 'published-y']],
  4444. 'Published',
  4445. '/label',
  4446. ['label' => ['for' => 'published-n']],
  4447. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'N', 'id' => 'published-n', 'checked' => 'checked']],
  4448. 'Unpublished',
  4449. '/label',
  4450. '/div',
  4451. ];
  4452. $this->assertHtml($expected, $result);
  4453. }
  4454. /**
  4455. * testRadioNoLabel method
  4456. *
  4457. * Test that radio() works with label = false.
  4458. */
  4459. public function testRadioNoLabel(): void
  4460. {
  4461. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4462. $expected = [
  4463. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4464. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4465. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4466. ];
  4467. $this->assertHtml($expected, $result);
  4468. }
  4469. /**
  4470. * testRadioControlInsideLabel method
  4471. *
  4472. * Test generating radio input inside label ala twitter bootstrap.
  4473. */
  4474. public function testRadioControlInsideLabel(): void
  4475. {
  4476. $this->Form->setTemplates([
  4477. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4478. 'radioWrapper' => '{{label}}',
  4479. ]);
  4480. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4481. // phpcs:disable
  4482. $expected = [
  4483. ['input' => [
  4484. 'type' => 'hidden',
  4485. 'name' => 'Model[field]',
  4486. 'value' => '',
  4487. 'id' => 'model-field'
  4488. ]],
  4489. ['label' => ['for' => 'model-field-0']],
  4490. ['input' => [
  4491. 'type' => 'radio',
  4492. 'name' => 'Model[field]',
  4493. 'value' => '0',
  4494. 'id' => 'model-field-0'
  4495. ]],
  4496. 'option A',
  4497. '/label',
  4498. ['label' => ['for' => 'model-field-1']],
  4499. ['input' => [
  4500. 'type' => 'radio',
  4501. 'name' => 'Model[field]',
  4502. 'value' => '1',
  4503. 'id' => 'model-field-1'
  4504. ]],
  4505. 'option B',
  4506. '/label',
  4507. ];
  4508. // phpcs:enable
  4509. $this->assertHtml($expected, $result);
  4510. }
  4511. /**
  4512. * testRadioHiddenControlDisabling method
  4513. *
  4514. * Test disabling the hidden input for radio buttons.
  4515. */
  4516. public function testRadioHiddenControlDisabling(): void
  4517. {
  4518. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4519. $expected = [
  4520. 'label' => ['for' => 'model-1-field-0'],
  4521. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4522. 'option A',
  4523. '/label',
  4524. ];
  4525. $this->assertHtml($expected, $result);
  4526. }
  4527. /**
  4528. * testRadioOutOfRange method
  4529. *
  4530. * Test radio element set generation.
  4531. */
  4532. public function testRadioOutOfRange(): void
  4533. {
  4534. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4535. $expected = [
  4536. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4537. 'label' => ['for' => 'model-field-v'],
  4538. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4539. 'value',
  4540. '/label',
  4541. ];
  4542. $this->assertHtml($expected, $result);
  4543. }
  4544. /**
  4545. * testSelect method
  4546. *
  4547. * Test select element generation.
  4548. */
  4549. public function testSelect(): void
  4550. {
  4551. $result = $this->Form->select('Model.field', []);
  4552. $expected = [
  4553. 'select' => ['name' => 'Model[field]'],
  4554. '/select',
  4555. ];
  4556. $this->assertHtml($expected, $result);
  4557. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4558. $this->Form->create();
  4559. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4560. $expected = [
  4561. 'select' => ['name' => 'Model[field]'],
  4562. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4563. 'good',
  4564. '/option',
  4565. ['option' => ['value' => 'other']],
  4566. 'bad',
  4567. '/option',
  4568. '/select',
  4569. ];
  4570. $this->assertHtml($expected, $result);
  4571. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4572. $this->assertHtml($expected, $result);
  4573. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4574. $this->Form->create();
  4575. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4576. $expected = [
  4577. 'select' => ['name' => 'Model[field]'],
  4578. ['option' => ['value' => 'value']],
  4579. 'good',
  4580. '/option',
  4581. ['option' => ['value' => 'other']],
  4582. 'bad',
  4583. '/option',
  4584. '/select',
  4585. ];
  4586. $this->assertHtml($expected, $result);
  4587. $options = [
  4588. ['value' => 'first', 'text' => 'First'],
  4589. ['value' => 'first', 'text' => 'Another First'],
  4590. ];
  4591. $result = $this->Form->select(
  4592. 'Model.field',
  4593. $options,
  4594. ['escape' => false, 'empty' => false]
  4595. );
  4596. $expected = [
  4597. 'select' => ['name' => 'Model[field]'],
  4598. ['option' => ['value' => 'first']],
  4599. 'First',
  4600. '/option',
  4601. ['option' => ['value' => 'first']],
  4602. 'Another First',
  4603. '/option',
  4604. '/select',
  4605. ];
  4606. $this->assertHtml($expected, $result);
  4607. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4608. $this->Form->create();
  4609. $result = $this->Form->select(
  4610. 'Model.contact_id',
  4611. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4612. ['escape' => false, 'empty' => 'pick something']
  4613. );
  4614. $expected = [
  4615. 'select' => ['name' => 'Model[contact_id]'],
  4616. ['option' => ['value' => '']], 'pick something', '/option',
  4617. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4618. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4619. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4620. '/select',
  4621. ];
  4622. $this->assertHtml($expected, $result);
  4623. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4624. $this->Form->create();
  4625. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4626. $expected = [
  4627. 'select' => ['name' => 'Model[field]'],
  4628. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4629. ['option' => ['value' => '1']], 'Yes', '/option',
  4630. '/select',
  4631. ];
  4632. $this->assertHtml($expected, $result);
  4633. $article = new Article([
  4634. 'status' => ArticleStatus::Unpublished,
  4635. ]);
  4636. $this->Form->create($article);
  4637. $result = $this->Form->select('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4638. $expected = [
  4639. 'select' => ['name' => 'status'],
  4640. ['option' => ['value' => 'Y']], 'Published', '/option',
  4641. ['option' => ['value' => 'N', 'selected' => 'selected']], 'Unpublished', '/option',
  4642. '/select',
  4643. ];
  4644. $this->assertHtml($expected, $result);
  4645. }
  4646. /**
  4647. * testSelectEscapeHtml method
  4648. *
  4649. * Test that select() escapes HTML.
  4650. */
  4651. public function testSelectEscapeHtml(): void
  4652. {
  4653. $result = $this->Form->select(
  4654. 'Model.field',
  4655. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4656. ['empty' => false]
  4657. );
  4658. $expected = [
  4659. 'select' => ['name' => 'Model[field]'],
  4660. ['option' => ['value' => 'first']],
  4661. 'first &quot;html&quot; &lt;chars&gt;',
  4662. '/option',
  4663. ['option' => ['value' => 'second']],
  4664. 'value',
  4665. '/option',
  4666. '/select',
  4667. ];
  4668. $this->assertHtml($expected, $result);
  4669. $result = $this->Form->select(
  4670. 'Model.field',
  4671. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4672. ['escape' => false, 'empty' => false]
  4673. );
  4674. $expected = [
  4675. 'select' => ['name' => 'Model[field]'],
  4676. ['option' => ['value' => 'first']],
  4677. 'first "html" <chars>',
  4678. '/option',
  4679. ['option' => ['value' => 'second']],
  4680. 'value',
  4681. '/option',
  4682. '/select',
  4683. ];
  4684. $this->assertHtml($expected, $result);
  4685. }
  4686. /**
  4687. * testSelectRequired method
  4688. *
  4689. * Test select() with required and disabled attributes.
  4690. */
  4691. public function testSelectRequired(): void
  4692. {
  4693. $this->article['required'] = [
  4694. 'user_id' => true,
  4695. ];
  4696. $this->Form->create($this->article);
  4697. $result = $this->Form->select('user_id', ['option A']);
  4698. $expected = [
  4699. 'select' => [
  4700. 'name' => 'user_id',
  4701. 'required' => 'required',
  4702. ],
  4703. ['option' => ['value' => '0']], 'option A', '/option',
  4704. '/select',
  4705. ];
  4706. $this->assertHtml($expected, $result);
  4707. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4708. $expected = [
  4709. 'select' => [
  4710. 'name' => 'user_id',
  4711. 'disabled' => 'disabled',
  4712. ],
  4713. ['option' => ['value' => '0']], 'option A', '/option',
  4714. '/select',
  4715. ];
  4716. $this->assertHtml($expected, $result);
  4717. }
  4718. public function testSelectEmptyWithRequiredFalse(): void
  4719. {
  4720. $Articles = $this->getTableLocator()->get('Articles');
  4721. $validator = $Articles->getValidator('default');
  4722. $validator->allowEmptyString('user_id');
  4723. $Articles->setValidator('default', $validator);
  4724. $entity = $Articles->newEmptyEntity();
  4725. $this->Form->create($entity);
  4726. $result = $this->Form->select('user_id', ['option A']);
  4727. $expected = [
  4728. 'select' => [
  4729. 'name' => 'user_id',
  4730. ],
  4731. ['option' => ['value' => '']], '/option',
  4732. ['option' => ['value' => '0']], 'option A', '/option',
  4733. '/select',
  4734. ];
  4735. $this->assertHtml($expected, $result);
  4736. }
  4737. /**
  4738. * testNestedSelect method
  4739. *
  4740. * Test select element generation with optgroups.
  4741. */
  4742. public function testNestedSelect(): void
  4743. {
  4744. $result = $this->Form->select(
  4745. 'Model.field',
  4746. [1 => 'One', 2 => 'Two', 'Three' => [
  4747. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4748. ]],
  4749. ['empty' => false]
  4750. );
  4751. $expected = [
  4752. 'select' => ['name' => 'Model[field]'],
  4753. ['option' => ['value' => 1]],
  4754. 'One',
  4755. '/option',
  4756. ['option' => ['value' => 2]],
  4757. 'Two',
  4758. '/option',
  4759. ['optgroup' => ['label' => 'Three']],
  4760. ['option' => ['value' => 3]],
  4761. 'Three',
  4762. '/option',
  4763. ['option' => ['value' => 4]],
  4764. 'Four',
  4765. '/option',
  4766. ['option' => ['value' => 5]],
  4767. 'Five',
  4768. '/option',
  4769. '/optgroup',
  4770. '/select',
  4771. ];
  4772. $this->assertHtml($expected, $result);
  4773. }
  4774. /**
  4775. * testSelectMultiple method
  4776. *
  4777. * Test generation of multiple select elements.
  4778. */
  4779. public function testSelectMultiple(): void
  4780. {
  4781. $options = ['first', 'second', 'third'];
  4782. $result = $this->Form->select(
  4783. 'Model.multi_field',
  4784. $options,
  4785. ['form' => 'my-form', 'multiple' => true]
  4786. );
  4787. $expected = [
  4788. 'input' => [
  4789. 'type' => 'hidden',
  4790. 'name' => 'Model[multi_field]',
  4791. 'value' => '',
  4792. 'form' => 'my-form',
  4793. ],
  4794. 'select' => [
  4795. 'name' => 'Model[multi_field][]',
  4796. 'multiple' => 'multiple',
  4797. 'form' => 'my-form',
  4798. ],
  4799. ['option' => ['value' => '0']],
  4800. 'first',
  4801. '/option',
  4802. ['option' => ['value' => '1']],
  4803. 'second',
  4804. '/option',
  4805. ['option' => ['value' => '2']],
  4806. 'third',
  4807. '/option',
  4808. '/select',
  4809. ];
  4810. $this->assertHtml($expected, $result);
  4811. $result = $this->Form->select(
  4812. 'Model.multi_field',
  4813. $options,
  4814. ['multiple' => 'multiple', 'form' => 'my-form']
  4815. );
  4816. $this->assertHtml($expected, $result);
  4817. $result = $this->Form->select(
  4818. 'Model.multi_field',
  4819. $options,
  4820. ['form' => 'my-form', 'multiple' => false]
  4821. );
  4822. $this->assertStringNotContainsString('multiple', $result);
  4823. }
  4824. /**
  4825. * testCheckboxZeroValue method
  4826. *
  4827. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4828. */
  4829. public function testCheckboxZeroValue(): void
  4830. {
  4831. $result = $this->Form->control('User.get_spam', [
  4832. 'type' => 'checkbox',
  4833. 'value' => '0',
  4834. 'hiddenField' => '1',
  4835. ]);
  4836. $expected = [
  4837. 'div' => ['class' => 'input checkbox'],
  4838. 'label' => ['for' => 'user-get-spam'],
  4839. ['input' => [
  4840. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4841. 'value' => '1',
  4842. ]],
  4843. ['input' => [
  4844. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4845. 'value' => '0', 'id' => 'user-get-spam',
  4846. ]],
  4847. 'Get Spam',
  4848. '/label',
  4849. '/div',
  4850. ];
  4851. $this->assertHtml($expected, $result);
  4852. $result = $this->Form->control('User.get_spam', [
  4853. 'type' => 'checkbox',
  4854. 'value' => '0',
  4855. 'hiddenField' => '',
  4856. ]);
  4857. $expected = [
  4858. 'div' => ['class' => 'input checkbox'],
  4859. 'label' => ['for' => 'user-get-spam'],
  4860. ['input' => [
  4861. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4862. 'value' => '',
  4863. ]],
  4864. ['input' => [
  4865. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4866. 'value' => '0', 'id' => 'user-get-spam',
  4867. ]],
  4868. 'Get Spam',
  4869. '/label',
  4870. '/div',
  4871. ];
  4872. $this->assertHtml($expected, $result);
  4873. }
  4874. /**
  4875. * testHabtmSelectBox method
  4876. *
  4877. * Test generation of habtm select boxes.
  4878. */
  4879. public function testHabtmSelectBox(): void
  4880. {
  4881. $options = [
  4882. 1 => 'blue',
  4883. 2 => 'red',
  4884. 3 => 'green',
  4885. ];
  4886. $tags = [
  4887. new Entity(['id' => 1, 'name' => 'blue']),
  4888. new Entity(['id' => 3, 'name' => 'green']),
  4889. ];
  4890. $article = new Article(['tags' => $tags]);
  4891. $this->Form->create($article);
  4892. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4893. $expected = [
  4894. 'div' => ['class' => 'input select'],
  4895. 'label' => ['for' => 'tags-ids'],
  4896. 'Tags',
  4897. '/label',
  4898. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4899. 'select' => [
  4900. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4901. 'multiple' => 'multiple',
  4902. ],
  4903. ['option' => ['value' => '1', 'selected' => 'selected']],
  4904. 'blue',
  4905. '/option',
  4906. ['option' => ['value' => '2']],
  4907. 'red',
  4908. '/option',
  4909. ['option' => ['value' => '3', 'selected' => 'selected']],
  4910. 'green',
  4911. '/option',
  4912. '/select',
  4913. '/div',
  4914. ];
  4915. $this->assertHtml($expected, $result);
  4916. // make sure only 50 is selected, and not 50f5c0cf
  4917. $options = [
  4918. '1' => 'blue',
  4919. '50f5c0cf' => 'red',
  4920. '50' => 'green',
  4921. ];
  4922. $tags = [
  4923. new Entity(['id' => 1, 'name' => 'blue']),
  4924. new Entity(['id' => 50, 'name' => 'green']),
  4925. ];
  4926. $article = new Article(['tags' => $tags]);
  4927. $this->Form->create($article);
  4928. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4929. $expected = [
  4930. 'div' => ['class' => 'input select'],
  4931. 'label' => ['for' => 'tags-ids'],
  4932. 'Tags',
  4933. '/label',
  4934. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4935. 'select' => [
  4936. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4937. 'multiple' => 'multiple',
  4938. ],
  4939. ['option' => ['value' => '1', 'selected' => 'selected']],
  4940. 'blue',
  4941. '/option',
  4942. ['option' => ['value' => '50f5c0cf']],
  4943. 'red',
  4944. '/option',
  4945. ['option' => ['value' => '50', 'selected' => 'selected']],
  4946. 'green',
  4947. '/option',
  4948. '/select',
  4949. '/div',
  4950. ];
  4951. $this->assertHtml($expected, $result);
  4952. $spacecraft = [
  4953. 1 => 'Orion',
  4954. 2 => 'Helios',
  4955. ];
  4956. $this->View->set('spacecraft', $spacecraft);
  4957. $this->Form->create();
  4958. $result = $this->Form->control('spacecraft._ids');
  4959. $expected = [
  4960. 'div' => ['class' => 'input select'],
  4961. 'label' => ['for' => 'spacecraft-ids'],
  4962. 'Spacecraft',
  4963. '/label',
  4964. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4965. 'select' => [
  4966. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4967. 'multiple' => 'multiple',
  4968. ],
  4969. ['option' => ['value' => '1']],
  4970. 'Orion',
  4971. '/option',
  4972. ['option' => ['value' => '2']],
  4973. 'Helios',
  4974. '/option',
  4975. '/select',
  4976. '/div',
  4977. ];
  4978. $this->assertHtml($expected, $result);
  4979. }
  4980. /**
  4981. * testErrorsForBelongsToManySelect method
  4982. *
  4983. * Tests that errors for belongsToMany select fields are being
  4984. * picked up properly.
  4985. */
  4986. public function testErrorsForBelongsToManySelect(): void
  4987. {
  4988. $spacecraft = [
  4989. 1 => 'Orion',
  4990. 2 => 'Helios',
  4991. ];
  4992. $this->View->set('spacecraft', $spacecraft);
  4993. $article = new Article();
  4994. $article->setError('spacecraft', ['Invalid']);
  4995. $this->Form->create($article);
  4996. $result = $this->Form->control('spacecraft._ids');
  4997. $expected = [
  4998. ['div' => ['class' => 'input select error']],
  4999. 'label' => ['for' => 'spacecraft-ids'],
  5000. 'Spacecraft',
  5001. '/label',
  5002. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5003. 'select' => [
  5004. 'name' => 'spacecraft[_ids][]',
  5005. 'id' => 'spacecraft-ids',
  5006. 'multiple' => 'multiple',
  5007. ],
  5008. ['option' => ['value' => '1']],
  5009. 'Orion',
  5010. '/option',
  5011. ['option' => ['value' => '2']],
  5012. 'Helios',
  5013. '/option',
  5014. '/select',
  5015. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  5016. 'Invalid',
  5017. '/div',
  5018. '/div',
  5019. ];
  5020. $this->assertHtml($expected, $result);
  5021. }
  5022. /**
  5023. * testSelectMultipleCheckboxes method
  5024. *
  5025. * Test generation of multi select elements in checkbox format.
  5026. */
  5027. public function testSelectMultipleCheckboxes(): void
  5028. {
  5029. $result = $this->Form->select(
  5030. 'Model.multi_field',
  5031. ['first', 'second', 'third'],
  5032. ['multiple' => 'checkbox']
  5033. );
  5034. $expected = [
  5035. 'input' => [
  5036. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5037. ],
  5038. ['div' => ['class' => 'checkbox']],
  5039. ['label' => ['for' => 'model-multi-field-0']],
  5040. ['input' => [
  5041. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5042. 'value' => '0', 'id' => 'model-multi-field-0',
  5043. ]],
  5044. 'first',
  5045. '/label',
  5046. '/div',
  5047. ['div' => ['class' => 'checkbox']],
  5048. ['label' => ['for' => 'model-multi-field-1']],
  5049. ['input' => [
  5050. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5051. 'value' => '1', 'id' => 'model-multi-field-1',
  5052. ]],
  5053. 'second',
  5054. '/label',
  5055. '/div',
  5056. ['div' => ['class' => 'checkbox']],
  5057. ['label' => ['for' => 'model-multi-field-2']],
  5058. ['input' => [
  5059. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5060. 'value' => '2', 'id' => 'model-multi-field-2',
  5061. ]],
  5062. 'third',
  5063. '/label',
  5064. '/div',
  5065. ];
  5066. $this->assertHtml($expected, $result);
  5067. $result = $this->Form->select(
  5068. 'Model.multi_field',
  5069. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5070. ['multiple' => 'checkbox']
  5071. );
  5072. $expected = [
  5073. 'input' => [
  5074. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5075. ],
  5076. ['div' => ['class' => 'checkbox']],
  5077. ['label' => ['for' => 'model-multi-field-a+']],
  5078. ['input' => [
  5079. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5080. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5081. ]],
  5082. 'first',
  5083. '/label',
  5084. '/div',
  5085. ['div' => ['class' => 'checkbox']],
  5086. ['label' => ['for' => 'model-multi-field-a++']],
  5087. ['input' => [
  5088. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5089. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5090. ]],
  5091. 'second',
  5092. '/label',
  5093. '/div',
  5094. ['div' => ['class' => 'checkbox']],
  5095. ['label' => ['for' => 'model-multi-field-a+++']],
  5096. ['input' => [
  5097. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5098. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5099. ]],
  5100. 'third',
  5101. '/label',
  5102. '/div',
  5103. ];
  5104. $this->assertHtml($expected, $result);
  5105. $result = $this->Form->select(
  5106. 'Model.multi_field',
  5107. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5108. ['multiple' => 'checkbox']
  5109. );
  5110. $expected = [
  5111. 'input' => [
  5112. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5113. ],
  5114. ['div' => ['class' => 'checkbox']],
  5115. ['label' => ['for' => 'model-multi-field-a-b']],
  5116. ['input' => [
  5117. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5118. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5119. ]],
  5120. 'first',
  5121. '/label',
  5122. '/div',
  5123. ['div' => ['class' => 'checkbox']],
  5124. ['label' => ['for' => 'model-multi-field-a-b1']],
  5125. ['input' => [
  5126. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5127. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5128. ]],
  5129. 'second',
  5130. '/label',
  5131. '/div',
  5132. ['div' => ['class' => 'checkbox']],
  5133. ['label' => ['for' => 'model-multi-field-a-b2']],
  5134. ['input' => [
  5135. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5136. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5137. ]],
  5138. 'third',
  5139. '/label',
  5140. '/div',
  5141. ];
  5142. $this->assertHtml($expected, $result);
  5143. }
  5144. /**
  5145. * testSelectMultipleCheckboxRequestData method
  5146. *
  5147. * Ensure that multiCheckbox reads from the request data.
  5148. */
  5149. public function testSelectMultipleCheckboxRequestData(): void
  5150. {
  5151. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5152. $result = $this->Form->select(
  5153. 'Model.tags',
  5154. ['1' => 'first', 'Array' => 'Array'],
  5155. ['multiple' => 'checkbox']
  5156. );
  5157. $expected = [
  5158. 'input' => [
  5159. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  5160. ],
  5161. ['div' => ['class' => 'checkbox']],
  5162. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5163. ['input' => [
  5164. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5165. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5166. ]],
  5167. 'first',
  5168. '/label',
  5169. '/div',
  5170. ['div' => ['class' => 'checkbox']],
  5171. ['label' => ['for' => 'model-tags-array']],
  5172. ['input' => [
  5173. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5174. 'value' => 'Array', 'id' => 'model-tags-array',
  5175. ]],
  5176. 'Array',
  5177. '/label',
  5178. '/div',
  5179. ];
  5180. $this->assertHtml($expected, $result);
  5181. }
  5182. /**
  5183. * testSelectMultipleCheckboxSecurity method
  5184. *
  5185. * Checks the security hash array generated for multiple-input checkbox elements.
  5186. */
  5187. public function testSelectMultipleCheckboxSecurity(): void
  5188. {
  5189. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5190. $this->Form->create();
  5191. $this->Form->select(
  5192. 'Model.multi_field',
  5193. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5194. ['multiple' => 'checkbox']
  5195. );
  5196. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5197. $this->assertEquals(['Model.multi_field'], $fields);
  5198. $result = $this->Form->secure();
  5199. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5200. $hash = urlencode($hash . ':');
  5201. $this->assertStringContainsString('"' . $hash . '"', $result);
  5202. }
  5203. /**
  5204. * testSelectMultipleSecureWithNoOptions method
  5205. *
  5206. * Multiple select elements should always be secured as they always participate
  5207. * in the POST data.
  5208. */
  5209. public function testSelectMultipleSecureWithNoOptions(): void
  5210. {
  5211. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5212. $this->Form->create();
  5213. $this->Form->select(
  5214. 'Model.select',
  5215. [],
  5216. ['multiple' => true]
  5217. );
  5218. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5219. $this->assertEquals(['Model.select'], $result);
  5220. }
  5221. /**
  5222. * testSelectNoSecureWithNoOptions method
  5223. *
  5224. * When a select box has no options it should not be added to the fields list
  5225. * as it always fail post validation.
  5226. */
  5227. public function testSelectNoSecureWithNoOptions(): void
  5228. {
  5229. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5230. $this->Form->create();
  5231. $this->Form->select(
  5232. 'Model.select',
  5233. []
  5234. );
  5235. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5236. $this->assertEquals([], $result);
  5237. $this->Form->select(
  5238. 'Model.user_id',
  5239. [],
  5240. ['empty' => true]
  5241. );
  5242. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5243. $this->assertEquals(['Model.user_id'], $result);
  5244. }
  5245. /**
  5246. * testControlMultipleCheckboxes method
  5247. *
  5248. * Test control() resulting in multi select elements being generated.
  5249. */
  5250. public function testControlMultipleCheckboxes(): void
  5251. {
  5252. $result = $this->Form->control('Model.multi_field', [
  5253. 'options' => ['first', 'second', 'third'],
  5254. 'multiple' => 'checkbox',
  5255. ]);
  5256. $expected = [
  5257. ['div' => ['class' => 'input select']],
  5258. ['label' => ['for' => 'model-multi-field']],
  5259. 'Multi Field',
  5260. '/label',
  5261. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5262. ['div' => ['class' => 'checkbox']],
  5263. ['label' => ['for' => 'model-multi-field-0']],
  5264. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5265. 'first',
  5266. '/label',
  5267. '/div',
  5268. ['div' => ['class' => 'checkbox']],
  5269. ['label' => ['for' => 'model-multi-field-1']],
  5270. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5271. 'second',
  5272. '/label',
  5273. '/div',
  5274. ['div' => ['class' => 'checkbox']],
  5275. ['label' => ['for' => 'model-multi-field-2']],
  5276. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5277. 'third',
  5278. '/label',
  5279. '/div',
  5280. '/div',
  5281. ];
  5282. $this->assertHtml($expected, $result);
  5283. $result = $this->Form->control('Model.multi_field', [
  5284. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5285. 'multiple' => 'checkbox',
  5286. ]);
  5287. $expected = [
  5288. ['div' => ['class' => 'input select']],
  5289. ['label' => ['for' => 'model-multi-field']],
  5290. 'Multi Field',
  5291. '/label',
  5292. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5293. ['div' => ['class' => 'checkbox']],
  5294. ['label' => ['for' => 'model-multi-field-a']],
  5295. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5296. 'first',
  5297. '/label',
  5298. '/div',
  5299. ['div' => ['class' => 'checkbox']],
  5300. ['label' => ['for' => 'model-multi-field-b']],
  5301. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5302. 'second',
  5303. '/label',
  5304. '/div',
  5305. ['div' => ['class' => 'checkbox']],
  5306. ['label' => ['for' => 'model-multi-field-c']],
  5307. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5308. 'third',
  5309. '/label',
  5310. '/div',
  5311. '/div',
  5312. ];
  5313. $this->assertHtml($expected, $result);
  5314. }
  5315. /**
  5316. * testSelectHiddenFieldOmission method
  5317. *
  5318. * Test that select() with 'hiddenField' => false omits the hidden field.
  5319. */
  5320. public function testSelectHiddenFieldOmission(): void
  5321. {
  5322. $result = $this->Form->select(
  5323. 'Model.multi_field',
  5324. ['first', 'second'],
  5325. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5326. );
  5327. $this->assertStringNotContainsString('type="hidden"', $result);
  5328. }
  5329. /**
  5330. * testSelectCheckboxMultipleOverrideName method
  5331. *
  5332. * Test that select() with multiple = checkbox works with overriding name attribute.
  5333. */
  5334. public function testSelectCheckboxMultipleOverrideName(): void
  5335. {
  5336. $result = $this->Form->select('category', ['1', '2'], [
  5337. 'multiple' => 'checkbox',
  5338. 'name' => 'fish',
  5339. ]);
  5340. $expected = [
  5341. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5342. ['div' => ['class' => 'checkbox']],
  5343. ['label' => ['for' => 'fish-0']],
  5344. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5345. '1',
  5346. '/label',
  5347. '/div',
  5348. ['div' => ['class' => 'checkbox']],
  5349. ['label' => ['for' => 'fish-1']],
  5350. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5351. '2',
  5352. '/label',
  5353. '/div',
  5354. ];
  5355. $this->assertHtml($expected, $result);
  5356. $result = $this->Form->multiCheckbox(
  5357. 'category',
  5358. new Collection(['1', '2']),
  5359. ['name' => 'fish']
  5360. );
  5361. $this->assertHtml($expected, $result);
  5362. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5363. 'name' => 'fish',
  5364. ]);
  5365. $this->assertHtml($expected, $result);
  5366. }
  5367. /**
  5368. * testSelectCheckboxMultipleOverrideName method
  5369. *
  5370. * Test that select() with multiple = checkbox works with overriding name attribute.
  5371. */
  5372. public function testSelectCheckboxMultipleCustomId(): void
  5373. {
  5374. $result = $this->Form->select('category', ['1', '2'], [
  5375. 'multiple' => 'checkbox',
  5376. 'id' => 'cat',
  5377. ]);
  5378. $expected = [
  5379. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5380. ['div' => ['class' => 'checkbox']],
  5381. ['label' => ['for' => 'cat-0']],
  5382. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5383. '1',
  5384. '/label',
  5385. '/div',
  5386. ['div' => ['class' => 'checkbox']],
  5387. ['label' => ['for' => 'cat-1']],
  5388. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5389. '2',
  5390. '/label',
  5391. '/div',
  5392. ];
  5393. $this->assertHtml($expected, $result);
  5394. $result = $this->Form->multiCheckbox(
  5395. 'category',
  5396. ['1', '2'],
  5397. ['id' => 'cat']
  5398. );
  5399. $this->assertHtml($expected, $result);
  5400. }
  5401. /**
  5402. * testControlMultiCheckbox method
  5403. *
  5404. * Test that control() works with multicheckbox.
  5405. */
  5406. public function testControlMultiCheckbox(): void
  5407. {
  5408. $result = $this->Form->control('category', [
  5409. 'type' => 'multicheckbox',
  5410. 'options' => ['1', '2'],
  5411. ]);
  5412. $expected = [
  5413. ['div' => ['class' => 'input multicheckbox']],
  5414. '<label',
  5415. 'Category',
  5416. '/label',
  5417. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5418. ['div' => ['class' => 'checkbox']],
  5419. ['label' => ['for' => 'category-0']],
  5420. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5421. '1',
  5422. '/label',
  5423. '/div',
  5424. ['div' => ['class' => 'checkbox']],
  5425. ['label' => ['for' => 'category-1']],
  5426. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5427. '2',
  5428. '/label',
  5429. '/div',
  5430. '/div',
  5431. ];
  5432. $this->assertHtml($expected, $result);
  5433. }
  5434. /**
  5435. * testCheckbox method
  5436. *
  5437. * Test generation of checkboxes.
  5438. */
  5439. public function testCheckbox(): void
  5440. {
  5441. $result = $this->Form->checkbox('Model.field');
  5442. $expected = [
  5443. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5444. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5445. ];
  5446. $this->assertHtml($expected, $result);
  5447. $result = $this->Form->checkbox('Model.field', [
  5448. 'id' => 'theID',
  5449. 'value' => 'myvalue',
  5450. 'form' => 'my-form',
  5451. ]);
  5452. $expected = [
  5453. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5454. ['input' => [
  5455. 'type' => 'checkbox', 'name' => 'Model[field]',
  5456. 'value' => 'myvalue', 'id' => 'theID',
  5457. 'form' => 'my-form',
  5458. ]],
  5459. ];
  5460. $this->assertHtml($expected, $result);
  5461. }
  5462. /**
  5463. * testCheckboxDefaultValue method
  5464. *
  5465. * Test default value setting on checkbox() method.
  5466. */
  5467. public function testCheckboxDefaultValue(): void
  5468. {
  5469. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5470. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5471. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5472. $this->assertHtml($expected, $result);
  5473. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5474. $this->Form->create();
  5475. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5476. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5477. $this->assertHtml($expected, $result);
  5478. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5479. $this->Form->create();
  5480. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5481. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5482. $this->assertHtml($expected, $result);
  5483. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5484. $this->Form->create();
  5485. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5486. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5487. $this->assertHtml($expected, $result);
  5488. $Articles = $this->getTableLocator()->get('Articles');
  5489. $Articles->getSchema()->addColumn(
  5490. 'published',
  5491. ['type' => 'boolean', 'null' => false, 'default' => true]
  5492. );
  5493. $this->Form->create($Articles->newEmptyEntity());
  5494. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5495. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5496. $this->assertHtml($expected, $result);
  5497. }
  5498. /**
  5499. * testCheckboxCheckedAndError method
  5500. *
  5501. * Test checkbox being checked or having errors.
  5502. */
  5503. public function testCheckboxCheckedAndError(): void
  5504. {
  5505. $this->article['errors'] = [
  5506. 'published' => true,
  5507. ];
  5508. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5509. $this->Form->create($this->article);
  5510. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5511. $expected = [
  5512. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5513. ['input' => [
  5514. 'type' => 'checkbox',
  5515. 'name' => 'published',
  5516. 'value' => 'myvalue',
  5517. 'id' => 'theID',
  5518. 'checked' => 'checked',
  5519. 'class' => 'form-error',
  5520. ]],
  5521. ];
  5522. $this->assertHtml($expected, $result);
  5523. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5524. $this->Form->create($this->article);
  5525. $result = $this->Form->checkbox('published');
  5526. $expected = [
  5527. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5528. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5529. ];
  5530. $this->assertHtml($expected, $result);
  5531. }
  5532. /**
  5533. * testCheckboxCustomNameAttribute method
  5534. *
  5535. * Test checkbox() with a custom name attribute.
  5536. */
  5537. public function testCheckboxCustomNameAttribute(): void
  5538. {
  5539. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5540. $expected = [
  5541. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5542. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5543. ];
  5544. $this->assertHtml($expected, $result);
  5545. }
  5546. /**
  5547. * testCheckboxHiddenField method
  5548. *
  5549. * Test that the hidden input for checkboxes can be omitted or set to a
  5550. * specific value.
  5551. */
  5552. public function testCheckboxHiddenField(): void
  5553. {
  5554. $result = $this->Form->checkbox('UserForm.something', [
  5555. 'hiddenField' => false,
  5556. ]);
  5557. $expected = [
  5558. 'input' => [
  5559. 'type' => 'checkbox',
  5560. 'name' => 'UserForm[something]',
  5561. 'value' => '1',
  5562. ],
  5563. ];
  5564. $this->assertHtml($expected, $result);
  5565. $result = $this->Form->checkbox('UserForm.something', [
  5566. 'value' => 'Y',
  5567. 'hiddenField' => '',
  5568. ]);
  5569. $expected = [
  5570. ['input' => [
  5571. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5572. 'value' => '',
  5573. ]],
  5574. ['input' => [
  5575. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5576. 'value' => 'Y',
  5577. ]],
  5578. ];
  5579. $this->assertHtml($expected, $result);
  5580. $result = $this->Form->checkbox('UserForm.something', [
  5581. 'value' => 'Y',
  5582. 'hiddenField' => 'N',
  5583. ]);
  5584. $expected = [
  5585. ['input' => [
  5586. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5587. 'value' => 'N',
  5588. ]],
  5589. ['input' => [
  5590. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5591. 'value' => 'Y',
  5592. ]],
  5593. ];
  5594. $this->assertHtml($expected, $result);
  5595. }
  5596. /**
  5597. * testTime method
  5598. *
  5599. * Test the time type.
  5600. */
  5601. public function testTime(): void
  5602. {
  5603. $result = $this->Form->time('start_time', [
  5604. 'value' => '2014-03-08 16:30:00',
  5605. ]);
  5606. $expected = [
  5607. 'input' => [
  5608. 'type' => 'time',
  5609. 'name' => 'start_time',
  5610. 'value' => '16:30:00',
  5611. 'step' => '1',
  5612. ],
  5613. ];
  5614. $this->assertHtml($expected, $result);
  5615. $result = $this->Form->time('start_time', [
  5616. 'value' => new ChronosTime('16:30:00'),
  5617. ]);
  5618. $expected = [
  5619. 'input' => [
  5620. 'type' => 'time',
  5621. 'name' => 'start_time',
  5622. 'value' => '16:30:00',
  5623. 'step' => '1',
  5624. ],
  5625. ];
  5626. $this->assertHtml($expected, $result);
  5627. }
  5628. /**
  5629. * testDate method
  5630. *
  5631. * Test the date type.
  5632. */
  5633. public function testDate(): void
  5634. {
  5635. $result = $this->Form->date('start_day', [
  5636. 'value' => '2014-03-08',
  5637. ]);
  5638. $expected = [
  5639. 'input' => [
  5640. 'type' => 'date',
  5641. 'name' => 'start_day',
  5642. 'value' => '2014-03-08',
  5643. ],
  5644. ];
  5645. $this->assertHtml($expected, $result);
  5646. $result = $this->Form->date('start_day', [
  5647. 'value' => new Date('2014-03-08'),
  5648. ]);
  5649. $this->assertHtml($expected, $result);
  5650. }
  5651. /**
  5652. * testDateTime method
  5653. */
  5654. public function testDateTime(): void
  5655. {
  5656. $result = $this->Form->dateTime('date', ['default' => true]);
  5657. $expected = [
  5658. 'input' => [
  5659. 'type' => 'datetime-local',
  5660. 'name' => 'date',
  5661. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5662. 'step' => '1',
  5663. ],
  5664. ];
  5665. $this->assertHtml($expected, $result);
  5666. }
  5667. /**
  5668. * testDateTimeSecured method
  5669. *
  5670. * Test that datetime fields are added to protected fields list.
  5671. */
  5672. public function testDateTimeSecured(): void
  5673. {
  5674. $this->View->setRequest(
  5675. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5676. );
  5677. $this->Form->create();
  5678. $this->Form->dateTime('date');
  5679. $expected = ['date'];
  5680. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5681. $this->assertEquals($expected, $result);
  5682. $this->Form->date('published');
  5683. $expected = ['date', 'published'];
  5684. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5685. $this->assertEquals($expected, $result);
  5686. }
  5687. /**
  5688. * testDateTimeSecuredDisabled method
  5689. *
  5690. * Test that datetime fields are added to protected fields list.
  5691. */
  5692. public function testDateTimeSecuredDisabled(): void
  5693. {
  5694. $this->View->setRequest(
  5695. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5696. );
  5697. $this->Form->create();
  5698. $this->Form->dateTime('date', ['secure' => false]);
  5699. $expected = [];
  5700. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5701. $this->assertEquals($expected, $result);
  5702. $this->Form->date('published', ['secure' => false]);
  5703. $expected = [];
  5704. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5705. $this->assertEquals($expected, $result);
  5706. }
  5707. /**
  5708. * testDatetimeWithDefault method
  5709. *
  5710. * Test that datetime() and default values work.
  5711. */
  5712. public function testDatetimeWithDefault(): void
  5713. {
  5714. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5715. $expected = [
  5716. 'input' => [
  5717. 'type' => 'datetime-local',
  5718. 'name' => 'updated',
  5719. 'value' => '2009-06-01T11:15:30',
  5720. 'step' => '1',
  5721. ],
  5722. ];
  5723. $this->assertHtml($expected, $result);
  5724. $result = $this->Form->dateTime('updated', [
  5725. 'default' => '2009-06-01 11:15:30',
  5726. ]);
  5727. $expected = [
  5728. 'input' => [
  5729. 'type' => 'datetime-local',
  5730. 'name' => 'updated',
  5731. 'value' => '2009-06-01T11:15:30',
  5732. 'step' => '1',
  5733. ],
  5734. ];
  5735. $this->assertHtml($expected, $result);
  5736. }
  5737. /**
  5738. * testMonth method
  5739. *
  5740. * Test generation of a month input.
  5741. */
  5742. public function testMonth(): void
  5743. {
  5744. $result = $this->Form->month('field', ['value' => '']);
  5745. $expected = [
  5746. 'input' => [
  5747. 'type' => 'month',
  5748. 'name' => 'field',
  5749. 'value' => '',
  5750. ],
  5751. ];
  5752. $this->assertHtml($expected, $result);
  5753. $this->View->setRequest(
  5754. $this->View->getRequest()->withData('release', '2050-02-10')
  5755. );
  5756. $this->Form->create();
  5757. $result = $this->Form->month('release');
  5758. $expected = [
  5759. 'input' => [
  5760. 'type' => 'month',
  5761. 'name' => 'release',
  5762. 'value' => '2050-02',
  5763. ],
  5764. ];
  5765. $this->assertHtml($expected, $result);
  5766. $this->View->setRequest(
  5767. $this->View->getRequest()->withData('release', '2050-03')
  5768. );
  5769. $this->Form->create();
  5770. $result = $this->Form->month('release');
  5771. $expected = [
  5772. 'input' => [
  5773. 'type' => 'month',
  5774. 'name' => 'release',
  5775. 'value' => '2050-03',
  5776. ],
  5777. ];
  5778. $this->assertHtml($expected, $result);
  5779. }
  5780. /**
  5781. * testYear method
  5782. *
  5783. * Test generation of a year input.
  5784. */
  5785. public function testYear(): void
  5786. {
  5787. $this->View->setRequest(
  5788. $this->View->getRequest()->withData('published', '2006')
  5789. );
  5790. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5791. $expected = [
  5792. ['select' => ['name' => 'field']],
  5793. ['option' => ['selected' => 'selected', 'value' => '']],
  5794. '/option',
  5795. ['option' => ['value' => '2007']],
  5796. '2007',
  5797. '/option',
  5798. ['option' => ['value' => '2006']],
  5799. '2006',
  5800. '/option',
  5801. '/select',
  5802. ];
  5803. $this->assertHtml($expected, $result);
  5804. $result = $this->Form->year('field', [
  5805. 'value' => '',
  5806. 'min' => 2006,
  5807. 'max' => 2007,
  5808. 'order' => 'asc',
  5809. ]);
  5810. $expected = [
  5811. ['select' => ['name' => 'field']],
  5812. ['option' => ['selected' => 'selected', 'value' => '']],
  5813. '/option',
  5814. ['option' => ['value' => '2006']],
  5815. '2006',
  5816. '/option',
  5817. ['option' => ['value' => '2007']],
  5818. '2007',
  5819. '/option',
  5820. '/select',
  5821. ];
  5822. $this->assertHtml($expected, $result);
  5823. $result = $this->Form->year('published', [
  5824. 'empty' => false,
  5825. 'min' => 2006,
  5826. 'max' => 2007,
  5827. ]);
  5828. $expected = [
  5829. ['select' => ['name' => 'published']],
  5830. ['option' => ['value' => '2007']],
  5831. '2007',
  5832. '/option',
  5833. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5834. '2006',
  5835. '/option',
  5836. '/select',
  5837. ];
  5838. $this->assertHtml($expected, $result);
  5839. $result = $this->Form->year('published', [
  5840. 'empty' => false,
  5841. 'value' => new Date('2008-01-12'),
  5842. 'min' => 2007,
  5843. 'max' => 2009,
  5844. ]);
  5845. $expected = [
  5846. ['select' => ['name' => 'published']],
  5847. ['option' => ['value' => '2009']],
  5848. '2009',
  5849. '/option',
  5850. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5851. '2008',
  5852. '/option',
  5853. ['option' => ['value' => '2007']],
  5854. '2007',
  5855. '/option',
  5856. '/select',
  5857. ];
  5858. $this->assertHtml($expected, $result);
  5859. $result = $this->Form->year('published', [
  5860. 'empty' => 'Published on',
  5861. ]);
  5862. $this->assertStringContainsString('Published on', $result);
  5863. }
  5864. /**
  5865. * testControlYearPreEpoch method
  5866. *
  5867. * Test minYear being prior to the unix epoch.
  5868. */
  5869. public function testControlYearPreEpoch(): void
  5870. {
  5871. $start = date('Y') - 80;
  5872. $end = date('Y') - 18;
  5873. $result = $this->Form->control('birth_year', [
  5874. 'type' => 'year',
  5875. 'label' => 'Birth Year',
  5876. 'min' => $start,
  5877. 'max' => $end,
  5878. ]);
  5879. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5880. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5881. $this->assertStringNotContainsString('value="00">00', $result);
  5882. }
  5883. /**
  5884. * test control() datetime & required attributes
  5885. */
  5886. public function testControlDatetimeRequired(): void
  5887. {
  5888. $result = $this->Form->control('birthday', [
  5889. 'type' => 'date',
  5890. 'required' => true,
  5891. ]);
  5892. $this->assertStringContainsString(
  5893. '<input type="date" name="birthday" required="required"',
  5894. $result
  5895. );
  5896. }
  5897. /**
  5898. * testYearAutoExpandRange method
  5899. */
  5900. public function testYearAutoExpandRange(): void
  5901. {
  5902. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5903. $result = $this->Form->year('birthday');
  5904. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5905. $result = $matches[1];
  5906. $expected = range(date('Y') + 5, 1930);
  5907. $this->assertEquals($expected, $result);
  5908. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5909. $this->Form->create();
  5910. $result = $this->Form->year('release');
  5911. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5912. $result = $matches[1];
  5913. $expected = range(2050, date('Y') - 5);
  5914. $this->assertEquals($expected, $result);
  5915. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5916. $this->Form->create();
  5917. $result = $this->Form->year('release', [
  5918. 'min' => 1890,
  5919. 'max' => 1900,
  5920. ]);
  5921. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5922. $result = $matches[1];
  5923. $expected = range(1900, 1881);
  5924. $this->assertEquals($expected, $result);
  5925. }
  5926. /**
  5927. * test control placeholder + label
  5928. */
  5929. public function testControlLabelAndPlaceholder(): void
  5930. {
  5931. $this->Form->create($this->article);
  5932. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5933. $expected = [
  5934. 'div' => ['class' => 'input text required'],
  5935. 'label' => ['for' => 'title'],
  5936. 'Title',
  5937. '/label',
  5938. 'input' => [
  5939. 'type' => 'text',
  5940. 'required' => 'required',
  5941. 'placeholder' => 'Add title',
  5942. 'id' => 'title',
  5943. 'name' => 'title',
  5944. 'data-validity-message' => 'This field cannot be left empty',
  5945. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5946. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5947. ],
  5948. '/div',
  5949. ];
  5950. $this->assertHtml($expected, $result);
  5951. }
  5952. /**
  5953. * testControlLabelFalse method
  5954. *
  5955. * Test the label option being set to false.
  5956. */
  5957. public function testControlLabelFalse(): void
  5958. {
  5959. $this->Form->create($this->article);
  5960. $result = $this->Form->control('title', ['label' => false]);
  5961. $expected = [
  5962. 'div' => ['class' => 'input text required'],
  5963. 'input' => [
  5964. 'type' => 'text',
  5965. 'required' => 'required',
  5966. 'id' => 'title',
  5967. 'name' => 'title',
  5968. 'data-validity-message' => 'This field cannot be left empty',
  5969. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5970. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5971. ],
  5972. '/div',
  5973. ];
  5974. $this->assertHtml($expected, $result);
  5975. $this->Form->create($this->article);
  5976. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5977. $expected['input'] += [
  5978. 'placeholder' => 'Add title',
  5979. 'aria-label' => 'Add title',
  5980. ];
  5981. $this->assertHtml($expected, $result);
  5982. }
  5983. /**
  5984. * testTextArea method
  5985. *
  5986. * Test generation of a textarea input.
  5987. */
  5988. public function testTextArea(): void
  5989. {
  5990. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5991. $result = $this->Form->textarea('field');
  5992. $expected = [
  5993. 'textarea' => ['name' => 'field', 'rows' => 5],
  5994. 'some test data',
  5995. '/textarea',
  5996. ];
  5997. $this->assertHtml($expected, $result);
  5998. $result = $this->Form->textarea('user.bio');
  5999. $expected = [
  6000. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6001. '/textarea',
  6002. ];
  6003. $this->assertHtml($expected, $result);
  6004. $this->View->setRequest($this->View->getRequest()
  6005. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6006. $this->Form->create();
  6007. $result = $this->Form->textarea('field');
  6008. $expected = [
  6009. 'textarea' => ['name' => 'field', 'rows' => 5],
  6010. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6011. '/textarea',
  6012. ];
  6013. $this->assertHtml($expected, $result);
  6014. $this->View->setRequest($this->View->getRequest()->withData(
  6015. 'Model.field',
  6016. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6017. ));
  6018. $this->Form->create();
  6019. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6020. $expected = [
  6021. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6022. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6023. '/textarea',
  6024. ];
  6025. $this->assertHtml($expected, $result);
  6026. $result = $this->Form->textarea('0.OtherModel.field');
  6027. $expected = [
  6028. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6029. '/textarea',
  6030. ];
  6031. $this->assertHtml($expected, $result);
  6032. }
  6033. /**
  6034. * testTextAreaWithStupidCharacters method
  6035. *
  6036. * Test text area with non-ascii characters.
  6037. */
  6038. public function testTextAreaWithStupidCharacters(): void
  6039. {
  6040. $result = $this->Form->textarea('Post.content', [
  6041. 'value' => 'GREAT®',
  6042. 'rows' => '15',
  6043. 'cols' => '75',
  6044. ]);
  6045. $expected = [
  6046. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6047. 'GREAT®',
  6048. '/textarea',
  6049. ];
  6050. $this->assertHtml($expected, $result);
  6051. }
  6052. /**
  6053. * testTextAreaMaxLength method
  6054. *
  6055. * Test textareas maxlength read from schema.
  6056. */
  6057. public function testTextAreaMaxLength(): void
  6058. {
  6059. $this->Form->create([
  6060. 'schema' => [
  6061. 'stuff' => ['type' => 'string', 'length' => 10],
  6062. ],
  6063. ]);
  6064. $result = $this->Form->control('other', ['type' => 'textarea']);
  6065. $expected = [
  6066. 'div' => ['class' => 'input textarea'],
  6067. 'label' => ['for' => 'other'],
  6068. 'Other',
  6069. '/label',
  6070. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6071. '/textarea',
  6072. '/div',
  6073. ];
  6074. $this->assertHtml($expected, $result);
  6075. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6076. $expected = [
  6077. 'div' => ['class' => 'input textarea'],
  6078. 'label' => ['for' => 'stuff'],
  6079. 'Stuff',
  6080. '/label',
  6081. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6082. '/textarea',
  6083. '/div',
  6084. ];
  6085. $this->assertHtml($expected, $result);
  6086. }
  6087. /**
  6088. * testHiddenField method
  6089. *
  6090. * Test generation of a hidden input.
  6091. */
  6092. public function testHidden(): void
  6093. {
  6094. $this->article['errors'] = [
  6095. 'field' => true,
  6096. ];
  6097. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6098. $this->Form->create($this->article);
  6099. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6100. $expected = [
  6101. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6102. $this->assertHtml($expected, $result);
  6103. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6104. $expected = [
  6105. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6106. ];
  6107. $this->assertHtml($expected, $result);
  6108. $article = new Article([
  6109. 'status' => ArticleStatus::Unpublished,
  6110. ]);
  6111. $this->Form->create($article);
  6112. $result = $this->Form->hidden('status');
  6113. $expected = [
  6114. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => 'N'],
  6115. ];
  6116. $this->assertHtml($expected, $result);
  6117. }
  6118. /**
  6119. * Test hidden() with various boolean values.
  6120. */
  6121. public function testHiddenBooleanValues(): void
  6122. {
  6123. $this->Form->create($this->article);
  6124. $result = $this->Form->hidden('field', ['value' => null]);
  6125. $expected = [
  6126. 'input' => ['type' => 'hidden', 'name' => 'field'],
  6127. ];
  6128. $this->assertHtml($expected, $result);
  6129. $result = $this->Form->hidden('field', ['value' => true]);
  6130. $expected = [
  6131. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  6132. ];
  6133. $this->assertHtml($expected, $result);
  6134. $result = $this->Form->hidden('field', ['value' => false]);
  6135. $expected = [
  6136. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  6137. ];
  6138. $this->assertHtml($expected, $result);
  6139. }
  6140. /**
  6141. * testFileUploadField method
  6142. *
  6143. * Test generation of a file upload input.
  6144. */
  6145. public function testFileUploadField(): void
  6146. {
  6147. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6148. $result = $this->Form->file('Model.upload');
  6149. $this->assertHtml($expected, $result);
  6150. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6151. 'name' => '', 'type' => '', 'tmp_name' => '',
  6152. 'error' => 4, 'size' => 0,
  6153. ]));
  6154. $result = $this->Form->file('Model.upload');
  6155. $this->assertHtml($expected, $result);
  6156. $this->View->setRequest(
  6157. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6158. );
  6159. $result = $this->Form->file('Model.upload');
  6160. $this->assertHtml($expected, $result);
  6161. }
  6162. /**
  6163. * testFileUploadOnOtherModel method
  6164. *
  6165. * Test File upload input on a model not used in create().
  6166. */
  6167. public function testFileUploadOnOtherModel(): void
  6168. {
  6169. $this->Form->create($this->article, ['type' => 'file']);
  6170. $result = $this->Form->file('ValidateProfile.city');
  6171. $expected = [
  6172. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6173. ];
  6174. $this->assertHtml($expected, $result);
  6175. }
  6176. /**
  6177. * testButton method
  6178. *
  6179. * Test generation of a form button.
  6180. */
  6181. public function testButton(): void
  6182. {
  6183. $result = $this->Form->button('Hi');
  6184. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6185. $this->assertHtml($expected, $result);
  6186. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6187. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6188. $this->assertHtml($expected, $result);
  6189. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6190. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6191. $this->assertHtml($expected, $result);
  6192. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6193. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6194. $this->assertHtml($expected, $result);
  6195. $result = $this->Form->button('No type', ['type' => false]);
  6196. $expected = ['button' => [], 'No type', '/button'];
  6197. $this->assertHtml($expected, $result);
  6198. $result = $this->Form->button('Upload Text', [
  6199. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6200. 'escape' => false,
  6201. ]);
  6202. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6203. }
  6204. /**
  6205. * testButtonUnlockedByDefault method
  6206. *
  6207. * Test that button() makes unlocked fields by default.
  6208. */
  6209. public function testButtonUnlockedByDefault(): void
  6210. {
  6211. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6212. $this->Form->create();
  6213. $this->Form->button('Save', ['name' => 'save']);
  6214. $this->Form->button('Clear');
  6215. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6216. $this->assertEquals(['save'], $result);
  6217. }
  6218. /**
  6219. * Test generation of a form button with confirm message.
  6220. */
  6221. public function testButtonWithConfirm(): void
  6222. {
  6223. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6224. $expected = ['button' => [
  6225. 'type' => 'submit',
  6226. 'data-confirm-message' => 'Confirm me!',
  6227. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6228. ], 'Hi', '/button'];
  6229. $this->assertHtml($expected, $result);
  6230. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6231. $expected = ['button' => [
  6232. 'type' => 'submit',
  6233. 'data-confirm-message' => 'Confirm "me"!',
  6234. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6235. ], 'Hi', '/button'];
  6236. $this->assertHtml($expected, $result);
  6237. }
  6238. /**
  6239. * testPostButton method
  6240. */
  6241. public function testPostButton(): void
  6242. {
  6243. $result = $this->Form->postButton('Hi', '/controller/action');
  6244. $expected = [
  6245. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6246. 'button' => ['type' => 'submit'],
  6247. 'Hi',
  6248. '/button',
  6249. '/form',
  6250. ];
  6251. $this->assertHtml($expected, $result);
  6252. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6253. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6254. }
  6255. /**
  6256. * testPostButtonMethodType method
  6257. */
  6258. public function testPostButtonMethodType(): void
  6259. {
  6260. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6261. $expected = [
  6262. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6263. 'div' => ['style' => 'display:none;'],
  6264. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6265. '/div',
  6266. 'button' => ['type' => 'submit'],
  6267. 'Hi',
  6268. '/button',
  6269. '/form',
  6270. ];
  6271. $this->assertHtml($expected, $result);
  6272. }
  6273. /**
  6274. * testPostButtonFormOptions method
  6275. */
  6276. public function testPostButtonFormOptions(): void
  6277. {
  6278. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6279. $expected = [
  6280. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6281. 'button' => ['type' => 'submit'],
  6282. 'Hi',
  6283. '/button',
  6284. '/form',
  6285. ];
  6286. $this->assertHtml($expected, $result);
  6287. }
  6288. /**
  6289. * testPostButtonNestedData method
  6290. *
  6291. * Test using postButton with N dimensional data.
  6292. */
  6293. public function testPostButtonNestedData(): void
  6294. {
  6295. $data = [
  6296. 'one' => [
  6297. 'two' => [
  6298. 3, 4, 5,
  6299. ],
  6300. ],
  6301. ];
  6302. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6303. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6304. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6305. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6306. }
  6307. /**
  6308. * testSecurePostButton method
  6309. *
  6310. * Test that postButton adds _Token fields.
  6311. */
  6312. public function testSecurePostButton(): void
  6313. {
  6314. $this->View->setRequest($this->View->getRequest()
  6315. ->withAttribute('csrfToken', 'testkey')
  6316. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6317. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6318. $tokenDebug = urlencode(json_encode([
  6319. '/posts/delete/1',
  6320. [],
  6321. [],
  6322. ]));
  6323. $expected = [
  6324. 'form' => [
  6325. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6326. ],
  6327. ['div' => ['style' => 'display:none;']],
  6328. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6329. '/div',
  6330. 'button' => ['type' => 'submit'],
  6331. 'Delete',
  6332. '/button',
  6333. ['div' => ['style' => 'display:none;']],
  6334. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6335. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6336. ['input' => [
  6337. 'type' => 'hidden',
  6338. 'name' => '_Token[debug]',
  6339. 'value' => $tokenDebug,
  6340. ]],
  6341. '/div',
  6342. '/form',
  6343. ];
  6344. $this->assertHtml($expected, $result);
  6345. }
  6346. /**
  6347. * testPostLink method
  6348. */
  6349. public function testPostLink(): void
  6350. {
  6351. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6352. $expected = [
  6353. 'form' => [
  6354. 'method' => 'post', 'action' => '/posts/delete/1',
  6355. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6356. ],
  6357. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6358. '/form',
  6359. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6360. 'Delete',
  6361. '/a',
  6362. ];
  6363. $this->assertHtml($expected, $result);
  6364. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6365. $expected = [
  6366. 'form' => [
  6367. 'method' => 'post', 'action' => '/posts/delete/1',
  6368. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6369. ],
  6370. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6371. '/form',
  6372. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6373. 'Delete',
  6374. '/a',
  6375. ];
  6376. $this->assertHtml($expected, $result);
  6377. $result = $this->Form->postLink(
  6378. 'Delete',
  6379. '/posts/delete/1',
  6380. ['target' => '_blank', 'class' => 'btn btn-danger']
  6381. );
  6382. $expected = [
  6383. 'form' => [
  6384. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6385. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6386. ],
  6387. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6388. '/form',
  6389. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6390. 'Delete',
  6391. '/a',
  6392. ];
  6393. $this->assertHtml($expected, $result);
  6394. }
  6395. /**
  6396. * testPostLinkWithConfirm method
  6397. *
  6398. * Test the confirm option for postLink().
  6399. */
  6400. public function testPostLinkWithConfirm(): void
  6401. {
  6402. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6403. $expected = [
  6404. 'form' => [
  6405. 'method' => 'post', 'action' => '/posts/delete/1',
  6406. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6407. ],
  6408. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6409. '/form',
  6410. 'a' => [
  6411. 'href' => '#',
  6412. 'data-confirm-message' => 'Confirm?',
  6413. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6414. ],
  6415. 'Delete',
  6416. '/a',
  6417. ];
  6418. $this->assertHtml($expected, $result);
  6419. $result = $this->Form->postLink(
  6420. 'Delete',
  6421. '/posts/delete/1',
  6422. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6423. );
  6424. $expected = [
  6425. 'form' => [
  6426. 'method' => 'post', 'action' => '/posts/delete/1',
  6427. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6428. ],
  6429. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6430. '/form',
  6431. 'a' => [
  6432. 'href' => '#',
  6433. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6434. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6435. ],
  6436. 'Delete',
  6437. '/a',
  6438. ];
  6439. $this->assertHtml($expected, $result);
  6440. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6441. $result = $this->Form->postLink(
  6442. 'Delete',
  6443. '/posts/delete/1',
  6444. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6445. );
  6446. $expected = [
  6447. 'form' => [
  6448. 'method' => 'post', 'action' => '/posts/delete/1',
  6449. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6450. ],
  6451. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6452. '/form',
  6453. 'a' => [
  6454. 'href' => '#',
  6455. 'data-confirm-message' => 'Confirm this deletion?',
  6456. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6457. ],
  6458. 'Delete',
  6459. '/a',
  6460. ];
  6461. $this->assertHtml($expected, $result);
  6462. }
  6463. /**
  6464. * testPostLinkWithQuery method
  6465. *
  6466. * Test postLink() with query string args.
  6467. */
  6468. public function testPostLinkWithQuery(): void
  6469. {
  6470. $result = $this->Form->postLink(
  6471. 'Delete',
  6472. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6473. );
  6474. $expected = [
  6475. 'form' => [
  6476. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6477. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6478. ],
  6479. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6480. '/form',
  6481. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6482. 'Delete',
  6483. '/a',
  6484. ];
  6485. $this->assertHtml($expected, $result);
  6486. }
  6487. /**
  6488. * testPostLinkWithData method
  6489. *
  6490. * Test postLink with additional data.
  6491. */
  6492. public function testPostLinkWithData(): void
  6493. {
  6494. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6495. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6496. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6497. $this->Form->create($entity);
  6498. $this->Form->end();
  6499. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6500. $this->assertStringContainsString(
  6501. '<input type="hidden" name="name" value="show"',
  6502. $result,
  6503. 'should not contain entity data.'
  6504. );
  6505. }
  6506. /**
  6507. * testPostLinkSecurityHash method
  6508. *
  6509. * Test that security hashes for postLink include the url.
  6510. */
  6511. public function testPostLinkSecurityHash(): void
  6512. {
  6513. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6514. $hash .= '%3Aid';
  6515. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6516. $result = $this->Form->postLink(
  6517. 'Delete',
  6518. '/posts/delete/1',
  6519. ['data' => ['id' => 1]]
  6520. );
  6521. $tokenDebug = urlencode(json_encode([
  6522. '/posts/delete/1',
  6523. [
  6524. 'id' => 1,
  6525. ],
  6526. [],
  6527. ]));
  6528. $expected = [
  6529. 'form' => [
  6530. 'method' => 'post', 'action' => '/posts/delete/1',
  6531. 'name', 'style' => 'display:none;',
  6532. ],
  6533. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6534. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6535. 'div' => ['style' => 'display:none;'],
  6536. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6537. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6538. ['input' => [
  6539. 'type' => 'hidden',
  6540. 'name' => '_Token[debug]',
  6541. 'value' => $tokenDebug,
  6542. ]],
  6543. '/div',
  6544. '/form',
  6545. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6546. 'Delete',
  6547. '/a',
  6548. ];
  6549. $this->assertHtml($expected, $result);
  6550. }
  6551. /**
  6552. * testPostLinkSecurityHashBlockMode method
  6553. *
  6554. * Test that postLink doesn't modify the fields in the containing form.
  6555. *
  6556. * postLink() calls inside open forms should not modify the field list
  6557. * for the form.
  6558. */
  6559. public function testPostLinkSecurityHashBlockMode(): void
  6560. {
  6561. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6562. $hash .= '%3A';
  6563. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6564. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6565. $this->Form->control('title');
  6566. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6567. $result = $this->View->fetch('postLink');
  6568. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6569. $this->assertEquals(['title'], $fields);
  6570. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6571. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6572. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6573. }
  6574. /**
  6575. * testPostLinkSecurityHashNoDebugMode method
  6576. *
  6577. * Test that security does not include debug token if debug is false.
  6578. */
  6579. public function testPostLinkSecurityHashNoDebugMode(): void
  6580. {
  6581. Configure::write('debug', false);
  6582. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6583. $hash .= '%3Aid';
  6584. $this->View->setRequest($this->View->getRequest()
  6585. ->withAttribute('formTokenData', ['key' => 'test']));
  6586. $result = $this->Form->postLink(
  6587. 'Delete',
  6588. '/posts/delete/1',
  6589. ['data' => ['id' => 1]]
  6590. );
  6591. $expected = [
  6592. 'form' => [
  6593. 'method' => 'post', 'action' => '/posts/delete/1',
  6594. 'name', 'style' => 'display:none;',
  6595. ],
  6596. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6597. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6598. 'div' => ['style' => 'display:none;'],
  6599. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6600. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6601. '/div',
  6602. '/form',
  6603. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6604. 'Delete',
  6605. '/a',
  6606. ];
  6607. $this->assertHtml($expected, $result);
  6608. }
  6609. /**
  6610. * testPostLinkNestedData method
  6611. *
  6612. * Test using postLink with N dimensional data.
  6613. */
  6614. public function testPostLinkNestedData(): void
  6615. {
  6616. $data = [
  6617. 'one' => [
  6618. 'two' => [
  6619. 3, 4, 5,
  6620. ],
  6621. ],
  6622. ];
  6623. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6624. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6625. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6626. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6627. }
  6628. /**
  6629. * testPostLinkAfterGetForm method
  6630. *
  6631. * Test creating postLinks after a GET form.
  6632. */
  6633. public function testPostLinkAfterGetForm(): void
  6634. {
  6635. $this->View->setRequest($this->View->getRequest()
  6636. ->withAttribute('csrfToken', 'testkey')
  6637. ->withAttribute('formTokenData', []));
  6638. $this->Form->create($this->article, ['type' => 'get']);
  6639. $this->Form->end();
  6640. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6641. $tokenDebug = urlencode(json_encode([
  6642. '/posts/delete/1',
  6643. [],
  6644. [],
  6645. ]));
  6646. $expected = [
  6647. 'form' => [
  6648. 'method' => 'post', 'action' => '/posts/delete/1',
  6649. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6650. ],
  6651. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6652. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6653. 'div' => ['style' => 'display:none;'],
  6654. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6655. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6656. ['input' => [
  6657. 'type' => 'hidden', 'name' => '_Token[debug]',
  6658. 'value' => $tokenDebug,
  6659. ]],
  6660. '/div',
  6661. '/form',
  6662. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6663. 'Delete',
  6664. '/a',
  6665. ];
  6666. $this->assertHtml($expected, $result);
  6667. }
  6668. /**
  6669. * testPostLinkFormBuffer method
  6670. *
  6671. * Test that postLink adds form tags to view block.
  6672. */
  6673. public function testPostLinkFormBuffer(): void
  6674. {
  6675. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6676. $expected = [
  6677. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6678. 'Delete',
  6679. '/a',
  6680. ];
  6681. $this->assertHtml($expected, $result);
  6682. $result = $this->View->fetch('postLink');
  6683. $expected = [
  6684. 'form' => [
  6685. 'method' => 'post', 'action' => '/posts/delete/1',
  6686. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6687. ],
  6688. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6689. '/form',
  6690. ];
  6691. $this->assertHtml($expected, $result);
  6692. $result = $this->Form->postLink(
  6693. 'Delete',
  6694. '/posts/delete/2',
  6695. ['block' => true, 'method' => 'DELETE']
  6696. );
  6697. $expected = [
  6698. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6699. 'Delete',
  6700. '/a',
  6701. ];
  6702. $this->assertHtml($expected, $result);
  6703. $result = $this->View->fetch('postLink');
  6704. $expected = [
  6705. 'form' => [
  6706. 'method' => 'post', 'action' => '/posts/delete/1',
  6707. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6708. ],
  6709. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6710. '/form',
  6711. [
  6712. 'form' => [
  6713. 'method' => 'post', 'action' => '/posts/delete/2',
  6714. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6715. ],
  6716. ],
  6717. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6718. '/form',
  6719. ];
  6720. $this->assertHtml($expected, $result);
  6721. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6722. $expected = [
  6723. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6724. 'Delete',
  6725. '/a',
  6726. ];
  6727. $this->assertHtml($expected, $result);
  6728. $result = $this->View->fetch('foobar');
  6729. $expected = [
  6730. 'form' => [
  6731. 'method' => 'post', 'action' => '/posts/delete/1',
  6732. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6733. ],
  6734. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6735. '/form',
  6736. ];
  6737. $this->assertHtml($expected, $result);
  6738. }
  6739. /**
  6740. * testSubmitButton method
  6741. */
  6742. public function testSubmitButton(): void
  6743. {
  6744. $result = $this->Form->submit('');
  6745. $expected = [
  6746. 'div' => ['class' => 'submit'],
  6747. 'input' => ['type' => 'submit', 'value' => ''],
  6748. '/div',
  6749. ];
  6750. $this->assertHtml($expected, $result);
  6751. $result = $this->Form->submit('Test Submit');
  6752. $expected = [
  6753. 'div' => ['class' => 'submit'],
  6754. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6755. '/div',
  6756. ];
  6757. $this->assertHtml($expected, $result);
  6758. $result = $this->Form->submit('Next >');
  6759. $expected = [
  6760. 'div' => ['class' => 'submit'],
  6761. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6762. '/div',
  6763. ];
  6764. $this->assertHtml($expected, $result);
  6765. $result = $this->Form->submit('Next >', ['escape' => false]);
  6766. $expected = [
  6767. 'div' => ['class' => 'submit'],
  6768. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6769. '/div',
  6770. ];
  6771. $this->assertHtml($expected, $result);
  6772. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6773. $expected = [
  6774. 'div' => ['class' => 'submit'],
  6775. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6776. '/div',
  6777. ];
  6778. $this->assertHtml($expected, $result);
  6779. }
  6780. /**
  6781. * testSubmitImage method
  6782. *
  6783. * Test image submit types.
  6784. */
  6785. public function testSubmitImage(): void
  6786. {
  6787. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6788. $expected = [
  6789. 'div' => ['class' => 'submit'],
  6790. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6791. '/div',
  6792. ];
  6793. $this->assertHtml($expected, $result);
  6794. $result = $this->Form->submit('/relative/cake.power.gif');
  6795. $expected = [
  6796. 'div' => ['class' => 'submit'],
  6797. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6798. '/div',
  6799. ];
  6800. $this->assertHtml($expected, $result);
  6801. $result = $this->Form->submit('cake.power.gif');
  6802. $expected = [
  6803. 'div' => ['class' => 'submit'],
  6804. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6805. '/div',
  6806. ];
  6807. $this->assertHtml($expected, $result);
  6808. $result = $this->Form->submit('Not.an.image');
  6809. $expected = [
  6810. 'div' => ['class' => 'submit'],
  6811. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6812. '/div',
  6813. ];
  6814. $this->assertHtml($expected, $result);
  6815. }
  6816. /**
  6817. * testSubmitUnlockedByDefault method
  6818. *
  6819. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6820. * be submitted at a time.
  6821. */
  6822. public function testSubmitUnlockedByDefault(): void
  6823. {
  6824. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6825. $this->Form->create();
  6826. $this->Form->submit('Go go');
  6827. $this->Form->submit('Save', ['name' => 'save']);
  6828. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6829. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6830. }
  6831. /**
  6832. * testSubmitImageTimestamp method
  6833. *
  6834. * Test submit image with timestamps.
  6835. */
  6836. public function testSubmitImageTimestamp(): void
  6837. {
  6838. Configure::write('Asset.timestamp', 'force');
  6839. $result = $this->Form->submit('cake.power.gif');
  6840. $expected = [
  6841. 'div' => ['class' => 'submit'],
  6842. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6843. '/div',
  6844. ];
  6845. $this->assertHtml($expected, $result);
  6846. }
  6847. /**
  6848. * testDateTimeWithGetForms method
  6849. *
  6850. * Test that datetime() works with GET style forms.
  6851. */
  6852. public function testDateTimeWithGetForms(): void
  6853. {
  6854. $this->Form->create($this->article, ['type' => 'get']);
  6855. $result = $this->Form->datetime('created');
  6856. $expected = [
  6857. 'input' => [
  6858. 'type' => 'datetime-local',
  6859. 'name' => 'created',
  6860. 'value' => '',
  6861. 'step' => '1',
  6862. ],
  6863. ];
  6864. $this->assertHtml($expected, $result);
  6865. $result = $this->Form->datetime('created', ['default' => true]);
  6866. $expected = [
  6867. 'input' => [
  6868. 'type' => 'datetime-local',
  6869. 'name' => 'created',
  6870. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6871. 'step' => '1',
  6872. ],
  6873. ];
  6874. $this->assertHtml($expected, $result);
  6875. }
  6876. /**
  6877. * Provides fractional schema types
  6878. *
  6879. * @return array
  6880. */
  6881. public static function fractionalTypeProvider(): array
  6882. {
  6883. return [
  6884. ['datetimefractional'],
  6885. ['timestampfractional'],
  6886. ['timestamptimezone'],
  6887. ];
  6888. }
  6889. /**
  6890. * testDateTimeWithFractional method
  6891. *
  6892. * Test that datetime() works with datetimefractional.
  6893. */
  6894. #[DataProvider('fractionalTypeProvider')]
  6895. public function testDateTimeWithFractional(string $type): void
  6896. {
  6897. $this->Form->create([
  6898. 'schema' => [
  6899. 'created' => ['type' => $type],
  6900. ],
  6901. ]);
  6902. $result = $this->Form->datetime('created', [
  6903. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6904. ]);
  6905. $expected = [
  6906. 'input' => [
  6907. 'type' => 'datetime-local',
  6908. 'name' => 'created',
  6909. 'value' => '2019-09-27T02:52:43.123',
  6910. 'step' => '0.001',
  6911. ],
  6912. ];
  6913. $this->assertHtml($expected, $result);
  6914. }
  6915. /**
  6916. * testControlWithFractional method
  6917. *
  6918. * Test that control() works with datetimefractional.
  6919. */
  6920. #[DataProvider('fractionalTypeProvider')]
  6921. public function testControlWithFractional(string $type): void
  6922. {
  6923. $this->Form->create([
  6924. 'schema' => [
  6925. 'created' => ['type' => $type],
  6926. ],
  6927. ]);
  6928. $result = $this->Form->control('created', [
  6929. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6930. ]);
  6931. $expected = [
  6932. 'div' => ['class' => 'input datetime'],
  6933. 'label' => ['for' => 'created'],
  6934. 'Created',
  6935. '/label',
  6936. 'input' => [
  6937. 'type' => 'datetime-local',
  6938. 'name' => 'created',
  6939. 'id' => 'created',
  6940. 'value' => '2019-09-27T02:52:43.123',
  6941. 'step' => '0.001',
  6942. ],
  6943. '/div',
  6944. ];
  6945. $this->assertHtml($expected, $result);
  6946. }
  6947. /**
  6948. * testForMagicControlNonExistentNotValidated method
  6949. */
  6950. public function testForMagicControlNonExistentNotValidated(): void
  6951. {
  6952. $this->Form->create($this->article);
  6953. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6954. $result = $this->Form->control('nonexistent_not_validated');
  6955. $expected = [
  6956. 'label' => ['for' => 'nonexistent-not-validated'],
  6957. 'Nonexistent Not Validated',
  6958. '/label',
  6959. 'input' => [
  6960. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6961. 'id' => 'nonexistent-not-validated',
  6962. ],
  6963. ];
  6964. $this->assertHtml($expected, $result);
  6965. $result = $this->Form->control('nonexistent_not_validated', [
  6966. 'val' => 'my value',
  6967. ]);
  6968. $expected = [
  6969. 'label' => ['for' => 'nonexistent-not-validated'],
  6970. 'Nonexistent Not Validated',
  6971. '/label',
  6972. 'input' => [
  6973. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6974. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6975. ],
  6976. ];
  6977. $this->assertHtml($expected, $result);
  6978. $this->View->setRequest(
  6979. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6980. );
  6981. $this->Form->create($this->article);
  6982. $result = $this->Form->control('nonexistent_not_validated');
  6983. $expected = [
  6984. 'label' => ['for' => 'nonexistent-not-validated'],
  6985. 'Nonexistent Not Validated',
  6986. '/label',
  6987. 'input' => [
  6988. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6989. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6990. ],
  6991. ];
  6992. $this->assertHtml($expected, $result);
  6993. }
  6994. /**
  6995. * testFormMagicControlLabel method
  6996. */
  6997. public function testFormMagicControlLabel(): void
  6998. {
  6999. $this->getTableLocator()->get('Contacts', [
  7000. 'className' => ContactsTable::class,
  7001. ]);
  7002. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7003. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7004. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7005. $expected = [
  7006. 'label' => ['for' => 'contacts-name'],
  7007. 'My label',
  7008. '/label',
  7009. 'input' => [
  7010. 'type' => 'text',
  7011. 'name' => 'Contacts[name]',
  7012. 'id' => 'contacts-name',
  7013. 'maxlength' => '255',
  7014. ],
  7015. ];
  7016. $this->assertHtml($expected, $result);
  7017. $result = $this->Form->control('name', [
  7018. 'label' => ['class' => 'mandatory'],
  7019. ]);
  7020. $expected = [
  7021. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7022. 'Name',
  7023. '/label',
  7024. 'input' => [
  7025. 'type' => 'text', 'name' => 'name',
  7026. 'id' => 'name', 'maxlength' => '255',
  7027. ],
  7028. ];
  7029. $this->assertHtml($expected, $result);
  7030. $result = $this->Form->control('name', [
  7031. 'div' => false,
  7032. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7033. ]);
  7034. $expected = [
  7035. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7036. 'My label',
  7037. '/label',
  7038. 'input' => [
  7039. 'type' => 'text', 'name' => 'name',
  7040. 'id' => 'name', 'maxlength' => '255',
  7041. ],
  7042. ];
  7043. $this->assertHtml($expected, $result);
  7044. $result = $this->Form->control('Contact.name', [
  7045. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7046. ]);
  7047. $expected = [
  7048. 'label' => ['for' => 'my_id'],
  7049. 'Name',
  7050. '/label',
  7051. 'input' => [
  7052. 'type' => 'text', 'name' => 'Contact[name]',
  7053. 'id' => 'my_id', 'maxlength' => '255',
  7054. ],
  7055. ];
  7056. $this->assertHtml($expected, $result);
  7057. $result = $this->Form->control('1.id');
  7058. $expected = ['input' => [
  7059. 'type' => 'hidden', 'name' => '1[id]',
  7060. 'id' => '1-id',
  7061. ]];
  7062. $this->assertHtml($expected, $result);
  7063. $result = $this->Form->control('1.name');
  7064. $expected = [
  7065. 'label' => ['for' => '1-name'],
  7066. 'Name',
  7067. '/label',
  7068. 'input' => [
  7069. 'type' => 'text', 'name' => '1[name]',
  7070. 'id' => '1-name', 'maxlength' => '255',
  7071. ],
  7072. ];
  7073. $this->assertHtml($expected, $result);
  7074. }
  7075. /**
  7076. * testFormEnd method
  7077. */
  7078. public function testFormEnd(): void
  7079. {
  7080. $this->assertSame('</form>', $this->Form->end());
  7081. }
  7082. /**
  7083. * testMultiRecordForm method
  7084. *
  7085. * Test the generation of fields for a multi record form.
  7086. */
  7087. public function testMultiRecordForm(): void
  7088. {
  7089. $articles = $this->getTableLocator()->get('Articles');
  7090. $articles->hasMany('Comments');
  7091. $comment = new Entity(['comment' => 'Value']);
  7092. $article = new Article(['comments' => [$comment]]);
  7093. $this->Form->create([$article]);
  7094. $result = $this->Form->control('0.comments.1.comment');
  7095. // phpcs:disable
  7096. $expected = [
  7097. 'div' => ['class' => 'input textarea'],
  7098. 'label' => ['for' => '0-comments-1-comment'],
  7099. 'Comment',
  7100. '/label',
  7101. 'textarea' => [
  7102. 'name',
  7103. 'id' => '0-comments-1-comment',
  7104. 'rows' => 5
  7105. ],
  7106. '/textarea',
  7107. '/div'
  7108. ];
  7109. // phpcs:enable
  7110. $this->assertHtml($expected, $result);
  7111. $result = $this->Form->control('0.comments.0.comment');
  7112. // phpcs:disable
  7113. $expected = [
  7114. 'div' => ['class' => 'input textarea'],
  7115. 'label' => ['for' => '0-comments-0-comment'],
  7116. 'Comment',
  7117. '/label',
  7118. 'textarea' => [
  7119. 'name',
  7120. 'id' => '0-comments-0-comment',
  7121. 'rows' => 5
  7122. ],
  7123. 'Value',
  7124. '/textarea',
  7125. '/div'
  7126. ];
  7127. // phpcs:enable
  7128. $this->assertHtml($expected, $result);
  7129. $comment->setError('comment', ['Not valid']);
  7130. $result = $this->Form->control('0.comments.0.comment');
  7131. // phpcs:disable
  7132. $expected = [
  7133. 'div' => ['class' => 'input textarea error'],
  7134. 'label' => ['for' => '0-comments-0-comment'],
  7135. 'Comment',
  7136. '/label',
  7137. 'textarea' => [
  7138. 'name',
  7139. 'class' => 'form-error',
  7140. 'id' => '0-comments-0-comment',
  7141. 'aria-invalid' => 'true',
  7142. 'aria-describedby' => '0-comments-0-comment-error',
  7143. 'rows' => 5
  7144. ],
  7145. 'Value',
  7146. '/textarea',
  7147. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  7148. 'Not valid',
  7149. '/div',
  7150. '/div'
  7151. ];
  7152. // phpcs:enable
  7153. $this->assertHtml($expected, $result);
  7154. $this->getTableLocator()->get('Comments')
  7155. ->getValidator('default')
  7156. ->allowEmptyString('comment', null, false);
  7157. $result = $this->Form->control('0.comments.1.comment');
  7158. // phpcs:disable
  7159. $expected = [
  7160. 'div' => ['class' => 'input textarea required'],
  7161. 'label' => ['for' => '0-comments-1-comment'],
  7162. 'Comment',
  7163. '/label',
  7164. 'textarea' => [
  7165. 'name',
  7166. 'required' => 'required',
  7167. 'id' => '0-comments-1-comment',
  7168. 'rows' => 5,
  7169. 'data-validity-message' => 'This field cannot be left empty',
  7170. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7171. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7172. ],
  7173. '/textarea',
  7174. '/div'
  7175. ];
  7176. // phpcs:enable
  7177. $this->assertHtml($expected, $result);
  7178. }
  7179. /**
  7180. * testHtml5Controls method
  7181. *
  7182. * Test that some html5 inputs + FormHelper::__call() work.
  7183. */
  7184. public function testHtml5Controls(): void
  7185. {
  7186. $result = $this->Form->email('User.email');
  7187. $expected = [
  7188. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7189. ];
  7190. $this->assertHtml($expected, $result);
  7191. $result = $this->Form->search('User.query');
  7192. $expected = [
  7193. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7194. ];
  7195. $this->assertHtml($expected, $result);
  7196. $result = $this->Form->search('User.query', ['value' => 'test']);
  7197. $expected = [
  7198. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7199. ];
  7200. $this->assertHtml($expected, $result);
  7201. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7202. $expected = [
  7203. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7204. ];
  7205. $this->assertHtml($expected, $result);
  7206. }
  7207. /**
  7208. * testHtml5ControlWithControl method
  7209. *
  7210. * Test accessing html5 inputs through control().
  7211. */
  7212. public function testHtml5ControlWithControl(): void
  7213. {
  7214. $this->Form->create();
  7215. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7216. $result = $this->Form->control('website', [
  7217. 'type' => 'url',
  7218. 'val' => 'http://domain.tld',
  7219. 'label' => false,
  7220. ]);
  7221. $expected = [
  7222. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7223. ];
  7224. $this->assertHtml($expected, $result);
  7225. }
  7226. /**
  7227. * testHtml5ControlException method
  7228. *
  7229. * Test errors when field name is missing.
  7230. */
  7231. public function testHtml5ControlException(): void
  7232. {
  7233. $this->expectException(CakeException::class);
  7234. $this->Form->email();
  7235. }
  7236. /**
  7237. * tests fields that are required use custom validation messages
  7238. */
  7239. public function testHtml5ErrorMessage(): void
  7240. {
  7241. $this->Form->setConfig('autoSetCustomValidity', true);
  7242. $validator = (new Validator())
  7243. ->notEmptyString('email', 'Custom error message')
  7244. ->requirePresence('password')
  7245. ->alphaNumeric('password')
  7246. ->requirePresence('accept_tos')
  7247. ->notBlank('phone');
  7248. $table = $this->getTableLocator()->get('Contacts', [
  7249. 'className' => ContactsTable::class,
  7250. ]);
  7251. $table->setValidator('default', $validator);
  7252. $contact = new Entity();
  7253. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7254. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7255. $result = $this->Form->control('password');
  7256. $expected = [
  7257. 'label' => ['for' => 'password'],
  7258. 'Password',
  7259. '/label',
  7260. 'input' => [
  7261. 'id' => 'password',
  7262. 'name' => 'password',
  7263. 'type' => 'password',
  7264. 'value' => '',
  7265. 'required' => 'required',
  7266. 'data-validity-message' => 'This field cannot be left empty',
  7267. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7268. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7269. ],
  7270. ];
  7271. $this->assertHtml($expected, $result);
  7272. $result = $this->Form->control('phone');
  7273. $expected = [
  7274. 'label' => ['for' => 'phone'],
  7275. 'Phone',
  7276. '/label',
  7277. 'input' => [
  7278. 'id' => 'phone',
  7279. 'name' => 'phone',
  7280. 'type' => 'tel',
  7281. 'value' => '',
  7282. 'maxlength' => 255,
  7283. 'required' => 'required',
  7284. 'data-validity-message' => 'This field cannot be left empty',
  7285. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7286. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7287. ],
  7288. ];
  7289. $this->assertHtml($expected, $result);
  7290. $result = $this->Form->control('email');
  7291. $expected = [
  7292. 'label' => ['for' => 'email'],
  7293. 'Email',
  7294. '/label',
  7295. 'input' => [
  7296. 'id' => 'email',
  7297. 'name' => 'email',
  7298. 'type' => 'email',
  7299. 'value' => '',
  7300. 'maxlength' => 255,
  7301. 'required' => 'required',
  7302. 'data-validity-message' => 'Custom error message',
  7303. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7304. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7305. ],
  7306. ];
  7307. $this->assertHtml($expected, $result);
  7308. $result = $this->Form->control('accept_tos', ['type' => 'checkbox']);
  7309. $expected = [
  7310. ['input' => ['type' => 'hidden', 'name' => 'accept_tos', 'value' => '0']],
  7311. 'label' => ['for' => 'accept-tos'],
  7312. [
  7313. 'input' => [
  7314. 'required' => 'required',
  7315. 'type' => 'checkbox',
  7316. 'name' => 'accept_tos',
  7317. 'id' => 'accept-tos',
  7318. 'value' => '1',
  7319. 'data-validity-message' => 'This field cannot be left empty',
  7320. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7321. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.checked) this.setCustomValidity(this.dataset.validityMessage)',
  7322. ],
  7323. ],
  7324. 'Accept Tos',
  7325. '/label',
  7326. ];
  7327. $this->assertHtml($expected, $result);
  7328. }
  7329. /**
  7330. * tests that custom validation messages are in templateVars
  7331. */
  7332. public function testHtml5ErrorMessageInTemplateVars(): void
  7333. {
  7334. $validator = (new Validator())
  7335. ->notEmptyString('email', 'Custom error "message" & entities')
  7336. ->requirePresence('password')
  7337. ->alphaNumeric('password')
  7338. ->notBlank('phone');
  7339. $table = $this->getTableLocator()->get('Contacts', [
  7340. 'className' => ContactsTable::class,
  7341. ]);
  7342. $table->setValidator('default', $validator);
  7343. $contact = new Entity();
  7344. $this->Form->setConfig('autoSetCustomValidity', false);
  7345. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7346. $this->Form->setTemplates([
  7347. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7348. 'inputContainer' => '{{content}}',
  7349. ]);
  7350. $result = $this->Form->control('password');
  7351. $expected = [
  7352. 'label' => ['for' => 'password'],
  7353. 'Password',
  7354. '/label',
  7355. 'input' => [
  7356. 'id' => 'password',
  7357. 'name' => 'password',
  7358. 'type' => 'password',
  7359. 'value' => '',
  7360. 'required' => 'required',
  7361. 'data-message' => 'This field cannot be left empty',
  7362. ],
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $result = $this->Form->control('phone');
  7366. $expected = [
  7367. 'label' => ['for' => 'phone'],
  7368. 'Phone',
  7369. '/label',
  7370. 'input' => [
  7371. 'id' => 'phone',
  7372. 'name' => 'phone',
  7373. 'type' => 'tel',
  7374. 'value' => '',
  7375. 'maxlength' => 255,
  7376. 'required' => 'required',
  7377. 'data-message' => 'This field cannot be left empty',
  7378. ],
  7379. ];
  7380. $this->assertHtml($expected, $result);
  7381. $result = $this->Form->control('email', [
  7382. 'templateVars' => [
  7383. 'custom' => 'data-custom="1"',
  7384. ],
  7385. ]);
  7386. $expected = [
  7387. 'label' => ['for' => 'email'],
  7388. 'Email',
  7389. '/label',
  7390. 'input' => [
  7391. 'id' => 'email',
  7392. 'name' => 'email',
  7393. 'type' => 'email',
  7394. 'value' => '',
  7395. 'maxlength' => 255,
  7396. 'required' => 'required',
  7397. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7398. 'data-custom' => '1',
  7399. ],
  7400. ];
  7401. $this->assertHtml($expected, $result);
  7402. $this->Form->setConfig('autoSetCustomValidity', true);
  7403. }
  7404. /**
  7405. * testRequiredAttribute method
  7406. *
  7407. * Tests that formhelper sets required attributes.
  7408. */
  7409. public function testRequiredAttribute(): void
  7410. {
  7411. $this->article['required'] = [
  7412. 'title' => true,
  7413. 'body' => false,
  7414. ];
  7415. $this->Form->create($this->article);
  7416. $result = $this->Form->control('title');
  7417. $expected = [
  7418. 'div' => ['class' => 'input text required'],
  7419. 'label' => ['for' => 'title'],
  7420. 'Title',
  7421. '/label',
  7422. 'input' => [
  7423. 'type' => 'text',
  7424. 'name' => 'title',
  7425. 'id' => 'title',
  7426. 'required' => 'required',
  7427. 'data-validity-message' => 'This field cannot be left empty',
  7428. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7429. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7430. ],
  7431. '/div',
  7432. ];
  7433. $this->assertHtml($expected, $result);
  7434. $result = $this->Form->control('title', ['required' => false]);
  7435. $this->assertStringNotContainsString('required', $result);
  7436. $result = $this->Form->control('body');
  7437. $expected = [
  7438. 'div' => ['class' => 'input text'],
  7439. 'label' => ['for' => 'body'],
  7440. 'Body',
  7441. '/label',
  7442. 'input' => [
  7443. 'type' => 'text',
  7444. 'name' => 'body',
  7445. 'id' => 'body',
  7446. ],
  7447. '/div',
  7448. ];
  7449. $this->assertHtml($expected, $result);
  7450. $result = $this->Form->control('body', ['required' => true]);
  7451. $this->assertStringContainsString('required', $result);
  7452. }
  7453. /**
  7454. * testControlsNotNested method
  7455. *
  7456. * Tests that it is possible to put inputs outside of the label.
  7457. */
  7458. public function testControlsNotNested(): void
  7459. {
  7460. $this->Form->setTemplates([
  7461. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7462. 'formGroup' => '{{input}}{{label}}',
  7463. ]);
  7464. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7465. $expected = [
  7466. 'div' => ['class' => 'input checkbox'],
  7467. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7468. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7469. 'label' => ['for' => 'foo'],
  7470. 'Foo',
  7471. '/label',
  7472. '/div',
  7473. ];
  7474. $this->assertHtml($expected, $result);
  7475. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7476. $expected = [
  7477. 'div' => ['class' => 'input checkbox'],
  7478. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7479. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7480. '/div',
  7481. ];
  7482. $this->assertHtml($expected, $result);
  7483. $result = $this->Form->control('confirm', [
  7484. 'type' => 'radio',
  7485. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7486. ]);
  7487. $expected = [
  7488. 'div' => ['class' => 'input radio'],
  7489. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7490. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7491. ['label' => ['for' => 'confirm-y']],
  7492. 'Yes',
  7493. '/label',
  7494. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7495. ['label' => ['for' => 'confirm-n']],
  7496. 'No',
  7497. '/label',
  7498. '<label',
  7499. 'Confirm',
  7500. '/label',
  7501. '/div',
  7502. ];
  7503. $this->assertHtml($expected, $result);
  7504. $result = $this->Form->select('category', ['1', '2'], [
  7505. 'multiple' => 'checkbox',
  7506. 'name' => 'fish',
  7507. ]);
  7508. $expected = [
  7509. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7510. ['div' => ['class' => 'checkbox']],
  7511. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7512. ['label' => ['for' => 'fish-0']],
  7513. '1',
  7514. '/label',
  7515. '/div',
  7516. ['div' => ['class' => 'checkbox']],
  7517. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7518. ['label' => ['for' => 'fish-1']],
  7519. '2',
  7520. '/label',
  7521. '/div',
  7522. ];
  7523. $this->assertHtml($expected, $result);
  7524. }
  7525. /**
  7526. * testControlContainerTemplates method
  7527. *
  7528. * Test that *Container templates are used by input.
  7529. */
  7530. public function testControlContainerTemplates(): void
  7531. {
  7532. $this->Form->setTemplates([
  7533. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7534. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7535. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7536. ]);
  7537. $this->article['errors'] = [
  7538. 'Article' => ['published' => 'error message'],
  7539. ];
  7540. $this->Form->create($this->article);
  7541. $result = $this->Form->control('accept', [
  7542. 'type' => 'checkbox',
  7543. ]);
  7544. $expected = [
  7545. 'div' => ['class' => 'check'],
  7546. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7547. 'label' => ['for' => 'accept'],
  7548. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7549. 'Accept',
  7550. '/label',
  7551. '/div',
  7552. ];
  7553. $this->assertHtml($expected, $result);
  7554. $result = $this->Form->control('accept', [
  7555. 'type' => 'radio',
  7556. 'options' => ['Y', 'N'],
  7557. ]);
  7558. $this->assertStringContainsString('<div class="rad">', $result);
  7559. $result = $this->Form->control('Article.published', [
  7560. 'type' => 'radio',
  7561. 'options' => ['Y', 'N'],
  7562. ]);
  7563. $this->assertStringContainsString('<div class="rad err">', $result);
  7564. }
  7565. /**
  7566. * testFormGroupTemplates method
  7567. *
  7568. * Test that *Container templates are used by input.
  7569. */
  7570. public function testFormGroupTemplates(): void
  7571. {
  7572. $this->Form->setTemplates([
  7573. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7574. ]);
  7575. $this->Form->create($this->article);
  7576. $result = $this->Form->control('accept', [
  7577. 'type' => 'radio',
  7578. 'options' => ['Y', 'N'],
  7579. ]);
  7580. $this->assertStringContainsString('<div class="radio">', $result);
  7581. }
  7582. /**
  7583. * testResetTemplates method
  7584. *
  7585. * Test resetting templates.
  7586. */
  7587. public function testResetTemplates(): void
  7588. {
  7589. $this->Form->setTemplates(['input' => '<input/>']);
  7590. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7591. $this->Form->resetTemplates();
  7592. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7593. }
  7594. /**
  7595. * testContext method
  7596. *
  7597. * Test the context method.
  7598. */
  7599. public function testContext(): void
  7600. {
  7601. $result = $this->Form->context();
  7602. $this->assertInstanceOf(ContextInterface::class, $result);
  7603. $stub = new StubContext();
  7604. $this->assertSame($stub, $this->Form->context($stub));
  7605. $this->assertSame($stub, $this->Form->context());
  7606. }
  7607. /**
  7608. * testAutoDomId method
  7609. */
  7610. public function testAutoDomId(): void
  7611. {
  7612. $result = $this->Form->text('field', ['id' => true]);
  7613. $expected = [
  7614. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7615. ];
  7616. $this->assertHtml($expected, $result);
  7617. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7618. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7619. $expected = [
  7620. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7621. ['label' => ['for' => 'field-0']],
  7622. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7623. 'option A',
  7624. '/label',
  7625. ['label' => ['for' => 'field-1']],
  7626. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7627. 'option B',
  7628. '/label',
  7629. ];
  7630. $this->assertHtml($expected, $result);
  7631. $result = $this->Form->select(
  7632. 'multi_field',
  7633. ['first', 'second'],
  7634. ['multiple' => 'checkbox', 'id' => true]
  7635. );
  7636. $expected = [
  7637. 'input' => [
  7638. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7639. ],
  7640. ['div' => ['class' => 'checkbox']],
  7641. ['label' => ['for' => 'multi-field-0']],
  7642. ['input' => [
  7643. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7644. 'value' => '0', 'id' => 'multi-field-0',
  7645. ]],
  7646. 'first',
  7647. '/label',
  7648. '/div',
  7649. ['div' => ['class' => 'checkbox']],
  7650. ['label' => ['for' => 'multi-field-1']],
  7651. ['input' => [
  7652. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7653. 'value' => '1', 'id' => 'multi-field-1',
  7654. ]],
  7655. 'second',
  7656. '/label',
  7657. '/div',
  7658. ];
  7659. $this->assertHtml($expected, $result);
  7660. }
  7661. /**
  7662. * Test the basic setters and getters for value sources
  7663. */
  7664. public function testFormValueSourcesSettersGetters(): void
  7665. {
  7666. $this->View->setRequest($this->View->getRequest()
  7667. ->withData('id', '1')
  7668. ->withQueryParams(['id' => '2']));
  7669. $expected = ['data', 'context'];
  7670. $result = $this->Form->getValueSources();
  7671. $this->assertEquals($expected, $result);
  7672. $this->Form->setValueSources(['context']);
  7673. $result = $this->Form->getSourceValue('id');
  7674. $this->assertNull($result);
  7675. $this->Form->setValueSources('query');
  7676. $expected = ['query'];
  7677. $result = $this->Form->getValueSources();
  7678. $this->assertEquals($expected, $result);
  7679. $expected = '2';
  7680. $result = $this->Form->getSourceValue('id');
  7681. $this->assertSame($expected, $result);
  7682. $this->Form->setValueSources(['data']);
  7683. $expected = '1';
  7684. $result = $this->Form->getSourceValue('id');
  7685. $this->assertSame($expected, $result);
  7686. $this->Form->setValueSources(['query', 'data']);
  7687. $expected = '2';
  7688. $result = $this->Form->getSourceValue('id');
  7689. $this->assertSame($expected, $result);
  7690. }
  7691. public function testValueSourcesValidation(): void
  7692. {
  7693. $this->expectException(InvalidArgumentException::class);
  7694. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7695. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7696. }
  7697. /**
  7698. * Tests the different input rendering values based on sources values switching
  7699. */
  7700. public function testFormValueSourcesSingleSwitchRendering(): void
  7701. {
  7702. $articles = $this->getTableLocator()->get('Articles');
  7703. $article = new Article();
  7704. $articles->patchEntity($article, ['id' => '3']);
  7705. $this->Form->create($article);
  7706. $this->Form->setValueSources(['context']);
  7707. $result = $this->Form->control('id');
  7708. $expected = [
  7709. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7710. ];
  7711. $this->assertHtml($expected, $result);
  7712. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7713. $this->Form->setValueSources(['query']);
  7714. $result = $this->Form->control('id');
  7715. $expected = [
  7716. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7717. ];
  7718. $this->assertHtml($expected, $result);
  7719. $this->View->setRequest($this->View->getRequest()
  7720. ->withQueryParams(['id' => '5a'])
  7721. ->withData('id', '5b'));
  7722. $this->Form->setValueSources(['context']);
  7723. $this->Form->create($article);
  7724. $result = $this->Form->control('id');
  7725. $expected = [
  7726. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7727. ];
  7728. $this->assertHtml($expected, $result);
  7729. $this->Form->setValueSources(['data']);
  7730. $this->Form->create($article);
  7731. $result = $this->Form->control('id');
  7732. $expected = [
  7733. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7734. ];
  7735. $this->assertHtml($expected, $result);
  7736. $this->Form->setValueSources(['query']);
  7737. $result = $this->Form->control('id');
  7738. $expected = [
  7739. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7740. ];
  7741. $this->assertHtml($expected, $result);
  7742. }
  7743. /**
  7744. * Tests the different input rendering values based on sources values switching while supplying
  7745. * an entity (base context) and multiple sources (such as data, query)
  7746. */
  7747. public function testFormValueSourcesListSwitchRendering(): void
  7748. {
  7749. $articles = $this->getTableLocator()->get('Articles');
  7750. $article = new Article();
  7751. $articles->patchEntity($article, ['id' => '3']);
  7752. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7753. $this->Form->create($article);
  7754. $this->Form->setValueSources(['context', 'query']);
  7755. $result = $this->Form->control('id');
  7756. $expected = [
  7757. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7758. ];
  7759. $this->assertHtml($expected, $result);
  7760. $this->Form->setValueSources(['query', 'context']);
  7761. $result = $this->Form->control('id');
  7762. $expected = [
  7763. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7764. ];
  7765. $this->assertHtml($expected, $result);
  7766. $this->Form->setValueSources(['data', 'query', 'context']);
  7767. $result = $this->Form->control('id');
  7768. $expected = [
  7769. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7770. ];
  7771. $this->assertHtml($expected, $result);
  7772. $this->View->setRequest($this->View->getRequest()
  7773. ->withData('id', '8')
  7774. ->withQueryParams(['id' => '9']));
  7775. $this->Form->setValueSources(['data', 'query', 'context']);
  7776. $result = $this->Form->control('id');
  7777. $expected = [
  7778. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7779. ];
  7780. $this->assertHtml($expected, $result);
  7781. }
  7782. /**
  7783. * Test the different form input renderings based on values sources switchings through form options
  7784. */
  7785. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7786. {
  7787. $articles = $this->getTableLocator()->get('Articles');
  7788. $article = new Article();
  7789. $articles->patchEntity($article, ['id' => '3']);
  7790. $this->View->setRequest(
  7791. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7792. );
  7793. $this->Form->create($article, ['valueSources' => 'query']);
  7794. $result = $this->Form->control('id');
  7795. $expected = [
  7796. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7797. ];
  7798. $this->assertHtml($expected, $result);
  7799. $result = $this->Form->getSourceValue('id');
  7800. $this->assertSame('5', $result);
  7801. $this->Form->setValueSources(['context']);
  7802. $this->Form->create($article, ['valueSources' => 'query']);
  7803. $result = $this->Form->control('id');
  7804. $expected = [
  7805. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7806. ];
  7807. $this->assertHtml($expected, $result);
  7808. $result = $this->Form->getSourceValue('id');
  7809. $this->assertSame('5', $result);
  7810. $this->Form->setValueSources(['query']);
  7811. $this->Form->create($article, ['valueSources' => 'data']);
  7812. $result = $this->Form->control('id');
  7813. $expected = [
  7814. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7815. ];
  7816. $this->assertHtml($expected, $result);
  7817. $result = $this->Form->getSourceValue('id');
  7818. $this->assertSame('4', $result);
  7819. $this->Form->setValueSources(['query']);
  7820. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7821. $result = $this->Form->control('id');
  7822. $expected = [
  7823. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7824. ];
  7825. $this->assertHtml($expected, $result);
  7826. $result = $this->Form->getSourceValue('id');
  7827. $this->assertSame(3, $result);
  7828. }
  7829. /**
  7830. * Test the different form input renderings based on values sources switchings through form options
  7831. */
  7832. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7833. {
  7834. $articles = $this->getTableLocator()->get('Articles');
  7835. $article = new Article();
  7836. $articles->patchEntity($article, ['id' => '3']);
  7837. $this->View->setRequest(
  7838. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7839. );
  7840. $this->Form->setValueSources(['context'])
  7841. ->create($article, ['valueSources' => ['query', 'data']]);
  7842. $result = $this->Form->control('id');
  7843. $expected = [
  7844. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7845. ];
  7846. $this->assertHtml($expected, $result);
  7847. $result = $this->Form->getSourceValue('id');
  7848. $this->assertSame('11', $result);
  7849. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7850. $this->Form->setValueSources(['context'])
  7851. ->create($article, ['valueSources' => ['query', 'data']]);
  7852. $result = $this->Form->control('id');
  7853. $expected = [
  7854. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7855. ];
  7856. $this->assertHtml($expected, $result);
  7857. $result = $this->Form->getSourceValue('id');
  7858. $this->assertSame('10', $result);
  7859. }
  7860. /**
  7861. * Test the different form values sources resetting through From::end();
  7862. */
  7863. public function testFormValueSourcesResetViaEnd(): void
  7864. {
  7865. $expected = ['data', 'context'];
  7866. $result = $this->Form->getValueSources();
  7867. $this->assertEquals($expected, $result);
  7868. $expected = ['query', 'context', 'data'];
  7869. $this->Form->setValueSources(['query', 'context', 'data']);
  7870. $result = $this->Form->getValueSources();
  7871. $this->assertEquals($expected, $result);
  7872. $this->Form->create();
  7873. $result = $this->Form->getValueSources();
  7874. $this->assertEquals($expected, $result);
  7875. $this->Form->end();
  7876. $result = $this->Form->getValueSources();
  7877. $this->assertEquals(['data', 'context'], $result);
  7878. }
  7879. /**
  7880. * Test sources values defaults handling
  7881. */
  7882. public function testFormValueSourcesDefaults(): void
  7883. {
  7884. $this->View->setRequest(
  7885. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7886. );
  7887. $this->Form->create();
  7888. $result = $this->Form->password('password');
  7889. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7890. $this->assertHtml($expected, $result);
  7891. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7892. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7893. $this->assertHtml($expected, $result);
  7894. $this->Form->setValueSources('query');
  7895. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7896. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7897. $this->assertHtml($expected, $result);
  7898. $this->Form->setValueSources('data');
  7899. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7900. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7901. $this->assertHtml($expected, $result);
  7902. }
  7903. /**
  7904. * Test sources values schema defaults handling
  7905. */
  7906. public function testSourcesValueDoesntExistPassThrough(): void
  7907. {
  7908. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7909. $articles = $this->getTableLocator()->get('Articles');
  7910. $entity = $articles->newEmptyEntity();
  7911. $this->Form->create($entity);
  7912. $this->Form->setValueSources(['query', 'context']);
  7913. $result = $this->Form->getSourceValue('category');
  7914. $this->assertSame('sesame-cookies', $result);
  7915. $this->Form->setValueSources(['context', 'query']);
  7916. $result = $this->Form->getSourceValue('category');
  7917. $this->assertSame('sesame-cookies', $result);
  7918. }
  7919. /**
  7920. * testNestedLabelInput method
  7921. *
  7922. * Test the `nestedInput` parameter
  7923. */
  7924. public function testNestedLabelInput(): void
  7925. {
  7926. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7927. $expected = [
  7928. 'div' => ['class' => 'input text'],
  7929. 'label' => ['for' => 'foo'],
  7930. ['input' => [
  7931. 'type' => 'text',
  7932. 'name' => 'foo',
  7933. 'id' => 'foo',
  7934. ]],
  7935. 'Foo',
  7936. '/label',
  7937. '/div',
  7938. ];
  7939. $this->assertHtml($expected, $result);
  7940. }
  7941. /**
  7942. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7943. *
  7944. * This test makes sure `false` excludes the label from the render
  7945. */
  7946. public function testControlLabelManipulationDisableLabels(): void
  7947. {
  7948. $result = $this->Form->control('test', [
  7949. 'type' => 'radio',
  7950. 'options' => ['A', 'B'],
  7951. 'labelOptions' => false,
  7952. ]);
  7953. $expected = [
  7954. ['div' => ['class' => 'input radio']],
  7955. '<label',
  7956. 'Test',
  7957. '/label',
  7958. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7959. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7960. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7961. '/div',
  7962. ];
  7963. $this->assertHtml($expected, $result);
  7964. $result = $this->Form->control('checkbox1', [
  7965. 'label' => 'My checkboxes',
  7966. 'multiple' => 'checkbox',
  7967. 'type' => 'select',
  7968. 'options' => [
  7969. ['text' => 'First Checkbox', 'value' => 1],
  7970. ['text' => 'Second Checkbox', 'value' => 2],
  7971. ],
  7972. 'labelOptions' => false,
  7973. ]);
  7974. $expected = [
  7975. ['div' => ['class' => 'input select']],
  7976. ['label' => ['for' => 'checkbox1']],
  7977. 'My checkboxes',
  7978. '/label',
  7979. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7980. ['div' => ['class' => 'checkbox']],
  7981. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7982. '/div',
  7983. ['div' => ['class' => 'checkbox']],
  7984. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7985. '/div',
  7986. '/div',
  7987. ];
  7988. $this->assertHtml($expected, $result);
  7989. }
  7990. /**
  7991. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7992. *
  7993. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7994. * added to the class if checked.
  7995. *
  7996. * Also checks to make sure any custom attributes are rendered correctly
  7997. */
  7998. public function testControlLabelManipulationRadios(): void
  7999. {
  8000. $result = $this->Form->control('test', [
  8001. 'type' => 'radio',
  8002. 'options' => ['A', 'B'],
  8003. 'labelOptions' => ['class' => 'custom-class'],
  8004. ]);
  8005. $expected = [
  8006. ['div' => ['class' => 'input radio']],
  8007. '<label',
  8008. 'Test',
  8009. '/label',
  8010. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8011. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8012. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8013. 'A',
  8014. '/label',
  8015. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8016. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8017. 'B',
  8018. '/label',
  8019. '/div',
  8020. ];
  8021. $this->assertHtml($expected, $result);
  8022. $result = $this->Form->control('test', [
  8023. 'type' => 'radio',
  8024. 'options' => ['A', 'B'],
  8025. 'value' => 1,
  8026. 'labelOptions' => ['class' => 'custom-class'],
  8027. ]);
  8028. $expected = [
  8029. ['div' => ['class' => 'input radio']],
  8030. '<label',
  8031. 'Test',
  8032. '/label',
  8033. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8034. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8035. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8036. 'A',
  8037. '/label',
  8038. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8039. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8040. 'B',
  8041. '/label',
  8042. '/div',
  8043. ];
  8044. $this->assertHtml($expected, $result);
  8045. $result = $this->Form->control('test', [
  8046. 'type' => 'radio',
  8047. 'options' => ['A', 'B'],
  8048. 'value' => 1,
  8049. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8050. ]);
  8051. $expected = [
  8052. ['div' => ['class' => 'input radio']],
  8053. '<label',
  8054. 'Test',
  8055. '/label',
  8056. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8057. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8058. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8059. 'A',
  8060. '/label',
  8061. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8062. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8063. 'B',
  8064. '/label',
  8065. '/div',
  8066. ];
  8067. $this->assertHtml($expected, $result);
  8068. $result = $this->Form->radio('test', ['A', 'B'], [
  8069. 'label' => [
  8070. 'class' => ['custom-class', 'another-class'],
  8071. 'data-name' => 'bob',
  8072. ],
  8073. 'value' => 1,
  8074. ]);
  8075. $expected = [
  8076. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  8077. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8078. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8079. 'A',
  8080. '/label',
  8081. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8082. ['input' => [
  8083. 'type' => 'radio',
  8084. 'name' => 'test',
  8085. 'value' => '1',
  8086. 'id' => 'test-1',
  8087. 'checked' => 'checked',
  8088. ]],
  8089. 'B',
  8090. '/label',
  8091. ];
  8092. $this->assertHtml($expected, $result);
  8093. }
  8094. /**
  8095. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8096. *
  8097. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8098. * added to the class if checked.
  8099. *
  8100. * Also checks to make sure any custom attributes are rendered correctly
  8101. */
  8102. public function testControlLabelManipulationCheckboxes(): void
  8103. {
  8104. $result = $this->Form->control('checkbox1', [
  8105. 'label' => 'My checkboxes',
  8106. 'multiple' => 'checkbox',
  8107. 'type' => 'select',
  8108. 'options' => [
  8109. ['text' => 'First Checkbox', 'value' => 1],
  8110. ['text' => 'Second Checkbox', 'value' => 2],
  8111. ],
  8112. 'labelOptions' => ['class' => 'custom-class'],
  8113. 'value' => ['1'],
  8114. ]);
  8115. $expected = [
  8116. ['div' => ['class' => 'input select']],
  8117. ['label' => ['for' => 'checkbox1']],
  8118. 'My checkboxes',
  8119. '/label',
  8120. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8121. ['div' => ['class' => 'checkbox']],
  8122. ['label' => [
  8123. 'class' => 'custom-class selected',
  8124. 'for' => 'checkbox1-1',
  8125. ]],
  8126. ['input' => [
  8127. 'type' => 'checkbox',
  8128. 'name' => 'checkbox1[]',
  8129. 'value' => '1',
  8130. 'id' => 'checkbox1-1',
  8131. 'checked' => 'checked',
  8132. ]],
  8133. 'First Checkbox',
  8134. '/label',
  8135. '/div',
  8136. ['div' => ['class' => 'checkbox']],
  8137. ['label' => [
  8138. 'class' => 'custom-class',
  8139. 'for' => 'checkbox1-2',
  8140. ]],
  8141. ['input' => [
  8142. 'type' => 'checkbox',
  8143. 'name' => 'checkbox1[]',
  8144. 'value' => '2',
  8145. 'id' => 'checkbox1-2',
  8146. ]],
  8147. 'Second Checkbox',
  8148. '/label',
  8149. '/div',
  8150. '/div',
  8151. ];
  8152. $this->assertHtml($expected, $result);
  8153. $result = $this->Form->control('checkbox1', [
  8154. 'label' => 'My checkboxes',
  8155. 'multiple' => 'checkbox',
  8156. 'type' => 'select',
  8157. 'options' => [
  8158. ['text' => 'First Checkbox', 'value' => 1],
  8159. ['text' => 'Second Checkbox', 'value' => 2],
  8160. ],
  8161. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8162. 'value' => ['1'],
  8163. ]);
  8164. $expected = [
  8165. ['div' => ['class' => 'input select']],
  8166. ['label' => ['for' => 'checkbox1']],
  8167. 'My checkboxes',
  8168. '/label',
  8169. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8170. ['div' => ['class' => 'checkbox']],
  8171. ['label' => [
  8172. 'class' => 'custom-class another-class selected',
  8173. 'data-name' => 'bob',
  8174. 'for' => 'checkbox1-1',
  8175. ]],
  8176. ['input' => [
  8177. 'type' => 'checkbox',
  8178. 'name' => 'checkbox1[]',
  8179. 'value' => '1',
  8180. 'id' => 'checkbox1-1',
  8181. 'checked' => 'checked',
  8182. ]],
  8183. 'First Checkbox',
  8184. '/label',
  8185. '/div',
  8186. ['div' => ['class' => 'checkbox']],
  8187. ['label' => [
  8188. 'class' => 'custom-class another-class',
  8189. 'data-name' => 'bob',
  8190. 'for' => 'checkbox1-2',
  8191. ]],
  8192. ['input' => [
  8193. 'type' => 'checkbox',
  8194. 'name' => 'checkbox1[]',
  8195. 'value' => '2',
  8196. 'id' => 'checkbox1-2',
  8197. ]],
  8198. 'Second Checkbox',
  8199. '/label',
  8200. '/div',
  8201. '/div',
  8202. ];
  8203. $this->assertHtml($expected, $result);
  8204. }
  8205. /**
  8206. * testControlMaxLengthArrayContext method
  8207. *
  8208. * Test control() with maxlength attribute in Array Context.
  8209. */
  8210. public function testControlMaxLengthArrayContext(): void
  8211. {
  8212. $this->article['schema'] = [
  8213. 'title' => ['length' => 10],
  8214. ];
  8215. $this->Form->create($this->article);
  8216. $result = $this->Form->control('title');
  8217. $expected = [
  8218. 'div' => ['class'],
  8219. 'label' => ['for'],
  8220. 'Title',
  8221. '/label',
  8222. 'input' => [
  8223. 'id',
  8224. 'name' => 'title',
  8225. 'type' => 'text',
  8226. 'required' => 'required',
  8227. 'maxlength' => 10,
  8228. 'data-validity-message' => 'This field cannot be left empty',
  8229. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8230. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8231. ],
  8232. '/div',
  8233. ];
  8234. $this->assertHtml($expected, $result);
  8235. }
  8236. /**
  8237. * testControlMaxLengthEntityContext method
  8238. *
  8239. * Test control() with maxlength attribute in Entity Context.
  8240. */
  8241. public function testControlMaxLengthEntityContext(): void
  8242. {
  8243. $this->article['schema']['title']['length'] = 45;
  8244. $validator = new Validator();
  8245. $validator->maxLength('title', 10);
  8246. $article = new EntityContext(
  8247. [
  8248. 'entity' => new Entity($this->article),
  8249. 'table' => new Table([
  8250. 'alias' => 'Articles',
  8251. 'schema' => $this->article['schema'],
  8252. 'validator' => $validator,
  8253. ]),
  8254. ]
  8255. );
  8256. $this->Form->create($article);
  8257. $result = $this->Form->control('title');
  8258. $expected = [
  8259. 'div' => ['class'],
  8260. 'label' => ['for'],
  8261. 'Title',
  8262. '/label',
  8263. 'input' => [
  8264. 'id',
  8265. 'name' => 'title',
  8266. 'type' => 'text',
  8267. 'required' => 'required',
  8268. 'maxlength' => 10,
  8269. 'data-validity-message' => 'This field cannot be left empty',
  8270. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8271. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8272. ],
  8273. '/div',
  8274. ];
  8275. $this->assertHtml($expected, $result);
  8276. $this->article['schema']['title']['length'] = 45;
  8277. $validator = new Validator();
  8278. $validator->maxLength('title', 55);
  8279. $article = new EntityContext(
  8280. [
  8281. 'entity' => new Entity($this->article),
  8282. 'table' => new Table([
  8283. 'schema' => $this->article['schema'],
  8284. 'validator' => $validator,
  8285. 'alias' => 'Articles',
  8286. ]),
  8287. ]
  8288. );
  8289. $this->Form->create($article);
  8290. $result = $this->Form->control('title');
  8291. $expected = [
  8292. 'div' => ['class'],
  8293. 'label' => ['for'],
  8294. 'Title',
  8295. '/label',
  8296. 'input' => [
  8297. 'id',
  8298. 'name' => 'title',
  8299. 'type' => 'text',
  8300. 'required' => 'required',
  8301. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8302. 'data-validity-message' => 'This field cannot be left empty',
  8303. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8304. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8305. ],
  8306. '/div',
  8307. ];
  8308. $this->assertHtml($expected, $result);
  8309. $this->article['schema']['title']['length'] = 45;
  8310. $validator = new Validator();
  8311. $validator->maxLength('title', 55);
  8312. $article = new EntityContext(
  8313. [
  8314. 'entity' => new Entity($this->article),
  8315. 'table' => new Table([
  8316. 'schema' => $this->article['schema'],
  8317. 'validator' => $validator,
  8318. 'alias' => 'Articles',
  8319. ]),
  8320. ]
  8321. );
  8322. $this->Form->create($article);
  8323. $result = $this->Form->control('title', ['maxlength' => 10]);
  8324. $expected = [
  8325. 'div' => ['class'],
  8326. 'label' => ['for'],
  8327. 'Title',
  8328. '/label',
  8329. 'input' => [
  8330. 'id',
  8331. 'name' => 'title',
  8332. 'type' => 'text',
  8333. 'required' => 'required',
  8334. 'maxlength' => 10, // Length set in options should take highest precedence.
  8335. 'data-validity-message' => 'This field cannot be left empty',
  8336. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8337. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8338. ],
  8339. '/div',
  8340. ];
  8341. $this->assertHtml($expected, $result);
  8342. }
  8343. /**
  8344. * testControlMinMaxLengthEntityContext method
  8345. *
  8346. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8347. */
  8348. public function testControlMinMaxLengthEntityContext(): void
  8349. {
  8350. $validator = new Validator();
  8351. $validator->maxLength('title', 10);
  8352. $article = new EntityContext(
  8353. [
  8354. 'entity' => new Entity($this->article),
  8355. 'table' => new Table([
  8356. 'alias' => 'Articles',
  8357. 'schema' => $this->article['schema'],
  8358. 'validator' => $validator,
  8359. ]),
  8360. ]
  8361. );
  8362. $this->Form->create($article);
  8363. $result = $this->Form->control('title');
  8364. $expected = [
  8365. 'div' => ['class'],
  8366. 'label' => ['for'],
  8367. 'Title',
  8368. '/label',
  8369. 'input' => [
  8370. 'id',
  8371. 'name' => 'title',
  8372. 'type' => 'text',
  8373. 'required' => 'required',
  8374. 'maxlength' => 10,
  8375. 'data-validity-message' => 'This field cannot be left empty',
  8376. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8377. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8378. ],
  8379. '/div',
  8380. ];
  8381. $this->assertHtml($expected, $result);
  8382. }
  8383. /**
  8384. * testControlMaxLengthFormContext method
  8385. *
  8386. * Test control() with maxlength attribute in Form Context.
  8387. */
  8388. public function testControlMaxLengthFormContext(): void
  8389. {
  8390. $validator = new Validator();
  8391. $validator->maxLength('title', 10);
  8392. $form = new Form();
  8393. $form->setValidator('default', $validator);
  8394. $this->Form->create($form);
  8395. $result = $this->Form->control('title');
  8396. $expected = [
  8397. 'div' => ['class'],
  8398. 'label' => ['for'],
  8399. 'Title',
  8400. '/label',
  8401. 'input' => [
  8402. 'id',
  8403. 'name' => 'title',
  8404. 'type' => 'text',
  8405. 'required' => 'required',
  8406. 'maxlength' => 10,
  8407. 'data-validity-message' => 'This field cannot be left empty',
  8408. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8409. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8410. ],
  8411. '/div',
  8412. ];
  8413. $this->assertHtml($expected, $result);
  8414. }
  8415. }