FormHelperTest.php 311 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use ArrayObject;
  18. use Cake\Collection\Collection;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Exception\CakeException;
  21. use Cake\Database\Type\EnumType;
  22. use Cake\Form\Form;
  23. use Cake\Http\ServerRequest;
  24. use Cake\I18n\Date;
  25. use Cake\I18n\DateTime;
  26. use Cake\ORM\Entity;
  27. use Cake\ORM\Table;
  28. use Cake\Routing\Router;
  29. use Cake\TestSuite\TestCase;
  30. use Cake\Utility\Security;
  31. use Cake\Validation\Validator;
  32. use Cake\View\Form\EntityContext;
  33. use Cake\View\Helper\FormHelper;
  34. use Cake\View\View;
  35. use Cake\View\Widget\WidgetLocator;
  36. use InvalidArgumentException;
  37. use ReflectionProperty;
  38. use TestApp\Model\Entity\Article;
  39. use TestApp\Model\Enum\ArticleStatus;
  40. use TestApp\Model\Enum\ArticleStatusLabel;
  41. use TestApp\Model\Table\ContactsTable;
  42. use TestApp\Model\Table\ValidateUsersTable;
  43. use TestApp\View\Form\StubContext;
  44. /**
  45. * FormHelperTest class
  46. *
  47. * @property \Cake\View\Helper\FormHelper $Form
  48. * @property \Cake\View\View $View
  49. */
  50. class FormHelperTest extends TestCase
  51. {
  52. /**
  53. * Fixtures to be used
  54. *
  55. * @var array<string>
  56. */
  57. protected array $fixtures = ['core.Articles', 'core.Comments'];
  58. /**
  59. * @var array
  60. */
  61. protected $article = [];
  62. /**
  63. * @var string
  64. */
  65. protected $url;
  66. /**
  67. * @var \Cake\View\Helper\FormHelper
  68. */
  69. protected $Form;
  70. /**
  71. * @var \Cake\View\View
  72. */
  73. protected $View;
  74. /**
  75. * setUp method
  76. */
  77. public function setUp(): void
  78. {
  79. parent::setUp();
  80. Configure::write('Config.language', 'eng');
  81. Configure::write('App.base', '');
  82. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  83. $request = new ServerRequest([
  84. 'webroot' => '',
  85. 'base' => '',
  86. 'url' => '/articles/add',
  87. 'params' => [
  88. 'controller' => 'Articles',
  89. 'action' => 'add',
  90. 'plugin' => null,
  91. ],
  92. ]);
  93. $this->View = new View($request);
  94. Router::reload();
  95. Router::setRequest($request);
  96. $this->url = '/articles/add';
  97. $this->Form = new FormHelper($this->View);
  98. $this->article = [
  99. 'schema' => [
  100. 'id' => ['type' => 'integer'],
  101. 'author_id' => ['type' => 'integer', 'null' => true],
  102. 'title' => ['type' => 'string', 'null' => true],
  103. 'body' => 'text',
  104. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  105. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  106. ],
  107. 'required' => [
  108. 'author_id' => true,
  109. 'title' => true,
  110. ],
  111. ];
  112. Security::setSalt('foo!');
  113. $builder = Router::createRouteBuilder('/');
  114. $builder->connect('/{controller}', ['action' => 'index']);
  115. $builder->connect('/{controller}/{action}/*');
  116. }
  117. /**
  118. * tearDown method
  119. */
  120. public function tearDown(): void
  121. {
  122. parent::tearDown();
  123. unset($this->Form, $this->View);
  124. }
  125. /**
  126. * Test construct() with the templates option.
  127. */
  128. public function testConstructTemplatesFile(): void
  129. {
  130. $helper = new FormHelper($this->View, [
  131. 'templates' => 'htmlhelper_tags',
  132. ]);
  133. $result = $helper->control('name');
  134. $this->assertStringContainsString('<input', $result);
  135. }
  136. /**
  137. * Test that when specifying custom widgets the config array for that widget
  138. * is overwritten instead of merged.
  139. */
  140. public function testConstructWithWidgets(): void
  141. {
  142. $config = [
  143. 'widgets' => [
  144. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  145. ],
  146. ];
  147. $helper = new FormHelper($this->View, $config);
  148. $locator = $helper->getWidgetLocator();
  149. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  150. }
  151. /**
  152. * Test that when specifying custom widgets config file and it should be
  153. * added to widgets array. WidgetLocator will load widgets in constructor.
  154. */
  155. public function testConstructWithWidgetsConfig(): void
  156. {
  157. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  158. $locator = $helper->getWidgetLocator();
  159. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  160. }
  161. /**
  162. * Test setting the widget locator
  163. */
  164. public function testSetAndGetWidgetLocator(): void
  165. {
  166. $helper = new FormHelper($this->View);
  167. $locator = new WidgetLocator($helper->templater(), $this->View);
  168. $helper->setWidgetLocator($locator);
  169. $this->assertSame($locator, $helper->getWidgetLocator());
  170. }
  171. /**
  172. * Test overridding grouped input types which controls generation of "for"
  173. * attribute of labels.
  174. */
  175. public function testConstructWithGroupedInputTypes(): void
  176. {
  177. $helper = new FormHelper($this->View, [
  178. 'groupedInputTypes' => ['radio'],
  179. ]);
  180. $result = $helper->control('when', ['type' => 'datetime-local']);
  181. $this->assertStringContainsString('<label for="when">When</label>', $result);
  182. }
  183. /**
  184. * Test registering a new widget class and rendering it.
  185. */
  186. public function testAddWidgetAndRenderWidget(): void
  187. {
  188. $data = [
  189. 'val' => 1,
  190. ];
  191. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  192. $this->Form->addWidget('test', $mock);
  193. $mock->expects($this->once())
  194. ->method('render')
  195. ->with($data)
  196. ->willReturn('HTML');
  197. $result = $this->Form->widget('test', $data);
  198. $this->assertSame('HTML', $result);
  199. }
  200. /**
  201. * Test that secureFields() of widget is called after calling render(),
  202. * not before.
  203. */
  204. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  205. {
  206. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  207. 'unlockedFields' => [],
  208. ]));
  209. $data = [
  210. 'val' => 1,
  211. 'name' => 'test',
  212. ];
  213. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  214. $this->Form->addWidget('test', $mock);
  215. $mock->expects($this->once())
  216. ->method('render')
  217. ->with($data)
  218. ->willReturn('HTML');
  219. $mock->expects($this->once())
  220. ->method('secureFields')
  221. ->with($data)
  222. ->willReturn(['test']);
  223. $this->Form->create();
  224. $result = $this->Form->widget('test', $data + ['secure' => true]);
  225. $this->assertSame('HTML', $result);
  226. }
  227. /**
  228. * Test that empty string is not added to secure fields list when
  229. * rendering input widget without name.
  230. */
  231. public function testRenderingWidgetWithEmptyName(): void
  232. {
  233. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  234. $this->Form->create();
  235. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  236. $this->assertSame('<select name=""></select>', $result);
  237. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  238. $this->assertEquals([], $result);
  239. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  240. $this->assertSame('<select name="0"></select>', $result);
  241. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  242. $this->assertEquals(['0'], $result);
  243. }
  244. /**
  245. * Test adding a new context class.
  246. */
  247. public function testAddContextProvider(): void
  248. {
  249. $context = 'My data';
  250. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  251. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  252. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  253. $this->assertSame($context, $data['entity']);
  254. return $stub;
  255. });
  256. $this->Form->create($context);
  257. $result = $this->Form->context();
  258. $this->assertSame($stub, $result);
  259. }
  260. /**
  261. * Test replacing a context class.
  262. */
  263. public function testAddContextProviderReplace(): void
  264. {
  265. $entity = new Article();
  266. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  267. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  268. return $stub;
  269. });
  270. $this->Form->create($entity);
  271. $result = $this->Form->context();
  272. $this->assertSame($stub, $result);
  273. }
  274. /**
  275. * Test overriding a context class.
  276. */
  277. public function testAddContextProviderAdd(): void
  278. {
  279. $entity = new Article();
  280. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  281. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  282. if ($data['entity'] instanceof Entity) {
  283. return $stub;
  284. }
  285. });
  286. $this->Form->create($entity);
  287. $result = $this->Form->context();
  288. $this->assertSame($stub, $result);
  289. }
  290. /**
  291. * Provides context options for create().
  292. *
  293. * @return array
  294. */
  295. public static function contextSelectionProvider(): array
  296. {
  297. $entity = new Article();
  298. $collection = new Collection([$entity]);
  299. $emptyCollection = new Collection([]);
  300. $data = [
  301. 'schema' => [
  302. 'title' => ['type' => 'string'],
  303. ],
  304. ];
  305. $form = new Form();
  306. $custom = new StubContext();
  307. return [
  308. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  309. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  310. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  311. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  312. 'form' => [$form, 'Cake\View\Form\FormContext'],
  313. 'none' => [null, 'Cake\View\Form\NullContext'],
  314. 'custom' => [$custom, $custom::class],
  315. ];
  316. }
  317. /**
  318. * Test default context selection in create()
  319. *
  320. * @dataProvider contextSelectionProvider
  321. * @param mixed $data
  322. */
  323. public function testCreateContextSelectionBuiltIn($data, string $class): void
  324. {
  325. $this->Form->create($data);
  326. $this->assertInstanceOf($class, $this->Form->context());
  327. }
  328. /**
  329. * Data provider for type option.
  330. *
  331. * @return array
  332. */
  333. public static function requestTypeProvider(): array
  334. {
  335. return [
  336. // type, method, override
  337. ['post', 'post', 'POST'],
  338. ['put', 'post', 'PUT'],
  339. ['patch', 'post', 'PATCH'],
  340. ['delete', 'post', 'DELETE'],
  341. ];
  342. }
  343. /**
  344. * Test creating file forms.
  345. */
  346. public function testCreateFile(): void
  347. {
  348. $encoding = strtolower(Configure::read('App.encoding'));
  349. $result = $this->Form->create(null, ['type' => 'file']);
  350. $expected = [
  351. 'form' => [
  352. 'method' => 'post', 'action' => '/articles/add',
  353. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  354. ],
  355. 'div' => ['style' => 'display:none;'],
  356. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  357. '/div',
  358. ];
  359. $this->assertHtml($expected, $result);
  360. }
  361. /**
  362. * Test creating GET forms.
  363. */
  364. public function testCreateGet(): void
  365. {
  366. $encoding = strtolower(Configure::read('App.encoding'));
  367. $result = $this->Form->create(null, ['type' => 'get']);
  368. $expected = ['form' => [
  369. 'method' => 'get', 'action' => '/articles/add',
  370. 'accept-charset' => $encoding,
  371. ]];
  372. $this->assertHtml($expected, $result);
  373. }
  374. /**
  375. * Test explicit method/enctype options.
  376. *
  377. * Explicit method overwrites inferred method from 'type'
  378. */
  379. public function testCreateExplicitMethodEnctype(): void
  380. {
  381. $encoding = strtolower(Configure::read('App.encoding'));
  382. $result = $this->Form->create(null, [
  383. 'type' => 'get',
  384. 'method' => 'put',
  385. 'enctype' => 'multipart/form-data',
  386. ]);
  387. $expected = ['form' => [
  388. 'method' => 'put',
  389. 'action' => '/articles/add',
  390. 'enctype' => 'multipart/form-data',
  391. 'accept-charset' => $encoding,
  392. ]];
  393. $this->assertHtml($expected, $result);
  394. }
  395. /**
  396. * Test create() with the templates option.
  397. */
  398. public function testCreateTemplatesArray(): void
  399. {
  400. $result = $this->Form->create($this->article, [
  401. 'templates' => [
  402. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  403. ],
  404. ]);
  405. $expected = [
  406. 'form' => [
  407. 'class' => 'form-horizontal',
  408. 'method' => 'post',
  409. 'action' => '/articles/add',
  410. 'accept-charset' => 'utf-8',
  411. ],
  412. ];
  413. $this->assertHtml($expected, $result);
  414. }
  415. /**
  416. * Test create() with the templates option.
  417. */
  418. public function testCreateTemplatesFile(): void
  419. {
  420. $result = $this->Form->create($this->article, [
  421. 'templates' => 'htmlhelper_tags',
  422. ]);
  423. $expected = [
  424. 'start form',
  425. ];
  426. $this->assertHtml($expected, $result);
  427. }
  428. /**
  429. * Test that create() and end() restore templates.
  430. */
  431. public function testCreateEndRestoreTemplates(): void
  432. {
  433. $this->Form->create($this->article, [
  434. 'templates' => ['input' => 'custom input element'],
  435. ]);
  436. $this->Form->end();
  437. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  438. }
  439. /**
  440. * Test create() with the templates option.
  441. */
  442. public function testCreateTemplatesRequiredClass(): void
  443. {
  444. $this->Form->create($this->article, [
  445. 'templates' => [
  446. 'requiredClass' => 'is-required',
  447. ],
  448. ]);
  449. $result = $this->Form->control('title');
  450. $expected = [
  451. 'div' => ['class' => 'input text is-required'],
  452. 'label' => ['for' => 'title'],
  453. 'Title',
  454. '/label',
  455. 'input' => [
  456. 'type' => 'text',
  457. 'name' => 'title',
  458. 'id' => 'title',
  459. 'required' => 'required',
  460. 'data-validity-message' => 'This field cannot be left empty',
  461. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  462. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  463. 'aria-required' => 'true',
  464. ],
  465. '/div',
  466. ];
  467. $this->assertHtml($expected, $result);
  468. }
  469. /**
  470. * Test using template vars in various templates used by control() method.
  471. */
  472. public function testControlTemplateVars(): void
  473. {
  474. $result = $this->Form->control('text', [
  475. 'templates' => [
  476. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  477. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  478. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  479. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  480. ],
  481. 'templateVars' => [
  482. 'forinput' => 'in-input',
  483. 'forlabel' => 'in-label',
  484. 'forgroup' => 'in-group',
  485. 'forcontainer' => 'in-container',
  486. ],
  487. ]);
  488. $expected = [
  489. 'div' => ['class'],
  490. 'label' => ['for'],
  491. 'Text in-label',
  492. '/label',
  493. 'in-group',
  494. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  495. 'in-container',
  496. '/div',
  497. ];
  498. $this->assertHtml($expected, $result);
  499. }
  500. /**
  501. * Test ensuring template variables work in template files loaded
  502. * during control().
  503. */
  504. public function testControlTemplatesFromFile(): void
  505. {
  506. $result = $this->Form->control('title', [
  507. 'templates' => 'test_templates',
  508. 'templateVars' => [
  509. 'forcontainer' => 'container-data',
  510. ],
  511. ]);
  512. $expected = [
  513. 'div' => ['class'],
  514. 'label' => ['for'],
  515. 'Title',
  516. '/label',
  517. 'input' => ['name', 'type' => 'text', 'id'],
  518. 'container-data',
  519. '/div',
  520. ];
  521. $this->assertHtml($expected, $result);
  522. }
  523. /**
  524. * Test using template vars in inputSubmit and submitContainer template.
  525. */
  526. public function testSubmitTemplateVars(): void
  527. {
  528. $this->Form->setTemplates([
  529. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  530. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  531. ]);
  532. $result = $this->Form->submit('Submit', [
  533. 'templateVars' => [
  534. 'forinput' => 'in-input',
  535. 'forcontainer' => 'in-container',
  536. ],
  537. ]);
  538. $expected = [
  539. 'div' => ['class'],
  540. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  541. 'in-container',
  542. '/div',
  543. ];
  544. $this->assertHtml($expected, $result);
  545. }
  546. /**
  547. * test the create() method
  548. *
  549. * @dataProvider requestTypeProvider
  550. */
  551. public function testCreateTypeOptions(string $type, string $method, string $override): void
  552. {
  553. $encoding = strtolower(Configure::read('App.encoding'));
  554. $result = $this->Form->create(null, ['type' => $type]);
  555. $expected = [
  556. 'form' => [
  557. 'method' => $method, 'action' => '/articles/add',
  558. 'accept-charset' => $encoding,
  559. ],
  560. ];
  561. $extra = [
  562. 'div' => ['style' => 'display:none;'],
  563. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  564. '/div',
  565. ];
  566. if ($type !== 'post') {
  567. $expected = array_merge($expected, $extra);
  568. }
  569. $this->assertHtml($expected, $result);
  570. }
  571. /**
  572. * Test using template vars in Create (formStart template)
  573. */
  574. public function testCreateTemplateVars(): void
  575. {
  576. $result = $this->Form->create($this->article, [
  577. 'templates' => [
  578. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  579. ],
  580. 'templateVars' => ['header' => 'headertext'],
  581. ]);
  582. $expected = [
  583. 'h4' => ['class'],
  584. 'headertext',
  585. '/h4',
  586. 'form' => [
  587. 'method' => 'post',
  588. 'action' => '/articles/add',
  589. 'accept-charset' => 'utf-8',
  590. ],
  591. ];
  592. $this->assertHtml($expected, $result);
  593. }
  594. /**
  595. * Test opening a form for an update operation.
  596. */
  597. public function testCreateUpdateForm(): void
  598. {
  599. $encoding = strtolower(Configure::read('App.encoding'));
  600. $this->View->setRequest($this->View->getRequest()
  601. ->withRequestTarget('/articles/edit/1')
  602. ->withParam('action', 'edit'));
  603. $this->article['defaults']['id'] = 1;
  604. $result = $this->Form->create($this->article);
  605. $expected = [
  606. 'form' => [
  607. 'method' => 'post',
  608. 'action' => '/articles/edit/1',
  609. 'accept-charset' => $encoding,
  610. ],
  611. 'div' => ['style' => 'display:none;'],
  612. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  613. '/div',
  614. ];
  615. $this->assertHtml($expected, $result);
  616. }
  617. /**
  618. * test create() with automatic url generation
  619. */
  620. public function testCreateAutoUrl(): void
  621. {
  622. $encoding = strtolower(Configure::read('App.encoding'));
  623. $this->View->setRequest($this->View->getRequest()
  624. ->withRequestTarget('/articles/delete/10')
  625. ->withParam('action', 'delete'));
  626. $result = $this->Form->create($this->article);
  627. $expected = [
  628. 'form' => [
  629. 'method' => 'post', 'action' => '/articles/delete/10',
  630. 'accept-charset' => $encoding,
  631. ],
  632. ];
  633. $this->assertHtml($expected, $result);
  634. $this->article['defaults'] = ['id' => 1];
  635. $this->View->setRequest($this->View->getRequest()
  636. ->withRequestTarget('/Articles/edit/1')
  637. ->withParam('action', 'delete'));
  638. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  639. $expected = [
  640. 'form' => [
  641. 'method' => 'post',
  642. 'action' => '/Articles/edit/1',
  643. 'accept-charset' => $encoding,
  644. ],
  645. 'div' => ['style' => 'display:none;'],
  646. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  647. '/div',
  648. ];
  649. $this->assertHtml($expected, $result);
  650. $this->View->setRequest($this->View->getRequest()
  651. ->withParam('action', 'add'));
  652. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  653. $expected = [
  654. 'form' => [
  655. 'method' => 'post',
  656. 'action' => '/Articles/publish/1',
  657. 'accept-charset' => $encoding,
  658. ],
  659. 'div' => ['style' => 'display:none;'],
  660. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  661. '/div',
  662. ];
  663. $this->assertHtml($expected, $result);
  664. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  665. $expected = [
  666. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  667. 'div' => ['style' => 'display:none;'],
  668. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  669. '/div',
  670. ];
  671. $this->assertHtml($expected, $result);
  672. $this->View->setRequest($this->View->getRequest()
  673. ->withParam('controller', 'Pages'));
  674. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  675. $expected = [
  676. 'form' => [
  677. 'method' => 'post', 'action' => '/Pages/signup/1',
  678. 'accept-charset' => $encoding,
  679. ],
  680. 'div' => ['style' => 'display:none;'],
  681. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  682. '/div',
  683. ];
  684. $this->assertHtml($expected, $result);
  685. }
  686. /**
  687. * Test create() with no URL (no "action" attribute for <form> tag)
  688. */
  689. public function testCreateNoUrl(): void
  690. {
  691. $result = $this->Form->create(null, ['url' => false]);
  692. $expected = [
  693. 'form' => [
  694. 'method' => 'post',
  695. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  696. ],
  697. ];
  698. $this->assertHtml($expected, $result);
  699. }
  700. /**
  701. * test create() with a custom route
  702. */
  703. public function testCreateCustomRoute(): void
  704. {
  705. $builder = Router::createRouteBuilder('/');
  706. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  707. $encoding = strtolower(Configure::read('App.encoding'));
  708. $this->View->setRequest($this->View->getRequest()
  709. ->withParam('controller', 'Users'));
  710. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post', 'action' => '/login',
  714. 'accept-charset' => $encoding,
  715. ],
  716. ];
  717. $this->assertHtml($expected, $result);
  718. $builder->connect(
  719. '/new-article',
  720. ['controller' => 'Articles', 'action' => 'myAction'],
  721. ['_name' => 'my-route']
  722. );
  723. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post', 'action' => '/new-article',
  727. 'accept-charset' => $encoding,
  728. ],
  729. ];
  730. $this->assertHtml($expected, $result);
  731. }
  732. /**
  733. * test automatic accept-charset overriding
  734. */
  735. public function testCreateWithAcceptCharset(): void
  736. {
  737. $result = $this->Form->create(
  738. $this->article,
  739. [
  740. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  741. ]
  742. );
  743. $expected = [
  744. 'form' => [
  745. 'method' => 'post', 'action' => '/Articles',
  746. 'accept-charset' => 'iso-8859-1',
  747. ],
  748. ];
  749. $this->assertHtml($expected, $result);
  750. }
  751. /**
  752. * Test base form URL when 'url' param is passed with multiple parameters (&)
  753. */
  754. public function testCreateQueryStringRequest(): void
  755. {
  756. $encoding = strtolower(Configure::read('App.encoding'));
  757. $result = $this->Form->create($this->article, [
  758. 'type' => 'post',
  759. 'escape' => false,
  760. 'url' => [
  761. 'controller' => 'Controller',
  762. 'action' => 'action',
  763. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  764. ],
  765. ]);
  766. $expected = [
  767. 'form' => [
  768. 'method' => 'post',
  769. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  770. 'accept-charset' => $encoding,
  771. ],
  772. ];
  773. $this->assertHtml($expected, $result);
  774. $result = $this->Form->create($this->article, [
  775. 'type' => 'post',
  776. 'url' => [
  777. 'controller' => 'Controller',
  778. 'action' => 'action',
  779. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  780. ],
  781. ]);
  782. $this->assertHtml($expected, $result);
  783. }
  784. /**
  785. * test that create() doesn't cause errors by multiple id's being in the primary key
  786. * as could happen with multiple select or checkboxes.
  787. */
  788. public function testCreateWithMultipleIdInData(): void
  789. {
  790. $encoding = strtolower(Configure::read('App.encoding'));
  791. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  792. $result = $this->Form->create($this->article);
  793. $expected = [
  794. 'form' => [
  795. 'method' => 'post',
  796. 'action' => '/articles/add',
  797. 'accept-charset' => $encoding,
  798. ],
  799. ];
  800. $this->assertHtml($expected, $result);
  801. }
  802. /**
  803. * test that create() doesn't add in extra passed params.
  804. */
  805. public function testCreatePassedArgs(): void
  806. {
  807. $encoding = strtolower(Configure::read('App.encoding'));
  808. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  809. $result = $this->Form->create($this->article, [
  810. 'type' => 'post',
  811. 'escape' => false,
  812. 'url' => [
  813. 'action' => 'edit',
  814. 'myparam',
  815. ],
  816. ]);
  817. $expected = [
  818. 'form' => [
  819. 'method' => 'post',
  820. 'action' => '/Articles/edit/myparam',
  821. 'accept-charset' => $encoding,
  822. ],
  823. ];
  824. $this->assertHtml($expected, $result);
  825. }
  826. /**
  827. * test creating a get form, and get form inputs.
  828. */
  829. public function testGetFormCreate(): void
  830. {
  831. $encoding = strtolower(Configure::read('App.encoding'));
  832. $result = $this->Form->create($this->article, ['type' => 'get']);
  833. $expected = ['form' => [
  834. 'method' => 'get', 'action' => '/articles/add',
  835. 'accept-charset' => $encoding,
  836. ]];
  837. $this->assertHtml($expected, $result);
  838. $result = $this->Form->text('title');
  839. $expected = ['input' => [
  840. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  841. ]];
  842. $this->assertHtml($expected, $result);
  843. $result = $this->Form->password('password');
  844. $expected = ['input' => [
  845. 'name' => 'password', 'type' => 'password',
  846. ]];
  847. $this->assertHtml($expected, $result);
  848. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  849. $result = $this->Form->text('user_form');
  850. $expected = ['input' => [
  851. 'name' => 'user_form', 'type' => 'text',
  852. ]];
  853. $this->assertHtml($expected, $result);
  854. }
  855. /**
  856. * test get form, and inputs when the model param is false
  857. */
  858. public function testGetFormWithFalseModel(): void
  859. {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  862. $result = $this->Form->create(null, [
  863. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  864. ]);
  865. $expected = ['form' => [
  866. 'method' => 'get', 'action' => '/ContactTest/add',
  867. 'accept-charset' => $encoding,
  868. ]];
  869. $this->assertHtml($expected, $result);
  870. $result = $this->Form->text('reason');
  871. $expected = [
  872. 'input' => ['type' => 'text', 'name' => 'reason'],
  873. ];
  874. $this->assertHtml($expected, $result);
  875. }
  876. /**
  877. * testFormCreateWithSecurity method
  878. *
  879. * Test form->create() with security key.
  880. */
  881. public function testCreateWithSecurity(): void
  882. {
  883. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  884. $encoding = strtolower(Configure::read('App.encoding'));
  885. $result = $this->Form->create($this->article, [
  886. 'url' => '/articles/publish',
  887. ]);
  888. $expected = [
  889. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  890. 'div' => ['style' => 'display:none;'],
  891. ['input' => [
  892. 'type' => 'hidden',
  893. 'name' => '_csrfToken',
  894. 'value' => 'testKey',
  895. ]],
  896. '/div',
  897. ];
  898. $this->assertHtml($expected, $result);
  899. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  900. $expected['form']['id'] = 'MyForm';
  901. $this->assertHtml($expected, $result);
  902. }
  903. /**
  904. * testFormCreateGetNoSecurity method
  905. *
  906. * Test form->create() with no security key as its a get form
  907. */
  908. public function testCreateEndGetNoSecurity(): void
  909. {
  910. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  911. $article = new Article();
  912. $result = $this->Form->create($article, [
  913. 'type' => 'get',
  914. 'url' => '/contacts/add',
  915. ]);
  916. $this->assertStringNotContainsString('testKey', $result);
  917. $result = $this->Form->end();
  918. $this->assertStringNotContainsString('testKey', $result);
  919. }
  920. /**
  921. * Tests form hash generation with model-less data
  922. */
  923. public function testValidateHashNoModel(): void
  924. {
  925. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  926. $fields = ['anything'];
  927. $this->Form->create();
  928. $result = $this->Form->secure($fields);
  929. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  930. $this->assertStringContainsString($hash, $result);
  931. }
  932. /**
  933. * Tests that hidden fields generated for checkboxes don't get locked
  934. */
  935. public function testNoCheckboxLocking(): void
  936. {
  937. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  938. $this->Form->create();
  939. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  940. $this->Form->checkbox('check', ['value' => '1']);
  941. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  942. }
  943. /**
  944. * testFormSecurityFields method
  945. *
  946. * Test generation of secure form hash generation.
  947. */
  948. public function testFormSecurityFields(): void
  949. {
  950. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  951. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  952. $this->Form->create();
  953. $result = $this->Form->secure($fields);
  954. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  955. $hash .= ':' . 'Model.valid';
  956. $hash = urlencode($hash);
  957. $tokenDebug = urlencode(json_encode([
  958. $this->url,
  959. $fields,
  960. [],
  961. ]));
  962. $expected = [
  963. 'div' => ['style' => 'display:none;'],
  964. ['input' => [
  965. 'type' => 'hidden',
  966. 'name' => '_Token[fields]',
  967. 'value' => $hash,
  968. ]],
  969. ['input' => [
  970. 'type' => 'hidden',
  971. 'name' => '_Token[unlocked]',
  972. 'value' => '',
  973. ]],
  974. ['input' => [
  975. 'type' => 'hidden',
  976. 'name' => '_Token[debug]',
  977. 'value' => $tokenDebug,
  978. ]],
  979. '/div',
  980. ];
  981. $this->assertHtml($expected, $result);
  982. }
  983. /**
  984. * testFormSecurityFields method
  985. *
  986. * Test debug token is not generated if debug is false
  987. */
  988. public function testFormSecurityFieldsNoDebugMode(): void
  989. {
  990. Configure::write('debug', false);
  991. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  992. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  993. $this->Form->create();
  994. $result = $this->Form->secure($fields);
  995. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  996. $hash .= ':' . 'Model.valid';
  997. $hash = urlencode($hash);
  998. $expected = [
  999. 'div' => ['style' => 'display:none;'],
  1000. ['input' => [
  1001. 'type' => 'hidden',
  1002. 'name' => '_Token[fields]',
  1003. 'value' => $hash,
  1004. ]],
  1005. ['input' => [
  1006. 'type' => 'hidden',
  1007. 'name' => '_Token[unlocked]',
  1008. 'value' => '',
  1009. ]],
  1010. '/div',
  1011. ];
  1012. $this->assertHtml($expected, $result);
  1013. }
  1014. /**
  1015. * Tests correct generation of number fields for smallint
  1016. */
  1017. public function testTextFieldGenerationForSmallint(): void
  1018. {
  1019. $this->article['schema'] = [
  1020. 'foo' => [
  1021. 'type' => 'smallinteger',
  1022. 'null' => false,
  1023. 'default' => null,
  1024. 'length' => 10,
  1025. ],
  1026. ];
  1027. $this->Form->create($this->article);
  1028. $result = $this->Form->control('foo');
  1029. $this->assertStringContainsString('class="input number"', $result);
  1030. $this->assertStringContainsString('type="number"', $result);
  1031. }
  1032. /**
  1033. * Tests correct generation of number fields for tinyint
  1034. */
  1035. public function testTextFieldGenerationForTinyint(): void
  1036. {
  1037. $this->article['schema'] = [
  1038. 'foo' => [
  1039. 'type' => 'tinyinteger',
  1040. 'null' => false,
  1041. 'default' => null,
  1042. 'length' => 10,
  1043. ],
  1044. ];
  1045. $this->Form->create($this->article);
  1046. $result = $this->Form->control('foo');
  1047. $this->assertStringContainsString('class="input number"', $result);
  1048. $this->assertStringContainsString('type="number"', $result);
  1049. }
  1050. /**
  1051. * Tests correct generation of number fields for double and float fields
  1052. */
  1053. public function testTextFieldGenerationForFloats(): void
  1054. {
  1055. $this->article['schema'] = [
  1056. 'foo' => [
  1057. 'type' => 'float',
  1058. 'null' => false,
  1059. 'default' => null,
  1060. 'length' => 10,
  1061. ],
  1062. ];
  1063. $this->Form->create($this->article);
  1064. $result = $this->Form->control('foo');
  1065. $expected = [
  1066. 'div' => ['class' => 'input number'],
  1067. 'label' => ['for' => 'foo'],
  1068. 'Foo',
  1069. '/label',
  1070. ['input' => [
  1071. 'type' => 'number',
  1072. 'name' => 'foo',
  1073. 'id' => 'foo',
  1074. 'step' => 'any',
  1075. ]],
  1076. '/div',
  1077. ];
  1078. $this->assertHtml($expected, $result);
  1079. $result = $this->Form->control('foo', ['step' => 0.5]);
  1080. $expected = [
  1081. 'div' => ['class' => 'input number'],
  1082. 'label' => ['for' => 'foo'],
  1083. 'Foo',
  1084. '/label',
  1085. ['input' => [
  1086. 'type' => 'number',
  1087. 'name' => 'foo',
  1088. 'id' => 'foo',
  1089. 'step' => '0.5',
  1090. ]],
  1091. '/div',
  1092. ];
  1093. $this->assertHtml($expected, $result);
  1094. }
  1095. /**
  1096. * Tests correct generation of number fields for integer fields
  1097. */
  1098. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1099. {
  1100. $this->getTableLocator()->get('Contacts', [
  1101. 'className' => ContactsTable::class,
  1102. ]);
  1103. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1104. $result = $this->Form->control('age');
  1105. $expected = [
  1106. 'div' => ['class' => 'input number'],
  1107. 'label' => ['for' => 'age'],
  1108. 'Age',
  1109. '/label',
  1110. ['input' => [
  1111. 'type' => 'number', 'name' => 'age',
  1112. 'id' => 'age',
  1113. ]],
  1114. '/div',
  1115. ];
  1116. $this->assertHtml($expected, $result);
  1117. }
  1118. /**
  1119. * Tests correct generation of file upload fields for binary fields
  1120. */
  1121. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1122. {
  1123. $table = $this->getTableLocator()->get('Contacts', [
  1124. 'className' => ContactsTable::class,
  1125. ]);
  1126. $table->setSchema(['foo' => [
  1127. 'type' => 'binary',
  1128. 'null' => false,
  1129. 'default' => null,
  1130. 'length' => 1024,
  1131. ]]);
  1132. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1133. $result = $this->Form->control('foo');
  1134. $expected = [
  1135. 'div' => ['class' => 'input file'],
  1136. 'label' => ['for' => 'foo'],
  1137. 'Foo',
  1138. '/label',
  1139. ['input' => [
  1140. 'type' => 'file', 'name' => 'foo',
  1141. 'id' => 'foo',
  1142. ]],
  1143. '/div',
  1144. ];
  1145. $this->assertHtml($expected, $result);
  1146. }
  1147. /**
  1148. * testFormSecurityMultipleFields method
  1149. *
  1150. * Test secure() with multiple row form. Ensure hash is correct.
  1151. */
  1152. public function testFormSecurityMultipleFields(): void
  1153. {
  1154. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1155. $this->Form->create();
  1156. $fields = [
  1157. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1158. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1159. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1160. ];
  1161. $result = $this->Form->secure($fields);
  1162. $sortedFields = [
  1163. 'Model.0.password',
  1164. 'Model.0.username',
  1165. 'Model.1.password',
  1166. 'Model.1.username',
  1167. 'Model.0.hidden' => 'value',
  1168. 'Model.0.valid' => '0',
  1169. 'Model.1.hidden' => 'value',
  1170. 'Model.1.valid' => '0',
  1171. ];
  1172. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1173. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1174. $hash = urlencode($hash);
  1175. $tokenDebug = urlencode(json_encode([
  1176. $this->url,
  1177. $fields,
  1178. [],
  1179. ]));
  1180. $expected = [
  1181. 'div' => ['style' => 'display:none;'],
  1182. ['input' => [
  1183. 'type' => 'hidden',
  1184. 'name' => '_Token[fields]',
  1185. 'value' => $hash,
  1186. ]],
  1187. ['input' => [
  1188. 'type' => 'hidden',
  1189. 'name' => '_Token[unlocked]',
  1190. 'value' => '',
  1191. ]],
  1192. ['input' => [
  1193. 'type' => 'hidden',
  1194. 'name' => '_Token[debug]',
  1195. 'value' => $tokenDebug,
  1196. ]],
  1197. '/div',
  1198. ];
  1199. $this->assertHtml($expected, $result);
  1200. }
  1201. /**
  1202. * testFormSecurityMultipleSubmitButtons
  1203. *
  1204. * test form submit generation and ensure that _Token is only created on end()
  1205. */
  1206. public function testFormSecurityMultipleSubmitButtons(): void
  1207. {
  1208. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1209. $this->Form->create($this->article);
  1210. $this->Form->text('Address.title');
  1211. $this->Form->text('Address.first_name');
  1212. $result = $this->Form->submit('Save', ['name' => 'save']);
  1213. $expected = [
  1214. 'div' => ['class' => 'submit'],
  1215. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1216. '/div',
  1217. ];
  1218. $this->assertHtml($expected, $result);
  1219. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1220. $expected = [
  1221. 'div' => ['class' => 'submit'],
  1222. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1223. '/div',
  1224. ];
  1225. $this->assertHtml($expected, $result);
  1226. $result = $this->Form->end();
  1227. $tokenDebug = urlencode(json_encode([
  1228. '/articles/add',
  1229. [
  1230. 'Address.title',
  1231. 'Address.first_name',
  1232. ],
  1233. ['save', 'cancel'],
  1234. ]));
  1235. $expected = [
  1236. 'div' => ['style' => 'display:none;'],
  1237. ['input' => [
  1238. 'type' => 'hidden',
  1239. 'name' => '_Token[fields]',
  1240. 'value',
  1241. ]],
  1242. ['input' => [
  1243. 'type' => 'hidden',
  1244. 'name' => '_Token[unlocked]',
  1245. 'value' => 'cancel%7Csave',
  1246. ]],
  1247. ['input' => [
  1248. 'type' => 'hidden',
  1249. 'name' => '_Token[debug]',
  1250. 'value' => $tokenDebug,
  1251. ]],
  1252. '/div',
  1253. ];
  1254. $this->assertHtml($expected, $result);
  1255. }
  1256. /**
  1257. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1258. */
  1259. public function testSecurityButtonNestedNamed(): void
  1260. {
  1261. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1262. $this->Form->create();
  1263. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1264. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1265. $this->assertEquals(['Address.button'], $result);
  1266. }
  1267. /**
  1268. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1269. */
  1270. public function testSecuritySubmitNestedNamed(): void
  1271. {
  1272. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1273. $this->Form->create($this->article);
  1274. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1275. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1276. $this->assertEquals(['Address.button'], $result);
  1277. }
  1278. /**
  1279. * Test that the correct fields are unlocked for image submits with no names.
  1280. */
  1281. public function testSecuritySubmitImageNoName(): void
  1282. {
  1283. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1284. $this->Form->create();
  1285. $result = $this->Form->submit('save.png');
  1286. $expected = [
  1287. 'div' => ['class' => 'submit'],
  1288. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1289. '/div',
  1290. ];
  1291. $this->assertHtml($expected, $result);
  1292. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1293. $this->assertEquals(['x', 'y'], $result);
  1294. }
  1295. /**
  1296. * Test that the correct fields are unlocked for image submits with names.
  1297. */
  1298. public function testSecuritySubmitImageName(): void
  1299. {
  1300. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1301. $this->Form->create();
  1302. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1303. $expected = [
  1304. 'div' => ['class' => 'submit'],
  1305. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1306. '/div',
  1307. ];
  1308. $this->assertHtml($expected, $result);
  1309. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1310. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1311. }
  1312. /**
  1313. * testFormSecurityMultipleControlFields method
  1314. *
  1315. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1316. */
  1317. public function testFormSecurityMultipleControlFields(): void
  1318. {
  1319. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1320. $this->Form->create();
  1321. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1322. $this->Form->control('Addresses.0.title');
  1323. $this->Form->control('Addresses.0.first_name');
  1324. $this->Form->control('Addresses.0.last_name');
  1325. $this->Form->control('Addresses.0.address');
  1326. $this->Form->control('Addresses.0.city');
  1327. $this->Form->control('Addresses.0.phone');
  1328. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1329. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1330. $this->Form->control('Addresses.1.title');
  1331. $this->Form->control('Addresses.1.first_name');
  1332. $this->Form->control('Addresses.1.last_name');
  1333. $this->Form->control('Addresses.1.address');
  1334. $this->Form->control('Addresses.1.city');
  1335. $this->Form->control('Addresses.1.phone');
  1336. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1337. $result = $this->Form->secure();
  1338. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1339. $tokenDebug = urlencode(json_encode([
  1340. '/articles/add',
  1341. [
  1342. 'Addresses.0.id' => '123456',
  1343. 'Addresses.0.title',
  1344. 'Addresses.0.first_name',
  1345. 'Addresses.0.last_name',
  1346. 'Addresses.0.address',
  1347. 'Addresses.0.city',
  1348. 'Addresses.0.phone',
  1349. 'Addresses.0.primary',
  1350. 'Addresses.1.id' => '654321',
  1351. 'Addresses.1.title',
  1352. 'Addresses.1.first_name',
  1353. 'Addresses.1.last_name',
  1354. 'Addresses.1.address',
  1355. 'Addresses.1.city',
  1356. 'Addresses.1.phone',
  1357. 'Addresses.1.primary',
  1358. ],
  1359. [],
  1360. ]));
  1361. $expected = [
  1362. 'div' => ['style' => 'display:none;'],
  1363. ['input' => [
  1364. 'type' => 'hidden',
  1365. 'name' => '_Token[fields]',
  1366. 'value' => $hash,
  1367. ]],
  1368. ['input' => [
  1369. 'type' => 'hidden',
  1370. 'name' => '_Token[unlocked]',
  1371. 'value' => '',
  1372. ]],
  1373. ['input' => [
  1374. 'type' => 'hidden',
  1375. 'name' => '_Token[debug]',
  1376. 'value' => $tokenDebug,
  1377. ]],
  1378. '/div',
  1379. ];
  1380. $this->assertHtml($expected, $result);
  1381. }
  1382. /**
  1383. * testFormSecurityArrayFields method
  1384. *
  1385. * Test form security with Model.field.0 style inputs.
  1386. */
  1387. public function testFormSecurityArrayFields(): void
  1388. {
  1389. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1390. $this->Form->create();
  1391. $this->Form->text('Address.primary.1');
  1392. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1393. $this->assertSame('Address.primary', $result[0]);
  1394. $this->Form->text('Address.secondary.1.0');
  1395. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1396. $this->assertSame('Address.secondary', $result[1]);
  1397. }
  1398. /**
  1399. * testFormSecurityMultipleControlDisabledFields method
  1400. *
  1401. * Test secure form generation with multiple records and disabled fields.
  1402. */
  1403. public function testFormSecurityMultipleControlDisabledFields(): void
  1404. {
  1405. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1406. 'unlockedFields' => ['first_name', 'address'],
  1407. ]));
  1408. $this->Form->create();
  1409. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1410. $this->Form->text('Addresses.0.title');
  1411. $this->Form->text('Addresses.0.first_name');
  1412. $this->Form->text('Addresses.0.last_name');
  1413. $this->Form->text('Addresses.0.address');
  1414. $this->Form->text('Addresses.0.city');
  1415. $this->Form->text('Addresses.0.phone');
  1416. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1417. $this->Form->text('Addresses.1.title');
  1418. $this->Form->text('Addresses.1.first_name');
  1419. $this->Form->text('Addresses.1.last_name');
  1420. $this->Form->text('Addresses.1.address');
  1421. $this->Form->text('Addresses.1.city');
  1422. $this->Form->text('Addresses.1.phone');
  1423. $result = $this->Form->secure();
  1424. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1425. $tokenDebug = urlencode(json_encode([
  1426. '/articles/add',
  1427. [
  1428. 'Addresses.0.id' => '123456',
  1429. 'Addresses.0.title',
  1430. 'Addresses.0.last_name',
  1431. 'Addresses.0.city',
  1432. 'Addresses.0.phone',
  1433. 'Addresses.1.id' => '654321',
  1434. 'Addresses.1.title',
  1435. 'Addresses.1.last_name',
  1436. 'Addresses.1.city',
  1437. 'Addresses.1.phone',
  1438. ],
  1439. [
  1440. 'first_name',
  1441. 'address',
  1442. ],
  1443. ]));
  1444. $expected = [
  1445. 'div' => ['style' => 'display:none;'],
  1446. ['input' => [
  1447. 'type' => 'hidden',
  1448. 'name' => '_Token[fields]',
  1449. 'value' => $hash,
  1450. ]],
  1451. ['input' => [
  1452. 'type' => 'hidden',
  1453. 'name' => '_Token[unlocked]',
  1454. 'value' => 'address%7Cfirst_name',
  1455. ]],
  1456. ['input' => [
  1457. 'type' => 'hidden',
  1458. 'name' => '_Token[debug]',
  1459. 'value' => $tokenDebug,
  1460. ]],
  1461. '/div',
  1462. ];
  1463. $this->assertHtml($expected, $result);
  1464. }
  1465. /**
  1466. * testFormSecurityControlDisabledFields method
  1467. *
  1468. * Test single record form with disabled fields.
  1469. */
  1470. public function testFormSecurityControlUnlockedFields(): void
  1471. {
  1472. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1473. 'unlockedFields' => ['first_name', 'address'],
  1474. ]));
  1475. $this->Form->create();
  1476. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1477. $this->assertEquals(
  1478. $this->View->getRequest()->getAttribute('formTokenData'),
  1479. ['unlockedFields' => $result]
  1480. );
  1481. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1482. $this->Form->text('Addresses.title');
  1483. $this->Form->text('Addresses.first_name');
  1484. $this->Form->text('Addresses.last_name');
  1485. $this->Form->text('Addresses.address');
  1486. $this->Form->text('Addresses.city');
  1487. $this->Form->text('Addresses.phone');
  1488. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1489. $expected = [
  1490. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1491. 'Addresses.city', 'Addresses.phone',
  1492. ];
  1493. $this->assertEquals($expected, $result);
  1494. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1495. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1496. $tokenDebug = urlencode(json_encode([
  1497. '/articles/add',
  1498. [
  1499. 'Addresses.id' => '123456',
  1500. 'Addresses.title',
  1501. 'Addresses.last_name',
  1502. 'Addresses.city',
  1503. 'Addresses.phone',
  1504. ],
  1505. [
  1506. 'first_name',
  1507. 'address',
  1508. ],
  1509. ]));
  1510. $expected = [
  1511. 'div' => ['style' => 'display:none;'],
  1512. ['input' => [
  1513. 'type' => 'hidden',
  1514. 'name' => '_Token[fields]',
  1515. 'value' => $hash,
  1516. 'data-foo' => 'bar',
  1517. ]],
  1518. ['input' => [
  1519. 'type' => 'hidden',
  1520. 'name' => '_Token[unlocked]',
  1521. 'value' => 'address%7Cfirst_name',
  1522. 'data-foo' => 'bar',
  1523. ]],
  1524. ['input' => [
  1525. 'type' => 'hidden', 'name' => '_Token[debug]',
  1526. 'value' => $tokenDebug,
  1527. 'data-foo' => 'bar',
  1528. ]],
  1529. '/div',
  1530. ];
  1531. $this->assertHtml($expected, $result);
  1532. }
  1533. /**
  1534. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1535. *
  1536. * Test single record form with debugSecurity param.
  1537. */
  1538. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1539. {
  1540. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1541. 'unlockedFields' => ['first_name', 'address'],
  1542. ]));
  1543. $this->Form->create();
  1544. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1545. $this->assertEquals(
  1546. $this->View->getRequest()->getAttribute('formTokenData'),
  1547. ['unlockedFields' => $result]
  1548. );
  1549. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1550. $this->Form->text('Addresses.title');
  1551. $this->Form->text('Addresses.first_name');
  1552. $this->Form->text('Addresses.last_name');
  1553. $this->Form->text('Addresses.address');
  1554. $this->Form->text('Addresses.city');
  1555. $this->Form->text('Addresses.phone');
  1556. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1557. $expected = [
  1558. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1559. 'Addresses.city', 'Addresses.phone',
  1560. ];
  1561. $this->assertEquals($expected, $result);
  1562. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1563. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1564. $tokenDebug = urlencode(json_encode([
  1565. '/articles/add',
  1566. [
  1567. 'Addresses.id' => '123456',
  1568. 'Addresses.title',
  1569. 'Addresses.last_name',
  1570. 'Addresses.city',
  1571. 'Addresses.phone',
  1572. ],
  1573. [
  1574. 'first_name',
  1575. 'address',
  1576. ],
  1577. ]));
  1578. $expected = [
  1579. 'div' => ['style' => 'display:none;'],
  1580. ['input' => [
  1581. 'type' => 'hidden',
  1582. 'name' => '_Token[fields]',
  1583. 'value' => $hash,
  1584. 'data-foo' => 'bar',
  1585. ]],
  1586. ['input' => [
  1587. 'type' => 'hidden',
  1588. 'name' => '_Token[unlocked]',
  1589. 'value' => 'address%7Cfirst_name',
  1590. 'data-foo' => 'bar',
  1591. ]],
  1592. ['input' => [
  1593. 'type' => 'hidden', 'name' => '_Token[debug]',
  1594. 'value' => $tokenDebug,
  1595. 'data-foo' => 'bar',
  1596. ]],
  1597. '/div',
  1598. ];
  1599. $this->assertHtml($expected, $result);
  1600. }
  1601. /**
  1602. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1603. *
  1604. * Debug is false, debugSecurity is true -> no debug
  1605. */
  1606. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1607. {
  1608. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1609. 'unlockedFields' => ['first_name', 'address'],
  1610. ]));
  1611. $this->Form->create();
  1612. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1613. $this->assertEquals(
  1614. $this->View->getRequest()->getAttribute('formTokenData'),
  1615. ['unlockedFields' => $result]
  1616. );
  1617. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1618. $this->Form->text('Addresses.title');
  1619. $this->Form->text('Addresses.first_name');
  1620. $this->Form->text('Addresses.last_name');
  1621. $this->Form->text('Addresses.address');
  1622. $this->Form->text('Addresses.city');
  1623. $this->Form->text('Addresses.phone');
  1624. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1625. $expected = [
  1626. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1627. 'Addresses.city', 'Addresses.phone',
  1628. ];
  1629. $this->assertEquals($expected, $result);
  1630. Configure::write('debug', false);
  1631. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1632. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1633. $expected = [
  1634. 'div' => ['style' => 'display:none;'],
  1635. ['input' => [
  1636. 'type' => 'hidden',
  1637. 'name' => '_Token[fields]',
  1638. 'value' => $hash,
  1639. 'data-foo' => 'bar',
  1640. ]],
  1641. ['input' => [
  1642. 'type' => 'hidden',
  1643. 'name' => '_Token[unlocked]',
  1644. 'value' => 'address%7Cfirst_name',
  1645. 'data-foo' => 'bar',
  1646. ]],
  1647. '/div',
  1648. ];
  1649. $this->assertHtml($expected, $result);
  1650. }
  1651. /**
  1652. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1653. *
  1654. * Test single record form with debugSecurity param.
  1655. */
  1656. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1657. {
  1658. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1659. 'unlockedFields' => ['first_name', 'address'],
  1660. ]));
  1661. $this->Form->create();
  1662. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1663. $this->assertEquals(
  1664. $this->View->getRequest()->getAttribute('formTokenData'),
  1665. ['unlockedFields' => $result]
  1666. );
  1667. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1668. $this->Form->text('Addresses.title');
  1669. $this->Form->text('Addresses.first_name');
  1670. $this->Form->text('Addresses.last_name');
  1671. $this->Form->text('Addresses.address');
  1672. $this->Form->text('Addresses.city');
  1673. $this->Form->text('Addresses.phone');
  1674. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1675. $expected = [
  1676. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1677. 'Addresses.city', 'Addresses.phone',
  1678. ];
  1679. $this->assertEquals($expected, $result);
  1680. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1681. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1682. $expected = [
  1683. 'div' => ['style' => 'display:none;'],
  1684. ['input' => [
  1685. 'type' => 'hidden',
  1686. 'name' => '_Token[fields]',
  1687. 'value' => $hash,
  1688. 'data-foo' => 'bar',
  1689. ]],
  1690. ['input' => [
  1691. 'type' => 'hidden',
  1692. 'name' => '_Token[unlocked]',
  1693. 'value' => 'address%7Cfirst_name',
  1694. 'data-foo' => 'bar',
  1695. ]],
  1696. '/div',
  1697. ];
  1698. $this->assertHtml($expected, $result);
  1699. }
  1700. /**
  1701. * testFormSecureWithCustomNameAttribute method
  1702. *
  1703. * Test securing inputs with custom name attributes.
  1704. */
  1705. public function testFormSecureWithCustomNameAttribute(): void
  1706. {
  1707. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1708. $this->Form->create();
  1709. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1710. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1711. $this->assertSame('User.custom', $result[0]);
  1712. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1713. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1714. $this->assertSame('User.custom.another.value', $result[1]);
  1715. }
  1716. /**
  1717. * testFormSecuredControl method
  1718. *
  1719. * Test generation of entire secure form, assertions made on control() output.
  1720. */
  1721. public function testFormSecuredControl(): void
  1722. {
  1723. $this->View->setRequest($this->View->getRequest()
  1724. ->withAttribute('formTokenData', [])
  1725. ->withAttribute('csrfToken', 'testKey'));
  1726. $this->article['schema'] = [
  1727. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1728. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1729. ];
  1730. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1731. $encoding = strtolower(Configure::read('App.encoding'));
  1732. $expected = [
  1733. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1734. 'div' => ['style' => 'display:none;'],
  1735. ['input' => [
  1736. 'type' => 'hidden',
  1737. 'name' => '_csrfToken',
  1738. 'value' => 'testKey',
  1739. ]],
  1740. '/div',
  1741. ];
  1742. $this->assertHtml($expected, $result);
  1743. $result = $this->Form->control('ratio');
  1744. $expected = [
  1745. 'div' => ['class'],
  1746. 'label' => ['for'],
  1747. 'Ratio',
  1748. '/label',
  1749. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1750. '/div',
  1751. ];
  1752. $this->assertHtml($expected, $result);
  1753. $result = $this->Form->control('population');
  1754. $expected = [
  1755. 'div' => ['class'],
  1756. 'label' => ['for'],
  1757. 'Population',
  1758. '/label',
  1759. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1760. '/div',
  1761. ];
  1762. $this->assertHtml($expected, $result);
  1763. $result = $this->Form->control('published', ['type' => 'text']);
  1764. $expected = [
  1765. 'div' => ['class' => 'input text'],
  1766. 'label' => ['for' => 'published'],
  1767. 'Published',
  1768. '/label',
  1769. ['input' => [
  1770. 'type' => 'text',
  1771. 'name' => 'published',
  1772. 'id' => 'published',
  1773. ]],
  1774. '/div',
  1775. ];
  1776. $this->assertHtml($expected, $result);
  1777. $result = $this->Form->control('other', ['type' => 'text']);
  1778. $expected = [
  1779. 'div' => ['class' => 'input text'],
  1780. 'label' => ['for' => 'other'],
  1781. 'Other',
  1782. '/label',
  1783. ['input' => [
  1784. 'type' => 'text',
  1785. 'name' => 'other',
  1786. 'id',
  1787. ]],
  1788. '/div',
  1789. ];
  1790. $this->assertHtml($expected, $result);
  1791. $result = $this->Form->hidden('stuff');
  1792. $expected = [
  1793. 'input' => [
  1794. 'type' => 'hidden',
  1795. 'name' => 'stuff',
  1796. ],
  1797. ];
  1798. $this->assertHtml($expected, $result);
  1799. $result = $this->Form->hidden('hidden', ['value' => false]);
  1800. $expected = ['input' => [
  1801. 'type' => 'hidden',
  1802. 'name' => 'hidden',
  1803. 'value' => '0',
  1804. ]];
  1805. $this->assertHtml($expected, $result);
  1806. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1807. $expected = [
  1808. 'div' => ['class' => 'input checkbox'],
  1809. ['input' => [
  1810. 'type' => 'hidden',
  1811. 'name' => 'something',
  1812. 'value' => '0',
  1813. ]],
  1814. 'label' => ['for' => 'something'],
  1815. ['input' => [
  1816. 'type' => 'checkbox',
  1817. 'name' => 'something',
  1818. 'value' => '1',
  1819. 'id' => 'something',
  1820. ]],
  1821. 'Something',
  1822. '/label',
  1823. '/div',
  1824. ];
  1825. $this->assertHtml($expected, $result);
  1826. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1827. $expectedFields = [
  1828. 'ratio',
  1829. 'population',
  1830. 'published',
  1831. 'other',
  1832. 'stuff' => '',
  1833. 'hidden' => '0',
  1834. 'something',
  1835. ];
  1836. $this->assertEquals($expectedFields, $result);
  1837. $result = $this->Form->secure();
  1838. $tokenDebug = urlencode(json_encode([
  1839. '/articles/add',
  1840. $expectedFields,
  1841. [],
  1842. ]));
  1843. $expected = [
  1844. 'div' => ['style' => 'display:none;'],
  1845. ['input' => [
  1846. 'type' => 'hidden',
  1847. 'name' => '_Token[fields]',
  1848. 'value',
  1849. ]],
  1850. ['input' => [
  1851. 'type' => 'hidden',
  1852. 'name' => '_Token[unlocked]',
  1853. 'value' => '',
  1854. ]],
  1855. ['input' => [
  1856. 'type' => 'hidden', 'name' => '_Token[debug]',
  1857. 'value' => $tokenDebug,
  1858. ]],
  1859. '/div',
  1860. ];
  1861. $this->assertHtml($expected, $result);
  1862. $data = [
  1863. 'ratio' => '',
  1864. 'population' => '',
  1865. 'published' => '',
  1866. 'other' => '',
  1867. 'stuff' => '',
  1868. 'hidden' => '0',
  1869. 'something' => '',
  1870. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1871. ];
  1872. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1873. }
  1874. /**
  1875. * testSecuredControlCustomName method
  1876. *
  1877. * Test secured inputs with custom names.
  1878. */
  1879. public function testSecuredControlCustomName(): void
  1880. {
  1881. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1882. $this->Form->create();
  1883. $this->Form->text('text_input', [
  1884. 'name' => 'Option[General.default_role]',
  1885. ]);
  1886. $expected = ['Option.General.default_role'];
  1887. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1888. $this->assertEquals($expected, $result);
  1889. $this->Form->select('select_box', [1, 2], [
  1890. 'name' => 'Option[General.select_role]',
  1891. ]);
  1892. $expected[] = 'Option.General.select_role';
  1893. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1894. $this->assertEquals($expected, $result);
  1895. $this->Form->text('other.things[]');
  1896. $expected[] = 'other.things';
  1897. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1898. $this->assertEquals($expected, $result);
  1899. }
  1900. /**
  1901. * testSecuredControlDuplicate method
  1902. *
  1903. * Test that a hidden field followed by a visible field
  1904. * undoes the hidden field locking.
  1905. */
  1906. public function testSecuredControlDuplicate(): void
  1907. {
  1908. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1909. $this->Form->create();
  1910. $this->Form->control('text_val', [
  1911. 'type' => 'hidden',
  1912. 'value' => 'some text',
  1913. ]);
  1914. $expected = ['text_val' => 'some text'];
  1915. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1916. $this->assertEquals($expected, $result);
  1917. $this->Form->control('text_val', [
  1918. 'type' => 'text',
  1919. ]);
  1920. $expected = ['text_val'];
  1921. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1922. $this->assertEquals($expected, $result);
  1923. }
  1924. /**
  1925. * testFormSecuredFileControl method
  1926. *
  1927. * Tests that the correct keys are added to the field hash index.
  1928. */
  1929. public function testFormSecuredFileControl(): void
  1930. {
  1931. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1932. $this->Form->create();
  1933. $this->Form->file('Attachment.file');
  1934. $expected = ['Attachment.file'];
  1935. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1936. $this->assertEquals($expected, $result);
  1937. }
  1938. /**
  1939. * testFormSecuredMultipleSelect method
  1940. *
  1941. * Test that multiple selects keys are added to field hash.
  1942. */
  1943. public function testFormSecuredMultipleSelect(): void
  1944. {
  1945. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1946. $this->Form->create();
  1947. $options = ['1' => 'one', '2' => 'two'];
  1948. $this->Form->select('Model.select', $options);
  1949. $expected = ['Model.select'];
  1950. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1951. $this->assertEquals($expected, $result);
  1952. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1953. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1954. $this->assertEquals($expected, $result);
  1955. }
  1956. /**
  1957. * testFormSecuredRadio method
  1958. */
  1959. public function testFormSecuredRadio(): void
  1960. {
  1961. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1962. $this->Form->create();
  1963. $options = ['1' => 'option1', '2' => 'option2'];
  1964. $this->Form->radio('Test.test', $options);
  1965. $expected = ['Test.test'];
  1966. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1967. $this->assertEquals($expected, $result);
  1968. $this->Form->radio('Test.all', $options, [
  1969. 'disabled' => ['option1', 'option2'],
  1970. ]);
  1971. $expected = ['Test.test', 'Test.all' => ''];
  1972. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1973. $this->assertEquals($expected, $result);
  1974. $this->Form->radio('Test.some', $options, [
  1975. 'disabled' => ['option1'],
  1976. ]);
  1977. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1978. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1979. $this->assertEquals($expected, $result);
  1980. }
  1981. /**
  1982. * testFormSecuredAndDisabled method
  1983. *
  1984. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1985. * hashing.
  1986. */
  1987. public function testFormSecuredAndDisabled(): void
  1988. {
  1989. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1990. $this->Form->create();
  1991. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1992. $this->Form->text('Model.text', ['disabled' => true]);
  1993. $this->Form->password('Model.text', ['disabled' => true]);
  1994. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1995. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1996. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1997. $this->Form->year('Model.year', ['disabled' => true]);
  1998. $this->Form->month('Model.month', ['disabled' => true]);
  1999. $this->Form->day('Model.day', ['disabled' => true]);
  2000. $this->Form->hour('Model.hour', ['disabled' => true]);
  2001. $this->Form->minute('Model.minute', ['disabled' => true]);
  2002. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2003. $expected = [
  2004. 'Model.radio' => '',
  2005. ];
  2006. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2007. $this->assertEquals($expected, $result);
  2008. }
  2009. /**
  2010. * testUnlockFieldAddsToList method
  2011. *
  2012. * Test disableField.
  2013. */
  2014. public function testUnlockFieldAddsToList(): void
  2015. {
  2016. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2017. 'unlockedFields' => [],
  2018. ]));
  2019. $this->Form->create();
  2020. $this->Form->unlockField('Contact.name');
  2021. $this->Form->text('Contact.name');
  2022. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2023. $this->assertEquals([], $result);
  2024. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2025. $this->assertEquals(['Contact.name'], $result);
  2026. }
  2027. /**
  2028. * testUnlockFieldRemovingFromFields method
  2029. *
  2030. * Test unlockField removing from fields array.
  2031. */
  2032. public function testUnlockFieldRemovingFromFields(): void
  2033. {
  2034. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2035. 'unlockedFields' => [],
  2036. ]));
  2037. $this->Form->create($this->article);
  2038. $this->Form->hidden('Article.id', ['value' => 1]);
  2039. $this->Form->text('Article.title');
  2040. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2041. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2042. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2043. $this->Form->unlockField('Article.title');
  2044. $this->Form->unlockField('Article.id');
  2045. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2046. $this->assertEquals([], $result);
  2047. }
  2048. /**
  2049. * testResetUnlockFields method
  2050. *
  2051. * Test reset unlockFields, when create new form.
  2052. */
  2053. public function testResetUnlockFields(): void
  2054. {
  2055. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2056. 'key' => 'testKey',
  2057. 'unlockedFields' => [],
  2058. ]));
  2059. $this->Form->create();
  2060. $this->Form->unlockField('Contact.id');
  2061. $this->Form->hidden('Contact.id', ['value' => 1]);
  2062. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2063. $this->assertEmpty($result, 'Field should be unlocked');
  2064. $this->Form->end();
  2065. $this->Form->create();
  2066. $this->Form->hidden('Contact.id', ['value' => 1]);
  2067. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2068. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2069. }
  2070. /**
  2071. * testSecuredFormUrlIgnoresHost method
  2072. *
  2073. * Test that only the path + query elements of a form's URL show up in their hash.
  2074. */
  2075. public function testSecuredFormUrlIgnoresHost(): void
  2076. {
  2077. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2078. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2079. $this->Form->create($this->article, [
  2080. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2081. ]);
  2082. $result = $this->Form->secure();
  2083. $this->assertStringContainsString($expected, $result);
  2084. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2085. $result = $this->Form->secure();
  2086. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2087. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2088. $result = $this->Form->secure();
  2089. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2090. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2091. $result = $this->Form->secure();
  2092. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2093. }
  2094. /**
  2095. * testSecuredFormUrlHasHtmlAndIdentifier method
  2096. *
  2097. * Test that URL, HTML and identifier show up in their hashes.
  2098. */
  2099. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2100. {
  2101. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2102. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2103. $this->Form->create($this->article, [
  2104. 'url' => [
  2105. 'controller' => 'articles',
  2106. 'action' => 'view',
  2107. '?' => [
  2108. 'page' => 1,
  2109. 'limit' => 10,
  2110. 'html' => '<>"',
  2111. ],
  2112. '#' => 'result',
  2113. ],
  2114. ]);
  2115. $result = $this->Form->secure();
  2116. $this->assertStringContainsString($expected, $result);
  2117. $this->Form->create($this->article, [
  2118. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2119. ]);
  2120. $result = $this->Form->secure();
  2121. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2122. $this->Form->create($this->article, [
  2123. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2124. ]);
  2125. $result = $this->Form->secure();
  2126. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2127. }
  2128. /**
  2129. * testErrorMessageDisplay method
  2130. *
  2131. * Test error message display.
  2132. */
  2133. public function testErrorMessageDisplay(): void
  2134. {
  2135. $this->article['errors'] = [
  2136. 'Article' => [
  2137. 'title' => 'error message',
  2138. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2139. ],
  2140. ];
  2141. $this->Form->create($this->article);
  2142. $result = $this->Form->control('Article.title');
  2143. $expected = [
  2144. 'div' => ['class' => 'input text error'],
  2145. 'label' => ['for' => 'article-title'],
  2146. 'Title',
  2147. '/label',
  2148. 'input' => [
  2149. 'type' => 'text',
  2150. 'name' => 'Article[title]',
  2151. 'id' => 'article-title',
  2152. 'class' => 'form-error',
  2153. 'aria-invalid' => 'true',
  2154. 'aria-describedby' => 'article-title-error',
  2155. ],
  2156. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2157. 'error message',
  2158. '/div',
  2159. '/div',
  2160. ];
  2161. $this->assertHtml($expected, $result);
  2162. $result = $this->Form->control('Article.title', [
  2163. 'templates' => [
  2164. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2165. ],
  2166. ]);
  2167. $expected = [
  2168. 'div' => ['class' => 'input text error'],
  2169. 'label' => ['for' => 'article-title'],
  2170. 'Title',
  2171. '/label',
  2172. 'input' => [
  2173. 'type' => 'text',
  2174. 'name' => 'Article[title]',
  2175. 'id' => 'article-title',
  2176. 'class' => 'form-error',
  2177. // No aria-describedby because error template is custom
  2178. 'aria-invalid' => 'true',
  2179. ],
  2180. '/div',
  2181. ];
  2182. $this->assertHtml($expected, $result);
  2183. $result = $this->Form->control('Article.content');
  2184. $expected = [
  2185. 'div' => ['class' => 'input text error'],
  2186. 'label' => ['for' => 'article-content'],
  2187. 'Content',
  2188. '/label',
  2189. 'input' => [
  2190. 'type' => 'text',
  2191. 'name' => 'Article[content]',
  2192. 'id' => 'article-content',
  2193. 'class' => 'form-error',
  2194. 'aria-invalid' => 'true',
  2195. 'aria-describedby' => 'article-content-error',
  2196. ],
  2197. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2198. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2199. '/div',
  2200. '/div',
  2201. ];
  2202. $this->assertHtml($expected, $result);
  2203. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2204. $expected = [
  2205. 'div' => ['class' => 'input text error'],
  2206. 'label' => ['for' => 'article-content'],
  2207. 'Content',
  2208. '/label',
  2209. 'input' => [
  2210. 'type' => 'text',
  2211. 'name' => 'Article[content]',
  2212. 'id' => 'article-content',
  2213. 'class' => 'form-error',
  2214. 'aria-invalid' => 'true',
  2215. 'aria-describedby' => 'article-content-error',
  2216. ],
  2217. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2218. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2219. '/div',
  2220. '/div',
  2221. ];
  2222. $this->assertHtml($expected, $result);
  2223. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2224. $expected = [
  2225. 'div' => ['class' => 'input text error'],
  2226. 'label' => ['for' => 'article-content'],
  2227. 'Content',
  2228. '/label',
  2229. 'input' => [
  2230. 'type' => 'text',
  2231. 'name' => 'Article[content]',
  2232. 'id' => 'article-content',
  2233. 'class' => 'form-error',
  2234. 'aria-invalid' => 'true',
  2235. 'aria-describedby' => 'article-content-error',
  2236. ],
  2237. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2238. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2239. '/div',
  2240. '/div',
  2241. ];
  2242. $this->assertHtml($expected, $result);
  2243. }
  2244. /**
  2245. * testEmptyErrorValidation method
  2246. *
  2247. * Test validation errors, when validation message is an empty string.
  2248. */
  2249. public function testEmptyErrorValidation(): void
  2250. {
  2251. $this->article['errors'] = [
  2252. 'Article' => ['title' => ''],
  2253. ];
  2254. $this->Form->create($this->article);
  2255. $result = $this->Form->control('Article.title');
  2256. $expected = [
  2257. 'div' => ['class' => 'input text error'],
  2258. 'label' => ['for' => 'article-title'],
  2259. 'Title',
  2260. '/label',
  2261. 'input' => [
  2262. 'type' => 'text',
  2263. 'name' => 'Article[title]',
  2264. 'id' => 'article-title',
  2265. 'class' => 'form-error',
  2266. 'aria-invalid' => 'true',
  2267. 'aria-describedby' => 'article-title-error',
  2268. ],
  2269. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2270. [],
  2271. '/div',
  2272. '/div',
  2273. ];
  2274. $this->assertHtml($expected, $result);
  2275. }
  2276. /**
  2277. * testEmptyControlErrorValidation method
  2278. *
  2279. * Test validation errors, when calling control() overriding validation message by an empty string.
  2280. */
  2281. public function testEmptyControlErrorValidation(): void
  2282. {
  2283. $this->article['errors'] = [
  2284. 'Article' => ['title' => 'error message'],
  2285. ];
  2286. $this->Form->create($this->article);
  2287. $result = $this->Form->control('Article.title', ['error' => '']);
  2288. $expected = [
  2289. 'div' => ['class' => 'input text error'],
  2290. 'label' => ['for' => 'article-title'],
  2291. 'Title',
  2292. '/label',
  2293. 'input' => [
  2294. 'aria-invalid' => 'true',
  2295. 'aria-describedby' => 'article-title-error',
  2296. 'type' => 'text',
  2297. 'name' => 'Article[title]',
  2298. 'id' => 'article-title',
  2299. 'class' => 'form-error',
  2300. ],
  2301. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2302. [],
  2303. '/div',
  2304. '/div',
  2305. ];
  2306. $this->assertHtml($expected, $result);
  2307. }
  2308. /**
  2309. * testControlErrorMessage method
  2310. *
  2311. * Test validation errors, when calling control() overriding validation messages.
  2312. */
  2313. public function testControlErrorMessage(): void
  2314. {
  2315. $this->article['errors'] = [
  2316. 'title' => ['error message'],
  2317. ];
  2318. $this->Form->create($this->article);
  2319. $result = $this->Form->control('title', [
  2320. 'error' => 'Custom error!',
  2321. ]);
  2322. $expected = [
  2323. 'div' => ['class' => 'input text required error'],
  2324. 'label' => ['for' => 'title'],
  2325. 'Title',
  2326. '/label',
  2327. 'input' => [
  2328. 'type' => 'text',
  2329. 'name' => 'title',
  2330. 'id' => 'title',
  2331. 'class' => 'form-error',
  2332. 'required' => 'required',
  2333. 'data-validity-message' => 'This field cannot be left empty',
  2334. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2335. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2336. 'aria-required' => 'true',
  2337. 'aria-invalid' => 'true',
  2338. 'aria-describedby' => 'title-error',
  2339. ],
  2340. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2341. 'Custom error!',
  2342. '/div',
  2343. '/div',
  2344. ];
  2345. $this->assertHtml($expected, $result);
  2346. $result = $this->Form->control('title', [
  2347. 'error' => ['error message' => 'Custom error!'],
  2348. ]);
  2349. $expected = [
  2350. 'div' => ['class' => 'input text required error'],
  2351. 'label' => ['for' => 'title'],
  2352. 'Title',
  2353. '/label',
  2354. 'input' => [
  2355. 'type' => 'text',
  2356. 'name' => 'title',
  2357. 'id' => 'title',
  2358. 'aria-required' => 'true',
  2359. 'aria-invalid' => 'true',
  2360. 'aria-describedby' => 'title-error',
  2361. 'class' => 'form-error',
  2362. 'required' => 'required',
  2363. 'data-validity-message' => 'This field cannot be left empty',
  2364. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2365. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2366. ],
  2367. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2368. 'Custom error!',
  2369. '/div',
  2370. '/div',
  2371. ];
  2372. $this->assertHtml($expected, $result);
  2373. }
  2374. /**
  2375. * testFormValidationAssociated method
  2376. *
  2377. * Tests displaying errors for nested entities.
  2378. */
  2379. public function testFormValidationAssociated(): void
  2380. {
  2381. $nested = new Entity(['foo' => 'bar']);
  2382. $nested->setError('foo', ['not a valid bar']);
  2383. $entity = new Entity(['nested' => $nested]);
  2384. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2385. $result = $this->Form->error('nested.foo');
  2386. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2387. }
  2388. /**
  2389. * testFormValidationAssociatedSecondLevel method
  2390. *
  2391. * Test form error display with associated model.
  2392. */
  2393. public function testFormValidationAssociatedSecondLevel(): void
  2394. {
  2395. $inner = new Entity(['bar' => 'baz']);
  2396. $nested = new Entity(['foo' => $inner]);
  2397. $entity = new Entity(['nested' => $nested]);
  2398. $inner->setError('bar', ['not a valid one']);
  2399. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2400. $result = $this->Form->error('nested.foo.bar');
  2401. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2402. }
  2403. /**
  2404. * testFormValidationMultiRecord method
  2405. *
  2406. * Test form error display with multiple records.
  2407. */
  2408. public function testFormValidationMultiRecord(): void
  2409. {
  2410. $one = new Entity();
  2411. $two = new Entity();
  2412. $this->getTableLocator()->get('Contacts', [
  2413. 'className' => ContactsTable::class,
  2414. ]);
  2415. $one->set('email', '');
  2416. $one->setError('email', ['invalid email']);
  2417. $two->set('name', '');
  2418. $two->setError('name', ['This is wrong']);
  2419. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2420. $result = $this->Form->control('0.email');
  2421. $expected = [
  2422. 'div' => ['class' => 'input email error'],
  2423. 'label' => ['for' => '0-email'],
  2424. 'Email',
  2425. '/label',
  2426. 'input' => [
  2427. 'type' => 'email',
  2428. 'name' => '0[email]',
  2429. 'id' => '0-email',
  2430. 'class' => 'form-error',
  2431. 'maxlength' => 255,
  2432. 'value' => '',
  2433. 'aria-invalid' => 'true',
  2434. 'aria-describedby' => '0-email-error',
  2435. ],
  2436. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2437. 'invalid email',
  2438. '/div',
  2439. '/div',
  2440. ];
  2441. $this->assertHtml($expected, $result);
  2442. $result = $this->Form->control('1.name');
  2443. $expected = [
  2444. 'div' => ['class' => 'input text error'],
  2445. 'label' => ['for' => '1-name'],
  2446. 'Name',
  2447. '/label',
  2448. 'input' => [
  2449. 'type' => 'text',
  2450. 'name' => '1[name]',
  2451. 'id' => '1-name',
  2452. 'class' => 'form-error',
  2453. 'maxlength' => 255,
  2454. 'value' => '',
  2455. 'aria-invalid' => 'true',
  2456. 'aria-describedby' => '1-name-error',
  2457. ],
  2458. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2459. 'This is wrong',
  2460. '/div',
  2461. '/div',
  2462. ];
  2463. $this->assertHtml($expected, $result);
  2464. }
  2465. /**
  2466. * testControl method
  2467. *
  2468. * Test various incarnations of control().
  2469. */
  2470. public function testControl(): void
  2471. {
  2472. $this->getTableLocator()->get('ValidateUsers', [
  2473. 'className' => ValidateUsersTable::class,
  2474. ]);
  2475. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2476. $result = $this->Form->control('ValidateUsers.balance');
  2477. $expected = [
  2478. 'div' => ['class'],
  2479. 'label' => ['for'],
  2480. 'Balance',
  2481. '/label',
  2482. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2483. '/div',
  2484. ];
  2485. $this->assertHtml($expected, $result);
  2486. $result = $this->Form->control('ValidateUser.cost_decimal');
  2487. $expected = [
  2488. 'div' => ['class'],
  2489. 'label' => ['for'],
  2490. 'Cost Decimal',
  2491. '/label',
  2492. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2493. '/div',
  2494. ];
  2495. $this->assertHtml($expected, $result);
  2496. $result = $this->Form->control('ValidateUser.null_decimal');
  2497. $expected = [
  2498. 'div' => ['class'],
  2499. 'label' => ['for'],
  2500. 'Null Decimal',
  2501. '/label',
  2502. 'input' => ['name', 'type' => 'number', 'id'],
  2503. '/div',
  2504. ];
  2505. $this->assertHtml($expected, $result);
  2506. }
  2507. /**
  2508. * testControlCustomization method
  2509. *
  2510. * Tests the input method and passing custom options.
  2511. */
  2512. public function testControlCustomization(): void
  2513. {
  2514. $this->getTableLocator()->get('Contacts', [
  2515. 'className' => ContactsTable::class,
  2516. ]);
  2517. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2518. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2519. $expected = [
  2520. 'div' => ['class' => 'input email'],
  2521. 'label' => ['for' => 'custom'],
  2522. 'Email',
  2523. '/label',
  2524. ['input' => [
  2525. 'type' => 'email', 'name' => 'Contact[email]',
  2526. 'id' => 'custom', 'maxlength' => 255,
  2527. ]],
  2528. '/div',
  2529. ];
  2530. $this->assertHtml($expected, $result);
  2531. $result = $this->Form->control('Contact.email', [
  2532. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2533. ]);
  2534. $expected = [
  2535. '<div',
  2536. 'label' => ['for' => 'contact-email'],
  2537. 'Email',
  2538. '/label',
  2539. ['input' => [
  2540. 'type' => 'email', 'name' => 'Contact[email]',
  2541. 'id' => 'contact-email', 'maxlength' => 255,
  2542. ]],
  2543. '/div',
  2544. ];
  2545. $this->assertHtml($expected, $result);
  2546. $result = $this->Form->control('Contact.email', [
  2547. 'templates' => [
  2548. 'formGroup' => '{{input}}',
  2549. 'inputContainer' => '<div><div>{{label}}</div>{{content}}</div>',
  2550. ],
  2551. ]);
  2552. $expected = [
  2553. '<div',
  2554. '<div',
  2555. 'label' => ['for' => 'contact-email'],
  2556. 'Email',
  2557. '/label',
  2558. '/div',
  2559. ['input' => [
  2560. 'type' => 'email', 'name' => 'Contact[email]',
  2561. 'id' => 'contact-email', 'maxlength' => 255,
  2562. ]],
  2563. '/div',
  2564. ];
  2565. $this->assertHtml($expected, $result);
  2566. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2567. $expected = [
  2568. 'div' => ['class' => 'input text'],
  2569. 'label' => ['for' => 'contact-email'],
  2570. 'Email',
  2571. '/label',
  2572. ['input' => [
  2573. 'type' => 'text', 'name' => 'Contact[email]',
  2574. 'id' => 'contact-email', 'maxlength' => '255',
  2575. ]],
  2576. '/div',
  2577. ];
  2578. $this->assertHtml($expected, $result);
  2579. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2580. $expected = [
  2581. 'div' => ['class' => 'input text'],
  2582. 'label' => ['for' => 'contact-5-email'],
  2583. 'Email',
  2584. '/label',
  2585. ['input' => [
  2586. 'type' => 'text', 'name' => 'Contact[5][email]',
  2587. 'id' => 'contact-5-email', 'maxlength' => '255',
  2588. ]],
  2589. '/div',
  2590. ];
  2591. $this->assertHtml($expected, $result);
  2592. $result = $this->Form->control('Contact.password');
  2593. $expected = [
  2594. 'div' => ['class' => 'input password'],
  2595. 'label' => ['for' => 'contact-password'],
  2596. 'Password',
  2597. '/label',
  2598. ['input' => [
  2599. 'type' => 'password', 'name' => 'Contact[password]',
  2600. 'id' => 'contact-password',
  2601. ]],
  2602. '/div',
  2603. ];
  2604. $this->assertHtml($expected, $result);
  2605. $result = $this->Form->control('Contact.email', [
  2606. 'type' => 'file', 'class' => 'textbox',
  2607. ]);
  2608. $expected = [
  2609. 'div' => ['class' => 'input file'],
  2610. 'label' => ['for' => 'contact-email'],
  2611. 'Email',
  2612. '/label',
  2613. ['input' => [
  2614. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2615. 'id' => 'contact-email',
  2616. ]],
  2617. '/div',
  2618. ];
  2619. $this->assertHtml($expected, $result);
  2620. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2621. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2622. $result = $this->Form->control('phone');
  2623. $expected = [
  2624. 'div' => ['class' => 'input tel'],
  2625. 'label' => ['for' => 'phone'],
  2626. 'Phone',
  2627. '/label',
  2628. ['input' => [
  2629. 'type' => 'tel', 'name' => 'phone',
  2630. 'value' => 'Hello &amp; World &gt; weird chars',
  2631. 'id' => 'phone', 'maxlength' => 255,
  2632. ]],
  2633. '/div',
  2634. ];
  2635. $this->assertHtml($expected, $result);
  2636. $this->View->setRequest(
  2637. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2638. );
  2639. $this->Form->create();
  2640. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2641. $expected = [
  2642. 'div' => ['class' => 'input text'],
  2643. 'label' => ['for' => 'myId'],
  2644. 'Field',
  2645. '/label',
  2646. 'input' => [
  2647. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2648. 'value' => 'My value', 'id' => 'myId',
  2649. ],
  2650. '/div',
  2651. ];
  2652. $this->assertHtml($expected, $result);
  2653. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2654. $this->Form->create();
  2655. $entity->setError('field', 'Badness!');
  2656. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2657. $result = $this->Form->control('field');
  2658. $expected = [
  2659. 'div' => ['class' => 'input text error'],
  2660. 'label' => ['for' => 'field'],
  2661. 'Field',
  2662. '/label',
  2663. 'input' => [
  2664. 'type' => 'text',
  2665. 'name' => 'field',
  2666. 'id' => 'field',
  2667. 'class' => 'form-error',
  2668. 'aria-invalid' => 'true',
  2669. 'aria-describedby' => 'field-error',
  2670. ],
  2671. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2672. 'Badness!',
  2673. '/div',
  2674. '/div',
  2675. ];
  2676. $this->assertHtml($expected, $result);
  2677. $result = $this->Form->control('field', [
  2678. 'templates' => [
  2679. 'inputContainerError' => '{{content}}{{error}}',
  2680. 'error' => '<span class="error-message">{{content}}</span>',
  2681. ],
  2682. ]);
  2683. $expected = [
  2684. 'label' => ['for' => 'field'],
  2685. 'Field',
  2686. '/label',
  2687. 'input' => [
  2688. 'type' => 'text',
  2689. 'name' => 'field',
  2690. 'id' => 'field',
  2691. 'class' => 'form-error',
  2692. // No aria-describedby because error template is custom
  2693. 'aria-invalid' => 'true',
  2694. ],
  2695. ['span' => ['class' => 'error-message']],
  2696. 'Badness!',
  2697. '/span',
  2698. ];
  2699. $this->assertHtml($expected, $result);
  2700. $entity->setError('field', ['minLength'], true);
  2701. $result = $this->Form->control('field', [
  2702. 'error' => [
  2703. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2704. 'maxLength' => 'login too large',
  2705. ],
  2706. ]);
  2707. $expected = [
  2708. 'div' => ['class' => 'input text error'],
  2709. 'label' => ['for' => 'field'],
  2710. 'Field',
  2711. '/label',
  2712. 'input' => [
  2713. 'type' => 'text',
  2714. 'name' => 'field',
  2715. 'id' => 'field',
  2716. 'class' => 'form-error',
  2717. 'aria-invalid' => 'true',
  2718. 'aria-describedby' => 'field-error',
  2719. ],
  2720. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2721. 'Le login doit contenir au moins 2 caractères',
  2722. '/div',
  2723. '/div',
  2724. ];
  2725. $this->assertHtml($expected, $result);
  2726. $entity->setError('field', ['maxLength'], true);
  2727. $result = $this->Form->control('field', [
  2728. 'error' => [
  2729. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2730. 'maxLength' => 'login too large',
  2731. ],
  2732. ]);
  2733. $expected = [
  2734. 'div' => ['class' => 'input text error'],
  2735. 'label' => ['for' => 'field'],
  2736. 'Field',
  2737. '/label',
  2738. 'input' => [
  2739. 'type' => 'text',
  2740. 'name' => 'field',
  2741. 'id' => 'field',
  2742. 'class' => 'form-error',
  2743. 'aria-invalid' => 'true',
  2744. 'aria-describedby' => 'field-error',
  2745. ],
  2746. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2747. 'login too large',
  2748. '/div',
  2749. '/div',
  2750. ];
  2751. $this->assertHtml($expected, $result);
  2752. }
  2753. /**
  2754. * testControlWithTemplateFile method
  2755. *
  2756. * Test that control() accepts a template file.
  2757. */
  2758. public function testControlWithTemplateFile(): void
  2759. {
  2760. $result = $this->Form->control('field', [
  2761. 'templates' => 'htmlhelper_tags',
  2762. ]);
  2763. $expected = [
  2764. 'label' => ['for' => 'field'],
  2765. 'Field',
  2766. '/label',
  2767. 'input' => [
  2768. 'type' => 'text', 'name' => 'field',
  2769. 'id' => 'field',
  2770. ],
  2771. ];
  2772. $this->assertHtml($expected, $result);
  2773. }
  2774. /**
  2775. * testNestedControlsEndWithBrackets method
  2776. *
  2777. * Test that nested inputs end with brackets.
  2778. */
  2779. public function testNestedControlsEndWithBrackets(): void
  2780. {
  2781. $result = $this->Form->text('nested.text[]');
  2782. $expected = [
  2783. 'input' => [
  2784. 'type' => 'text', 'name' => 'nested[text][]',
  2785. ],
  2786. ];
  2787. $this->assertHtml($expected, $result);
  2788. $result = $this->Form->file('nested.file[]');
  2789. $expected = [
  2790. 'input' => [
  2791. 'type' => 'file', 'name' => 'nested[file][]',
  2792. ],
  2793. ];
  2794. $this->assertHtml($expected, $result);
  2795. }
  2796. /**
  2797. * testCreateIdPrefix method
  2798. *
  2799. * Test id prefix.
  2800. */
  2801. public function testCreateIdPrefix(): void
  2802. {
  2803. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2804. $result = $this->Form->control('field');
  2805. $expected = [
  2806. 'div' => ['class' => 'input text'],
  2807. 'label' => ['for' => 'prefix-field'],
  2808. 'Field',
  2809. '/label',
  2810. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2811. '/div',
  2812. ];
  2813. $this->assertHtml($expected, $result);
  2814. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2815. $expected = [
  2816. 'div' => ['class' => 'input text'],
  2817. 'label' => ['for' => 'custom-id'],
  2818. 'Field',
  2819. '/label',
  2820. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2821. '/div',
  2822. ];
  2823. $this->assertHtml($expected, $result);
  2824. $result = $this->Form->radio('Model.field', ['option A']);
  2825. $expected = [
  2826. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2827. 'label' => ['for' => 'prefix-model-field-0'],
  2828. ['input' => [
  2829. 'type' => 'radio',
  2830. 'name' => 'Model[field]',
  2831. 'value' => '0',
  2832. 'id' => 'prefix-model-field-0',
  2833. ]],
  2834. 'option A',
  2835. '/label',
  2836. ];
  2837. $this->assertHtml($expected, $result);
  2838. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2839. $expected = [
  2840. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2841. 'label' => ['for' => 'prefix-model-field-0'],
  2842. ['input' => [
  2843. 'type' => 'radio',
  2844. 'name' => 'Model[field]',
  2845. 'value' => '0',
  2846. 'id' => 'prefix-model-field-0',
  2847. ]],
  2848. 'option A',
  2849. '/label',
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. $result = $this->Form->select(
  2853. 'Model.multi_field',
  2854. ['first'],
  2855. ['multiple' => 'checkbox']
  2856. );
  2857. $expected = [
  2858. 'input' => [
  2859. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2860. ],
  2861. ['div' => ['class' => 'checkbox']],
  2862. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2863. ['input' => [
  2864. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2865. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2866. ]],
  2867. 'first',
  2868. '/label',
  2869. '/div',
  2870. ];
  2871. $this->assertHtml($expected, $result);
  2872. $this->Form->end();
  2873. $result = $this->Form->control('field');
  2874. $expected = [
  2875. 'div' => ['class' => 'input text'],
  2876. 'label' => ['for' => 'field'],
  2877. 'Field',
  2878. '/label',
  2879. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2880. '/div',
  2881. ];
  2882. $this->assertHtml($expected, $result);
  2883. }
  2884. /**
  2885. * testControlZero method
  2886. *
  2887. * Test that inputs with 0 can be created.
  2888. */
  2889. public function testControlZero(): void
  2890. {
  2891. $this->getTableLocator()->get('Contacts', [
  2892. 'className' => ContactsTable::class,
  2893. ]);
  2894. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2895. $result = $this->Form->control('0');
  2896. $expected = [
  2897. 'div' => ['class' => 'input text'],
  2898. 'label' => ['for' => '0'], '/label',
  2899. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2900. '/div',
  2901. ];
  2902. $this->assertHtml($expected, $result);
  2903. }
  2904. /**
  2905. * testControlCheckbox method
  2906. *
  2907. * Test control() with checkbox creation.
  2908. */
  2909. public function testControlCheckbox(): void
  2910. {
  2911. $articles = $this->getTableLocator()->get('Articles');
  2912. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2913. $article = $articles->newEmptyEntity();
  2914. $this->Form->create($article);
  2915. $result = $this->Form->control('Articles.active');
  2916. $expected = [
  2917. 'div' => ['class' => 'input checkbox'],
  2918. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2919. 'label' => ['for' => 'articles-active'],
  2920. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2921. 'Active',
  2922. '/label',
  2923. '/div',
  2924. ];
  2925. $this->assertHtml($expected, $result);
  2926. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2927. $expected = [
  2928. 'div' => ['class' => 'input checkbox'],
  2929. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2930. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2931. '/div',
  2932. ];
  2933. $this->assertHtml($expected, $result);
  2934. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2935. $expected = [
  2936. 'div' => ['class' => 'input checkbox'],
  2937. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2938. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2939. '/div',
  2940. ];
  2941. $this->assertHtml($expected, $result);
  2942. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2943. $expected = [
  2944. 'div' => ['class' => 'input checkbox'],
  2945. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2946. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2947. '/div',
  2948. ];
  2949. $this->assertHtml($expected, $result);
  2950. $result = $this->Form->control('Articles.disabled', [
  2951. 'label' => 'Disabled',
  2952. 'type' => 'checkbox',
  2953. 'data-foo' => 'disabled',
  2954. ]);
  2955. $expected = [
  2956. 'div' => ['class' => 'input checkbox'],
  2957. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2958. 'label' => ['for' => 'articles-disabled'],
  2959. ['input' => [
  2960. 'type' => 'checkbox',
  2961. 'name' => 'Articles[disabled]',
  2962. 'value' => '1',
  2963. 'id' => 'articles-disabled',
  2964. 'data-foo' => 'disabled',
  2965. ]],
  2966. 'Disabled',
  2967. '/label',
  2968. '/div',
  2969. ];
  2970. $this->assertHtml($expected, $result);
  2971. $result = $this->Form->control('Articles.confirm', [
  2972. 'label' => 'Confirm <b>me</b>!',
  2973. 'type' => 'checkbox',
  2974. 'escape' => false,
  2975. ]);
  2976. $expected = [
  2977. 'div' => ['class' => 'input checkbox'],
  2978. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2979. 'label' => ['for' => 'articles-confirm'],
  2980. ['input' => [
  2981. 'type' => 'checkbox',
  2982. 'name' => 'Articles[confirm]',
  2983. 'value' => '1',
  2984. 'id' => 'articles-confirm',
  2985. ]],
  2986. 'Confirm <b>me</b>!',
  2987. '/label',
  2988. '/div',
  2989. ];
  2990. $this->assertHtml($expected, $result);
  2991. }
  2992. /**
  2993. * testControlHidden method
  2994. *
  2995. * Test that control() does not create wrapping div and label tag for hidden fields.
  2996. */
  2997. public function testControlHidden(): void
  2998. {
  2999. $this->getTableLocator()->get('ValidateUsers', [
  3000. 'className' => ValidateUsersTable::class,
  3001. ]);
  3002. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3003. $result = $this->Form->control('ValidateUser.id');
  3004. $expected = [
  3005. 'input' => ['name', 'type' => 'hidden', 'id'],
  3006. ];
  3007. $this->assertHtml($expected, $result);
  3008. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3009. $expected = [
  3010. 'input' => ['name', 'type' => 'hidden', 'id'],
  3011. ];
  3012. $this->assertHtml($expected, $result);
  3013. }
  3014. /**
  3015. * testControlDatetime method
  3016. *
  3017. * Test form->control() with datetime.
  3018. */
  3019. public function testControlDatetime(): void
  3020. {
  3021. $result = $this->Form->control('prueba', [
  3022. 'type' => 'datetime',
  3023. 'value' => new DateTime('2019-09-27 02:52:43'),
  3024. ]);
  3025. $expected = [
  3026. 'div' => ['class' => 'input datetime'],
  3027. 'label' => ['for' => 'prueba'],
  3028. 'Prueba',
  3029. '/label',
  3030. 'input' => [
  3031. 'name' => 'prueba',
  3032. 'id' => 'prueba',
  3033. 'type' => 'datetime-local',
  3034. 'value' => '2019-09-27T02:52:43',
  3035. 'step' => '1',
  3036. ],
  3037. '/div',
  3038. ];
  3039. $this->assertHtml($expected, $result);
  3040. }
  3041. /**
  3042. * testControlDatetimeIdPrefix method
  3043. *
  3044. * Test form->control() with datetime with id prefix.
  3045. */
  3046. public function testControlDatetimeIdPrefix(): void
  3047. {
  3048. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3049. $result = $this->Form->control('prueba', [
  3050. 'type' => 'datetime',
  3051. ]);
  3052. $expected = [
  3053. 'div' => ['class' => 'input datetime'],
  3054. 'label' => ['for' => 'prefix-prueba'],
  3055. 'Prueba',
  3056. '/label',
  3057. 'input' => [
  3058. 'name' => 'prueba',
  3059. 'id' => 'prefix-prueba',
  3060. 'type' => 'datetime-local',
  3061. 'value' => '',
  3062. 'step' => '1',
  3063. ],
  3064. '/div',
  3065. ];
  3066. $this->assertHtml($expected, $result);
  3067. }
  3068. /**
  3069. * testControlDatetimeStep method
  3070. *
  3071. * Test form->control() with datetime with custom step size.
  3072. */
  3073. public function testControlDatetimeStep(): void
  3074. {
  3075. $result = $this->Form->control('prueba', [
  3076. 'type' => 'datetime',
  3077. 'value' => new DateTime('2019-09-27 02:52:43'),
  3078. 'step' => '0.5',
  3079. ]);
  3080. $expected = [
  3081. 'div' => ['class' => 'input datetime'],
  3082. 'label' => ['for' => 'prueba'],
  3083. 'Prueba',
  3084. '/label',
  3085. 'input' => [
  3086. 'name' => 'prueba',
  3087. 'id' => 'prueba',
  3088. 'type' => 'datetime-local',
  3089. 'value' => '2019-09-27T02:52:43.000',
  3090. 'step' => '0.5',
  3091. ],
  3092. '/div',
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. }
  3096. /**
  3097. * testControlCheckboxWithDisabledElements method
  3098. *
  3099. * Test generating checkboxes with disabled elements.
  3100. */
  3101. public function testControlCheckboxWithDisabledElements(): void
  3102. {
  3103. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3104. $result = $this->Form->control('Contact.multiple', [
  3105. 'multiple' => 'checkbox',
  3106. 'disabled' => 'disabled',
  3107. 'options' => $options,
  3108. ]);
  3109. $expected = [
  3110. ['div' => ['class' => 'input select']],
  3111. ['label' => ['for' => 'contact-multiple']],
  3112. 'Multiple',
  3113. '/label',
  3114. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3115. ['div' => ['class' => 'checkbox']],
  3116. ['label' => ['for' => 'contact-multiple-1']],
  3117. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3118. 'One',
  3119. '/label',
  3120. '/div',
  3121. ['div' => ['class' => 'checkbox']],
  3122. ['label' => ['for' => 'contact-multiple-2']],
  3123. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3124. 'Two',
  3125. '/label',
  3126. '/div',
  3127. ['div' => ['class' => 'checkbox']],
  3128. ['label' => ['for' => 'contact-multiple-3']],
  3129. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3130. 'Three',
  3131. '/label',
  3132. '/div',
  3133. '/div',
  3134. ];
  3135. $this->assertHtml($expected, $result);
  3136. // make sure 50 does only disable 50, and not 50f5c0cf
  3137. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3138. $disabled = [50];
  3139. $expected = [
  3140. ['div' => ['class' => 'input select']],
  3141. ['label' => ['for' => 'contact-multiple']],
  3142. 'Multiple',
  3143. '/label',
  3144. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3145. ['div' => ['class' => 'checkbox']],
  3146. ['label' => ['for' => 'contact-multiple-50']],
  3147. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3148. 'Fifty',
  3149. '/label',
  3150. '/div',
  3151. ['div' => ['class' => 'checkbox']],
  3152. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3153. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3154. 'Stringy',
  3155. '/label',
  3156. '/div',
  3157. '/div',
  3158. ];
  3159. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3160. $this->assertHtml($expected, $result);
  3161. }
  3162. /**
  3163. * testControlWithLeadingInteger method
  3164. *
  3165. * Test input name with leading integer, ensure attributes are generated correctly.
  3166. */
  3167. public function testControlWithLeadingInteger(): void
  3168. {
  3169. $result = $this->Form->text('0.Node.title');
  3170. $expected = [
  3171. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3172. ];
  3173. $this->assertHtml($expected, $result);
  3174. }
  3175. /**
  3176. * testControlSelectType method
  3177. *
  3178. * Test form->control() with select type inputs.
  3179. */
  3180. public function testControlSelectType(): void
  3181. {
  3182. $result = $this->Form->control(
  3183. 'email',
  3184. [
  3185. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3186. );
  3187. $expected = [
  3188. 'div' => ['class' => 'input select'],
  3189. 'label' => ['for' => 'email'],
  3190. 'Email',
  3191. '/label',
  3192. ['select' => ['name' => 'email', 'id' => 'email']],
  3193. ['option' => ['value' => '']],
  3194. '/option',
  3195. ['option' => ['value' => 'è']],
  3196. 'Firést',
  3197. '/option',
  3198. ['option' => ['value' => 'é']],
  3199. 'Secoènd',
  3200. '/option',
  3201. '/select',
  3202. '/div',
  3203. ];
  3204. $this->assertHtml($expected, $result);
  3205. $result = $this->Form->control(
  3206. 'email',
  3207. [
  3208. 'options' => ['First', 'Second'], 'empty' => true]
  3209. );
  3210. $expected = [
  3211. 'div' => ['class' => 'input select'],
  3212. 'label' => ['for' => 'email'],
  3213. 'Email',
  3214. '/label',
  3215. ['select' => ['name' => 'email', 'id' => 'email']],
  3216. ['option' => ['value' => '']],
  3217. '/option',
  3218. ['option' => ['value' => '0']],
  3219. 'First',
  3220. '/option',
  3221. ['option' => ['value' => '1']],
  3222. 'Second',
  3223. '/option',
  3224. '/select',
  3225. '/div',
  3226. ];
  3227. $this->assertHtml($expected, $result);
  3228. $result = $this->Form->control('email', [
  3229. 'type' => 'select',
  3230. 'options' => new ArrayObject(['First', 'Second']),
  3231. 'empty' => true,
  3232. ]);
  3233. $this->assertHtml($expected, $result);
  3234. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3235. $this->View->setRequest(
  3236. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3237. );
  3238. $this->Form->create();
  3239. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3240. $expected = [
  3241. 'div' => ['class' => 'input select'],
  3242. 'label' => ['for' => 'model-user-id'],
  3243. 'User',
  3244. '/label',
  3245. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3246. ['option' => ['value' => '']],
  3247. '/option',
  3248. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3249. 'good',
  3250. '/option',
  3251. ['option' => ['value' => 'other']],
  3252. 'bad',
  3253. '/option',
  3254. '/select',
  3255. '/div',
  3256. ];
  3257. $this->assertHtml($expected, $result);
  3258. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3259. $this->View->setRequest(
  3260. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3261. );
  3262. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3263. $expected = [
  3264. 'div' => ['class' => 'input select'],
  3265. 'label' => ['for' => 'thing-user-id'],
  3266. 'User',
  3267. '/label',
  3268. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3269. ['option' => ['value' => '']],
  3270. 'Some Empty',
  3271. '/option',
  3272. ['option' => ['value' => 'value']],
  3273. 'good',
  3274. '/option',
  3275. ['option' => ['value' => 'other']],
  3276. 'bad',
  3277. '/option',
  3278. '/select',
  3279. '/div',
  3280. ];
  3281. $this->assertHtml($expected, $result);
  3282. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3283. $this->View->setRequest(
  3284. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3285. );
  3286. $this->Form->create();
  3287. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3288. $expected = [
  3289. 'div' => ['class' => 'input select'],
  3290. 'label' => ['for' => 'thing-user-id'],
  3291. 'User',
  3292. '/label',
  3293. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3294. ['option' => ['value' => '']],
  3295. 'Some Empty',
  3296. '/option',
  3297. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3298. 'good',
  3299. '/option',
  3300. ['option' => ['value' => 'other']],
  3301. 'bad',
  3302. '/option',
  3303. '/select',
  3304. '/div',
  3305. ];
  3306. $this->assertHtml($expected, $result);
  3307. $result = $this->Form->control('Publisher.id', [
  3308. 'label' => 'Publisher',
  3309. 'type' => 'select',
  3310. 'multiple' => 'checkbox',
  3311. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3312. ]);
  3313. $expected = [
  3314. ['div' => ['class' => 'input select']],
  3315. ['label' => ['for' => 'publisher-id']],
  3316. 'Publisher',
  3317. '/label',
  3318. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3319. ['div' => ['class' => 'checkbox']],
  3320. ['label' => ['for' => 'publisher-id-value-1']],
  3321. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3322. 'Label 1',
  3323. '/label',
  3324. '/div',
  3325. ['div' => ['class' => 'checkbox']],
  3326. ['label' => ['for' => 'publisher-id-value-2']],
  3327. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3328. 'Label 2',
  3329. '/label',
  3330. '/div',
  3331. '/div',
  3332. ];
  3333. $this->assertHtml($expected, $result);
  3334. $articlesTable = $this->getTableLocator()->get('Articles');
  3335. $articlesTable->getSchema()->setColumnType(
  3336. 'published',
  3337. EnumType::from(ArticleStatus::class)
  3338. );
  3339. $this->Form->create($articlesTable->newEmptyEntity());
  3340. $result = $this->Form->control('published');
  3341. $expected = [
  3342. 'div' => ['class' => 'input select'],
  3343. 'label' => ['for' => 'published'],
  3344. 'Published',
  3345. '/label',
  3346. 'select' => ['name' => 'published', 'id' => 'published'],
  3347. ['option' => ['value' => 'Y']],
  3348. 'PUBLISHED',
  3349. '/option',
  3350. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3351. 'UNPUBLISHED',
  3352. '/option',
  3353. '/select',
  3354. '/div',
  3355. ];
  3356. $this->assertHtml($expected, $result);
  3357. $articlesTable->getSchema()->setColumnType(
  3358. 'published',
  3359. EnumType::from(ArticleStatusLabel::class)
  3360. );
  3361. $this->Form->create($articlesTable->newEmptyEntity());
  3362. $result = $this->Form->control('published');
  3363. $expected = [
  3364. 'div' => ['class' => 'input select'],
  3365. 'label' => ['for' => 'published'],
  3366. 'Published',
  3367. '/label',
  3368. 'select' => ['name' => 'published', 'id' => 'published'],
  3369. ['option' => ['value' => 'Y']],
  3370. 'Is published',
  3371. '/option',
  3372. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3373. 'Is unpublished',
  3374. '/option',
  3375. '/select',
  3376. '/div',
  3377. ];
  3378. $this->assertHtml($expected, $result);
  3379. }
  3380. /**
  3381. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3382. *
  3383. * Test that control() and a non standard primary key makes a hidden input by default.
  3384. */
  3385. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3386. {
  3387. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3388. $this->Form->create($this->article);
  3389. $result = $this->Form->control('title');
  3390. $expected = [
  3391. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3392. ];
  3393. $this->assertHtml($expected, $result);
  3394. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3395. $this->Form->create($this->article);
  3396. $result = $this->Form->control('title');
  3397. $expected = [
  3398. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3399. ];
  3400. $this->assertHtml($expected, $result);
  3401. $result = $this->Form->control('body');
  3402. $expected = [
  3403. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3404. ];
  3405. $this->assertHtml($expected, $result);
  3406. }
  3407. /**
  3408. * testControlOverridingMagicSelectType method
  3409. *
  3410. * Test that overriding the magic select type widget is possible.
  3411. */
  3412. public function testControlOverridingMagicSelectType(): void
  3413. {
  3414. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3415. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3416. $expected = [
  3417. 'div' => ['class' => 'input text'],
  3418. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3419. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3420. '/div',
  3421. ];
  3422. $this->assertHtml($expected, $result);
  3423. //Check that magic types still work for plural/singular vars
  3424. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3425. $result = $this->Form->control('Model.type');
  3426. $expected = [
  3427. 'div' => ['class' => 'input select'],
  3428. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3429. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3430. ['option' => ['value' => 'value']], 'good', '/option',
  3431. ['option' => ['value' => 'other']], 'bad', '/option',
  3432. '/select',
  3433. '/div',
  3434. ];
  3435. $this->assertHtml($expected, $result);
  3436. }
  3437. /**
  3438. * testControlMagicTypeDoesNotOverride method
  3439. *
  3440. * Test that inferred types do not override developer input.
  3441. */
  3442. public function testControlMagicTypeDoesNotOverride(): void
  3443. {
  3444. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3445. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3446. $expected = [
  3447. 'div' => ['class' => 'input checkbox'],
  3448. ['input' => [
  3449. 'type' => 'hidden',
  3450. 'name' => 'Model[user]',
  3451. 'value' => 0,
  3452. ]],
  3453. 'label' => ['for' => 'model-user'],
  3454. ['input' => [
  3455. 'name' => 'Model[user]',
  3456. 'type' => 'checkbox',
  3457. 'id' => 'model-user',
  3458. 'value' => 1,
  3459. ]],
  3460. 'User',
  3461. '/label',
  3462. '/div',
  3463. ];
  3464. $this->assertHtml($expected, $result);
  3465. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3466. $options = [
  3467. 1 => 'blue',
  3468. 2 => 'red',
  3469. ];
  3470. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3471. $expected = [
  3472. 'div' => ['class' => 'input select'],
  3473. 'label' => ['for' => 'tags-ids'],
  3474. 'Tags',
  3475. '/label',
  3476. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3477. ['div' => ['class' => 'checkbox']],
  3478. ['label' => ['for' => 'tags-ids-1']],
  3479. ['input' => [
  3480. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3481. 'value' => '1', 'name' => 'tags[_ids][]',
  3482. ]],
  3483. 'blue',
  3484. '/label',
  3485. '/div',
  3486. ['div' => ['class' => 'checkbox']],
  3487. ['label' => ['for' => 'tags-ids-2']],
  3488. ['input' => [
  3489. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3490. 'value' => '2', 'name' => 'tags[_ids][]',
  3491. ]],
  3492. 'red',
  3493. '/label',
  3494. '/div',
  3495. '/div',
  3496. ];
  3497. $this->assertHtml($expected, $result);
  3498. }
  3499. /**
  3500. * testControlMagicSelectForTypeNumber method
  3501. *
  3502. * Test that magic control() selects are created for type=number.
  3503. */
  3504. public function testControlMagicSelectForTypeNumber(): void
  3505. {
  3506. $this->getTableLocator()->get('ValidateUsers', [
  3507. 'className' => ValidateUsersTable::class,
  3508. ]);
  3509. $entity = new Entity(['balance' => 1]);
  3510. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3511. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3512. $result = $this->Form->control('balance');
  3513. $expected = [
  3514. 'div' => ['class' => 'input select'],
  3515. 'label' => ['for' => 'balance'],
  3516. 'Balance',
  3517. '/label',
  3518. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3519. ['option' => ['value' => '0']],
  3520. 'nothing',
  3521. '/option',
  3522. ['option' => ['value' => '1', 'selected' => 'selected']],
  3523. 'some',
  3524. '/option',
  3525. ['option' => ['value' => '100']],
  3526. 'a lot',
  3527. '/option',
  3528. '/select',
  3529. '/div',
  3530. ];
  3531. $this->assertHtml($expected, $result);
  3532. }
  3533. /**
  3534. * testInvalidControlTypeOption method
  3535. *
  3536. * Test invalid 'input' type option to control() function.
  3537. */
  3538. public function testInvalidControlTypeOption(): void
  3539. {
  3540. $this->expectException(InvalidArgumentException::class);
  3541. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3542. $this->Form->control('text', ['type' => 'input']);
  3543. }
  3544. /**
  3545. * testControlMagicSelectChangeToRadio method
  3546. *
  3547. * Test that magic control() selects can easily be converted into radio types without error.
  3548. */
  3549. public function testControlMagicSelectChangeToRadio(): void
  3550. {
  3551. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3552. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3553. $this->assertStringContainsString('input type="radio"', $result);
  3554. }
  3555. /**
  3556. * testFormControlSubmit method
  3557. *
  3558. * Test correct results for form::control() and type submit.
  3559. */
  3560. public function testFormControlSubmit(): void
  3561. {
  3562. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3563. $expected = [
  3564. 'div' => ['class' => 'submit'],
  3565. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3566. '/div',
  3567. ];
  3568. $this->assertHtml($expected, $result);
  3569. }
  3570. /**
  3571. * testFormControls method
  3572. *
  3573. * Test correct results from Form::controls().
  3574. */
  3575. public function testFormControlsLegendFieldset(): void
  3576. {
  3577. $this->Form->create($this->article);
  3578. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3579. $expected = [
  3580. '<fieldset',
  3581. '<legend',
  3582. 'The Legend',
  3583. '/legend',
  3584. '*/fieldset',
  3585. ];
  3586. $this->assertHtml($expected, $result);
  3587. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3588. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3589. $this->assertStringContainsString('<fieldset>', $result);
  3590. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3591. $this->assertStringNotContainsString('<legend>', $result);
  3592. $this->assertStringNotContainsString('<fieldset>', $result);
  3593. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3594. $this->assertStringNotContainsString('<legend>', $result);
  3595. $this->assertStringNotContainsString('<fieldset>', $result);
  3596. $this->Form->create($this->article);
  3597. $this->View->setRequest($this->View->getRequest()
  3598. ->withParam('prefix', 'admin')
  3599. ->withParam('action', 'admin_edit')
  3600. ->withParam('controller', 'articles'));
  3601. $result = $this->Form->allControls();
  3602. $expected = [
  3603. '<fieldset',
  3604. '<legend',
  3605. 'New Article',
  3606. '/legend',
  3607. '*/fieldset',
  3608. ];
  3609. $this->assertHtml($expected, $result);
  3610. $this->Form->create($this->article);
  3611. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3612. $expected = [
  3613. '<fieldset',
  3614. '<legend',
  3615. 'The Legend',
  3616. '/legend',
  3617. '*/fieldset',
  3618. ];
  3619. $this->assertHtml($expected, $result);
  3620. $this->Form->create($this->article);
  3621. $result = $this->Form->allControls([], [
  3622. 'fieldset' => [
  3623. 'class' => 'some-class some-other-class',
  3624. 'disabled' => true,
  3625. 'data-param' => 'a-param',
  3626. ],
  3627. 'legend' => 'The Legend',
  3628. ]);
  3629. $expected = [
  3630. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3631. '<legend',
  3632. 'The Legend',
  3633. '/legend',
  3634. '*/fieldset',
  3635. ];
  3636. $this->assertHtml($expected, $result);
  3637. }
  3638. /**
  3639. * testFormControls method
  3640. *
  3641. * Test the controls() method.
  3642. */
  3643. public function testFormControls(): void
  3644. {
  3645. $this->Form->create($this->article);
  3646. $result = $this->Form->allControls();
  3647. $expected = [
  3648. '<fieldset',
  3649. '<legend', 'New Article', '/legend',
  3650. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3651. ['div' => ['class' => 'input select required']],
  3652. '*/div',
  3653. ['div' => ['class' => 'input text required']],
  3654. '*/div',
  3655. ['div' => ['class' => 'input text']],
  3656. '*/div',
  3657. ['div' => ['class' => 'input text']],
  3658. '*/div',
  3659. '/fieldset',
  3660. ];
  3661. $this->assertHtml($expected, $result);
  3662. $result = $this->Form->allControls([
  3663. 'published' => ['type' => 'boolean'],
  3664. ]);
  3665. $expected = [
  3666. '<fieldset',
  3667. '<legend', 'New Article', '/legend',
  3668. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3669. ['div' => ['class' => 'input select required']],
  3670. '*/div',
  3671. ['div' => ['class' => 'input text required']],
  3672. '*/div',
  3673. ['div' => ['class' => 'input text']],
  3674. '*/div',
  3675. ['div' => ['class' => 'input boolean']],
  3676. '*/div',
  3677. '/fieldset',
  3678. ];
  3679. $this->assertHtml($expected, $result);
  3680. $this->Form->create($this->article);
  3681. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3682. $expected = [
  3683. 'fieldset' => [],
  3684. 'legend' => [],
  3685. 'Hello',
  3686. '/legend',
  3687. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3688. ['div' => ['class' => 'input select required']],
  3689. '*/div',
  3690. ['div' => ['class' => 'input text required']],
  3691. '*/div',
  3692. ['div' => ['class' => 'input text']],
  3693. '*/div',
  3694. ['div' => ['class' => 'input text']],
  3695. '*/div',
  3696. '/fieldset',
  3697. ];
  3698. $this->assertHtml($expected, $result);
  3699. $this->Form->create();
  3700. $expected = [
  3701. 'fieldset' => [],
  3702. ['div' => ['class' => 'input text']],
  3703. 'label' => ['for' => 'foo'],
  3704. 'Foo',
  3705. '/label',
  3706. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3707. '*/div',
  3708. '/fieldset',
  3709. ];
  3710. $result = $this->Form->allControls(
  3711. ['foo' => ['type' => 'text']],
  3712. ['legend' => false]
  3713. );
  3714. $this->assertHtml($expected, $result);
  3715. }
  3716. /**
  3717. * testFormControlsBlacklist method
  3718. */
  3719. public function testFormControlsBlacklist(): void
  3720. {
  3721. $this->Form->create($this->article);
  3722. $result = $this->Form->allControls([
  3723. 'id' => false,
  3724. ]);
  3725. $expected = [
  3726. '<fieldset',
  3727. '<legend', 'New Article', '/legend',
  3728. ['div' => ['class' => 'input select required']],
  3729. '*/div',
  3730. ['div' => ['class' => 'input text required']],
  3731. '*/div',
  3732. ['div' => ['class' => 'input text']],
  3733. '*/div',
  3734. ['div' => ['class' => 'input text']],
  3735. '*/div',
  3736. '/fieldset',
  3737. ];
  3738. $this->assertHtml($expected, $result);
  3739. $this->Form->create($this->article);
  3740. $result = $this->Form->allControls([
  3741. 'id' => [],
  3742. ]);
  3743. $expected = [
  3744. '<fieldset',
  3745. '<legend', 'New Article', '/legend',
  3746. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3747. ['div' => ['class' => 'input select required']],
  3748. '*/div',
  3749. ['div' => ['class' => 'input text required']],
  3750. '*/div',
  3751. ['div' => ['class' => 'input text']],
  3752. '*/div',
  3753. ['div' => ['class' => 'input text']],
  3754. '*/div',
  3755. '/fieldset',
  3756. ];
  3757. $this->assertHtml($expected, $result, true);
  3758. }
  3759. /**
  3760. * testSelectAsCheckbox method
  3761. *
  3762. * Test multi-select widget with checkbox formatting.
  3763. */
  3764. public function testSelectAsCheckbox(): void
  3765. {
  3766. $result = $this->Form->select(
  3767. 'Model.multi_field',
  3768. ['first', 'second', 'third'],
  3769. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3770. );
  3771. $expected = [
  3772. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3773. ['div' => ['class' => 'checkbox']],
  3774. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3775. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3776. 'first',
  3777. '/label',
  3778. '/div',
  3779. ['div' => ['class' => 'checkbox']],
  3780. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3781. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3782. 'second',
  3783. '/label',
  3784. '/div',
  3785. ['div' => ['class' => 'checkbox']],
  3786. ['label' => ['for' => 'model-multi-field-2']],
  3787. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3788. 'third',
  3789. '/label',
  3790. '/div',
  3791. ];
  3792. $this->assertHtml($expected, $result);
  3793. $result = $this->Form->select(
  3794. 'Model.multi_field',
  3795. ['1/2' => 'half'],
  3796. ['multiple' => 'checkbox']
  3797. );
  3798. $expected = [
  3799. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3800. ['div' => ['class' => 'checkbox']],
  3801. ['label' => ['for' => 'model-multi-field-1-2']],
  3802. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3803. 'half',
  3804. '/label',
  3805. '/div',
  3806. ];
  3807. $this->assertHtml($expected, $result);
  3808. }
  3809. /**
  3810. * testLabel method
  3811. *
  3812. * Test label generation.
  3813. */
  3814. public function testLabel(): void
  3815. {
  3816. $result = $this->Form->label('Person.name');
  3817. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3818. $this->assertHtml($expected, $result);
  3819. $result = $this->Form->label('Person.name');
  3820. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3821. $this->assertHtml($expected, $result);
  3822. $result = $this->Form->label('Person.first_name');
  3823. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3824. $this->assertHtml($expected, $result);
  3825. $result = $this->Form->label('Person.first_name', 'Your first name');
  3826. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3827. $this->assertHtml($expected, $result);
  3828. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3829. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3830. $this->assertHtml($expected, $result);
  3831. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3832. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3833. $this->assertHtml($expected, $result);
  3834. $result = $this->Form->label('Person.first_name', '');
  3835. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3836. $this->assertHtml($expected, $result);
  3837. $result = $this->Form->label('Person.2.name', '');
  3838. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3839. $this->assertHtml($expected, $result);
  3840. }
  3841. /**
  3842. * testLabelContainControl method
  3843. *
  3844. * Test that label() can accept an input with the correct template vars.
  3845. */
  3846. public function testLabelContainControl(): void
  3847. {
  3848. $this->Form->setTemplates([
  3849. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3850. ]);
  3851. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3852. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3853. ]);
  3854. $expected = [
  3855. 'label' => ['for' => 'person-accept-terms'],
  3856. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3857. 'Accept',
  3858. '/label',
  3859. ];
  3860. $this->assertHtml($expected, $result);
  3861. }
  3862. /**
  3863. * testTextbox method
  3864. *
  3865. * Test textbox element generation.
  3866. */
  3867. public function testTextbox(): void
  3868. {
  3869. $result = $this->Form->text('Model.field');
  3870. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3871. $this->assertHtml($expected, $result);
  3872. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3873. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3874. $this->assertHtml($expected, $result);
  3875. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3876. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3877. $this->assertHtml($expected, $result);
  3878. }
  3879. /**
  3880. * testTextBoxDataAndError method
  3881. *
  3882. * Test that text() hooks up with request data and error fields.
  3883. */
  3884. public function testTextBoxDataAndError(): void
  3885. {
  3886. $this->article['errors'] = [
  3887. 'Contact' => ['text' => 'wrong'],
  3888. ];
  3889. $this->View->setRequest($this->View->getRequest()
  3890. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3891. ->withData('Contact.text', 'test'));
  3892. $this->Form->create($this->article);
  3893. $result = $this->Form->text('Model.text');
  3894. $expected = [
  3895. 'input' => [
  3896. 'type' => 'text',
  3897. 'name' => 'Model[text]',
  3898. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3899. ],
  3900. ];
  3901. $this->assertHtml($expected, $result);
  3902. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3903. $expected = [
  3904. 'input' => [
  3905. 'type' => 'text',
  3906. 'name' => 'Contact[text]',
  3907. 'value' => 'test',
  3908. 'id' => 'theID',
  3909. 'class' => 'form-error',
  3910. ],
  3911. ];
  3912. $this->assertHtml($expected, $result);
  3913. }
  3914. /**
  3915. * testDefaultValue method
  3916. *
  3917. * Test default value setting.
  3918. */
  3919. public function testTextDefaultValue(): void
  3920. {
  3921. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3922. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3923. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3924. $this->assertHtml($expected, $result);
  3925. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3926. $this->Form->create();
  3927. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3928. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3929. $this->assertHtml($expected, $result);
  3930. $Articles = $this->getTableLocator()->get('Articles');
  3931. $title = $Articles->getSchema()->getColumn('title');
  3932. $Articles->getSchema()->addColumn(
  3933. 'title',
  3934. ['default' => 'default title', 'length' => 255] + $title
  3935. );
  3936. $entity = $Articles->newEmptyEntity();
  3937. $this->Form->create($entity);
  3938. // Get default value from schema
  3939. $result = $this->Form->text('title');
  3940. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3941. $this->assertHtml($expected, $result);
  3942. // Don't get value from schema
  3943. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3944. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3945. $this->assertHtml($expected, $result);
  3946. // Custom default value overrides default value from schema
  3947. $result = $this->Form->text('title', ['default' => 'override default']);
  3948. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3949. $this->assertHtml($expected, $result);
  3950. // Default value from schema is used only for new entities.
  3951. $entity->setNew(false);
  3952. $result = $this->Form->text('title');
  3953. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3954. $this->assertHtml($expected, $result);
  3955. }
  3956. /**
  3957. * testError method
  3958. *
  3959. * Test field error generation.
  3960. */
  3961. public function testError(): void
  3962. {
  3963. $this->article['errors'] = [
  3964. 'Article' => ['field' => 'email'],
  3965. ];
  3966. $this->Form->create($this->article);
  3967. $result = $this->Form->error('Article.field');
  3968. $expected = [
  3969. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3970. 'email',
  3971. '/div',
  3972. ];
  3973. $this->assertHtml($expected, $result);
  3974. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3975. $expected = [
  3976. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3977. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3978. '/div',
  3979. ];
  3980. $this->assertHtml($expected, $result);
  3981. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3982. $expected = [
  3983. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3984. '<strong', 'Badness!', '/strong',
  3985. '/div',
  3986. ];
  3987. $this->assertHtml($expected, $result);
  3988. }
  3989. /**
  3990. * testErrorRuleName method
  3991. *
  3992. * Test error translation can use rule names for translating.
  3993. */
  3994. public function testErrorRuleName(): void
  3995. {
  3996. $this->article['errors'] = [
  3997. 'Article' => [
  3998. 'field' => ['email' => 'Your email was not good'],
  3999. ],
  4000. ];
  4001. $this->Form->create($this->article);
  4002. $result = $this->Form->error('Article.field');
  4003. $expected = [
  4004. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4005. 'Your email was not good',
  4006. '/div',
  4007. ];
  4008. $this->assertHtml($expected, $result);
  4009. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4010. $expected = [
  4011. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4012. 'Email in use',
  4013. '/div',
  4014. ];
  4015. $this->assertHtml($expected, $result);
  4016. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4017. $expected = [
  4018. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4019. 'Email in use',
  4020. '/div',
  4021. ];
  4022. $this->assertHtml($expected, $result);
  4023. $result = $this->Form->error('Article.field', [
  4024. 'email' => 'Key is preferred',
  4025. 'Your email was not good' => 'Email in use',
  4026. ]);
  4027. $expected = [
  4028. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4029. 'Key is preferred',
  4030. '/div',
  4031. ];
  4032. $this->assertHtml($expected, $result);
  4033. }
  4034. /**
  4035. * testErrorMessages method
  4036. *
  4037. * Test error with nested lists.
  4038. */
  4039. public function testErrorMessages(): void
  4040. {
  4041. $this->article['errors'] = [
  4042. 'Article' => ['field' => 'email'],
  4043. ];
  4044. $this->Form->create($this->article);
  4045. $result = $this->Form->error('Article.field', [
  4046. 'email' => 'No good!',
  4047. ]);
  4048. $expected = [
  4049. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4050. 'No good!',
  4051. '/div',
  4052. ];
  4053. $this->assertHtml($expected, $result);
  4054. }
  4055. /**
  4056. * testErrorMultipleMessages method
  4057. *
  4058. * Test error() with multiple messages.
  4059. */
  4060. public function testErrorMultipleMessages(): void
  4061. {
  4062. $this->article['errors'] = [
  4063. 'field' => ['notBlank', 'email', 'Something else'],
  4064. ];
  4065. $this->Form->create($this->article);
  4066. $result = $this->Form->error('field', [
  4067. 'notBlank' => 'Cannot be empty',
  4068. 'email' => 'No good!',
  4069. ]);
  4070. $expected = [
  4071. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4072. 'ul' => [],
  4073. '<li', 'Cannot be empty', '/li',
  4074. '<li', 'No good!', '/li',
  4075. '<li', 'Something else', '/li',
  4076. '/ul',
  4077. '/div',
  4078. ];
  4079. $this->assertHtml($expected, $result);
  4080. }
  4081. /**
  4082. * testPassword method
  4083. *
  4084. * Test password element generation.
  4085. */
  4086. public function testPassword(): void
  4087. {
  4088. $this->article['errors'] = [
  4089. 'Contact' => [
  4090. 'passwd' => 1,
  4091. ],
  4092. ];
  4093. $this->Form->create($this->article);
  4094. $result = $this->Form->password('Contact.field');
  4095. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4096. $this->assertHtml($expected, $result);
  4097. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4098. $this->Form->create($this->article);
  4099. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4100. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4101. $this->assertHtml($expected, $result);
  4102. }
  4103. /**
  4104. * testRadio method
  4105. *
  4106. * Test radio element set generation.
  4107. */
  4108. public function testRadio(): void
  4109. {
  4110. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4111. $expected = [
  4112. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4113. ['label' => ['for' => 'model-field-0']],
  4114. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4115. 'option A',
  4116. '/label',
  4117. ['label' => ['for' => 'model-field-1']],
  4118. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4119. 'option B',
  4120. '/label',
  4121. ];
  4122. $this->assertHtml($expected, $result);
  4123. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4124. $this->assertHtml($expected, $result);
  4125. $result = $this->Form->radio(
  4126. 'Employee.vegetarian',
  4127. ['yes' => 'Yes', 'no' => 'No'],
  4128. ['form' => 'my-form', 'id' => 'id-veg']
  4129. );
  4130. $expected = [
  4131. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4132. ['label' => ['for' => 'id-veg-yes']],
  4133. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4134. 'Yes',
  4135. '/label',
  4136. ['label' => ['for' => 'id-veg-no']],
  4137. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4138. 'No',
  4139. '/label',
  4140. ];
  4141. $this->assertHtml($expected, $result);
  4142. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4143. $expected = [
  4144. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4145. ['label' => ['for' => 'model-custom-0']],
  4146. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4147. 'option A',
  4148. '/label',
  4149. ['label' => ['for' => 'model-custom-1']],
  4150. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4151. 'option B',
  4152. '/label',
  4153. ];
  4154. $this->assertHtml($expected, $result);
  4155. $result = $this->Form->radio(
  4156. 'Employee.gender',
  4157. [
  4158. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4159. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4160. ]
  4161. );
  4162. $expected = [
  4163. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4164. ['label' => ['for' => 'employee-gender-male']],
  4165. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4166. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4167. 'Male',
  4168. '/label',
  4169. ['label' => ['for' => 'employee-gender-female']],
  4170. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4171. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4172. 'Female',
  4173. '/label',
  4174. ];
  4175. $this->assertHtml($expected, $result);
  4176. $article = new Article([
  4177. 'status' => ArticleStatus::UNPUBLISHED,
  4178. ]);
  4179. $this->Form->create($article);
  4180. $result = $this->Form->radio('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4181. $expected = [
  4182. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => '', 'id' => 'status'],
  4183. ['label' => ['for' => 'status-y']],
  4184. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'Y', 'id' => 'status-y']],
  4185. 'Published',
  4186. '/label',
  4187. ['label' => ['for' => 'status-n']],
  4188. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'N', 'id' => 'status-n', 'checked' => 'checked']],
  4189. 'Unpublished',
  4190. '/label',
  4191. ];
  4192. $this->assertHtml($expected, $result);
  4193. }
  4194. /**
  4195. * Test radio with complex options and empty disabled data.
  4196. */
  4197. public function testRadioComplexDisabled(): void
  4198. {
  4199. $options = [
  4200. ['value' => 'r', 'text' => 'red'],
  4201. ['value' => 'b', 'text' => 'blue'],
  4202. ];
  4203. $attrs = ['disabled' => []];
  4204. $result = $this->Form->radio('Model.field', $options, $attrs);
  4205. $expected = [
  4206. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4207. ['label' => ['for' => 'model-field-r']],
  4208. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4209. 'red',
  4210. '/label',
  4211. ['label' => ['for' => 'model-field-b']],
  4212. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4213. 'blue',
  4214. '/label',
  4215. ];
  4216. $this->assertHtml($expected, $result);
  4217. $attrs = ['disabled' => ['r']];
  4218. $result = $this->Form->radio('Model.field', $options, $attrs);
  4219. $this->assertStringContainsString('disabled="disabled"', $result);
  4220. }
  4221. /**
  4222. * testRadioDefaultValue method
  4223. *
  4224. * Test default value setting on radio() method.
  4225. */
  4226. public function testRadioDefaultValue(): void
  4227. {
  4228. $Articles = $this->getTableLocator()->get('Articles');
  4229. $title = $Articles->getSchema()->getColumn('title');
  4230. $Articles->getSchema()->addColumn(
  4231. 'title',
  4232. ['default' => '1'] + $title
  4233. );
  4234. $this->Form->create($Articles->newEmptyEntity());
  4235. $result = $this->Form->radio('title', ['option A', 'option B']);
  4236. $expected = [
  4237. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4238. ['label' => ['for' => 'title-0']],
  4239. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4240. 'option A',
  4241. '/label',
  4242. ['label' => ['for' => 'title-1']],
  4243. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4244. 'option B',
  4245. '/label',
  4246. ];
  4247. $this->assertHtml($expected, $result);
  4248. }
  4249. /**
  4250. * Test setting a hiddenField value on radio buttons.
  4251. */
  4252. public function testRadioHiddenFieldValue(): void
  4253. {
  4254. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4255. $expected = [
  4256. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4257. 'label' => ['for' => 'title-0'],
  4258. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4259. 'option A',
  4260. '/label',
  4261. ];
  4262. $this->assertHtml($expected, $result);
  4263. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4264. $expected = [
  4265. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4266. 'label' => ['for' => 'title-0'],
  4267. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4268. 'option A',
  4269. '/label',
  4270. ];
  4271. $this->assertHtml($expected, $result);
  4272. }
  4273. /**
  4274. * testControlRadio method
  4275. *
  4276. * Test that input works with radio types.
  4277. */
  4278. public function testControlRadio(): void
  4279. {
  4280. $result = $this->Form->control('test', [
  4281. 'type' => 'radio',
  4282. 'options' => ['A', 'B'],
  4283. ]);
  4284. $expected = [
  4285. ['div' => ['class' => 'input radio']],
  4286. '<label',
  4287. 'Test',
  4288. '/label',
  4289. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4290. ['label' => ['for' => 'test-0']],
  4291. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4292. 'A',
  4293. '/label',
  4294. ['label' => ['for' => 'test-1']],
  4295. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4296. 'B',
  4297. '/label',
  4298. '/div',
  4299. ];
  4300. $this->assertHtml($expected, $result);
  4301. $result = $this->Form->control('test', [
  4302. 'type' => 'radio',
  4303. 'options' => ['A', 'B'],
  4304. 'value' => '0',
  4305. ]);
  4306. $expected = [
  4307. ['div' => ['class' => 'input radio']],
  4308. '<label',
  4309. 'Test',
  4310. '/label',
  4311. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4312. ['label' => ['for' => 'test-0']],
  4313. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4314. 'A',
  4315. '/label',
  4316. ['label' => ['for' => 'test-1']],
  4317. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4318. 'B',
  4319. '/label',
  4320. '/div',
  4321. ];
  4322. $this->assertHtml($expected, $result);
  4323. $result = $this->Form->control('test', [
  4324. 'type' => 'radio',
  4325. 'options' => ['A', 'B'],
  4326. 'label' => false,
  4327. ]);
  4328. $expected = [
  4329. ['div' => ['class' => 'input radio']],
  4330. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4331. ['label' => ['for' => 'test-0']],
  4332. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4333. 'A',
  4334. '/label',
  4335. ['label' => ['for' => 'test-1']],
  4336. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4337. 'B',
  4338. '/label',
  4339. '/div',
  4340. ];
  4341. $this->assertHtml($expected, $result);
  4342. $result = $this->Form->control('accept', [
  4343. 'type' => 'radio',
  4344. 'options' => [
  4345. 1 => 'positive',
  4346. -1 => 'negative',
  4347. ],
  4348. 'label' => false,
  4349. ]);
  4350. $expected = [
  4351. ['div' => ['class' => 'input radio']],
  4352. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4353. ['label' => ['for' => 'accept-1']],
  4354. ['input' => [
  4355. 'type' => 'radio',
  4356. 'name' => 'accept',
  4357. 'value' => '1',
  4358. 'id' => 'accept-1',
  4359. ]],
  4360. 'positive',
  4361. '/label',
  4362. ['label' => ['for' => 'accept--1']],
  4363. ['input' => [
  4364. 'type' => 'radio',
  4365. 'name' => 'accept',
  4366. 'value' => '-1',
  4367. 'id' => 'accept--1',
  4368. ]],
  4369. 'negative',
  4370. '/label',
  4371. '/div',
  4372. ];
  4373. $this->assertHtml($expected, $result);
  4374. $articlesTable = $this->getTableLocator()->get('Articles');
  4375. $articlesTable->getSchema()->setColumnType(
  4376. 'published',
  4377. EnumType::from(ArticleStatus::class)
  4378. );
  4379. $this->Form->create($articlesTable->newEmptyEntity());
  4380. $result = $this->Form->control('published', ['type' => 'radio', 'label' => false,]);
  4381. $expected = [
  4382. ['div' => ['class' => 'input radio']],
  4383. 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => '', 'id' => 'published'],
  4384. ['label' => ['for' => 'published-y']],
  4385. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'Y', 'id' => 'published-y']],
  4386. 'PUBLISHED',
  4387. '/label',
  4388. ['label' => ['for' => 'published-n']],
  4389. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'N', 'id' => 'published-n', 'checked' => 'checked']],
  4390. 'UNPUBLISHED',
  4391. '/label',
  4392. '/div',
  4393. ];
  4394. $this->assertHtml($expected, $result);
  4395. }
  4396. /**
  4397. * testRadioNoLabel method
  4398. *
  4399. * Test that radio() works with label = false.
  4400. */
  4401. public function testRadioNoLabel(): void
  4402. {
  4403. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4404. $expected = [
  4405. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4406. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4407. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4408. ];
  4409. $this->assertHtml($expected, $result);
  4410. }
  4411. /**
  4412. * testRadioControlInsideLabel method
  4413. *
  4414. * Test generating radio input inside label ala twitter bootstrap.
  4415. */
  4416. public function testRadioControlInsideLabel(): void
  4417. {
  4418. $this->Form->setTemplates([
  4419. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4420. 'radioWrapper' => '{{label}}',
  4421. ]);
  4422. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4423. // phpcs:disable
  4424. $expected = [
  4425. ['input' => [
  4426. 'type' => 'hidden',
  4427. 'name' => 'Model[field]',
  4428. 'value' => '',
  4429. 'id' => 'model-field'
  4430. ]],
  4431. ['label' => ['for' => 'model-field-0']],
  4432. ['input' => [
  4433. 'type' => 'radio',
  4434. 'name' => 'Model[field]',
  4435. 'value' => '0',
  4436. 'id' => 'model-field-0'
  4437. ]],
  4438. 'option A',
  4439. '/label',
  4440. ['label' => ['for' => 'model-field-1']],
  4441. ['input' => [
  4442. 'type' => 'radio',
  4443. 'name' => 'Model[field]',
  4444. 'value' => '1',
  4445. 'id' => 'model-field-1'
  4446. ]],
  4447. 'option B',
  4448. '/label',
  4449. ];
  4450. // phpcs:enable
  4451. $this->assertHtml($expected, $result);
  4452. }
  4453. /**
  4454. * testRadioHiddenControlDisabling method
  4455. *
  4456. * Test disabling the hidden input for radio buttons.
  4457. */
  4458. public function testRadioHiddenControlDisabling(): void
  4459. {
  4460. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4461. $expected = [
  4462. 'label' => ['for' => 'model-1-field-0'],
  4463. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4464. 'option A',
  4465. '/label',
  4466. ];
  4467. $this->assertHtml($expected, $result);
  4468. }
  4469. /**
  4470. * testRadioOutOfRange method
  4471. *
  4472. * Test radio element set generation.
  4473. */
  4474. public function testRadioOutOfRange(): void
  4475. {
  4476. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4477. $expected = [
  4478. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4479. 'label' => ['for' => 'model-field-v'],
  4480. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4481. 'value',
  4482. '/label',
  4483. ];
  4484. $this->assertHtml($expected, $result);
  4485. }
  4486. /**
  4487. * testSelect method
  4488. *
  4489. * Test select element generation.
  4490. */
  4491. public function testSelect(): void
  4492. {
  4493. $result = $this->Form->select('Model.field', []);
  4494. $expected = [
  4495. 'select' => ['name' => 'Model[field]'],
  4496. '/select',
  4497. ];
  4498. $this->assertHtml($expected, $result);
  4499. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4500. $this->Form->create();
  4501. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4502. $expected = [
  4503. 'select' => ['name' => 'Model[field]'],
  4504. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4505. 'good',
  4506. '/option',
  4507. ['option' => ['value' => 'other']],
  4508. 'bad',
  4509. '/option',
  4510. '/select',
  4511. ];
  4512. $this->assertHtml($expected, $result);
  4513. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4514. $this->assertHtml($expected, $result);
  4515. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4516. $this->Form->create();
  4517. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4518. $expected = [
  4519. 'select' => ['name' => 'Model[field]'],
  4520. ['option' => ['value' => 'value']],
  4521. 'good',
  4522. '/option',
  4523. ['option' => ['value' => 'other']],
  4524. 'bad',
  4525. '/option',
  4526. '/select',
  4527. ];
  4528. $this->assertHtml($expected, $result);
  4529. $options = [
  4530. ['value' => 'first', 'text' => 'First'],
  4531. ['value' => 'first', 'text' => 'Another First'],
  4532. ];
  4533. $result = $this->Form->select(
  4534. 'Model.field',
  4535. $options,
  4536. ['escape' => false, 'empty' => false]
  4537. );
  4538. $expected = [
  4539. 'select' => ['name' => 'Model[field]'],
  4540. ['option' => ['value' => 'first']],
  4541. 'First',
  4542. '/option',
  4543. ['option' => ['value' => 'first']],
  4544. 'Another First',
  4545. '/option',
  4546. '/select',
  4547. ];
  4548. $this->assertHtml($expected, $result);
  4549. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4550. $this->Form->create();
  4551. $result = $this->Form->select(
  4552. 'Model.contact_id',
  4553. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4554. ['escape' => false, 'empty' => 'pick something']
  4555. );
  4556. $expected = [
  4557. 'select' => ['name' => 'Model[contact_id]'],
  4558. ['option' => ['value' => '']], 'pick something', '/option',
  4559. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4560. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4561. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4562. '/select',
  4563. ];
  4564. $this->assertHtml($expected, $result);
  4565. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4566. $this->Form->create();
  4567. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4568. $expected = [
  4569. 'select' => ['name' => 'Model[field]'],
  4570. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4571. ['option' => ['value' => '1']], 'Yes', '/option',
  4572. '/select',
  4573. ];
  4574. $this->assertHtml($expected, $result);
  4575. $article = new Article([
  4576. 'status' => ArticleStatus::UNPUBLISHED,
  4577. ]);
  4578. $this->Form->create($article);
  4579. $result = $this->Form->select('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4580. $expected = [
  4581. 'select' => ['name' => 'status'],
  4582. ['option' => ['value' => 'Y']], 'Published', '/option',
  4583. ['option' => ['value' => 'N', 'selected' => 'selected']], 'Unpublished', '/option',
  4584. '/select',
  4585. ];
  4586. $this->assertHtml($expected, $result);
  4587. }
  4588. /**
  4589. * testSelectEscapeHtml method
  4590. *
  4591. * Test that select() escapes HTML.
  4592. */
  4593. public function testSelectEscapeHtml(): void
  4594. {
  4595. $result = $this->Form->select(
  4596. 'Model.field',
  4597. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4598. ['empty' => false]
  4599. );
  4600. $expected = [
  4601. 'select' => ['name' => 'Model[field]'],
  4602. ['option' => ['value' => 'first']],
  4603. 'first &quot;html&quot; &lt;chars&gt;',
  4604. '/option',
  4605. ['option' => ['value' => 'second']],
  4606. 'value',
  4607. '/option',
  4608. '/select',
  4609. ];
  4610. $this->assertHtml($expected, $result);
  4611. $result = $this->Form->select(
  4612. 'Model.field',
  4613. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4614. ['escape' => false, 'empty' => false]
  4615. );
  4616. $expected = [
  4617. 'select' => ['name' => 'Model[field]'],
  4618. ['option' => ['value' => 'first']],
  4619. 'first "html" <chars>',
  4620. '/option',
  4621. ['option' => ['value' => 'second']],
  4622. 'value',
  4623. '/option',
  4624. '/select',
  4625. ];
  4626. $this->assertHtml($expected, $result);
  4627. }
  4628. /**
  4629. * testSelectRequired method
  4630. *
  4631. * Test select() with required and disabled attributes.
  4632. */
  4633. public function testSelectRequired(): void
  4634. {
  4635. $this->article['required'] = [
  4636. 'user_id' => true,
  4637. ];
  4638. $this->Form->create($this->article);
  4639. $result = $this->Form->select('user_id', ['option A']);
  4640. $expected = [
  4641. 'select' => [
  4642. 'name' => 'user_id',
  4643. 'required' => 'required',
  4644. ],
  4645. ['option' => ['value' => '0']], 'option A', '/option',
  4646. '/select',
  4647. ];
  4648. $this->assertHtml($expected, $result);
  4649. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4650. $expected = [
  4651. 'select' => [
  4652. 'name' => 'user_id',
  4653. 'disabled' => 'disabled',
  4654. ],
  4655. ['option' => ['value' => '0']], 'option A', '/option',
  4656. '/select',
  4657. ];
  4658. $this->assertHtml($expected, $result);
  4659. }
  4660. public function testSelectEmptyWithRequiredFalse(): void
  4661. {
  4662. $Articles = $this->getTableLocator()->get('Articles');
  4663. $validator = $Articles->getValidator('default');
  4664. $validator->allowEmptyString('user_id');
  4665. $Articles->setValidator('default', $validator);
  4666. $entity = $Articles->newEmptyEntity();
  4667. $this->Form->create($entity);
  4668. $result = $this->Form->select('user_id', ['option A']);
  4669. $expected = [
  4670. 'select' => [
  4671. 'name' => 'user_id',
  4672. ],
  4673. ['option' => ['value' => '']], '/option',
  4674. ['option' => ['value' => '0']], 'option A', '/option',
  4675. '/select',
  4676. ];
  4677. $this->assertHtml($expected, $result);
  4678. }
  4679. /**
  4680. * testNestedSelect method
  4681. *
  4682. * Test select element generation with optgroups.
  4683. */
  4684. public function testNestedSelect(): void
  4685. {
  4686. $result = $this->Form->select(
  4687. 'Model.field',
  4688. [1 => 'One', 2 => 'Two', 'Three' => [
  4689. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4690. ]],
  4691. ['empty' => false]
  4692. );
  4693. $expected = [
  4694. 'select' => ['name' => 'Model[field]'],
  4695. ['option' => ['value' => 1]],
  4696. 'One',
  4697. '/option',
  4698. ['option' => ['value' => 2]],
  4699. 'Two',
  4700. '/option',
  4701. ['optgroup' => ['label' => 'Three']],
  4702. ['option' => ['value' => 3]],
  4703. 'Three',
  4704. '/option',
  4705. ['option' => ['value' => 4]],
  4706. 'Four',
  4707. '/option',
  4708. ['option' => ['value' => 5]],
  4709. 'Five',
  4710. '/option',
  4711. '/optgroup',
  4712. '/select',
  4713. ];
  4714. $this->assertHtml($expected, $result);
  4715. }
  4716. /**
  4717. * testSelectMultiple method
  4718. *
  4719. * Test generation of multiple select elements.
  4720. */
  4721. public function testSelectMultiple(): void
  4722. {
  4723. $options = ['first', 'second', 'third'];
  4724. $result = $this->Form->select(
  4725. 'Model.multi_field',
  4726. $options,
  4727. ['form' => 'my-form', 'multiple' => true]
  4728. );
  4729. $expected = [
  4730. 'input' => [
  4731. 'type' => 'hidden',
  4732. 'name' => 'Model[multi_field]',
  4733. 'value' => '',
  4734. 'form' => 'my-form',
  4735. ],
  4736. 'select' => [
  4737. 'name' => 'Model[multi_field][]',
  4738. 'multiple' => 'multiple',
  4739. 'form' => 'my-form',
  4740. ],
  4741. ['option' => ['value' => '0']],
  4742. 'first',
  4743. '/option',
  4744. ['option' => ['value' => '1']],
  4745. 'second',
  4746. '/option',
  4747. ['option' => ['value' => '2']],
  4748. 'third',
  4749. '/option',
  4750. '/select',
  4751. ];
  4752. $this->assertHtml($expected, $result);
  4753. $result = $this->Form->select(
  4754. 'Model.multi_field',
  4755. $options,
  4756. ['multiple' => 'multiple', 'form' => 'my-form']
  4757. );
  4758. $this->assertHtml($expected, $result);
  4759. $result = $this->Form->select(
  4760. 'Model.multi_field',
  4761. $options,
  4762. ['form' => 'my-form', 'multiple' => false]
  4763. );
  4764. $this->assertStringNotContainsString('multiple', $result);
  4765. }
  4766. /**
  4767. * testCheckboxZeroValue method
  4768. *
  4769. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4770. */
  4771. public function testCheckboxZeroValue(): void
  4772. {
  4773. $result = $this->Form->control('User.get_spam', [
  4774. 'type' => 'checkbox',
  4775. 'value' => '0',
  4776. 'hiddenField' => '1',
  4777. ]);
  4778. $expected = [
  4779. 'div' => ['class' => 'input checkbox'],
  4780. 'label' => ['for' => 'user-get-spam'],
  4781. ['input' => [
  4782. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4783. 'value' => '1',
  4784. ]],
  4785. ['input' => [
  4786. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4787. 'value' => '0', 'id' => 'user-get-spam',
  4788. ]],
  4789. 'Get Spam',
  4790. '/label',
  4791. '/div',
  4792. ];
  4793. $this->assertHtml($expected, $result);
  4794. $result = $this->Form->control('User.get_spam', [
  4795. 'type' => 'checkbox',
  4796. 'value' => '0',
  4797. 'hiddenField' => '',
  4798. ]);
  4799. $expected = [
  4800. 'div' => ['class' => 'input checkbox'],
  4801. 'label' => ['for' => 'user-get-spam'],
  4802. ['input' => [
  4803. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4804. 'value' => '',
  4805. ]],
  4806. ['input' => [
  4807. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4808. 'value' => '0', 'id' => 'user-get-spam',
  4809. ]],
  4810. 'Get Spam',
  4811. '/label',
  4812. '/div',
  4813. ];
  4814. $this->assertHtml($expected, $result);
  4815. }
  4816. /**
  4817. * testHabtmSelectBox method
  4818. *
  4819. * Test generation of habtm select boxes.
  4820. */
  4821. public function testHabtmSelectBox(): void
  4822. {
  4823. $options = [
  4824. 1 => 'blue',
  4825. 2 => 'red',
  4826. 3 => 'green',
  4827. ];
  4828. $tags = [
  4829. new Entity(['id' => 1, 'name' => 'blue']),
  4830. new Entity(['id' => 3, 'name' => 'green']),
  4831. ];
  4832. $article = new Article(['tags' => $tags]);
  4833. $this->Form->create($article);
  4834. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4835. $expected = [
  4836. 'div' => ['class' => 'input select'],
  4837. 'label' => ['for' => 'tags-ids'],
  4838. 'Tags',
  4839. '/label',
  4840. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4841. 'select' => [
  4842. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4843. 'multiple' => 'multiple',
  4844. ],
  4845. ['option' => ['value' => '1', 'selected' => 'selected']],
  4846. 'blue',
  4847. '/option',
  4848. ['option' => ['value' => '2']],
  4849. 'red',
  4850. '/option',
  4851. ['option' => ['value' => '3', 'selected' => 'selected']],
  4852. 'green',
  4853. '/option',
  4854. '/select',
  4855. '/div',
  4856. ];
  4857. $this->assertHtml($expected, $result);
  4858. // make sure only 50 is selected, and not 50f5c0cf
  4859. $options = [
  4860. '1' => 'blue',
  4861. '50f5c0cf' => 'red',
  4862. '50' => 'green',
  4863. ];
  4864. $tags = [
  4865. new Entity(['id' => 1, 'name' => 'blue']),
  4866. new Entity(['id' => 50, 'name' => 'green']),
  4867. ];
  4868. $article = new Article(['tags' => $tags]);
  4869. $this->Form->create($article);
  4870. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4871. $expected = [
  4872. 'div' => ['class' => 'input select'],
  4873. 'label' => ['for' => 'tags-ids'],
  4874. 'Tags',
  4875. '/label',
  4876. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4877. 'select' => [
  4878. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4879. 'multiple' => 'multiple',
  4880. ],
  4881. ['option' => ['value' => '1', 'selected' => 'selected']],
  4882. 'blue',
  4883. '/option',
  4884. ['option' => ['value' => '50f5c0cf']],
  4885. 'red',
  4886. '/option',
  4887. ['option' => ['value' => '50', 'selected' => 'selected']],
  4888. 'green',
  4889. '/option',
  4890. '/select',
  4891. '/div',
  4892. ];
  4893. $this->assertHtml($expected, $result);
  4894. $spacecraft = [
  4895. 1 => 'Orion',
  4896. 2 => 'Helios',
  4897. ];
  4898. $this->View->set('spacecraft', $spacecraft);
  4899. $this->Form->create();
  4900. $result = $this->Form->control('spacecraft._ids');
  4901. $expected = [
  4902. 'div' => ['class' => 'input select'],
  4903. 'label' => ['for' => 'spacecraft-ids'],
  4904. 'Spacecraft',
  4905. '/label',
  4906. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4907. 'select' => [
  4908. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4909. 'multiple' => 'multiple',
  4910. ],
  4911. ['option' => ['value' => '1']],
  4912. 'Orion',
  4913. '/option',
  4914. ['option' => ['value' => '2']],
  4915. 'Helios',
  4916. '/option',
  4917. '/select',
  4918. '/div',
  4919. ];
  4920. $this->assertHtml($expected, $result);
  4921. }
  4922. /**
  4923. * testErrorsForBelongsToManySelect method
  4924. *
  4925. * Tests that errors for belongsToMany select fields are being
  4926. * picked up properly.
  4927. */
  4928. public function testErrorsForBelongsToManySelect(): void
  4929. {
  4930. $spacecraft = [
  4931. 1 => 'Orion',
  4932. 2 => 'Helios',
  4933. ];
  4934. $this->View->set('spacecraft', $spacecraft);
  4935. $article = new Article();
  4936. $article->setError('spacecraft', ['Invalid']);
  4937. $this->Form->create($article);
  4938. $result = $this->Form->control('spacecraft._ids');
  4939. $expected = [
  4940. ['div' => ['class' => 'input select error']],
  4941. 'label' => ['for' => 'spacecraft-ids'],
  4942. 'Spacecraft',
  4943. '/label',
  4944. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4945. 'select' => [
  4946. 'name' => 'spacecraft[_ids][]',
  4947. 'id' => 'spacecraft-ids',
  4948. 'multiple' => 'multiple',
  4949. ],
  4950. ['option' => ['value' => '1']],
  4951. 'Orion',
  4952. '/option',
  4953. ['option' => ['value' => '2']],
  4954. 'Helios',
  4955. '/option',
  4956. '/select',
  4957. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4958. 'Invalid',
  4959. '/div',
  4960. '/div',
  4961. ];
  4962. $this->assertHtml($expected, $result);
  4963. }
  4964. /**
  4965. * testSelectMultipleCheckboxes method
  4966. *
  4967. * Test generation of multi select elements in checkbox format.
  4968. */
  4969. public function testSelectMultipleCheckboxes(): void
  4970. {
  4971. $result = $this->Form->select(
  4972. 'Model.multi_field',
  4973. ['first', 'second', 'third'],
  4974. ['multiple' => 'checkbox']
  4975. );
  4976. $expected = [
  4977. 'input' => [
  4978. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4979. ],
  4980. ['div' => ['class' => 'checkbox']],
  4981. ['label' => ['for' => 'model-multi-field-0']],
  4982. ['input' => [
  4983. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4984. 'value' => '0', 'id' => 'model-multi-field-0',
  4985. ]],
  4986. 'first',
  4987. '/label',
  4988. '/div',
  4989. ['div' => ['class' => 'checkbox']],
  4990. ['label' => ['for' => 'model-multi-field-1']],
  4991. ['input' => [
  4992. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4993. 'value' => '1', 'id' => 'model-multi-field-1',
  4994. ]],
  4995. 'second',
  4996. '/label',
  4997. '/div',
  4998. ['div' => ['class' => 'checkbox']],
  4999. ['label' => ['for' => 'model-multi-field-2']],
  5000. ['input' => [
  5001. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5002. 'value' => '2', 'id' => 'model-multi-field-2',
  5003. ]],
  5004. 'third',
  5005. '/label',
  5006. '/div',
  5007. ];
  5008. $this->assertHtml($expected, $result);
  5009. $result = $this->Form->select(
  5010. 'Model.multi_field',
  5011. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5012. ['multiple' => 'checkbox']
  5013. );
  5014. $expected = [
  5015. 'input' => [
  5016. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5017. ],
  5018. ['div' => ['class' => 'checkbox']],
  5019. ['label' => ['for' => 'model-multi-field-a+']],
  5020. ['input' => [
  5021. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5022. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5023. ]],
  5024. 'first',
  5025. '/label',
  5026. '/div',
  5027. ['div' => ['class' => 'checkbox']],
  5028. ['label' => ['for' => 'model-multi-field-a++']],
  5029. ['input' => [
  5030. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5031. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5032. ]],
  5033. 'second',
  5034. '/label',
  5035. '/div',
  5036. ['div' => ['class' => 'checkbox']],
  5037. ['label' => ['for' => 'model-multi-field-a+++']],
  5038. ['input' => [
  5039. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5040. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5041. ]],
  5042. 'third',
  5043. '/label',
  5044. '/div',
  5045. ];
  5046. $this->assertHtml($expected, $result);
  5047. $result = $this->Form->select(
  5048. 'Model.multi_field',
  5049. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5050. ['multiple' => 'checkbox']
  5051. );
  5052. $expected = [
  5053. 'input' => [
  5054. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5055. ],
  5056. ['div' => ['class' => 'checkbox']],
  5057. ['label' => ['for' => 'model-multi-field-a-b']],
  5058. ['input' => [
  5059. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5060. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5061. ]],
  5062. 'first',
  5063. '/label',
  5064. '/div',
  5065. ['div' => ['class' => 'checkbox']],
  5066. ['label' => ['for' => 'model-multi-field-a-b1']],
  5067. ['input' => [
  5068. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5069. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5070. ]],
  5071. 'second',
  5072. '/label',
  5073. '/div',
  5074. ['div' => ['class' => 'checkbox']],
  5075. ['label' => ['for' => 'model-multi-field-a-b2']],
  5076. ['input' => [
  5077. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5078. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5079. ]],
  5080. 'third',
  5081. '/label',
  5082. '/div',
  5083. ];
  5084. $this->assertHtml($expected, $result);
  5085. }
  5086. /**
  5087. * testSelectMultipleCheckboxRequestData method
  5088. *
  5089. * Ensure that multiCheckbox reads from the request data.
  5090. */
  5091. public function testSelectMultipleCheckboxRequestData(): void
  5092. {
  5093. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5094. $result = $this->Form->select(
  5095. 'Model.tags',
  5096. ['1' => 'first', 'Array' => 'Array'],
  5097. ['multiple' => 'checkbox']
  5098. );
  5099. $expected = [
  5100. 'input' => [
  5101. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  5102. ],
  5103. ['div' => ['class' => 'checkbox']],
  5104. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5105. ['input' => [
  5106. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5107. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5108. ]],
  5109. 'first',
  5110. '/label',
  5111. '/div',
  5112. ['div' => ['class' => 'checkbox']],
  5113. ['label' => ['for' => 'model-tags-array']],
  5114. ['input' => [
  5115. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5116. 'value' => 'Array', 'id' => 'model-tags-array',
  5117. ]],
  5118. 'Array',
  5119. '/label',
  5120. '/div',
  5121. ];
  5122. $this->assertHtml($expected, $result);
  5123. }
  5124. /**
  5125. * testSelectMultipleCheckboxSecurity method
  5126. *
  5127. * Checks the security hash array generated for multiple-input checkbox elements.
  5128. */
  5129. public function testSelectMultipleCheckboxSecurity(): void
  5130. {
  5131. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5132. $this->Form->create();
  5133. $this->Form->select(
  5134. 'Model.multi_field',
  5135. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5136. ['multiple' => 'checkbox']
  5137. );
  5138. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5139. $this->assertEquals(['Model.multi_field'], $fields);
  5140. $result = $this->Form->secure();
  5141. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5142. $hash = urlencode($hash . ':');
  5143. $this->assertStringContainsString('"' . $hash . '"', $result);
  5144. }
  5145. /**
  5146. * testSelectMultipleSecureWithNoOptions method
  5147. *
  5148. * Multiple select elements should always be secured as they always participate
  5149. * in the POST data.
  5150. */
  5151. public function testSelectMultipleSecureWithNoOptions(): void
  5152. {
  5153. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5154. $this->Form->create();
  5155. $this->Form->select(
  5156. 'Model.select',
  5157. [],
  5158. ['multiple' => true]
  5159. );
  5160. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5161. $this->assertEquals(['Model.select'], $result);
  5162. }
  5163. /**
  5164. * testSelectNoSecureWithNoOptions method
  5165. *
  5166. * When a select box has no options it should not be added to the fields list
  5167. * as it always fail post validation.
  5168. */
  5169. public function testSelectNoSecureWithNoOptions(): void
  5170. {
  5171. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5172. $this->Form->create();
  5173. $this->Form->select(
  5174. 'Model.select',
  5175. []
  5176. );
  5177. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5178. $this->assertEquals([], $result);
  5179. $this->Form->select(
  5180. 'Model.user_id',
  5181. [],
  5182. ['empty' => true]
  5183. );
  5184. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5185. $this->assertEquals(['Model.user_id'], $result);
  5186. }
  5187. /**
  5188. * testControlMultipleCheckboxes method
  5189. *
  5190. * Test control() resulting in multi select elements being generated.
  5191. */
  5192. public function testControlMultipleCheckboxes(): void
  5193. {
  5194. $result = $this->Form->control('Model.multi_field', [
  5195. 'options' => ['first', 'second', 'third'],
  5196. 'multiple' => 'checkbox',
  5197. ]);
  5198. $expected = [
  5199. ['div' => ['class' => 'input select']],
  5200. ['label' => ['for' => 'model-multi-field']],
  5201. 'Multi Field',
  5202. '/label',
  5203. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5204. ['div' => ['class' => 'checkbox']],
  5205. ['label' => ['for' => 'model-multi-field-0']],
  5206. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5207. 'first',
  5208. '/label',
  5209. '/div',
  5210. ['div' => ['class' => 'checkbox']],
  5211. ['label' => ['for' => 'model-multi-field-1']],
  5212. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5213. 'second',
  5214. '/label',
  5215. '/div',
  5216. ['div' => ['class' => 'checkbox']],
  5217. ['label' => ['for' => 'model-multi-field-2']],
  5218. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5219. 'third',
  5220. '/label',
  5221. '/div',
  5222. '/div',
  5223. ];
  5224. $this->assertHtml($expected, $result);
  5225. $result = $this->Form->control('Model.multi_field', [
  5226. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5227. 'multiple' => 'checkbox',
  5228. ]);
  5229. $expected = [
  5230. ['div' => ['class' => 'input select']],
  5231. ['label' => ['for' => 'model-multi-field']],
  5232. 'Multi Field',
  5233. '/label',
  5234. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5235. ['div' => ['class' => 'checkbox']],
  5236. ['label' => ['for' => 'model-multi-field-a']],
  5237. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5238. 'first',
  5239. '/label',
  5240. '/div',
  5241. ['div' => ['class' => 'checkbox']],
  5242. ['label' => ['for' => 'model-multi-field-b']],
  5243. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5244. 'second',
  5245. '/label',
  5246. '/div',
  5247. ['div' => ['class' => 'checkbox']],
  5248. ['label' => ['for' => 'model-multi-field-c']],
  5249. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5250. 'third',
  5251. '/label',
  5252. '/div',
  5253. '/div',
  5254. ];
  5255. $this->assertHtml($expected, $result);
  5256. }
  5257. /**
  5258. * testSelectHiddenFieldOmission method
  5259. *
  5260. * Test that select() with 'hiddenField' => false omits the hidden field.
  5261. */
  5262. public function testSelectHiddenFieldOmission(): void
  5263. {
  5264. $result = $this->Form->select(
  5265. 'Model.multi_field',
  5266. ['first', 'second'],
  5267. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5268. );
  5269. $this->assertStringNotContainsString('type="hidden"', $result);
  5270. }
  5271. /**
  5272. * testSelectCheckboxMultipleOverrideName method
  5273. *
  5274. * Test that select() with multiple = checkbox works with overriding name attribute.
  5275. */
  5276. public function testSelectCheckboxMultipleOverrideName(): void
  5277. {
  5278. $result = $this->Form->select('category', ['1', '2'], [
  5279. 'multiple' => 'checkbox',
  5280. 'name' => 'fish',
  5281. ]);
  5282. $expected = [
  5283. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5284. ['div' => ['class' => 'checkbox']],
  5285. ['label' => ['for' => 'fish-0']],
  5286. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5287. '1',
  5288. '/label',
  5289. '/div',
  5290. ['div' => ['class' => 'checkbox']],
  5291. ['label' => ['for' => 'fish-1']],
  5292. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5293. '2',
  5294. '/label',
  5295. '/div',
  5296. ];
  5297. $this->assertHtml($expected, $result);
  5298. $result = $this->Form->multiCheckbox(
  5299. 'category',
  5300. new Collection(['1', '2']),
  5301. ['name' => 'fish']
  5302. );
  5303. $this->assertHtml($expected, $result);
  5304. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5305. 'name' => 'fish',
  5306. ]);
  5307. $this->assertHtml($expected, $result);
  5308. }
  5309. /**
  5310. * testSelectCheckboxMultipleOverrideName method
  5311. *
  5312. * Test that select() with multiple = checkbox works with overriding name attribute.
  5313. */
  5314. public function testSelectCheckboxMultipleCustomId(): void
  5315. {
  5316. $result = $this->Form->select('category', ['1', '2'], [
  5317. 'multiple' => 'checkbox',
  5318. 'id' => 'cat',
  5319. ]);
  5320. $expected = [
  5321. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5322. ['div' => ['class' => 'checkbox']],
  5323. ['label' => ['for' => 'cat-0']],
  5324. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5325. '1',
  5326. '/label',
  5327. '/div',
  5328. ['div' => ['class' => 'checkbox']],
  5329. ['label' => ['for' => 'cat-1']],
  5330. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5331. '2',
  5332. '/label',
  5333. '/div',
  5334. ];
  5335. $this->assertHtml($expected, $result);
  5336. $result = $this->Form->multiCheckbox(
  5337. 'category',
  5338. ['1', '2'],
  5339. ['id' => 'cat']
  5340. );
  5341. $this->assertHtml($expected, $result);
  5342. }
  5343. /**
  5344. * testControlMultiCheckbox method
  5345. *
  5346. * Test that control() works with multicheckbox.
  5347. */
  5348. public function testControlMultiCheckbox(): void
  5349. {
  5350. $result = $this->Form->control('category', [
  5351. 'type' => 'multicheckbox',
  5352. 'options' => ['1', '2'],
  5353. ]);
  5354. $expected = [
  5355. ['div' => ['class' => 'input multicheckbox']],
  5356. '<label',
  5357. 'Category',
  5358. '/label',
  5359. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5360. ['div' => ['class' => 'checkbox']],
  5361. ['label' => ['for' => 'category-0']],
  5362. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5363. '1',
  5364. '/label',
  5365. '/div',
  5366. ['div' => ['class' => 'checkbox']],
  5367. ['label' => ['for' => 'category-1']],
  5368. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5369. '2',
  5370. '/label',
  5371. '/div',
  5372. '/div',
  5373. ];
  5374. $this->assertHtml($expected, $result);
  5375. }
  5376. /**
  5377. * testCheckbox method
  5378. *
  5379. * Test generation of checkboxes.
  5380. */
  5381. public function testCheckbox(): void
  5382. {
  5383. $result = $this->Form->checkbox('Model.field');
  5384. $expected = [
  5385. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5386. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5387. ];
  5388. $this->assertHtml($expected, $result);
  5389. $result = $this->Form->checkbox('Model.field', [
  5390. 'id' => 'theID',
  5391. 'value' => 'myvalue',
  5392. 'form' => 'my-form',
  5393. ]);
  5394. $expected = [
  5395. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5396. ['input' => [
  5397. 'type' => 'checkbox', 'name' => 'Model[field]',
  5398. 'value' => 'myvalue', 'id' => 'theID',
  5399. 'form' => 'my-form',
  5400. ]],
  5401. ];
  5402. $this->assertHtml($expected, $result);
  5403. }
  5404. /**
  5405. * testCheckboxDefaultValue method
  5406. *
  5407. * Test default value setting on checkbox() method.
  5408. */
  5409. public function testCheckboxDefaultValue(): void
  5410. {
  5411. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5412. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5413. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5414. $this->assertHtml($expected, $result);
  5415. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5416. $this->Form->create();
  5417. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5418. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5419. $this->assertHtml($expected, $result);
  5420. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5421. $this->Form->create();
  5422. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5423. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5424. $this->assertHtml($expected, $result);
  5425. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5426. $this->Form->create();
  5427. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5428. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5429. $this->assertHtml($expected, $result);
  5430. $Articles = $this->getTableLocator()->get('Articles');
  5431. $Articles->getSchema()->addColumn(
  5432. 'published',
  5433. ['type' => 'boolean', 'null' => false, 'default' => true]
  5434. );
  5435. $this->Form->create($Articles->newEmptyEntity());
  5436. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5437. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5438. $this->assertHtml($expected, $result);
  5439. }
  5440. /**
  5441. * testCheckboxCheckedAndError method
  5442. *
  5443. * Test checkbox being checked or having errors.
  5444. */
  5445. public function testCheckboxCheckedAndError(): void
  5446. {
  5447. $this->article['errors'] = [
  5448. 'published' => true,
  5449. ];
  5450. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5451. $this->Form->create($this->article);
  5452. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5453. $expected = [
  5454. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5455. ['input' => [
  5456. 'type' => 'checkbox',
  5457. 'name' => 'published',
  5458. 'value' => 'myvalue',
  5459. 'id' => 'theID',
  5460. 'checked' => 'checked',
  5461. 'class' => 'form-error',
  5462. ]],
  5463. ];
  5464. $this->assertHtml($expected, $result);
  5465. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5466. $this->Form->create($this->article);
  5467. $result = $this->Form->checkbox('published');
  5468. $expected = [
  5469. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5470. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5471. ];
  5472. $this->assertHtml($expected, $result);
  5473. }
  5474. /**
  5475. * testCheckboxCustomNameAttribute method
  5476. *
  5477. * Test checkbox() with a custom name attribute.
  5478. */
  5479. public function testCheckboxCustomNameAttribute(): void
  5480. {
  5481. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5482. $expected = [
  5483. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5484. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5485. ];
  5486. $this->assertHtml($expected, $result);
  5487. }
  5488. /**
  5489. * testCheckboxHiddenField method
  5490. *
  5491. * Test that the hidden input for checkboxes can be omitted or set to a
  5492. * specific value.
  5493. */
  5494. public function testCheckboxHiddenField(): void
  5495. {
  5496. $result = $this->Form->checkbox('UserForm.something', [
  5497. 'hiddenField' => false,
  5498. ]);
  5499. $expected = [
  5500. 'input' => [
  5501. 'type' => 'checkbox',
  5502. 'name' => 'UserForm[something]',
  5503. 'value' => '1',
  5504. ],
  5505. ];
  5506. $this->assertHtml($expected, $result);
  5507. $result = $this->Form->checkbox('UserForm.something', [
  5508. 'value' => 'Y',
  5509. 'hiddenField' => '',
  5510. ]);
  5511. $expected = [
  5512. ['input' => [
  5513. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5514. 'value' => '',
  5515. ]],
  5516. ['input' => [
  5517. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5518. 'value' => 'Y',
  5519. ]],
  5520. ];
  5521. $this->assertHtml($expected, $result);
  5522. $result = $this->Form->checkbox('UserForm.something', [
  5523. 'value' => 'Y',
  5524. 'hiddenField' => 'N',
  5525. ]);
  5526. $expected = [
  5527. ['input' => [
  5528. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5529. 'value' => 'N',
  5530. ]],
  5531. ['input' => [
  5532. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5533. 'value' => 'Y',
  5534. ]],
  5535. ];
  5536. $this->assertHtml($expected, $result);
  5537. }
  5538. /**
  5539. * testTime method
  5540. *
  5541. * Test the time type.
  5542. */
  5543. public function testTime(): void
  5544. {
  5545. $result = $this->Form->time('start_time', [
  5546. 'value' => '2014-03-08 16:30:00',
  5547. ]);
  5548. $expected = [
  5549. 'input' => [
  5550. 'type' => 'time',
  5551. 'name' => 'start_time',
  5552. 'value' => '16:30:00',
  5553. 'step' => '1',
  5554. ],
  5555. ];
  5556. $this->assertHtml($expected, $result);
  5557. }
  5558. /**
  5559. * testDate method
  5560. *
  5561. * Test the date type.
  5562. */
  5563. public function testDate(): void
  5564. {
  5565. $result = $this->Form->date('start_day', [
  5566. 'value' => '2014-03-08',
  5567. ]);
  5568. $expected = [
  5569. 'input' => [
  5570. 'type' => 'date',
  5571. 'name' => 'start_day',
  5572. 'value' => '2014-03-08',
  5573. ],
  5574. ];
  5575. $this->assertHtml($expected, $result);
  5576. $result = $this->Form->date('start_day', [
  5577. 'value' => new Date('2014-03-08'),
  5578. ]);
  5579. $this->assertHtml($expected, $result);
  5580. }
  5581. /**
  5582. * testDateTime method
  5583. */
  5584. public function testDateTime(): void
  5585. {
  5586. $result = $this->Form->dateTime('date', ['default' => true]);
  5587. $expected = [
  5588. 'input' => [
  5589. 'type' => 'datetime-local',
  5590. 'name' => 'date',
  5591. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5592. 'step' => '1',
  5593. ],
  5594. ];
  5595. $this->assertHtml($expected, $result);
  5596. }
  5597. /**
  5598. * testDateTimeSecured method
  5599. *
  5600. * Test that datetime fields are added to protected fields list.
  5601. */
  5602. public function testDateTimeSecured(): void
  5603. {
  5604. $this->View->setRequest(
  5605. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5606. );
  5607. $this->Form->create();
  5608. $this->Form->dateTime('date');
  5609. $expected = ['date'];
  5610. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5611. $this->assertEquals($expected, $result);
  5612. $this->Form->date('published');
  5613. $expected = ['date', 'published'];
  5614. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5615. $this->assertEquals($expected, $result);
  5616. }
  5617. /**
  5618. * testDateTimeSecuredDisabled method
  5619. *
  5620. * Test that datetime fields are added to protected fields list.
  5621. */
  5622. public function testDateTimeSecuredDisabled(): void
  5623. {
  5624. $this->View->setRequest(
  5625. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5626. );
  5627. $this->Form->create();
  5628. $this->Form->dateTime('date', ['secure' => false]);
  5629. $expected = [];
  5630. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5631. $this->assertEquals($expected, $result);
  5632. $this->Form->date('published', ['secure' => false]);
  5633. $expected = [];
  5634. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5635. $this->assertEquals($expected, $result);
  5636. }
  5637. /**
  5638. * testDatetimeWithDefault method
  5639. *
  5640. * Test that datetime() and default values work.
  5641. */
  5642. public function testDatetimeWithDefault(): void
  5643. {
  5644. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5645. $expected = [
  5646. 'input' => [
  5647. 'type' => 'datetime-local',
  5648. 'name' => 'updated',
  5649. 'value' => '2009-06-01T11:15:30',
  5650. 'step' => '1',
  5651. ],
  5652. ];
  5653. $this->assertHtml($expected, $result);
  5654. $result = $this->Form->dateTime('updated', [
  5655. 'default' => '2009-06-01 11:15:30',
  5656. ]);
  5657. $expected = [
  5658. 'input' => [
  5659. 'type' => 'datetime-local',
  5660. 'name' => 'updated',
  5661. 'value' => '2009-06-01T11:15:30',
  5662. 'step' => '1',
  5663. ],
  5664. ];
  5665. $this->assertHtml($expected, $result);
  5666. }
  5667. /**
  5668. * testMonth method
  5669. *
  5670. * Test generation of a month input.
  5671. */
  5672. public function testMonth(): void
  5673. {
  5674. $result = $this->Form->month('field', ['value' => '']);
  5675. $expected = [
  5676. 'input' => [
  5677. 'type' => 'month',
  5678. 'name' => 'field',
  5679. 'value' => '',
  5680. ],
  5681. ];
  5682. $this->assertHtml($expected, $result);
  5683. $this->View->setRequest(
  5684. $this->View->getRequest()->withData('release', '2050-02-10')
  5685. );
  5686. $this->Form->create();
  5687. $result = $this->Form->month('release');
  5688. $expected = [
  5689. 'input' => [
  5690. 'type' => 'month',
  5691. 'name' => 'release',
  5692. 'value' => '2050-02',
  5693. ],
  5694. ];
  5695. $this->assertHtml($expected, $result);
  5696. $this->View->setRequest(
  5697. $this->View->getRequest()->withData('release', '2050-03')
  5698. );
  5699. $this->Form->create();
  5700. $result = $this->Form->month('release');
  5701. $expected = [
  5702. 'input' => [
  5703. 'type' => 'month',
  5704. 'name' => 'release',
  5705. 'value' => '2050-03',
  5706. ],
  5707. ];
  5708. $this->assertHtml($expected, $result);
  5709. }
  5710. /**
  5711. * testYear method
  5712. *
  5713. * Test generation of a year input.
  5714. */
  5715. public function testYear(): void
  5716. {
  5717. $this->View->setRequest(
  5718. $this->View->getRequest()->withData('published', '2006')
  5719. );
  5720. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5721. $expected = [
  5722. ['select' => ['name' => 'field']],
  5723. ['option' => ['selected' => 'selected', 'value' => '']],
  5724. '/option',
  5725. ['option' => ['value' => '2007']],
  5726. '2007',
  5727. '/option',
  5728. ['option' => ['value' => '2006']],
  5729. '2006',
  5730. '/option',
  5731. '/select',
  5732. ];
  5733. $this->assertHtml($expected, $result);
  5734. $result = $this->Form->year('field', [
  5735. 'value' => '',
  5736. 'min' => 2006,
  5737. 'max' => 2007,
  5738. 'order' => 'asc',
  5739. ]);
  5740. $expected = [
  5741. ['select' => ['name' => 'field']],
  5742. ['option' => ['selected' => 'selected', 'value' => '']],
  5743. '/option',
  5744. ['option' => ['value' => '2006']],
  5745. '2006',
  5746. '/option',
  5747. ['option' => ['value' => '2007']],
  5748. '2007',
  5749. '/option',
  5750. '/select',
  5751. ];
  5752. $this->assertHtml($expected, $result);
  5753. $result = $this->Form->year('published', [
  5754. 'empty' => false,
  5755. 'min' => 2006,
  5756. 'max' => 2007,
  5757. ]);
  5758. $expected = [
  5759. ['select' => ['name' => 'published']],
  5760. ['option' => ['value' => '2007']],
  5761. '2007',
  5762. '/option',
  5763. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5764. '2006',
  5765. '/option',
  5766. '/select',
  5767. ];
  5768. $this->assertHtml($expected, $result);
  5769. $result = $this->Form->year('published', [
  5770. 'empty' => false,
  5771. 'value' => new Date('2008-01-12'),
  5772. 'min' => 2007,
  5773. 'max' => 2009,
  5774. ]);
  5775. $expected = [
  5776. ['select' => ['name' => 'published']],
  5777. ['option' => ['value' => '2009']],
  5778. '2009',
  5779. '/option',
  5780. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5781. '2008',
  5782. '/option',
  5783. ['option' => ['value' => '2007']],
  5784. '2007',
  5785. '/option',
  5786. '/select',
  5787. ];
  5788. $this->assertHtml($expected, $result);
  5789. $result = $this->Form->year('published', [
  5790. 'empty' => 'Published on',
  5791. ]);
  5792. $this->assertStringContainsString('Published on', $result);
  5793. }
  5794. /**
  5795. * testControlYearPreEpoch method
  5796. *
  5797. * Test minYear being prior to the unix epoch.
  5798. */
  5799. public function testControlYearPreEpoch(): void
  5800. {
  5801. $start = date('Y') - 80;
  5802. $end = date('Y') - 18;
  5803. $result = $this->Form->control('birth_year', [
  5804. 'type' => 'year',
  5805. 'label' => 'Birth Year',
  5806. 'min' => $start,
  5807. 'max' => $end,
  5808. ]);
  5809. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5810. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5811. $this->assertStringNotContainsString('value="00">00', $result);
  5812. }
  5813. /**
  5814. * test control() datetime & required attributes
  5815. */
  5816. public function testControlDatetimeRequired(): void
  5817. {
  5818. $result = $this->Form->control('birthday', [
  5819. 'type' => 'date',
  5820. 'required' => true,
  5821. ]);
  5822. $this->assertStringContainsString(
  5823. '<input type="date" name="birthday" required="required"',
  5824. $result
  5825. );
  5826. }
  5827. /**
  5828. * testYearAutoExpandRange method
  5829. */
  5830. public function testYearAutoExpandRange(): void
  5831. {
  5832. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5833. $result = $this->Form->year('birthday');
  5834. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5835. $result = $matches[1];
  5836. $expected = range(date('Y') + 5, 1930);
  5837. $this->assertEquals($expected, $result);
  5838. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5839. $this->Form->create();
  5840. $result = $this->Form->year('release');
  5841. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5842. $result = $matches[1];
  5843. $expected = range(2050, date('Y') - 5);
  5844. $this->assertEquals($expected, $result);
  5845. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5846. $this->Form->create();
  5847. $result = $this->Form->year('release', [
  5848. 'min' => 1890,
  5849. 'max' => 1900,
  5850. ]);
  5851. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5852. $result = $matches[1];
  5853. $expected = range(1900, 1881);
  5854. $this->assertEquals($expected, $result);
  5855. }
  5856. /**
  5857. * test control placeholder + label
  5858. */
  5859. public function testControlLabelAndPlaceholder(): void
  5860. {
  5861. $this->Form->create($this->article);
  5862. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5863. $expected = [
  5864. 'div' => ['class' => 'input text required'],
  5865. 'label' => ['for' => 'title'],
  5866. 'Title',
  5867. '/label',
  5868. 'input' => [
  5869. 'aria-required' => 'true',
  5870. 'type' => 'text',
  5871. 'required' => 'required',
  5872. 'placeholder' => 'Add title',
  5873. 'id' => 'title',
  5874. 'name' => 'title',
  5875. 'data-validity-message' => 'This field cannot be left empty',
  5876. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5877. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5878. ],
  5879. '/div',
  5880. ];
  5881. $this->assertHtml($expected, $result);
  5882. }
  5883. /**
  5884. * testControlLabelFalse method
  5885. *
  5886. * Test the label option being set to false.
  5887. */
  5888. public function testControlLabelFalse(): void
  5889. {
  5890. $this->Form->create($this->article);
  5891. $result = $this->Form->control('title', ['label' => false]);
  5892. $expected = [
  5893. 'div' => ['class' => 'input text required'],
  5894. 'input' => [
  5895. 'aria-required' => 'true',
  5896. 'type' => 'text',
  5897. 'required' => 'required',
  5898. 'id' => 'title',
  5899. 'name' => 'title',
  5900. 'data-validity-message' => 'This field cannot be left empty',
  5901. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5902. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5903. ],
  5904. '/div',
  5905. ];
  5906. $this->assertHtml($expected, $result);
  5907. $this->Form->create($this->article);
  5908. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5909. $expected['input'] += [
  5910. 'placeholder' => 'Add title',
  5911. 'aria-label' => 'Add title',
  5912. ];
  5913. $this->assertHtml($expected, $result);
  5914. }
  5915. /**
  5916. * testTextArea method
  5917. *
  5918. * Test generation of a textarea input.
  5919. */
  5920. public function testTextArea(): void
  5921. {
  5922. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5923. $result = $this->Form->textarea('field');
  5924. $expected = [
  5925. 'textarea' => ['name' => 'field', 'rows' => 5],
  5926. 'some test data',
  5927. '/textarea',
  5928. ];
  5929. $this->assertHtml($expected, $result);
  5930. $result = $this->Form->textarea('user.bio');
  5931. $expected = [
  5932. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5933. '/textarea',
  5934. ];
  5935. $this->assertHtml($expected, $result);
  5936. $this->View->setRequest($this->View->getRequest()
  5937. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5938. $this->Form->create();
  5939. $result = $this->Form->textarea('field');
  5940. $expected = [
  5941. 'textarea' => ['name' => 'field', 'rows' => 5],
  5942. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5943. '/textarea',
  5944. ];
  5945. $this->assertHtml($expected, $result);
  5946. $this->View->setRequest($this->View->getRequest()->withData(
  5947. 'Model.field',
  5948. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5949. ));
  5950. $this->Form->create();
  5951. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5952. $expected = [
  5953. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5954. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5955. '/textarea',
  5956. ];
  5957. $this->assertHtml($expected, $result);
  5958. $result = $this->Form->textarea('0.OtherModel.field');
  5959. $expected = [
  5960. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5961. '/textarea',
  5962. ];
  5963. $this->assertHtml($expected, $result);
  5964. }
  5965. /**
  5966. * testTextAreaWithStupidCharacters method
  5967. *
  5968. * Test text area with non-ascii characters.
  5969. */
  5970. public function testTextAreaWithStupidCharacters(): void
  5971. {
  5972. $result = $this->Form->textarea('Post.content', [
  5973. 'value' => 'GREAT®',
  5974. 'rows' => '15',
  5975. 'cols' => '75',
  5976. ]);
  5977. $expected = [
  5978. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5979. 'GREAT®',
  5980. '/textarea',
  5981. ];
  5982. $this->assertHtml($expected, $result);
  5983. }
  5984. /**
  5985. * testTextAreaMaxLength method
  5986. *
  5987. * Test textareas maxlength read from schema.
  5988. */
  5989. public function testTextAreaMaxLength(): void
  5990. {
  5991. $this->Form->create([
  5992. 'schema' => [
  5993. 'stuff' => ['type' => 'string', 'length' => 10],
  5994. ],
  5995. ]);
  5996. $result = $this->Form->control('other', ['type' => 'textarea']);
  5997. $expected = [
  5998. 'div' => ['class' => 'input textarea'],
  5999. 'label' => ['for' => 'other'],
  6000. 'Other',
  6001. '/label',
  6002. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6003. '/textarea',
  6004. '/div',
  6005. ];
  6006. $this->assertHtml($expected, $result);
  6007. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6008. $expected = [
  6009. 'div' => ['class' => 'input textarea'],
  6010. 'label' => ['for' => 'stuff'],
  6011. 'Stuff',
  6012. '/label',
  6013. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6014. '/textarea',
  6015. '/div',
  6016. ];
  6017. $this->assertHtml($expected, $result);
  6018. }
  6019. /**
  6020. * testHiddenField method
  6021. *
  6022. * Test generation of a hidden input.
  6023. */
  6024. public function testHidden(): void
  6025. {
  6026. $this->article['errors'] = [
  6027. 'field' => true,
  6028. ];
  6029. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6030. $this->Form->create($this->article);
  6031. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6032. $expected = [
  6033. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6034. $this->assertHtml($expected, $result);
  6035. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6036. $expected = [
  6037. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6038. ];
  6039. $this->assertHtml($expected, $result);
  6040. $article = new Article([
  6041. 'status' => ArticleStatus::UNPUBLISHED,
  6042. ]);
  6043. $this->Form->create($article);
  6044. $result = $this->Form->hidden('status');
  6045. $expected = [
  6046. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => 'N'],
  6047. ];
  6048. $this->assertHtml($expected, $result);
  6049. }
  6050. /**
  6051. * Test hidden() with various boolean values.
  6052. */
  6053. public function testHiddenBooleanValues(): void
  6054. {
  6055. $this->Form->create($this->article);
  6056. $result = $this->Form->hidden('field', ['value' => null]);
  6057. $expected = [
  6058. 'input' => ['type' => 'hidden', 'name' => 'field'],
  6059. ];
  6060. $this->assertHtml($expected, $result);
  6061. $result = $this->Form->hidden('field', ['value' => true]);
  6062. $expected = [
  6063. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  6064. ];
  6065. $this->assertHtml($expected, $result);
  6066. $result = $this->Form->hidden('field', ['value' => false]);
  6067. $expected = [
  6068. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  6069. ];
  6070. $this->assertHtml($expected, $result);
  6071. }
  6072. /**
  6073. * testFileUploadField method
  6074. *
  6075. * Test generation of a file upload input.
  6076. */
  6077. public function testFileUploadField(): void
  6078. {
  6079. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6080. $result = $this->Form->file('Model.upload');
  6081. $this->assertHtml($expected, $result);
  6082. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6083. 'name' => '', 'type' => '', 'tmp_name' => '',
  6084. 'error' => 4, 'size' => 0,
  6085. ]));
  6086. $result = $this->Form->file('Model.upload');
  6087. $this->assertHtml($expected, $result);
  6088. $this->View->setRequest(
  6089. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6090. );
  6091. $result = $this->Form->file('Model.upload');
  6092. $this->assertHtml($expected, $result);
  6093. }
  6094. /**
  6095. * testFileUploadOnOtherModel method
  6096. *
  6097. * Test File upload input on a model not used in create().
  6098. */
  6099. public function testFileUploadOnOtherModel(): void
  6100. {
  6101. $this->Form->create($this->article, ['type' => 'file']);
  6102. $result = $this->Form->file('ValidateProfile.city');
  6103. $expected = [
  6104. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6105. ];
  6106. $this->assertHtml($expected, $result);
  6107. }
  6108. /**
  6109. * testButton method
  6110. *
  6111. * Test generation of a form button.
  6112. */
  6113. public function testButton(): void
  6114. {
  6115. $result = $this->Form->button('Hi');
  6116. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6117. $this->assertHtml($expected, $result);
  6118. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6119. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6120. $this->assertHtml($expected, $result);
  6121. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6122. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6123. $this->assertHtml($expected, $result);
  6124. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6125. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6126. $this->assertHtml($expected, $result);
  6127. $result = $this->Form->button('No type', ['type' => false]);
  6128. $expected = ['button' => [], 'No type', '/button'];
  6129. $this->assertHtml($expected, $result);
  6130. $result = $this->Form->button('Upload Text', [
  6131. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6132. 'escape' => false,
  6133. ]);
  6134. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6135. }
  6136. /**
  6137. * testButtonUnlockedByDefault method
  6138. *
  6139. * Test that button() makes unlocked fields by default.
  6140. */
  6141. public function testButtonUnlockedByDefault(): void
  6142. {
  6143. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6144. $this->Form->create();
  6145. $this->Form->button('Save', ['name' => 'save']);
  6146. $this->Form->button('Clear');
  6147. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6148. $this->assertEquals(['save'], $result);
  6149. }
  6150. /**
  6151. * Test generation of a form button with confirm message.
  6152. */
  6153. public function testButtonWithConfirm(): void
  6154. {
  6155. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6156. $expected = ['button' => [
  6157. 'type' => 'submit',
  6158. 'data-confirm-message' => 'Confirm me!',
  6159. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6160. ], 'Hi', '/button'];
  6161. $this->assertHtml($expected, $result);
  6162. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6163. $expected = ['button' => [
  6164. 'type' => 'submit',
  6165. 'data-confirm-message' => 'Confirm "me"!',
  6166. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6167. ], 'Hi', '/button'];
  6168. $this->assertHtml($expected, $result);
  6169. }
  6170. /**
  6171. * testPostButton method
  6172. */
  6173. public function testPostButton(): void
  6174. {
  6175. $result = $this->Form->postButton('Hi', '/controller/action');
  6176. $expected = [
  6177. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6178. 'button' => ['type' => 'submit'],
  6179. 'Hi',
  6180. '/button',
  6181. '/form',
  6182. ];
  6183. $this->assertHtml($expected, $result);
  6184. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6185. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6186. }
  6187. /**
  6188. * testPostButtonMethodType method
  6189. */
  6190. public function testPostButtonMethodType(): void
  6191. {
  6192. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6193. $expected = [
  6194. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6195. 'div' => ['style' => 'display:none;'],
  6196. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6197. '/div',
  6198. 'button' => ['type' => 'submit'],
  6199. 'Hi',
  6200. '/button',
  6201. '/form',
  6202. ];
  6203. $this->assertHtml($expected, $result);
  6204. }
  6205. /**
  6206. * testPostButtonFormOptions method
  6207. */
  6208. public function testPostButtonFormOptions(): void
  6209. {
  6210. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6211. $expected = [
  6212. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6213. 'button' => ['type' => 'submit'],
  6214. 'Hi',
  6215. '/button',
  6216. '/form',
  6217. ];
  6218. $this->assertHtml($expected, $result);
  6219. }
  6220. /**
  6221. * testPostButtonNestedData method
  6222. *
  6223. * Test using postButton with N dimensional data.
  6224. */
  6225. public function testPostButtonNestedData(): void
  6226. {
  6227. $data = [
  6228. 'one' => [
  6229. 'two' => [
  6230. 3, 4, 5,
  6231. ],
  6232. ],
  6233. ];
  6234. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6235. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6236. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6237. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6238. }
  6239. /**
  6240. * testSecurePostButton method
  6241. *
  6242. * Test that postButton adds _Token fields.
  6243. */
  6244. public function testSecurePostButton(): void
  6245. {
  6246. $this->View->setRequest($this->View->getRequest()
  6247. ->withAttribute('csrfToken', 'testkey')
  6248. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6249. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6250. $tokenDebug = urlencode(json_encode([
  6251. '/posts/delete/1',
  6252. [],
  6253. [],
  6254. ]));
  6255. $expected = [
  6256. 'form' => [
  6257. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6258. ],
  6259. ['div' => ['style' => 'display:none;']],
  6260. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6261. '/div',
  6262. 'button' => ['type' => 'submit'],
  6263. 'Delete',
  6264. '/button',
  6265. ['div' => ['style' => 'display:none;']],
  6266. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6267. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6268. ['input' => [
  6269. 'type' => 'hidden',
  6270. 'name' => '_Token[debug]',
  6271. 'value' => $tokenDebug,
  6272. ]],
  6273. '/div',
  6274. '/form',
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. }
  6278. /**
  6279. * testPostLink method
  6280. */
  6281. public function testPostLink(): void
  6282. {
  6283. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6284. $expected = [
  6285. 'form' => [
  6286. 'method' => 'post', 'action' => '/posts/delete/1',
  6287. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6288. ],
  6289. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6290. '/form',
  6291. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6292. 'Delete',
  6293. '/a',
  6294. ];
  6295. $this->assertHtml($expected, $result);
  6296. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6297. $expected = [
  6298. 'form' => [
  6299. 'method' => 'post', 'action' => '/posts/delete/1',
  6300. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6301. ],
  6302. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6303. '/form',
  6304. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6305. 'Delete',
  6306. '/a',
  6307. ];
  6308. $this->assertHtml($expected, $result);
  6309. $result = $this->Form->postLink(
  6310. 'Delete',
  6311. '/posts/delete/1',
  6312. ['target' => '_blank', 'class' => 'btn btn-danger']
  6313. );
  6314. $expected = [
  6315. 'form' => [
  6316. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6317. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6318. ],
  6319. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6320. '/form',
  6321. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6322. 'Delete',
  6323. '/a',
  6324. ];
  6325. $this->assertHtml($expected, $result);
  6326. }
  6327. /**
  6328. * testPostLinkWithConfirm method
  6329. *
  6330. * Test the confirm option for postLink().
  6331. */
  6332. public function testPostLinkWithConfirm(): void
  6333. {
  6334. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6335. $expected = [
  6336. 'form' => [
  6337. 'method' => 'post', 'action' => '/posts/delete/1',
  6338. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6339. ],
  6340. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6341. '/form',
  6342. 'a' => [
  6343. 'href' => '#',
  6344. 'data-confirm-message' => 'Confirm?',
  6345. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6346. ],
  6347. 'Delete',
  6348. '/a',
  6349. ];
  6350. $this->assertHtml($expected, $result);
  6351. $result = $this->Form->postLink(
  6352. 'Delete',
  6353. '/posts/delete/1',
  6354. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6355. );
  6356. $expected = [
  6357. 'form' => [
  6358. 'method' => 'post', 'action' => '/posts/delete/1',
  6359. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6360. ],
  6361. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6362. '/form',
  6363. 'a' => [
  6364. 'href' => '#',
  6365. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6366. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6367. ],
  6368. 'Delete',
  6369. '/a',
  6370. ];
  6371. $this->assertHtml($expected, $result);
  6372. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6373. $result = $this->Form->postLink(
  6374. 'Delete',
  6375. '/posts/delete/1',
  6376. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6377. );
  6378. $expected = [
  6379. 'form' => [
  6380. 'method' => 'post', 'action' => '/posts/delete/1',
  6381. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6382. ],
  6383. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6384. '/form',
  6385. 'a' => [
  6386. 'href' => '#',
  6387. 'data-confirm-message' => 'Confirm this deletion?',
  6388. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6389. ],
  6390. 'Delete',
  6391. '/a',
  6392. ];
  6393. $this->assertHtml($expected, $result);
  6394. }
  6395. /**
  6396. * testPostLinkWithQuery method
  6397. *
  6398. * Test postLink() with query string args.
  6399. */
  6400. public function testPostLinkWithQuery(): void
  6401. {
  6402. $result = $this->Form->postLink(
  6403. 'Delete',
  6404. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6405. );
  6406. $expected = [
  6407. 'form' => [
  6408. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6409. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6410. ],
  6411. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6412. '/form',
  6413. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6414. 'Delete',
  6415. '/a',
  6416. ];
  6417. $this->assertHtml($expected, $result);
  6418. }
  6419. /**
  6420. * testPostLinkWithData method
  6421. *
  6422. * Test postLink with additional data.
  6423. */
  6424. public function testPostLinkWithData(): void
  6425. {
  6426. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6427. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6428. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6429. $this->Form->create($entity);
  6430. $this->Form->end();
  6431. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6432. $this->assertStringContainsString(
  6433. '<input type="hidden" name="name" value="show"',
  6434. $result,
  6435. 'should not contain entity data.'
  6436. );
  6437. }
  6438. /**
  6439. * testPostLinkSecurityHash method
  6440. *
  6441. * Test that security hashes for postLink include the url.
  6442. */
  6443. public function testPostLinkSecurityHash(): void
  6444. {
  6445. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6446. $hash .= '%3Aid';
  6447. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6448. $result = $this->Form->postLink(
  6449. 'Delete',
  6450. '/posts/delete/1',
  6451. ['data' => ['id' => 1]]
  6452. );
  6453. $tokenDebug = urlencode(json_encode([
  6454. '/posts/delete/1',
  6455. [
  6456. 'id' => 1,
  6457. ],
  6458. [],
  6459. ]));
  6460. $expected = [
  6461. 'form' => [
  6462. 'method' => 'post', 'action' => '/posts/delete/1',
  6463. 'name', 'style' => 'display:none;',
  6464. ],
  6465. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6466. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6467. 'div' => ['style' => 'display:none;'],
  6468. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6469. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6470. ['input' => [
  6471. 'type' => 'hidden',
  6472. 'name' => '_Token[debug]',
  6473. 'value' => $tokenDebug,
  6474. ]],
  6475. '/div',
  6476. '/form',
  6477. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6478. 'Delete',
  6479. '/a',
  6480. ];
  6481. $this->assertHtml($expected, $result);
  6482. }
  6483. /**
  6484. * testPostLinkSecurityHashBlockMode method
  6485. *
  6486. * Test that postLink doesn't modify the fields in the containing form.
  6487. *
  6488. * postLink() calls inside open forms should not modify the field list
  6489. * for the form.
  6490. */
  6491. public function testPostLinkSecurityHashBlockMode(): void
  6492. {
  6493. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6494. $hash .= '%3A';
  6495. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6496. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6497. $this->Form->control('title');
  6498. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6499. $result = $this->View->fetch('postLink');
  6500. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6501. $this->assertEquals(['title'], $fields);
  6502. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6503. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6504. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6505. }
  6506. /**
  6507. * testPostLinkSecurityHashNoDebugMode method
  6508. *
  6509. * Test that security does not include debug token if debug is false.
  6510. */
  6511. public function testPostLinkSecurityHashNoDebugMode(): void
  6512. {
  6513. Configure::write('debug', false);
  6514. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6515. $hash .= '%3Aid';
  6516. $this->View->setRequest($this->View->getRequest()
  6517. ->withAttribute('formTokenData', ['key' => 'test']));
  6518. $result = $this->Form->postLink(
  6519. 'Delete',
  6520. '/posts/delete/1',
  6521. ['data' => ['id' => 1]]
  6522. );
  6523. $expected = [
  6524. 'form' => [
  6525. 'method' => 'post', 'action' => '/posts/delete/1',
  6526. 'name', 'style' => 'display:none;',
  6527. ],
  6528. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6529. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6530. 'div' => ['style' => 'display:none;'],
  6531. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6532. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6533. '/div',
  6534. '/form',
  6535. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6536. 'Delete',
  6537. '/a',
  6538. ];
  6539. $this->assertHtml($expected, $result);
  6540. }
  6541. /**
  6542. * testPostLinkNestedData method
  6543. *
  6544. * Test using postLink with N dimensional data.
  6545. */
  6546. public function testPostLinkNestedData(): void
  6547. {
  6548. $data = [
  6549. 'one' => [
  6550. 'two' => [
  6551. 3, 4, 5,
  6552. ],
  6553. ],
  6554. ];
  6555. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6556. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6557. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6558. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6559. }
  6560. /**
  6561. * testPostLinkAfterGetForm method
  6562. *
  6563. * Test creating postLinks after a GET form.
  6564. */
  6565. public function testPostLinkAfterGetForm(): void
  6566. {
  6567. $this->View->setRequest($this->View->getRequest()
  6568. ->withAttribute('csrfToken', 'testkey')
  6569. ->withAttribute('formTokenData', []));
  6570. $this->Form->create($this->article, ['type' => 'get']);
  6571. $this->Form->end();
  6572. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6573. $tokenDebug = urlencode(json_encode([
  6574. '/posts/delete/1',
  6575. [],
  6576. [],
  6577. ]));
  6578. $expected = [
  6579. 'form' => [
  6580. 'method' => 'post', 'action' => '/posts/delete/1',
  6581. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6582. ],
  6583. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6584. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6585. 'div' => ['style' => 'display:none;'],
  6586. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6587. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6588. ['input' => [
  6589. 'type' => 'hidden', 'name' => '_Token[debug]',
  6590. 'value' => $tokenDebug,
  6591. ]],
  6592. '/div',
  6593. '/form',
  6594. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6595. 'Delete',
  6596. '/a',
  6597. ];
  6598. $this->assertHtml($expected, $result);
  6599. }
  6600. /**
  6601. * testPostLinkFormBuffer method
  6602. *
  6603. * Test that postLink adds form tags to view block.
  6604. */
  6605. public function testPostLinkFormBuffer(): void
  6606. {
  6607. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6608. $expected = [
  6609. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6610. 'Delete',
  6611. '/a',
  6612. ];
  6613. $this->assertHtml($expected, $result);
  6614. $result = $this->View->fetch('postLink');
  6615. $expected = [
  6616. 'form' => [
  6617. 'method' => 'post', 'action' => '/posts/delete/1',
  6618. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6619. ],
  6620. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6621. '/form',
  6622. ];
  6623. $this->assertHtml($expected, $result);
  6624. $result = $this->Form->postLink(
  6625. 'Delete',
  6626. '/posts/delete/2',
  6627. ['block' => true, 'method' => 'DELETE']
  6628. );
  6629. $expected = [
  6630. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6631. 'Delete',
  6632. '/a',
  6633. ];
  6634. $this->assertHtml($expected, $result);
  6635. $result = $this->View->fetch('postLink');
  6636. $expected = [
  6637. 'form' => [
  6638. 'method' => 'post', 'action' => '/posts/delete/1',
  6639. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6640. ],
  6641. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6642. '/form',
  6643. [
  6644. 'form' => [
  6645. 'method' => 'post', 'action' => '/posts/delete/2',
  6646. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6647. ],
  6648. ],
  6649. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6650. '/form',
  6651. ];
  6652. $this->assertHtml($expected, $result);
  6653. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6654. $expected = [
  6655. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6656. 'Delete',
  6657. '/a',
  6658. ];
  6659. $this->assertHtml($expected, $result);
  6660. $result = $this->View->fetch('foobar');
  6661. $expected = [
  6662. 'form' => [
  6663. 'method' => 'post', 'action' => '/posts/delete/1',
  6664. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6665. ],
  6666. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6667. '/form',
  6668. ];
  6669. $this->assertHtml($expected, $result);
  6670. }
  6671. /**
  6672. * testSubmitButton method
  6673. */
  6674. public function testSubmitButton(): void
  6675. {
  6676. $result = $this->Form->submit('');
  6677. $expected = [
  6678. 'div' => ['class' => 'submit'],
  6679. 'input' => ['type' => 'submit', 'value' => ''],
  6680. '/div',
  6681. ];
  6682. $this->assertHtml($expected, $result);
  6683. $result = $this->Form->submit('Test Submit');
  6684. $expected = [
  6685. 'div' => ['class' => 'submit'],
  6686. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6687. '/div',
  6688. ];
  6689. $this->assertHtml($expected, $result);
  6690. $result = $this->Form->submit('Next >');
  6691. $expected = [
  6692. 'div' => ['class' => 'submit'],
  6693. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6694. '/div',
  6695. ];
  6696. $this->assertHtml($expected, $result);
  6697. $result = $this->Form->submit('Next >', ['escape' => false]);
  6698. $expected = [
  6699. 'div' => ['class' => 'submit'],
  6700. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6701. '/div',
  6702. ];
  6703. $this->assertHtml($expected, $result);
  6704. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6705. $expected = [
  6706. 'div' => ['class' => 'submit'],
  6707. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6708. '/div',
  6709. ];
  6710. $this->assertHtml($expected, $result);
  6711. }
  6712. /**
  6713. * testSubmitImage method
  6714. *
  6715. * Test image submit types.
  6716. */
  6717. public function testSubmitImage(): void
  6718. {
  6719. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6720. $expected = [
  6721. 'div' => ['class' => 'submit'],
  6722. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6723. '/div',
  6724. ];
  6725. $this->assertHtml($expected, $result);
  6726. $result = $this->Form->submit('/relative/cake.power.gif');
  6727. $expected = [
  6728. 'div' => ['class' => 'submit'],
  6729. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6730. '/div',
  6731. ];
  6732. $this->assertHtml($expected, $result);
  6733. $result = $this->Form->submit('cake.power.gif');
  6734. $expected = [
  6735. 'div' => ['class' => 'submit'],
  6736. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6737. '/div',
  6738. ];
  6739. $this->assertHtml($expected, $result);
  6740. $result = $this->Form->submit('Not.an.image');
  6741. $expected = [
  6742. 'div' => ['class' => 'submit'],
  6743. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6744. '/div',
  6745. ];
  6746. $this->assertHtml($expected, $result);
  6747. }
  6748. /**
  6749. * testSubmitUnlockedByDefault method
  6750. *
  6751. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6752. * be submitted at a time.
  6753. */
  6754. public function testSubmitUnlockedByDefault(): void
  6755. {
  6756. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6757. $this->Form->create();
  6758. $this->Form->submit('Go go');
  6759. $this->Form->submit('Save', ['name' => 'save']);
  6760. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6761. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6762. }
  6763. /**
  6764. * testSubmitImageTimestamp method
  6765. *
  6766. * Test submit image with timestamps.
  6767. */
  6768. public function testSubmitImageTimestamp(): void
  6769. {
  6770. Configure::write('Asset.timestamp', 'force');
  6771. $result = $this->Form->submit('cake.power.gif');
  6772. $expected = [
  6773. 'div' => ['class' => 'submit'],
  6774. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6775. '/div',
  6776. ];
  6777. $this->assertHtml($expected, $result);
  6778. }
  6779. /**
  6780. * testDateTimeWithGetForms method
  6781. *
  6782. * Test that datetime() works with GET style forms.
  6783. */
  6784. public function testDateTimeWithGetForms(): void
  6785. {
  6786. $this->Form->create($this->article, ['type' => 'get']);
  6787. $result = $this->Form->datetime('created');
  6788. $expected = [
  6789. 'input' => [
  6790. 'type' => 'datetime-local',
  6791. 'name' => 'created',
  6792. 'value' => '',
  6793. 'step' => '1',
  6794. ],
  6795. ];
  6796. $this->assertHtml($expected, $result);
  6797. $result = $this->Form->datetime('created', ['default' => true]);
  6798. $expected = [
  6799. 'input' => [
  6800. 'type' => 'datetime-local',
  6801. 'name' => 'created',
  6802. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6803. 'step' => '1',
  6804. ],
  6805. ];
  6806. $this->assertHtml($expected, $result);
  6807. }
  6808. /**
  6809. * Provides fractional schema types
  6810. *
  6811. * @return array
  6812. */
  6813. public static function fractionalTypeProvider(): array
  6814. {
  6815. return [
  6816. ['datetimefractional'],
  6817. ['timestampfractional'],
  6818. ['timestamptimezone'],
  6819. ];
  6820. }
  6821. /**
  6822. * testDateTimeWithFractional method
  6823. *
  6824. * Test that datetime() works with datetimefractional.
  6825. *
  6826. * @dataProvider fractionalTypeProvider
  6827. */
  6828. public function testDateTimeWithFractional(string $type): void
  6829. {
  6830. $this->Form->create([
  6831. 'schema' => [
  6832. 'created' => ['type' => $type],
  6833. ],
  6834. ]);
  6835. $result = $this->Form->datetime('created', [
  6836. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6837. ]);
  6838. $expected = [
  6839. 'input' => [
  6840. 'type' => 'datetime-local',
  6841. 'name' => 'created',
  6842. 'value' => '2019-09-27T02:52:43.123',
  6843. 'step' => '0.001',
  6844. ],
  6845. ];
  6846. $this->assertHtml($expected, $result);
  6847. }
  6848. /**
  6849. * testControlWithFractional method
  6850. *
  6851. * Test that control() works with datetimefractional.
  6852. *
  6853. * @dataProvider fractionalTypeProvider
  6854. */
  6855. public function testControlWithFractional(string $type): void
  6856. {
  6857. $this->Form->create([
  6858. 'schema' => [
  6859. 'created' => ['type' => $type],
  6860. ],
  6861. ]);
  6862. $result = $this->Form->control('created', [
  6863. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6864. ]);
  6865. $expected = [
  6866. 'div' => ['class' => 'input datetime'],
  6867. 'label' => ['for' => 'created'],
  6868. 'Created',
  6869. '/label',
  6870. 'input' => [
  6871. 'type' => 'datetime-local',
  6872. 'name' => 'created',
  6873. 'id' => 'created',
  6874. 'value' => '2019-09-27T02:52:43.123',
  6875. 'step' => '0.001',
  6876. ],
  6877. '/div',
  6878. ];
  6879. $this->assertHtml($expected, $result);
  6880. }
  6881. /**
  6882. * testForMagicControlNonExistentNotValidated method
  6883. */
  6884. public function testForMagicControlNonExistentNotValidated(): void
  6885. {
  6886. $this->Form->create($this->article);
  6887. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6888. $result = $this->Form->control('nonexistent_not_validated');
  6889. $expected = [
  6890. 'label' => ['for' => 'nonexistent-not-validated'],
  6891. 'Nonexistent Not Validated',
  6892. '/label',
  6893. 'input' => [
  6894. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6895. 'id' => 'nonexistent-not-validated',
  6896. ],
  6897. ];
  6898. $this->assertHtml($expected, $result);
  6899. $result = $this->Form->control('nonexistent_not_validated', [
  6900. 'val' => 'my value',
  6901. ]);
  6902. $expected = [
  6903. 'label' => ['for' => 'nonexistent-not-validated'],
  6904. 'Nonexistent Not Validated',
  6905. '/label',
  6906. 'input' => [
  6907. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6908. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6909. ],
  6910. ];
  6911. $this->assertHtml($expected, $result);
  6912. $this->View->setRequest(
  6913. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6914. );
  6915. $this->Form->create($this->article);
  6916. $result = $this->Form->control('nonexistent_not_validated');
  6917. $expected = [
  6918. 'label' => ['for' => 'nonexistent-not-validated'],
  6919. 'Nonexistent Not Validated',
  6920. '/label',
  6921. 'input' => [
  6922. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6923. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6924. ],
  6925. ];
  6926. $this->assertHtml($expected, $result);
  6927. }
  6928. /**
  6929. * testFormMagicControlLabel method
  6930. */
  6931. public function testFormMagicControlLabel(): void
  6932. {
  6933. $this->getTableLocator()->get('Contacts', [
  6934. 'className' => ContactsTable::class,
  6935. ]);
  6936. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6937. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6938. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6939. $expected = [
  6940. 'label' => ['for' => 'contacts-name'],
  6941. 'My label',
  6942. '/label',
  6943. 'input' => [
  6944. 'type' => 'text',
  6945. 'name' => 'Contacts[name]',
  6946. 'id' => 'contacts-name',
  6947. 'maxlength' => '255',
  6948. ],
  6949. ];
  6950. $this->assertHtml($expected, $result);
  6951. $result = $this->Form->control('name', [
  6952. 'label' => ['class' => 'mandatory'],
  6953. ]);
  6954. $expected = [
  6955. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6956. 'Name',
  6957. '/label',
  6958. 'input' => [
  6959. 'type' => 'text', 'name' => 'name',
  6960. 'id' => 'name', 'maxlength' => '255',
  6961. ],
  6962. ];
  6963. $this->assertHtml($expected, $result);
  6964. $result = $this->Form->control('name', [
  6965. 'div' => false,
  6966. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6967. ]);
  6968. $expected = [
  6969. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6970. 'My label',
  6971. '/label',
  6972. 'input' => [
  6973. 'type' => 'text', 'name' => 'name',
  6974. 'id' => 'name', 'maxlength' => '255',
  6975. ],
  6976. ];
  6977. $this->assertHtml($expected, $result);
  6978. $result = $this->Form->control('Contact.name', [
  6979. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6980. ]);
  6981. $expected = [
  6982. 'label' => ['for' => 'my_id'],
  6983. 'Name',
  6984. '/label',
  6985. 'input' => [
  6986. 'type' => 'text', 'name' => 'Contact[name]',
  6987. 'id' => 'my_id', 'maxlength' => '255',
  6988. ],
  6989. ];
  6990. $this->assertHtml($expected, $result);
  6991. $result = $this->Form->control('1.id');
  6992. $expected = ['input' => [
  6993. 'type' => 'hidden', 'name' => '1[id]',
  6994. 'id' => '1-id',
  6995. ]];
  6996. $this->assertHtml($expected, $result);
  6997. $result = $this->Form->control('1.name');
  6998. $expected = [
  6999. 'label' => ['for' => '1-name'],
  7000. 'Name',
  7001. '/label',
  7002. 'input' => [
  7003. 'type' => 'text', 'name' => '1[name]',
  7004. 'id' => '1-name', 'maxlength' => '255',
  7005. ],
  7006. ];
  7007. $this->assertHtml($expected, $result);
  7008. }
  7009. /**
  7010. * testFormEnd method
  7011. */
  7012. public function testFormEnd(): void
  7013. {
  7014. $this->assertSame('</form>', $this->Form->end());
  7015. }
  7016. /**
  7017. * testMultiRecordForm method
  7018. *
  7019. * Test the generation of fields for a multi record form.
  7020. */
  7021. public function testMultiRecordForm(): void
  7022. {
  7023. $articles = $this->getTableLocator()->get('Articles');
  7024. $articles->hasMany('Comments');
  7025. $comment = new Entity(['comment' => 'Value']);
  7026. $article = new Article(['comments' => [$comment]]);
  7027. $this->Form->create([$article]);
  7028. $result = $this->Form->control('0.comments.1.comment');
  7029. // phpcs:disable
  7030. $expected = [
  7031. 'div' => ['class' => 'input textarea'],
  7032. 'label' => ['for' => '0-comments-1-comment'],
  7033. 'Comment',
  7034. '/label',
  7035. 'textarea' => [
  7036. 'name',
  7037. 'id' => '0-comments-1-comment',
  7038. 'rows' => 5
  7039. ],
  7040. '/textarea',
  7041. '/div'
  7042. ];
  7043. // phpcs:enable
  7044. $this->assertHtml($expected, $result);
  7045. $result = $this->Form->control('0.comments.0.comment');
  7046. // phpcs:disable
  7047. $expected = [
  7048. 'div' => ['class' => 'input textarea'],
  7049. 'label' => ['for' => '0-comments-0-comment'],
  7050. 'Comment',
  7051. '/label',
  7052. 'textarea' => [
  7053. 'name',
  7054. 'id' => '0-comments-0-comment',
  7055. 'rows' => 5
  7056. ],
  7057. 'Value',
  7058. '/textarea',
  7059. '/div'
  7060. ];
  7061. // phpcs:enable
  7062. $this->assertHtml($expected, $result);
  7063. $comment->setError('comment', ['Not valid']);
  7064. $result = $this->Form->control('0.comments.0.comment');
  7065. // phpcs:disable
  7066. $expected = [
  7067. 'div' => ['class' => 'input textarea error'],
  7068. 'label' => ['for' => '0-comments-0-comment'],
  7069. 'Comment',
  7070. '/label',
  7071. 'textarea' => [
  7072. 'name',
  7073. 'class' => 'form-error',
  7074. 'id' => '0-comments-0-comment',
  7075. 'aria-invalid' => 'true',
  7076. 'aria-describedby' => '0-comments-0-comment-error',
  7077. 'rows' => 5
  7078. ],
  7079. 'Value',
  7080. '/textarea',
  7081. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  7082. 'Not valid',
  7083. '/div',
  7084. '/div'
  7085. ];
  7086. // phpcs:enable
  7087. $this->assertHtml($expected, $result);
  7088. $this->getTableLocator()->get('Comments')
  7089. ->getValidator('default')
  7090. ->allowEmptyString('comment', null, false);
  7091. $result = $this->Form->control('0.comments.1.comment');
  7092. // phpcs:disable
  7093. $expected = [
  7094. 'div' => ['class' => 'input textarea required'],
  7095. 'label' => ['for' => '0-comments-1-comment'],
  7096. 'Comment',
  7097. '/label',
  7098. 'textarea' => [
  7099. 'name',
  7100. 'aria-required' => 'true',
  7101. 'required' => 'required',
  7102. 'id' => '0-comments-1-comment',
  7103. 'rows' => 5,
  7104. 'data-validity-message' => 'This field cannot be left empty',
  7105. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7106. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7107. ],
  7108. '/textarea',
  7109. '/div'
  7110. ];
  7111. // phpcs:enable
  7112. $this->assertHtml($expected, $result);
  7113. }
  7114. /**
  7115. * testHtml5Controls method
  7116. *
  7117. * Test that some html5 inputs + FormHelper::__call() work.
  7118. */
  7119. public function testHtml5Controls(): void
  7120. {
  7121. $result = $this->Form->email('User.email');
  7122. $expected = [
  7123. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7124. ];
  7125. $this->assertHtml($expected, $result);
  7126. $result = $this->Form->search('User.query');
  7127. $expected = [
  7128. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7129. ];
  7130. $this->assertHtml($expected, $result);
  7131. $result = $this->Form->search('User.query', ['value' => 'test']);
  7132. $expected = [
  7133. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7134. ];
  7135. $this->assertHtml($expected, $result);
  7136. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7137. $expected = [
  7138. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7139. ];
  7140. $this->assertHtml($expected, $result);
  7141. }
  7142. /**
  7143. * testHtml5ControlWithControl method
  7144. *
  7145. * Test accessing html5 inputs through control().
  7146. */
  7147. public function testHtml5ControlWithControl(): void
  7148. {
  7149. $this->Form->create();
  7150. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7151. $result = $this->Form->control('website', [
  7152. 'type' => 'url',
  7153. 'val' => 'http://domain.tld',
  7154. 'label' => false,
  7155. ]);
  7156. $expected = [
  7157. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7158. ];
  7159. $this->assertHtml($expected, $result);
  7160. }
  7161. /**
  7162. * testHtml5ControlException method
  7163. *
  7164. * Test errors when field name is missing.
  7165. */
  7166. public function testHtml5ControlException(): void
  7167. {
  7168. $this->expectException(CakeException::class);
  7169. $this->Form->email();
  7170. }
  7171. /**
  7172. * tests fields that are required use custom validation messages
  7173. */
  7174. public function testHtml5ErrorMessage(): void
  7175. {
  7176. $this->Form->setConfig('autoSetCustomValidity', true);
  7177. $validator = (new Validator())
  7178. ->notEmptyString('email', 'Custom error message')
  7179. ->requirePresence('password')
  7180. ->alphaNumeric('password')
  7181. ->notBlank('phone');
  7182. $table = $this->getTableLocator()->get('Contacts', [
  7183. 'className' => ContactsTable::class,
  7184. ]);
  7185. $table->setValidator('default', $validator);
  7186. $contact = new Entity();
  7187. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7188. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7189. $result = $this->Form->control('password');
  7190. $expected = [
  7191. 'label' => ['for' => 'password'],
  7192. 'Password',
  7193. '/label',
  7194. 'input' => [
  7195. 'aria-required' => 'true',
  7196. 'id' => 'password',
  7197. 'name' => 'password',
  7198. 'type' => 'password',
  7199. 'value' => '',
  7200. 'required' => 'required',
  7201. 'data-validity-message' => 'This field cannot be left empty',
  7202. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7203. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7204. ],
  7205. ];
  7206. $this->assertHtml($expected, $result);
  7207. $result = $this->Form->control('phone');
  7208. $expected = [
  7209. 'label' => ['for' => 'phone'],
  7210. 'Phone',
  7211. '/label',
  7212. 'input' => [
  7213. 'aria-required' => 'true',
  7214. 'id' => 'phone',
  7215. 'name' => 'phone',
  7216. 'type' => 'tel',
  7217. 'value' => '',
  7218. 'maxlength' => 255,
  7219. 'required' => 'required',
  7220. 'data-validity-message' => 'This field cannot be left empty',
  7221. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7222. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7223. ],
  7224. ];
  7225. $this->assertHtml($expected, $result);
  7226. $result = $this->Form->control('email');
  7227. $expected = [
  7228. 'label' => ['for' => 'email'],
  7229. 'Email',
  7230. '/label',
  7231. 'input' => [
  7232. 'aria-required' => 'true',
  7233. 'id' => 'email',
  7234. 'name' => 'email',
  7235. 'type' => 'email',
  7236. 'value' => '',
  7237. 'maxlength' => 255,
  7238. 'required' => 'required',
  7239. 'data-validity-message' => 'Custom error message',
  7240. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7241. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7242. ],
  7243. ];
  7244. $this->assertHtml($expected, $result);
  7245. }
  7246. /**
  7247. * tests that custom validation messages are in templateVars
  7248. */
  7249. public function testHtml5ErrorMessageInTemplateVars(): void
  7250. {
  7251. $validator = (new Validator())
  7252. ->notEmptyString('email', 'Custom error "message" & entities')
  7253. ->requirePresence('password')
  7254. ->alphaNumeric('password')
  7255. ->notBlank('phone');
  7256. $table = $this->getTableLocator()->get('Contacts', [
  7257. 'className' => ContactsTable::class,
  7258. ]);
  7259. $table->setValidator('default', $validator);
  7260. $contact = new Entity();
  7261. $this->Form->setConfig('autoSetCustomValidity', false);
  7262. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7263. $this->Form->setTemplates([
  7264. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7265. 'inputContainer' => '{{content}}',
  7266. ]);
  7267. $result = $this->Form->control('password');
  7268. $expected = [
  7269. 'label' => ['for' => 'password'],
  7270. 'Password',
  7271. '/label',
  7272. 'input' => [
  7273. 'aria-required' => 'true',
  7274. 'id' => 'password',
  7275. 'name' => 'password',
  7276. 'type' => 'password',
  7277. 'value' => '',
  7278. 'required' => 'required',
  7279. 'data-message' => 'This field cannot be left empty',
  7280. ],
  7281. ];
  7282. $this->assertHtml($expected, $result);
  7283. $result = $this->Form->control('phone');
  7284. $expected = [
  7285. 'label' => ['for' => 'phone'],
  7286. 'Phone',
  7287. '/label',
  7288. 'input' => [
  7289. 'aria-required' => 'true',
  7290. 'id' => 'phone',
  7291. 'name' => 'phone',
  7292. 'type' => 'tel',
  7293. 'value' => '',
  7294. 'maxlength' => 255,
  7295. 'required' => 'required',
  7296. 'data-message' => 'This field cannot be left empty',
  7297. ],
  7298. ];
  7299. $this->assertHtml($expected, $result);
  7300. $result = $this->Form->control('email', [
  7301. 'templateVars' => [
  7302. 'custom' => 'data-custom="1"',
  7303. ],
  7304. ]);
  7305. $expected = [
  7306. 'label' => ['for' => 'email'],
  7307. 'Email',
  7308. '/label',
  7309. 'input' => [
  7310. 'aria-required' => 'true',
  7311. 'id' => 'email',
  7312. 'name' => 'email',
  7313. 'type' => 'email',
  7314. 'value' => '',
  7315. 'maxlength' => 255,
  7316. 'required' => 'required',
  7317. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7318. 'data-custom' => '1',
  7319. ],
  7320. ];
  7321. $this->assertHtml($expected, $result);
  7322. $this->Form->setConfig('autoSetCustomValidity', true);
  7323. }
  7324. /**
  7325. * testRequiredAttribute method
  7326. *
  7327. * Tests that formhelper sets required attributes.
  7328. */
  7329. public function testRequiredAttribute(): void
  7330. {
  7331. $this->article['required'] = [
  7332. 'title' => true,
  7333. 'body' => false,
  7334. ];
  7335. $this->Form->create($this->article);
  7336. $result = $this->Form->control('title');
  7337. $expected = [
  7338. 'div' => ['class' => 'input text required'],
  7339. 'label' => ['for' => 'title'],
  7340. 'Title',
  7341. '/label',
  7342. 'input' => [
  7343. 'type' => 'text',
  7344. 'name' => 'title',
  7345. 'id' => 'title',
  7346. 'aria-required' => 'true',
  7347. 'required' => 'required',
  7348. 'data-validity-message' => 'This field cannot be left empty',
  7349. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7350. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7351. ],
  7352. '/div',
  7353. ];
  7354. $this->assertHtml($expected, $result);
  7355. $result = $this->Form->control('title', ['required' => false]);
  7356. $this->assertStringNotContainsString('required', $result);
  7357. $result = $this->Form->control('body');
  7358. $expected = [
  7359. 'div' => ['class' => 'input text'],
  7360. 'label' => ['for' => 'body'],
  7361. 'Body',
  7362. '/label',
  7363. 'input' => [
  7364. 'type' => 'text',
  7365. 'name' => 'body',
  7366. 'id' => 'body',
  7367. ],
  7368. '/div',
  7369. ];
  7370. $this->assertHtml($expected, $result);
  7371. $result = $this->Form->control('body', ['required' => true]);
  7372. $this->assertStringContainsString('required', $result);
  7373. }
  7374. /**
  7375. * testControlsNotNested method
  7376. *
  7377. * Tests that it is possible to put inputs outside of the label.
  7378. */
  7379. public function testControlsNotNested(): void
  7380. {
  7381. $this->Form->setTemplates([
  7382. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7383. 'formGroup' => '{{input}}{{label}}',
  7384. ]);
  7385. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7386. $expected = [
  7387. 'div' => ['class' => 'input checkbox'],
  7388. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7389. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7390. 'label' => ['for' => 'foo'],
  7391. 'Foo',
  7392. '/label',
  7393. '/div',
  7394. ];
  7395. $this->assertHtml($expected, $result);
  7396. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7397. $expected = [
  7398. 'div' => ['class' => 'input checkbox'],
  7399. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7400. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7401. '/div',
  7402. ];
  7403. $this->assertHtml($expected, $result);
  7404. $result = $this->Form->control('confirm', [
  7405. 'type' => 'radio',
  7406. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7407. ]);
  7408. $expected = [
  7409. 'div' => ['class' => 'input radio'],
  7410. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7411. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7412. ['label' => ['for' => 'confirm-y']],
  7413. 'Yes',
  7414. '/label',
  7415. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7416. ['label' => ['for' => 'confirm-n']],
  7417. 'No',
  7418. '/label',
  7419. '<label',
  7420. 'Confirm',
  7421. '/label',
  7422. '/div',
  7423. ];
  7424. $this->assertHtml($expected, $result);
  7425. $result = $this->Form->select('category', ['1', '2'], [
  7426. 'multiple' => 'checkbox',
  7427. 'name' => 'fish',
  7428. ]);
  7429. $expected = [
  7430. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7431. ['div' => ['class' => 'checkbox']],
  7432. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7433. ['label' => ['for' => 'fish-0']],
  7434. '1',
  7435. '/label',
  7436. '/div',
  7437. ['div' => ['class' => 'checkbox']],
  7438. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7439. ['label' => ['for' => 'fish-1']],
  7440. '2',
  7441. '/label',
  7442. '/div',
  7443. ];
  7444. $this->assertHtml($expected, $result);
  7445. }
  7446. /**
  7447. * testControlContainerTemplates method
  7448. *
  7449. * Test that *Container templates are used by input.
  7450. */
  7451. public function testControlContainerTemplates(): void
  7452. {
  7453. $this->Form->setTemplates([
  7454. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7455. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7456. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7457. ]);
  7458. $this->article['errors'] = [
  7459. 'Article' => ['published' => 'error message'],
  7460. ];
  7461. $this->Form->create($this->article);
  7462. $result = $this->Form->control('accept', [
  7463. 'type' => 'checkbox',
  7464. ]);
  7465. $expected = [
  7466. 'div' => ['class' => 'check'],
  7467. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7468. 'label' => ['for' => 'accept'],
  7469. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7470. 'Accept',
  7471. '/label',
  7472. '/div',
  7473. ];
  7474. $this->assertHtml($expected, $result);
  7475. $result = $this->Form->control('accept', [
  7476. 'type' => 'radio',
  7477. 'options' => ['Y', 'N'],
  7478. ]);
  7479. $this->assertStringContainsString('<div class="rad">', $result);
  7480. $result = $this->Form->control('Article.published', [
  7481. 'type' => 'radio',
  7482. 'options' => ['Y', 'N'],
  7483. ]);
  7484. $this->assertStringContainsString('<div class="rad err">', $result);
  7485. }
  7486. /**
  7487. * testFormGroupTemplates method
  7488. *
  7489. * Test that *Container templates are used by input.
  7490. */
  7491. public function testFormGroupTemplates(): void
  7492. {
  7493. $this->Form->setTemplates([
  7494. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7495. ]);
  7496. $this->Form->create($this->article);
  7497. $result = $this->Form->control('accept', [
  7498. 'type' => 'radio',
  7499. 'options' => ['Y', 'N'],
  7500. ]);
  7501. $this->assertStringContainsString('<div class="radio">', $result);
  7502. }
  7503. /**
  7504. * testResetTemplates method
  7505. *
  7506. * Test resetting templates.
  7507. */
  7508. public function testResetTemplates(): void
  7509. {
  7510. $this->Form->setTemplates(['input' => '<input/>']);
  7511. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7512. $this->Form->resetTemplates();
  7513. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7514. }
  7515. /**
  7516. * testContext method
  7517. *
  7518. * Test the context method.
  7519. */
  7520. public function testContext(): void
  7521. {
  7522. $result = $this->Form->context();
  7523. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7524. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7525. $this->assertSame($mock, $this->Form->context($mock));
  7526. $this->assertSame($mock, $this->Form->context());
  7527. }
  7528. /**
  7529. * testAutoDomId method
  7530. */
  7531. public function testAutoDomId(): void
  7532. {
  7533. $result = $this->Form->text('field', ['id' => true]);
  7534. $expected = [
  7535. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7536. ];
  7537. $this->assertHtml($expected, $result);
  7538. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7539. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7540. $expected = [
  7541. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7542. ['label' => ['for' => 'field-0']],
  7543. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7544. 'option A',
  7545. '/label',
  7546. ['label' => ['for' => 'field-1']],
  7547. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7548. 'option B',
  7549. '/label',
  7550. ];
  7551. $this->assertHtml($expected, $result);
  7552. $result = $this->Form->select(
  7553. 'multi_field',
  7554. ['first', 'second'],
  7555. ['multiple' => 'checkbox', 'id' => true]
  7556. );
  7557. $expected = [
  7558. 'input' => [
  7559. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7560. ],
  7561. ['div' => ['class' => 'checkbox']],
  7562. ['label' => ['for' => 'multi-field-0']],
  7563. ['input' => [
  7564. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7565. 'value' => '0', 'id' => 'multi-field-0',
  7566. ]],
  7567. 'first',
  7568. '/label',
  7569. '/div',
  7570. ['div' => ['class' => 'checkbox']],
  7571. ['label' => ['for' => 'multi-field-1']],
  7572. ['input' => [
  7573. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7574. 'value' => '1', 'id' => 'multi-field-1',
  7575. ]],
  7576. 'second',
  7577. '/label',
  7578. '/div',
  7579. ];
  7580. $this->assertHtml($expected, $result);
  7581. }
  7582. /**
  7583. * Test the basic setters and getters for value sources
  7584. */
  7585. public function testFormValueSourcesSettersGetters(): void
  7586. {
  7587. $this->View->setRequest($this->View->getRequest()
  7588. ->withData('id', '1')
  7589. ->withQueryParams(['id' => '2']));
  7590. $expected = ['data', 'context'];
  7591. $result = $this->Form->getValueSources();
  7592. $this->assertEquals($expected, $result);
  7593. $this->Form->setValueSources(['context']);
  7594. $result = $this->Form->getSourceValue('id');
  7595. $this->assertNull($result);
  7596. $this->Form->setValueSources('query');
  7597. $expected = ['query'];
  7598. $result = $this->Form->getValueSources();
  7599. $this->assertEquals($expected, $result);
  7600. $expected = '2';
  7601. $result = $this->Form->getSourceValue('id');
  7602. $this->assertSame($expected, $result);
  7603. $this->Form->setValueSources(['data']);
  7604. $expected = '1';
  7605. $result = $this->Form->getSourceValue('id');
  7606. $this->assertSame($expected, $result);
  7607. $this->Form->setValueSources(['query', 'data']);
  7608. $expected = '2';
  7609. $result = $this->Form->getSourceValue('id');
  7610. $this->assertSame($expected, $result);
  7611. }
  7612. public function testValueSourcesValidation(): void
  7613. {
  7614. $this->expectException(InvalidArgumentException::class);
  7615. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7616. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7617. }
  7618. /**
  7619. * Tests the different input rendering values based on sources values switching
  7620. */
  7621. public function testFormValueSourcesSingleSwitchRendering(): void
  7622. {
  7623. $articles = $this->getTableLocator()->get('Articles');
  7624. $article = new Article();
  7625. $articles->patchEntity($article, ['id' => '3']);
  7626. $this->Form->create($article);
  7627. $this->Form->setValueSources(['context']);
  7628. $result = $this->Form->control('id');
  7629. $expected = [
  7630. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7631. ];
  7632. $this->assertHtml($expected, $result);
  7633. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7634. $this->Form->setValueSources(['query']);
  7635. $result = $this->Form->control('id');
  7636. $expected = [
  7637. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7638. ];
  7639. $this->assertHtml($expected, $result);
  7640. $this->View->setRequest($this->View->getRequest()
  7641. ->withQueryParams(['id' => '5a'])
  7642. ->withData('id', '5b'));
  7643. $this->Form->setValueSources(['context']);
  7644. $this->Form->create($article);
  7645. $result = $this->Form->control('id');
  7646. $expected = [
  7647. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7648. ];
  7649. $this->assertHtml($expected, $result);
  7650. $this->Form->setValueSources(['data']);
  7651. $this->Form->create($article);
  7652. $result = $this->Form->control('id');
  7653. $expected = [
  7654. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7655. ];
  7656. $this->assertHtml($expected, $result);
  7657. $this->Form->setValueSources(['query']);
  7658. $result = $this->Form->control('id');
  7659. $expected = [
  7660. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7661. ];
  7662. $this->assertHtml($expected, $result);
  7663. }
  7664. /**
  7665. * Tests the different input rendering values based on sources values switching while supplying
  7666. * an entity (base context) and multiple sources (such as data, query)
  7667. */
  7668. public function testFormValueSourcesListSwitchRendering(): void
  7669. {
  7670. $articles = $this->getTableLocator()->get('Articles');
  7671. $article = new Article();
  7672. $articles->patchEntity($article, ['id' => '3']);
  7673. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7674. $this->Form->create($article);
  7675. $this->Form->setValueSources(['context', 'query']);
  7676. $result = $this->Form->control('id');
  7677. $expected = [
  7678. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7679. ];
  7680. $this->assertHtml($expected, $result);
  7681. $this->Form->setValueSources(['query', 'context']);
  7682. $result = $this->Form->control('id');
  7683. $expected = [
  7684. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7685. ];
  7686. $this->assertHtml($expected, $result);
  7687. $this->Form->setValueSources(['data', 'query', 'context']);
  7688. $result = $this->Form->control('id');
  7689. $expected = [
  7690. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7691. ];
  7692. $this->assertHtml($expected, $result);
  7693. $this->View->setRequest($this->View->getRequest()
  7694. ->withData('id', '8')
  7695. ->withQueryParams(['id' => '9']));
  7696. $this->Form->setValueSources(['data', 'query', 'context']);
  7697. $result = $this->Form->control('id');
  7698. $expected = [
  7699. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7700. ];
  7701. $this->assertHtml($expected, $result);
  7702. }
  7703. /**
  7704. * Test the different form input renderings based on values sources switchings through form options
  7705. */
  7706. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7707. {
  7708. $articles = $this->getTableLocator()->get('Articles');
  7709. $article = new Article();
  7710. $articles->patchEntity($article, ['id' => '3']);
  7711. $this->View->setRequest(
  7712. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7713. );
  7714. $this->Form->create($article, ['valueSources' => 'query']);
  7715. $result = $this->Form->control('id');
  7716. $expected = [
  7717. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7718. ];
  7719. $this->assertHtml($expected, $result);
  7720. $result = $this->Form->getSourceValue('id');
  7721. $this->assertSame('5', $result);
  7722. $this->Form->setValueSources(['context']);
  7723. $this->Form->create($article, ['valueSources' => 'query']);
  7724. $result = $this->Form->control('id');
  7725. $expected = [
  7726. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7727. ];
  7728. $this->assertHtml($expected, $result);
  7729. $result = $this->Form->getSourceValue('id');
  7730. $this->assertSame('5', $result);
  7731. $this->Form->setValueSources(['query']);
  7732. $this->Form->create($article, ['valueSources' => 'data']);
  7733. $result = $this->Form->control('id');
  7734. $expected = [
  7735. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7736. ];
  7737. $this->assertHtml($expected, $result);
  7738. $result = $this->Form->getSourceValue('id');
  7739. $this->assertSame('4', $result);
  7740. $this->Form->setValueSources(['query']);
  7741. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7742. $result = $this->Form->control('id');
  7743. $expected = [
  7744. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7745. ];
  7746. $this->assertHtml($expected, $result);
  7747. $result = $this->Form->getSourceValue('id');
  7748. $this->assertSame(3, $result);
  7749. }
  7750. /**
  7751. * Test the different form input renderings based on values sources switchings through form options
  7752. */
  7753. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7754. {
  7755. $articles = $this->getTableLocator()->get('Articles');
  7756. $article = new Article();
  7757. $articles->patchEntity($article, ['id' => '3']);
  7758. $this->View->setRequest(
  7759. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7760. );
  7761. $this->Form->setValueSources(['context'])
  7762. ->create($article, ['valueSources' => ['query', 'data']]);
  7763. $result = $this->Form->control('id');
  7764. $expected = [
  7765. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7766. ];
  7767. $this->assertHtml($expected, $result);
  7768. $result = $this->Form->getSourceValue('id');
  7769. $this->assertSame('11', $result);
  7770. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7771. $this->Form->setValueSources(['context'])
  7772. ->create($article, ['valueSources' => ['query', 'data']]);
  7773. $result = $this->Form->control('id');
  7774. $expected = [
  7775. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7776. ];
  7777. $this->assertHtml($expected, $result);
  7778. $result = $this->Form->getSourceValue('id');
  7779. $this->assertSame('10', $result);
  7780. }
  7781. /**
  7782. * Test the different form values sources resetting through From::end();
  7783. */
  7784. public function testFormValueSourcesResetViaEnd(): void
  7785. {
  7786. $expected = ['data', 'context'];
  7787. $result = $this->Form->getValueSources();
  7788. $this->assertEquals($expected, $result);
  7789. $expected = ['query', 'context', 'data'];
  7790. $this->Form->setValueSources(['query', 'context', 'data']);
  7791. $result = $this->Form->getValueSources();
  7792. $this->assertEquals($expected, $result);
  7793. $this->Form->create();
  7794. $result = $this->Form->getValueSources();
  7795. $this->assertEquals($expected, $result);
  7796. $this->Form->end();
  7797. $result = $this->Form->getValueSources();
  7798. $this->assertEquals(['data', 'context'], $result);
  7799. }
  7800. /**
  7801. * Test sources values defaults handling
  7802. */
  7803. public function testFormValueSourcesDefaults(): void
  7804. {
  7805. $this->View->setRequest(
  7806. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7807. );
  7808. $this->Form->create();
  7809. $result = $this->Form->password('password');
  7810. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7811. $this->assertHtml($expected, $result);
  7812. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7813. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7814. $this->assertHtml($expected, $result);
  7815. $this->Form->setValueSources('query');
  7816. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7817. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7818. $this->assertHtml($expected, $result);
  7819. $this->Form->setValueSources('data');
  7820. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7821. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7822. $this->assertHtml($expected, $result);
  7823. }
  7824. /**
  7825. * Test sources values schema defaults handling
  7826. */
  7827. public function testSourcesValueDoesntExistPassThrough(): void
  7828. {
  7829. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7830. $articles = $this->getTableLocator()->get('Articles');
  7831. $entity = $articles->newEmptyEntity();
  7832. $this->Form->create($entity);
  7833. $this->Form->setValueSources(['query', 'context']);
  7834. $result = $this->Form->getSourceValue('category');
  7835. $this->assertSame('sesame-cookies', $result);
  7836. $this->Form->setValueSources(['context', 'query']);
  7837. $result = $this->Form->getSourceValue('category');
  7838. $this->assertSame('sesame-cookies', $result);
  7839. }
  7840. /**
  7841. * testNestedLabelInput method
  7842. *
  7843. * Test the `nestedInput` parameter
  7844. */
  7845. public function testNestedLabelInput(): void
  7846. {
  7847. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7848. $expected = [
  7849. 'div' => ['class' => 'input text'],
  7850. 'label' => ['for' => 'foo'],
  7851. ['input' => [
  7852. 'type' => 'text',
  7853. 'name' => 'foo',
  7854. 'id' => 'foo',
  7855. ]],
  7856. 'Foo',
  7857. '/label',
  7858. '/div',
  7859. ];
  7860. $this->assertHtml($expected, $result);
  7861. }
  7862. /**
  7863. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7864. *
  7865. * This test makes sure `false` excludes the label from the render
  7866. */
  7867. public function testControlLabelManipulationDisableLabels(): void
  7868. {
  7869. $result = $this->Form->control('test', [
  7870. 'type' => 'radio',
  7871. 'options' => ['A', 'B'],
  7872. 'labelOptions' => false,
  7873. ]);
  7874. $expected = [
  7875. ['div' => ['class' => 'input radio']],
  7876. '<label',
  7877. 'Test',
  7878. '/label',
  7879. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7880. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7881. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7882. '/div',
  7883. ];
  7884. $this->assertHtml($expected, $result);
  7885. $result = $this->Form->control('checkbox1', [
  7886. 'label' => 'My checkboxes',
  7887. 'multiple' => 'checkbox',
  7888. 'type' => 'select',
  7889. 'options' => [
  7890. ['text' => 'First Checkbox', 'value' => 1],
  7891. ['text' => 'Second Checkbox', 'value' => 2],
  7892. ],
  7893. 'labelOptions' => false,
  7894. ]);
  7895. $expected = [
  7896. ['div' => ['class' => 'input select']],
  7897. ['label' => ['for' => 'checkbox1']],
  7898. 'My checkboxes',
  7899. '/label',
  7900. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7901. ['div' => ['class' => 'checkbox']],
  7902. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7903. '/div',
  7904. ['div' => ['class' => 'checkbox']],
  7905. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7906. '/div',
  7907. '/div',
  7908. ];
  7909. $this->assertHtml($expected, $result);
  7910. }
  7911. /**
  7912. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7913. *
  7914. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7915. * added to the class if checked.
  7916. *
  7917. * Also checks to make sure any custom attributes are rendered correctly
  7918. */
  7919. public function testControlLabelManipulationRadios(): void
  7920. {
  7921. $result = $this->Form->control('test', [
  7922. 'type' => 'radio',
  7923. 'options' => ['A', 'B'],
  7924. 'labelOptions' => ['class' => 'custom-class'],
  7925. ]);
  7926. $expected = [
  7927. ['div' => ['class' => 'input radio']],
  7928. '<label',
  7929. 'Test',
  7930. '/label',
  7931. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7932. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7933. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7934. 'A',
  7935. '/label',
  7936. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7937. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7938. 'B',
  7939. '/label',
  7940. '/div',
  7941. ];
  7942. $this->assertHtml($expected, $result);
  7943. $result = $this->Form->control('test', [
  7944. 'type' => 'radio',
  7945. 'options' => ['A', 'B'],
  7946. 'value' => 1,
  7947. 'labelOptions' => ['class' => 'custom-class'],
  7948. ]);
  7949. $expected = [
  7950. ['div' => ['class' => 'input radio']],
  7951. '<label',
  7952. 'Test',
  7953. '/label',
  7954. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7955. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7956. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7957. 'A',
  7958. '/label',
  7959. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7960. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7961. 'B',
  7962. '/label',
  7963. '/div',
  7964. ];
  7965. $this->assertHtml($expected, $result);
  7966. $result = $this->Form->control('test', [
  7967. 'type' => 'radio',
  7968. 'options' => ['A', 'B'],
  7969. 'value' => 1,
  7970. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7971. ]);
  7972. $expected = [
  7973. ['div' => ['class' => 'input radio']],
  7974. '<label',
  7975. 'Test',
  7976. '/label',
  7977. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7978. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7979. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7980. 'A',
  7981. '/label',
  7982. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7983. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7984. 'B',
  7985. '/label',
  7986. '/div',
  7987. ];
  7988. $this->assertHtml($expected, $result);
  7989. $result = $this->Form->radio('test', ['A', 'B'], [
  7990. 'label' => [
  7991. 'class' => ['custom-class', 'another-class'],
  7992. 'data-name' => 'bob',
  7993. ],
  7994. 'value' => 1,
  7995. ]);
  7996. $expected = [
  7997. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  7998. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7999. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8000. 'A',
  8001. '/label',
  8002. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8003. ['input' => [
  8004. 'type' => 'radio',
  8005. 'name' => 'test',
  8006. 'value' => '1',
  8007. 'id' => 'test-1',
  8008. 'checked' => 'checked',
  8009. ]],
  8010. 'B',
  8011. '/label',
  8012. ];
  8013. $this->assertHtml($expected, $result);
  8014. }
  8015. /**
  8016. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8017. *
  8018. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8019. * added to the class if checked.
  8020. *
  8021. * Also checks to make sure any custom attributes are rendered correctly
  8022. */
  8023. public function testControlLabelManipulationCheckboxes(): void
  8024. {
  8025. $result = $this->Form->control('checkbox1', [
  8026. 'label' => 'My checkboxes',
  8027. 'multiple' => 'checkbox',
  8028. 'type' => 'select',
  8029. 'options' => [
  8030. ['text' => 'First Checkbox', 'value' => 1],
  8031. ['text' => 'Second Checkbox', 'value' => 2],
  8032. ],
  8033. 'labelOptions' => ['class' => 'custom-class'],
  8034. 'value' => ['1'],
  8035. ]);
  8036. $expected = [
  8037. ['div' => ['class' => 'input select']],
  8038. ['label' => ['for' => 'checkbox1']],
  8039. 'My checkboxes',
  8040. '/label',
  8041. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8042. ['div' => ['class' => 'checkbox']],
  8043. ['label' => [
  8044. 'class' => 'custom-class selected',
  8045. 'for' => 'checkbox1-1',
  8046. ]],
  8047. ['input' => [
  8048. 'type' => 'checkbox',
  8049. 'name' => 'checkbox1[]',
  8050. 'value' => '1',
  8051. 'id' => 'checkbox1-1',
  8052. 'checked' => 'checked',
  8053. ]],
  8054. 'First Checkbox',
  8055. '/label',
  8056. '/div',
  8057. ['div' => ['class' => 'checkbox']],
  8058. ['label' => [
  8059. 'class' => 'custom-class',
  8060. 'for' => 'checkbox1-2',
  8061. ]],
  8062. ['input' => [
  8063. 'type' => 'checkbox',
  8064. 'name' => 'checkbox1[]',
  8065. 'value' => '2',
  8066. 'id' => 'checkbox1-2',
  8067. ]],
  8068. 'Second Checkbox',
  8069. '/label',
  8070. '/div',
  8071. '/div',
  8072. ];
  8073. $this->assertHtml($expected, $result);
  8074. $result = $this->Form->control('checkbox1', [
  8075. 'label' => 'My checkboxes',
  8076. 'multiple' => 'checkbox',
  8077. 'type' => 'select',
  8078. 'options' => [
  8079. ['text' => 'First Checkbox', 'value' => 1],
  8080. ['text' => 'Second Checkbox', 'value' => 2],
  8081. ],
  8082. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8083. 'value' => ['1'],
  8084. ]);
  8085. $expected = [
  8086. ['div' => ['class' => 'input select']],
  8087. ['label' => ['for' => 'checkbox1']],
  8088. 'My checkboxes',
  8089. '/label',
  8090. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8091. ['div' => ['class' => 'checkbox']],
  8092. ['label' => [
  8093. 'class' => 'custom-class another-class selected',
  8094. 'data-name' => 'bob',
  8095. 'for' => 'checkbox1-1',
  8096. ]],
  8097. ['input' => [
  8098. 'type' => 'checkbox',
  8099. 'name' => 'checkbox1[]',
  8100. 'value' => '1',
  8101. 'id' => 'checkbox1-1',
  8102. 'checked' => 'checked',
  8103. ]],
  8104. 'First Checkbox',
  8105. '/label',
  8106. '/div',
  8107. ['div' => ['class' => 'checkbox']],
  8108. ['label' => [
  8109. 'class' => 'custom-class another-class',
  8110. 'data-name' => 'bob',
  8111. 'for' => 'checkbox1-2',
  8112. ]],
  8113. ['input' => [
  8114. 'type' => 'checkbox',
  8115. 'name' => 'checkbox1[]',
  8116. 'value' => '2',
  8117. 'id' => 'checkbox1-2',
  8118. ]],
  8119. 'Second Checkbox',
  8120. '/label',
  8121. '/div',
  8122. '/div',
  8123. ];
  8124. $this->assertHtml($expected, $result);
  8125. }
  8126. /**
  8127. * testControlMaxLengthArrayContext method
  8128. *
  8129. * Test control() with maxlength attribute in Array Context.
  8130. */
  8131. public function testControlMaxLengthArrayContext(): void
  8132. {
  8133. $this->article['schema'] = [
  8134. 'title' => ['length' => 10],
  8135. ];
  8136. $this->Form->create($this->article);
  8137. $result = $this->Form->control('title');
  8138. $expected = [
  8139. 'div' => ['class'],
  8140. 'label' => ['for'],
  8141. 'Title',
  8142. '/label',
  8143. 'input' => [
  8144. 'aria-required' => 'true',
  8145. 'id',
  8146. 'name' => 'title',
  8147. 'type' => 'text',
  8148. 'required' => 'required',
  8149. 'maxlength' => 10,
  8150. 'data-validity-message' => 'This field cannot be left empty',
  8151. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8152. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8153. ],
  8154. '/div',
  8155. ];
  8156. $this->assertHtml($expected, $result);
  8157. }
  8158. /**
  8159. * testControlMaxLengthEntityContext method
  8160. *
  8161. * Test control() with maxlength attribute in Entity Context.
  8162. */
  8163. public function testControlMaxLengthEntityContext(): void
  8164. {
  8165. $this->article['schema']['title']['length'] = 45;
  8166. $validator = new Validator();
  8167. $validator->maxLength('title', 10);
  8168. $article = new EntityContext(
  8169. [
  8170. 'entity' => new Entity($this->article),
  8171. 'table' => new Table([
  8172. 'alias' => 'Articles',
  8173. 'schema' => $this->article['schema'],
  8174. 'validator' => $validator,
  8175. ]),
  8176. ]
  8177. );
  8178. $this->Form->create($article);
  8179. $result = $this->Form->control('title');
  8180. $expected = [
  8181. 'div' => ['class'],
  8182. 'label' => ['for'],
  8183. 'Title',
  8184. '/label',
  8185. 'input' => [
  8186. 'aria-required' => 'true',
  8187. 'id',
  8188. 'name' => 'title',
  8189. 'type' => 'text',
  8190. 'required' => 'required',
  8191. 'maxlength' => 10,
  8192. 'data-validity-message' => 'This field cannot be left empty',
  8193. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8194. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8195. ],
  8196. '/div',
  8197. ];
  8198. $this->assertHtml($expected, $result);
  8199. $this->article['schema']['title']['length'] = 45;
  8200. $validator = new Validator();
  8201. $validator->maxLength('title', 55);
  8202. $article = new EntityContext(
  8203. [
  8204. 'entity' => new Entity($this->article),
  8205. 'table' => new Table([
  8206. 'schema' => $this->article['schema'],
  8207. 'validator' => $validator,
  8208. 'alias' => 'Articles',
  8209. ]),
  8210. ]
  8211. );
  8212. $this->Form->create($article);
  8213. $result = $this->Form->control('title');
  8214. $expected = [
  8215. 'div' => ['class'],
  8216. 'label' => ['for'],
  8217. 'Title',
  8218. '/label',
  8219. 'input' => [
  8220. 'aria-required' => 'true',
  8221. 'id',
  8222. 'name' => 'title',
  8223. 'type' => 'text',
  8224. 'required' => 'required',
  8225. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8226. 'data-validity-message' => 'This field cannot be left empty',
  8227. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8228. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8229. ],
  8230. '/div',
  8231. ];
  8232. $this->assertHtml($expected, $result);
  8233. $this->article['schema']['title']['length'] = 45;
  8234. $validator = new Validator();
  8235. $validator->maxLength('title', 55);
  8236. $article = new EntityContext(
  8237. [
  8238. 'entity' => new Entity($this->article),
  8239. 'table' => new Table([
  8240. 'schema' => $this->article['schema'],
  8241. 'validator' => $validator,
  8242. 'alias' => 'Articles',
  8243. ]),
  8244. ]
  8245. );
  8246. $this->Form->create($article);
  8247. $result = $this->Form->control('title', ['maxlength' => 10]);
  8248. $expected = [
  8249. 'div' => ['class'],
  8250. 'label' => ['for'],
  8251. 'Title',
  8252. '/label',
  8253. 'input' => [
  8254. 'aria-required' => 'true',
  8255. 'id',
  8256. 'name' => 'title',
  8257. 'type' => 'text',
  8258. 'required' => 'required',
  8259. 'maxlength' => 10, // Length set in options should take highest precedence.
  8260. 'data-validity-message' => 'This field cannot be left empty',
  8261. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8262. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8263. ],
  8264. '/div',
  8265. ];
  8266. $this->assertHtml($expected, $result);
  8267. }
  8268. /**
  8269. * testControlMinMaxLengthEntityContext method
  8270. *
  8271. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8272. */
  8273. public function testControlMinMaxLengthEntityContext(): void
  8274. {
  8275. $validator = new Validator();
  8276. $validator->maxLength('title', 10);
  8277. $article = new EntityContext(
  8278. [
  8279. 'entity' => new Entity($this->article),
  8280. 'table' => new Table([
  8281. 'alias' => 'Articles',
  8282. 'schema' => $this->article['schema'],
  8283. 'validator' => $validator,
  8284. ]),
  8285. ]
  8286. );
  8287. $this->Form->create($article);
  8288. $result = $this->Form->control('title');
  8289. $expected = [
  8290. 'div' => ['class'],
  8291. 'label' => ['for'],
  8292. 'Title',
  8293. '/label',
  8294. 'input' => [
  8295. 'aria-required' => 'true',
  8296. 'id',
  8297. 'name' => 'title',
  8298. 'type' => 'text',
  8299. 'required' => 'required',
  8300. 'maxlength' => 10,
  8301. 'data-validity-message' => 'This field cannot be left empty',
  8302. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8303. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8304. ],
  8305. '/div',
  8306. ];
  8307. $this->assertHtml($expected, $result);
  8308. }
  8309. /**
  8310. * testControlMaxLengthFormContext method
  8311. *
  8312. * Test control() with maxlength attribute in Form Context.
  8313. */
  8314. public function testControlMaxLengthFormContext(): void
  8315. {
  8316. $validator = new Validator();
  8317. $validator->maxLength('title', 10);
  8318. $form = new Form();
  8319. $form->setValidator('default', $validator);
  8320. $this->Form->create($form);
  8321. $result = $this->Form->control('title');
  8322. $expected = [
  8323. 'div' => ['class'],
  8324. 'label' => ['for'],
  8325. 'Title',
  8326. '/label',
  8327. 'input' => [
  8328. 'aria-required' => 'true',
  8329. 'id',
  8330. 'name' => 'title',
  8331. 'type' => 'text',
  8332. 'required' => 'required',
  8333. 'maxlength' => 10,
  8334. 'data-validity-message' => 'This field cannot be left empty',
  8335. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8336. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8337. ],
  8338. '/div',
  8339. ];
  8340. $this->assertHtml($expected, $result);
  8341. }
  8342. }