FormHelperTest.php 305 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use ArrayObject;
  18. use Cake\Collection\Collection;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Exception\CakeException;
  21. use Cake\Form\Form;
  22. use Cake\Http\ServerRequest;
  23. use Cake\I18n\Date;
  24. use Cake\I18n\DateTime;
  25. use Cake\ORM\Entity;
  26. use Cake\ORM\Table;
  27. use Cake\Routing\Router;
  28. use Cake\TestSuite\TestCase;
  29. use Cake\Utility\Security;
  30. use Cake\Validation\Validator;
  31. use Cake\View\Form\EntityContext;
  32. use Cake\View\Helper\FormHelper;
  33. use Cake\View\View;
  34. use Cake\View\Widget\WidgetLocator;
  35. use InvalidArgumentException;
  36. use ReflectionProperty;
  37. use TestApp\Model\Entity\Article;
  38. use TestApp\Model\Table\ContactsTable;
  39. use TestApp\Model\Table\ValidateUsersTable;
  40. /**
  41. * FormHelperTest class
  42. *
  43. * @property \Cake\View\Helper\FormHelper $Form
  44. * @property \Cake\View\View $View
  45. */
  46. class FormHelperTest extends TestCase
  47. {
  48. /**
  49. * Fixtures to be used
  50. *
  51. * @var array<string>
  52. */
  53. protected array $fixtures = ['core.Articles', 'core.Comments'];
  54. /**
  55. * @var array
  56. */
  57. protected $article = [];
  58. /**
  59. * @var string
  60. */
  61. protected $url;
  62. /**
  63. * @var \Cake\View\Helper\FormHelper
  64. */
  65. protected $Form;
  66. /**
  67. * @var \Cake\View\View
  68. */
  69. protected $View;
  70. /**
  71. * setUp method
  72. */
  73. public function setUp(): void
  74. {
  75. parent::setUp();
  76. Configure::write('Config.language', 'eng');
  77. Configure::write('App.base', '');
  78. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  79. $request = new ServerRequest([
  80. 'webroot' => '',
  81. 'base' => '',
  82. 'url' => '/articles/add',
  83. 'params' => [
  84. 'controller' => 'Articles',
  85. 'action' => 'add',
  86. 'plugin' => null,
  87. ],
  88. ]);
  89. $this->View = new View($request);
  90. Router::reload();
  91. Router::setRequest($request);
  92. $this->url = '/articles/add';
  93. $this->Form = new FormHelper($this->View);
  94. $this->article = [
  95. 'schema' => [
  96. 'id' => ['type' => 'integer'],
  97. 'author_id' => ['type' => 'integer', 'null' => true],
  98. 'title' => ['type' => 'string', 'null' => true],
  99. 'body' => 'text',
  100. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  101. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  102. ],
  103. 'required' => [
  104. 'author_id' => true,
  105. 'title' => true,
  106. ],
  107. ];
  108. Security::setSalt('foo!');
  109. $builder = Router::createRouteBuilder('/');
  110. $builder->connect('/{controller}', ['action' => 'index']);
  111. $builder->connect('/{controller}/{action}/*');
  112. }
  113. /**
  114. * tearDown method
  115. */
  116. public function tearDown(): void
  117. {
  118. parent::tearDown();
  119. unset($this->Form, $this->Controller, $this->View);
  120. }
  121. /**
  122. * Test construct() with the templates option.
  123. */
  124. public function testConstructTemplatesFile(): void
  125. {
  126. $helper = new FormHelper($this->View, [
  127. 'templates' => 'htmlhelper_tags',
  128. ]);
  129. $result = $helper->control('name');
  130. $this->assertStringContainsString('<input', $result);
  131. }
  132. /**
  133. * Test that when specifying custom widgets the config array for that widget
  134. * is overwritten instead of merged.
  135. */
  136. public function testConstructWithWidgets(): void
  137. {
  138. $config = [
  139. 'widgets' => [
  140. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  141. ],
  142. ];
  143. $helper = new FormHelper($this->View, $config);
  144. $locator = $helper->getWidgetLocator();
  145. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  146. }
  147. /**
  148. * Test that when specifying custom widgets config file and it should be
  149. * added to widgets array. WidgetLocator will load widgets in constructor.
  150. */
  151. public function testConstructWithWidgetsConfig(): void
  152. {
  153. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  154. $locator = $helper->getWidgetLocator();
  155. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  156. }
  157. /**
  158. * Test setting the widget locator
  159. */
  160. public function testSetAndGetWidgetLocator(): void
  161. {
  162. $helper = new FormHelper($this->View);
  163. $locator = new WidgetLocator($helper->templater(), $this->View);
  164. $helper->setWidgetLocator($locator);
  165. $this->assertSame($locator, $helper->getWidgetLocator());
  166. }
  167. /**
  168. * Test overridding grouped input types which controls generation of "for"
  169. * attribute of labels.
  170. */
  171. public function testConstructWithGroupedInputTypes(): void
  172. {
  173. $helper = new FormHelper($this->View, [
  174. 'groupedInputTypes' => ['radio'],
  175. ]);
  176. $result = $helper->control('when', ['type' => 'datetime-local']);
  177. $this->assertStringContainsString('<label for="when">When</label>', $result);
  178. }
  179. /**
  180. * Test registering a new widget class and rendering it.
  181. */
  182. public function testAddWidgetAndRenderWidget(): void
  183. {
  184. $data = [
  185. 'val' => 1,
  186. ];
  187. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  188. $this->Form->addWidget('test', $mock);
  189. $mock->expects($this->once())
  190. ->method('render')
  191. ->with($data)
  192. ->will($this->returnValue('HTML'));
  193. $result = $this->Form->widget('test', $data);
  194. $this->assertSame('HTML', $result);
  195. }
  196. /**
  197. * Test that secureFields() of widget is called after calling render(),
  198. * not before.
  199. */
  200. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  201. {
  202. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  203. 'unlockedFields' => [],
  204. ]));
  205. $data = [
  206. 'val' => 1,
  207. 'name' => 'test',
  208. ];
  209. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  210. $this->Form->addWidget('test', $mock);
  211. $mock->expects($this->once())
  212. ->method('render')
  213. ->with($data)
  214. ->will($this->returnValue('HTML'));
  215. $mock->expects($this->once())
  216. ->method('secureFields')
  217. ->with($data)
  218. ->will($this->returnValue(['test']));
  219. $this->Form->create();
  220. $result = $this->Form->widget('test', $data + ['secure' => true]);
  221. $this->assertSame('HTML', $result);
  222. }
  223. /**
  224. * Test that empty string is not added to secure fields list when
  225. * rendering input widget without name.
  226. */
  227. public function testRenderingWidgetWithEmptyName(): void
  228. {
  229. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  230. $this->Form->create();
  231. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  232. $this->assertSame('<select name=""></select>', $result);
  233. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  234. $this->assertEquals([], $result);
  235. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  236. $this->assertSame('<select name="0"></select>', $result);
  237. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  238. $this->assertEquals(['0'], $result);
  239. }
  240. /**
  241. * Test adding a new context class.
  242. */
  243. public function testAddContextProvider(): void
  244. {
  245. $context = 'My data';
  246. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  247. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  248. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  249. $this->assertSame($context, $data['entity']);
  250. return $stub;
  251. });
  252. $this->Form->create($context);
  253. $result = $this->Form->context();
  254. $this->assertSame($stub, $result);
  255. }
  256. /**
  257. * Test replacing a context class.
  258. */
  259. public function testAddContextProviderReplace(): void
  260. {
  261. $entity = new Article();
  262. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  263. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  264. return $stub;
  265. });
  266. $this->Form->create($entity);
  267. $result = $this->Form->context();
  268. $this->assertSame($stub, $result);
  269. }
  270. /**
  271. * Test overriding a context class.
  272. */
  273. public function testAddContextProviderAdd(): void
  274. {
  275. $entity = new Article();
  276. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  277. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  278. if ($data['entity'] instanceof Entity) {
  279. return $stub;
  280. }
  281. });
  282. $this->Form->create($entity);
  283. $result = $this->Form->context();
  284. $this->assertSame($stub, $result);
  285. }
  286. /**
  287. * Provides context options for create().
  288. *
  289. * @return array
  290. */
  291. public function contextSelectionProvider(): array
  292. {
  293. $entity = new Article();
  294. $collection = new Collection([$entity]);
  295. $emptyCollection = new Collection([]);
  296. $arrayObject = new ArrayObject([]);
  297. $data = [
  298. 'schema' => [
  299. 'title' => ['type' => 'string'],
  300. ],
  301. ];
  302. $form = new Form();
  303. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  304. return [
  305. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  306. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  307. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  308. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  309. 'form' => [$form, 'Cake\View\Form\FormContext'],
  310. 'none' => [null, 'Cake\View\Form\NullContext'],
  311. 'custom' => [$custom, $custom::class],
  312. ];
  313. }
  314. /**
  315. * Test default context selection in create()
  316. *
  317. * @dataProvider contextSelectionProvider
  318. * @param mixed $data
  319. */
  320. public function testCreateContextSelectionBuiltIn($data, string $class): void
  321. {
  322. $this->Form->create($data);
  323. $this->assertInstanceOf($class, $this->Form->context());
  324. }
  325. /**
  326. * Data provider for type option.
  327. *
  328. * @return array
  329. */
  330. public static function requestTypeProvider(): array
  331. {
  332. return [
  333. // type, method, override
  334. ['post', 'post', 'POST'],
  335. ['put', 'post', 'PUT'],
  336. ['patch', 'post', 'PATCH'],
  337. ['delete', 'post', 'DELETE'],
  338. ];
  339. }
  340. /**
  341. * Test creating file forms.
  342. */
  343. public function testCreateFile(): void
  344. {
  345. $encoding = strtolower(Configure::read('App.encoding'));
  346. $result = $this->Form->create(null, ['type' => 'file']);
  347. $expected = [
  348. 'form' => [
  349. 'method' => 'post', 'action' => '/articles/add',
  350. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  351. ],
  352. 'div' => ['style' => 'display:none;'],
  353. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  354. '/div',
  355. ];
  356. $this->assertHtml($expected, $result);
  357. }
  358. /**
  359. * Test creating GET forms.
  360. */
  361. public function testCreateGet(): void
  362. {
  363. $encoding = strtolower(Configure::read('App.encoding'));
  364. $result = $this->Form->create(null, ['type' => 'get']);
  365. $expected = ['form' => [
  366. 'method' => 'get', 'action' => '/articles/add',
  367. 'accept-charset' => $encoding,
  368. ]];
  369. $this->assertHtml($expected, $result);
  370. }
  371. /**
  372. * Test explicit method/enctype options.
  373. *
  374. * Explicit method overwrites inferred method from 'type'
  375. */
  376. public function testCreateExplicitMethodEnctype(): void
  377. {
  378. $encoding = strtolower(Configure::read('App.encoding'));
  379. $result = $this->Form->create(null, [
  380. 'type' => 'get',
  381. 'method' => 'put',
  382. 'enctype' => 'multipart/form-data',
  383. ]);
  384. $expected = ['form' => [
  385. 'method' => 'put',
  386. 'action' => '/articles/add',
  387. 'enctype' => 'multipart/form-data',
  388. 'accept-charset' => $encoding,
  389. ]];
  390. $this->assertHtml($expected, $result);
  391. }
  392. /**
  393. * Test create() with the templates option.
  394. */
  395. public function testCreateTemplatesArray(): void
  396. {
  397. $result = $this->Form->create($this->article, [
  398. 'templates' => [
  399. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  400. ],
  401. ]);
  402. $expected = [
  403. 'form' => [
  404. 'class' => 'form-horizontal',
  405. 'method' => 'post',
  406. 'action' => '/articles/add',
  407. 'accept-charset' => 'utf-8',
  408. ],
  409. ];
  410. $this->assertHtml($expected, $result);
  411. }
  412. /**
  413. * Test create() with the templates option.
  414. */
  415. public function testCreateTemplatesFile(): void
  416. {
  417. $result = $this->Form->create($this->article, [
  418. 'templates' => 'htmlhelper_tags',
  419. ]);
  420. $expected = [
  421. 'start form',
  422. ];
  423. $this->assertHtml($expected, $result);
  424. }
  425. /**
  426. * Test that create() and end() restore templates.
  427. */
  428. public function testCreateEndRestoreTemplates(): void
  429. {
  430. $this->Form->create($this->article, [
  431. 'templates' => ['input' => 'custom input element'],
  432. ]);
  433. $this->Form->end();
  434. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  435. }
  436. /**
  437. * Test using template vars in various templates used by control() method.
  438. */
  439. public function testControlTemplateVars(): void
  440. {
  441. $result = $this->Form->control('text', [
  442. 'templates' => [
  443. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  444. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  445. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  446. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  447. ],
  448. 'templateVars' => [
  449. 'forinput' => 'in-input',
  450. 'forlabel' => 'in-label',
  451. 'forgroup' => 'in-group',
  452. 'forcontainer' => 'in-container',
  453. ],
  454. ]);
  455. $expected = [
  456. 'div' => ['class'],
  457. 'label' => ['for'],
  458. 'Text in-label',
  459. '/label',
  460. 'in-group',
  461. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  462. 'in-container',
  463. '/div',
  464. ];
  465. $this->assertHtml($expected, $result);
  466. }
  467. /**
  468. * Test ensuring template variables work in template files loaded
  469. * during control().
  470. */
  471. public function testControlTemplatesFromFile(): void
  472. {
  473. $result = $this->Form->control('title', [
  474. 'templates' => 'test_templates',
  475. 'templateVars' => [
  476. 'forcontainer' => 'container-data',
  477. ],
  478. ]);
  479. $expected = [
  480. 'div' => ['class'],
  481. 'label' => ['for'],
  482. 'Title',
  483. '/label',
  484. 'input' => ['name', 'type' => 'text', 'id'],
  485. 'container-data',
  486. '/div',
  487. ];
  488. $this->assertHtml($expected, $result);
  489. }
  490. /**
  491. * Test using template vars in inputSubmit and submitContainer template.
  492. */
  493. public function testSubmitTemplateVars(): void
  494. {
  495. $this->Form->setTemplates([
  496. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  497. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  498. ]);
  499. $result = $this->Form->submit('Submit', [
  500. 'templateVars' => [
  501. 'forinput' => 'in-input',
  502. 'forcontainer' => 'in-container',
  503. ],
  504. ]);
  505. $expected = [
  506. 'div' => ['class'],
  507. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  508. 'in-container',
  509. '/div',
  510. ];
  511. $this->assertHtml($expected, $result);
  512. }
  513. /**
  514. * test the create() method
  515. *
  516. * @dataProvider requestTypeProvider
  517. */
  518. public function testCreateTypeOptions(string $type, string $method, string $override): void
  519. {
  520. $encoding = strtolower(Configure::read('App.encoding'));
  521. $result = $this->Form->create(null, ['type' => $type]);
  522. $expected = [
  523. 'form' => [
  524. 'method' => $method, 'action' => '/articles/add',
  525. 'accept-charset' => $encoding,
  526. ],
  527. ];
  528. $extra = [
  529. 'div' => ['style' => 'display:none;'],
  530. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  531. '/div',
  532. ];
  533. if ($type !== 'post') {
  534. $expected = array_merge($expected, $extra);
  535. }
  536. $this->assertHtml($expected, $result);
  537. }
  538. /**
  539. * Test using template vars in Create (formStart template)
  540. */
  541. public function testCreateTemplateVars(): void
  542. {
  543. $result = $this->Form->create($this->article, [
  544. 'templates' => [
  545. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  546. ],
  547. 'templateVars' => ['header' => 'headertext'],
  548. ]);
  549. $expected = [
  550. 'h4' => ['class'],
  551. 'headertext',
  552. '/h4',
  553. 'form' => [
  554. 'method' => 'post',
  555. 'action' => '/articles/add',
  556. 'accept-charset' => 'utf-8',
  557. ],
  558. ];
  559. $this->assertHtml($expected, $result);
  560. }
  561. /**
  562. * Test opening a form for an update operation.
  563. */
  564. public function testCreateUpdateForm(): void
  565. {
  566. $encoding = strtolower(Configure::read('App.encoding'));
  567. $this->View->setRequest($this->View->getRequest()
  568. ->withRequestTarget('/articles/edit/1')
  569. ->withParam('action', 'edit'));
  570. $this->article['defaults']['id'] = 1;
  571. $result = $this->Form->create($this->article);
  572. $expected = [
  573. 'form' => [
  574. 'method' => 'post',
  575. 'action' => '/articles/edit/1',
  576. 'accept-charset' => $encoding,
  577. ],
  578. 'div' => ['style' => 'display:none;'],
  579. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  580. '/div',
  581. ];
  582. $this->assertHtml($expected, $result);
  583. }
  584. /**
  585. * test create() with automatic url generation
  586. */
  587. public function testCreateAutoUrl(): void
  588. {
  589. $encoding = strtolower(Configure::read('App.encoding'));
  590. $this->View->setRequest($this->View->getRequest()
  591. ->withRequestTarget('/articles/delete/10')
  592. ->withParam('action', 'delete'));
  593. $result = $this->Form->create($this->article);
  594. $expected = [
  595. 'form' => [
  596. 'method' => 'post', 'action' => '/articles/delete/10',
  597. 'accept-charset' => $encoding,
  598. ],
  599. ];
  600. $this->assertHtml($expected, $result);
  601. $this->article['defaults'] = ['id' => 1];
  602. $this->View->setRequest($this->View->getRequest()
  603. ->withRequestTarget('/Articles/edit/1')
  604. ->withParam('action', 'delete'));
  605. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  606. $expected = [
  607. 'form' => [
  608. 'method' => 'post',
  609. 'action' => '/Articles/edit/1',
  610. 'accept-charset' => $encoding,
  611. ],
  612. 'div' => ['style' => 'display:none;'],
  613. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  614. '/div',
  615. ];
  616. $this->assertHtml($expected, $result);
  617. $this->View->setRequest($this->View->getRequest()
  618. ->withParam('action', 'add'));
  619. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  620. $expected = [
  621. 'form' => [
  622. 'method' => 'post',
  623. 'action' => '/Articles/publish/1',
  624. 'accept-charset' => $encoding,
  625. ],
  626. 'div' => ['style' => 'display:none;'],
  627. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  628. '/div',
  629. ];
  630. $this->assertHtml($expected, $result);
  631. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  632. $expected = [
  633. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  634. 'div' => ['style' => 'display:none;'],
  635. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  636. '/div',
  637. ];
  638. $this->assertHtml($expected, $result);
  639. $this->View->setRequest($this->View->getRequest()
  640. ->withParam('controller', 'Pages'));
  641. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  642. $expected = [
  643. 'form' => [
  644. 'method' => 'post', 'action' => '/Pages/signup/1',
  645. 'accept-charset' => $encoding,
  646. ],
  647. 'div' => ['style' => 'display:none;'],
  648. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  649. '/div',
  650. ];
  651. $this->assertHtml($expected, $result);
  652. }
  653. /**
  654. * Test create() with no URL (no "action" attribute for <form> tag)
  655. */
  656. public function testCreateNoUrl(): void
  657. {
  658. $result = $this->Form->create(null, ['url' => false]);
  659. $expected = [
  660. 'form' => [
  661. 'method' => 'post',
  662. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  663. ],
  664. ];
  665. $this->assertHtml($expected, $result);
  666. }
  667. /**
  668. * test create() with a custom route
  669. */
  670. public function testCreateCustomRoute(): void
  671. {
  672. $builder = Router::createRouteBuilder('/');
  673. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  674. $encoding = strtolower(Configure::read('App.encoding'));
  675. $this->View->setRequest($this->View->getRequest()
  676. ->withParam('controller', 'Users'));
  677. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  678. $expected = [
  679. 'form' => [
  680. 'method' => 'post', 'action' => '/login',
  681. 'accept-charset' => $encoding,
  682. ],
  683. ];
  684. $this->assertHtml($expected, $result);
  685. $builder->connect(
  686. '/new-article',
  687. ['controller' => 'Articles', 'action' => 'myAction'],
  688. ['_name' => 'my-route']
  689. );
  690. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  691. $expected = [
  692. 'form' => [
  693. 'method' => 'post', 'action' => '/new-article',
  694. 'accept-charset' => $encoding,
  695. ],
  696. ];
  697. $this->assertHtml($expected, $result);
  698. }
  699. /**
  700. * test automatic accept-charset overriding
  701. */
  702. public function testCreateWithAcceptCharset(): void
  703. {
  704. $result = $this->Form->create(
  705. $this->article,
  706. [
  707. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  708. ]
  709. );
  710. $expected = [
  711. 'form' => [
  712. 'method' => 'post', 'action' => '/Articles',
  713. 'accept-charset' => 'iso-8859-1',
  714. ],
  715. ];
  716. $this->assertHtml($expected, $result);
  717. }
  718. /**
  719. * Test base form URL when 'url' param is passed with multiple parameters (&)
  720. */
  721. public function testCreateQueryStringRequest(): void
  722. {
  723. $encoding = strtolower(Configure::read('App.encoding'));
  724. $result = $this->Form->create($this->article, [
  725. 'type' => 'post',
  726. 'escape' => false,
  727. 'url' => [
  728. 'controller' => 'Controller',
  729. 'action' => 'action',
  730. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  731. ],
  732. ]);
  733. $expected = [
  734. 'form' => [
  735. 'method' => 'post',
  736. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  737. 'accept-charset' => $encoding,
  738. ],
  739. ];
  740. $this->assertHtml($expected, $result);
  741. $result = $this->Form->create($this->article, [
  742. 'type' => 'post',
  743. 'url' => [
  744. 'controller' => 'Controller',
  745. 'action' => 'action',
  746. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  747. ],
  748. ]);
  749. $this->assertHtml($expected, $result);
  750. }
  751. /**
  752. * test that create() doesn't cause errors by multiple id's being in the primary key
  753. * as could happen with multiple select or checkboxes.
  754. */
  755. public function testCreateWithMultipleIdInData(): void
  756. {
  757. $encoding = strtolower(Configure::read('App.encoding'));
  758. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  759. $result = $this->Form->create($this->article);
  760. $expected = [
  761. 'form' => [
  762. 'method' => 'post',
  763. 'action' => '/articles/add',
  764. 'accept-charset' => $encoding,
  765. ],
  766. ];
  767. $this->assertHtml($expected, $result);
  768. }
  769. /**
  770. * test that create() doesn't add in extra passed params.
  771. */
  772. public function testCreatePassedArgs(): void
  773. {
  774. $encoding = strtolower(Configure::read('App.encoding'));
  775. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  776. $result = $this->Form->create($this->article, [
  777. 'type' => 'post',
  778. 'escape' => false,
  779. 'url' => [
  780. 'action' => 'edit',
  781. 'myparam',
  782. ],
  783. ]);
  784. $expected = [
  785. 'form' => [
  786. 'method' => 'post',
  787. 'action' => '/Articles/edit/myparam',
  788. 'accept-charset' => $encoding,
  789. ],
  790. ];
  791. $this->assertHtml($expected, $result);
  792. }
  793. /**
  794. * test creating a get form, and get form inputs.
  795. */
  796. public function testGetFormCreate(): void
  797. {
  798. $encoding = strtolower(Configure::read('App.encoding'));
  799. $result = $this->Form->create($this->article, ['type' => 'get']);
  800. $expected = ['form' => [
  801. 'method' => 'get', 'action' => '/articles/add',
  802. 'accept-charset' => $encoding,
  803. ]];
  804. $this->assertHtml($expected, $result);
  805. $result = $this->Form->text('title');
  806. $expected = ['input' => [
  807. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  808. ]];
  809. $this->assertHtml($expected, $result);
  810. $result = $this->Form->password('password');
  811. $expected = ['input' => [
  812. 'name' => 'password', 'type' => 'password',
  813. ]];
  814. $this->assertHtml($expected, $result);
  815. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  816. $result = $this->Form->text('user_form');
  817. $expected = ['input' => [
  818. 'name' => 'user_form', 'type' => 'text',
  819. ]];
  820. $this->assertHtml($expected, $result);
  821. }
  822. /**
  823. * test get form, and inputs when the model param is false
  824. */
  825. public function testGetFormWithFalseModel(): void
  826. {
  827. $encoding = strtolower(Configure::read('App.encoding'));
  828. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  829. $result = $this->Form->create(null, [
  830. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  831. ]);
  832. $expected = ['form' => [
  833. 'method' => 'get', 'action' => '/ContactTest/add',
  834. 'accept-charset' => $encoding,
  835. ]];
  836. $this->assertHtml($expected, $result);
  837. $result = $this->Form->text('reason');
  838. $expected = [
  839. 'input' => ['type' => 'text', 'name' => 'reason'],
  840. ];
  841. $this->assertHtml($expected, $result);
  842. }
  843. /**
  844. * testFormCreateWithSecurity method
  845. *
  846. * Test form->create() with security key.
  847. */
  848. public function testCreateWithSecurity(): void
  849. {
  850. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  851. $encoding = strtolower(Configure::read('App.encoding'));
  852. $result = $this->Form->create($this->article, [
  853. 'url' => '/articles/publish',
  854. ]);
  855. $expected = [
  856. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  857. 'div' => ['style' => 'display:none;'],
  858. ['input' => [
  859. 'type' => 'hidden',
  860. 'name' => '_csrfToken',
  861. 'value' => 'testKey',
  862. 'autocomplete' => 'off',
  863. ]],
  864. '/div',
  865. ];
  866. $this->assertHtml($expected, $result);
  867. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  868. $expected['form']['id'] = 'MyForm';
  869. $this->assertHtml($expected, $result);
  870. }
  871. /**
  872. * testFormCreateGetNoSecurity method
  873. *
  874. * Test form->create() with no security key as its a get form
  875. */
  876. public function testCreateEndGetNoSecurity(): void
  877. {
  878. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  879. $article = new Article();
  880. $result = $this->Form->create($article, [
  881. 'type' => 'get',
  882. 'url' => '/contacts/add',
  883. ]);
  884. $this->assertStringNotContainsString('testKey', $result);
  885. $result = $this->Form->end();
  886. $this->assertStringNotContainsString('testKey', $result);
  887. }
  888. /**
  889. * Tests form hash generation with model-less data
  890. */
  891. public function testValidateHashNoModel(): void
  892. {
  893. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  894. $fields = ['anything'];
  895. $this->Form->create();
  896. $result = $this->Form->secure($fields);
  897. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  898. $this->assertStringContainsString($hash, $result);
  899. }
  900. /**
  901. * Tests that hidden fields generated for checkboxes don't get locked
  902. */
  903. public function testNoCheckboxLocking(): void
  904. {
  905. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  906. $this->Form->create();
  907. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  908. $this->Form->checkbox('check', ['value' => '1']);
  909. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  910. }
  911. /**
  912. * testFormSecurityFields method
  913. *
  914. * Test generation of secure form hash generation.
  915. */
  916. public function testFormSecurityFields(): void
  917. {
  918. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  919. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  920. $this->Form->create();
  921. $result = $this->Form->secure($fields);
  922. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  923. $hash .= ':' . 'Model.valid';
  924. $hash = urlencode($hash);
  925. $tokenDebug = urlencode(json_encode([
  926. $this->url,
  927. $fields,
  928. [],
  929. ]));
  930. $expected = [
  931. 'div' => ['style' => 'display:none;'],
  932. ['input' => [
  933. 'type' => 'hidden',
  934. 'name' => '_Token[fields]',
  935. 'value' => $hash,
  936. 'autocomplete' => 'off',
  937. ]],
  938. ['input' => [
  939. 'type' => 'hidden',
  940. 'name' => '_Token[unlocked]',
  941. 'value' => '',
  942. 'autocomplete' => 'off',
  943. ]],
  944. ['input' => [
  945. 'type' => 'hidden',
  946. 'name' => '_Token[debug]',
  947. 'value' => $tokenDebug,
  948. 'autocomplete' => 'off',
  949. ]],
  950. '/div',
  951. ];
  952. $this->assertHtml($expected, $result);
  953. }
  954. /**
  955. * testFormSecurityFields method
  956. *
  957. * Test debug token is not generated if debug is false
  958. */
  959. public function testFormSecurityFieldsNoDebugMode(): void
  960. {
  961. Configure::write('debug', false);
  962. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  963. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  964. $this->Form->create();
  965. $result = $this->Form->secure($fields);
  966. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  967. $hash .= ':' . 'Model.valid';
  968. $hash = urlencode($hash);
  969. $expected = [
  970. 'div' => ['style' => 'display:none;'],
  971. ['input' => [
  972. 'type' => 'hidden',
  973. 'name' => '_Token[fields]',
  974. 'autocomplete' => 'off',
  975. 'value' => $hash,
  976. ]],
  977. ['input' => [
  978. 'type' => 'hidden',
  979. 'name' => '_Token[unlocked]',
  980. 'autocomplete' => 'off',
  981. 'value' => '',
  982. ]],
  983. '/div',
  984. ];
  985. $this->assertHtml($expected, $result);
  986. }
  987. /**
  988. * Tests correct generation of number fields for smallint
  989. */
  990. public function testTextFieldGenerationForSmallint(): void
  991. {
  992. $this->article['schema'] = [
  993. 'foo' => [
  994. 'type' => 'smallinteger',
  995. 'null' => false,
  996. 'default' => null,
  997. 'length' => 10,
  998. ],
  999. ];
  1000. $this->Form->create($this->article);
  1001. $result = $this->Form->control('foo');
  1002. $this->assertStringContainsString('class="input number"', $result);
  1003. $this->assertStringContainsString('type="number"', $result);
  1004. }
  1005. /**
  1006. * Tests correct generation of number fields for tinyint
  1007. */
  1008. public function testTextFieldGenerationForTinyint(): void
  1009. {
  1010. $this->article['schema'] = [
  1011. 'foo' => [
  1012. 'type' => 'tinyinteger',
  1013. 'null' => false,
  1014. 'default' => null,
  1015. 'length' => 10,
  1016. ],
  1017. ];
  1018. $this->Form->create($this->article);
  1019. $result = $this->Form->control('foo');
  1020. $this->assertStringContainsString('class="input number"', $result);
  1021. $this->assertStringContainsString('type="number"', $result);
  1022. }
  1023. /**
  1024. * Tests correct generation of number fields for double and float fields
  1025. */
  1026. public function testTextFieldGenerationForFloats(): void
  1027. {
  1028. $this->article['schema'] = [
  1029. 'foo' => [
  1030. 'type' => 'float',
  1031. 'null' => false,
  1032. 'default' => null,
  1033. 'length' => 10,
  1034. ],
  1035. ];
  1036. $this->Form->create($this->article);
  1037. $result = $this->Form->control('foo');
  1038. $expected = [
  1039. 'div' => ['class' => 'input number'],
  1040. 'label' => ['for' => 'foo'],
  1041. 'Foo',
  1042. '/label',
  1043. ['input' => [
  1044. 'type' => 'number',
  1045. 'name' => 'foo',
  1046. 'id' => 'foo',
  1047. 'step' => 'any',
  1048. ]],
  1049. '/div',
  1050. ];
  1051. $this->assertHtml($expected, $result);
  1052. $result = $this->Form->control('foo', ['step' => 0.5]);
  1053. $expected = [
  1054. 'div' => ['class' => 'input number'],
  1055. 'label' => ['for' => 'foo'],
  1056. 'Foo',
  1057. '/label',
  1058. ['input' => [
  1059. 'type' => 'number',
  1060. 'name' => 'foo',
  1061. 'id' => 'foo',
  1062. 'step' => '0.5',
  1063. ]],
  1064. '/div',
  1065. ];
  1066. $this->assertHtml($expected, $result);
  1067. }
  1068. /**
  1069. * Tests correct generation of number fields for integer fields
  1070. */
  1071. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1072. {
  1073. $this->getTableLocator()->get('Contacts', [
  1074. 'className' => ContactsTable::class,
  1075. ]);
  1076. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1077. $result = $this->Form->control('age');
  1078. $expected = [
  1079. 'div' => ['class' => 'input number'],
  1080. 'label' => ['for' => 'age'],
  1081. 'Age',
  1082. '/label',
  1083. ['input' => [
  1084. 'type' => 'number', 'name' => 'age',
  1085. 'id' => 'age',
  1086. ]],
  1087. '/div',
  1088. ];
  1089. $this->assertHtml($expected, $result);
  1090. }
  1091. /**
  1092. * Tests correct generation of file upload fields for binary fields
  1093. */
  1094. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1095. {
  1096. $table = $this->getTableLocator()->get('Contacts', [
  1097. 'className' => ContactsTable::class,
  1098. ]);
  1099. $table->setSchema(['foo' => [
  1100. 'type' => 'binary',
  1101. 'null' => false,
  1102. 'default' => null,
  1103. 'length' => 1024,
  1104. ]]);
  1105. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1106. $result = $this->Form->control('foo');
  1107. $expected = [
  1108. 'div' => ['class' => 'input file'],
  1109. 'label' => ['for' => 'foo'],
  1110. 'Foo',
  1111. '/label',
  1112. ['input' => [
  1113. 'type' => 'file', 'name' => 'foo',
  1114. 'id' => 'foo',
  1115. ]],
  1116. '/div',
  1117. ];
  1118. $this->assertHtml($expected, $result);
  1119. }
  1120. /**
  1121. * testFormSecurityMultipleFields method
  1122. *
  1123. * Test secure() with multiple row form. Ensure hash is correct.
  1124. */
  1125. public function testFormSecurityMultipleFields(): void
  1126. {
  1127. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1128. $this->Form->create();
  1129. $fields = [
  1130. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1131. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1132. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1133. ];
  1134. $result = $this->Form->secure($fields);
  1135. $sortedFields = [
  1136. 'Model.0.password',
  1137. 'Model.0.username',
  1138. 'Model.1.password',
  1139. 'Model.1.username',
  1140. 'Model.0.hidden' => 'value',
  1141. 'Model.0.valid' => '0',
  1142. 'Model.1.hidden' => 'value',
  1143. 'Model.1.valid' => '0',
  1144. ];
  1145. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1146. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1147. $hash = urlencode($hash);
  1148. $tokenDebug = urlencode(json_encode([
  1149. $this->url,
  1150. $fields,
  1151. [],
  1152. ]));
  1153. $expected = [
  1154. 'div' => ['style' => 'display:none;'],
  1155. ['input' => [
  1156. 'type' => 'hidden',
  1157. 'name' => '_Token[fields]',
  1158. 'value' => $hash,
  1159. 'autocomplete' => 'off',
  1160. ]],
  1161. ['input' => [
  1162. 'type' => 'hidden',
  1163. 'name' => '_Token[unlocked]',
  1164. 'autocomplete' => 'off',
  1165. 'value' => '',
  1166. ]],
  1167. ['input' => [
  1168. 'type' => 'hidden',
  1169. 'name' => '_Token[debug]',
  1170. 'value' => $tokenDebug,
  1171. 'autocomplete' => 'off',
  1172. ]],
  1173. '/div',
  1174. ];
  1175. $this->assertHtml($expected, $result);
  1176. }
  1177. /**
  1178. * testFormSecurityMultipleSubmitButtons
  1179. *
  1180. * test form submit generation and ensure that _Token is only created on end()
  1181. */
  1182. public function testFormSecurityMultipleSubmitButtons(): void
  1183. {
  1184. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1185. $this->Form->create($this->article);
  1186. $this->Form->text('Address.title');
  1187. $this->Form->text('Address.first_name');
  1188. $result = $this->Form->submit('Save', ['name' => 'save']);
  1189. $expected = [
  1190. 'div' => ['class' => 'submit'],
  1191. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1192. '/div',
  1193. ];
  1194. $this->assertHtml($expected, $result);
  1195. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1196. $expected = [
  1197. 'div' => ['class' => 'submit'],
  1198. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1199. '/div',
  1200. ];
  1201. $this->assertHtml($expected, $result);
  1202. $result = $this->Form->end();
  1203. $tokenDebug = urlencode(json_encode([
  1204. '/articles/add',
  1205. [
  1206. 'Address.title',
  1207. 'Address.first_name',
  1208. ],
  1209. ['save', 'cancel'],
  1210. ]));
  1211. $expected = [
  1212. 'div' => ['style' => 'display:none;'],
  1213. ['input' => [
  1214. 'type' => 'hidden',
  1215. 'name' => '_Token[fields]',
  1216. 'autocomplete',
  1217. 'value',
  1218. ]],
  1219. ['input' => [
  1220. 'type' => 'hidden',
  1221. 'name' => '_Token[unlocked]',
  1222. 'value' => 'cancel%7Csave',
  1223. 'autocomplete' => 'off',
  1224. ]],
  1225. ['input' => [
  1226. 'type' => 'hidden',
  1227. 'name' => '_Token[debug]',
  1228. 'value' => $tokenDebug,
  1229. 'autocomplete' => 'off',
  1230. ]],
  1231. '/div',
  1232. ];
  1233. $this->assertHtml($expected, $result);
  1234. }
  1235. /**
  1236. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1237. */
  1238. public function testSecurityButtonNestedNamed(): void
  1239. {
  1240. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1241. $this->Form->create();
  1242. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1243. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1244. $this->assertEquals(['Address.button'], $result);
  1245. }
  1246. /**
  1247. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1248. */
  1249. public function testSecuritySubmitNestedNamed(): void
  1250. {
  1251. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1252. $this->Form->create($this->article);
  1253. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1254. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1255. $this->assertEquals(['Address.button'], $result);
  1256. }
  1257. /**
  1258. * Test that the correct fields are unlocked for image submits with no names.
  1259. */
  1260. public function testSecuritySubmitImageNoName(): void
  1261. {
  1262. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1263. $this->Form->create();
  1264. $result = $this->Form->submit('save.png');
  1265. $expected = [
  1266. 'div' => ['class' => 'submit'],
  1267. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1268. '/div',
  1269. ];
  1270. $this->assertHtml($expected, $result);
  1271. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1272. $this->assertEquals(['x', 'y'], $result);
  1273. }
  1274. /**
  1275. * Test that the correct fields are unlocked for image submits with names.
  1276. */
  1277. public function testSecuritySubmitImageName(): void
  1278. {
  1279. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1280. $this->Form->create();
  1281. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1282. $expected = [
  1283. 'div' => ['class' => 'submit'],
  1284. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1285. '/div',
  1286. ];
  1287. $this->assertHtml($expected, $result);
  1288. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1289. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1290. }
  1291. /**
  1292. * testFormSecurityMultipleControlFields method
  1293. *
  1294. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1295. */
  1296. public function testFormSecurityMultipleControlFields(): void
  1297. {
  1298. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1299. $this->Form->create();
  1300. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1301. $this->Form->control('Addresses.0.title');
  1302. $this->Form->control('Addresses.0.first_name');
  1303. $this->Form->control('Addresses.0.last_name');
  1304. $this->Form->control('Addresses.0.address');
  1305. $this->Form->control('Addresses.0.city');
  1306. $this->Form->control('Addresses.0.phone');
  1307. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1308. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1309. $this->Form->control('Addresses.1.title');
  1310. $this->Form->control('Addresses.1.first_name');
  1311. $this->Form->control('Addresses.1.last_name');
  1312. $this->Form->control('Addresses.1.address');
  1313. $this->Form->control('Addresses.1.city');
  1314. $this->Form->control('Addresses.1.phone');
  1315. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1316. $result = $this->Form->secure();
  1317. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1318. $tokenDebug = urlencode(json_encode([
  1319. '/articles/add',
  1320. [
  1321. 'Addresses.0.id' => '123456',
  1322. 'Addresses.0.title',
  1323. 'Addresses.0.first_name',
  1324. 'Addresses.0.last_name',
  1325. 'Addresses.0.address',
  1326. 'Addresses.0.city',
  1327. 'Addresses.0.phone',
  1328. 'Addresses.0.primary',
  1329. 'Addresses.1.id' => '654321',
  1330. 'Addresses.1.title',
  1331. 'Addresses.1.first_name',
  1332. 'Addresses.1.last_name',
  1333. 'Addresses.1.address',
  1334. 'Addresses.1.city',
  1335. 'Addresses.1.phone',
  1336. 'Addresses.1.primary',
  1337. ],
  1338. [],
  1339. ]));
  1340. $expected = [
  1341. 'div' => ['style' => 'display:none;'],
  1342. ['input' => [
  1343. 'type' => 'hidden',
  1344. 'name' => '_Token[fields]',
  1345. 'value' => $hash,
  1346. 'autocomplete' => 'off',
  1347. ]],
  1348. ['input' => [
  1349. 'type' => 'hidden',
  1350. 'name' => '_Token[unlocked]',
  1351. 'autocomplete' => 'off',
  1352. 'value' => '',
  1353. ]],
  1354. ['input' => [
  1355. 'type' => 'hidden',
  1356. 'name' => '_Token[debug]',
  1357. 'value' => $tokenDebug,
  1358. 'autocomplete' => 'off',
  1359. ]],
  1360. '/div',
  1361. ];
  1362. $this->assertHtml($expected, $result);
  1363. }
  1364. /**
  1365. * testFormSecurityArrayFields method
  1366. *
  1367. * Test form security with Model.field.0 style inputs.
  1368. */
  1369. public function testFormSecurityArrayFields(): void
  1370. {
  1371. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1372. $this->Form->create();
  1373. $this->Form->text('Address.primary.1');
  1374. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1375. $this->assertSame('Address.primary', $result[0]);
  1376. $this->Form->text('Address.secondary.1.0');
  1377. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1378. $this->assertSame('Address.secondary', $result[1]);
  1379. }
  1380. /**
  1381. * testFormSecurityMultipleControlDisabledFields method
  1382. *
  1383. * Test secure form generation with multiple records and disabled fields.
  1384. */
  1385. public function testFormSecurityMultipleControlDisabledFields(): void
  1386. {
  1387. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1388. 'unlockedFields' => ['first_name', 'address'],
  1389. ]));
  1390. $this->Form->create();
  1391. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1392. $this->Form->text('Addresses.0.title');
  1393. $this->Form->text('Addresses.0.first_name');
  1394. $this->Form->text('Addresses.0.last_name');
  1395. $this->Form->text('Addresses.0.address');
  1396. $this->Form->text('Addresses.0.city');
  1397. $this->Form->text('Addresses.0.phone');
  1398. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1399. $this->Form->text('Addresses.1.title');
  1400. $this->Form->text('Addresses.1.first_name');
  1401. $this->Form->text('Addresses.1.last_name');
  1402. $this->Form->text('Addresses.1.address');
  1403. $this->Form->text('Addresses.1.city');
  1404. $this->Form->text('Addresses.1.phone');
  1405. $result = $this->Form->secure();
  1406. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1407. $tokenDebug = urlencode(json_encode([
  1408. '/articles/add',
  1409. [
  1410. 'Addresses.0.id' => '123456',
  1411. 'Addresses.0.title',
  1412. 'Addresses.0.last_name',
  1413. 'Addresses.0.city',
  1414. 'Addresses.0.phone',
  1415. 'Addresses.1.id' => '654321',
  1416. 'Addresses.1.title',
  1417. 'Addresses.1.last_name',
  1418. 'Addresses.1.city',
  1419. 'Addresses.1.phone',
  1420. ],
  1421. [
  1422. 'first_name',
  1423. 'address',
  1424. ],
  1425. ]));
  1426. $expected = [
  1427. 'div' => ['style' => 'display:none;'],
  1428. ['input' => [
  1429. 'type' => 'hidden',
  1430. 'name' => '_Token[fields]',
  1431. 'autocomplete' => 'off',
  1432. 'value' => $hash,
  1433. ]],
  1434. ['input' => [
  1435. 'type' => 'hidden',
  1436. 'name' => '_Token[unlocked]',
  1437. 'autocomplete' => 'off',
  1438. 'value' => 'address%7Cfirst_name',
  1439. ]],
  1440. ['input' => [
  1441. 'type' => 'hidden',
  1442. 'name' => '_Token[debug]',
  1443. 'autocomplete' => 'off',
  1444. 'value' => $tokenDebug,
  1445. ]],
  1446. '/div',
  1447. ];
  1448. $this->assertHtml($expected, $result);
  1449. }
  1450. /**
  1451. * testFormSecurityControlDisabledFields method
  1452. *
  1453. * Test single record form with disabled fields.
  1454. */
  1455. public function testFormSecurityControlUnlockedFields(): void
  1456. {
  1457. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1458. 'unlockedFields' => ['first_name', 'address'],
  1459. ]));
  1460. $this->Form->create();
  1461. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1462. $this->assertEquals(
  1463. $this->View->getRequest()->getAttribute('formTokenData'),
  1464. ['unlockedFields' => $result]
  1465. );
  1466. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1467. $this->Form->text('Addresses.title');
  1468. $this->Form->text('Addresses.first_name');
  1469. $this->Form->text('Addresses.last_name');
  1470. $this->Form->text('Addresses.address');
  1471. $this->Form->text('Addresses.city');
  1472. $this->Form->text('Addresses.phone');
  1473. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1474. $expected = [
  1475. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1476. 'Addresses.city', 'Addresses.phone',
  1477. ];
  1478. $this->assertEquals($expected, $result);
  1479. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1480. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1481. $tokenDebug = urlencode(json_encode([
  1482. '/articles/add',
  1483. [
  1484. 'Addresses.id' => '123456',
  1485. 'Addresses.title',
  1486. 'Addresses.last_name',
  1487. 'Addresses.city',
  1488. 'Addresses.phone',
  1489. ],
  1490. [
  1491. 'first_name',
  1492. 'address',
  1493. ],
  1494. ]));
  1495. $expected = [
  1496. 'div' => ['style' => 'display:none;'],
  1497. ['input' => [
  1498. 'type' => 'hidden',
  1499. 'name' => '_Token[fields]',
  1500. 'value' => $hash,
  1501. 'autocomplete' => 'off',
  1502. 'data-foo' => 'bar',
  1503. ]],
  1504. ['input' => [
  1505. 'type' => 'hidden',
  1506. 'name' => '_Token[unlocked]',
  1507. 'value' => 'address%7Cfirst_name',
  1508. 'autocomplete' => 'off',
  1509. 'data-foo' => 'bar',
  1510. ]],
  1511. ['input' => [
  1512. 'type' => 'hidden', 'name' => '_Token[debug]',
  1513. 'value' => $tokenDebug,
  1514. 'autocomplete' => 'off',
  1515. 'data-foo' => 'bar',
  1516. ]],
  1517. '/div',
  1518. ];
  1519. $this->assertHtml($expected, $result);
  1520. }
  1521. /**
  1522. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1523. *
  1524. * Test single record form with debugSecurity param.
  1525. */
  1526. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1527. {
  1528. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1529. 'unlockedFields' => ['first_name', 'address'],
  1530. ]));
  1531. $this->Form->create();
  1532. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1533. $this->assertEquals(
  1534. $this->View->getRequest()->getAttribute('formTokenData'),
  1535. ['unlockedFields' => $result]
  1536. );
  1537. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1538. $this->Form->text('Addresses.title');
  1539. $this->Form->text('Addresses.first_name');
  1540. $this->Form->text('Addresses.last_name');
  1541. $this->Form->text('Addresses.address');
  1542. $this->Form->text('Addresses.city');
  1543. $this->Form->text('Addresses.phone');
  1544. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1545. $expected = [
  1546. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1547. 'Addresses.city', 'Addresses.phone',
  1548. ];
  1549. $this->assertEquals($expected, $result);
  1550. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1551. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1552. $tokenDebug = urlencode(json_encode([
  1553. '/articles/add',
  1554. [
  1555. 'Addresses.id' => '123456',
  1556. 'Addresses.title',
  1557. 'Addresses.last_name',
  1558. 'Addresses.city',
  1559. 'Addresses.phone',
  1560. ],
  1561. [
  1562. 'first_name',
  1563. 'address',
  1564. ],
  1565. ]));
  1566. $expected = [
  1567. 'div' => ['style' => 'display:none;'],
  1568. ['input' => [
  1569. 'type' => 'hidden',
  1570. 'name' => '_Token[fields]',
  1571. 'value' => $hash,
  1572. 'autocomplete' => 'off',
  1573. 'data-foo' => 'bar',
  1574. ]],
  1575. ['input' => [
  1576. 'type' => 'hidden',
  1577. 'name' => '_Token[unlocked]',
  1578. 'value' => 'address%7Cfirst_name',
  1579. 'autocomplete' => 'off',
  1580. 'data-foo' => 'bar',
  1581. ]],
  1582. ['input' => [
  1583. 'type' => 'hidden', 'name' => '_Token[debug]',
  1584. 'value' => $tokenDebug,
  1585. 'autocomplete' => 'off',
  1586. 'data-foo' => 'bar',
  1587. ]],
  1588. '/div',
  1589. ];
  1590. $this->assertHtml($expected, $result);
  1591. }
  1592. /**
  1593. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1594. *
  1595. * Debug is false, debugSecurity is true -> no debug
  1596. */
  1597. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1598. {
  1599. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1600. 'unlockedFields' => ['first_name', 'address'],
  1601. ]));
  1602. $this->Form->create();
  1603. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1604. $this->assertEquals(
  1605. $this->View->getRequest()->getAttribute('formTokenData'),
  1606. ['unlockedFields' => $result]
  1607. );
  1608. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1609. $this->Form->text('Addresses.title');
  1610. $this->Form->text('Addresses.first_name');
  1611. $this->Form->text('Addresses.last_name');
  1612. $this->Form->text('Addresses.address');
  1613. $this->Form->text('Addresses.city');
  1614. $this->Form->text('Addresses.phone');
  1615. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1616. $expected = [
  1617. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1618. 'Addresses.city', 'Addresses.phone',
  1619. ];
  1620. $this->assertEquals($expected, $result);
  1621. Configure::write('debug', false);
  1622. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1623. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1624. $expected = [
  1625. 'div' => ['style' => 'display:none;'],
  1626. ['input' => [
  1627. 'type' => 'hidden',
  1628. 'name' => '_Token[fields]',
  1629. 'value' => $hash,
  1630. 'autocomplete' => 'off',
  1631. 'data-foo' => 'bar',
  1632. ]],
  1633. ['input' => [
  1634. 'type' => 'hidden',
  1635. 'name' => '_Token[unlocked]',
  1636. 'value' => 'address%7Cfirst_name',
  1637. 'autocomplete' => 'off',
  1638. 'data-foo' => 'bar',
  1639. ]],
  1640. '/div',
  1641. ];
  1642. $this->assertHtml($expected, $result);
  1643. }
  1644. /**
  1645. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1646. *
  1647. * Test single record form with debugSecurity param.
  1648. */
  1649. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1650. {
  1651. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1652. 'unlockedFields' => ['first_name', 'address'],
  1653. ]));
  1654. $this->Form->create();
  1655. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1656. $this->assertEquals(
  1657. $this->View->getRequest()->getAttribute('formTokenData'),
  1658. ['unlockedFields' => $result]
  1659. );
  1660. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1661. $this->Form->text('Addresses.title');
  1662. $this->Form->text('Addresses.first_name');
  1663. $this->Form->text('Addresses.last_name');
  1664. $this->Form->text('Addresses.address');
  1665. $this->Form->text('Addresses.city');
  1666. $this->Form->text('Addresses.phone');
  1667. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1668. $expected = [
  1669. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1670. 'Addresses.city', 'Addresses.phone',
  1671. ];
  1672. $this->assertEquals($expected, $result);
  1673. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1674. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1675. $expected = [
  1676. 'div' => ['style' => 'display:none;'],
  1677. ['input' => [
  1678. 'type' => 'hidden',
  1679. 'name' => '_Token[fields]',
  1680. 'value' => $hash,
  1681. 'autocomplete' => 'off',
  1682. 'data-foo' => 'bar',
  1683. ]],
  1684. ['input' => [
  1685. 'type' => 'hidden',
  1686. 'name' => '_Token[unlocked]',
  1687. 'value' => 'address%7Cfirst_name',
  1688. 'autocomplete' => 'off',
  1689. 'data-foo' => 'bar',
  1690. ]],
  1691. '/div',
  1692. ];
  1693. $this->assertHtml($expected, $result);
  1694. }
  1695. /**
  1696. * testFormSecureWithCustomNameAttribute method
  1697. *
  1698. * Test securing inputs with custom name attributes.
  1699. */
  1700. public function testFormSecureWithCustomNameAttribute(): void
  1701. {
  1702. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1703. $this->Form->create();
  1704. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1705. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1706. $this->assertSame('User.custom', $result[0]);
  1707. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1708. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1709. $this->assertSame('User.custom.another.value', $result[1]);
  1710. }
  1711. /**
  1712. * testFormSecuredControl method
  1713. *
  1714. * Test generation of entire secure form, assertions made on control() output.
  1715. */
  1716. public function testFormSecuredControl(): void
  1717. {
  1718. $this->View->setRequest($this->View->getRequest()
  1719. ->withAttribute('formTokenData', [])
  1720. ->withAttribute('csrfToken', 'testKey'));
  1721. $this->article['schema'] = [
  1722. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1723. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1724. ];
  1725. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1726. $encoding = strtolower(Configure::read('App.encoding'));
  1727. $expected = [
  1728. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1729. 'div' => ['style' => 'display:none;'],
  1730. ['input' => [
  1731. 'type' => 'hidden',
  1732. 'name' => '_csrfToken',
  1733. 'value' => 'testKey',
  1734. 'autocomplete' => 'off',
  1735. ]],
  1736. '/div',
  1737. ];
  1738. $this->assertHtml($expected, $result);
  1739. $result = $this->Form->control('ratio');
  1740. $expected = [
  1741. 'div' => ['class'],
  1742. 'label' => ['for'],
  1743. 'Ratio',
  1744. '/label',
  1745. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1746. '/div',
  1747. ];
  1748. $this->assertHtml($expected, $result);
  1749. $result = $this->Form->control('population');
  1750. $expected = [
  1751. 'div' => ['class'],
  1752. 'label' => ['for'],
  1753. 'Population',
  1754. '/label',
  1755. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1756. '/div',
  1757. ];
  1758. $this->assertHtml($expected, $result);
  1759. $result = $this->Form->control('published', ['type' => 'text']);
  1760. $expected = [
  1761. 'div' => ['class' => 'input text'],
  1762. 'label' => ['for' => 'published'],
  1763. 'Published',
  1764. '/label',
  1765. ['input' => [
  1766. 'type' => 'text',
  1767. 'name' => 'published',
  1768. 'id' => 'published',
  1769. ]],
  1770. '/div',
  1771. ];
  1772. $this->assertHtml($expected, $result);
  1773. $result = $this->Form->control('other', ['type' => 'text']);
  1774. $expected = [
  1775. 'div' => ['class' => 'input text'],
  1776. 'label' => ['for' => 'other'],
  1777. 'Other',
  1778. '/label',
  1779. ['input' => [
  1780. 'type' => 'text',
  1781. 'name' => 'other',
  1782. 'id',
  1783. ]],
  1784. '/div',
  1785. ];
  1786. $this->assertHtml($expected, $result);
  1787. $result = $this->Form->hidden('stuff');
  1788. $expected = [
  1789. 'input' => [
  1790. 'type' => 'hidden',
  1791. 'name' => 'stuff',
  1792. ],
  1793. ];
  1794. $this->assertHtml($expected, $result);
  1795. $result = $this->Form->hidden('hidden', ['value' => false]);
  1796. $expected = ['input' => [
  1797. 'type' => 'hidden',
  1798. 'name' => 'hidden',
  1799. 'value' => '0',
  1800. ]];
  1801. $this->assertHtml($expected, $result);
  1802. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1803. $expected = [
  1804. 'div' => ['class' => 'input checkbox'],
  1805. ['input' => [
  1806. 'type' => 'hidden',
  1807. 'name' => 'something',
  1808. 'value' => '0',
  1809. ]],
  1810. 'label' => ['for' => 'something'],
  1811. ['input' => [
  1812. 'type' => 'checkbox',
  1813. 'name' => 'something',
  1814. 'value' => '1',
  1815. 'id' => 'something',
  1816. ]],
  1817. 'Something',
  1818. '/label',
  1819. '/div',
  1820. ];
  1821. $this->assertHtml($expected, $result);
  1822. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1823. $expectedFields = [
  1824. 'ratio',
  1825. 'population',
  1826. 'published',
  1827. 'other',
  1828. 'stuff' => '',
  1829. 'hidden' => '0',
  1830. 'something',
  1831. ];
  1832. $this->assertEquals($expectedFields, $result);
  1833. $result = $this->Form->secure();
  1834. $tokenDebug = urlencode(json_encode([
  1835. '/articles/add',
  1836. $expectedFields,
  1837. [],
  1838. ]));
  1839. $expected = [
  1840. 'div' => ['style' => 'display:none;'],
  1841. ['input' => [
  1842. 'type' => 'hidden',
  1843. 'name' => '_Token[fields]',
  1844. 'value',
  1845. 'autocomplete',
  1846. ]],
  1847. ['input' => [
  1848. 'type' => 'hidden',
  1849. 'name' => '_Token[unlocked]',
  1850. 'value' => '',
  1851. 'autocomplete' => 'off',
  1852. ]],
  1853. ['input' => [
  1854. 'type' => 'hidden', 'name' => '_Token[debug]',
  1855. 'value' => $tokenDebug,
  1856. 'autocomplete' => 'off',
  1857. ]],
  1858. '/div',
  1859. ];
  1860. $this->assertHtml($expected, $result);
  1861. $data = [
  1862. 'ratio' => '',
  1863. 'population' => '',
  1864. 'published' => '',
  1865. 'other' => '',
  1866. 'stuff' => '',
  1867. 'hidden' => '0',
  1868. 'something' => '',
  1869. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1870. ];
  1871. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1872. }
  1873. /**
  1874. * testSecuredControlCustomName method
  1875. *
  1876. * Test secured inputs with custom names.
  1877. */
  1878. public function testSecuredControlCustomName(): void
  1879. {
  1880. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1881. $this->Form->create();
  1882. $this->Form->text('text_input', [
  1883. 'name' => 'Option[General.default_role]',
  1884. ]);
  1885. $expected = ['Option.General.default_role'];
  1886. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1887. $this->assertEquals($expected, $result);
  1888. $this->Form->select('select_box', [1, 2], [
  1889. 'name' => 'Option[General.select_role]',
  1890. ]);
  1891. $expected[] = 'Option.General.select_role';
  1892. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1893. $this->assertEquals($expected, $result);
  1894. $this->Form->text('other.things[]');
  1895. $expected[] = 'other.things';
  1896. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1897. $this->assertEquals($expected, $result);
  1898. }
  1899. /**
  1900. * testSecuredControlDuplicate method
  1901. *
  1902. * Test that a hidden field followed by a visible field
  1903. * undoes the hidden field locking.
  1904. */
  1905. public function testSecuredControlDuplicate(): void
  1906. {
  1907. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1908. $this->Form->create();
  1909. $this->Form->control('text_val', [
  1910. 'type' => 'hidden',
  1911. 'value' => 'some text',
  1912. ]);
  1913. $expected = ['text_val' => 'some text'];
  1914. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1915. $this->assertEquals($expected, $result);
  1916. $this->Form->control('text_val', [
  1917. 'type' => 'text',
  1918. ]);
  1919. $expected = ['text_val'];
  1920. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1921. $this->assertEquals($expected, $result);
  1922. }
  1923. /**
  1924. * testFormSecuredFileControl method
  1925. *
  1926. * Tests that the correct keys are added to the field hash index.
  1927. */
  1928. public function testFormSecuredFileControl(): void
  1929. {
  1930. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1931. $this->Form->create();
  1932. $this->Form->file('Attachment.file');
  1933. $expected = ['Attachment.file'];
  1934. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1935. $this->assertEquals($expected, $result);
  1936. }
  1937. /**
  1938. * testFormSecuredMultipleSelect method
  1939. *
  1940. * Test that multiple selects keys are added to field hash.
  1941. */
  1942. public function testFormSecuredMultipleSelect(): void
  1943. {
  1944. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1945. $this->Form->create();
  1946. $options = ['1' => 'one', '2' => 'two'];
  1947. $this->Form->select('Model.select', $options);
  1948. $expected = ['Model.select'];
  1949. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1950. $this->assertEquals($expected, $result);
  1951. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1952. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1953. $this->assertEquals($expected, $result);
  1954. }
  1955. /**
  1956. * testFormSecuredRadio method
  1957. */
  1958. public function testFormSecuredRadio(): void
  1959. {
  1960. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1961. $this->Form->create();
  1962. $options = ['1' => 'option1', '2' => 'option2'];
  1963. $this->Form->radio('Test.test', $options);
  1964. $expected = ['Test.test'];
  1965. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1966. $this->assertEquals($expected, $result);
  1967. $this->Form->radio('Test.all', $options, [
  1968. 'disabled' => ['option1', 'option2'],
  1969. ]);
  1970. $expected = ['Test.test', 'Test.all' => ''];
  1971. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1972. $this->assertEquals($expected, $result);
  1973. $this->Form->radio('Test.some', $options, [
  1974. 'disabled' => ['option1'],
  1975. ]);
  1976. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1977. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1978. $this->assertEquals($expected, $result);
  1979. }
  1980. /**
  1981. * testFormSecuredAndDisabled method
  1982. *
  1983. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1984. * hashing.
  1985. */
  1986. public function testFormSecuredAndDisabled(): void
  1987. {
  1988. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1989. $this->Form->create();
  1990. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1991. $this->Form->text('Model.text', ['disabled' => true]);
  1992. $this->Form->password('Model.text', ['disabled' => true]);
  1993. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1994. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1995. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1996. $this->Form->year('Model.year', ['disabled' => true]);
  1997. $this->Form->month('Model.month', ['disabled' => true]);
  1998. $this->Form->day('Model.day', ['disabled' => true]);
  1999. $this->Form->hour('Model.hour', ['disabled' => true]);
  2000. $this->Form->minute('Model.minute', ['disabled' => true]);
  2001. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2002. $expected = [
  2003. 'Model.radio' => '',
  2004. ];
  2005. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2006. $this->assertEquals($expected, $result);
  2007. }
  2008. /**
  2009. * testUnlockFieldAddsToList method
  2010. *
  2011. * Test disableField.
  2012. */
  2013. public function testUnlockFieldAddsToList(): void
  2014. {
  2015. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2016. 'unlockedFields' => [],
  2017. ]));
  2018. $this->Form->create();
  2019. $this->Form->unlockField('Contact.name');
  2020. $this->Form->text('Contact.name');
  2021. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2022. $this->assertEquals([], $result);
  2023. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2024. $this->assertEquals(['Contact.name'], $result);
  2025. }
  2026. /**
  2027. * testUnlockFieldRemovingFromFields method
  2028. *
  2029. * Test unlockField removing from fields array.
  2030. */
  2031. public function testUnlockFieldRemovingFromFields(): void
  2032. {
  2033. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2034. 'unlockedFields' => [],
  2035. ]));
  2036. $this->Form->create($this->article);
  2037. $this->Form->hidden('Article.id', ['value' => 1]);
  2038. $this->Form->text('Article.title');
  2039. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2040. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2041. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2042. $this->Form->unlockField('Article.title');
  2043. $this->Form->unlockField('Article.id');
  2044. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2045. $this->assertEquals([], $result);
  2046. }
  2047. /**
  2048. * testResetUnlockFields method
  2049. *
  2050. * Test reset unlockFields, when create new form.
  2051. */
  2052. public function testResetUnlockFields(): void
  2053. {
  2054. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2055. 'key' => 'testKey',
  2056. 'unlockedFields' => [],
  2057. ]));
  2058. $this->Form->create();
  2059. $this->Form->unlockField('Contact.id');
  2060. $this->Form->hidden('Contact.id', ['value' => 1]);
  2061. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2062. $this->assertEmpty($result, 'Field should be unlocked');
  2063. $this->Form->end();
  2064. $this->Form->create();
  2065. $this->Form->hidden('Contact.id', ['value' => 1]);
  2066. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2067. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2068. }
  2069. /**
  2070. * testSecuredFormUrlIgnoresHost method
  2071. *
  2072. * Test that only the path + query elements of a form's URL show up in their hash.
  2073. */
  2074. public function testSecuredFormUrlIgnoresHost(): void
  2075. {
  2076. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2077. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2078. $this->Form->create($this->article, [
  2079. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2080. ]);
  2081. $result = $this->Form->secure();
  2082. $this->assertStringContainsString($expected, $result);
  2083. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2084. $result = $this->Form->secure();
  2085. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2086. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2087. $result = $this->Form->secure();
  2088. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2089. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2090. $result = $this->Form->secure();
  2091. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2092. }
  2093. /**
  2094. * testSecuredFormUrlHasHtmlAndIdentifier method
  2095. *
  2096. * Test that URL, HTML and identifier show up in their hashes.
  2097. */
  2098. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2099. {
  2100. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2101. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2102. $this->Form->create($this->article, [
  2103. 'url' => [
  2104. 'controller' => 'articles',
  2105. 'action' => 'view',
  2106. '?' => [
  2107. 'page' => 1,
  2108. 'limit' => 10,
  2109. 'html' => '<>"',
  2110. ],
  2111. '#' => 'result',
  2112. ],
  2113. ]);
  2114. $result = $this->Form->secure();
  2115. $this->assertStringContainsString($expected, $result);
  2116. $this->Form->create($this->article, [
  2117. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2118. ]);
  2119. $result = $this->Form->secure();
  2120. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2121. $this->Form->create($this->article, [
  2122. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2123. ]);
  2124. $result = $this->Form->secure();
  2125. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2126. }
  2127. /**
  2128. * testErrorMessageDisplay method
  2129. *
  2130. * Test error message display.
  2131. */
  2132. public function testErrorMessageDisplay(): void
  2133. {
  2134. $this->article['errors'] = [
  2135. 'Article' => [
  2136. 'title' => 'error message',
  2137. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2138. ],
  2139. ];
  2140. $this->Form->create($this->article);
  2141. $result = $this->Form->control('Article.title');
  2142. $expected = [
  2143. 'div' => ['class' => 'input text error'],
  2144. 'label' => ['for' => 'article-title'],
  2145. 'Title',
  2146. '/label',
  2147. 'input' => [
  2148. 'type' => 'text',
  2149. 'name' => 'Article[title]',
  2150. 'id' => 'article-title',
  2151. 'class' => 'form-error',
  2152. 'aria-invalid' => 'true',
  2153. 'aria-describedby' => 'article-title-error',
  2154. ],
  2155. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2156. 'error message',
  2157. '/div',
  2158. '/div',
  2159. ];
  2160. $this->assertHtml($expected, $result);
  2161. $result = $this->Form->control('Article.title', [
  2162. 'templates' => [
  2163. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2164. ],
  2165. ]);
  2166. $expected = [
  2167. 'div' => ['class' => 'input text error'],
  2168. 'label' => ['for' => 'article-title'],
  2169. 'Title',
  2170. '/label',
  2171. 'input' => [
  2172. 'type' => 'text',
  2173. 'name' => 'Article[title]',
  2174. 'id' => 'article-title',
  2175. 'class' => 'form-error',
  2176. // No aria-describedby because error template is custom
  2177. 'aria-invalid' => 'true',
  2178. ],
  2179. '/div',
  2180. ];
  2181. $this->assertHtml($expected, $result);
  2182. $result = $this->Form->control('Article.content');
  2183. $expected = [
  2184. 'div' => ['class' => 'input text error'],
  2185. 'label' => ['for' => 'article-content'],
  2186. 'Content',
  2187. '/label',
  2188. 'input' => [
  2189. 'type' => 'text',
  2190. 'name' => 'Article[content]',
  2191. 'id' => 'article-content',
  2192. 'class' => 'form-error',
  2193. 'aria-invalid' => 'true',
  2194. 'aria-describedby' => 'article-content-error',
  2195. ],
  2196. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2197. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2198. '/div',
  2199. '/div',
  2200. ];
  2201. $this->assertHtml($expected, $result);
  2202. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2203. $expected = [
  2204. 'div' => ['class' => 'input text error'],
  2205. 'label' => ['for' => 'article-content'],
  2206. 'Content',
  2207. '/label',
  2208. 'input' => [
  2209. 'type' => 'text',
  2210. 'name' => 'Article[content]',
  2211. 'id' => 'article-content',
  2212. 'class' => 'form-error',
  2213. 'aria-invalid' => 'true',
  2214. 'aria-describedby' => 'article-content-error',
  2215. ],
  2216. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2217. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2218. '/div',
  2219. '/div',
  2220. ];
  2221. $this->assertHtml($expected, $result);
  2222. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2223. $expected = [
  2224. 'div' => ['class' => 'input text error'],
  2225. 'label' => ['for' => 'article-content'],
  2226. 'Content',
  2227. '/label',
  2228. 'input' => [
  2229. 'type' => 'text',
  2230. 'name' => 'Article[content]',
  2231. 'id' => 'article-content',
  2232. 'class' => 'form-error',
  2233. 'aria-invalid' => 'true',
  2234. 'aria-describedby' => 'article-content-error',
  2235. ],
  2236. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2237. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2238. '/div',
  2239. '/div',
  2240. ];
  2241. $this->assertHtml($expected, $result);
  2242. }
  2243. /**
  2244. * testEmptyErrorValidation method
  2245. *
  2246. * Test validation errors, when validation message is an empty string.
  2247. */
  2248. public function testEmptyErrorValidation(): void
  2249. {
  2250. $this->article['errors'] = [
  2251. 'Article' => ['title' => ''],
  2252. ];
  2253. $this->Form->create($this->article);
  2254. $result = $this->Form->control('Article.title');
  2255. $expected = [
  2256. 'div' => ['class' => 'input text error'],
  2257. 'label' => ['for' => 'article-title'],
  2258. 'Title',
  2259. '/label',
  2260. 'input' => [
  2261. 'type' => 'text',
  2262. 'name' => 'Article[title]',
  2263. 'id' => 'article-title',
  2264. 'class' => 'form-error',
  2265. 'aria-invalid' => 'true',
  2266. 'aria-describedby' => 'article-title-error',
  2267. ],
  2268. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2269. [],
  2270. '/div',
  2271. '/div',
  2272. ];
  2273. $this->assertHtml($expected, $result);
  2274. }
  2275. /**
  2276. * testEmptyControlErrorValidation method
  2277. *
  2278. * Test validation errors, when calling control() overriding validation message by an empty string.
  2279. */
  2280. public function testEmptyControlErrorValidation(): void
  2281. {
  2282. $this->article['errors'] = [
  2283. 'Article' => ['title' => 'error message'],
  2284. ];
  2285. $this->Form->create($this->article);
  2286. $result = $this->Form->control('Article.title', ['error' => '']);
  2287. $expected = [
  2288. 'div' => ['class' => 'input text error'],
  2289. 'label' => ['for' => 'article-title'],
  2290. 'Title',
  2291. '/label',
  2292. 'input' => [
  2293. 'aria-invalid' => 'true',
  2294. 'aria-describedby' => 'article-title-error',
  2295. 'type' => 'text',
  2296. 'name' => 'Article[title]',
  2297. 'id' => 'article-title',
  2298. 'class' => 'form-error',
  2299. ],
  2300. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2301. [],
  2302. '/div',
  2303. '/div',
  2304. ];
  2305. $this->assertHtml($expected, $result);
  2306. }
  2307. /**
  2308. * testControlErrorMessage method
  2309. *
  2310. * Test validation errors, when calling control() overriding validation messages.
  2311. */
  2312. public function testControlErrorMessage(): void
  2313. {
  2314. $this->article['errors'] = [
  2315. 'title' => ['error message'],
  2316. ];
  2317. $this->Form->create($this->article);
  2318. $result = $this->Form->control('title', [
  2319. 'error' => 'Custom error!',
  2320. ]);
  2321. $expected = [
  2322. 'div' => ['class' => 'input text required error'],
  2323. 'label' => ['for' => 'title'],
  2324. 'Title',
  2325. '/label',
  2326. 'input' => [
  2327. 'type' => 'text',
  2328. 'name' => 'title',
  2329. 'id' => 'title',
  2330. 'class' => 'form-error',
  2331. 'required' => 'required',
  2332. 'data-validity-message' => 'This field cannot be left empty',
  2333. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2334. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2335. 'aria-required' => 'true',
  2336. 'aria-invalid' => 'true',
  2337. 'aria-describedby' => 'title-error',
  2338. ],
  2339. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2340. 'Custom error!',
  2341. '/div',
  2342. '/div',
  2343. ];
  2344. $this->assertHtml($expected, $result);
  2345. $result = $this->Form->control('title', [
  2346. 'error' => ['error message' => 'Custom error!'],
  2347. ]);
  2348. $expected = [
  2349. 'div' => ['class' => 'input text required error'],
  2350. 'label' => ['for' => 'title'],
  2351. 'Title',
  2352. '/label',
  2353. 'input' => [
  2354. 'type' => 'text',
  2355. 'name' => 'title',
  2356. 'id' => 'title',
  2357. 'aria-required' => 'true',
  2358. 'aria-invalid' => 'true',
  2359. 'aria-describedby' => 'title-error',
  2360. 'class' => 'form-error',
  2361. 'required' => 'required',
  2362. 'data-validity-message' => 'This field cannot be left empty',
  2363. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2364. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2365. ],
  2366. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2367. 'Custom error!',
  2368. '/div',
  2369. '/div',
  2370. ];
  2371. $this->assertHtml($expected, $result);
  2372. }
  2373. /**
  2374. * testFormValidationAssociated method
  2375. *
  2376. * Tests displaying errors for nested entities.
  2377. */
  2378. public function testFormValidationAssociated(): void
  2379. {
  2380. $nested = new Entity(['foo' => 'bar']);
  2381. $nested->setError('foo', ['not a valid bar']);
  2382. $entity = new Entity(['nested' => $nested]);
  2383. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2384. $result = $this->Form->error('nested.foo');
  2385. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2386. }
  2387. /**
  2388. * testFormValidationAssociatedSecondLevel method
  2389. *
  2390. * Test form error display with associated model.
  2391. */
  2392. public function testFormValidationAssociatedSecondLevel(): void
  2393. {
  2394. $inner = new Entity(['bar' => 'baz']);
  2395. $nested = new Entity(['foo' => $inner]);
  2396. $entity = new Entity(['nested' => $nested]);
  2397. $inner->setError('bar', ['not a valid one']);
  2398. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2399. $result = $this->Form->error('nested.foo.bar');
  2400. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2401. }
  2402. /**
  2403. * testFormValidationMultiRecord method
  2404. *
  2405. * Test form error display with multiple records.
  2406. */
  2407. public function testFormValidationMultiRecord(): void
  2408. {
  2409. $one = new Entity();
  2410. $two = new Entity();
  2411. $this->getTableLocator()->get('Contacts', [
  2412. 'className' => ContactsTable::class,
  2413. ]);
  2414. $one->set('email', '');
  2415. $one->setError('email', ['invalid email']);
  2416. $two->set('name', '');
  2417. $two->setError('name', ['This is wrong']);
  2418. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2419. $result = $this->Form->control('0.email');
  2420. $expected = [
  2421. 'div' => ['class' => 'input email error'],
  2422. 'label' => ['for' => '0-email'],
  2423. 'Email',
  2424. '/label',
  2425. 'input' => [
  2426. 'type' => 'email',
  2427. 'name' => '0[email]',
  2428. 'id' => '0-email',
  2429. 'class' => 'form-error',
  2430. 'maxlength' => 255,
  2431. 'value' => '',
  2432. 'aria-invalid' => 'true',
  2433. 'aria-describedby' => '0-email-error',
  2434. ],
  2435. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2436. 'invalid email',
  2437. '/div',
  2438. '/div',
  2439. ];
  2440. $this->assertHtml($expected, $result);
  2441. $result = $this->Form->control('1.name');
  2442. $expected = [
  2443. 'div' => ['class' => 'input text error'],
  2444. 'label' => ['for' => '1-name'],
  2445. 'Name',
  2446. '/label',
  2447. 'input' => [
  2448. 'type' => 'text',
  2449. 'name' => '1[name]',
  2450. 'id' => '1-name',
  2451. 'class' => 'form-error',
  2452. 'maxlength' => 255,
  2453. 'value' => '',
  2454. 'aria-invalid' => 'true',
  2455. 'aria-describedby' => '1-name-error',
  2456. ],
  2457. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2458. 'This is wrong',
  2459. '/div',
  2460. '/div',
  2461. ];
  2462. $this->assertHtml($expected, $result);
  2463. }
  2464. /**
  2465. * testControl method
  2466. *
  2467. * Test various incarnations of control().
  2468. */
  2469. public function testControl(): void
  2470. {
  2471. $this->getTableLocator()->get('ValidateUsers', [
  2472. 'className' => ValidateUsersTable::class,
  2473. ]);
  2474. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2475. $result = $this->Form->control('ValidateUsers.balance');
  2476. $expected = [
  2477. 'div' => ['class'],
  2478. 'label' => ['for'],
  2479. 'Balance',
  2480. '/label',
  2481. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2482. '/div',
  2483. ];
  2484. $this->assertHtml($expected, $result);
  2485. $result = $this->Form->control('ValidateUser.cost_decimal');
  2486. $expected = [
  2487. 'div' => ['class'],
  2488. 'label' => ['for'],
  2489. 'Cost Decimal',
  2490. '/label',
  2491. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2492. '/div',
  2493. ];
  2494. $this->assertHtml($expected, $result);
  2495. $result = $this->Form->control('ValidateUser.null_decimal');
  2496. $expected = [
  2497. 'div' => ['class'],
  2498. 'label' => ['for'],
  2499. 'Null Decimal',
  2500. '/label',
  2501. 'input' => ['name', 'type' => 'number', 'id'],
  2502. '/div',
  2503. ];
  2504. $this->assertHtml($expected, $result);
  2505. }
  2506. /**
  2507. * testControlCustomization method
  2508. *
  2509. * Tests the input method and passing custom options.
  2510. */
  2511. public function testControlCustomization(): void
  2512. {
  2513. $this->getTableLocator()->get('Contacts', [
  2514. 'className' => ContactsTable::class,
  2515. ]);
  2516. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2517. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2518. $expected = [
  2519. 'div' => ['class' => 'input email'],
  2520. 'label' => ['for' => 'custom'],
  2521. 'Email',
  2522. '/label',
  2523. ['input' => [
  2524. 'type' => 'email', 'name' => 'Contact[email]',
  2525. 'id' => 'custom', 'maxlength' => 255,
  2526. ]],
  2527. '/div',
  2528. ];
  2529. $this->assertHtml($expected, $result);
  2530. $result = $this->Form->control('Contact.email', [
  2531. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2532. ]);
  2533. $expected = [
  2534. '<div',
  2535. 'label' => ['for' => 'contact-email'],
  2536. 'Email',
  2537. '/label',
  2538. ['input' => [
  2539. 'type' => 'email', 'name' => 'Contact[email]',
  2540. 'id' => 'contact-email', 'maxlength' => 255,
  2541. ]],
  2542. '/div',
  2543. ];
  2544. $this->assertHtml($expected, $result);
  2545. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2546. $expected = [
  2547. 'div' => ['class' => 'input text'],
  2548. 'label' => ['for' => 'contact-email'],
  2549. 'Email',
  2550. '/label',
  2551. ['input' => [
  2552. 'type' => 'text', 'name' => 'Contact[email]',
  2553. 'id' => 'contact-email', 'maxlength' => '255',
  2554. ]],
  2555. '/div',
  2556. ];
  2557. $this->assertHtml($expected, $result);
  2558. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2559. $expected = [
  2560. 'div' => ['class' => 'input text'],
  2561. 'label' => ['for' => 'contact-5-email'],
  2562. 'Email',
  2563. '/label',
  2564. ['input' => [
  2565. 'type' => 'text', 'name' => 'Contact[5][email]',
  2566. 'id' => 'contact-5-email', 'maxlength' => '255',
  2567. ]],
  2568. '/div',
  2569. ];
  2570. $this->assertHtml($expected, $result);
  2571. $result = $this->Form->control('Contact.password');
  2572. $expected = [
  2573. 'div' => ['class' => 'input password'],
  2574. 'label' => ['for' => 'contact-password'],
  2575. 'Password',
  2576. '/label',
  2577. ['input' => [
  2578. 'type' => 'password', 'name' => 'Contact[password]',
  2579. 'id' => 'contact-password',
  2580. ]],
  2581. '/div',
  2582. ];
  2583. $this->assertHtml($expected, $result);
  2584. $result = $this->Form->control('Contact.email', [
  2585. 'type' => 'file', 'class' => 'textbox',
  2586. ]);
  2587. $expected = [
  2588. 'div' => ['class' => 'input file'],
  2589. 'label' => ['for' => 'contact-email'],
  2590. 'Email',
  2591. '/label',
  2592. ['input' => [
  2593. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2594. 'id' => 'contact-email',
  2595. ]],
  2596. '/div',
  2597. ];
  2598. $this->assertHtml($expected, $result);
  2599. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2600. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2601. $result = $this->Form->control('phone');
  2602. $expected = [
  2603. 'div' => ['class' => 'input tel'],
  2604. 'label' => ['for' => 'phone'],
  2605. 'Phone',
  2606. '/label',
  2607. ['input' => [
  2608. 'type' => 'tel', 'name' => 'phone',
  2609. 'value' => 'Hello &amp; World &gt; weird chars',
  2610. 'id' => 'phone', 'maxlength' => 255,
  2611. ]],
  2612. '/div',
  2613. ];
  2614. $this->assertHtml($expected, $result);
  2615. $this->View->setRequest(
  2616. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2617. );
  2618. $this->Form->create();
  2619. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2620. $expected = [
  2621. 'div' => ['class' => 'input text'],
  2622. 'label' => ['for' => 'myId'],
  2623. 'Field',
  2624. '/label',
  2625. 'input' => [
  2626. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2627. 'value' => 'My value', 'id' => 'myId',
  2628. ],
  2629. '/div',
  2630. ];
  2631. $this->assertHtml($expected, $result);
  2632. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2633. $this->Form->create();
  2634. $entity->setError('field', 'Badness!');
  2635. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2636. $result = $this->Form->control('field');
  2637. $expected = [
  2638. 'div' => ['class' => 'input text error'],
  2639. 'label' => ['for' => 'field'],
  2640. 'Field',
  2641. '/label',
  2642. 'input' => [
  2643. 'type' => 'text',
  2644. 'name' => 'field',
  2645. 'id' => 'field',
  2646. 'class' => 'form-error',
  2647. 'aria-invalid' => 'true',
  2648. 'aria-describedby' => 'field-error',
  2649. ],
  2650. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2651. 'Badness!',
  2652. '/div',
  2653. '/div',
  2654. ];
  2655. $this->assertHtml($expected, $result);
  2656. $result = $this->Form->control('field', [
  2657. 'templates' => [
  2658. 'inputContainerError' => '{{content}}{{error}}',
  2659. 'error' => '<span class="error-message">{{content}}</span>',
  2660. ],
  2661. ]);
  2662. $expected = [
  2663. 'label' => ['for' => 'field'],
  2664. 'Field',
  2665. '/label',
  2666. 'input' => [
  2667. 'type' => 'text',
  2668. 'name' => 'field',
  2669. 'id' => 'field',
  2670. 'class' => 'form-error',
  2671. // No aria-describedby because error template is custom
  2672. 'aria-invalid' => 'true',
  2673. ],
  2674. ['span' => ['class' => 'error-message']],
  2675. 'Badness!',
  2676. '/span',
  2677. ];
  2678. $this->assertHtml($expected, $result);
  2679. $entity->setError('field', ['minLength'], true);
  2680. $result = $this->Form->control('field', [
  2681. 'error' => [
  2682. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2683. 'maxLength' => 'login too large',
  2684. ],
  2685. ]);
  2686. $expected = [
  2687. 'div' => ['class' => 'input text error'],
  2688. 'label' => ['for' => 'field'],
  2689. 'Field',
  2690. '/label',
  2691. 'input' => [
  2692. 'type' => 'text',
  2693. 'name' => 'field',
  2694. 'id' => 'field',
  2695. 'class' => 'form-error',
  2696. 'aria-invalid' => 'true',
  2697. 'aria-describedby' => 'field-error',
  2698. ],
  2699. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2700. 'Le login doit contenir au moins 2 caractères',
  2701. '/div',
  2702. '/div',
  2703. ];
  2704. $this->assertHtml($expected, $result);
  2705. $entity->setError('field', ['maxLength'], true);
  2706. $result = $this->Form->control('field', [
  2707. 'error' => [
  2708. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2709. 'maxLength' => 'login too large',
  2710. ],
  2711. ]);
  2712. $expected = [
  2713. 'div' => ['class' => 'input text error'],
  2714. 'label' => ['for' => 'field'],
  2715. 'Field',
  2716. '/label',
  2717. 'input' => [
  2718. 'type' => 'text',
  2719. 'name' => 'field',
  2720. 'id' => 'field',
  2721. 'class' => 'form-error',
  2722. 'aria-invalid' => 'true',
  2723. 'aria-describedby' => 'field-error',
  2724. ],
  2725. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2726. 'login too large',
  2727. '/div',
  2728. '/div',
  2729. ];
  2730. $this->assertHtml($expected, $result);
  2731. }
  2732. /**
  2733. * testControlWithTemplateFile method
  2734. *
  2735. * Test that control() accepts a template file.
  2736. */
  2737. public function testControlWithTemplateFile(): void
  2738. {
  2739. $result = $this->Form->control('field', [
  2740. 'templates' => 'htmlhelper_tags',
  2741. ]);
  2742. $expected = [
  2743. 'label' => ['for' => 'field'],
  2744. 'Field',
  2745. '/label',
  2746. 'input' => [
  2747. 'type' => 'text', 'name' => 'field',
  2748. 'id' => 'field',
  2749. ],
  2750. ];
  2751. $this->assertHtml($expected, $result);
  2752. }
  2753. /**
  2754. * testNestedControlsEndWithBrackets method
  2755. *
  2756. * Test that nested inputs end with brackets.
  2757. */
  2758. public function testNestedControlsEndWithBrackets(): void
  2759. {
  2760. $result = $this->Form->text('nested.text[]');
  2761. $expected = [
  2762. 'input' => [
  2763. 'type' => 'text', 'name' => 'nested[text][]',
  2764. ],
  2765. ];
  2766. $this->assertHtml($expected, $result);
  2767. $result = $this->Form->file('nested.file[]');
  2768. $expected = [
  2769. 'input' => [
  2770. 'type' => 'file', 'name' => 'nested[file][]',
  2771. ],
  2772. ];
  2773. $this->assertHtml($expected, $result);
  2774. }
  2775. /**
  2776. * testCreateIdPrefix method
  2777. *
  2778. * Test id prefix.
  2779. */
  2780. public function testCreateIdPrefix(): void
  2781. {
  2782. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2783. $result = $this->Form->control('field');
  2784. $expected = [
  2785. 'div' => ['class' => 'input text'],
  2786. 'label' => ['for' => 'prefix-field'],
  2787. 'Field',
  2788. '/label',
  2789. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2790. '/div',
  2791. ];
  2792. $this->assertHtml($expected, $result);
  2793. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2794. $expected = [
  2795. 'div' => ['class' => 'input text'],
  2796. 'label' => ['for' => 'custom-id'],
  2797. 'Field',
  2798. '/label',
  2799. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2800. '/div',
  2801. ];
  2802. $this->assertHtml($expected, $result);
  2803. $result = $this->Form->radio('Model.field', ['option A']);
  2804. $expected = [
  2805. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2806. 'label' => ['for' => 'prefix-model-field-0'],
  2807. ['input' => [
  2808. 'type' => 'radio',
  2809. 'name' => 'Model[field]',
  2810. 'value' => '0',
  2811. 'id' => 'prefix-model-field-0',
  2812. ]],
  2813. 'option A',
  2814. '/label',
  2815. ];
  2816. $this->assertHtml($expected, $result);
  2817. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2818. $expected = [
  2819. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2820. 'label' => ['for' => 'prefix-model-field-0'],
  2821. ['input' => [
  2822. 'type' => 'radio',
  2823. 'name' => 'Model[field]',
  2824. 'value' => '0',
  2825. 'id' => 'prefix-model-field-0',
  2826. ]],
  2827. 'option A',
  2828. '/label',
  2829. ];
  2830. $this->assertHtml($expected, $result);
  2831. $result = $this->Form->select(
  2832. 'Model.multi_field',
  2833. ['first'],
  2834. ['multiple' => 'checkbox']
  2835. );
  2836. $expected = [
  2837. 'input' => [
  2838. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2839. ],
  2840. ['div' => ['class' => 'checkbox']],
  2841. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2842. ['input' => [
  2843. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2844. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2845. ]],
  2846. 'first',
  2847. '/label',
  2848. '/div',
  2849. ];
  2850. $this->assertHtml($expected, $result);
  2851. $this->Form->end();
  2852. $result = $this->Form->control('field');
  2853. $expected = [
  2854. 'div' => ['class' => 'input text'],
  2855. 'label' => ['for' => 'field'],
  2856. 'Field',
  2857. '/label',
  2858. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2859. '/div',
  2860. ];
  2861. $this->assertHtml($expected, $result);
  2862. }
  2863. /**
  2864. * testControlZero method
  2865. *
  2866. * Test that inputs with 0 can be created.
  2867. */
  2868. public function testControlZero(): void
  2869. {
  2870. $this->getTableLocator()->get('Contacts', [
  2871. 'className' => ContactsTable::class,
  2872. ]);
  2873. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2874. $result = $this->Form->control('0');
  2875. $expected = [
  2876. 'div' => ['class' => 'input text'],
  2877. 'label' => ['for' => '0'], '/label',
  2878. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2879. '/div',
  2880. ];
  2881. $this->assertHtml($expected, $result);
  2882. }
  2883. /**
  2884. * testControlCheckbox method
  2885. *
  2886. * Test control() with checkbox creation.
  2887. */
  2888. public function testControlCheckbox(): void
  2889. {
  2890. $articles = $this->getTableLocator()->get('Articles');
  2891. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2892. $article = $articles->newEmptyEntity();
  2893. $this->Form->create($article);
  2894. $result = $this->Form->control('Articles.active');
  2895. $expected = [
  2896. 'div' => ['class' => 'input checkbox'],
  2897. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2898. 'label' => ['for' => 'articles-active'],
  2899. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2900. 'Active',
  2901. '/label',
  2902. '/div',
  2903. ];
  2904. $this->assertHtml($expected, $result);
  2905. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2906. $expected = [
  2907. 'div' => ['class' => 'input checkbox'],
  2908. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2909. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2910. '/div',
  2911. ];
  2912. $this->assertHtml($expected, $result);
  2913. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2914. $expected = [
  2915. 'div' => ['class' => 'input checkbox'],
  2916. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2917. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2918. '/div',
  2919. ];
  2920. $this->assertHtml($expected, $result);
  2921. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2922. $expected = [
  2923. 'div' => ['class' => 'input checkbox'],
  2924. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2925. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2926. '/div',
  2927. ];
  2928. $this->assertHtml($expected, $result);
  2929. $result = $this->Form->control('Articles.disabled', [
  2930. 'label' => 'Disabled',
  2931. 'type' => 'checkbox',
  2932. 'data-foo' => 'disabled',
  2933. ]);
  2934. $expected = [
  2935. 'div' => ['class' => 'input checkbox'],
  2936. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2937. 'label' => ['for' => 'articles-disabled'],
  2938. ['input' => [
  2939. 'type' => 'checkbox',
  2940. 'name' => 'Articles[disabled]',
  2941. 'value' => '1',
  2942. 'id' => 'articles-disabled',
  2943. 'data-foo' => 'disabled',
  2944. ]],
  2945. 'Disabled',
  2946. '/label',
  2947. '/div',
  2948. ];
  2949. $this->assertHtml($expected, $result);
  2950. $result = $this->Form->control('Articles.confirm', [
  2951. 'label' => 'Confirm <b>me</b>!',
  2952. 'type' => 'checkbox',
  2953. 'escape' => false,
  2954. ]);
  2955. $expected = [
  2956. 'div' => ['class' => 'input checkbox'],
  2957. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2958. 'label' => ['for' => 'articles-confirm'],
  2959. ['input' => [
  2960. 'type' => 'checkbox',
  2961. 'name' => 'Articles[confirm]',
  2962. 'value' => '1',
  2963. 'id' => 'articles-confirm',
  2964. ]],
  2965. 'Confirm <b>me</b>!',
  2966. '/label',
  2967. '/div',
  2968. ];
  2969. $this->assertHtml($expected, $result);
  2970. }
  2971. /**
  2972. * testControlHidden method
  2973. *
  2974. * Test that control() does not create wrapping div and label tag for hidden fields.
  2975. */
  2976. public function testControlHidden(): void
  2977. {
  2978. $this->getTableLocator()->get('ValidateUsers', [
  2979. 'className' => ValidateUsersTable::class,
  2980. ]);
  2981. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2982. $result = $this->Form->control('ValidateUser.id');
  2983. $expected = [
  2984. 'input' => ['name', 'type' => 'hidden', 'id'],
  2985. ];
  2986. $this->assertHtml($expected, $result);
  2987. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  2988. $expected = [
  2989. 'input' => ['name', 'type' => 'hidden', 'id'],
  2990. ];
  2991. $this->assertHtml($expected, $result);
  2992. }
  2993. /**
  2994. * testControlDatetime method
  2995. *
  2996. * Test form->control() with datetime.
  2997. */
  2998. public function testControlDatetime(): void
  2999. {
  3000. $result = $this->Form->control('prueba', [
  3001. 'type' => 'datetime',
  3002. 'value' => new DateTime('2019-09-27 02:52:43'),
  3003. ]);
  3004. $expected = [
  3005. 'div' => ['class' => 'input datetime'],
  3006. 'label' => ['for' => 'prueba'],
  3007. 'Prueba',
  3008. '/label',
  3009. 'input' => [
  3010. 'name' => 'prueba',
  3011. 'id' => 'prueba',
  3012. 'type' => 'datetime-local',
  3013. 'value' => '2019-09-27T02:52:43',
  3014. 'step' => '1',
  3015. ],
  3016. '/div',
  3017. ];
  3018. $this->assertHtml($expected, $result);
  3019. }
  3020. /**
  3021. * testControlDatetimeIdPrefix method
  3022. *
  3023. * Test form->control() with datetime with id prefix.
  3024. */
  3025. public function testControlDatetimeIdPrefix(): void
  3026. {
  3027. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3028. $result = $this->Form->control('prueba', [
  3029. 'type' => 'datetime',
  3030. ]);
  3031. $expected = [
  3032. 'div' => ['class' => 'input datetime'],
  3033. 'label' => ['for' => 'prefix-prueba'],
  3034. 'Prueba',
  3035. '/label',
  3036. 'input' => [
  3037. 'name' => 'prueba',
  3038. 'id' => 'prefix-prueba',
  3039. 'type' => 'datetime-local',
  3040. 'value' => '',
  3041. 'step' => '1',
  3042. ],
  3043. '/div',
  3044. ];
  3045. $this->assertHtml($expected, $result);
  3046. }
  3047. /**
  3048. * testControlDatetimeStep method
  3049. *
  3050. * Test form->control() with datetime with custom step size.
  3051. */
  3052. public function testControlDatetimeStep(): void
  3053. {
  3054. $result = $this->Form->control('prueba', [
  3055. 'type' => 'datetime',
  3056. 'value' => new DateTime('2019-09-27 02:52:43'),
  3057. 'step' => '0.5',
  3058. ]);
  3059. $expected = [
  3060. 'div' => ['class' => 'input datetime'],
  3061. 'label' => ['for' => 'prueba'],
  3062. 'Prueba',
  3063. '/label',
  3064. 'input' => [
  3065. 'name' => 'prueba',
  3066. 'id' => 'prueba',
  3067. 'type' => 'datetime-local',
  3068. 'value' => '2019-09-27T02:52:43.000',
  3069. 'step' => '0.5',
  3070. ],
  3071. '/div',
  3072. ];
  3073. $this->assertHtml($expected, $result);
  3074. }
  3075. /**
  3076. * testControlCheckboxWithDisabledElements method
  3077. *
  3078. * Test generating checkboxes with disabled elements.
  3079. */
  3080. public function testControlCheckboxWithDisabledElements(): void
  3081. {
  3082. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3083. $result = $this->Form->control('Contact.multiple', [
  3084. 'multiple' => 'checkbox',
  3085. 'disabled' => 'disabled',
  3086. 'options' => $options,
  3087. ]);
  3088. $expected = [
  3089. ['div' => ['class' => 'input select']],
  3090. ['label' => ['for' => 'contact-multiple']],
  3091. 'Multiple',
  3092. '/label',
  3093. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3094. ['div' => ['class' => 'checkbox']],
  3095. ['label' => ['for' => 'contact-multiple-1']],
  3096. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3097. 'One',
  3098. '/label',
  3099. '/div',
  3100. ['div' => ['class' => 'checkbox']],
  3101. ['label' => ['for' => 'contact-multiple-2']],
  3102. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3103. 'Two',
  3104. '/label',
  3105. '/div',
  3106. ['div' => ['class' => 'checkbox']],
  3107. ['label' => ['for' => 'contact-multiple-3']],
  3108. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3109. 'Three',
  3110. '/label',
  3111. '/div',
  3112. '/div',
  3113. ];
  3114. $this->assertHtml($expected, $result);
  3115. // make sure 50 does only disable 50, and not 50f5c0cf
  3116. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3117. $disabled = [50];
  3118. $expected = [
  3119. ['div' => ['class' => 'input select']],
  3120. ['label' => ['for' => 'contact-multiple']],
  3121. 'Multiple',
  3122. '/label',
  3123. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3124. ['div' => ['class' => 'checkbox']],
  3125. ['label' => ['for' => 'contact-multiple-50']],
  3126. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3127. 'Fifty',
  3128. '/label',
  3129. '/div',
  3130. ['div' => ['class' => 'checkbox']],
  3131. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3132. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3133. 'Stringy',
  3134. '/label',
  3135. '/div',
  3136. '/div',
  3137. ];
  3138. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3139. $this->assertHtml($expected, $result);
  3140. }
  3141. /**
  3142. * testControlWithLeadingInteger method
  3143. *
  3144. * Test input name with leading integer, ensure attributes are generated correctly.
  3145. */
  3146. public function testControlWithLeadingInteger(): void
  3147. {
  3148. $result = $this->Form->text('0.Node.title');
  3149. $expected = [
  3150. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3151. ];
  3152. $this->assertHtml($expected, $result);
  3153. }
  3154. /**
  3155. * testControlSelectType method
  3156. *
  3157. * Test form->control() with select type inputs.
  3158. */
  3159. public function testControlSelectType(): void
  3160. {
  3161. $result = $this->Form->control(
  3162. 'email',
  3163. [
  3164. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3165. );
  3166. $expected = [
  3167. 'div' => ['class' => 'input select'],
  3168. 'label' => ['for' => 'email'],
  3169. 'Email',
  3170. '/label',
  3171. ['select' => ['name' => 'email', 'id' => 'email']],
  3172. ['option' => ['value' => '']],
  3173. '/option',
  3174. ['option' => ['value' => 'è']],
  3175. 'Firést',
  3176. '/option',
  3177. ['option' => ['value' => 'é']],
  3178. 'Secoènd',
  3179. '/option',
  3180. '/select',
  3181. '/div',
  3182. ];
  3183. $this->assertHtml($expected, $result);
  3184. $result = $this->Form->control(
  3185. 'email',
  3186. [
  3187. 'options' => ['First', 'Second'], 'empty' => true]
  3188. );
  3189. $expected = [
  3190. 'div' => ['class' => 'input select'],
  3191. 'label' => ['for' => 'email'],
  3192. 'Email',
  3193. '/label',
  3194. ['select' => ['name' => 'email', 'id' => 'email']],
  3195. ['option' => ['value' => '']],
  3196. '/option',
  3197. ['option' => ['value' => '0']],
  3198. 'First',
  3199. '/option',
  3200. ['option' => ['value' => '1']],
  3201. 'Second',
  3202. '/option',
  3203. '/select',
  3204. '/div',
  3205. ];
  3206. $this->assertHtml($expected, $result);
  3207. $result = $this->Form->control('email', [
  3208. 'type' => 'select',
  3209. 'options' => new ArrayObject(['First', 'Second']),
  3210. 'empty' => true,
  3211. ]);
  3212. $this->assertHtml($expected, $result);
  3213. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3214. $this->View->setRequest(
  3215. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3216. );
  3217. $this->Form->create();
  3218. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3219. $expected = [
  3220. 'div' => ['class' => 'input select'],
  3221. 'label' => ['for' => 'model-user-id'],
  3222. 'User',
  3223. '/label',
  3224. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3225. ['option' => ['value' => '']],
  3226. '/option',
  3227. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3228. 'good',
  3229. '/option',
  3230. ['option' => ['value' => 'other']],
  3231. 'bad',
  3232. '/option',
  3233. '/select',
  3234. '/div',
  3235. ];
  3236. $this->assertHtml($expected, $result);
  3237. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3238. $this->View->setRequest(
  3239. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3240. );
  3241. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3242. $expected = [
  3243. 'div' => ['class' => 'input select'],
  3244. 'label' => ['for' => 'thing-user-id'],
  3245. 'User',
  3246. '/label',
  3247. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3248. ['option' => ['value' => '']],
  3249. 'Some Empty',
  3250. '/option',
  3251. ['option' => ['value' => 'value']],
  3252. 'good',
  3253. '/option',
  3254. ['option' => ['value' => 'other']],
  3255. 'bad',
  3256. '/option',
  3257. '/select',
  3258. '/div',
  3259. ];
  3260. $this->assertHtml($expected, $result);
  3261. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3262. $this->View->setRequest(
  3263. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3264. );
  3265. $this->Form->create();
  3266. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3267. $expected = [
  3268. 'div' => ['class' => 'input select'],
  3269. 'label' => ['for' => 'thing-user-id'],
  3270. 'User',
  3271. '/label',
  3272. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3273. ['option' => ['value' => '']],
  3274. 'Some Empty',
  3275. '/option',
  3276. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3277. 'good',
  3278. '/option',
  3279. ['option' => ['value' => 'other']],
  3280. 'bad',
  3281. '/option',
  3282. '/select',
  3283. '/div',
  3284. ];
  3285. $this->assertHtml($expected, $result);
  3286. $result = $this->Form->control('Publisher.id', [
  3287. 'label' => 'Publisher',
  3288. 'type' => 'select',
  3289. 'multiple' => 'checkbox',
  3290. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3291. ]);
  3292. $expected = [
  3293. ['div' => ['class' => 'input select']],
  3294. ['label' => ['for' => 'publisher-id']],
  3295. 'Publisher',
  3296. '/label',
  3297. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3298. ['div' => ['class' => 'checkbox']],
  3299. ['label' => ['for' => 'publisher-id-value-1']],
  3300. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3301. 'Label 1',
  3302. '/label',
  3303. '/div',
  3304. ['div' => ['class' => 'checkbox']],
  3305. ['label' => ['for' => 'publisher-id-value-2']],
  3306. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3307. 'Label 2',
  3308. '/label',
  3309. '/div',
  3310. '/div',
  3311. ];
  3312. $this->assertHtml($expected, $result);
  3313. }
  3314. /**
  3315. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3316. *
  3317. * Test that control() and a non standard primary key makes a hidden input by default.
  3318. */
  3319. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3320. {
  3321. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3322. $this->Form->create($this->article);
  3323. $result = $this->Form->control('title');
  3324. $expected = [
  3325. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3326. ];
  3327. $this->assertHtml($expected, $result);
  3328. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3329. $this->Form->create($this->article);
  3330. $result = $this->Form->control('title');
  3331. $expected = [
  3332. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3333. ];
  3334. $this->assertHtml($expected, $result);
  3335. $result = $this->Form->control('body');
  3336. $expected = [
  3337. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3338. ];
  3339. $this->assertHtml($expected, $result);
  3340. }
  3341. /**
  3342. * testControlOverridingMagicSelectType method
  3343. *
  3344. * Test that overriding the magic select type widget is possible.
  3345. */
  3346. public function testControlOverridingMagicSelectType(): void
  3347. {
  3348. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3349. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3350. $expected = [
  3351. 'div' => ['class' => 'input text'],
  3352. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3353. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3354. '/div',
  3355. ];
  3356. $this->assertHtml($expected, $result);
  3357. //Check that magic types still work for plural/singular vars
  3358. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3359. $result = $this->Form->control('Model.type');
  3360. $expected = [
  3361. 'div' => ['class' => 'input select'],
  3362. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3363. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3364. ['option' => ['value' => 'value']], 'good', '/option',
  3365. ['option' => ['value' => 'other']], 'bad', '/option',
  3366. '/select',
  3367. '/div',
  3368. ];
  3369. $this->assertHtml($expected, $result);
  3370. }
  3371. /**
  3372. * testControlMagicTypeDoesNotOverride method
  3373. *
  3374. * Test that inferred types do not override developer input.
  3375. */
  3376. public function testControlMagicTypeDoesNotOverride(): void
  3377. {
  3378. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3379. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3380. $expected = [
  3381. 'div' => ['class' => 'input checkbox'],
  3382. ['input' => [
  3383. 'type' => 'hidden',
  3384. 'name' => 'Model[user]',
  3385. 'value' => 0,
  3386. ]],
  3387. 'label' => ['for' => 'model-user'],
  3388. ['input' => [
  3389. 'name' => 'Model[user]',
  3390. 'type' => 'checkbox',
  3391. 'id' => 'model-user',
  3392. 'value' => 1,
  3393. ]],
  3394. 'User',
  3395. '/label',
  3396. '/div',
  3397. ];
  3398. $this->assertHtml($expected, $result);
  3399. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3400. $options = [
  3401. 1 => 'blue',
  3402. 2 => 'red',
  3403. ];
  3404. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3405. $expected = [
  3406. 'div' => ['class' => 'input select'],
  3407. 'label' => ['for' => 'tags-ids'],
  3408. 'Tags',
  3409. '/label',
  3410. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3411. ['div' => ['class' => 'checkbox']],
  3412. ['label' => ['for' => 'tags-ids-1']],
  3413. ['input' => [
  3414. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3415. 'value' => '1', 'name' => 'tags[_ids][]',
  3416. ]],
  3417. 'blue',
  3418. '/label',
  3419. '/div',
  3420. ['div' => ['class' => 'checkbox']],
  3421. ['label' => ['for' => 'tags-ids-2']],
  3422. ['input' => [
  3423. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3424. 'value' => '2', 'name' => 'tags[_ids][]',
  3425. ]],
  3426. 'red',
  3427. '/label',
  3428. '/div',
  3429. '/div',
  3430. ];
  3431. $this->assertHtml($expected, $result);
  3432. }
  3433. /**
  3434. * testControlMagicSelectForTypeNumber method
  3435. *
  3436. * Test that magic control() selects are created for type=number.
  3437. */
  3438. public function testControlMagicSelectForTypeNumber(): void
  3439. {
  3440. $this->getTableLocator()->get('ValidateUsers', [
  3441. 'className' => ValidateUsersTable::class,
  3442. ]);
  3443. $entity = new Entity(['balance' => 1]);
  3444. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3445. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3446. $result = $this->Form->control('balance');
  3447. $expected = [
  3448. 'div' => ['class' => 'input select'],
  3449. 'label' => ['for' => 'balance'],
  3450. 'Balance',
  3451. '/label',
  3452. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3453. ['option' => ['value' => '0']],
  3454. 'nothing',
  3455. '/option',
  3456. ['option' => ['value' => '1', 'selected' => 'selected']],
  3457. 'some',
  3458. '/option',
  3459. ['option' => ['value' => '100']],
  3460. 'a lot',
  3461. '/option',
  3462. '/select',
  3463. '/div',
  3464. ];
  3465. $this->assertHtml($expected, $result);
  3466. }
  3467. /**
  3468. * testInvalidControlTypeOption method
  3469. *
  3470. * Test invalid 'input' type option to control() function.
  3471. */
  3472. public function testInvalidControlTypeOption(): void
  3473. {
  3474. $this->expectException(InvalidArgumentException::class);
  3475. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3476. $this->Form->control('text', ['type' => 'input']);
  3477. }
  3478. /**
  3479. * testControlMagicSelectChangeToRadio method
  3480. *
  3481. * Test that magic control() selects can easily be converted into radio types without error.
  3482. */
  3483. public function testControlMagicSelectChangeToRadio(): void
  3484. {
  3485. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3486. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3487. $this->assertStringContainsString('input type="radio"', $result);
  3488. }
  3489. /**
  3490. * testFormControlSubmit method
  3491. *
  3492. * Test correct results for form::control() and type submit.
  3493. */
  3494. public function testFormControlSubmit(): void
  3495. {
  3496. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3497. $expected = [
  3498. 'div' => ['class' => 'submit'],
  3499. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3500. '/div',
  3501. ];
  3502. $this->assertHtml($expected, $result);
  3503. }
  3504. /**
  3505. * testFormControls method
  3506. *
  3507. * Test correct results from Form::controls().
  3508. */
  3509. public function testFormControlsLegendFieldset(): void
  3510. {
  3511. $this->Form->create($this->article);
  3512. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3513. $expected = [
  3514. '<fieldset',
  3515. '<legend',
  3516. 'The Legend',
  3517. '/legend',
  3518. '*/fieldset',
  3519. ];
  3520. $this->assertHtml($expected, $result);
  3521. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3522. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3523. $this->assertStringContainsString('<fieldset>', $result);
  3524. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3525. $this->assertStringNotContainsString('<legend>', $result);
  3526. $this->assertStringNotContainsString('<fieldset>', $result);
  3527. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3528. $this->assertStringNotContainsString('<legend>', $result);
  3529. $this->assertStringNotContainsString('<fieldset>', $result);
  3530. $this->Form->create($this->article);
  3531. $this->View->setRequest($this->View->getRequest()
  3532. ->withParam('prefix', 'admin')
  3533. ->withParam('action', 'admin_edit')
  3534. ->withParam('controller', 'articles'));
  3535. $result = $this->Form->allControls();
  3536. $expected = [
  3537. '<fieldset',
  3538. '<legend',
  3539. 'New Article',
  3540. '/legend',
  3541. '*/fieldset',
  3542. ];
  3543. $this->assertHtml($expected, $result);
  3544. $this->Form->create($this->article);
  3545. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3546. $expected = [
  3547. '<fieldset',
  3548. '<legend',
  3549. 'The Legend',
  3550. '/legend',
  3551. '*/fieldset',
  3552. ];
  3553. $this->assertHtml($expected, $result);
  3554. $this->Form->create($this->article);
  3555. $result = $this->Form->allControls([], [
  3556. 'fieldset' => [
  3557. 'class' => 'some-class some-other-class',
  3558. 'disabled' => true,
  3559. 'data-param' => 'a-param',
  3560. ],
  3561. 'legend' => 'The Legend',
  3562. ]);
  3563. $expected = [
  3564. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3565. '<legend',
  3566. 'The Legend',
  3567. '/legend',
  3568. '*/fieldset',
  3569. ];
  3570. $this->assertHtml($expected, $result);
  3571. }
  3572. /**
  3573. * testFormControls method
  3574. *
  3575. * Test the controls() method.
  3576. */
  3577. public function testFormControls(): void
  3578. {
  3579. $this->Form->create($this->article);
  3580. $result = $this->Form->allControls();
  3581. $expected = [
  3582. '<fieldset',
  3583. '<legend', 'New Article', '/legend',
  3584. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3585. ['div' => ['class' => 'input select required']],
  3586. '*/div',
  3587. ['div' => ['class' => 'input text required']],
  3588. '*/div',
  3589. ['div' => ['class' => 'input text']],
  3590. '*/div',
  3591. ['div' => ['class' => 'input text']],
  3592. '*/div',
  3593. '/fieldset',
  3594. ];
  3595. $this->assertHtml($expected, $result);
  3596. $result = $this->Form->allControls([
  3597. 'published' => ['type' => 'boolean'],
  3598. ]);
  3599. $expected = [
  3600. '<fieldset',
  3601. '<legend', 'New Article', '/legend',
  3602. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3603. ['div' => ['class' => 'input select required']],
  3604. '*/div',
  3605. ['div' => ['class' => 'input text required']],
  3606. '*/div',
  3607. ['div' => ['class' => 'input text']],
  3608. '*/div',
  3609. ['div' => ['class' => 'input boolean']],
  3610. '*/div',
  3611. '/fieldset',
  3612. ];
  3613. $this->assertHtml($expected, $result);
  3614. $this->Form->create($this->article);
  3615. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3616. $expected = [
  3617. 'fieldset' => [],
  3618. 'legend' => [],
  3619. 'Hello',
  3620. '/legend',
  3621. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3622. ['div' => ['class' => 'input select required']],
  3623. '*/div',
  3624. ['div' => ['class' => 'input text required']],
  3625. '*/div',
  3626. ['div' => ['class' => 'input text']],
  3627. '*/div',
  3628. ['div' => ['class' => 'input text']],
  3629. '*/div',
  3630. '/fieldset',
  3631. ];
  3632. $this->assertHtml($expected, $result);
  3633. $this->Form->create();
  3634. $expected = [
  3635. 'fieldset' => [],
  3636. ['div' => ['class' => 'input text']],
  3637. 'label' => ['for' => 'foo'],
  3638. 'Foo',
  3639. '/label',
  3640. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3641. '*/div',
  3642. '/fieldset',
  3643. ];
  3644. $result = $this->Form->allControls(
  3645. ['foo' => ['type' => 'text']],
  3646. ['legend' => false]
  3647. );
  3648. $this->assertHtml($expected, $result);
  3649. }
  3650. /**
  3651. * testFormControlsBlacklist method
  3652. */
  3653. public function testFormControlsBlacklist(): void
  3654. {
  3655. $this->Form->create($this->article);
  3656. $result = $this->Form->allControls([
  3657. 'id' => false,
  3658. ]);
  3659. $expected = [
  3660. '<fieldset',
  3661. '<legend', 'New Article', '/legend',
  3662. ['div' => ['class' => 'input select required']],
  3663. '*/div',
  3664. ['div' => ['class' => 'input text required']],
  3665. '*/div',
  3666. ['div' => ['class' => 'input text']],
  3667. '*/div',
  3668. ['div' => ['class' => 'input text']],
  3669. '*/div',
  3670. '/fieldset',
  3671. ];
  3672. $this->assertHtml($expected, $result);
  3673. $this->Form->create($this->article);
  3674. $result = $this->Form->allControls([
  3675. 'id' => [],
  3676. ]);
  3677. $expected = [
  3678. '<fieldset',
  3679. '<legend', 'New Article', '/legend',
  3680. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3681. ['div' => ['class' => 'input select required']],
  3682. '*/div',
  3683. ['div' => ['class' => 'input text required']],
  3684. '*/div',
  3685. ['div' => ['class' => 'input text']],
  3686. '*/div',
  3687. ['div' => ['class' => 'input text']],
  3688. '*/div',
  3689. '/fieldset',
  3690. ];
  3691. $this->assertHtml($expected, $result, true);
  3692. }
  3693. /**
  3694. * testSelectAsCheckbox method
  3695. *
  3696. * Test multi-select widget with checkbox formatting.
  3697. */
  3698. public function testSelectAsCheckbox(): void
  3699. {
  3700. $result = $this->Form->select(
  3701. 'Model.multi_field',
  3702. ['first', 'second', 'third'],
  3703. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3704. );
  3705. $expected = [
  3706. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3707. ['div' => ['class' => 'checkbox']],
  3708. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3709. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3710. 'first',
  3711. '/label',
  3712. '/div',
  3713. ['div' => ['class' => 'checkbox']],
  3714. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3715. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3716. 'second',
  3717. '/label',
  3718. '/div',
  3719. ['div' => ['class' => 'checkbox']],
  3720. ['label' => ['for' => 'model-multi-field-2']],
  3721. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3722. 'third',
  3723. '/label',
  3724. '/div',
  3725. ];
  3726. $this->assertHtml($expected, $result);
  3727. $result = $this->Form->select(
  3728. 'Model.multi_field',
  3729. ['1/2' => 'half'],
  3730. ['multiple' => 'checkbox']
  3731. );
  3732. $expected = [
  3733. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3734. ['div' => ['class' => 'checkbox']],
  3735. ['label' => ['for' => 'model-multi-field-1-2']],
  3736. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3737. 'half',
  3738. '/label',
  3739. '/div',
  3740. ];
  3741. $this->assertHtml($expected, $result);
  3742. }
  3743. /**
  3744. * testLabel method
  3745. *
  3746. * Test label generation.
  3747. */
  3748. public function testLabel(): void
  3749. {
  3750. $result = $this->Form->label('Person.name');
  3751. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3752. $this->assertHtml($expected, $result);
  3753. $result = $this->Form->label('Person.name');
  3754. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3755. $this->assertHtml($expected, $result);
  3756. $result = $this->Form->label('Person.first_name');
  3757. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3758. $this->assertHtml($expected, $result);
  3759. $result = $this->Form->label('Person.first_name', 'Your first name');
  3760. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3761. $this->assertHtml($expected, $result);
  3762. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3763. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3764. $this->assertHtml($expected, $result);
  3765. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3766. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3767. $this->assertHtml($expected, $result);
  3768. $result = $this->Form->label('Person.first_name', '');
  3769. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3770. $this->assertHtml($expected, $result);
  3771. $result = $this->Form->label('Person.2.name', '');
  3772. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3773. $this->assertHtml($expected, $result);
  3774. }
  3775. /**
  3776. * testLabelContainControl method
  3777. *
  3778. * Test that label() can accept an input with the correct template vars.
  3779. */
  3780. public function testLabelContainControl(): void
  3781. {
  3782. $this->Form->setTemplates([
  3783. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3784. ]);
  3785. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3786. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3787. ]);
  3788. $expected = [
  3789. 'label' => ['for' => 'person-accept-terms'],
  3790. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3791. 'Accept',
  3792. '/label',
  3793. ];
  3794. $this->assertHtml($expected, $result);
  3795. }
  3796. /**
  3797. * testTextbox method
  3798. *
  3799. * Test textbox element generation.
  3800. */
  3801. public function testTextbox(): void
  3802. {
  3803. $result = $this->Form->text('Model.field');
  3804. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3805. $this->assertHtml($expected, $result);
  3806. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3807. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3808. $this->assertHtml($expected, $result);
  3809. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3810. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3811. $this->assertHtml($expected, $result);
  3812. }
  3813. /**
  3814. * testTextBoxDataAndError method
  3815. *
  3816. * Test that text() hooks up with request data and error fields.
  3817. */
  3818. public function testTextBoxDataAndError(): void
  3819. {
  3820. $this->article['errors'] = [
  3821. 'Contact' => ['text' => 'wrong'],
  3822. ];
  3823. $this->View->setRequest($this->View->getRequest()
  3824. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3825. ->withData('Contact.text', 'test'));
  3826. $this->Form->create($this->article);
  3827. $result = $this->Form->text('Model.text');
  3828. $expected = [
  3829. 'input' => [
  3830. 'type' => 'text',
  3831. 'name' => 'Model[text]',
  3832. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3833. ],
  3834. ];
  3835. $this->assertHtml($expected, $result);
  3836. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3837. $expected = [
  3838. 'input' => [
  3839. 'type' => 'text',
  3840. 'name' => 'Contact[text]',
  3841. 'value' => 'test',
  3842. 'id' => 'theID',
  3843. 'class' => 'form-error',
  3844. ],
  3845. ];
  3846. $this->assertHtml($expected, $result);
  3847. }
  3848. /**
  3849. * testDefaultValue method
  3850. *
  3851. * Test default value setting.
  3852. */
  3853. public function testTextDefaultValue(): void
  3854. {
  3855. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3856. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3857. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3858. $this->assertHtml($expected, $result);
  3859. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3860. $this->Form->create();
  3861. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3862. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3863. $this->assertHtml($expected, $result);
  3864. $Articles = $this->getTableLocator()->get('Articles');
  3865. $title = $Articles->getSchema()->getColumn('title');
  3866. $Articles->getSchema()->addColumn(
  3867. 'title',
  3868. ['default' => 'default title', 'length' => 255] + $title
  3869. );
  3870. $entity = $Articles->newEmptyEntity();
  3871. $this->Form->create($entity);
  3872. // Get default value from schema
  3873. $result = $this->Form->text('title');
  3874. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3875. $this->assertHtml($expected, $result);
  3876. // Don't get value from schema
  3877. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3878. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3879. $this->assertHtml($expected, $result);
  3880. // Custom default value overrides default value from schema
  3881. $result = $this->Form->text('title', ['default' => 'override default']);
  3882. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3883. $this->assertHtml($expected, $result);
  3884. // Default value from schema is used only for new entities.
  3885. $entity->setNew(false);
  3886. $result = $this->Form->text('title');
  3887. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3888. $this->assertHtml($expected, $result);
  3889. }
  3890. /**
  3891. * testError method
  3892. *
  3893. * Test field error generation.
  3894. */
  3895. public function testError(): void
  3896. {
  3897. $this->article['errors'] = [
  3898. 'Article' => ['field' => 'email'],
  3899. ];
  3900. $this->Form->create($this->article);
  3901. $result = $this->Form->error('Article.field');
  3902. $expected = [
  3903. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3904. 'email',
  3905. '/div',
  3906. ];
  3907. $this->assertHtml($expected, $result);
  3908. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3909. $expected = [
  3910. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3911. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3912. '/div',
  3913. ];
  3914. $this->assertHtml($expected, $result);
  3915. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3916. $expected = [
  3917. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3918. '<strong', 'Badness!', '/strong',
  3919. '/div',
  3920. ];
  3921. $this->assertHtml($expected, $result);
  3922. }
  3923. /**
  3924. * testErrorRuleName method
  3925. *
  3926. * Test error translation can use rule names for translating.
  3927. */
  3928. public function testErrorRuleName(): void
  3929. {
  3930. $this->article['errors'] = [
  3931. 'Article' => [
  3932. 'field' => ['email' => 'Your email was not good'],
  3933. ],
  3934. ];
  3935. $this->Form->create($this->article);
  3936. $result = $this->Form->error('Article.field');
  3937. $expected = [
  3938. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3939. 'Your email was not good',
  3940. '/div',
  3941. ];
  3942. $this->assertHtml($expected, $result);
  3943. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3944. $expected = [
  3945. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3946. 'Email in use',
  3947. '/div',
  3948. ];
  3949. $this->assertHtml($expected, $result);
  3950. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3951. $expected = [
  3952. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3953. 'Email in use',
  3954. '/div',
  3955. ];
  3956. $this->assertHtml($expected, $result);
  3957. $result = $this->Form->error('Article.field', [
  3958. 'email' => 'Key is preferred',
  3959. 'Your email was not good' => 'Email in use',
  3960. ]);
  3961. $expected = [
  3962. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3963. 'Key is preferred',
  3964. '/div',
  3965. ];
  3966. $this->assertHtml($expected, $result);
  3967. }
  3968. /**
  3969. * testErrorMessages method
  3970. *
  3971. * Test error with nested lists.
  3972. */
  3973. public function testErrorMessages(): void
  3974. {
  3975. $this->article['errors'] = [
  3976. 'Article' => ['field' => 'email'],
  3977. ];
  3978. $this->Form->create($this->article);
  3979. $result = $this->Form->error('Article.field', [
  3980. 'email' => 'No good!',
  3981. ]);
  3982. $expected = [
  3983. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  3984. 'No good!',
  3985. '/div',
  3986. ];
  3987. $this->assertHtml($expected, $result);
  3988. }
  3989. /**
  3990. * testErrorMultipleMessages method
  3991. *
  3992. * Test error() with multiple messages.
  3993. */
  3994. public function testErrorMultipleMessages(): void
  3995. {
  3996. $this->article['errors'] = [
  3997. 'field' => ['notBlank', 'email', 'Something else'],
  3998. ];
  3999. $this->Form->create($this->article);
  4000. $result = $this->Form->error('field', [
  4001. 'notBlank' => 'Cannot be empty',
  4002. 'email' => 'No good!',
  4003. ]);
  4004. $expected = [
  4005. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4006. 'ul' => [],
  4007. '<li', 'Cannot be empty', '/li',
  4008. '<li', 'No good!', '/li',
  4009. '<li', 'Something else', '/li',
  4010. '/ul',
  4011. '/div',
  4012. ];
  4013. $this->assertHtml($expected, $result);
  4014. }
  4015. /**
  4016. * testPassword method
  4017. *
  4018. * Test password element generation.
  4019. */
  4020. public function testPassword(): void
  4021. {
  4022. $this->article['errors'] = [
  4023. 'Contact' => [
  4024. 'passwd' => 1,
  4025. ],
  4026. ];
  4027. $this->Form->create($this->article);
  4028. $result = $this->Form->password('Contact.field');
  4029. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4030. $this->assertHtml($expected, $result);
  4031. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4032. $this->Form->create($this->article);
  4033. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4034. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4035. $this->assertHtml($expected, $result);
  4036. }
  4037. /**
  4038. * testRadio method
  4039. *
  4040. * Test radio element set generation.
  4041. */
  4042. public function testRadio(): void
  4043. {
  4044. $result = $this->Form->radio('Model.field', ['option A']);
  4045. $expected = [
  4046. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4047. 'label' => ['for' => 'model-field-0'],
  4048. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4049. 'option A',
  4050. '/label',
  4051. ];
  4052. $this->assertHtml($expected, $result);
  4053. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4054. $this->assertHtml($expected, $result);
  4055. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4056. $expected = [
  4057. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4058. ['label' => ['for' => 'model-field-0']],
  4059. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4060. 'option A',
  4061. '/label',
  4062. ['label' => ['for' => 'model-field-1']],
  4063. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4064. 'option B',
  4065. '/label',
  4066. ];
  4067. $this->assertHtml($expected, $result);
  4068. $result = $this->Form->radio(
  4069. 'Employee.gender',
  4070. ['male' => 'Male', 'female' => 'Female'],
  4071. ['form' => 'my-form']
  4072. );
  4073. $expected = [
  4074. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form', 'id' => 'employee-gender'],
  4075. ['label' => ['for' => 'employee-gender-male']],
  4076. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4077. 'Male',
  4078. '/label',
  4079. ['label' => ['for' => 'employee-gender-female']],
  4080. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4081. 'Female',
  4082. '/label',
  4083. ];
  4084. $this->assertHtml($expected, $result);
  4085. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4086. $expected = [
  4087. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4088. ['label' => ['for' => 'model-custom-0']],
  4089. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4090. 'option A',
  4091. '/label',
  4092. ['label' => ['for' => 'model-custom-1']],
  4093. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4094. 'option B',
  4095. '/label',
  4096. ];
  4097. $this->assertHtml($expected, $result);
  4098. $result = $this->Form->radio(
  4099. 'Employee.gender',
  4100. [
  4101. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4102. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4103. ]
  4104. );
  4105. $expected = [
  4106. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4107. ['label' => ['for' => 'employee-gender-male']],
  4108. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4109. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4110. 'Male',
  4111. '/label',
  4112. ['label' => ['for' => 'employee-gender-female']],
  4113. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4114. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4115. 'Female',
  4116. '/label',
  4117. ];
  4118. $this->assertHtml($expected, $result);
  4119. }
  4120. /**
  4121. * Test radio with complex options and empty disabled data.
  4122. */
  4123. public function testRadioComplexDisabled(): void
  4124. {
  4125. $options = [
  4126. ['value' => 'r', 'text' => 'red'],
  4127. ['value' => 'b', 'text' => 'blue'],
  4128. ];
  4129. $attrs = ['disabled' => []];
  4130. $result = $this->Form->radio('Model.field', $options, $attrs);
  4131. $expected = [
  4132. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4133. ['label' => ['for' => 'model-field-r']],
  4134. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4135. 'red',
  4136. '/label',
  4137. ['label' => ['for' => 'model-field-b']],
  4138. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4139. 'blue',
  4140. '/label',
  4141. ];
  4142. $this->assertHtml($expected, $result);
  4143. $attrs = ['disabled' => ['r']];
  4144. $result = $this->Form->radio('Model.field', $options, $attrs);
  4145. $this->assertStringContainsString('disabled="disabled"', $result);
  4146. }
  4147. /**
  4148. * testRadioDefaultValue method
  4149. *
  4150. * Test default value setting on radio() method.
  4151. */
  4152. public function testRadioDefaultValue(): void
  4153. {
  4154. $Articles = $this->getTableLocator()->get('Articles');
  4155. $title = $Articles->getSchema()->getColumn('title');
  4156. $Articles->getSchema()->addColumn(
  4157. 'title',
  4158. ['default' => '1'] + $title
  4159. );
  4160. $this->Form->create($Articles->newEmptyEntity());
  4161. $result = $this->Form->radio('title', ['option A', 'option B']);
  4162. $expected = [
  4163. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4164. ['label' => ['for' => 'title-0']],
  4165. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4166. 'option A',
  4167. '/label',
  4168. ['label' => ['for' => 'title-1']],
  4169. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4170. 'option B',
  4171. '/label',
  4172. ];
  4173. $this->assertHtml($expected, $result);
  4174. }
  4175. /**
  4176. * Test setting a hiddenField value on radio buttons.
  4177. */
  4178. public function testRadioHiddenFieldValue(): void
  4179. {
  4180. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4181. $expected = [
  4182. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4183. 'label' => ['for' => 'title-0'],
  4184. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4185. 'option A',
  4186. '/label',
  4187. ];
  4188. $this->assertHtml($expected, $result);
  4189. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4190. $expected = [
  4191. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4192. 'label' => ['for' => 'title-0'],
  4193. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4194. 'option A',
  4195. '/label',
  4196. ];
  4197. $this->assertHtml($expected, $result);
  4198. }
  4199. /**
  4200. * testControlRadio method
  4201. *
  4202. * Test that input works with radio types.
  4203. */
  4204. public function testControlRadio(): void
  4205. {
  4206. $result = $this->Form->control('test', [
  4207. 'type' => 'radio',
  4208. 'options' => ['A', 'B'],
  4209. ]);
  4210. $expected = [
  4211. ['div' => ['class' => 'input radio']],
  4212. '<label',
  4213. 'Test',
  4214. '/label',
  4215. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4216. ['label' => ['for' => 'test-0']],
  4217. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4218. 'A',
  4219. '/label',
  4220. ['label' => ['for' => 'test-1']],
  4221. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4222. 'B',
  4223. '/label',
  4224. '/div',
  4225. ];
  4226. $this->assertHtml($expected, $result);
  4227. $result = $this->Form->control('test', [
  4228. 'type' => 'radio',
  4229. 'options' => ['A', 'B'],
  4230. 'value' => '0',
  4231. ]);
  4232. $expected = [
  4233. ['div' => ['class' => 'input radio']],
  4234. '<label',
  4235. 'Test',
  4236. '/label',
  4237. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4238. ['label' => ['for' => 'test-0']],
  4239. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4240. 'A',
  4241. '/label',
  4242. ['label' => ['for' => 'test-1']],
  4243. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4244. 'B',
  4245. '/label',
  4246. '/div',
  4247. ];
  4248. $this->assertHtml($expected, $result);
  4249. $result = $this->Form->control('test', [
  4250. 'type' => 'radio',
  4251. 'options' => ['A', 'B'],
  4252. 'label' => false,
  4253. ]);
  4254. $expected = [
  4255. ['div' => ['class' => 'input radio']],
  4256. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4257. ['label' => ['for' => 'test-0']],
  4258. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4259. 'A',
  4260. '/label',
  4261. ['label' => ['for' => 'test-1']],
  4262. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4263. 'B',
  4264. '/label',
  4265. '/div',
  4266. ];
  4267. $this->assertHtml($expected, $result);
  4268. $result = $this->Form->control('accept', [
  4269. 'type' => 'radio',
  4270. 'options' => [
  4271. 1 => 'positive',
  4272. -1 => 'negative',
  4273. ],
  4274. 'label' => false,
  4275. ]);
  4276. $expected = [
  4277. ['div' => ['class' => 'input radio']],
  4278. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4279. ['label' => ['for' => 'accept-1']],
  4280. ['input' => [
  4281. 'type' => 'radio',
  4282. 'name' => 'accept',
  4283. 'value' => '1',
  4284. 'id' => 'accept-1',
  4285. ]],
  4286. 'positive',
  4287. '/label',
  4288. ['label' => ['for' => 'accept--1']],
  4289. ['input' => [
  4290. 'type' => 'radio',
  4291. 'name' => 'accept',
  4292. 'value' => '-1',
  4293. 'id' => 'accept--1',
  4294. ]],
  4295. 'negative',
  4296. '/label',
  4297. '/div',
  4298. ];
  4299. $this->assertHtml($expected, $result);
  4300. }
  4301. /**
  4302. * testRadioNoLabel method
  4303. *
  4304. * Test that radio() works with label = false.
  4305. */
  4306. public function testRadioNoLabel(): void
  4307. {
  4308. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4309. $expected = [
  4310. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4311. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4312. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4313. ];
  4314. $this->assertHtml($expected, $result);
  4315. }
  4316. /**
  4317. * testRadioControlInsideLabel method
  4318. *
  4319. * Test generating radio input inside label ala twitter bootstrap.
  4320. */
  4321. public function testRadioControlInsideLabel(): void
  4322. {
  4323. $this->Form->setTemplates([
  4324. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4325. 'radioWrapper' => '{{label}}',
  4326. ]);
  4327. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4328. // phpcs:disable
  4329. $expected = [
  4330. ['input' => [
  4331. 'type' => 'hidden',
  4332. 'name' => 'Model[field]',
  4333. 'value' => '',
  4334. 'id' => 'model-field'
  4335. ]],
  4336. ['label' => ['for' => 'model-field-0']],
  4337. ['input' => [
  4338. 'type' => 'radio',
  4339. 'name' => 'Model[field]',
  4340. 'value' => '0',
  4341. 'id' => 'model-field-0'
  4342. ]],
  4343. 'option A',
  4344. '/label',
  4345. ['label' => ['for' => 'model-field-1']],
  4346. ['input' => [
  4347. 'type' => 'radio',
  4348. 'name' => 'Model[field]',
  4349. 'value' => '1',
  4350. 'id' => 'model-field-1'
  4351. ]],
  4352. 'option B',
  4353. '/label',
  4354. ];
  4355. // phpcs:enable
  4356. $this->assertHtml($expected, $result);
  4357. }
  4358. /**
  4359. * testRadioHiddenControlDisabling method
  4360. *
  4361. * Test disabling the hidden input for radio buttons.
  4362. */
  4363. public function testRadioHiddenControlDisabling(): void
  4364. {
  4365. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4366. $expected = [
  4367. 'label' => ['for' => 'model-1-field-0'],
  4368. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4369. 'option A',
  4370. '/label',
  4371. ];
  4372. $this->assertHtml($expected, $result);
  4373. }
  4374. /**
  4375. * testRadioOutOfRange method
  4376. *
  4377. * Test radio element set generation.
  4378. */
  4379. public function testRadioOutOfRange(): void
  4380. {
  4381. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4382. $expected = [
  4383. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4384. 'label' => ['for' => 'model-field-v'],
  4385. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4386. 'value',
  4387. '/label',
  4388. ];
  4389. $this->assertHtml($expected, $result);
  4390. }
  4391. /**
  4392. * testSelect method
  4393. *
  4394. * Test select element generation.
  4395. */
  4396. public function testSelect(): void
  4397. {
  4398. $result = $this->Form->select('Model.field', []);
  4399. $expected = [
  4400. 'select' => ['name' => 'Model[field]'],
  4401. '/select',
  4402. ];
  4403. $this->assertHtml($expected, $result);
  4404. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4405. $this->Form->create();
  4406. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4407. $expected = [
  4408. 'select' => ['name' => 'Model[field]'],
  4409. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4410. 'good',
  4411. '/option',
  4412. ['option' => ['value' => 'other']],
  4413. 'bad',
  4414. '/option',
  4415. '/select',
  4416. ];
  4417. $this->assertHtml($expected, $result);
  4418. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4419. $this->assertHtml($expected, $result);
  4420. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4421. $this->Form->create();
  4422. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4423. $expected = [
  4424. 'select' => ['name' => 'Model[field]'],
  4425. ['option' => ['value' => 'value']],
  4426. 'good',
  4427. '/option',
  4428. ['option' => ['value' => 'other']],
  4429. 'bad',
  4430. '/option',
  4431. '/select',
  4432. ];
  4433. $this->assertHtml($expected, $result);
  4434. $options = [
  4435. ['value' => 'first', 'text' => 'First'],
  4436. ['value' => 'first', 'text' => 'Another First'],
  4437. ];
  4438. $result = $this->Form->select(
  4439. 'Model.field',
  4440. $options,
  4441. ['escape' => false, 'empty' => false]
  4442. );
  4443. $expected = [
  4444. 'select' => ['name' => 'Model[field]'],
  4445. ['option' => ['value' => 'first']],
  4446. 'First',
  4447. '/option',
  4448. ['option' => ['value' => 'first']],
  4449. 'Another First',
  4450. '/option',
  4451. '/select',
  4452. ];
  4453. $this->assertHtml($expected, $result);
  4454. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4455. $this->Form->create();
  4456. $result = $this->Form->select(
  4457. 'Model.contact_id',
  4458. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4459. ['escape' => false, 'empty' => 'pick something']
  4460. );
  4461. $expected = [
  4462. 'select' => ['name' => 'Model[contact_id]'],
  4463. ['option' => ['value' => '']], 'pick something', '/option',
  4464. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4465. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4466. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4467. '/select',
  4468. ];
  4469. $this->assertHtml($expected, $result);
  4470. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4471. $this->Form->create();
  4472. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4473. $expected = [
  4474. 'select' => ['name' => 'Model[field]'],
  4475. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4476. ['option' => ['value' => '1']], 'Yes', '/option',
  4477. '/select',
  4478. ];
  4479. $this->assertHtml($expected, $result);
  4480. }
  4481. /**
  4482. * testSelectEscapeHtml method
  4483. *
  4484. * Test that select() escapes HTML.
  4485. */
  4486. public function testSelectEscapeHtml(): void
  4487. {
  4488. $result = $this->Form->select(
  4489. 'Model.field',
  4490. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4491. ['empty' => false]
  4492. );
  4493. $expected = [
  4494. 'select' => ['name' => 'Model[field]'],
  4495. ['option' => ['value' => 'first']],
  4496. 'first &quot;html&quot; &lt;chars&gt;',
  4497. '/option',
  4498. ['option' => ['value' => 'second']],
  4499. 'value',
  4500. '/option',
  4501. '/select',
  4502. ];
  4503. $this->assertHtml($expected, $result);
  4504. $result = $this->Form->select(
  4505. 'Model.field',
  4506. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4507. ['escape' => false, 'empty' => false]
  4508. );
  4509. $expected = [
  4510. 'select' => ['name' => 'Model[field]'],
  4511. ['option' => ['value' => 'first']],
  4512. 'first "html" <chars>',
  4513. '/option',
  4514. ['option' => ['value' => 'second']],
  4515. 'value',
  4516. '/option',
  4517. '/select',
  4518. ];
  4519. $this->assertHtml($expected, $result);
  4520. }
  4521. /**
  4522. * testSelectRequired method
  4523. *
  4524. * Test select() with required and disabled attributes.
  4525. */
  4526. public function testSelectRequired(): void
  4527. {
  4528. $this->article['required'] = [
  4529. 'user_id' => true,
  4530. ];
  4531. $this->Form->create($this->article);
  4532. $result = $this->Form->select('user_id', ['option A']);
  4533. $expected = [
  4534. 'select' => [
  4535. 'name' => 'user_id',
  4536. 'required' => 'required',
  4537. ],
  4538. ['option' => ['value' => '0']], 'option A', '/option',
  4539. '/select',
  4540. ];
  4541. $this->assertHtml($expected, $result);
  4542. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4543. $expected = [
  4544. 'select' => [
  4545. 'name' => 'user_id',
  4546. 'disabled' => 'disabled',
  4547. ],
  4548. ['option' => ['value' => '0']], 'option A', '/option',
  4549. '/select',
  4550. ];
  4551. $this->assertHtml($expected, $result);
  4552. }
  4553. public function testSelectEmptyWithRequiredFalse(): void
  4554. {
  4555. $Articles = $this->getTableLocator()->get('Articles');
  4556. $validator = $Articles->getValidator('default');
  4557. $validator->allowEmptyString('user_id');
  4558. $Articles->setValidator('default', $validator);
  4559. $entity = $Articles->newEmptyEntity();
  4560. $this->Form->create($entity);
  4561. $result = $this->Form->select('user_id', ['option A']);
  4562. $expected = [
  4563. 'select' => [
  4564. 'name' => 'user_id',
  4565. ],
  4566. ['option' => ['value' => '']], '/option',
  4567. ['option' => ['value' => '0']], 'option A', '/option',
  4568. '/select',
  4569. ];
  4570. $this->assertHtml($expected, $result);
  4571. }
  4572. /**
  4573. * testNestedSelect method
  4574. *
  4575. * Test select element generation with optgroups.
  4576. */
  4577. public function testNestedSelect(): void
  4578. {
  4579. $result = $this->Form->select(
  4580. 'Model.field',
  4581. [1 => 'One', 2 => 'Two', 'Three' => [
  4582. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4583. ]],
  4584. ['empty' => false]
  4585. );
  4586. $expected = [
  4587. 'select' => ['name' => 'Model[field]'],
  4588. ['option' => ['value' => 1]],
  4589. 'One',
  4590. '/option',
  4591. ['option' => ['value' => 2]],
  4592. 'Two',
  4593. '/option',
  4594. ['optgroup' => ['label' => 'Three']],
  4595. ['option' => ['value' => 3]],
  4596. 'Three',
  4597. '/option',
  4598. ['option' => ['value' => 4]],
  4599. 'Four',
  4600. '/option',
  4601. ['option' => ['value' => 5]],
  4602. 'Five',
  4603. '/option',
  4604. '/optgroup',
  4605. '/select',
  4606. ];
  4607. $this->assertHtml($expected, $result);
  4608. }
  4609. /**
  4610. * testSelectMultiple method
  4611. *
  4612. * Test generation of multiple select elements.
  4613. */
  4614. public function testSelectMultiple(): void
  4615. {
  4616. $options = ['first', 'second', 'third'];
  4617. $result = $this->Form->select(
  4618. 'Model.multi_field',
  4619. $options,
  4620. ['form' => 'my-form', 'multiple' => true]
  4621. );
  4622. $expected = [
  4623. 'input' => [
  4624. 'type' => 'hidden',
  4625. 'name' => 'Model[multi_field]',
  4626. 'value' => '',
  4627. 'form' => 'my-form',
  4628. ],
  4629. 'select' => [
  4630. 'name' => 'Model[multi_field][]',
  4631. 'multiple' => 'multiple',
  4632. 'form' => 'my-form',
  4633. ],
  4634. ['option' => ['value' => '0']],
  4635. 'first',
  4636. '/option',
  4637. ['option' => ['value' => '1']],
  4638. 'second',
  4639. '/option',
  4640. ['option' => ['value' => '2']],
  4641. 'third',
  4642. '/option',
  4643. '/select',
  4644. ];
  4645. $this->assertHtml($expected, $result);
  4646. $result = $this->Form->select(
  4647. 'Model.multi_field',
  4648. $options,
  4649. ['multiple' => 'multiple', 'form' => 'my-form']
  4650. );
  4651. $this->assertHtml($expected, $result);
  4652. $result = $this->Form->select(
  4653. 'Model.multi_field',
  4654. $options,
  4655. ['form' => 'my-form', 'multiple' => false]
  4656. );
  4657. $this->assertStringNotContainsString('multiple', $result);
  4658. }
  4659. /**
  4660. * testCheckboxZeroValue method
  4661. *
  4662. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4663. */
  4664. public function testCheckboxZeroValue(): void
  4665. {
  4666. $result = $this->Form->control('User.get_spam', [
  4667. 'type' => 'checkbox',
  4668. 'value' => '0',
  4669. 'hiddenField' => '1',
  4670. ]);
  4671. $expected = [
  4672. 'div' => ['class' => 'input checkbox'],
  4673. 'label' => ['for' => 'user-get-spam'],
  4674. ['input' => [
  4675. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4676. 'value' => '1',
  4677. ]],
  4678. ['input' => [
  4679. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4680. 'value' => '0', 'id' => 'user-get-spam',
  4681. ]],
  4682. 'Get Spam',
  4683. '/label',
  4684. '/div',
  4685. ];
  4686. $this->assertHtml($expected, $result);
  4687. $result = $this->Form->control('User.get_spam', [
  4688. 'type' => 'checkbox',
  4689. 'value' => '0',
  4690. 'hiddenField' => '',
  4691. ]);
  4692. $expected = [
  4693. 'div' => ['class' => 'input checkbox'],
  4694. 'label' => ['for' => 'user-get-spam'],
  4695. ['input' => [
  4696. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4697. 'value' => '',
  4698. ]],
  4699. ['input' => [
  4700. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4701. 'value' => '0', 'id' => 'user-get-spam',
  4702. ]],
  4703. 'Get Spam',
  4704. '/label',
  4705. '/div',
  4706. ];
  4707. $this->assertHtml($expected, $result);
  4708. }
  4709. /**
  4710. * testHabtmSelectBox method
  4711. *
  4712. * Test generation of habtm select boxes.
  4713. */
  4714. public function testHabtmSelectBox(): void
  4715. {
  4716. $options = [
  4717. 1 => 'blue',
  4718. 2 => 'red',
  4719. 3 => 'green',
  4720. ];
  4721. $tags = [
  4722. new Entity(['id' => 1, 'name' => 'blue']),
  4723. new Entity(['id' => 3, 'name' => 'green']),
  4724. ];
  4725. $article = new Article(['tags' => $tags]);
  4726. $this->Form->create($article);
  4727. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4728. $expected = [
  4729. 'div' => ['class' => 'input select'],
  4730. 'label' => ['for' => 'tags-ids'],
  4731. 'Tags',
  4732. '/label',
  4733. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4734. 'select' => [
  4735. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4736. 'multiple' => 'multiple',
  4737. ],
  4738. ['option' => ['value' => '1', 'selected' => 'selected']],
  4739. 'blue',
  4740. '/option',
  4741. ['option' => ['value' => '2']],
  4742. 'red',
  4743. '/option',
  4744. ['option' => ['value' => '3', 'selected' => 'selected']],
  4745. 'green',
  4746. '/option',
  4747. '/select',
  4748. '/div',
  4749. ];
  4750. $this->assertHtml($expected, $result);
  4751. // make sure only 50 is selected, and not 50f5c0cf
  4752. $options = [
  4753. '1' => 'blue',
  4754. '50f5c0cf' => 'red',
  4755. '50' => 'green',
  4756. ];
  4757. $tags = [
  4758. new Entity(['id' => 1, 'name' => 'blue']),
  4759. new Entity(['id' => 50, 'name' => 'green']),
  4760. ];
  4761. $article = new Article(['tags' => $tags]);
  4762. $this->Form->create($article);
  4763. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4764. $expected = [
  4765. 'div' => ['class' => 'input select'],
  4766. 'label' => ['for' => 'tags-ids'],
  4767. 'Tags',
  4768. '/label',
  4769. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4770. 'select' => [
  4771. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4772. 'multiple' => 'multiple',
  4773. ],
  4774. ['option' => ['value' => '1', 'selected' => 'selected']],
  4775. 'blue',
  4776. '/option',
  4777. ['option' => ['value' => '50f5c0cf']],
  4778. 'red',
  4779. '/option',
  4780. ['option' => ['value' => '50', 'selected' => 'selected']],
  4781. 'green',
  4782. '/option',
  4783. '/select',
  4784. '/div',
  4785. ];
  4786. $this->assertHtml($expected, $result);
  4787. $spacecraft = [
  4788. 1 => 'Orion',
  4789. 2 => 'Helios',
  4790. ];
  4791. $this->View->set('spacecraft', $spacecraft);
  4792. $this->Form->create();
  4793. $result = $this->Form->control('spacecraft._ids');
  4794. $expected = [
  4795. 'div' => ['class' => 'input select'],
  4796. 'label' => ['for' => 'spacecraft-ids'],
  4797. 'Spacecraft',
  4798. '/label',
  4799. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4800. 'select' => [
  4801. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4802. 'multiple' => 'multiple',
  4803. ],
  4804. ['option' => ['value' => '1']],
  4805. 'Orion',
  4806. '/option',
  4807. ['option' => ['value' => '2']],
  4808. 'Helios',
  4809. '/option',
  4810. '/select',
  4811. '/div',
  4812. ];
  4813. $this->assertHtml($expected, $result);
  4814. }
  4815. /**
  4816. * testErrorsForBelongsToManySelect method
  4817. *
  4818. * Tests that errors for belongsToMany select fields are being
  4819. * picked up properly.
  4820. */
  4821. public function testErrorsForBelongsToManySelect(): void
  4822. {
  4823. $spacecraft = [
  4824. 1 => 'Orion',
  4825. 2 => 'Helios',
  4826. ];
  4827. $this->View->set('spacecraft', $spacecraft);
  4828. $article = new Article();
  4829. $article->setError('spacecraft', ['Invalid']);
  4830. $this->Form->create($article);
  4831. $result = $this->Form->control('spacecraft._ids');
  4832. $expected = [
  4833. ['div' => ['class' => 'input select error']],
  4834. 'label' => ['for' => 'spacecraft-ids'],
  4835. 'Spacecraft',
  4836. '/label',
  4837. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4838. 'select' => [
  4839. 'name' => 'spacecraft[_ids][]',
  4840. 'id' => 'spacecraft-ids',
  4841. 'multiple' => 'multiple',
  4842. ],
  4843. ['option' => ['value' => '1']],
  4844. 'Orion',
  4845. '/option',
  4846. ['option' => ['value' => '2']],
  4847. 'Helios',
  4848. '/option',
  4849. '/select',
  4850. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4851. 'Invalid',
  4852. '/div',
  4853. '/div',
  4854. ];
  4855. $this->assertHtml($expected, $result);
  4856. }
  4857. /**
  4858. * testSelectMultipleCheckboxes method
  4859. *
  4860. * Test generation of multi select elements in checkbox format.
  4861. */
  4862. public function testSelectMultipleCheckboxes(): void
  4863. {
  4864. $result = $this->Form->select(
  4865. 'Model.multi_field',
  4866. ['first', 'second', 'third'],
  4867. ['multiple' => 'checkbox']
  4868. );
  4869. $expected = [
  4870. 'input' => [
  4871. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4872. ],
  4873. ['div' => ['class' => 'checkbox']],
  4874. ['label' => ['for' => 'model-multi-field-0']],
  4875. ['input' => [
  4876. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4877. 'value' => '0', 'id' => 'model-multi-field-0',
  4878. ]],
  4879. 'first',
  4880. '/label',
  4881. '/div',
  4882. ['div' => ['class' => 'checkbox']],
  4883. ['label' => ['for' => 'model-multi-field-1']],
  4884. ['input' => [
  4885. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4886. 'value' => '1', 'id' => 'model-multi-field-1',
  4887. ]],
  4888. 'second',
  4889. '/label',
  4890. '/div',
  4891. ['div' => ['class' => 'checkbox']],
  4892. ['label' => ['for' => 'model-multi-field-2']],
  4893. ['input' => [
  4894. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4895. 'value' => '2', 'id' => 'model-multi-field-2',
  4896. ]],
  4897. 'third',
  4898. '/label',
  4899. '/div',
  4900. ];
  4901. $this->assertHtml($expected, $result);
  4902. $result = $this->Form->select(
  4903. 'Model.multi_field',
  4904. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4905. ['multiple' => 'checkbox']
  4906. );
  4907. $expected = [
  4908. 'input' => [
  4909. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4910. ],
  4911. ['div' => ['class' => 'checkbox']],
  4912. ['label' => ['for' => 'model-multi-field-a+']],
  4913. ['input' => [
  4914. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4915. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  4916. ]],
  4917. 'first',
  4918. '/label',
  4919. '/div',
  4920. ['div' => ['class' => 'checkbox']],
  4921. ['label' => ['for' => 'model-multi-field-a++']],
  4922. ['input' => [
  4923. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4924. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  4925. ]],
  4926. 'second',
  4927. '/label',
  4928. '/div',
  4929. ['div' => ['class' => 'checkbox']],
  4930. ['label' => ['for' => 'model-multi-field-a+++']],
  4931. ['input' => [
  4932. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4933. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  4934. ]],
  4935. 'third',
  4936. '/label',
  4937. '/div',
  4938. ];
  4939. $this->assertHtml($expected, $result);
  4940. $result = $this->Form->select(
  4941. 'Model.multi_field',
  4942. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4943. ['multiple' => 'checkbox']
  4944. );
  4945. $expected = [
  4946. 'input' => [
  4947. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4948. ],
  4949. ['div' => ['class' => 'checkbox']],
  4950. ['label' => ['for' => 'model-multi-field-a-b']],
  4951. ['input' => [
  4952. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4953. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  4954. ]],
  4955. 'first',
  4956. '/label',
  4957. '/div',
  4958. ['div' => ['class' => 'checkbox']],
  4959. ['label' => ['for' => 'model-multi-field-a-b1']],
  4960. ['input' => [
  4961. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4962. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  4963. ]],
  4964. 'second',
  4965. '/label',
  4966. '/div',
  4967. ['div' => ['class' => 'checkbox']],
  4968. ['label' => ['for' => 'model-multi-field-a-b2']],
  4969. ['input' => [
  4970. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4971. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  4972. ]],
  4973. 'third',
  4974. '/label',
  4975. '/div',
  4976. ];
  4977. $this->assertHtml($expected, $result);
  4978. }
  4979. /**
  4980. * testSelectMultipleCheckboxRequestData method
  4981. *
  4982. * Ensure that multiCheckbox reads from the request data.
  4983. */
  4984. public function testSelectMultipleCheckboxRequestData(): void
  4985. {
  4986. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  4987. $result = $this->Form->select(
  4988. 'Model.tags',
  4989. ['1' => 'first', 'Array' => 'Array'],
  4990. ['multiple' => 'checkbox']
  4991. );
  4992. $expected = [
  4993. 'input' => [
  4994. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  4995. ],
  4996. ['div' => ['class' => 'checkbox']],
  4997. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4998. ['input' => [
  4999. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5000. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5001. ]],
  5002. 'first',
  5003. '/label',
  5004. '/div',
  5005. ['div' => ['class' => 'checkbox']],
  5006. ['label' => ['for' => 'model-tags-array']],
  5007. ['input' => [
  5008. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5009. 'value' => 'Array', 'id' => 'model-tags-array',
  5010. ]],
  5011. 'Array',
  5012. '/label',
  5013. '/div',
  5014. ];
  5015. $this->assertHtml($expected, $result);
  5016. }
  5017. /**
  5018. * testSelectMultipleCheckboxSecurity method
  5019. *
  5020. * Checks the security hash array generated for multiple-input checkbox elements.
  5021. */
  5022. public function testSelectMultipleCheckboxSecurity(): void
  5023. {
  5024. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5025. $this->Form->create();
  5026. $this->Form->select(
  5027. 'Model.multi_field',
  5028. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5029. ['multiple' => 'checkbox']
  5030. );
  5031. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5032. $this->assertEquals(['Model.multi_field'], $fields);
  5033. $result = $this->Form->secure();
  5034. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5035. $hash = urlencode($hash . ':');
  5036. $this->assertStringContainsString('"' . $hash . '"', $result);
  5037. }
  5038. /**
  5039. * testSelectMultipleSecureWithNoOptions method
  5040. *
  5041. * Multiple select elements should always be secured as they always participate
  5042. * in the POST data.
  5043. */
  5044. public function testSelectMultipleSecureWithNoOptions(): void
  5045. {
  5046. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5047. $this->Form->create();
  5048. $this->Form->select(
  5049. 'Model.select',
  5050. [],
  5051. ['multiple' => true]
  5052. );
  5053. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5054. $this->assertEquals(['Model.select'], $result);
  5055. }
  5056. /**
  5057. * testSelectNoSecureWithNoOptions method
  5058. *
  5059. * When a select box has no options it should not be added to the fields list
  5060. * as it always fail post validation.
  5061. */
  5062. public function testSelectNoSecureWithNoOptions(): void
  5063. {
  5064. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5065. $this->Form->create();
  5066. $this->Form->select(
  5067. 'Model.select',
  5068. []
  5069. );
  5070. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5071. $this->assertEquals([], $result);
  5072. $this->Form->select(
  5073. 'Model.user_id',
  5074. [],
  5075. ['empty' => true]
  5076. );
  5077. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5078. $this->assertEquals(['Model.user_id'], $result);
  5079. }
  5080. /**
  5081. * testControlMultipleCheckboxes method
  5082. *
  5083. * Test control() resulting in multi select elements being generated.
  5084. */
  5085. public function testControlMultipleCheckboxes(): void
  5086. {
  5087. $result = $this->Form->control('Model.multi_field', [
  5088. 'options' => ['first', 'second', 'third'],
  5089. 'multiple' => 'checkbox',
  5090. ]);
  5091. $expected = [
  5092. ['div' => ['class' => 'input select']],
  5093. ['label' => ['for' => 'model-multi-field']],
  5094. 'Multi Field',
  5095. '/label',
  5096. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5097. ['div' => ['class' => 'checkbox']],
  5098. ['label' => ['for' => 'model-multi-field-0']],
  5099. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5100. 'first',
  5101. '/label',
  5102. '/div',
  5103. ['div' => ['class' => 'checkbox']],
  5104. ['label' => ['for' => 'model-multi-field-1']],
  5105. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5106. 'second',
  5107. '/label',
  5108. '/div',
  5109. ['div' => ['class' => 'checkbox']],
  5110. ['label' => ['for' => 'model-multi-field-2']],
  5111. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5112. 'third',
  5113. '/label',
  5114. '/div',
  5115. '/div',
  5116. ];
  5117. $this->assertHtml($expected, $result);
  5118. $result = $this->Form->control('Model.multi_field', [
  5119. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5120. 'multiple' => 'checkbox',
  5121. ]);
  5122. $expected = [
  5123. ['div' => ['class' => 'input select']],
  5124. ['label' => ['for' => 'model-multi-field']],
  5125. 'Multi Field',
  5126. '/label',
  5127. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5128. ['div' => ['class' => 'checkbox']],
  5129. ['label' => ['for' => 'model-multi-field-a']],
  5130. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5131. 'first',
  5132. '/label',
  5133. '/div',
  5134. ['div' => ['class' => 'checkbox']],
  5135. ['label' => ['for' => 'model-multi-field-b']],
  5136. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5137. 'second',
  5138. '/label',
  5139. '/div',
  5140. ['div' => ['class' => 'checkbox']],
  5141. ['label' => ['for' => 'model-multi-field-c']],
  5142. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5143. 'third',
  5144. '/label',
  5145. '/div',
  5146. '/div',
  5147. ];
  5148. $this->assertHtml($expected, $result);
  5149. }
  5150. /**
  5151. * testSelectHiddenFieldOmission method
  5152. *
  5153. * Test that select() with 'hiddenField' => false omits the hidden field.
  5154. */
  5155. public function testSelectHiddenFieldOmission(): void
  5156. {
  5157. $result = $this->Form->select(
  5158. 'Model.multi_field',
  5159. ['first', 'second'],
  5160. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5161. );
  5162. $this->assertStringNotContainsString('type="hidden"', $result);
  5163. }
  5164. /**
  5165. * testSelectCheckboxMultipleOverrideName method
  5166. *
  5167. * Test that select() with multiple = checkbox works with overriding name attribute.
  5168. */
  5169. public function testSelectCheckboxMultipleOverrideName(): void
  5170. {
  5171. $result = $this->Form->select('category', ['1', '2'], [
  5172. 'multiple' => 'checkbox',
  5173. 'name' => 'fish',
  5174. ]);
  5175. $expected = [
  5176. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5177. ['div' => ['class' => 'checkbox']],
  5178. ['label' => ['for' => 'fish-0']],
  5179. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5180. '1',
  5181. '/label',
  5182. '/div',
  5183. ['div' => ['class' => 'checkbox']],
  5184. ['label' => ['for' => 'fish-1']],
  5185. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5186. '2',
  5187. '/label',
  5188. '/div',
  5189. ];
  5190. $this->assertHtml($expected, $result);
  5191. $result = $this->Form->multiCheckbox(
  5192. 'category',
  5193. new Collection(['1', '2']),
  5194. ['name' => 'fish']
  5195. );
  5196. $this->assertHtml($expected, $result);
  5197. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5198. 'name' => 'fish',
  5199. ]);
  5200. $this->assertHtml($expected, $result);
  5201. }
  5202. /**
  5203. * testControlMultiCheckbox method
  5204. *
  5205. * Test that control() works with multicheckbox.
  5206. */
  5207. public function testControlMultiCheckbox(): void
  5208. {
  5209. $result = $this->Form->control('category', [
  5210. 'type' => 'multicheckbox',
  5211. 'options' => ['1', '2'],
  5212. ]);
  5213. $expected = [
  5214. ['div' => ['class' => 'input multicheckbox']],
  5215. '<label',
  5216. 'Category',
  5217. '/label',
  5218. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5219. ['div' => ['class' => 'checkbox']],
  5220. ['label' => ['for' => 'category-0']],
  5221. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5222. '1',
  5223. '/label',
  5224. '/div',
  5225. ['div' => ['class' => 'checkbox']],
  5226. ['label' => ['for' => 'category-1']],
  5227. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5228. '2',
  5229. '/label',
  5230. '/div',
  5231. '/div',
  5232. ];
  5233. $this->assertHtml($expected, $result);
  5234. }
  5235. /**
  5236. * testCheckbox method
  5237. *
  5238. * Test generation of checkboxes.
  5239. */
  5240. public function testCheckbox(): void
  5241. {
  5242. $result = $this->Form->checkbox('Model.field');
  5243. $expected = [
  5244. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5245. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5246. ];
  5247. $this->assertHtml($expected, $result);
  5248. $result = $this->Form->checkbox('Model.field', [
  5249. 'id' => 'theID',
  5250. 'value' => 'myvalue',
  5251. 'form' => 'my-form',
  5252. ]);
  5253. $expected = [
  5254. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5255. ['input' => [
  5256. 'type' => 'checkbox', 'name' => 'Model[field]',
  5257. 'value' => 'myvalue', 'id' => 'theID',
  5258. 'form' => 'my-form',
  5259. ]],
  5260. ];
  5261. $this->assertHtml($expected, $result);
  5262. }
  5263. /**
  5264. * testCheckboxDefaultValue method
  5265. *
  5266. * Test default value setting on checkbox() method.
  5267. */
  5268. public function testCheckboxDefaultValue(): void
  5269. {
  5270. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5271. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5272. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5273. $this->assertHtml($expected, $result);
  5274. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5275. $this->Form->create();
  5276. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5277. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5278. $this->assertHtml($expected, $result);
  5279. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5280. $this->Form->create();
  5281. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5282. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5283. $this->assertHtml($expected, $result);
  5284. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5285. $this->Form->create();
  5286. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5287. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5288. $this->assertHtml($expected, $result);
  5289. $Articles = $this->getTableLocator()->get('Articles');
  5290. $Articles->getSchema()->addColumn(
  5291. 'published',
  5292. ['type' => 'boolean', 'null' => false, 'default' => true]
  5293. );
  5294. $this->Form->create($Articles->newEmptyEntity());
  5295. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5296. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5297. $this->assertHtml($expected, $result);
  5298. }
  5299. /**
  5300. * testCheckboxCheckedAndError method
  5301. *
  5302. * Test checkbox being checked or having errors.
  5303. */
  5304. public function testCheckboxCheckedAndError(): void
  5305. {
  5306. $this->article['errors'] = [
  5307. 'published' => true,
  5308. ];
  5309. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5310. $this->Form->create($this->article);
  5311. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5312. $expected = [
  5313. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5314. ['input' => [
  5315. 'type' => 'checkbox',
  5316. 'name' => 'published',
  5317. 'value' => 'myvalue',
  5318. 'id' => 'theID',
  5319. 'checked' => 'checked',
  5320. 'class' => 'form-error',
  5321. ]],
  5322. ];
  5323. $this->assertHtml($expected, $result);
  5324. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5325. $this->Form->create($this->article);
  5326. $result = $this->Form->checkbox('published');
  5327. $expected = [
  5328. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5329. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5330. ];
  5331. $this->assertHtml($expected, $result);
  5332. }
  5333. /**
  5334. * testCheckboxCustomNameAttribute method
  5335. *
  5336. * Test checkbox() with a custom name attribute.
  5337. */
  5338. public function testCheckboxCustomNameAttribute(): void
  5339. {
  5340. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5341. $expected = [
  5342. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5343. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5344. ];
  5345. $this->assertHtml($expected, $result);
  5346. }
  5347. /**
  5348. * testCheckboxHiddenField method
  5349. *
  5350. * Test that the hidden input for checkboxes can be omitted or set to a
  5351. * specific value.
  5352. */
  5353. public function testCheckboxHiddenField(): void
  5354. {
  5355. $result = $this->Form->checkbox('UserForm.something', [
  5356. 'hiddenField' => false,
  5357. ]);
  5358. $expected = [
  5359. 'input' => [
  5360. 'type' => 'checkbox',
  5361. 'name' => 'UserForm[something]',
  5362. 'value' => '1',
  5363. ],
  5364. ];
  5365. $this->assertHtml($expected, $result);
  5366. $result = $this->Form->checkbox('UserForm.something', [
  5367. 'value' => 'Y',
  5368. 'hiddenField' => '',
  5369. ]);
  5370. $expected = [
  5371. ['input' => [
  5372. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5373. 'value' => '',
  5374. ]],
  5375. ['input' => [
  5376. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5377. 'value' => 'Y',
  5378. ]],
  5379. ];
  5380. $this->assertHtml($expected, $result);
  5381. $result = $this->Form->checkbox('UserForm.something', [
  5382. 'value' => 'Y',
  5383. 'hiddenField' => 'N',
  5384. ]);
  5385. $expected = [
  5386. ['input' => [
  5387. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5388. 'value' => 'N',
  5389. ]],
  5390. ['input' => [
  5391. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5392. 'value' => 'Y',
  5393. ]],
  5394. ];
  5395. $this->assertHtml($expected, $result);
  5396. }
  5397. /**
  5398. * testTime method
  5399. *
  5400. * Test the time type.
  5401. */
  5402. public function testTime(): void
  5403. {
  5404. $result = $this->Form->time('start_time', [
  5405. 'value' => '2014-03-08 16:30:00',
  5406. ]);
  5407. $expected = [
  5408. 'input' => [
  5409. 'type' => 'time',
  5410. 'name' => 'start_time',
  5411. 'value' => '16:30:00',
  5412. 'step' => '1',
  5413. ],
  5414. ];
  5415. $this->assertHtml($expected, $result);
  5416. }
  5417. /**
  5418. * testDate method
  5419. *
  5420. * Test the date type.
  5421. */
  5422. public function testDate(): void
  5423. {
  5424. $result = $this->Form->date('start_day', [
  5425. 'value' => '2014-03-08',
  5426. ]);
  5427. $expected = [
  5428. 'input' => [
  5429. 'type' => 'date',
  5430. 'name' => 'start_day',
  5431. 'value' => '2014-03-08',
  5432. ],
  5433. ];
  5434. $this->assertHtml($expected, $result);
  5435. $result = $this->Form->date('start_day', [
  5436. 'value' => new Date('2014-03-08'),
  5437. ]);
  5438. $this->assertHtml($expected, $result);
  5439. }
  5440. /**
  5441. * testDateTime method
  5442. */
  5443. public function testDateTime(): void
  5444. {
  5445. $result = $this->Form->dateTime('date', ['default' => true]);
  5446. $expected = [
  5447. 'input' => [
  5448. 'type' => 'datetime-local',
  5449. 'name' => 'date',
  5450. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5451. 'step' => '1',
  5452. ],
  5453. ];
  5454. $this->assertHtml($expected, $result);
  5455. }
  5456. /**
  5457. * testDateTimeSecured method
  5458. *
  5459. * Test that datetime fields are added to protected fields list.
  5460. */
  5461. public function testDateTimeSecured(): void
  5462. {
  5463. $this->View->setRequest(
  5464. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5465. );
  5466. $this->Form->create();
  5467. $this->Form->dateTime('date');
  5468. $expected = ['date'];
  5469. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5470. $this->assertEquals($expected, $result);
  5471. $this->Form->date('published');
  5472. $expected = ['date', 'published'];
  5473. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5474. $this->assertEquals($expected, $result);
  5475. }
  5476. /**
  5477. * testDateTimeSecuredDisabled method
  5478. *
  5479. * Test that datetime fields are added to protected fields list.
  5480. */
  5481. public function testDateTimeSecuredDisabled(): void
  5482. {
  5483. $this->View->setRequest(
  5484. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5485. );
  5486. $this->Form->create();
  5487. $this->Form->dateTime('date', ['secure' => false]);
  5488. $expected = [];
  5489. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5490. $this->assertEquals($expected, $result);
  5491. $this->Form->date('published', ['secure' => false]);
  5492. $expected = [];
  5493. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5494. $this->assertEquals($expected, $result);
  5495. }
  5496. /**
  5497. * testDatetimeWithDefault method
  5498. *
  5499. * Test that datetime() and default values work.
  5500. */
  5501. public function testDatetimeWithDefault(): void
  5502. {
  5503. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5504. $expected = [
  5505. 'input' => [
  5506. 'type' => 'datetime-local',
  5507. 'name' => 'updated',
  5508. 'value' => '2009-06-01T11:15:30',
  5509. 'step' => '1',
  5510. ],
  5511. ];
  5512. $this->assertHtml($expected, $result);
  5513. $result = $this->Form->dateTime('updated', [
  5514. 'default' => '2009-06-01 11:15:30',
  5515. ]);
  5516. $expected = [
  5517. 'input' => [
  5518. 'type' => 'datetime-local',
  5519. 'name' => 'updated',
  5520. 'value' => '2009-06-01T11:15:30',
  5521. 'step' => '1',
  5522. ],
  5523. ];
  5524. $this->assertHtml($expected, $result);
  5525. }
  5526. /**
  5527. * testMonth method
  5528. *
  5529. * Test generation of a month input.
  5530. */
  5531. public function testMonth(): void
  5532. {
  5533. $result = $this->Form->month('field', ['value' => '']);
  5534. $expected = [
  5535. 'input' => [
  5536. 'type' => 'month',
  5537. 'name' => 'field',
  5538. 'value' => '',
  5539. ],
  5540. ];
  5541. $this->assertHtml($expected, $result);
  5542. $this->View->setRequest(
  5543. $this->View->getRequest()->withData('release', '2050-02-10')
  5544. );
  5545. $this->Form->create();
  5546. $result = $this->Form->month('release');
  5547. $expected = [
  5548. 'input' => [
  5549. 'type' => 'month',
  5550. 'name' => 'release',
  5551. 'value' => '2050-02',
  5552. ],
  5553. ];
  5554. $this->assertHtml($expected, $result);
  5555. $this->View->setRequest(
  5556. $this->View->getRequest()->withData('release', '2050-03')
  5557. );
  5558. $this->Form->create();
  5559. $result = $this->Form->month('release');
  5560. $expected = [
  5561. 'input' => [
  5562. 'type' => 'month',
  5563. 'name' => 'release',
  5564. 'value' => '2050-03',
  5565. ],
  5566. ];
  5567. $this->assertHtml($expected, $result);
  5568. }
  5569. /**
  5570. * testYear method
  5571. *
  5572. * Test generation of a year input.
  5573. */
  5574. public function testYear(): void
  5575. {
  5576. $this->View->setRequest(
  5577. $this->View->getRequest()->withData('published', '2006')
  5578. );
  5579. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5580. $expected = [
  5581. ['select' => ['name' => 'field']],
  5582. ['option' => ['selected' => 'selected', 'value' => '']],
  5583. '/option',
  5584. ['option' => ['value' => '2007']],
  5585. '2007',
  5586. '/option',
  5587. ['option' => ['value' => '2006']],
  5588. '2006',
  5589. '/option',
  5590. '/select',
  5591. ];
  5592. $this->assertHtml($expected, $result);
  5593. $result = $this->Form->year('field', [
  5594. 'value' => '',
  5595. 'min' => 2006,
  5596. 'max' => 2007,
  5597. 'order' => 'asc',
  5598. ]);
  5599. $expected = [
  5600. ['select' => ['name' => 'field']],
  5601. ['option' => ['selected' => 'selected', 'value' => '']],
  5602. '/option',
  5603. ['option' => ['value' => '2006']],
  5604. '2006',
  5605. '/option',
  5606. ['option' => ['value' => '2007']],
  5607. '2007',
  5608. '/option',
  5609. '/select',
  5610. ];
  5611. $this->assertHtml($expected, $result);
  5612. $result = $this->Form->year('published', [
  5613. 'empty' => false,
  5614. 'min' => 2006,
  5615. 'max' => 2007,
  5616. ]);
  5617. $expected = [
  5618. ['select' => ['name' => 'published']],
  5619. ['option' => ['value' => '2007']],
  5620. '2007',
  5621. '/option',
  5622. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5623. '2006',
  5624. '/option',
  5625. '/select',
  5626. ];
  5627. $this->assertHtml($expected, $result);
  5628. $result = $this->Form->year('published', [
  5629. 'empty' => false,
  5630. 'value' => new Date('2008-01-12'),
  5631. 'min' => 2007,
  5632. 'max' => 2009,
  5633. ]);
  5634. $expected = [
  5635. ['select' => ['name' => 'published']],
  5636. ['option' => ['value' => '2009']],
  5637. '2009',
  5638. '/option',
  5639. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5640. '2008',
  5641. '/option',
  5642. ['option' => ['value' => '2007']],
  5643. '2007',
  5644. '/option',
  5645. '/select',
  5646. ];
  5647. $this->assertHtml($expected, $result);
  5648. $result = $this->Form->year('published', [
  5649. 'empty' => 'Published on',
  5650. ]);
  5651. $this->assertStringContainsString('Published on', $result);
  5652. }
  5653. /**
  5654. * testControlYearPreEpoch method
  5655. *
  5656. * Test minYear being prior to the unix epoch.
  5657. */
  5658. public function testControlYearPreEpoch(): void
  5659. {
  5660. $start = date('Y') - 80;
  5661. $end = date('Y') - 18;
  5662. $result = $this->Form->control('birth_year', [
  5663. 'type' => 'year',
  5664. 'label' => 'Birth Year',
  5665. 'min' => $start,
  5666. 'max' => $end,
  5667. ]);
  5668. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5669. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5670. $this->assertStringNotContainsString('value="00">00', $result);
  5671. }
  5672. /**
  5673. * test control() datetime & required attributes
  5674. */
  5675. public function testControlDatetimeRequired(): void
  5676. {
  5677. $result = $this->Form->control('birthday', [
  5678. 'type' => 'date',
  5679. 'required' => true,
  5680. ]);
  5681. $this->assertStringContainsString(
  5682. '<input type="date" name="birthday" required="required"',
  5683. $result
  5684. );
  5685. }
  5686. /**
  5687. * testYearAutoExpandRange method
  5688. */
  5689. public function testYearAutoExpandRange(): void
  5690. {
  5691. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5692. $result = $this->Form->year('birthday');
  5693. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5694. $result = $matches[1];
  5695. $expected = range(date('Y') + 5, 1930);
  5696. $this->assertEquals($expected, $result);
  5697. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5698. $this->Form->create();
  5699. $result = $this->Form->year('release');
  5700. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5701. $result = $matches[1];
  5702. $expected = range(2050, date('Y') - 5);
  5703. $this->assertEquals($expected, $result);
  5704. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5705. $this->Form->create();
  5706. $result = $this->Form->year('release', [
  5707. 'min' => 1890,
  5708. 'max' => 1900,
  5709. ]);
  5710. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5711. $result = $matches[1];
  5712. $expected = range(1900, 1881);
  5713. $this->assertEquals($expected, $result);
  5714. }
  5715. /**
  5716. * test control placeholder + label
  5717. */
  5718. public function testControlLabelAndPlaceholder(): void
  5719. {
  5720. $this->Form->create($this->article);
  5721. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5722. $expected = [
  5723. 'div' => ['class' => 'input text required'],
  5724. 'label' => ['for' => 'title'],
  5725. 'Title',
  5726. '/label',
  5727. 'input' => [
  5728. 'aria-required' => 'true',
  5729. 'type' => 'text',
  5730. 'required' => 'required',
  5731. 'placeholder' => 'Add title',
  5732. 'id' => 'title',
  5733. 'name' => 'title',
  5734. 'data-validity-message' => 'This field cannot be left empty',
  5735. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5736. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5737. ],
  5738. '/div',
  5739. ];
  5740. $this->assertHtml($expected, $result);
  5741. }
  5742. /**
  5743. * testControlLabelFalse method
  5744. *
  5745. * Test the label option being set to false.
  5746. */
  5747. public function testControlLabelFalse(): void
  5748. {
  5749. $this->Form->create($this->article);
  5750. $result = $this->Form->control('title', ['label' => false]);
  5751. $expected = [
  5752. 'div' => ['class' => 'input text required'],
  5753. 'input' => [
  5754. 'aria-required' => 'true',
  5755. 'type' => 'text',
  5756. 'required' => 'required',
  5757. 'id' => 'title',
  5758. 'name' => 'title',
  5759. 'data-validity-message' => 'This field cannot be left empty',
  5760. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5761. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5762. ],
  5763. '/div',
  5764. ];
  5765. $this->assertHtml($expected, $result);
  5766. $this->Form->create($this->article);
  5767. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5768. $expected['input'] += [
  5769. 'placeholder' => 'Add title',
  5770. 'aria-label' => 'Add title',
  5771. ];
  5772. $this->assertHtml($expected, $result);
  5773. }
  5774. /**
  5775. * testTextArea method
  5776. *
  5777. * Test generation of a textarea input.
  5778. */
  5779. public function testTextArea(): void
  5780. {
  5781. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5782. $result = $this->Form->textarea('field');
  5783. $expected = [
  5784. 'textarea' => ['name' => 'field', 'rows' => 5],
  5785. 'some test data',
  5786. '/textarea',
  5787. ];
  5788. $this->assertHtml($expected, $result);
  5789. $result = $this->Form->textarea('user.bio');
  5790. $expected = [
  5791. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5792. '/textarea',
  5793. ];
  5794. $this->assertHtml($expected, $result);
  5795. $this->View->setRequest($this->View->getRequest()
  5796. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5797. $this->Form->create();
  5798. $result = $this->Form->textarea('field');
  5799. $expected = [
  5800. 'textarea' => ['name' => 'field', 'rows' => 5],
  5801. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5802. '/textarea',
  5803. ];
  5804. $this->assertHtml($expected, $result);
  5805. $this->View->setRequest($this->View->getRequest()->withData(
  5806. 'Model.field',
  5807. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5808. ));
  5809. $this->Form->create();
  5810. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5811. $expected = [
  5812. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5813. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5814. '/textarea',
  5815. ];
  5816. $this->assertHtml($expected, $result);
  5817. $result = $this->Form->textarea('0.OtherModel.field');
  5818. $expected = [
  5819. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5820. '/textarea',
  5821. ];
  5822. $this->assertHtml($expected, $result);
  5823. }
  5824. /**
  5825. * testTextAreaWithStupidCharacters method
  5826. *
  5827. * Test text area with non-ascii characters.
  5828. */
  5829. public function testTextAreaWithStupidCharacters(): void
  5830. {
  5831. $result = $this->Form->textarea('Post.content', [
  5832. 'value' => 'GREAT®',
  5833. 'rows' => '15',
  5834. 'cols' => '75',
  5835. ]);
  5836. $expected = [
  5837. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5838. 'GREAT®',
  5839. '/textarea',
  5840. ];
  5841. $this->assertHtml($expected, $result);
  5842. }
  5843. /**
  5844. * testTextAreaMaxLength method
  5845. *
  5846. * Test textareas maxlength read from schema.
  5847. */
  5848. public function testTextAreaMaxLength(): void
  5849. {
  5850. $this->Form->create([
  5851. 'schema' => [
  5852. 'stuff' => ['type' => 'string', 'length' => 10],
  5853. ],
  5854. ]);
  5855. $result = $this->Form->control('other', ['type' => 'textarea']);
  5856. $expected = [
  5857. 'div' => ['class' => 'input textarea'],
  5858. 'label' => ['for' => 'other'],
  5859. 'Other',
  5860. '/label',
  5861. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5862. '/textarea',
  5863. '/div',
  5864. ];
  5865. $this->assertHtml($expected, $result);
  5866. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5867. $expected = [
  5868. 'div' => ['class' => 'input textarea'],
  5869. 'label' => ['for' => 'stuff'],
  5870. 'Stuff',
  5871. '/label',
  5872. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5873. '/textarea',
  5874. '/div',
  5875. ];
  5876. $this->assertHtml($expected, $result);
  5877. }
  5878. /**
  5879. * testHiddenField method
  5880. *
  5881. * Test generation of a hidden input.
  5882. */
  5883. public function testHidden(): void
  5884. {
  5885. $this->article['errors'] = [
  5886. 'field' => true,
  5887. ];
  5888. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5889. $this->Form->create($this->article);
  5890. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5891. $expected = [
  5892. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5893. $this->assertHtml($expected, $result);
  5894. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5895. $expected = [
  5896. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5897. ];
  5898. $this->assertHtml($expected, $result);
  5899. }
  5900. /**
  5901. * Test hidden() with various boolean values.
  5902. */
  5903. public function testHiddenBooleanValues(): void
  5904. {
  5905. $this->Form->create($this->article);
  5906. $result = $this->Form->hidden('field', ['value' => null]);
  5907. $expected = [
  5908. 'input' => ['type' => 'hidden', 'name' => 'field'],
  5909. ];
  5910. $this->assertHtml($expected, $result);
  5911. $result = $this->Form->hidden('field', ['value' => true]);
  5912. $expected = [
  5913. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  5914. ];
  5915. $this->assertHtml($expected, $result);
  5916. $result = $this->Form->hidden('field', ['value' => false]);
  5917. $expected = [
  5918. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  5919. ];
  5920. $this->assertHtml($expected, $result);
  5921. }
  5922. /**
  5923. * testFileUploadField method
  5924. *
  5925. * Test generation of a file upload input.
  5926. */
  5927. public function testFileUploadField(): void
  5928. {
  5929. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5930. $result = $this->Form->file('Model.upload');
  5931. $this->assertHtml($expected, $result);
  5932. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5933. 'name' => '', 'type' => '', 'tmp_name' => '',
  5934. 'error' => 4, 'size' => 0,
  5935. ]));
  5936. $result = $this->Form->file('Model.upload');
  5937. $this->assertHtml($expected, $result);
  5938. $this->View->setRequest(
  5939. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  5940. );
  5941. $result = $this->Form->file('Model.upload');
  5942. $this->assertHtml($expected, $result);
  5943. }
  5944. /**
  5945. * testFileUploadOnOtherModel method
  5946. *
  5947. * Test File upload input on a model not used in create().
  5948. */
  5949. public function testFileUploadOnOtherModel(): void
  5950. {
  5951. $this->Form->create($this->article, ['type' => 'file']);
  5952. $result = $this->Form->file('ValidateProfile.city');
  5953. $expected = [
  5954. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  5955. ];
  5956. $this->assertHtml($expected, $result);
  5957. }
  5958. /**
  5959. * testButton method
  5960. *
  5961. * Test generation of a form button.
  5962. */
  5963. public function testButton(): void
  5964. {
  5965. $result = $this->Form->button('Hi');
  5966. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5967. $this->assertHtml($expected, $result);
  5968. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  5969. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  5970. $this->assertHtml($expected, $result);
  5971. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  5972. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  5973. $this->assertHtml($expected, $result);
  5974. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  5975. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  5976. $this->assertHtml($expected, $result);
  5977. $result = $this->Form->button('No type', ['type' => false]);
  5978. $expected = ['button' => [], 'No type', '/button'];
  5979. $this->assertHtml($expected, $result);
  5980. $result = $this->Form->button('Upload Text', [
  5981. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5982. 'escape' => false,
  5983. ]);
  5984. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  5985. }
  5986. /**
  5987. * testButtonUnlockedByDefault method
  5988. *
  5989. * Test that button() makes unlocked fields by default.
  5990. */
  5991. public function testButtonUnlockedByDefault(): void
  5992. {
  5993. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5994. $this->Form->create();
  5995. $this->Form->button('Save', ['name' => 'save']);
  5996. $this->Form->button('Clear');
  5997. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  5998. $this->assertEquals(['save'], $result);
  5999. }
  6000. /**
  6001. * Test generation of a form button with confirm message.
  6002. */
  6003. public function testButtonWithConfirm(): void
  6004. {
  6005. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6006. $expected = ['button' => [
  6007. 'type' => 'submit',
  6008. 'data-confirm-message' => 'Confirm me!',
  6009. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6010. ], 'Hi', '/button'];
  6011. $this->assertHtml($expected, $result);
  6012. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6013. $expected = ['button' => [
  6014. 'type' => 'submit',
  6015. 'data-confirm-message' => 'Confirm "me"!',
  6016. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6017. ], 'Hi', '/button'];
  6018. $this->assertHtml($expected, $result);
  6019. }
  6020. /**
  6021. * testPostButton method
  6022. */
  6023. public function testPostButton(): void
  6024. {
  6025. $result = $this->Form->postButton('Hi', '/controller/action');
  6026. $expected = [
  6027. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6028. 'button' => ['type' => 'submit'],
  6029. 'Hi',
  6030. '/button',
  6031. '/form',
  6032. ];
  6033. $this->assertHtml($expected, $result);
  6034. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6035. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6036. }
  6037. /**
  6038. * testPostButtonMethodType method
  6039. */
  6040. public function testPostButtonMethodType(): void
  6041. {
  6042. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6043. $expected = [
  6044. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6045. 'div' => ['style' => 'display:none;'],
  6046. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6047. '/div',
  6048. 'button' => ['type' => 'submit'],
  6049. 'Hi',
  6050. '/button',
  6051. '/form',
  6052. ];
  6053. $this->assertHtml($expected, $result);
  6054. }
  6055. /**
  6056. * testPostButtonFormOptions method
  6057. */
  6058. public function testPostButtonFormOptions(): void
  6059. {
  6060. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6061. $expected = [
  6062. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6063. 'button' => ['type' => 'submit'],
  6064. 'Hi',
  6065. '/button',
  6066. '/form',
  6067. ];
  6068. $this->assertHtml($expected, $result);
  6069. }
  6070. /**
  6071. * testPostButtonNestedData method
  6072. *
  6073. * Test using postButton with N dimensional data.
  6074. */
  6075. public function testPostButtonNestedData(): void
  6076. {
  6077. $data = [
  6078. 'one' => [
  6079. 'two' => [
  6080. 3, 4, 5,
  6081. ],
  6082. ],
  6083. ];
  6084. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6085. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6086. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6087. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6088. }
  6089. /**
  6090. * testSecurePostButton method
  6091. *
  6092. * Test that postButton adds _Token fields.
  6093. */
  6094. public function testSecurePostButton(): void
  6095. {
  6096. $this->View->setRequest($this->View->getRequest()
  6097. ->withAttribute('csrfToken', 'testkey')
  6098. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6099. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6100. $tokenDebug = urlencode(json_encode([
  6101. '/posts/delete/1',
  6102. [],
  6103. [],
  6104. ]));
  6105. $expected = [
  6106. 'form' => [
  6107. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6108. ],
  6109. ['div' => ['style' => 'display:none;']],
  6110. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6111. '/div',
  6112. 'button' => ['type' => 'submit'],
  6113. 'Delete',
  6114. '/button',
  6115. ['div' => ['style' => 'display:none;']],
  6116. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6117. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6118. ['input' => [
  6119. 'type' => 'hidden',
  6120. 'name' => '_Token[debug]',
  6121. 'value' => $tokenDebug,
  6122. 'autocomplete' => 'off',
  6123. ]],
  6124. '/div',
  6125. '/form',
  6126. ];
  6127. $this->assertHtml($expected, $result);
  6128. }
  6129. /**
  6130. * testPostLink method
  6131. */
  6132. public function testPostLink(): void
  6133. {
  6134. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6135. $expected = [
  6136. 'form' => [
  6137. 'method' => 'post', 'action' => '/posts/delete/1',
  6138. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6139. ],
  6140. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6141. '/form',
  6142. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6143. 'Delete',
  6144. '/a',
  6145. ];
  6146. $this->assertHtml($expected, $result);
  6147. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6148. $expected = [
  6149. 'form' => [
  6150. 'method' => 'post', 'action' => '/posts/delete/1',
  6151. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6152. ],
  6153. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6154. '/form',
  6155. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6156. 'Delete',
  6157. '/a',
  6158. ];
  6159. $this->assertHtml($expected, $result);
  6160. $result = $this->Form->postLink(
  6161. 'Delete',
  6162. '/posts/delete/1',
  6163. ['target' => '_blank', 'class' => 'btn btn-danger']
  6164. );
  6165. $expected = [
  6166. 'form' => [
  6167. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6168. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6169. ],
  6170. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6171. '/form',
  6172. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6173. 'Delete',
  6174. '/a',
  6175. ];
  6176. $this->assertHtml($expected, $result);
  6177. }
  6178. /**
  6179. * testPostLinkWithConfirm method
  6180. *
  6181. * Test the confirm option for postLink().
  6182. */
  6183. public function testPostLinkWithConfirm(): void
  6184. {
  6185. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6186. $expected = [
  6187. 'form' => [
  6188. 'method' => 'post', 'action' => '/posts/delete/1',
  6189. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6190. ],
  6191. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6192. '/form',
  6193. 'a' => [
  6194. 'href' => '#',
  6195. 'data-confirm-message' => 'Confirm?',
  6196. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6197. ],
  6198. 'Delete',
  6199. '/a',
  6200. ];
  6201. $this->assertHtml($expected, $result);
  6202. $result = $this->Form->postLink(
  6203. 'Delete',
  6204. '/posts/delete/1',
  6205. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6206. );
  6207. $expected = [
  6208. 'form' => [
  6209. 'method' => 'post', 'action' => '/posts/delete/1',
  6210. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6211. ],
  6212. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6213. '/form',
  6214. 'a' => [
  6215. 'href' => '#',
  6216. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6217. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6218. ],
  6219. 'Delete',
  6220. '/a',
  6221. ];
  6222. $this->assertHtml($expected, $result);
  6223. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6224. $result = $this->Form->postLink(
  6225. 'Delete',
  6226. '/posts/delete/1',
  6227. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6228. );
  6229. $expected = [
  6230. 'form' => [
  6231. 'method' => 'post', 'action' => '/posts/delete/1',
  6232. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6233. ],
  6234. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6235. '/form',
  6236. 'a' => [
  6237. 'href' => '#',
  6238. 'data-confirm-message' => 'Confirm this deletion?',
  6239. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6240. ],
  6241. 'Delete',
  6242. '/a',
  6243. ];
  6244. $this->assertHtml($expected, $result);
  6245. }
  6246. /**
  6247. * testPostLinkWithQuery method
  6248. *
  6249. * Test postLink() with query string args.
  6250. */
  6251. public function testPostLinkWithQuery(): void
  6252. {
  6253. $result = $this->Form->postLink(
  6254. 'Delete',
  6255. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6256. );
  6257. $expected = [
  6258. 'form' => [
  6259. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6260. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6261. ],
  6262. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6263. '/form',
  6264. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6265. 'Delete',
  6266. '/a',
  6267. ];
  6268. $this->assertHtml($expected, $result);
  6269. }
  6270. /**
  6271. * testPostLinkWithData method
  6272. *
  6273. * Test postLink with additional data.
  6274. */
  6275. public function testPostLinkWithData(): void
  6276. {
  6277. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6278. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6279. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6280. $this->Form->create($entity);
  6281. $this->Form->end();
  6282. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6283. $this->assertStringContainsString(
  6284. '<input type="hidden" name="name" value="show"',
  6285. $result,
  6286. 'should not contain entity data.'
  6287. );
  6288. }
  6289. /**
  6290. * testPostLinkSecurityHash method
  6291. *
  6292. * Test that security hashes for postLink include the url.
  6293. */
  6294. public function testPostLinkSecurityHash(): void
  6295. {
  6296. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6297. $hash .= '%3Aid';
  6298. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6299. $result = $this->Form->postLink(
  6300. 'Delete',
  6301. '/posts/delete/1',
  6302. ['data' => ['id' => 1]]
  6303. );
  6304. $tokenDebug = urlencode(json_encode([
  6305. '/posts/delete/1',
  6306. [
  6307. 'id' => 1,
  6308. ],
  6309. [],
  6310. ]));
  6311. $expected = [
  6312. 'form' => [
  6313. 'method' => 'post', 'action' => '/posts/delete/1',
  6314. 'name', 'style' => 'display:none;',
  6315. ],
  6316. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6317. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6318. 'div' => ['style' => 'display:none;'],
  6319. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6320. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6321. ['input' => [
  6322. 'type' => 'hidden',
  6323. 'name' => '_Token[debug]',
  6324. 'value' => $tokenDebug,
  6325. 'autocomplete' => 'off',
  6326. ]],
  6327. '/div',
  6328. '/form',
  6329. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6330. 'Delete',
  6331. '/a',
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. }
  6335. /**
  6336. * testPostLinkSecurityHashBlockMode method
  6337. *
  6338. * Test that postLink doesn't modify the fields in the containing form.
  6339. *
  6340. * postLink() calls inside open forms should not modify the field list
  6341. * for the form.
  6342. */
  6343. public function testPostLinkSecurityHashBlockMode(): void
  6344. {
  6345. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6346. $hash .= '%3A';
  6347. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6348. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6349. $this->Form->control('title');
  6350. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6351. $result = $this->View->fetch('postLink');
  6352. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6353. $this->assertEquals(['title'], $fields);
  6354. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6355. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6356. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6357. }
  6358. /**
  6359. * testPostLinkSecurityHashNoDebugMode method
  6360. *
  6361. * Test that security does not include debug token if debug is false.
  6362. */
  6363. public function testPostLinkSecurityHashNoDebugMode(): void
  6364. {
  6365. Configure::write('debug', false);
  6366. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6367. $hash .= '%3Aid';
  6368. $this->View->setRequest($this->View->getRequest()
  6369. ->withAttribute('formTokenData', ['key' => 'test']));
  6370. $result = $this->Form->postLink(
  6371. 'Delete',
  6372. '/posts/delete/1',
  6373. ['data' => ['id' => 1]]
  6374. );
  6375. $expected = [
  6376. 'form' => [
  6377. 'method' => 'post', 'action' => '/posts/delete/1',
  6378. 'name', 'style' => 'display:none;',
  6379. ],
  6380. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6381. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6382. 'div' => ['style' => 'display:none;'],
  6383. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6384. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6385. '/div',
  6386. '/form',
  6387. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6388. 'Delete',
  6389. '/a',
  6390. ];
  6391. $this->assertHtml($expected, $result);
  6392. }
  6393. /**
  6394. * testPostLinkNestedData method
  6395. *
  6396. * Test using postLink with N dimensional data.
  6397. */
  6398. public function testPostLinkNestedData(): void
  6399. {
  6400. $data = [
  6401. 'one' => [
  6402. 'two' => [
  6403. 3, 4, 5,
  6404. ],
  6405. ],
  6406. ];
  6407. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6408. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6409. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6410. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6411. }
  6412. /**
  6413. * testPostLinkAfterGetForm method
  6414. *
  6415. * Test creating postLinks after a GET form.
  6416. */
  6417. public function testPostLinkAfterGetForm(): void
  6418. {
  6419. $this->View->setRequest($this->View->getRequest()
  6420. ->withAttribute('csrfToken', 'testkey')
  6421. ->withAttribute('formTokenData', []));
  6422. $this->Form->create($this->article, ['type' => 'get']);
  6423. $this->Form->end();
  6424. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6425. $tokenDebug = urlencode(json_encode([
  6426. '/posts/delete/1',
  6427. [],
  6428. [],
  6429. ]));
  6430. $expected = [
  6431. 'form' => [
  6432. 'method' => 'post', 'action' => '/posts/delete/1',
  6433. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6434. ],
  6435. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6436. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6437. 'div' => ['style' => 'display:none;'],
  6438. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6439. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6440. ['input' => [
  6441. 'type' => 'hidden', 'name' => '_Token[debug]',
  6442. 'value' => $tokenDebug,
  6443. 'autocomplete' => 'off',
  6444. ]],
  6445. '/div',
  6446. '/form',
  6447. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6448. 'Delete',
  6449. '/a',
  6450. ];
  6451. $this->assertHtml($expected, $result);
  6452. }
  6453. /**
  6454. * testPostLinkFormBuffer method
  6455. *
  6456. * Test that postLink adds form tags to view block.
  6457. */
  6458. public function testPostLinkFormBuffer(): void
  6459. {
  6460. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6461. $expected = [
  6462. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6463. 'Delete',
  6464. '/a',
  6465. ];
  6466. $this->assertHtml($expected, $result);
  6467. $result = $this->View->fetch('postLink');
  6468. $expected = [
  6469. 'form' => [
  6470. 'method' => 'post', 'action' => '/posts/delete/1',
  6471. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6472. ],
  6473. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6474. '/form',
  6475. ];
  6476. $this->assertHtml($expected, $result);
  6477. $result = $this->Form->postLink(
  6478. 'Delete',
  6479. '/posts/delete/2',
  6480. ['block' => true, 'method' => 'DELETE']
  6481. );
  6482. $expected = [
  6483. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6484. 'Delete',
  6485. '/a',
  6486. ];
  6487. $this->assertHtml($expected, $result);
  6488. $result = $this->View->fetch('postLink');
  6489. $expected = [
  6490. 'form' => [
  6491. 'method' => 'post', 'action' => '/posts/delete/1',
  6492. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6493. ],
  6494. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6495. '/form',
  6496. [
  6497. 'form' => [
  6498. 'method' => 'post', 'action' => '/posts/delete/2',
  6499. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6500. ],
  6501. ],
  6502. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6503. '/form',
  6504. ];
  6505. $this->assertHtml($expected, $result);
  6506. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6507. $expected = [
  6508. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6509. 'Delete',
  6510. '/a',
  6511. ];
  6512. $this->assertHtml($expected, $result);
  6513. $result = $this->View->fetch('foobar');
  6514. $expected = [
  6515. 'form' => [
  6516. 'method' => 'post', 'action' => '/posts/delete/1',
  6517. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6518. ],
  6519. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6520. '/form',
  6521. ];
  6522. $this->assertHtml($expected, $result);
  6523. }
  6524. /**
  6525. * testSubmitButton method
  6526. */
  6527. public function testSubmitButton(): void
  6528. {
  6529. $result = $this->Form->submit('');
  6530. $expected = [
  6531. 'div' => ['class' => 'submit'],
  6532. 'input' => ['type' => 'submit', 'value' => ''],
  6533. '/div',
  6534. ];
  6535. $this->assertHtml($expected, $result);
  6536. $result = $this->Form->submit('Test Submit');
  6537. $expected = [
  6538. 'div' => ['class' => 'submit'],
  6539. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6540. '/div',
  6541. ];
  6542. $this->assertHtml($expected, $result);
  6543. $result = $this->Form->submit('Next >');
  6544. $expected = [
  6545. 'div' => ['class' => 'submit'],
  6546. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6547. '/div',
  6548. ];
  6549. $this->assertHtml($expected, $result);
  6550. $result = $this->Form->submit('Next >', ['escape' => false]);
  6551. $expected = [
  6552. 'div' => ['class' => 'submit'],
  6553. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6554. '/div',
  6555. ];
  6556. $this->assertHtml($expected, $result);
  6557. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6558. $expected = [
  6559. 'div' => ['class' => 'submit'],
  6560. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6561. '/div',
  6562. ];
  6563. $this->assertHtml($expected, $result);
  6564. }
  6565. /**
  6566. * testSubmitImage method
  6567. *
  6568. * Test image submit types.
  6569. */
  6570. public function testSubmitImage(): void
  6571. {
  6572. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6573. $expected = [
  6574. 'div' => ['class' => 'submit'],
  6575. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6576. '/div',
  6577. ];
  6578. $this->assertHtml($expected, $result);
  6579. $result = $this->Form->submit('/relative/cake.power.gif');
  6580. $expected = [
  6581. 'div' => ['class' => 'submit'],
  6582. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6583. '/div',
  6584. ];
  6585. $this->assertHtml($expected, $result);
  6586. $result = $this->Form->submit('cake.power.gif');
  6587. $expected = [
  6588. 'div' => ['class' => 'submit'],
  6589. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6590. '/div',
  6591. ];
  6592. $this->assertHtml($expected, $result);
  6593. $result = $this->Form->submit('Not.an.image');
  6594. $expected = [
  6595. 'div' => ['class' => 'submit'],
  6596. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6597. '/div',
  6598. ];
  6599. $this->assertHtml($expected, $result);
  6600. }
  6601. /**
  6602. * testSubmitUnlockedByDefault method
  6603. *
  6604. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6605. * be submitted at a time.
  6606. */
  6607. public function testSubmitUnlockedByDefault(): void
  6608. {
  6609. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6610. $this->Form->create();
  6611. $this->Form->submit('Go go');
  6612. $this->Form->submit('Save', ['name' => 'save']);
  6613. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6614. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6615. }
  6616. /**
  6617. * testSubmitImageTimestamp method
  6618. *
  6619. * Test submit image with timestamps.
  6620. */
  6621. public function testSubmitImageTimestamp(): void
  6622. {
  6623. Configure::write('Asset.timestamp', 'force');
  6624. $result = $this->Form->submit('cake.power.gif');
  6625. $expected = [
  6626. 'div' => ['class' => 'submit'],
  6627. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6628. '/div',
  6629. ];
  6630. $this->assertHtml($expected, $result);
  6631. }
  6632. /**
  6633. * testDateTimeWithGetForms method
  6634. *
  6635. * Test that datetime() works with GET style forms.
  6636. */
  6637. public function testDateTimeWithGetForms(): void
  6638. {
  6639. $this->Form->create($this->article, ['type' => 'get']);
  6640. $result = $this->Form->datetime('created');
  6641. $expected = [
  6642. 'input' => [
  6643. 'type' => 'datetime-local',
  6644. 'name' => 'created',
  6645. 'value' => '',
  6646. 'step' => '1',
  6647. ],
  6648. ];
  6649. $this->assertHtml($expected, $result);
  6650. $result = $this->Form->datetime('created', ['default' => true]);
  6651. $expected = [
  6652. 'input' => [
  6653. 'type' => 'datetime-local',
  6654. 'name' => 'created',
  6655. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6656. 'step' => '1',
  6657. ],
  6658. ];
  6659. $this->assertHtml($expected, $result);
  6660. }
  6661. /**
  6662. * Provides fractional schema types
  6663. *
  6664. * @return array
  6665. */
  6666. public function fractionalTypeProvider(): array
  6667. {
  6668. return [
  6669. ['datetimefractional'],
  6670. ['timestampfractional'],
  6671. ['timestamptimezone'],
  6672. ];
  6673. }
  6674. /**
  6675. * testDateTimeWithFractional method
  6676. *
  6677. * Test that datetime() works with datetimefractional.
  6678. *
  6679. * @dataProvider fractionalTypeProvider
  6680. */
  6681. public function testDateTimeWithFractional(string $type): void
  6682. {
  6683. $this->Form->create([
  6684. 'schema' => [
  6685. 'created' => ['type' => $type],
  6686. ],
  6687. ]);
  6688. $result = $this->Form->datetime('created', [
  6689. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6690. ]);
  6691. $expected = [
  6692. 'input' => [
  6693. 'type' => 'datetime-local',
  6694. 'name' => 'created',
  6695. 'value' => '2019-09-27T02:52:43.123',
  6696. 'step' => '0.001',
  6697. ],
  6698. ];
  6699. $this->assertHtml($expected, $result);
  6700. }
  6701. /**
  6702. * testControlWithFractional method
  6703. *
  6704. * Test that control() works with datetimefractional.
  6705. *
  6706. * @dataProvider fractionalTypeProvider
  6707. */
  6708. public function testControlWithFractional(string $type): void
  6709. {
  6710. $this->Form->create([
  6711. 'schema' => [
  6712. 'created' => ['type' => $type],
  6713. ],
  6714. ]);
  6715. $result = $this->Form->control('created', [
  6716. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6717. ]);
  6718. $expected = [
  6719. 'div' => ['class' => 'input datetime'],
  6720. 'label' => ['for' => 'created'],
  6721. 'Created',
  6722. '/label',
  6723. 'input' => [
  6724. 'type' => 'datetime-local',
  6725. 'name' => 'created',
  6726. 'id' => 'created',
  6727. 'value' => '2019-09-27T02:52:43.123',
  6728. 'step' => '0.001',
  6729. ],
  6730. '/div',
  6731. ];
  6732. $this->assertHtml($expected, $result);
  6733. }
  6734. /**
  6735. * testForMagicControlNonExistentNotValidated method
  6736. */
  6737. public function testForMagicControlNonExistentNotValidated(): void
  6738. {
  6739. $this->Form->create($this->article);
  6740. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6741. $result = $this->Form->control('nonexistent_not_validated');
  6742. $expected = [
  6743. 'label' => ['for' => 'nonexistent-not-validated'],
  6744. 'Nonexistent Not Validated',
  6745. '/label',
  6746. 'input' => [
  6747. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6748. 'id' => 'nonexistent-not-validated',
  6749. ],
  6750. ];
  6751. $this->assertHtml($expected, $result);
  6752. $result = $this->Form->control('nonexistent_not_validated', [
  6753. 'val' => 'my value',
  6754. ]);
  6755. $expected = [
  6756. 'label' => ['for' => 'nonexistent-not-validated'],
  6757. 'Nonexistent Not Validated',
  6758. '/label',
  6759. 'input' => [
  6760. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6761. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6762. ],
  6763. ];
  6764. $this->assertHtml($expected, $result);
  6765. $this->View->setRequest(
  6766. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6767. );
  6768. $this->Form->create($this->article);
  6769. $result = $this->Form->control('nonexistent_not_validated');
  6770. $expected = [
  6771. 'label' => ['for' => 'nonexistent-not-validated'],
  6772. 'Nonexistent Not Validated',
  6773. '/label',
  6774. 'input' => [
  6775. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6776. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6777. ],
  6778. ];
  6779. $this->assertHtml($expected, $result);
  6780. }
  6781. /**
  6782. * testFormMagicControlLabel method
  6783. */
  6784. public function testFormMagicControlLabel(): void
  6785. {
  6786. $this->getTableLocator()->get('Contacts', [
  6787. 'className' => ContactsTable::class,
  6788. ]);
  6789. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6790. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6791. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6792. $expected = [
  6793. 'label' => ['for' => 'contacts-name'],
  6794. 'My label',
  6795. '/label',
  6796. 'input' => [
  6797. 'type' => 'text',
  6798. 'name' => 'Contacts[name]',
  6799. 'id' => 'contacts-name',
  6800. 'maxlength' => '255',
  6801. ],
  6802. ];
  6803. $this->assertHtml($expected, $result);
  6804. $result = $this->Form->control('name', [
  6805. 'label' => ['class' => 'mandatory'],
  6806. ]);
  6807. $expected = [
  6808. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6809. 'Name',
  6810. '/label',
  6811. 'input' => [
  6812. 'type' => 'text', 'name' => 'name',
  6813. 'id' => 'name', 'maxlength' => '255',
  6814. ],
  6815. ];
  6816. $this->assertHtml($expected, $result);
  6817. $result = $this->Form->control('name', [
  6818. 'div' => false,
  6819. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6820. ]);
  6821. $expected = [
  6822. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6823. 'My label',
  6824. '/label',
  6825. 'input' => [
  6826. 'type' => 'text', 'name' => 'name',
  6827. 'id' => 'name', 'maxlength' => '255',
  6828. ],
  6829. ];
  6830. $this->assertHtml($expected, $result);
  6831. $result = $this->Form->control('Contact.name', [
  6832. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6833. ]);
  6834. $expected = [
  6835. 'label' => ['for' => 'my_id'],
  6836. 'Name',
  6837. '/label',
  6838. 'input' => [
  6839. 'type' => 'text', 'name' => 'Contact[name]',
  6840. 'id' => 'my_id', 'maxlength' => '255',
  6841. ],
  6842. ];
  6843. $this->assertHtml($expected, $result);
  6844. $result = $this->Form->control('1.id');
  6845. $expected = ['input' => [
  6846. 'type' => 'hidden', 'name' => '1[id]',
  6847. 'id' => '1-id',
  6848. ]];
  6849. $this->assertHtml($expected, $result);
  6850. $result = $this->Form->control('1.name');
  6851. $expected = [
  6852. 'label' => ['for' => '1-name'],
  6853. 'Name',
  6854. '/label',
  6855. 'input' => [
  6856. 'type' => 'text', 'name' => '1[name]',
  6857. 'id' => '1-name', 'maxlength' => '255',
  6858. ],
  6859. ];
  6860. $this->assertHtml($expected, $result);
  6861. }
  6862. /**
  6863. * testFormEnd method
  6864. */
  6865. public function testFormEnd(): void
  6866. {
  6867. $this->assertSame('</form>', $this->Form->end());
  6868. }
  6869. /**
  6870. * testMultiRecordForm method
  6871. *
  6872. * Test the generation of fields for a multi record form.
  6873. */
  6874. public function testMultiRecordForm(): void
  6875. {
  6876. $articles = $this->getTableLocator()->get('Articles');
  6877. $articles->hasMany('Comments');
  6878. $comment = new Entity(['comment' => 'Value']);
  6879. $article = new Article(['comments' => [$comment]]);
  6880. $this->Form->create([$article]);
  6881. $result = $this->Form->control('0.comments.1.comment');
  6882. // phpcs:disable
  6883. $expected = [
  6884. 'div' => ['class' => 'input textarea'],
  6885. 'label' => ['for' => '0-comments-1-comment'],
  6886. 'Comment',
  6887. '/label',
  6888. 'textarea' => [
  6889. 'name',
  6890. 'id' => '0-comments-1-comment',
  6891. 'rows' => 5
  6892. ],
  6893. '/textarea',
  6894. '/div'
  6895. ];
  6896. // phpcs:enable
  6897. $this->assertHtml($expected, $result);
  6898. $result = $this->Form->control('0.comments.0.comment');
  6899. // phpcs:disable
  6900. $expected = [
  6901. 'div' => ['class' => 'input textarea'],
  6902. 'label' => ['for' => '0-comments-0-comment'],
  6903. 'Comment',
  6904. '/label',
  6905. 'textarea' => [
  6906. 'name',
  6907. 'id' => '0-comments-0-comment',
  6908. 'rows' => 5
  6909. ],
  6910. 'Value',
  6911. '/textarea',
  6912. '/div'
  6913. ];
  6914. // phpcs:enable
  6915. $this->assertHtml($expected, $result);
  6916. $comment->setError('comment', ['Not valid']);
  6917. $result = $this->Form->control('0.comments.0.comment');
  6918. // phpcs:disable
  6919. $expected = [
  6920. 'div' => ['class' => 'input textarea error'],
  6921. 'label' => ['for' => '0-comments-0-comment'],
  6922. 'Comment',
  6923. '/label',
  6924. 'textarea' => [
  6925. 'name',
  6926. 'class' => 'form-error',
  6927. 'id' => '0-comments-0-comment',
  6928. 'aria-invalid' => 'true',
  6929. 'aria-describedby' => '0-comments-0-comment-error',
  6930. 'rows' => 5
  6931. ],
  6932. 'Value',
  6933. '/textarea',
  6934. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  6935. 'Not valid',
  6936. '/div',
  6937. '/div'
  6938. ];
  6939. // phpcs:enable
  6940. $this->assertHtml($expected, $result);
  6941. $this->getTableLocator()->get('Comments')
  6942. ->getValidator('default')
  6943. ->allowEmptyString('comment', null, false);
  6944. $result = $this->Form->control('0.comments.1.comment');
  6945. // phpcs:disable
  6946. $expected = [
  6947. 'div' => ['class' => 'input textarea required'],
  6948. 'label' => ['for' => '0-comments-1-comment'],
  6949. 'Comment',
  6950. '/label',
  6951. 'textarea' => [
  6952. 'name',
  6953. 'aria-required' => 'true',
  6954. 'required' => 'required',
  6955. 'id' => '0-comments-1-comment',
  6956. 'rows' => 5,
  6957. 'data-validity-message' => 'This field cannot be left empty',
  6958. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  6959. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  6960. ],
  6961. '/textarea',
  6962. '/div'
  6963. ];
  6964. // phpcs:enable
  6965. $this->assertHtml($expected, $result);
  6966. }
  6967. /**
  6968. * testHtml5Controls method
  6969. *
  6970. * Test that some html5 inputs + FormHelper::__call() work.
  6971. */
  6972. public function testHtml5Controls(): void
  6973. {
  6974. $result = $this->Form->email('User.email');
  6975. $expected = [
  6976. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  6977. ];
  6978. $this->assertHtml($expected, $result);
  6979. $result = $this->Form->search('User.query');
  6980. $expected = [
  6981. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  6982. ];
  6983. $this->assertHtml($expected, $result);
  6984. $result = $this->Form->search('User.query', ['value' => 'test']);
  6985. $expected = [
  6986. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  6987. ];
  6988. $this->assertHtml($expected, $result);
  6989. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  6990. $expected = [
  6991. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  6992. ];
  6993. $this->assertHtml($expected, $result);
  6994. }
  6995. /**
  6996. * testHtml5ControlWithControl method
  6997. *
  6998. * Test accessing html5 inputs through control().
  6999. */
  7000. public function testHtml5ControlWithControl(): void
  7001. {
  7002. $this->Form->create();
  7003. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7004. $result = $this->Form->control('website', [
  7005. 'type' => 'url',
  7006. 'val' => 'http://domain.tld',
  7007. 'label' => false,
  7008. ]);
  7009. $expected = [
  7010. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7011. ];
  7012. $this->assertHtml($expected, $result);
  7013. }
  7014. /**
  7015. * testHtml5ControlException method
  7016. *
  7017. * Test errors when field name is missing.
  7018. */
  7019. public function testHtml5ControlException(): void
  7020. {
  7021. $this->expectException(CakeException::class);
  7022. $this->Form->email();
  7023. }
  7024. /**
  7025. * tests fields that are required use custom validation messages
  7026. */
  7027. public function testHtml5ErrorMessage(): void
  7028. {
  7029. $this->Form->setConfig('autoSetCustomValidity', true);
  7030. $validator = (new Validator())
  7031. ->notEmptyString('email', 'Custom error message')
  7032. ->requirePresence('password')
  7033. ->alphaNumeric('password')
  7034. ->notBlank('phone');
  7035. $table = $this->getTableLocator()->get('Contacts', [
  7036. 'className' => ContactsTable::class,
  7037. ]);
  7038. $table->setValidator('default', $validator);
  7039. $contact = new Entity();
  7040. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7041. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7042. $result = $this->Form->control('password');
  7043. $expected = [
  7044. 'label' => ['for' => 'password'],
  7045. 'Password',
  7046. '/label',
  7047. 'input' => [
  7048. 'aria-required' => 'true',
  7049. 'id' => 'password',
  7050. 'name' => 'password',
  7051. 'type' => 'password',
  7052. 'value' => '',
  7053. 'required' => 'required',
  7054. 'data-validity-message' => 'This field cannot be left empty',
  7055. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7056. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7057. ],
  7058. ];
  7059. $this->assertHtml($expected, $result);
  7060. $result = $this->Form->control('phone');
  7061. $expected = [
  7062. 'label' => ['for' => 'phone'],
  7063. 'Phone',
  7064. '/label',
  7065. 'input' => [
  7066. 'aria-required' => 'true',
  7067. 'id' => 'phone',
  7068. 'name' => 'phone',
  7069. 'type' => 'tel',
  7070. 'value' => '',
  7071. 'maxlength' => 255,
  7072. 'required' => 'required',
  7073. 'data-validity-message' => 'This field cannot be left empty',
  7074. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7075. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7076. ],
  7077. ];
  7078. $this->assertHtml($expected, $result);
  7079. $result = $this->Form->control('email');
  7080. $expected = [
  7081. 'label' => ['for' => 'email'],
  7082. 'Email',
  7083. '/label',
  7084. 'input' => [
  7085. 'aria-required' => 'true',
  7086. 'id' => 'email',
  7087. 'name' => 'email',
  7088. 'type' => 'email',
  7089. 'value' => '',
  7090. 'maxlength' => 255,
  7091. 'required' => 'required',
  7092. 'data-validity-message' => 'Custom error message',
  7093. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7094. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7095. ],
  7096. ];
  7097. $this->assertHtml($expected, $result);
  7098. }
  7099. /**
  7100. * tests that custom validation messages are in templateVars
  7101. */
  7102. public function testHtml5ErrorMessageInTemplateVars(): void
  7103. {
  7104. $validator = (new Validator())
  7105. ->notEmptyString('email', 'Custom error "message" & entities')
  7106. ->requirePresence('password')
  7107. ->alphaNumeric('password')
  7108. ->notBlank('phone');
  7109. $table = $this->getTableLocator()->get('Contacts', [
  7110. 'className' => ContactsTable::class,
  7111. ]);
  7112. $table->setValidator('default', $validator);
  7113. $contact = new Entity();
  7114. $this->Form->setConfig('autoSetCustomValidity', false);
  7115. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7116. $this->Form->setTemplates([
  7117. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7118. 'inputContainer' => '{{content}}',
  7119. ]);
  7120. $result = $this->Form->control('password');
  7121. $expected = [
  7122. 'label' => ['for' => 'password'],
  7123. 'Password',
  7124. '/label',
  7125. 'input' => [
  7126. 'aria-required' => 'true',
  7127. 'id' => 'password',
  7128. 'name' => 'password',
  7129. 'type' => 'password',
  7130. 'value' => '',
  7131. 'required' => 'required',
  7132. 'data-message' => 'This field cannot be left empty',
  7133. ],
  7134. ];
  7135. $this->assertHtml($expected, $result);
  7136. $result = $this->Form->control('phone');
  7137. $expected = [
  7138. 'label' => ['for' => 'phone'],
  7139. 'Phone',
  7140. '/label',
  7141. 'input' => [
  7142. 'aria-required' => 'true',
  7143. 'id' => 'phone',
  7144. 'name' => 'phone',
  7145. 'type' => 'tel',
  7146. 'value' => '',
  7147. 'maxlength' => 255,
  7148. 'required' => 'required',
  7149. 'data-message' => 'This field cannot be left empty',
  7150. ],
  7151. ];
  7152. $this->assertHtml($expected, $result);
  7153. $result = $this->Form->control('email', [
  7154. 'templateVars' => [
  7155. 'custom' => 'data-custom="1"',
  7156. ],
  7157. ]);
  7158. $expected = [
  7159. 'label' => ['for' => 'email'],
  7160. 'Email',
  7161. '/label',
  7162. 'input' => [
  7163. 'aria-required' => 'true',
  7164. 'id' => 'email',
  7165. 'name' => 'email',
  7166. 'type' => 'email',
  7167. 'value' => '',
  7168. 'maxlength' => 255,
  7169. 'required' => 'required',
  7170. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7171. 'data-custom' => '1',
  7172. ],
  7173. ];
  7174. $this->assertHtml($expected, $result);
  7175. $this->Form->setConfig('autoSetCustomValidity', true);
  7176. }
  7177. /**
  7178. * testRequiredAttribute method
  7179. *
  7180. * Tests that formhelper sets required attributes.
  7181. */
  7182. public function testRequiredAttribute(): void
  7183. {
  7184. $this->article['required'] = [
  7185. 'title' => true,
  7186. 'body' => false,
  7187. ];
  7188. $this->Form->create($this->article);
  7189. $result = $this->Form->control('title');
  7190. $expected = [
  7191. 'div' => ['class' => 'input text required'],
  7192. 'label' => ['for' => 'title'],
  7193. 'Title',
  7194. '/label',
  7195. 'input' => [
  7196. 'type' => 'text',
  7197. 'name' => 'title',
  7198. 'id' => 'title',
  7199. 'aria-required' => 'true',
  7200. 'required' => 'required',
  7201. 'data-validity-message' => 'This field cannot be left empty',
  7202. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7203. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7204. ],
  7205. '/div',
  7206. ];
  7207. $this->assertHtml($expected, $result);
  7208. $result = $this->Form->control('title', ['required' => false]);
  7209. $this->assertStringNotContainsString('required', $result);
  7210. $result = $this->Form->control('body');
  7211. $expected = [
  7212. 'div' => ['class' => 'input text'],
  7213. 'label' => ['for' => 'body'],
  7214. 'Body',
  7215. '/label',
  7216. 'input' => [
  7217. 'type' => 'text',
  7218. 'name' => 'body',
  7219. 'id' => 'body',
  7220. ],
  7221. '/div',
  7222. ];
  7223. $this->assertHtml($expected, $result);
  7224. $result = $this->Form->control('body', ['required' => true]);
  7225. $this->assertStringContainsString('required', $result);
  7226. }
  7227. /**
  7228. * testControlsNotNested method
  7229. *
  7230. * Tests that it is possible to put inputs outside of the label.
  7231. */
  7232. public function testControlsNotNested(): void
  7233. {
  7234. $this->Form->setTemplates([
  7235. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7236. 'formGroup' => '{{input}}{{label}}',
  7237. ]);
  7238. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7239. $expected = [
  7240. 'div' => ['class' => 'input checkbox'],
  7241. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7242. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7243. 'label' => ['for' => 'foo'],
  7244. 'Foo',
  7245. '/label',
  7246. '/div',
  7247. ];
  7248. $this->assertHtml($expected, $result);
  7249. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7250. $expected = [
  7251. 'div' => ['class' => 'input checkbox'],
  7252. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7253. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7254. '/div',
  7255. ];
  7256. $this->assertHtml($expected, $result);
  7257. $result = $this->Form->control('confirm', [
  7258. 'type' => 'radio',
  7259. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7260. ]);
  7261. $expected = [
  7262. 'div' => ['class' => 'input radio'],
  7263. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7264. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7265. ['label' => ['for' => 'confirm-y']],
  7266. 'Yes',
  7267. '/label',
  7268. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7269. ['label' => ['for' => 'confirm-n']],
  7270. 'No',
  7271. '/label',
  7272. '<label',
  7273. 'Confirm',
  7274. '/label',
  7275. '/div',
  7276. ];
  7277. $this->assertHtml($expected, $result);
  7278. $result = $this->Form->select('category', ['1', '2'], [
  7279. 'multiple' => 'checkbox',
  7280. 'name' => 'fish',
  7281. ]);
  7282. $expected = [
  7283. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7284. ['div' => ['class' => 'checkbox']],
  7285. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7286. ['label' => ['for' => 'fish-0']],
  7287. '1',
  7288. '/label',
  7289. '/div',
  7290. ['div' => ['class' => 'checkbox']],
  7291. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7292. ['label' => ['for' => 'fish-1']],
  7293. '2',
  7294. '/label',
  7295. '/div',
  7296. ];
  7297. $this->assertHtml($expected, $result);
  7298. }
  7299. /**
  7300. * testControlContainerTemplates method
  7301. *
  7302. * Test that *Container templates are used by input.
  7303. */
  7304. public function testControlContainerTemplates(): void
  7305. {
  7306. $this->Form->setTemplates([
  7307. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7308. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7309. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7310. ]);
  7311. $this->article['errors'] = [
  7312. 'Article' => ['published' => 'error message'],
  7313. ];
  7314. $this->Form->create($this->article);
  7315. $result = $this->Form->control('accept', [
  7316. 'type' => 'checkbox',
  7317. ]);
  7318. $expected = [
  7319. 'div' => ['class' => 'check'],
  7320. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7321. 'label' => ['for' => 'accept'],
  7322. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7323. 'Accept',
  7324. '/label',
  7325. '/div',
  7326. ];
  7327. $this->assertHtml($expected, $result);
  7328. $result = $this->Form->control('accept', [
  7329. 'type' => 'radio',
  7330. 'options' => ['Y', 'N'],
  7331. ]);
  7332. $this->assertStringContainsString('<div class="rad">', $result);
  7333. $result = $this->Form->control('Article.published', [
  7334. 'type' => 'radio',
  7335. 'options' => ['Y', 'N'],
  7336. ]);
  7337. $this->assertStringContainsString('<div class="rad err">', $result);
  7338. }
  7339. /**
  7340. * testFormGroupTemplates method
  7341. *
  7342. * Test that *Container templates are used by input.
  7343. */
  7344. public function testFormGroupTemplates(): void
  7345. {
  7346. $this->Form->setTemplates([
  7347. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7348. ]);
  7349. $this->Form->create($this->article);
  7350. $result = $this->Form->control('accept', [
  7351. 'type' => 'radio',
  7352. 'options' => ['Y', 'N'],
  7353. ]);
  7354. $this->assertStringContainsString('<div class="radio">', $result);
  7355. }
  7356. /**
  7357. * testResetTemplates method
  7358. *
  7359. * Test resetting templates.
  7360. */
  7361. public function testResetTemplates(): void
  7362. {
  7363. $this->Form->setTemplates(['input' => '<input/>']);
  7364. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7365. $this->Form->resetTemplates();
  7366. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7367. }
  7368. /**
  7369. * testContext method
  7370. *
  7371. * Test the context method.
  7372. */
  7373. public function testContext(): void
  7374. {
  7375. $result = $this->Form->context();
  7376. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7377. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7378. $this->assertSame($mock, $this->Form->context($mock));
  7379. $this->assertSame($mock, $this->Form->context());
  7380. }
  7381. /**
  7382. * testAutoDomId method
  7383. */
  7384. public function testAutoDomId(): void
  7385. {
  7386. $result = $this->Form->text('field', ['id' => true]);
  7387. $expected = [
  7388. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7389. ];
  7390. $this->assertHtml($expected, $result);
  7391. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7392. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7393. $expected = [
  7394. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7395. ['label' => ['for' => 'field-0']],
  7396. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7397. 'option A',
  7398. '/label',
  7399. ['label' => ['for' => 'field-1']],
  7400. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7401. 'option B',
  7402. '/label',
  7403. ];
  7404. $this->assertHtml($expected, $result);
  7405. $result = $this->Form->select(
  7406. 'multi_field',
  7407. ['first', 'second'],
  7408. ['multiple' => 'checkbox', 'id' => true]
  7409. );
  7410. $expected = [
  7411. 'input' => [
  7412. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7413. ],
  7414. ['div' => ['class' => 'checkbox']],
  7415. ['label' => ['for' => 'multi-field-0']],
  7416. ['input' => [
  7417. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7418. 'value' => '0', 'id' => 'multi-field-0',
  7419. ]],
  7420. 'first',
  7421. '/label',
  7422. '/div',
  7423. ['div' => ['class' => 'checkbox']],
  7424. ['label' => ['for' => 'multi-field-1']],
  7425. ['input' => [
  7426. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7427. 'value' => '1', 'id' => 'multi-field-1',
  7428. ]],
  7429. 'second',
  7430. '/label',
  7431. '/div',
  7432. ];
  7433. $this->assertHtml($expected, $result);
  7434. }
  7435. /**
  7436. * Test the basic setters and getters for value sources
  7437. */
  7438. public function testFormValueSourcesSettersGetters(): void
  7439. {
  7440. $this->View->setRequest($this->View->getRequest()
  7441. ->withData('id', '1')
  7442. ->withQueryParams(['id' => '2']));
  7443. $expected = ['data', 'context'];
  7444. $result = $this->Form->getValueSources();
  7445. $this->assertEquals($expected, $result);
  7446. $this->Form->setValueSources(['context']);
  7447. $result = $this->Form->getSourceValue('id');
  7448. $this->assertNull($result);
  7449. $this->Form->setValueSources('query');
  7450. $expected = ['query'];
  7451. $result = $this->Form->getValueSources();
  7452. $this->assertEquals($expected, $result);
  7453. $expected = '2';
  7454. $result = $this->Form->getSourceValue('id');
  7455. $this->assertSame($expected, $result);
  7456. $this->Form->setValueSources(['data']);
  7457. $expected = '1';
  7458. $result = $this->Form->getSourceValue('id');
  7459. $this->assertSame($expected, $result);
  7460. $this->Form->setValueSources(['query', 'data']);
  7461. $expected = '2';
  7462. $result = $this->Form->getSourceValue('id');
  7463. $this->assertSame($expected, $result);
  7464. }
  7465. public function testValueSourcesValidation(): void
  7466. {
  7467. $this->expectException(InvalidArgumentException::class);
  7468. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7469. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7470. }
  7471. /**
  7472. * Tests the different input rendering values based on sources values switching
  7473. */
  7474. public function testFormValueSourcesSingleSwitchRendering(): void
  7475. {
  7476. $articles = $this->getTableLocator()->get('Articles');
  7477. $article = new Article();
  7478. $articles->patchEntity($article, ['id' => '3']);
  7479. $this->Form->create($article);
  7480. $this->Form->setValueSources(['context']);
  7481. $result = $this->Form->control('id');
  7482. $expected = [
  7483. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7484. ];
  7485. $this->assertHtml($expected, $result);
  7486. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7487. $this->Form->setValueSources(['query']);
  7488. $result = $this->Form->control('id');
  7489. $expected = [
  7490. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7491. ];
  7492. $this->assertHtml($expected, $result);
  7493. $this->View->setRequest($this->View->getRequest()
  7494. ->withQueryParams(['id' => '5a'])
  7495. ->withData('id', '5b'));
  7496. $this->Form->setValueSources(['context']);
  7497. $this->Form->create($article);
  7498. $result = $this->Form->control('id');
  7499. $expected = [
  7500. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7501. ];
  7502. $this->assertHtml($expected, $result);
  7503. $this->Form->setValueSources(['data']);
  7504. $this->Form->create($article);
  7505. $result = $this->Form->control('id');
  7506. $expected = [
  7507. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7508. ];
  7509. $this->assertHtml($expected, $result);
  7510. $this->Form->setValueSources(['query']);
  7511. $result = $this->Form->control('id');
  7512. $expected = [
  7513. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7514. ];
  7515. $this->assertHtml($expected, $result);
  7516. }
  7517. /**
  7518. * Tests the different input rendering values based on sources values switching while supplying
  7519. * an entity (base context) and multiple sources (such as data, query)
  7520. */
  7521. public function testFormValueSourcesListSwitchRendering(): void
  7522. {
  7523. $articles = $this->getTableLocator()->get('Articles');
  7524. $article = new Article();
  7525. $articles->patchEntity($article, ['id' => '3']);
  7526. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7527. $this->Form->create($article);
  7528. $this->Form->setValueSources(['context', 'query']);
  7529. $result = $this->Form->control('id');
  7530. $expected = [
  7531. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7532. ];
  7533. $this->assertHtml($expected, $result);
  7534. $this->Form->setValueSources(['query', 'context']);
  7535. $result = $this->Form->control('id');
  7536. $expected = [
  7537. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7538. ];
  7539. $this->assertHtml($expected, $result);
  7540. $this->Form->setValueSources(['data', 'query', 'context']);
  7541. $result = $this->Form->control('id');
  7542. $expected = [
  7543. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7544. ];
  7545. $this->assertHtml($expected, $result);
  7546. $this->View->setRequest($this->View->getRequest()
  7547. ->withData('id', '8')
  7548. ->withQueryParams(['id' => '9']));
  7549. $this->Form->setValueSources(['data', 'query', 'context']);
  7550. $result = $this->Form->control('id');
  7551. $expected = [
  7552. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7553. ];
  7554. $this->assertHtml($expected, $result);
  7555. }
  7556. /**
  7557. * Test the different form input renderings based on values sources switchings through form options
  7558. */
  7559. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7560. {
  7561. $articles = $this->getTableLocator()->get('Articles');
  7562. $article = new Article();
  7563. $articles->patchEntity($article, ['id' => '3']);
  7564. $this->View->setRequest(
  7565. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7566. );
  7567. $this->Form->create($article, ['valueSources' => 'query']);
  7568. $result = $this->Form->control('id');
  7569. $expected = [
  7570. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7571. ];
  7572. $this->assertHtml($expected, $result);
  7573. $result = $this->Form->getSourceValue('id');
  7574. $this->assertSame('5', $result);
  7575. $this->Form->setValueSources(['context']);
  7576. $this->Form->create($article, ['valueSources' => 'query']);
  7577. $result = $this->Form->control('id');
  7578. $expected = [
  7579. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7580. ];
  7581. $this->assertHtml($expected, $result);
  7582. $result = $this->Form->getSourceValue('id');
  7583. $this->assertSame('5', $result);
  7584. $this->Form->setValueSources(['query']);
  7585. $this->Form->create($article, ['valueSources' => 'data']);
  7586. $result = $this->Form->control('id');
  7587. $expected = [
  7588. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7589. ];
  7590. $this->assertHtml($expected, $result);
  7591. $result = $this->Form->getSourceValue('id');
  7592. $this->assertSame('4', $result);
  7593. $this->Form->setValueSources(['query']);
  7594. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7595. $result = $this->Form->control('id');
  7596. $expected = [
  7597. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7598. ];
  7599. $this->assertHtml($expected, $result);
  7600. $result = $this->Form->getSourceValue('id');
  7601. $this->assertSame(3, $result);
  7602. }
  7603. /**
  7604. * Test the different form input renderings based on values sources switchings through form options
  7605. */
  7606. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7607. {
  7608. $articles = $this->getTableLocator()->get('Articles');
  7609. $article = new Article();
  7610. $articles->patchEntity($article, ['id' => '3']);
  7611. $this->View->setRequest(
  7612. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7613. );
  7614. $this->Form->setValueSources(['context'])
  7615. ->create($article, ['valueSources' => ['query', 'data']]);
  7616. $result = $this->Form->control('id');
  7617. $expected = [
  7618. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7619. ];
  7620. $this->assertHtml($expected, $result);
  7621. $result = $this->Form->getSourceValue('id');
  7622. $this->assertSame('11', $result);
  7623. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7624. $this->Form->setValueSources(['context'])
  7625. ->create($article, ['valueSources' => ['query', 'data']]);
  7626. $result = $this->Form->control('id');
  7627. $expected = [
  7628. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7629. ];
  7630. $this->assertHtml($expected, $result);
  7631. $result = $this->Form->getSourceValue('id');
  7632. $this->assertSame('10', $result);
  7633. }
  7634. /**
  7635. * Test the different form values sources resetting through From::end();
  7636. */
  7637. public function testFormValueSourcesResetViaEnd(): void
  7638. {
  7639. $expected = ['data', 'context'];
  7640. $result = $this->Form->getValueSources();
  7641. $this->assertEquals($expected, $result);
  7642. $expected = ['query', 'context', 'data'];
  7643. $this->Form->setValueSources(['query', 'context', 'data']);
  7644. $result = $this->Form->getValueSources();
  7645. $this->assertEquals($expected, $result);
  7646. $this->Form->create();
  7647. $result = $this->Form->getValueSources();
  7648. $this->assertEquals($expected, $result);
  7649. $this->Form->end();
  7650. $result = $this->Form->getValueSources();
  7651. $this->assertEquals(['data', 'context'], $result);
  7652. }
  7653. /**
  7654. * Test sources values defaults handling
  7655. */
  7656. public function testFormValueSourcesDefaults(): void
  7657. {
  7658. $this->View->setRequest(
  7659. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7660. );
  7661. $this->Form->create();
  7662. $result = $this->Form->password('password');
  7663. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7664. $this->assertHtml($expected, $result);
  7665. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7666. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7667. $this->assertHtml($expected, $result);
  7668. $this->Form->setValueSources('query');
  7669. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7670. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7671. $this->assertHtml($expected, $result);
  7672. $this->Form->setValueSources('data');
  7673. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7674. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7675. $this->assertHtml($expected, $result);
  7676. }
  7677. /**
  7678. * Test sources values schema defaults handling
  7679. */
  7680. public function testSourcesValueDoesntExistPassThrough(): void
  7681. {
  7682. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7683. $articles = $this->getTableLocator()->get('Articles');
  7684. $entity = $articles->newEmptyEntity();
  7685. $this->Form->create($entity);
  7686. $this->Form->setValueSources(['query', 'context']);
  7687. $result = $this->Form->getSourceValue('category');
  7688. $this->assertSame('sesame-cookies', $result);
  7689. $this->Form->setValueSources(['context', 'query']);
  7690. $result = $this->Form->getSourceValue('category');
  7691. $this->assertSame('sesame-cookies', $result);
  7692. }
  7693. /**
  7694. * testNestedLabelInput method
  7695. *
  7696. * Test the `nestedInput` parameter
  7697. */
  7698. public function testNestedLabelInput(): void
  7699. {
  7700. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7701. $expected = [
  7702. 'div' => ['class' => 'input text'],
  7703. 'label' => ['for' => 'foo'],
  7704. ['input' => [
  7705. 'type' => 'text',
  7706. 'name' => 'foo',
  7707. 'id' => 'foo',
  7708. ]],
  7709. 'Foo',
  7710. '/label',
  7711. '/div',
  7712. ];
  7713. $this->assertHtml($expected, $result);
  7714. }
  7715. /**
  7716. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7717. *
  7718. * This test makes sure `false` excludes the label from the render
  7719. */
  7720. public function testControlLabelManipulationDisableLabels(): void
  7721. {
  7722. $result = $this->Form->control('test', [
  7723. 'type' => 'radio',
  7724. 'options' => ['A', 'B'],
  7725. 'labelOptions' => false,
  7726. ]);
  7727. $expected = [
  7728. ['div' => ['class' => 'input radio']],
  7729. '<label',
  7730. 'Test',
  7731. '/label',
  7732. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7733. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7734. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7735. '/div',
  7736. ];
  7737. $this->assertHtml($expected, $result);
  7738. $result = $this->Form->control('checkbox1', [
  7739. 'label' => 'My checkboxes',
  7740. 'multiple' => 'checkbox',
  7741. 'type' => 'select',
  7742. 'options' => [
  7743. ['text' => 'First Checkbox', 'value' => 1],
  7744. ['text' => 'Second Checkbox', 'value' => 2],
  7745. ],
  7746. 'labelOptions' => false,
  7747. ]);
  7748. $expected = [
  7749. ['div' => ['class' => 'input select']],
  7750. ['label' => ['for' => 'checkbox1']],
  7751. 'My checkboxes',
  7752. '/label',
  7753. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7754. ['div' => ['class' => 'checkbox']],
  7755. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7756. '/div',
  7757. ['div' => ['class' => 'checkbox']],
  7758. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7759. '/div',
  7760. '/div',
  7761. ];
  7762. $this->assertHtml($expected, $result);
  7763. }
  7764. /**
  7765. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7766. *
  7767. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7768. * added to the class if checked.
  7769. *
  7770. * Also checks to make sure any custom attributes are rendered correctly
  7771. */
  7772. public function testControlLabelManipulationRadios(): void
  7773. {
  7774. $result = $this->Form->control('test', [
  7775. 'type' => 'radio',
  7776. 'options' => ['A', 'B'],
  7777. 'labelOptions' => ['class' => 'custom-class'],
  7778. ]);
  7779. $expected = [
  7780. ['div' => ['class' => 'input radio']],
  7781. '<label',
  7782. 'Test',
  7783. '/label',
  7784. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7785. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7786. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7787. 'A',
  7788. '/label',
  7789. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7790. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7791. 'B',
  7792. '/label',
  7793. '/div',
  7794. ];
  7795. $this->assertHtml($expected, $result);
  7796. $result = $this->Form->control('test', [
  7797. 'type' => 'radio',
  7798. 'options' => ['A', 'B'],
  7799. 'value' => 1,
  7800. 'labelOptions' => ['class' => 'custom-class'],
  7801. ]);
  7802. $expected = [
  7803. ['div' => ['class' => 'input radio']],
  7804. '<label',
  7805. 'Test',
  7806. '/label',
  7807. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7808. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7809. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7810. 'A',
  7811. '/label',
  7812. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7813. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7814. 'B',
  7815. '/label',
  7816. '/div',
  7817. ];
  7818. $this->assertHtml($expected, $result);
  7819. $result = $this->Form->control('test', [
  7820. 'type' => 'radio',
  7821. 'options' => ['A', 'B'],
  7822. 'value' => 1,
  7823. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7824. ]);
  7825. $expected = [
  7826. ['div' => ['class' => 'input radio']],
  7827. '<label',
  7828. 'Test',
  7829. '/label',
  7830. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7831. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7832. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7833. 'A',
  7834. '/label',
  7835. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7836. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7837. 'B',
  7838. '/label',
  7839. '/div',
  7840. ];
  7841. $this->assertHtml($expected, $result);
  7842. $result = $this->Form->radio('test', ['A', 'B'], [
  7843. 'label' => [
  7844. 'class' => ['custom-class', 'another-class'],
  7845. 'data-name' => 'bob',
  7846. ],
  7847. 'value' => 1,
  7848. ]);
  7849. $expected = [
  7850. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  7851. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7852. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7853. 'A',
  7854. '/label',
  7855. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7856. ['input' => [
  7857. 'type' => 'radio',
  7858. 'name' => 'test',
  7859. 'value' => '1',
  7860. 'id' => 'test-1',
  7861. 'checked' => 'checked',
  7862. ]],
  7863. 'B',
  7864. '/label',
  7865. ];
  7866. $this->assertHtml($expected, $result);
  7867. }
  7868. /**
  7869. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7870. *
  7871. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7872. * added to the class if checked.
  7873. *
  7874. * Also checks to make sure any custom attributes are rendered correctly
  7875. */
  7876. public function testControlLabelManipulationCheckboxes(): void
  7877. {
  7878. $result = $this->Form->control('checkbox1', [
  7879. 'label' => 'My checkboxes',
  7880. 'multiple' => 'checkbox',
  7881. 'type' => 'select',
  7882. 'options' => [
  7883. ['text' => 'First Checkbox', 'value' => 1],
  7884. ['text' => 'Second Checkbox', 'value' => 2],
  7885. ],
  7886. 'labelOptions' => ['class' => 'custom-class'],
  7887. 'value' => ['1'],
  7888. ]);
  7889. $expected = [
  7890. ['div' => ['class' => 'input select']],
  7891. ['label' => ['for' => 'checkbox1']],
  7892. 'My checkboxes',
  7893. '/label',
  7894. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7895. ['div' => ['class' => 'checkbox']],
  7896. ['label' => [
  7897. 'class' => 'custom-class selected',
  7898. 'for' => 'checkbox1-1',
  7899. ]],
  7900. ['input' => [
  7901. 'type' => 'checkbox',
  7902. 'name' => 'checkbox1[]',
  7903. 'value' => '1',
  7904. 'id' => 'checkbox1-1',
  7905. 'checked' => 'checked',
  7906. ]],
  7907. 'First Checkbox',
  7908. '/label',
  7909. '/div',
  7910. ['div' => ['class' => 'checkbox']],
  7911. ['label' => [
  7912. 'class' => 'custom-class',
  7913. 'for' => 'checkbox1-2',
  7914. ]],
  7915. ['input' => [
  7916. 'type' => 'checkbox',
  7917. 'name' => 'checkbox1[]',
  7918. 'value' => '2',
  7919. 'id' => 'checkbox1-2',
  7920. ]],
  7921. 'Second Checkbox',
  7922. '/label',
  7923. '/div',
  7924. '/div',
  7925. ];
  7926. $this->assertHtml($expected, $result);
  7927. $result = $this->Form->control('checkbox1', [
  7928. 'label' => 'My checkboxes',
  7929. 'multiple' => 'checkbox',
  7930. 'type' => 'select',
  7931. 'options' => [
  7932. ['text' => 'First Checkbox', 'value' => 1],
  7933. ['text' => 'Second Checkbox', 'value' => 2],
  7934. ],
  7935. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  7936. 'value' => ['1'],
  7937. ]);
  7938. $expected = [
  7939. ['div' => ['class' => 'input select']],
  7940. ['label' => ['for' => 'checkbox1']],
  7941. 'My checkboxes',
  7942. '/label',
  7943. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7944. ['div' => ['class' => 'checkbox']],
  7945. ['label' => [
  7946. 'class' => 'custom-class another-class selected',
  7947. 'data-name' => 'bob',
  7948. 'for' => 'checkbox1-1',
  7949. ]],
  7950. ['input' => [
  7951. 'type' => 'checkbox',
  7952. 'name' => 'checkbox1[]',
  7953. 'value' => '1',
  7954. 'id' => 'checkbox1-1',
  7955. 'checked' => 'checked',
  7956. ]],
  7957. 'First Checkbox',
  7958. '/label',
  7959. '/div',
  7960. ['div' => ['class' => 'checkbox']],
  7961. ['label' => [
  7962. 'class' => 'custom-class another-class',
  7963. 'data-name' => 'bob',
  7964. 'for' => 'checkbox1-2',
  7965. ]],
  7966. ['input' => [
  7967. 'type' => 'checkbox',
  7968. 'name' => 'checkbox1[]',
  7969. 'value' => '2',
  7970. 'id' => 'checkbox1-2',
  7971. ]],
  7972. 'Second Checkbox',
  7973. '/label',
  7974. '/div',
  7975. '/div',
  7976. ];
  7977. $this->assertHtml($expected, $result);
  7978. }
  7979. /**
  7980. * testControlMaxLengthArrayContext method
  7981. *
  7982. * Test control() with maxlength attribute in Array Context.
  7983. */
  7984. public function testControlMaxLengthArrayContext(): void
  7985. {
  7986. $this->article['schema'] = [
  7987. 'title' => ['length' => 10],
  7988. ];
  7989. $this->Form->create($this->article);
  7990. $result = $this->Form->control('title');
  7991. $expected = [
  7992. 'div' => ['class'],
  7993. 'label' => ['for'],
  7994. 'Title',
  7995. '/label',
  7996. 'input' => [
  7997. 'aria-required' => 'true',
  7998. 'id',
  7999. 'name' => 'title',
  8000. 'type' => 'text',
  8001. 'required' => 'required',
  8002. 'maxlength' => 10,
  8003. 'data-validity-message' => 'This field cannot be left empty',
  8004. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8005. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8006. ],
  8007. '/div',
  8008. ];
  8009. $this->assertHtml($expected, $result);
  8010. }
  8011. /**
  8012. * testControlMaxLengthEntityContext method
  8013. *
  8014. * Test control() with maxlength attribute in Entity Context.
  8015. */
  8016. public function testControlMaxLengthEntityContext(): void
  8017. {
  8018. $this->article['schema']['title']['length'] = 45;
  8019. $validator = new Validator();
  8020. $validator->maxLength('title', 10);
  8021. $article = new EntityContext(
  8022. [
  8023. 'entity' => new Entity($this->article),
  8024. 'table' => new Table([
  8025. 'alias' => 'Articles',
  8026. 'schema' => $this->article['schema'],
  8027. 'validator' => $validator,
  8028. ]),
  8029. ]
  8030. );
  8031. $this->Form->create($article);
  8032. $result = $this->Form->control('title');
  8033. $expected = [
  8034. 'div' => ['class'],
  8035. 'label' => ['for'],
  8036. 'Title',
  8037. '/label',
  8038. 'input' => [
  8039. 'aria-required' => 'true',
  8040. 'id',
  8041. 'name' => 'title',
  8042. 'type' => 'text',
  8043. 'required' => 'required',
  8044. 'maxlength' => 10,
  8045. 'data-validity-message' => 'This field cannot be left empty',
  8046. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8047. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8048. ],
  8049. '/div',
  8050. ];
  8051. $this->assertHtml($expected, $result);
  8052. $this->article['schema']['title']['length'] = 45;
  8053. $validator = new Validator();
  8054. $validator->maxLength('title', 55);
  8055. $article = new EntityContext(
  8056. [
  8057. 'entity' => new Entity($this->article),
  8058. 'table' => new Table([
  8059. 'schema' => $this->article['schema'],
  8060. 'validator' => $validator,
  8061. 'alias' => 'Articles',
  8062. ]),
  8063. ]
  8064. );
  8065. $this->Form->create($article);
  8066. $result = $this->Form->control('title');
  8067. $expected = [
  8068. 'div' => ['class'],
  8069. 'label' => ['for'],
  8070. 'Title',
  8071. '/label',
  8072. 'input' => [
  8073. 'aria-required' => 'true',
  8074. 'id',
  8075. 'name' => 'title',
  8076. 'type' => 'text',
  8077. 'required' => 'required',
  8078. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8079. 'data-validity-message' => 'This field cannot be left empty',
  8080. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8081. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8082. ],
  8083. '/div',
  8084. ];
  8085. $this->assertHtml($expected, $result);
  8086. $this->article['schema']['title']['length'] = 45;
  8087. $validator = new Validator();
  8088. $validator->maxLength('title', 55);
  8089. $article = new EntityContext(
  8090. [
  8091. 'entity' => new Entity($this->article),
  8092. 'table' => new Table([
  8093. 'schema' => $this->article['schema'],
  8094. 'validator' => $validator,
  8095. 'alias' => 'Articles',
  8096. ]),
  8097. ]
  8098. );
  8099. $this->Form->create($article);
  8100. $result = $this->Form->control('title', ['maxlength' => 10]);
  8101. $expected = [
  8102. 'div' => ['class'],
  8103. 'label' => ['for'],
  8104. 'Title',
  8105. '/label',
  8106. 'input' => [
  8107. 'aria-required' => 'true',
  8108. 'id',
  8109. 'name' => 'title',
  8110. 'type' => 'text',
  8111. 'required' => 'required',
  8112. 'maxlength' => 10, // Length set in options should take highest precedence.
  8113. 'data-validity-message' => 'This field cannot be left empty',
  8114. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8115. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8116. ],
  8117. '/div',
  8118. ];
  8119. $this->assertHtml($expected, $result);
  8120. }
  8121. /**
  8122. * testControlMinMaxLengthEntityContext method
  8123. *
  8124. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8125. */
  8126. public function testControlMinMaxLengthEntityContext(): void
  8127. {
  8128. $validator = new Validator();
  8129. $validator->maxLength('title', 10);
  8130. $article = new EntityContext(
  8131. [
  8132. 'entity' => new Entity($this->article),
  8133. 'table' => new Table([
  8134. 'alias' => 'Articles',
  8135. 'schema' => $this->article['schema'],
  8136. 'validator' => $validator,
  8137. ]),
  8138. ]
  8139. );
  8140. $this->Form->create($article);
  8141. $result = $this->Form->control('title');
  8142. $expected = [
  8143. 'div' => ['class'],
  8144. 'label' => ['for'],
  8145. 'Title',
  8146. '/label',
  8147. 'input' => [
  8148. 'aria-required' => 'true',
  8149. 'id',
  8150. 'name' => 'title',
  8151. 'type' => 'text',
  8152. 'required' => 'required',
  8153. 'maxlength' => 10,
  8154. 'data-validity-message' => 'This field cannot be left empty',
  8155. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8156. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8157. ],
  8158. '/div',
  8159. ];
  8160. $this->assertHtml($expected, $result);
  8161. }
  8162. /**
  8163. * testControlMaxLengthFormContext method
  8164. *
  8165. * Test control() with maxlength attribute in Form Context.
  8166. */
  8167. public function testControlMaxLengthFormContext(): void
  8168. {
  8169. $validator = new Validator();
  8170. $validator->maxLength('title', 10);
  8171. $form = new Form();
  8172. $form->setValidator('default', $validator);
  8173. $this->Form->create($form);
  8174. $result = $this->Form->control('title');
  8175. $expected = [
  8176. 'div' => ['class'],
  8177. 'label' => ['for'],
  8178. 'Title',
  8179. '/label',
  8180. 'input' => [
  8181. 'aria-required' => 'true',
  8182. 'id',
  8183. 'name' => 'title',
  8184. 'type' => 'text',
  8185. 'required' => 'required',
  8186. 'maxlength' => 10,
  8187. 'data-validity-message' => 'This field cannot be left empty',
  8188. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8189. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8190. ],
  8191. '/div',
  8192. ];
  8193. $this->assertHtml($expected, $result);
  8194. }
  8195. }