FormHelperTest.php 306 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926
  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\FrozenDate;
  24. use Cake\I18n\FrozenTime;
  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 RuntimeException;
  38. use TestApp\Model\Entity\Article;
  39. use TestApp\Model\Table\ContactsTable;
  40. use TestApp\Model\Table\ValidateUsersTable;
  41. /**
  42. * FormHelperTest class
  43. *
  44. * @property \Cake\View\Helper\FormHelper $Form
  45. * @property \Cake\View\View $View
  46. */
  47. class FormHelperTest extends TestCase
  48. {
  49. /**
  50. * Fixtures to be used
  51. *
  52. * @var array<string>
  53. */
  54. protected $fixtures = ['core.Articles', 'core.Comments'];
  55. /**
  56. * @var array
  57. */
  58. protected $article = [];
  59. /**
  60. * @var string
  61. */
  62. protected $url;
  63. /**
  64. * @var \Cake\View\Helper\FormHelper
  65. */
  66. protected $Form;
  67. /**
  68. * setUp method
  69. */
  70. public function setUp(): void
  71. {
  72. parent::setUp();
  73. Configure::write('Config.language', 'eng');
  74. Configure::write('App.base', '');
  75. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  76. $request = new ServerRequest([
  77. 'webroot' => '',
  78. 'base' => '',
  79. 'url' => '/articles/add',
  80. 'params' => [
  81. 'controller' => 'Articles',
  82. 'action' => 'add',
  83. 'plugin' => null,
  84. ],
  85. ]);
  86. $this->View = new View($request);
  87. Router::reload();
  88. Router::setRequest($request);
  89. $this->url = '/articles/add';
  90. $this->Form = new FormHelper($this->View);
  91. $this->article = [
  92. 'schema' => [
  93. 'id' => ['type' => 'integer'],
  94. 'author_id' => ['type' => 'integer', 'null' => true],
  95. 'title' => ['type' => 'string', 'null' => true],
  96. 'body' => 'text',
  97. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  98. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  99. ],
  100. 'required' => [
  101. 'author_id' => true,
  102. 'title' => true,
  103. ],
  104. ];
  105. Security::setSalt('foo!');
  106. $builder = Router::createRouteBuilder('/');
  107. $builder->connect('/{controller}', ['action' => 'index']);
  108. $builder->connect('/{controller}/{action}/*');
  109. }
  110. /**
  111. * tearDown method
  112. */
  113. public function tearDown(): void
  114. {
  115. parent::tearDown();
  116. unset($this->Form, $this->Controller, $this->View);
  117. }
  118. /**
  119. * Test construct() with the templates option.
  120. */
  121. public function testConstructTemplatesFile(): void
  122. {
  123. $helper = new FormHelper($this->View, [
  124. 'templates' => 'htmlhelper_tags',
  125. ]);
  126. $result = $helper->control('name');
  127. $this->assertStringContainsString('<input', $result);
  128. }
  129. /**
  130. * Test that when specifying custom widgets the config array for that widget
  131. * is overwritten instead of merged.
  132. */
  133. public function testConstructWithWidgets(): void
  134. {
  135. $config = [
  136. 'widgets' => [
  137. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  138. ],
  139. ];
  140. $helper = new FormHelper($this->View, $config);
  141. $locator = $helper->getWidgetLocator();
  142. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  143. }
  144. /**
  145. * Test that when specifying custom widgets config file and it should be
  146. * added to widgets array. WidgetLocator will load widgets in constructor.
  147. */
  148. public function testConstructWithWidgetsConfig(): void
  149. {
  150. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  151. $locator = $helper->getWidgetLocator();
  152. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  153. }
  154. /**
  155. * Test setting the widget locator
  156. */
  157. public function testSetAndGetWidgetLocator(): void
  158. {
  159. $helper = new FormHelper($this->View);
  160. $locator = new WidgetLocator($helper->templater(), $this->View);
  161. $helper->setWidgetLocator($locator);
  162. $this->assertSame($locator, $helper->getWidgetLocator());
  163. }
  164. /**
  165. * Test overridding grouped input types which controls generation of "for"
  166. * attribute of labels.
  167. */
  168. public function testConstructWithGroupedInputTypes(): void
  169. {
  170. $helper = new FormHelper($this->View, [
  171. 'groupedInputTypes' => ['radio'],
  172. ]);
  173. $result = $helper->control('when', ['type' => 'datetime-local']);
  174. $this->assertStringContainsString('<label for="when">When</label>', $result);
  175. }
  176. /**
  177. * Test registering a new widget class and rendering it.
  178. */
  179. public function testAddWidgetAndRenderWidget(): void
  180. {
  181. $data = [
  182. 'val' => 1,
  183. ];
  184. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  185. $this->Form->addWidget('test', $mock);
  186. $mock->expects($this->once())
  187. ->method('render')
  188. ->with($data)
  189. ->will($this->returnValue('HTML'));
  190. $result = $this->Form->widget('test', $data);
  191. $this->assertSame('HTML', $result);
  192. }
  193. /**
  194. * Test that secureFields() of widget is called after calling render(),
  195. * not before.
  196. */
  197. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  198. {
  199. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  200. 'unlockedFields' => [],
  201. ]));
  202. $data = [
  203. 'val' => 1,
  204. 'name' => 'test',
  205. ];
  206. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  207. $this->Form->addWidget('test', $mock);
  208. $mock->expects($this->once())
  209. ->method('render')
  210. ->with($data)
  211. ->will($this->returnValue('HTML'));
  212. $mock->expects($this->once())
  213. ->method('secureFields')
  214. ->with($data)
  215. ->will($this->returnValue(['test']));
  216. $this->Form->create();
  217. $result = $this->Form->widget('test', $data + ['secure' => true]);
  218. $this->assertSame('HTML', $result);
  219. }
  220. /**
  221. * Test that empty string is not added to secure fields list when
  222. * rendering input widget without name.
  223. */
  224. public function testRenderingWidgetWithEmptyName(): void
  225. {
  226. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  227. $this->Form->create();
  228. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  229. $this->assertSame('<select name=""></select>', $result);
  230. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  231. $this->assertEquals([], $result);
  232. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  233. $this->assertSame('<select name="0"></select>', $result);
  234. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  235. $this->assertEquals(['0'], $result);
  236. }
  237. /**
  238. * Test registering an invalid widget class.
  239. */
  240. public function testAddWidgetInvalid(): void
  241. {
  242. $this->expectException(\RuntimeException::class);
  243. $mock = new \stdClass();
  244. $this->Form->addWidget('test', $mock);
  245. $this->Form->widget('test');
  246. }
  247. /**
  248. * Test adding a new context class.
  249. */
  250. public function testAddContextProvider(): void
  251. {
  252. $context = 'My data';
  253. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  254. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  255. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  256. $this->assertSame($context, $data['entity']);
  257. return $stub;
  258. });
  259. $this->Form->create($context);
  260. $result = $this->Form->context();
  261. $this->assertSame($stub, $result);
  262. }
  263. /**
  264. * Test replacing a context class.
  265. */
  266. public function testAddContextProviderReplace(): void
  267. {
  268. $entity = new Article();
  269. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  270. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  271. return $stub;
  272. });
  273. $this->Form->create($entity);
  274. $result = $this->Form->context();
  275. $this->assertSame($stub, $result);
  276. }
  277. /**
  278. * Test overriding a context class.
  279. */
  280. public function testAddContextProviderAdd(): void
  281. {
  282. $entity = new Article();
  283. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  284. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  285. if ($data['entity'] instanceof Entity) {
  286. return $stub;
  287. }
  288. });
  289. $this->Form->create($entity);
  290. $result = $this->Form->context();
  291. $this->assertSame($stub, $result);
  292. }
  293. /**
  294. * Provides context options for create().
  295. *
  296. * @return array
  297. */
  298. public function contextSelectionProvider(): array
  299. {
  300. $entity = new Article();
  301. $collection = new Collection([$entity]);
  302. $emptyCollection = new Collection([]);
  303. $arrayObject = new ArrayObject([]);
  304. $data = [
  305. 'schema' => [
  306. 'title' => ['type' => 'string'],
  307. ],
  308. ];
  309. $form = new Form();
  310. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  311. return [
  312. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  313. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  314. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  315. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  316. 'form' => [$form, 'Cake\View\Form\FormContext'],
  317. 'none' => [null, 'Cake\View\Form\NullContext'],
  318. 'custom' => [$custom, get_class($custom)],
  319. ];
  320. }
  321. /**
  322. * Test default context selection in create()
  323. *
  324. * @dataProvider contextSelectionProvider
  325. * @param mixed $data
  326. */
  327. public function testCreateContextSelectionBuiltIn($data, string $class): void
  328. {
  329. $this->Form->create($data);
  330. $this->assertInstanceOf($class, $this->Form->context());
  331. }
  332. /**
  333. * Data provider for type option.
  334. *
  335. * @return array
  336. */
  337. public static function requestTypeProvider(): array
  338. {
  339. return [
  340. // type, method, override
  341. ['post', 'post', 'POST'],
  342. ['put', 'post', 'PUT'],
  343. ['patch', 'post', 'PATCH'],
  344. ['delete', 'post', 'DELETE'],
  345. ];
  346. }
  347. /**
  348. * Test creating file forms.
  349. */
  350. public function testCreateFile(): void
  351. {
  352. $encoding = strtolower(Configure::read('App.encoding'));
  353. $result = $this->Form->create(null, ['type' => 'file']);
  354. $expected = [
  355. 'form' => [
  356. 'method' => 'post', 'action' => '/articles/add',
  357. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  358. ],
  359. 'div' => ['style' => 'display:none;'],
  360. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  361. '/div',
  362. ];
  363. $this->assertHtml($expected, $result);
  364. }
  365. /**
  366. * Test creating GET forms.
  367. */
  368. public function testCreateGet(): void
  369. {
  370. $encoding = strtolower(Configure::read('App.encoding'));
  371. $result = $this->Form->create(null, ['type' => 'get']);
  372. $expected = ['form' => [
  373. 'method' => 'get', 'action' => '/articles/add',
  374. 'accept-charset' => $encoding,
  375. ]];
  376. $this->assertHtml($expected, $result);
  377. }
  378. /**
  379. * Test explicit method/enctype options.
  380. *
  381. * Explicit method overwrites inferred method from 'type'
  382. */
  383. public function testCreateExplicitMethodEnctype(): void
  384. {
  385. $encoding = strtolower(Configure::read('App.encoding'));
  386. $result = $this->Form->create(null, [
  387. 'type' => 'get',
  388. 'method' => 'put',
  389. 'enctype' => 'multipart/form-data',
  390. ]);
  391. $expected = ['form' => [
  392. 'method' => 'put',
  393. 'action' => '/articles/add',
  394. 'enctype' => 'multipart/form-data',
  395. 'accept-charset' => $encoding,
  396. ]];
  397. $this->assertHtml($expected, $result);
  398. }
  399. /**
  400. * Test create() with the templates option.
  401. */
  402. public function testCreateTemplatesArray(): void
  403. {
  404. $result = $this->Form->create($this->article, [
  405. 'templates' => [
  406. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  407. ],
  408. ]);
  409. $expected = [
  410. 'form' => [
  411. 'class' => 'form-horizontal',
  412. 'method' => 'post',
  413. 'action' => '/articles/add',
  414. 'accept-charset' => 'utf-8',
  415. ],
  416. ];
  417. $this->assertHtml($expected, $result);
  418. }
  419. /**
  420. * Test create() with the templates option.
  421. */
  422. public function testCreateTemplatesFile(): void
  423. {
  424. $result = $this->Form->create($this->article, [
  425. 'templates' => 'htmlhelper_tags',
  426. ]);
  427. $expected = [
  428. 'start form',
  429. ];
  430. $this->assertHtml($expected, $result);
  431. }
  432. /**
  433. * Test that create() and end() restore templates.
  434. */
  435. public function testCreateEndRestoreTemplates(): void
  436. {
  437. $this->Form->create($this->article, [
  438. 'templates' => ['input' => 'custom input element'],
  439. ]);
  440. $this->Form->end();
  441. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  442. }
  443. /**
  444. * Test using template vars in various templates used by control() method.
  445. */
  446. public function testControlTemplateVars(): void
  447. {
  448. $result = $this->Form->control('text', [
  449. 'templates' => [
  450. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  451. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  452. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  453. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  454. ],
  455. 'templateVars' => [
  456. 'forinput' => 'in-input',
  457. 'forlabel' => 'in-label',
  458. 'forgroup' => 'in-group',
  459. 'forcontainer' => 'in-container',
  460. ],
  461. ]);
  462. $expected = [
  463. 'div' => ['class'],
  464. 'label' => ['for'],
  465. 'Text in-label',
  466. '/label',
  467. 'in-group',
  468. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  469. 'in-container',
  470. '/div',
  471. ];
  472. $this->assertHtml($expected, $result);
  473. }
  474. /**
  475. * Test ensuring template variables work in template files loaded
  476. * during control().
  477. */
  478. public function testControlTemplatesFromFile(): void
  479. {
  480. $result = $this->Form->control('title', [
  481. 'templates' => 'test_templates',
  482. 'templateVars' => [
  483. 'forcontainer' => 'container-data',
  484. ],
  485. ]);
  486. $expected = [
  487. 'div' => ['class'],
  488. 'label' => ['for'],
  489. 'Title',
  490. '/label',
  491. 'input' => ['name', 'type' => 'text', 'id'],
  492. 'container-data',
  493. '/div',
  494. ];
  495. $this->assertHtml($expected, $result);
  496. }
  497. /**
  498. * Test using template vars in inputSubmit and submitContainer template.
  499. */
  500. public function testSubmitTemplateVars(): void
  501. {
  502. $this->Form->setTemplates([
  503. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  504. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  505. ]);
  506. $result = $this->Form->submit('Submit', [
  507. 'templateVars' => [
  508. 'forinput' => 'in-input',
  509. 'forcontainer' => 'in-container',
  510. ],
  511. ]);
  512. $expected = [
  513. 'div' => ['class'],
  514. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  515. 'in-container',
  516. '/div',
  517. ];
  518. $this->assertHtml($expected, $result);
  519. }
  520. /**
  521. * test the create() method
  522. *
  523. * @dataProvider requestTypeProvider
  524. */
  525. public function testCreateTypeOptions(string $type, string $method, string $override): void
  526. {
  527. $encoding = strtolower(Configure::read('App.encoding'));
  528. $result = $this->Form->create(null, ['type' => $type]);
  529. $expected = [
  530. 'form' => [
  531. 'method' => $method, 'action' => '/articles/add',
  532. 'accept-charset' => $encoding,
  533. ],
  534. ];
  535. $extra = [
  536. 'div' => ['style' => 'display:none;'],
  537. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  538. '/div',
  539. ];
  540. if ($type !== 'post') {
  541. $expected = array_merge($expected, $extra);
  542. }
  543. $this->assertHtml($expected, $result);
  544. }
  545. /**
  546. * Test using template vars in Create (formStart template)
  547. */
  548. public function testCreateTemplateVars(): void
  549. {
  550. $result = $this->Form->create($this->article, [
  551. 'templates' => [
  552. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  553. ],
  554. 'templateVars' => ['header' => 'headertext'],
  555. ]);
  556. $expected = [
  557. 'h4' => ['class'],
  558. 'headertext',
  559. '/h4',
  560. 'form' => [
  561. 'method' => 'post',
  562. 'action' => '/articles/add',
  563. 'accept-charset' => 'utf-8',
  564. ],
  565. ];
  566. $this->assertHtml($expected, $result);
  567. }
  568. /**
  569. * Test opening a form for an update operation.
  570. */
  571. public function testCreateUpdateForm(): void
  572. {
  573. $encoding = strtolower(Configure::read('App.encoding'));
  574. $this->View->setRequest($this->View->getRequest()
  575. ->withRequestTarget('/articles/edit/1')
  576. ->withParam('action', 'edit'));
  577. $this->article['defaults']['id'] = 1;
  578. $result = $this->Form->create($this->article);
  579. $expected = [
  580. 'form' => [
  581. 'method' => 'post',
  582. 'action' => '/articles/edit/1',
  583. 'accept-charset' => $encoding,
  584. ],
  585. 'div' => ['style' => 'display:none;'],
  586. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  587. '/div',
  588. ];
  589. $this->assertHtml($expected, $result);
  590. }
  591. /**
  592. * test create() with automatic url generation
  593. */
  594. public function testCreateAutoUrl(): void
  595. {
  596. $encoding = strtolower(Configure::read('App.encoding'));
  597. $this->View->setRequest($this->View->getRequest()
  598. ->withRequestTarget('/articles/delete/10')
  599. ->withParam('action', 'delete'));
  600. $result = $this->Form->create($this->article);
  601. $expected = [
  602. 'form' => [
  603. 'method' => 'post', 'action' => '/articles/delete/10',
  604. 'accept-charset' => $encoding,
  605. ],
  606. ];
  607. $this->assertHtml($expected, $result);
  608. $this->article['defaults'] = ['id' => 1];
  609. $this->View->setRequest($this->View->getRequest()
  610. ->withRequestTarget('/Articles/edit/1')
  611. ->withParam('action', 'delete'));
  612. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  613. $expected = [
  614. 'form' => [
  615. 'method' => 'post',
  616. 'action' => '/Articles/edit/1',
  617. 'accept-charset' => $encoding,
  618. ],
  619. 'div' => ['style' => 'display:none;'],
  620. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  621. '/div',
  622. ];
  623. $this->assertHtml($expected, $result);
  624. $this->View->setRequest($this->View->getRequest()
  625. ->withParam('action', 'add'));
  626. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  627. $expected = [
  628. 'form' => [
  629. 'method' => 'post',
  630. 'action' => '/Articles/publish/1',
  631. 'accept-charset' => $encoding,
  632. ],
  633. 'div' => ['style' => 'display:none;'],
  634. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  635. '/div',
  636. ];
  637. $this->assertHtml($expected, $result);
  638. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  639. $expected = [
  640. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  641. 'div' => ['style' => 'display:none;'],
  642. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  643. '/div',
  644. ];
  645. $this->assertHtml($expected, $result);
  646. $this->View->setRequest($this->View->getRequest()
  647. ->withParam('controller', 'Pages'));
  648. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  649. $expected = [
  650. 'form' => [
  651. 'method' => 'post', 'action' => '/Pages/signup/1',
  652. 'accept-charset' => $encoding,
  653. ],
  654. 'div' => ['style' => 'display:none;'],
  655. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  656. '/div',
  657. ];
  658. $this->assertHtml($expected, $result);
  659. }
  660. /**
  661. * Test create() with no URL (no "action" attribute for <form> tag)
  662. */
  663. public function testCreateNoUrl(): void
  664. {
  665. $result = $this->Form->create(null, ['url' => false]);
  666. $expected = [
  667. 'form' => [
  668. 'method' => 'post',
  669. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  670. ],
  671. ];
  672. $this->assertHtml($expected, $result);
  673. }
  674. /**
  675. * test create() with a custom route
  676. */
  677. public function testCreateCustomRoute(): void
  678. {
  679. $builder = Router::createRouteBuilder('/');
  680. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  681. $encoding = strtolower(Configure::read('App.encoding'));
  682. $this->View->setRequest($this->View->getRequest()
  683. ->withParam('controller', 'Users'));
  684. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  685. $expected = [
  686. 'form' => [
  687. 'method' => 'post', 'action' => '/login',
  688. 'accept-charset' => $encoding,
  689. ],
  690. ];
  691. $this->assertHtml($expected, $result);
  692. $builder->connect(
  693. '/new-article',
  694. ['controller' => 'Articles', 'action' => 'myAction'],
  695. ['_name' => 'my-route']
  696. );
  697. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  698. $expected = [
  699. 'form' => [
  700. 'method' => 'post', 'action' => '/new-article',
  701. 'accept-charset' => $encoding,
  702. ],
  703. ];
  704. $this->assertHtml($expected, $result);
  705. }
  706. /**
  707. * test automatic accept-charset overriding
  708. */
  709. public function testCreateWithAcceptCharset(): void
  710. {
  711. $result = $this->Form->create(
  712. $this->article,
  713. [
  714. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  715. ]
  716. );
  717. $expected = [
  718. 'form' => [
  719. 'method' => 'post', 'action' => '/Articles',
  720. 'accept-charset' => 'iso-8859-1',
  721. ],
  722. ];
  723. $this->assertHtml($expected, $result);
  724. }
  725. /**
  726. * Test base form URL when 'url' param is passed with multiple parameters (&)
  727. */
  728. public function testCreateQueryStringRequest(): void
  729. {
  730. $encoding = strtolower(Configure::read('App.encoding'));
  731. $result = $this->Form->create($this->article, [
  732. 'type' => 'post',
  733. 'escape' => false,
  734. 'url' => [
  735. 'controller' => 'Controller',
  736. 'action' => 'action',
  737. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  738. ],
  739. ]);
  740. $expected = [
  741. 'form' => [
  742. 'method' => 'post',
  743. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  744. 'accept-charset' => $encoding,
  745. ],
  746. ];
  747. $this->assertHtml($expected, $result);
  748. $result = $this->Form->create($this->article, [
  749. 'type' => 'post',
  750. 'url' => [
  751. 'controller' => 'Controller',
  752. 'action' => 'action',
  753. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  754. ],
  755. ]);
  756. $this->assertHtml($expected, $result);
  757. }
  758. /**
  759. * test that create() doesn't cause errors by multiple id's being in the primary key
  760. * as could happen with multiple select or checkboxes.
  761. */
  762. public function testCreateWithMultipleIdInData(): void
  763. {
  764. $encoding = strtolower(Configure::read('App.encoding'));
  765. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  766. $result = $this->Form->create($this->article);
  767. $expected = [
  768. 'form' => [
  769. 'method' => 'post',
  770. 'action' => '/articles/add',
  771. 'accept-charset' => $encoding,
  772. ],
  773. ];
  774. $this->assertHtml($expected, $result);
  775. }
  776. /**
  777. * test that create() doesn't add in extra passed params.
  778. */
  779. public function testCreatePassedArgs(): void
  780. {
  781. $encoding = strtolower(Configure::read('App.encoding'));
  782. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  783. $result = $this->Form->create($this->article, [
  784. 'type' => 'post',
  785. 'escape' => false,
  786. 'url' => [
  787. 'action' => 'edit',
  788. 'myparam',
  789. ],
  790. ]);
  791. $expected = [
  792. 'form' => [
  793. 'method' => 'post',
  794. 'action' => '/Articles/edit/myparam',
  795. 'accept-charset' => $encoding,
  796. ],
  797. ];
  798. $this->assertHtml($expected, $result);
  799. }
  800. /**
  801. * test creating a get form, and get form inputs.
  802. */
  803. public function testGetFormCreate(): void
  804. {
  805. $encoding = strtolower(Configure::read('App.encoding'));
  806. $result = $this->Form->create($this->article, ['type' => 'get']);
  807. $expected = ['form' => [
  808. 'method' => 'get', 'action' => '/articles/add',
  809. 'accept-charset' => $encoding,
  810. ]];
  811. $this->assertHtml($expected, $result);
  812. $result = $this->Form->text('title');
  813. $expected = ['input' => [
  814. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  815. ]];
  816. $this->assertHtml($expected, $result);
  817. $result = $this->Form->password('password');
  818. $expected = ['input' => [
  819. 'name' => 'password', 'type' => 'password',
  820. ]];
  821. $this->assertHtml($expected, $result);
  822. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  823. $result = $this->Form->text('user_form');
  824. $expected = ['input' => [
  825. 'name' => 'user_form', 'type' => 'text',
  826. ]];
  827. $this->assertHtml($expected, $result);
  828. }
  829. /**
  830. * test get form, and inputs when the model param is false
  831. */
  832. public function testGetFormWithFalseModel(): void
  833. {
  834. $encoding = strtolower(Configure::read('App.encoding'));
  835. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  836. $result = $this->Form->create(null, [
  837. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  838. ]);
  839. $expected = ['form' => [
  840. 'method' => 'get', 'action' => '/ContactTest/add',
  841. 'accept-charset' => $encoding,
  842. ]];
  843. $this->assertHtml($expected, $result);
  844. $result = $this->Form->text('reason');
  845. $expected = [
  846. 'input' => ['type' => 'text', 'name' => 'reason'],
  847. ];
  848. $this->assertHtml($expected, $result);
  849. }
  850. /**
  851. * testFormCreateWithSecurity method
  852. *
  853. * Test form->create() with security key.
  854. */
  855. public function testCreateWithSecurity(): void
  856. {
  857. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  858. $encoding = strtolower(Configure::read('App.encoding'));
  859. $result = $this->Form->create($this->article, [
  860. 'url' => '/articles/publish',
  861. ]);
  862. $expected = [
  863. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  864. 'div' => ['style' => 'display:none;'],
  865. ['input' => [
  866. 'type' => 'hidden',
  867. 'name' => '_csrfToken',
  868. 'value' => 'testKey',
  869. 'autocomplete' => 'off',
  870. ]],
  871. '/div',
  872. ];
  873. $this->assertHtml($expected, $result);
  874. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  875. $expected['form']['id'] = 'MyForm';
  876. $this->assertHtml($expected, $result);
  877. }
  878. /**
  879. * testFormCreateGetNoSecurity method
  880. *
  881. * Test form->create() with no security key as its a get form
  882. */
  883. public function testCreateEndGetNoSecurity(): void
  884. {
  885. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  886. $article = new Article();
  887. $result = $this->Form->create($article, [
  888. 'type' => 'get',
  889. 'url' => '/contacts/add',
  890. ]);
  891. $this->assertStringNotContainsString('testKey', $result);
  892. $result = $this->Form->end();
  893. $this->assertStringNotContainsString('testKey', $result);
  894. }
  895. /**
  896. * Tests form hash generation with model-less data
  897. */
  898. public function testValidateHashNoModel(): void
  899. {
  900. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  901. $fields = ['anything'];
  902. $this->Form->create();
  903. $result = $this->Form->secure($fields);
  904. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  905. $this->assertStringContainsString($hash, $result);
  906. }
  907. /**
  908. * Tests that hidden fields generated for checkboxes don't get locked
  909. */
  910. public function testNoCheckboxLocking(): void
  911. {
  912. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  913. $this->Form->create();
  914. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  915. $this->Form->checkbox('check', ['value' => '1']);
  916. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  917. }
  918. /**
  919. * testFormSecurityFields method
  920. *
  921. * Test generation of secure form hash generation.
  922. */
  923. public function testFormSecurityFields(): void
  924. {
  925. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  926. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  927. $this->Form->create();
  928. $result = $this->Form->secure($fields);
  929. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  930. $hash .= ':' . 'Model.valid';
  931. $hash = urlencode($hash);
  932. $tokenDebug = urlencode(json_encode([
  933. $this->url,
  934. $fields,
  935. [],
  936. ]));
  937. $expected = [
  938. 'div' => ['style' => 'display:none;'],
  939. ['input' => [
  940. 'type' => 'hidden',
  941. 'name' => '_Token[fields]',
  942. 'value' => $hash,
  943. 'autocomplete' => 'off',
  944. ]],
  945. ['input' => [
  946. 'type' => 'hidden',
  947. 'name' => '_Token[unlocked]',
  948. 'value' => '',
  949. 'autocomplete' => 'off',
  950. ]],
  951. ['input' => [
  952. 'type' => 'hidden',
  953. 'name' => '_Token[debug]',
  954. 'value' => $tokenDebug,
  955. 'autocomplete' => 'off',
  956. ]],
  957. '/div',
  958. ];
  959. $this->assertHtml($expected, $result);
  960. }
  961. /**
  962. * testFormSecurityFields method
  963. *
  964. * Test debug token is not generated if debug is false
  965. */
  966. public function testFormSecurityFieldsNoDebugMode(): void
  967. {
  968. Configure::write('debug', false);
  969. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  970. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  971. $this->Form->create();
  972. $result = $this->Form->secure($fields);
  973. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  974. $hash .= ':' . 'Model.valid';
  975. $hash = urlencode($hash);
  976. $expected = [
  977. 'div' => ['style' => 'display:none;'],
  978. ['input' => [
  979. 'type' => 'hidden',
  980. 'name' => '_Token[fields]',
  981. 'autocomplete' => 'off',
  982. 'value' => $hash,
  983. ]],
  984. ['input' => [
  985. 'type' => 'hidden',
  986. 'name' => '_Token[unlocked]',
  987. 'autocomplete' => 'off',
  988. 'value' => '',
  989. ]],
  990. '/div',
  991. ];
  992. $this->assertHtml($expected, $result);
  993. }
  994. /**
  995. * Tests correct generation of number fields for smallint
  996. */
  997. public function testTextFieldGenerationForSmallint(): void
  998. {
  999. $this->article['schema'] = [
  1000. 'foo' => [
  1001. 'type' => 'smallinteger',
  1002. 'null' => false,
  1003. 'default' => null,
  1004. 'length' => 10,
  1005. ],
  1006. ];
  1007. $this->Form->create($this->article);
  1008. $result = $this->Form->control('foo');
  1009. $this->assertStringContainsString('class="input number"', $result);
  1010. $this->assertStringContainsString('type="number"', $result);
  1011. }
  1012. /**
  1013. * Tests correct generation of number fields for tinyint
  1014. */
  1015. public function testTextFieldGenerationForTinyint(): void
  1016. {
  1017. $this->article['schema'] = [
  1018. 'foo' => [
  1019. 'type' => 'tinyinteger',
  1020. 'null' => false,
  1021. 'default' => null,
  1022. 'length' => 10,
  1023. ],
  1024. ];
  1025. $this->Form->create($this->article);
  1026. $result = $this->Form->control('foo');
  1027. $this->assertStringContainsString('class="input number"', $result);
  1028. $this->assertStringContainsString('type="number"', $result);
  1029. }
  1030. /**
  1031. * Tests correct generation of number fields for double and float fields
  1032. */
  1033. public function testTextFieldGenerationForFloats(): void
  1034. {
  1035. $this->article['schema'] = [
  1036. 'foo' => [
  1037. 'type' => 'float',
  1038. 'null' => false,
  1039. 'default' => null,
  1040. 'length' => 10,
  1041. ],
  1042. ];
  1043. $this->Form->create($this->article);
  1044. $result = $this->Form->control('foo');
  1045. $expected = [
  1046. 'div' => ['class' => 'input number'],
  1047. 'label' => ['for' => 'foo'],
  1048. 'Foo',
  1049. '/label',
  1050. ['input' => [
  1051. 'type' => 'number',
  1052. 'name' => 'foo',
  1053. 'id' => 'foo',
  1054. 'step' => 'any',
  1055. ]],
  1056. '/div',
  1057. ];
  1058. $this->assertHtml($expected, $result);
  1059. $result = $this->Form->control('foo', ['step' => 0.5]);
  1060. $expected = [
  1061. 'div' => ['class' => 'input number'],
  1062. 'label' => ['for' => 'foo'],
  1063. 'Foo',
  1064. '/label',
  1065. ['input' => [
  1066. 'type' => 'number',
  1067. 'name' => 'foo',
  1068. 'id' => 'foo',
  1069. 'step' => '0.5',
  1070. ]],
  1071. '/div',
  1072. ];
  1073. $this->assertHtml($expected, $result);
  1074. }
  1075. /**
  1076. * Tests correct generation of number fields for integer fields
  1077. */
  1078. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1079. {
  1080. $this->getTableLocator()->get('Contacts', [
  1081. 'className' => ContactsTable::class,
  1082. ]);
  1083. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1084. $result = $this->Form->control('age');
  1085. $expected = [
  1086. 'div' => ['class' => 'input number'],
  1087. 'label' => ['for' => 'age'],
  1088. 'Age',
  1089. '/label',
  1090. ['input' => [
  1091. 'type' => 'number', 'name' => 'age',
  1092. 'id' => 'age',
  1093. ]],
  1094. '/div',
  1095. ];
  1096. $this->assertHtml($expected, $result);
  1097. }
  1098. /**
  1099. * Tests correct generation of file upload fields for binary fields
  1100. */
  1101. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1102. {
  1103. $table = $this->getTableLocator()->get('Contacts', [
  1104. 'className' => ContactsTable::class,
  1105. ]);
  1106. $table->setSchema(['foo' => [
  1107. 'type' => 'binary',
  1108. 'null' => false,
  1109. 'default' => null,
  1110. 'length' => 1024,
  1111. ]]);
  1112. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1113. $result = $this->Form->control('foo');
  1114. $expected = [
  1115. 'div' => ['class' => 'input file'],
  1116. 'label' => ['for' => 'foo'],
  1117. 'Foo',
  1118. '/label',
  1119. ['input' => [
  1120. 'type' => 'file', 'name' => 'foo',
  1121. 'id' => 'foo',
  1122. ]],
  1123. '/div',
  1124. ];
  1125. $this->assertHtml($expected, $result);
  1126. }
  1127. /**
  1128. * testFormSecurityMultipleFields method
  1129. *
  1130. * Test secure() with multiple row form. Ensure hash is correct.
  1131. */
  1132. public function testFormSecurityMultipleFields(): void
  1133. {
  1134. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1135. $this->Form->create();
  1136. $fields = [
  1137. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1138. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1139. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1140. ];
  1141. $result = $this->Form->secure($fields);
  1142. $sortedFields = [
  1143. 'Model.0.password',
  1144. 'Model.0.username',
  1145. 'Model.1.password',
  1146. 'Model.1.username',
  1147. 'Model.0.hidden' => 'value',
  1148. 'Model.0.valid' => '0',
  1149. 'Model.1.hidden' => 'value',
  1150. 'Model.1.valid' => '0',
  1151. ];
  1152. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1153. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1154. $hash = urlencode($hash);
  1155. $tokenDebug = urlencode(json_encode([
  1156. $this->url,
  1157. $fields,
  1158. [],
  1159. ]));
  1160. $expected = [
  1161. 'div' => ['style' => 'display:none;'],
  1162. ['input' => [
  1163. 'type' => 'hidden',
  1164. 'name' => '_Token[fields]',
  1165. 'value' => $hash,
  1166. 'autocomplete' => 'off',
  1167. ]],
  1168. ['input' => [
  1169. 'type' => 'hidden',
  1170. 'name' => '_Token[unlocked]',
  1171. 'autocomplete' => 'off',
  1172. 'value' => '',
  1173. ]],
  1174. ['input' => [
  1175. 'type' => 'hidden',
  1176. 'name' => '_Token[debug]',
  1177. 'value' => $tokenDebug,
  1178. 'autocomplete' => 'off',
  1179. ]],
  1180. '/div',
  1181. ];
  1182. $this->assertHtml($expected, $result);
  1183. }
  1184. /**
  1185. * testFormSecurityMultipleSubmitButtons
  1186. *
  1187. * test form submit generation and ensure that _Token is only created on end()
  1188. */
  1189. public function testFormSecurityMultipleSubmitButtons(): void
  1190. {
  1191. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1192. $this->Form->create($this->article);
  1193. $this->Form->text('Address.title');
  1194. $this->Form->text('Address.first_name');
  1195. $result = $this->Form->submit('Save', ['name' => 'save']);
  1196. $expected = [
  1197. 'div' => ['class' => 'submit'],
  1198. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1199. '/div',
  1200. ];
  1201. $this->assertHtml($expected, $result);
  1202. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1203. $expected = [
  1204. 'div' => ['class' => 'submit'],
  1205. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1206. '/div',
  1207. ];
  1208. $this->assertHtml($expected, $result);
  1209. $result = $this->Form->end();
  1210. $tokenDebug = urlencode(json_encode([
  1211. '/articles/add',
  1212. [
  1213. 'Address.title',
  1214. 'Address.first_name',
  1215. ],
  1216. ['save', 'cancel'],
  1217. ]));
  1218. $expected = [
  1219. 'div' => ['style' => 'display:none;'],
  1220. ['input' => [
  1221. 'type' => 'hidden',
  1222. 'name' => '_Token[fields]',
  1223. 'autocomplete',
  1224. 'value',
  1225. ]],
  1226. ['input' => [
  1227. 'type' => 'hidden',
  1228. 'name' => '_Token[unlocked]',
  1229. 'value' => 'cancel%7Csave',
  1230. 'autocomplete' => 'off',
  1231. ]],
  1232. ['input' => [
  1233. 'type' => 'hidden',
  1234. 'name' => '_Token[debug]',
  1235. 'value' => $tokenDebug,
  1236. 'autocomplete' => 'off',
  1237. ]],
  1238. '/div',
  1239. ];
  1240. $this->assertHtml($expected, $result);
  1241. }
  1242. /**
  1243. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1244. */
  1245. public function testSecurityButtonNestedNamed(): void
  1246. {
  1247. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1248. $this->Form->create();
  1249. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1250. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1251. $this->assertEquals(['Address.button'], $result);
  1252. }
  1253. /**
  1254. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1255. */
  1256. public function testSecuritySubmitNestedNamed(): void
  1257. {
  1258. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1259. $this->Form->create($this->article);
  1260. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1261. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1262. $this->assertEquals(['Address.button'], $result);
  1263. }
  1264. /**
  1265. * Test that the correct fields are unlocked for image submits with no names.
  1266. */
  1267. public function testSecuritySubmitImageNoName(): void
  1268. {
  1269. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1270. $this->Form->create();
  1271. $result = $this->Form->submit('save.png');
  1272. $expected = [
  1273. 'div' => ['class' => 'submit'],
  1274. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1275. '/div',
  1276. ];
  1277. $this->assertHtml($expected, $result);
  1278. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1279. $this->assertEquals(['x', 'y'], $result);
  1280. }
  1281. /**
  1282. * Test that the correct fields are unlocked for image submits with names.
  1283. */
  1284. public function testSecuritySubmitImageName(): void
  1285. {
  1286. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1287. $this->Form->create();
  1288. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1289. $expected = [
  1290. 'div' => ['class' => 'submit'],
  1291. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1292. '/div',
  1293. ];
  1294. $this->assertHtml($expected, $result);
  1295. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1296. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1297. }
  1298. /**
  1299. * testFormSecurityMultipleControlFields method
  1300. *
  1301. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1302. */
  1303. public function testFormSecurityMultipleControlFields(): void
  1304. {
  1305. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1306. $this->Form->create();
  1307. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1308. $this->Form->control('Addresses.0.title');
  1309. $this->Form->control('Addresses.0.first_name');
  1310. $this->Form->control('Addresses.0.last_name');
  1311. $this->Form->control('Addresses.0.address');
  1312. $this->Form->control('Addresses.0.city');
  1313. $this->Form->control('Addresses.0.phone');
  1314. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1315. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1316. $this->Form->control('Addresses.1.title');
  1317. $this->Form->control('Addresses.1.first_name');
  1318. $this->Form->control('Addresses.1.last_name');
  1319. $this->Form->control('Addresses.1.address');
  1320. $this->Form->control('Addresses.1.city');
  1321. $this->Form->control('Addresses.1.phone');
  1322. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1323. $result = $this->Form->secure();
  1324. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1325. $tokenDebug = urlencode(json_encode([
  1326. '/articles/add',
  1327. [
  1328. 'Addresses.0.id' => '123456',
  1329. 'Addresses.0.title',
  1330. 'Addresses.0.first_name',
  1331. 'Addresses.0.last_name',
  1332. 'Addresses.0.address',
  1333. 'Addresses.0.city',
  1334. 'Addresses.0.phone',
  1335. 'Addresses.0.primary',
  1336. 'Addresses.1.id' => '654321',
  1337. 'Addresses.1.title',
  1338. 'Addresses.1.first_name',
  1339. 'Addresses.1.last_name',
  1340. 'Addresses.1.address',
  1341. 'Addresses.1.city',
  1342. 'Addresses.1.phone',
  1343. 'Addresses.1.primary',
  1344. ],
  1345. [],
  1346. ]));
  1347. $expected = [
  1348. 'div' => ['style' => 'display:none;'],
  1349. ['input' => [
  1350. 'type' => 'hidden',
  1351. 'name' => '_Token[fields]',
  1352. 'value' => $hash,
  1353. 'autocomplete' => 'off',
  1354. ]],
  1355. ['input' => [
  1356. 'type' => 'hidden',
  1357. 'name' => '_Token[unlocked]',
  1358. 'autocomplete' => 'off',
  1359. 'value' => '',
  1360. ]],
  1361. ['input' => [
  1362. 'type' => 'hidden',
  1363. 'name' => '_Token[debug]',
  1364. 'value' => $tokenDebug,
  1365. 'autocomplete' => 'off',
  1366. ]],
  1367. '/div',
  1368. ];
  1369. $this->assertHtml($expected, $result);
  1370. }
  1371. /**
  1372. * testFormSecurityArrayFields method
  1373. *
  1374. * Test form security with Model.field.0 style inputs.
  1375. */
  1376. public function testFormSecurityArrayFields(): void
  1377. {
  1378. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1379. $this->Form->create();
  1380. $this->Form->text('Address.primary.1');
  1381. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1382. $this->assertSame('Address.primary', $result[0]);
  1383. $this->Form->text('Address.secondary.1.0');
  1384. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1385. $this->assertSame('Address.secondary', $result[1]);
  1386. }
  1387. /**
  1388. * testFormSecurityMultipleControlDisabledFields method
  1389. *
  1390. * Test secure form generation with multiple records and disabled fields.
  1391. */
  1392. public function testFormSecurityMultipleControlDisabledFields(): void
  1393. {
  1394. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1395. 'unlockedFields' => ['first_name', 'address'],
  1396. ]));
  1397. $this->Form->create();
  1398. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1399. $this->Form->text('Addresses.0.title');
  1400. $this->Form->text('Addresses.0.first_name');
  1401. $this->Form->text('Addresses.0.last_name');
  1402. $this->Form->text('Addresses.0.address');
  1403. $this->Form->text('Addresses.0.city');
  1404. $this->Form->text('Addresses.0.phone');
  1405. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1406. $this->Form->text('Addresses.1.title');
  1407. $this->Form->text('Addresses.1.first_name');
  1408. $this->Form->text('Addresses.1.last_name');
  1409. $this->Form->text('Addresses.1.address');
  1410. $this->Form->text('Addresses.1.city');
  1411. $this->Form->text('Addresses.1.phone');
  1412. $result = $this->Form->secure();
  1413. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1414. $tokenDebug = urlencode(json_encode([
  1415. '/articles/add',
  1416. [
  1417. 'Addresses.0.id' => '123456',
  1418. 'Addresses.0.title',
  1419. 'Addresses.0.last_name',
  1420. 'Addresses.0.city',
  1421. 'Addresses.0.phone',
  1422. 'Addresses.1.id' => '654321',
  1423. 'Addresses.1.title',
  1424. 'Addresses.1.last_name',
  1425. 'Addresses.1.city',
  1426. 'Addresses.1.phone',
  1427. ],
  1428. [
  1429. 'first_name',
  1430. 'address',
  1431. ],
  1432. ]));
  1433. $expected = [
  1434. 'div' => ['style' => 'display:none;'],
  1435. ['input' => [
  1436. 'type' => 'hidden',
  1437. 'name' => '_Token[fields]',
  1438. 'autocomplete' => 'off',
  1439. 'value' => $hash,
  1440. ]],
  1441. ['input' => [
  1442. 'type' => 'hidden',
  1443. 'name' => '_Token[unlocked]',
  1444. 'autocomplete' => 'off',
  1445. 'value' => 'address%7Cfirst_name',
  1446. ]],
  1447. ['input' => [
  1448. 'type' => 'hidden',
  1449. 'name' => '_Token[debug]',
  1450. 'autocomplete' => 'off',
  1451. 'value' => $tokenDebug,
  1452. ]],
  1453. '/div',
  1454. ];
  1455. $this->assertHtml($expected, $result);
  1456. }
  1457. /**
  1458. * testFormSecurityControlDisabledFields method
  1459. *
  1460. * Test single record form with disabled fields.
  1461. */
  1462. public function testFormSecurityControlUnlockedFields(): void
  1463. {
  1464. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1465. 'unlockedFields' => ['first_name', 'address'],
  1466. ]));
  1467. $this->Form->create();
  1468. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1469. $this->assertEquals(
  1470. $this->View->getRequest()->getAttribute('formTokenData'),
  1471. ['unlockedFields' => $result]
  1472. );
  1473. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1474. $this->Form->text('Addresses.title');
  1475. $this->Form->text('Addresses.first_name');
  1476. $this->Form->text('Addresses.last_name');
  1477. $this->Form->text('Addresses.address');
  1478. $this->Form->text('Addresses.city');
  1479. $this->Form->text('Addresses.phone');
  1480. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1481. $expected = [
  1482. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1483. 'Addresses.city', 'Addresses.phone',
  1484. ];
  1485. $this->assertEquals($expected, $result);
  1486. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1487. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1488. $tokenDebug = urlencode(json_encode([
  1489. '/articles/add',
  1490. [
  1491. 'Addresses.id' => '123456',
  1492. 'Addresses.title',
  1493. 'Addresses.last_name',
  1494. 'Addresses.city',
  1495. 'Addresses.phone',
  1496. ],
  1497. [
  1498. 'first_name',
  1499. 'address',
  1500. ],
  1501. ]));
  1502. $expected = [
  1503. 'div' => ['style' => 'display:none;'],
  1504. ['input' => [
  1505. 'type' => 'hidden',
  1506. 'name' => '_Token[fields]',
  1507. 'value' => $hash,
  1508. 'autocomplete' => 'off',
  1509. 'data-foo' => 'bar',
  1510. ]],
  1511. ['input' => [
  1512. 'type' => 'hidden',
  1513. 'name' => '_Token[unlocked]',
  1514. 'value' => 'address%7Cfirst_name',
  1515. 'autocomplete' => 'off',
  1516. 'data-foo' => 'bar',
  1517. ]],
  1518. ['input' => [
  1519. 'type' => 'hidden', 'name' => '_Token[debug]',
  1520. 'value' => $tokenDebug,
  1521. 'autocomplete' => 'off',
  1522. 'data-foo' => 'bar',
  1523. ]],
  1524. '/div',
  1525. ];
  1526. $this->assertHtml($expected, $result);
  1527. }
  1528. /**
  1529. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1530. *
  1531. * Test single record form with debugSecurity param.
  1532. */
  1533. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1534. {
  1535. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1536. 'unlockedFields' => ['first_name', 'address'],
  1537. ]));
  1538. $this->Form->create();
  1539. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1540. $this->assertEquals(
  1541. $this->View->getRequest()->getAttribute('formTokenData'),
  1542. ['unlockedFields' => $result]
  1543. );
  1544. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1545. $this->Form->text('Addresses.title');
  1546. $this->Form->text('Addresses.first_name');
  1547. $this->Form->text('Addresses.last_name');
  1548. $this->Form->text('Addresses.address');
  1549. $this->Form->text('Addresses.city');
  1550. $this->Form->text('Addresses.phone');
  1551. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1552. $expected = [
  1553. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1554. 'Addresses.city', 'Addresses.phone',
  1555. ];
  1556. $this->assertEquals($expected, $result);
  1557. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1558. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1559. $tokenDebug = urlencode(json_encode([
  1560. '/articles/add',
  1561. [
  1562. 'Addresses.id' => '123456',
  1563. 'Addresses.title',
  1564. 'Addresses.last_name',
  1565. 'Addresses.city',
  1566. 'Addresses.phone',
  1567. ],
  1568. [
  1569. 'first_name',
  1570. 'address',
  1571. ],
  1572. ]));
  1573. $expected = [
  1574. 'div' => ['style' => 'display:none;'],
  1575. ['input' => [
  1576. 'type' => 'hidden',
  1577. 'name' => '_Token[fields]',
  1578. 'value' => $hash,
  1579. 'autocomplete' => 'off',
  1580. 'data-foo' => 'bar',
  1581. ]],
  1582. ['input' => [
  1583. 'type' => 'hidden',
  1584. 'name' => '_Token[unlocked]',
  1585. 'value' => 'address%7Cfirst_name',
  1586. 'autocomplete' => 'off',
  1587. 'data-foo' => 'bar',
  1588. ]],
  1589. ['input' => [
  1590. 'type' => 'hidden', 'name' => '_Token[debug]',
  1591. 'value' => $tokenDebug,
  1592. 'autocomplete' => 'off',
  1593. 'data-foo' => 'bar',
  1594. ]],
  1595. '/div',
  1596. ];
  1597. $this->assertHtml($expected, $result);
  1598. }
  1599. /**
  1600. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1601. *
  1602. * Debug is false, debugSecurity is true -> no debug
  1603. */
  1604. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1605. {
  1606. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1607. 'unlockedFields' => ['first_name', 'address'],
  1608. ]));
  1609. $this->Form->create();
  1610. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1611. $this->assertEquals(
  1612. $this->View->getRequest()->getAttribute('formTokenData'),
  1613. ['unlockedFields' => $result]
  1614. );
  1615. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1616. $this->Form->text('Addresses.title');
  1617. $this->Form->text('Addresses.first_name');
  1618. $this->Form->text('Addresses.last_name');
  1619. $this->Form->text('Addresses.address');
  1620. $this->Form->text('Addresses.city');
  1621. $this->Form->text('Addresses.phone');
  1622. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1623. $expected = [
  1624. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1625. 'Addresses.city', 'Addresses.phone',
  1626. ];
  1627. $this->assertEquals($expected, $result);
  1628. Configure::write('debug', false);
  1629. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1630. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1631. $expected = [
  1632. 'div' => ['style' => 'display:none;'],
  1633. ['input' => [
  1634. 'type' => 'hidden',
  1635. 'name' => '_Token[fields]',
  1636. 'value' => $hash,
  1637. 'autocomplete' => 'off',
  1638. 'data-foo' => 'bar',
  1639. ]],
  1640. ['input' => [
  1641. 'type' => 'hidden',
  1642. 'name' => '_Token[unlocked]',
  1643. 'value' => 'address%7Cfirst_name',
  1644. 'autocomplete' => 'off',
  1645. 'data-foo' => 'bar',
  1646. ]],
  1647. '/div',
  1648. ];
  1649. $this->assertHtml($expected, $result);
  1650. }
  1651. /**
  1652. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1653. *
  1654. * Test single record form with debugSecurity param.
  1655. */
  1656. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1657. {
  1658. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1659. 'unlockedFields' => ['first_name', 'address'],
  1660. ]));
  1661. $this->Form->create();
  1662. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1663. $this->assertEquals(
  1664. $this->View->getRequest()->getAttribute('formTokenData'),
  1665. ['unlockedFields' => $result]
  1666. );
  1667. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1668. $this->Form->text('Addresses.title');
  1669. $this->Form->text('Addresses.first_name');
  1670. $this->Form->text('Addresses.last_name');
  1671. $this->Form->text('Addresses.address');
  1672. $this->Form->text('Addresses.city');
  1673. $this->Form->text('Addresses.phone');
  1674. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1675. $expected = [
  1676. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1677. 'Addresses.city', 'Addresses.phone',
  1678. ];
  1679. $this->assertEquals($expected, $result);
  1680. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1681. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1682. $expected = [
  1683. 'div' => ['style' => 'display:none;'],
  1684. ['input' => [
  1685. 'type' => 'hidden',
  1686. 'name' => '_Token[fields]',
  1687. 'value' => $hash,
  1688. 'autocomplete' => 'off',
  1689. 'data-foo' => 'bar',
  1690. ]],
  1691. ['input' => [
  1692. 'type' => 'hidden',
  1693. 'name' => '_Token[unlocked]',
  1694. 'value' => 'address%7Cfirst_name',
  1695. 'autocomplete' => 'off',
  1696. 'data-foo' => 'bar',
  1697. ]],
  1698. '/div',
  1699. ];
  1700. $this->assertHtml($expected, $result);
  1701. }
  1702. /**
  1703. * testFormSecureWithCustomNameAttribute method
  1704. *
  1705. * Test securing inputs with custom name attributes.
  1706. */
  1707. public function testFormSecureWithCustomNameAttribute(): void
  1708. {
  1709. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1710. $this->Form->create();
  1711. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1712. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1713. $this->assertSame('User.custom', $result[0]);
  1714. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1715. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1716. $this->assertSame('User.custom.another.value', $result[1]);
  1717. }
  1718. /**
  1719. * testFormSecuredControl method
  1720. *
  1721. * Test generation of entire secure form, assertions made on control() output.
  1722. */
  1723. public function testFormSecuredControl(): void
  1724. {
  1725. $this->View->setRequest($this->View->getRequest()
  1726. ->withAttribute('formTokenData', [])
  1727. ->withAttribute('csrfToken', 'testKey'));
  1728. $this->article['schema'] = [
  1729. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1730. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1731. ];
  1732. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1733. $encoding = strtolower(Configure::read('App.encoding'));
  1734. $expected = [
  1735. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1736. 'div' => ['style' => 'display:none;'],
  1737. ['input' => [
  1738. 'type' => 'hidden',
  1739. 'name' => '_csrfToken',
  1740. 'value' => 'testKey',
  1741. 'autocomplete' => 'off',
  1742. ]],
  1743. '/div',
  1744. ];
  1745. $this->assertHtml($expected, $result);
  1746. $result = $this->Form->control('ratio');
  1747. $expected = [
  1748. 'div' => ['class'],
  1749. 'label' => ['for'],
  1750. 'Ratio',
  1751. '/label',
  1752. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1753. '/div',
  1754. ];
  1755. $this->assertHtml($expected, $result);
  1756. $result = $this->Form->control('population');
  1757. $expected = [
  1758. 'div' => ['class'],
  1759. 'label' => ['for'],
  1760. 'Population',
  1761. '/label',
  1762. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1763. '/div',
  1764. ];
  1765. $this->assertHtml($expected, $result);
  1766. $result = $this->Form->control('published', ['type' => 'text']);
  1767. $expected = [
  1768. 'div' => ['class' => 'input text'],
  1769. 'label' => ['for' => 'published'],
  1770. 'Published',
  1771. '/label',
  1772. ['input' => [
  1773. 'type' => 'text',
  1774. 'name' => 'published',
  1775. 'id' => 'published',
  1776. ]],
  1777. '/div',
  1778. ];
  1779. $this->assertHtml($expected, $result);
  1780. $result = $this->Form->control('other', ['type' => 'text']);
  1781. $expected = [
  1782. 'div' => ['class' => 'input text'],
  1783. 'label' => ['for' => 'other'],
  1784. 'Other',
  1785. '/label',
  1786. ['input' => [
  1787. 'type' => 'text',
  1788. 'name' => 'other',
  1789. 'id',
  1790. ]],
  1791. '/div',
  1792. ];
  1793. $this->assertHtml($expected, $result);
  1794. $result = $this->Form->hidden('stuff');
  1795. $expected = [
  1796. 'input' => [
  1797. 'type' => 'hidden',
  1798. 'name' => 'stuff',
  1799. ],
  1800. ];
  1801. $this->assertHtml($expected, $result);
  1802. $result = $this->Form->hidden('hidden', ['value' => false]);
  1803. $expected = ['input' => [
  1804. 'type' => 'hidden',
  1805. 'name' => 'hidden',
  1806. 'value' => '0',
  1807. ]];
  1808. $this->assertHtml($expected, $result);
  1809. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1810. $expected = [
  1811. 'div' => ['class' => 'input checkbox'],
  1812. ['input' => [
  1813. 'type' => 'hidden',
  1814. 'name' => 'something',
  1815. 'value' => '0',
  1816. ]],
  1817. 'label' => ['for' => 'something'],
  1818. ['input' => [
  1819. 'type' => 'checkbox',
  1820. 'name' => 'something',
  1821. 'value' => '1',
  1822. 'id' => 'something',
  1823. ]],
  1824. 'Something',
  1825. '/label',
  1826. '/div',
  1827. ];
  1828. $this->assertHtml($expected, $result);
  1829. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1830. $expectedFields = [
  1831. 'ratio',
  1832. 'population',
  1833. 'published',
  1834. 'other',
  1835. 'stuff' => '',
  1836. 'hidden' => '0',
  1837. 'something',
  1838. ];
  1839. $this->assertEquals($expectedFields, $result);
  1840. $result = $this->Form->secure();
  1841. $tokenDebug = urlencode(json_encode([
  1842. '/articles/add',
  1843. $expectedFields,
  1844. [],
  1845. ]));
  1846. $expected = [
  1847. 'div' => ['style' => 'display:none;'],
  1848. ['input' => [
  1849. 'type' => 'hidden',
  1850. 'name' => '_Token[fields]',
  1851. 'value',
  1852. 'autocomplete',
  1853. ]],
  1854. ['input' => [
  1855. 'type' => 'hidden',
  1856. 'name' => '_Token[unlocked]',
  1857. 'value' => '',
  1858. 'autocomplete' => 'off',
  1859. ]],
  1860. ['input' => [
  1861. 'type' => 'hidden', 'name' => '_Token[debug]',
  1862. 'value' => $tokenDebug,
  1863. 'autocomplete' => 'off',
  1864. ]],
  1865. '/div',
  1866. ];
  1867. $this->assertHtml($expected, $result);
  1868. $data = [
  1869. 'ratio' => '',
  1870. 'population' => '',
  1871. 'published' => '',
  1872. 'other' => '',
  1873. 'stuff' => '',
  1874. 'hidden' => '0',
  1875. 'something' => '',
  1876. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1877. ];
  1878. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1879. }
  1880. /**
  1881. * testSecuredControlCustomName method
  1882. *
  1883. * Test secured inputs with custom names.
  1884. */
  1885. public function testSecuredControlCustomName(): void
  1886. {
  1887. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1888. $this->Form->create();
  1889. $this->Form->text('text_input', [
  1890. 'name' => 'Option[General.default_role]',
  1891. ]);
  1892. $expected = ['Option.General.default_role'];
  1893. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1894. $this->assertEquals($expected, $result);
  1895. $this->Form->select('select_box', [1, 2], [
  1896. 'name' => 'Option[General.select_role]',
  1897. ]);
  1898. $expected[] = 'Option.General.select_role';
  1899. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1900. $this->assertEquals($expected, $result);
  1901. $this->Form->text('other.things[]');
  1902. $expected[] = 'other.things';
  1903. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1904. $this->assertEquals($expected, $result);
  1905. }
  1906. /**
  1907. * testSecuredControlDuplicate method
  1908. *
  1909. * Test that a hidden field followed by a visible field
  1910. * undoes the hidden field locking.
  1911. */
  1912. public function testSecuredControlDuplicate(): void
  1913. {
  1914. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1915. $this->Form->create();
  1916. $this->Form->control('text_val', [
  1917. 'type' => 'hidden',
  1918. 'value' => 'some text',
  1919. ]);
  1920. $expected = ['text_val' => 'some text'];
  1921. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1922. $this->assertEquals($expected, $result);
  1923. $this->Form->control('text_val', [
  1924. 'type' => 'text',
  1925. ]);
  1926. $expected = ['text_val'];
  1927. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1928. $this->assertEquals($expected, $result);
  1929. }
  1930. /**
  1931. * testFormSecuredFileControl method
  1932. *
  1933. * Tests that the correct keys are added to the field hash index.
  1934. */
  1935. public function testFormSecuredFileControl(): void
  1936. {
  1937. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1938. $this->Form->create();
  1939. $this->Form->file('Attachment.file');
  1940. $expected = ['Attachment.file'];
  1941. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1942. $this->assertEquals($expected, $result);
  1943. }
  1944. /**
  1945. * testFormSecuredMultipleSelect method
  1946. *
  1947. * Test that multiple selects keys are added to field hash.
  1948. */
  1949. public function testFormSecuredMultipleSelect(): void
  1950. {
  1951. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1952. $this->Form->create();
  1953. $options = ['1' => 'one', '2' => 'two'];
  1954. $this->Form->select('Model.select', $options);
  1955. $expected = ['Model.select'];
  1956. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1957. $this->assertEquals($expected, $result);
  1958. $this->Form->fields = [];
  1959. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1960. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1961. $this->assertEquals($expected, $result);
  1962. }
  1963. /**
  1964. * testFormSecuredRadio method
  1965. */
  1966. public function testFormSecuredRadio(): void
  1967. {
  1968. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1969. $this->Form->create();
  1970. $options = ['1' => 'option1', '2' => 'option2'];
  1971. $this->Form->radio('Test.test', $options);
  1972. $expected = ['Test.test'];
  1973. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1974. $this->assertEquals($expected, $result);
  1975. $this->Form->radio('Test.all', $options, [
  1976. 'disabled' => ['option1', 'option2'],
  1977. ]);
  1978. $expected = ['Test.test', 'Test.all' => ''];
  1979. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1980. $this->assertEquals($expected, $result);
  1981. $this->Form->radio('Test.some', $options, [
  1982. 'disabled' => ['option1'],
  1983. ]);
  1984. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1985. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1986. $this->assertEquals($expected, $result);
  1987. }
  1988. /**
  1989. * testFormSecuredAndDisabledNotAssoc method
  1990. *
  1991. * Test that when disabled is in a list based attribute array it works.
  1992. */
  1993. public function testFormSecuredAndDisabledNotAssoc(): void
  1994. {
  1995. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1996. $this->Form->create();
  1997. $this->deprecated(function () {
  1998. $this->Form->select('Model.select', [1, 2], ['disabled']);
  1999. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2000. $this->Form->text('Model.text', ['disabled']);
  2001. $this->Form->textarea('Model.textarea', ['disabled']);
  2002. $this->Form->password('Model.password', ['disabled']);
  2003. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2004. });
  2005. $expected = [
  2006. 'Model.radio' => '',
  2007. ];
  2008. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2009. $this->assertEquals($expected, $result);
  2010. }
  2011. /**
  2012. * testFormSecuredAndDisabled method
  2013. *
  2014. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2015. * hashing.
  2016. */
  2017. public function testFormSecuredAndDisabled(): void
  2018. {
  2019. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2020. $this->Form->create();
  2021. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2022. $this->Form->text('Model.text', ['disabled' => true]);
  2023. $this->Form->password('Model.text', ['disabled' => true]);
  2024. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2025. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2026. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2027. $this->Form->year('Model.year', ['disabled' => true]);
  2028. $this->Form->month('Model.month', ['disabled' => true]);
  2029. $this->Form->day('Model.day', ['disabled' => true]);
  2030. $this->Form->hour('Model.hour', ['disabled' => true]);
  2031. $this->Form->minute('Model.minute', ['disabled' => true]);
  2032. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2033. $expected = [
  2034. 'Model.radio' => '',
  2035. ];
  2036. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2037. $this->assertEquals($expected, $result);
  2038. }
  2039. /**
  2040. * testUnlockFieldAddsToList method
  2041. *
  2042. * Test disableField.
  2043. */
  2044. public function testUnlockFieldAddsToList(): void
  2045. {
  2046. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2047. 'unlockedFields' => [],
  2048. ]));
  2049. $this->Form->create();
  2050. $this->Form->unlockField('Contact.name');
  2051. $this->Form->text('Contact.name');
  2052. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2053. $this->assertEquals([], $result);
  2054. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2055. $this->assertEquals(['Contact.name'], $result);
  2056. }
  2057. /**
  2058. * testUnlockFieldRemovingFromFields method
  2059. *
  2060. * Test unlockField removing from fields array.
  2061. */
  2062. public function testUnlockFieldRemovingFromFields(): void
  2063. {
  2064. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2065. 'unlockedFields' => [],
  2066. ]));
  2067. $this->Form->create($this->article);
  2068. $this->Form->hidden('Article.id', ['value' => 1]);
  2069. $this->Form->text('Article.title');
  2070. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2071. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2072. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2073. $this->Form->unlockField('Article.title');
  2074. $this->Form->unlockField('Article.id');
  2075. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2076. $this->assertEquals([], $result);
  2077. }
  2078. /**
  2079. * testResetUnlockFields method
  2080. *
  2081. * Test reset unlockFields, when create new form.
  2082. */
  2083. public function testResetUnlockFields(): void
  2084. {
  2085. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2086. 'key' => 'testKey',
  2087. 'unlockedFields' => [],
  2088. ]));
  2089. $this->Form->create();
  2090. $this->Form->unlockField('Contact.id');
  2091. $this->Form->hidden('Contact.id', ['value' => 1]);
  2092. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2093. $this->assertEmpty($result, 'Field should be unlocked');
  2094. $this->Form->end();
  2095. $this->Form->create();
  2096. $this->Form->hidden('Contact.id', ['value' => 1]);
  2097. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2098. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2099. }
  2100. /**
  2101. * testSecuredFormUrlIgnoresHost method
  2102. *
  2103. * Test that only the path + query elements of a form's URL show up in their hash.
  2104. */
  2105. public function testSecuredFormUrlIgnoresHost(): void
  2106. {
  2107. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2108. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2109. $this->Form->create($this->article, [
  2110. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2111. ]);
  2112. $result = $this->Form->secure();
  2113. $this->assertStringContainsString($expected, $result);
  2114. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2115. $result = $this->Form->secure();
  2116. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2117. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2118. $result = $this->Form->secure();
  2119. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2120. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2121. $result = $this->Form->secure();
  2122. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2123. }
  2124. /**
  2125. * testSecuredFormUrlHasHtmlAndIdentifier method
  2126. *
  2127. * Test that URL, HTML and identifier show up in their hashes.
  2128. */
  2129. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2130. {
  2131. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2132. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2133. $this->Form->create($this->article, [
  2134. 'url' => [
  2135. 'controller' => 'articles',
  2136. 'action' => 'view',
  2137. '?' => [
  2138. 'page' => 1,
  2139. 'limit' => 10,
  2140. 'html' => '<>"',
  2141. ],
  2142. '#' => 'result',
  2143. ],
  2144. ]);
  2145. $result = $this->Form->secure();
  2146. $this->assertStringContainsString($expected, $result);
  2147. $this->Form->create($this->article, [
  2148. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2149. ]);
  2150. $result = $this->Form->secure();
  2151. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2152. $this->Form->create($this->article, [
  2153. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2154. ]);
  2155. $result = $this->Form->secure();
  2156. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2157. }
  2158. /**
  2159. * testErrorMessageDisplay method
  2160. *
  2161. * Test error message display.
  2162. */
  2163. public function testErrorMessageDisplay(): void
  2164. {
  2165. $this->article['errors'] = [
  2166. 'Article' => [
  2167. 'title' => 'error message',
  2168. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2169. ],
  2170. ];
  2171. $this->Form->create($this->article);
  2172. $result = $this->Form->control('Article.title');
  2173. $expected = [
  2174. 'div' => ['class' => 'input text error'],
  2175. 'label' => ['for' => 'article-title'],
  2176. 'Title',
  2177. '/label',
  2178. 'input' => [
  2179. 'type' => 'text',
  2180. 'name' => 'Article[title]',
  2181. 'id' => 'article-title',
  2182. 'class' => 'form-error',
  2183. 'aria-invalid' => 'true',
  2184. 'aria-describedby' => 'article-title-error',
  2185. ],
  2186. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2187. 'error message',
  2188. '/div',
  2189. '/div',
  2190. ];
  2191. $this->assertHtml($expected, $result);
  2192. $result = $this->Form->control('Article.title', [
  2193. 'templates' => [
  2194. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2195. ],
  2196. ]);
  2197. $expected = [
  2198. 'div' => ['class' => 'input text error'],
  2199. 'label' => ['for' => 'article-title'],
  2200. 'Title',
  2201. '/label',
  2202. 'input' => [
  2203. 'type' => 'text',
  2204. 'name' => 'Article[title]',
  2205. 'id' => 'article-title',
  2206. 'class' => 'form-error',
  2207. // No aria-describedby because error template is custom
  2208. 'aria-invalid' => 'true',
  2209. ],
  2210. '/div',
  2211. ];
  2212. $this->assertHtml($expected, $result);
  2213. $result = $this->Form->control('Article.content');
  2214. $expected = [
  2215. 'div' => ['class' => 'input text error'],
  2216. 'label' => ['for' => 'article-content'],
  2217. 'Content',
  2218. '/label',
  2219. 'input' => [
  2220. 'type' => 'text',
  2221. 'name' => 'Article[content]',
  2222. 'id' => 'article-content',
  2223. 'class' => 'form-error',
  2224. 'aria-invalid' => 'true',
  2225. 'aria-describedby' => 'article-content-error',
  2226. ],
  2227. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2228. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2229. '/div',
  2230. '/div',
  2231. ];
  2232. $this->assertHtml($expected, $result);
  2233. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2234. $expected = [
  2235. 'div' => ['class' => 'input text error'],
  2236. 'label' => ['for' => 'article-content'],
  2237. 'Content',
  2238. '/label',
  2239. 'input' => [
  2240. 'type' => 'text',
  2241. 'name' => 'Article[content]',
  2242. 'id' => 'article-content',
  2243. 'class' => 'form-error',
  2244. 'aria-invalid' => 'true',
  2245. 'aria-describedby' => 'article-content-error',
  2246. ],
  2247. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2248. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2249. '/div',
  2250. '/div',
  2251. ];
  2252. $this->assertHtml($expected, $result);
  2253. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2254. $expected = [
  2255. 'div' => ['class' => 'input text error'],
  2256. 'label' => ['for' => 'article-content'],
  2257. 'Content',
  2258. '/label',
  2259. 'input' => [
  2260. 'type' => 'text',
  2261. 'name' => 'Article[content]',
  2262. 'id' => 'article-content',
  2263. 'class' => 'form-error',
  2264. 'aria-invalid' => 'true',
  2265. 'aria-describedby' => 'article-content-error',
  2266. ],
  2267. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2268. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2269. '/div',
  2270. '/div',
  2271. ];
  2272. $this->assertHtml($expected, $result);
  2273. }
  2274. /**
  2275. * testEmptyErrorValidation method
  2276. *
  2277. * Test validation errors, when validation message is an empty string.
  2278. */
  2279. public function testEmptyErrorValidation(): void
  2280. {
  2281. $this->article['errors'] = [
  2282. 'Article' => ['title' => ''],
  2283. ];
  2284. $this->Form->create($this->article);
  2285. $result = $this->Form->control('Article.title');
  2286. $expected = [
  2287. 'div' => ['class' => 'input text error'],
  2288. 'label' => ['for' => 'article-title'],
  2289. 'Title',
  2290. '/label',
  2291. 'input' => [
  2292. 'type' => 'text',
  2293. 'name' => 'Article[title]',
  2294. 'id' => 'article-title',
  2295. 'class' => 'form-error',
  2296. 'aria-invalid' => 'true',
  2297. 'aria-describedby' => 'article-title-error',
  2298. ],
  2299. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2300. [],
  2301. '/div',
  2302. '/div',
  2303. ];
  2304. $this->assertHtml($expected, $result);
  2305. }
  2306. /**
  2307. * testEmptyControlErrorValidation method
  2308. *
  2309. * Test validation errors, when calling control() overriding validation message by an empty string.
  2310. */
  2311. public function testEmptyControlErrorValidation(): void
  2312. {
  2313. $this->article['errors'] = [
  2314. 'Article' => ['title' => 'error message'],
  2315. ];
  2316. $this->Form->create($this->article);
  2317. $result = $this->Form->control('Article.title', ['error' => '']);
  2318. $expected = [
  2319. 'div' => ['class' => 'input text error'],
  2320. 'label' => ['for' => 'article-title'],
  2321. 'Title',
  2322. '/label',
  2323. 'input' => [
  2324. 'aria-invalid' => 'true',
  2325. 'aria-describedby' => 'article-title-error',
  2326. 'type' => 'text',
  2327. 'name' => 'Article[title]',
  2328. 'id' => 'article-title',
  2329. 'class' => 'form-error',
  2330. ],
  2331. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2332. [],
  2333. '/div',
  2334. '/div',
  2335. ];
  2336. $this->assertHtml($expected, $result);
  2337. }
  2338. /**
  2339. * testControlErrorMessage method
  2340. *
  2341. * Test validation errors, when calling control() overriding validation messages.
  2342. */
  2343. public function testControlErrorMessage(): void
  2344. {
  2345. $this->article['errors'] = [
  2346. 'title' => ['error message'],
  2347. ];
  2348. $this->Form->create($this->article);
  2349. $result = $this->Form->control('title', [
  2350. 'error' => 'Custom error!',
  2351. ]);
  2352. $expected = [
  2353. 'div' => ['class' => 'input text required error'],
  2354. 'label' => ['for' => 'title'],
  2355. 'Title',
  2356. '/label',
  2357. 'input' => [
  2358. 'type' => 'text',
  2359. 'name' => 'title',
  2360. 'id' => 'title',
  2361. 'class' => 'form-error',
  2362. 'required' => 'required',
  2363. 'data-validity-message' => 'This field cannot be left empty',
  2364. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2365. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2366. 'aria-required' => 'true',
  2367. 'aria-invalid' => 'true',
  2368. 'aria-describedby' => 'title-error',
  2369. ],
  2370. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2371. 'Custom error!',
  2372. '/div',
  2373. '/div',
  2374. ];
  2375. $this->assertHtml($expected, $result);
  2376. $result = $this->Form->control('title', [
  2377. 'error' => ['error message' => 'Custom error!'],
  2378. ]);
  2379. $expected = [
  2380. 'div' => ['class' => 'input text required error'],
  2381. 'label' => ['for' => 'title'],
  2382. 'Title',
  2383. '/label',
  2384. 'input' => [
  2385. 'type' => 'text',
  2386. 'name' => 'title',
  2387. 'id' => 'title',
  2388. 'aria-required' => 'true',
  2389. 'aria-invalid' => 'true',
  2390. 'aria-describedby' => 'title-error',
  2391. 'class' => 'form-error',
  2392. 'required' => 'required',
  2393. 'data-validity-message' => 'This field cannot be left empty',
  2394. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2395. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2396. ],
  2397. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2398. 'Custom error!',
  2399. '/div',
  2400. '/div',
  2401. ];
  2402. $this->assertHtml($expected, $result);
  2403. }
  2404. /**
  2405. * testFormValidationAssociated method
  2406. *
  2407. * Tests displaying errors for nested entities.
  2408. */
  2409. public function testFormValidationAssociated(): void
  2410. {
  2411. $nested = new Entity(['foo' => 'bar']);
  2412. $nested->setError('foo', ['not a valid bar']);
  2413. $entity = new Entity(['nested' => $nested]);
  2414. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2415. $result = $this->Form->error('nested.foo');
  2416. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2417. }
  2418. /**
  2419. * testFormValidationAssociatedSecondLevel method
  2420. *
  2421. * Test form error display with associated model.
  2422. */
  2423. public function testFormValidationAssociatedSecondLevel(): void
  2424. {
  2425. $inner = new Entity(['bar' => 'baz']);
  2426. $nested = new Entity(['foo' => $inner]);
  2427. $entity = new Entity(['nested' => $nested]);
  2428. $inner->setError('bar', ['not a valid one']);
  2429. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2430. $result = $this->Form->error('nested.foo.bar');
  2431. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2432. }
  2433. /**
  2434. * testFormValidationMultiRecord method
  2435. *
  2436. * Test form error display with multiple records.
  2437. */
  2438. public function testFormValidationMultiRecord(): void
  2439. {
  2440. $one = new Entity();
  2441. $two = new Entity();
  2442. $this->getTableLocator()->get('Contacts', [
  2443. 'className' => ContactsTable::class,
  2444. ]);
  2445. $one->set('email', '');
  2446. $one->setError('email', ['invalid email']);
  2447. $two->set('name', '');
  2448. $two->setError('name', ['This is wrong']);
  2449. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2450. $result = $this->Form->control('0.email');
  2451. $expected = [
  2452. 'div' => ['class' => 'input email error'],
  2453. 'label' => ['for' => '0-email'],
  2454. 'Email',
  2455. '/label',
  2456. 'input' => [
  2457. 'type' => 'email',
  2458. 'name' => '0[email]',
  2459. 'id' => '0-email',
  2460. 'class' => 'form-error',
  2461. 'maxlength' => 255,
  2462. 'value' => '',
  2463. 'aria-invalid' => 'true',
  2464. 'aria-describedby' => '0-email-error',
  2465. ],
  2466. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2467. 'invalid email',
  2468. '/div',
  2469. '/div',
  2470. ];
  2471. $this->assertHtml($expected, $result);
  2472. $result = $this->Form->control('1.name');
  2473. $expected = [
  2474. 'div' => ['class' => 'input text error'],
  2475. 'label' => ['for' => '1-name'],
  2476. 'Name',
  2477. '/label',
  2478. 'input' => [
  2479. 'type' => 'text',
  2480. 'name' => '1[name]',
  2481. 'id' => '1-name',
  2482. 'class' => 'form-error',
  2483. 'maxlength' => 255,
  2484. 'value' => '',
  2485. 'aria-invalid' => 'true',
  2486. 'aria-describedby' => '1-name-error',
  2487. ],
  2488. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2489. 'This is wrong',
  2490. '/div',
  2491. '/div',
  2492. ];
  2493. $this->assertHtml($expected, $result);
  2494. }
  2495. /**
  2496. * testControl method
  2497. *
  2498. * Test various incarnations of control().
  2499. */
  2500. public function testControl(): void
  2501. {
  2502. $this->getTableLocator()->get('ValidateUsers', [
  2503. 'className' => ValidateUsersTable::class,
  2504. ]);
  2505. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2506. $result = $this->Form->control('ValidateUsers.balance');
  2507. $expected = [
  2508. 'div' => ['class'],
  2509. 'label' => ['for'],
  2510. 'Balance',
  2511. '/label',
  2512. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2513. '/div',
  2514. ];
  2515. $this->assertHtml($expected, $result);
  2516. $result = $this->Form->control('ValidateUser.cost_decimal');
  2517. $expected = [
  2518. 'div' => ['class'],
  2519. 'label' => ['for'],
  2520. 'Cost Decimal',
  2521. '/label',
  2522. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2523. '/div',
  2524. ];
  2525. $this->assertHtml($expected, $result);
  2526. $result = $this->Form->control('ValidateUser.null_decimal');
  2527. $expected = [
  2528. 'div' => ['class'],
  2529. 'label' => ['for'],
  2530. 'Null Decimal',
  2531. '/label',
  2532. 'input' => ['name', 'type' => 'number', 'id'],
  2533. '/div',
  2534. ];
  2535. $this->assertHtml($expected, $result);
  2536. }
  2537. /**
  2538. * testControlCustomization method
  2539. *
  2540. * Tests the input method and passing custom options.
  2541. */
  2542. public function testControlCustomization(): void
  2543. {
  2544. $this->getTableLocator()->get('Contacts', [
  2545. 'className' => ContactsTable::class,
  2546. ]);
  2547. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2548. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2549. $expected = [
  2550. 'div' => ['class' => 'input email'],
  2551. 'label' => ['for' => 'custom'],
  2552. 'Email',
  2553. '/label',
  2554. ['input' => [
  2555. 'type' => 'email', 'name' => 'Contact[email]',
  2556. 'id' => 'custom', 'maxlength' => 255,
  2557. ]],
  2558. '/div',
  2559. ];
  2560. $this->assertHtml($expected, $result);
  2561. $result = $this->Form->control('Contact.email', [
  2562. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2563. ]);
  2564. $expected = [
  2565. '<div',
  2566. 'label' => ['for' => 'contact-email'],
  2567. 'Email',
  2568. '/label',
  2569. ['input' => [
  2570. 'type' => 'email', 'name' => 'Contact[email]',
  2571. 'id' => 'contact-email', 'maxlength' => 255,
  2572. ]],
  2573. '/div',
  2574. ];
  2575. $this->assertHtml($expected, $result);
  2576. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2577. $expected = [
  2578. 'div' => ['class' => 'input text'],
  2579. 'label' => ['for' => 'contact-email'],
  2580. 'Email',
  2581. '/label',
  2582. ['input' => [
  2583. 'type' => 'text', 'name' => 'Contact[email]',
  2584. 'id' => 'contact-email', 'maxlength' => '255',
  2585. ]],
  2586. '/div',
  2587. ];
  2588. $this->assertHtml($expected, $result);
  2589. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2590. $expected = [
  2591. 'div' => ['class' => 'input text'],
  2592. 'label' => ['for' => 'contact-5-email'],
  2593. 'Email',
  2594. '/label',
  2595. ['input' => [
  2596. 'type' => 'text', 'name' => 'Contact[5][email]',
  2597. 'id' => 'contact-5-email', 'maxlength' => '255',
  2598. ]],
  2599. '/div',
  2600. ];
  2601. $this->assertHtml($expected, $result);
  2602. $result = $this->Form->control('Contact.password');
  2603. $expected = [
  2604. 'div' => ['class' => 'input password'],
  2605. 'label' => ['for' => 'contact-password'],
  2606. 'Password',
  2607. '/label',
  2608. ['input' => [
  2609. 'type' => 'password', 'name' => 'Contact[password]',
  2610. 'id' => 'contact-password',
  2611. ]],
  2612. '/div',
  2613. ];
  2614. $this->assertHtml($expected, $result);
  2615. $result = $this->Form->control('Contact.email', [
  2616. 'type' => 'file', 'class' => 'textbox',
  2617. ]);
  2618. $expected = [
  2619. 'div' => ['class' => 'input file'],
  2620. 'label' => ['for' => 'contact-email'],
  2621. 'Email',
  2622. '/label',
  2623. ['input' => [
  2624. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2625. 'id' => 'contact-email',
  2626. ]],
  2627. '/div',
  2628. ];
  2629. $this->assertHtml($expected, $result);
  2630. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2631. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2632. $result = $this->Form->control('phone');
  2633. $expected = [
  2634. 'div' => ['class' => 'input tel'],
  2635. 'label' => ['for' => 'phone'],
  2636. 'Phone',
  2637. '/label',
  2638. ['input' => [
  2639. 'type' => 'tel', 'name' => 'phone',
  2640. 'value' => 'Hello &amp; World &gt; weird chars',
  2641. 'id' => 'phone', 'maxlength' => 255,
  2642. ]],
  2643. '/div',
  2644. ];
  2645. $this->assertHtml($expected, $result);
  2646. $this->View->setRequest(
  2647. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2648. );
  2649. $this->Form->create();
  2650. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2651. $expected = [
  2652. 'div' => ['class' => 'input text'],
  2653. 'label' => ['for' => 'myId'],
  2654. 'Field',
  2655. '/label',
  2656. 'input' => [
  2657. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2658. 'value' => 'My value', 'id' => 'myId',
  2659. ],
  2660. '/div',
  2661. ];
  2662. $this->assertHtml($expected, $result);
  2663. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2664. $this->Form->create();
  2665. $entity->setError('field', 'Badness!');
  2666. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2667. $result = $this->Form->control('field');
  2668. $expected = [
  2669. 'div' => ['class' => 'input text error'],
  2670. 'label' => ['for' => 'field'],
  2671. 'Field',
  2672. '/label',
  2673. 'input' => [
  2674. 'type' => 'text',
  2675. 'name' => 'field',
  2676. 'id' => 'field',
  2677. 'class' => 'form-error',
  2678. 'aria-invalid' => 'true',
  2679. 'aria-describedby' => 'field-error',
  2680. ],
  2681. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2682. 'Badness!',
  2683. '/div',
  2684. '/div',
  2685. ];
  2686. $this->assertHtml($expected, $result);
  2687. $result = $this->Form->control('field', [
  2688. 'templates' => [
  2689. 'inputContainerError' => '{{content}}{{error}}',
  2690. 'error' => '<span class="error-message">{{content}}</span>',
  2691. ],
  2692. ]);
  2693. $expected = [
  2694. 'label' => ['for' => 'field'],
  2695. 'Field',
  2696. '/label',
  2697. 'input' => [
  2698. 'type' => 'text',
  2699. 'name' => 'field',
  2700. 'id' => 'field',
  2701. 'class' => 'form-error',
  2702. // No aria-describedby because error template is custom
  2703. 'aria-invalid' => 'true',
  2704. ],
  2705. ['span' => ['class' => 'error-message']],
  2706. 'Badness!',
  2707. '/span',
  2708. ];
  2709. $this->assertHtml($expected, $result);
  2710. $entity->setError('field', ['minLength'], true);
  2711. $result = $this->Form->control('field', [
  2712. 'error' => [
  2713. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2714. 'maxLength' => 'login too large',
  2715. ],
  2716. ]);
  2717. $expected = [
  2718. 'div' => ['class' => 'input text error'],
  2719. 'label' => ['for' => 'field'],
  2720. 'Field',
  2721. '/label',
  2722. 'input' => [
  2723. 'type' => 'text',
  2724. 'name' => 'field',
  2725. 'id' => 'field',
  2726. 'class' => 'form-error',
  2727. 'aria-invalid' => 'true',
  2728. 'aria-describedby' => 'field-error',
  2729. ],
  2730. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2731. 'Le login doit contenir au moins 2 caractères',
  2732. '/div',
  2733. '/div',
  2734. ];
  2735. $this->assertHtml($expected, $result);
  2736. $entity->setError('field', ['maxLength'], true);
  2737. $result = $this->Form->control('field', [
  2738. 'error' => [
  2739. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2740. 'maxLength' => 'login too large',
  2741. ],
  2742. ]);
  2743. $expected = [
  2744. 'div' => ['class' => 'input text error'],
  2745. 'label' => ['for' => 'field'],
  2746. 'Field',
  2747. '/label',
  2748. 'input' => [
  2749. 'type' => 'text',
  2750. 'name' => 'field',
  2751. 'id' => 'field',
  2752. 'class' => 'form-error',
  2753. 'aria-invalid' => 'true',
  2754. 'aria-describedby' => 'field-error',
  2755. ],
  2756. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2757. 'login too large',
  2758. '/div',
  2759. '/div',
  2760. ];
  2761. $this->assertHtml($expected, $result);
  2762. }
  2763. /**
  2764. * testControlWithTemplateFile method
  2765. *
  2766. * Test that control() accepts a template file.
  2767. */
  2768. public function testControlWithTemplateFile(): void
  2769. {
  2770. $result = $this->Form->control('field', [
  2771. 'templates' => 'htmlhelper_tags',
  2772. ]);
  2773. $expected = [
  2774. 'label' => ['for' => 'field'],
  2775. 'Field',
  2776. '/label',
  2777. 'input' => [
  2778. 'type' => 'text', 'name' => 'field',
  2779. 'id' => 'field',
  2780. ],
  2781. ];
  2782. $this->assertHtml($expected, $result);
  2783. }
  2784. /**
  2785. * testNestedControlsEndWithBrackets method
  2786. *
  2787. * Test that nested inputs end with brackets.
  2788. */
  2789. public function testNestedControlsEndWithBrackets(): void
  2790. {
  2791. $result = $this->Form->text('nested.text[]');
  2792. $expected = [
  2793. 'input' => [
  2794. 'type' => 'text', 'name' => 'nested[text][]',
  2795. ],
  2796. ];
  2797. $this->assertHtml($expected, $result);
  2798. $result = $this->Form->file('nested.file[]');
  2799. $expected = [
  2800. 'input' => [
  2801. 'type' => 'file', 'name' => 'nested[file][]',
  2802. ],
  2803. ];
  2804. $this->assertHtml($expected, $result);
  2805. }
  2806. /**
  2807. * testCreateIdPrefix method
  2808. *
  2809. * Test id prefix.
  2810. */
  2811. public function testCreateIdPrefix(): void
  2812. {
  2813. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2814. $result = $this->Form->control('field');
  2815. $expected = [
  2816. 'div' => ['class' => 'input text'],
  2817. 'label' => ['for' => 'prefix-field'],
  2818. 'Field',
  2819. '/label',
  2820. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2821. '/div',
  2822. ];
  2823. $this->assertHtml($expected, $result);
  2824. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2825. $expected = [
  2826. 'div' => ['class' => 'input text'],
  2827. 'label' => ['for' => 'custom-id'],
  2828. 'Field',
  2829. '/label',
  2830. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2831. '/div',
  2832. ];
  2833. $this->assertHtml($expected, $result);
  2834. $result = $this->Form->radio('Model.field', ['option A']);
  2835. $expected = [
  2836. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2837. 'label' => ['for' => 'prefix-model-field-0'],
  2838. ['input' => [
  2839. 'type' => 'radio',
  2840. 'name' => 'Model[field]',
  2841. 'value' => '0',
  2842. 'id' => 'prefix-model-field-0',
  2843. ]],
  2844. 'option A',
  2845. '/label',
  2846. ];
  2847. $this->assertHtml($expected, $result);
  2848. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2849. $expected = [
  2850. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2851. 'label' => ['for' => 'prefix-model-field-0'],
  2852. ['input' => [
  2853. 'type' => 'radio',
  2854. 'name' => 'Model[field]',
  2855. 'value' => '0',
  2856. 'id' => 'prefix-model-field-0',
  2857. ]],
  2858. 'option A',
  2859. '/label',
  2860. ];
  2861. $this->assertHtml($expected, $result);
  2862. $result = $this->Form->select(
  2863. 'Model.multi_field',
  2864. ['first'],
  2865. ['multiple' => 'checkbox']
  2866. );
  2867. $expected = [
  2868. 'input' => [
  2869. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  2870. ],
  2871. ['div' => ['class' => 'checkbox']],
  2872. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2873. ['input' => [
  2874. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2875. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2876. ]],
  2877. 'first',
  2878. '/label',
  2879. '/div',
  2880. ];
  2881. $this->assertHtml($expected, $result);
  2882. $this->Form->end();
  2883. $result = $this->Form->control('field');
  2884. $expected = [
  2885. 'div' => ['class' => 'input text'],
  2886. 'label' => ['for' => 'field'],
  2887. 'Field',
  2888. '/label',
  2889. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2890. '/div',
  2891. ];
  2892. $this->assertHtml($expected, $result);
  2893. }
  2894. /**
  2895. * testControlZero method
  2896. *
  2897. * Test that inputs with 0 can be created.
  2898. */
  2899. public function testControlZero(): void
  2900. {
  2901. $this->getTableLocator()->get('Contacts', [
  2902. 'className' => ContactsTable::class,
  2903. ]);
  2904. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2905. $result = $this->Form->control('0');
  2906. $expected = [
  2907. 'div' => ['class' => 'input text'],
  2908. 'label' => ['for' => '0'], '/label',
  2909. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2910. '/div',
  2911. ];
  2912. $this->assertHtml($expected, $result);
  2913. }
  2914. /**
  2915. * testControlCheckbox method
  2916. *
  2917. * Test control() with checkbox creation.
  2918. */
  2919. public function testControlCheckbox(): void
  2920. {
  2921. $articles = $this->getTableLocator()->get('Articles');
  2922. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2923. $article = $articles->newEmptyEntity();
  2924. $this->Form->create($article);
  2925. $result = $this->Form->control('Articles.active');
  2926. $expected = [
  2927. 'div' => ['class' => 'input checkbox'],
  2928. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2929. 'label' => ['for' => 'articles-active'],
  2930. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2931. 'Active',
  2932. '/label',
  2933. '/div',
  2934. ];
  2935. $this->assertHtml($expected, $result);
  2936. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2937. $expected = [
  2938. 'div' => ['class' => 'input checkbox'],
  2939. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2940. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2941. '/div',
  2942. ];
  2943. $this->assertHtml($expected, $result);
  2944. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2945. $expected = [
  2946. 'div' => ['class' => 'input checkbox'],
  2947. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2948. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2949. '/div',
  2950. ];
  2951. $this->assertHtml($expected, $result);
  2952. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2953. $expected = [
  2954. 'div' => ['class' => 'input checkbox'],
  2955. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2956. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2957. '/div',
  2958. ];
  2959. $this->assertHtml($expected, $result);
  2960. $result = $this->Form->control('Articles.disabled', [
  2961. 'label' => 'Disabled',
  2962. 'type' => 'checkbox',
  2963. 'data-foo' => 'disabled',
  2964. ]);
  2965. $expected = [
  2966. 'div' => ['class' => 'input checkbox'],
  2967. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2968. 'label' => ['for' => 'articles-disabled'],
  2969. ['input' => [
  2970. 'type' => 'checkbox',
  2971. 'name' => 'Articles[disabled]',
  2972. 'value' => '1',
  2973. 'id' => 'articles-disabled',
  2974. 'data-foo' => 'disabled',
  2975. ]],
  2976. 'Disabled',
  2977. '/label',
  2978. '/div',
  2979. ];
  2980. $this->assertHtml($expected, $result);
  2981. $result = $this->Form->control('Articles.confirm', [
  2982. 'label' => 'Confirm <b>me</b>!',
  2983. 'type' => 'checkbox',
  2984. 'escape' => false,
  2985. ]);
  2986. $expected = [
  2987. 'div' => ['class' => 'input checkbox'],
  2988. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2989. 'label' => ['for' => 'articles-confirm'],
  2990. ['input' => [
  2991. 'type' => 'checkbox',
  2992. 'name' => 'Articles[confirm]',
  2993. 'value' => '1',
  2994. 'id' => 'articles-confirm',
  2995. ]],
  2996. 'Confirm <b>me</b>!',
  2997. '/label',
  2998. '/div',
  2999. ];
  3000. $this->assertHtml($expected, $result);
  3001. }
  3002. /**
  3003. * testControlHidden method
  3004. *
  3005. * Test that control() does not create wrapping div and label tag for hidden fields.
  3006. */
  3007. public function testControlHidden(): void
  3008. {
  3009. $this->getTableLocator()->get('ValidateUsers', [
  3010. 'className' => ValidateUsersTable::class,
  3011. ]);
  3012. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3013. $result = $this->Form->control('ValidateUser.id');
  3014. $expected = [
  3015. 'input' => ['name', 'type' => 'hidden', 'id'],
  3016. ];
  3017. $this->assertHtml($expected, $result);
  3018. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3019. $expected = [
  3020. 'input' => ['name', 'type' => 'hidden', 'id'],
  3021. ];
  3022. $this->assertHtml($expected, $result);
  3023. }
  3024. /**
  3025. * testControlDatetime method
  3026. *
  3027. * Test form->control() with datetime.
  3028. */
  3029. public function testControlDatetime(): void
  3030. {
  3031. $result = $this->Form->control('prueba', [
  3032. 'type' => 'datetime',
  3033. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3034. ]);
  3035. $expected = [
  3036. 'div' => ['class' => 'input datetime'],
  3037. 'label' => ['for' => 'prueba'],
  3038. 'Prueba',
  3039. '/label',
  3040. 'input' => [
  3041. 'name' => 'prueba',
  3042. 'id' => 'prueba',
  3043. 'type' => 'datetime-local',
  3044. 'value' => '2019-09-27T02:52:43',
  3045. 'step' => '1',
  3046. ],
  3047. '/div',
  3048. ];
  3049. $this->assertHtml($expected, $result);
  3050. }
  3051. /**
  3052. * testControlDatetimeIdPrefix method
  3053. *
  3054. * Test form->control() with datetime with id prefix.
  3055. */
  3056. public function testControlDatetimeIdPrefix(): void
  3057. {
  3058. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3059. $result = $this->Form->control('prueba', [
  3060. 'type' => 'datetime',
  3061. ]);
  3062. $expected = [
  3063. 'div' => ['class' => 'input datetime'],
  3064. 'label' => ['for' => 'prefix-prueba'],
  3065. 'Prueba',
  3066. '/label',
  3067. 'input' => [
  3068. 'name' => 'prueba',
  3069. 'id' => 'prefix-prueba',
  3070. 'type' => 'datetime-local',
  3071. 'value' => '',
  3072. 'step' => '1',
  3073. ],
  3074. '/div',
  3075. ];
  3076. $this->assertHtml($expected, $result);
  3077. }
  3078. /**
  3079. * testControlDatetimeStep method
  3080. *
  3081. * Test form->control() with datetime with custom step size.
  3082. */
  3083. public function testControlDatetimeStep(): void
  3084. {
  3085. $result = $this->Form->control('prueba', [
  3086. 'type' => 'datetime',
  3087. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3088. 'step' => '0.5',
  3089. ]);
  3090. $expected = [
  3091. 'div' => ['class' => 'input datetime'],
  3092. 'label' => ['for' => 'prueba'],
  3093. 'Prueba',
  3094. '/label',
  3095. 'input' => [
  3096. 'name' => 'prueba',
  3097. 'id' => 'prueba',
  3098. 'type' => 'datetime-local',
  3099. 'value' => '2019-09-27T02:52:43.000',
  3100. 'step' => '0.5',
  3101. ],
  3102. '/div',
  3103. ];
  3104. $this->assertHtml($expected, $result);
  3105. }
  3106. /**
  3107. * testControlCheckboxWithDisabledElements method
  3108. *
  3109. * Test generating checkboxes with disabled elements.
  3110. */
  3111. public function testControlCheckboxWithDisabledElements(): void
  3112. {
  3113. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3114. $result = $this->Form->control('Contact.multiple', [
  3115. 'multiple' => 'checkbox',
  3116. 'disabled' => 'disabled',
  3117. 'options' => $options,
  3118. ]);
  3119. $expected = [
  3120. ['div' => ['class' => 'input select']],
  3121. ['label' => ['for' => 'contact-multiple']],
  3122. 'Multiple',
  3123. '/label',
  3124. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3125. ['div' => ['class' => 'checkbox']],
  3126. ['label' => ['for' => 'contact-multiple-1']],
  3127. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3128. 'One',
  3129. '/label',
  3130. '/div',
  3131. ['div' => ['class' => 'checkbox']],
  3132. ['label' => ['for' => 'contact-multiple-2']],
  3133. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3134. 'Two',
  3135. '/label',
  3136. '/div',
  3137. ['div' => ['class' => 'checkbox']],
  3138. ['label' => ['for' => 'contact-multiple-3']],
  3139. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3140. 'Three',
  3141. '/label',
  3142. '/div',
  3143. '/div',
  3144. ];
  3145. $this->assertHtml($expected, $result);
  3146. // make sure 50 does only disable 50, and not 50f5c0cf
  3147. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3148. $disabled = [50];
  3149. $expected = [
  3150. ['div' => ['class' => 'input select']],
  3151. ['label' => ['for' => 'contact-multiple']],
  3152. 'Multiple',
  3153. '/label',
  3154. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3155. ['div' => ['class' => 'checkbox']],
  3156. ['label' => ['for' => 'contact-multiple-50']],
  3157. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3158. 'Fifty',
  3159. '/label',
  3160. '/div',
  3161. ['div' => ['class' => 'checkbox']],
  3162. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3163. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3164. 'Stringy',
  3165. '/label',
  3166. '/div',
  3167. '/div',
  3168. ];
  3169. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3170. $this->assertHtml($expected, $result);
  3171. }
  3172. /**
  3173. * testControlWithLeadingInteger method
  3174. *
  3175. * Test input name with leading integer, ensure attributes are generated correctly.
  3176. */
  3177. public function testControlWithLeadingInteger(): void
  3178. {
  3179. $result = $this->Form->text('0.Node.title');
  3180. $expected = [
  3181. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3182. ];
  3183. $this->assertHtml($expected, $result);
  3184. }
  3185. /**
  3186. * testControlSelectType method
  3187. *
  3188. * Test form->control() with select type inputs.
  3189. */
  3190. public function testControlSelectType(): void
  3191. {
  3192. $result = $this->Form->control(
  3193. 'email',
  3194. [
  3195. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3196. );
  3197. $expected = [
  3198. 'div' => ['class' => 'input select'],
  3199. 'label' => ['for' => 'email'],
  3200. 'Email',
  3201. '/label',
  3202. ['select' => ['name' => 'email', 'id' => 'email']],
  3203. ['option' => ['value' => '']],
  3204. '/option',
  3205. ['option' => ['value' => 'è']],
  3206. 'Firést',
  3207. '/option',
  3208. ['option' => ['value' => 'é']],
  3209. 'Secoènd',
  3210. '/option',
  3211. '/select',
  3212. '/div',
  3213. ];
  3214. $this->assertHtml($expected, $result);
  3215. $result = $this->Form->control(
  3216. 'email',
  3217. [
  3218. 'options' => ['First', 'Second'], 'empty' => true]
  3219. );
  3220. $expected = [
  3221. 'div' => ['class' => 'input select'],
  3222. 'label' => ['for' => 'email'],
  3223. 'Email',
  3224. '/label',
  3225. ['select' => ['name' => 'email', 'id' => 'email']],
  3226. ['option' => ['value' => '']],
  3227. '/option',
  3228. ['option' => ['value' => '0']],
  3229. 'First',
  3230. '/option',
  3231. ['option' => ['value' => '1']],
  3232. 'Second',
  3233. '/option',
  3234. '/select',
  3235. '/div',
  3236. ];
  3237. $this->assertHtml($expected, $result);
  3238. $result = $this->Form->control('email', [
  3239. 'type' => 'select',
  3240. 'options' => new ArrayObject(['First', 'Second']),
  3241. 'empty' => true,
  3242. ]);
  3243. $this->assertHtml($expected, $result);
  3244. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3245. $this->View->setRequest(
  3246. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3247. );
  3248. $this->Form->create();
  3249. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3250. $expected = [
  3251. 'div' => ['class' => 'input select'],
  3252. 'label' => ['for' => 'model-user-id'],
  3253. 'User',
  3254. '/label',
  3255. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3256. ['option' => ['value' => '']],
  3257. '/option',
  3258. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3259. 'good',
  3260. '/option',
  3261. ['option' => ['value' => 'other']],
  3262. 'bad',
  3263. '/option',
  3264. '/select',
  3265. '/div',
  3266. ];
  3267. $this->assertHtml($expected, $result);
  3268. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3269. $this->View->setRequest(
  3270. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3271. );
  3272. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3273. $expected = [
  3274. 'div' => ['class' => 'input select'],
  3275. 'label' => ['for' => 'thing-user-id'],
  3276. 'User',
  3277. '/label',
  3278. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3279. ['option' => ['value' => '']],
  3280. 'Some Empty',
  3281. '/option',
  3282. ['option' => ['value' => 'value']],
  3283. 'good',
  3284. '/option',
  3285. ['option' => ['value' => 'other']],
  3286. 'bad',
  3287. '/option',
  3288. '/select',
  3289. '/div',
  3290. ];
  3291. $this->assertHtml($expected, $result);
  3292. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3293. $this->View->setRequest(
  3294. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3295. );
  3296. $this->Form->create();
  3297. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3298. $expected = [
  3299. 'div' => ['class' => 'input select'],
  3300. 'label' => ['for' => 'thing-user-id'],
  3301. 'User',
  3302. '/label',
  3303. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3304. ['option' => ['value' => '']],
  3305. 'Some Empty',
  3306. '/option',
  3307. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3308. 'good',
  3309. '/option',
  3310. ['option' => ['value' => 'other']],
  3311. 'bad',
  3312. '/option',
  3313. '/select',
  3314. '/div',
  3315. ];
  3316. $this->assertHtml($expected, $result);
  3317. $result = $this->Form->control('Publisher.id', [
  3318. 'label' => 'Publisher',
  3319. 'type' => 'select',
  3320. 'multiple' => 'checkbox',
  3321. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3322. ]);
  3323. $expected = [
  3324. ['div' => ['class' => 'input select']],
  3325. ['label' => ['for' => 'publisher-id']],
  3326. 'Publisher',
  3327. '/label',
  3328. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3329. ['div' => ['class' => 'checkbox']],
  3330. ['label' => ['for' => 'publisher-id-value-1']],
  3331. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3332. 'Label 1',
  3333. '/label',
  3334. '/div',
  3335. ['div' => ['class' => 'checkbox']],
  3336. ['label' => ['for' => 'publisher-id-value-2']],
  3337. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3338. 'Label 2',
  3339. '/label',
  3340. '/div',
  3341. '/div',
  3342. ];
  3343. $this->assertHtml($expected, $result);
  3344. }
  3345. /**
  3346. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3347. *
  3348. * Test that control() and a non standard primary key makes a hidden input by default.
  3349. */
  3350. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3351. {
  3352. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3353. $this->Form->create($this->article);
  3354. $result = $this->Form->control('title');
  3355. $expected = [
  3356. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3357. ];
  3358. $this->assertHtml($expected, $result);
  3359. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3360. $this->Form->create($this->article);
  3361. $result = $this->Form->control('title');
  3362. $expected = [
  3363. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3364. ];
  3365. $this->assertHtml($expected, $result);
  3366. $result = $this->Form->control('body');
  3367. $expected = [
  3368. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3369. ];
  3370. $this->assertHtml($expected, $result);
  3371. }
  3372. /**
  3373. * testControlOverridingMagicSelectType method
  3374. *
  3375. * Test that overriding the magic select type widget is possible.
  3376. */
  3377. public function testControlOverridingMagicSelectType(): void
  3378. {
  3379. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3380. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3381. $expected = [
  3382. 'div' => ['class' => 'input text'],
  3383. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3384. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3385. '/div',
  3386. ];
  3387. $this->assertHtml($expected, $result);
  3388. //Check that magic types still work for plural/singular vars
  3389. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3390. $result = $this->Form->control('Model.type');
  3391. $expected = [
  3392. 'div' => ['class' => 'input select'],
  3393. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3394. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3395. ['option' => ['value' => 'value']], 'good', '/option',
  3396. ['option' => ['value' => 'other']], 'bad', '/option',
  3397. '/select',
  3398. '/div',
  3399. ];
  3400. $this->assertHtml($expected, $result);
  3401. }
  3402. /**
  3403. * testControlMagicTypeDoesNotOverride method
  3404. *
  3405. * Test that inferred types do not override developer input.
  3406. */
  3407. public function testControlMagicTypeDoesNotOverride(): void
  3408. {
  3409. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3410. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3411. $expected = [
  3412. 'div' => ['class' => 'input checkbox'],
  3413. ['input' => [
  3414. 'type' => 'hidden',
  3415. 'name' => 'Model[user]',
  3416. 'value' => 0,
  3417. ]],
  3418. 'label' => ['for' => 'model-user'],
  3419. ['input' => [
  3420. 'name' => 'Model[user]',
  3421. 'type' => 'checkbox',
  3422. 'id' => 'model-user',
  3423. 'value' => 1,
  3424. ]],
  3425. 'User',
  3426. '/label',
  3427. '/div',
  3428. ];
  3429. $this->assertHtml($expected, $result);
  3430. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3431. $options = [
  3432. 1 => 'blue',
  3433. 2 => 'red',
  3434. ];
  3435. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3436. $expected = [
  3437. 'div' => ['class' => 'input select'],
  3438. 'label' => ['for' => 'tags-ids'],
  3439. 'Tags',
  3440. '/label',
  3441. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3442. ['div' => ['class' => 'checkbox']],
  3443. ['label' => ['for' => 'tags-ids-1']],
  3444. ['input' => [
  3445. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3446. 'value' => '1', 'name' => 'tags[_ids][]',
  3447. ]],
  3448. 'blue',
  3449. '/label',
  3450. '/div',
  3451. ['div' => ['class' => 'checkbox']],
  3452. ['label' => ['for' => 'tags-ids-2']],
  3453. ['input' => [
  3454. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3455. 'value' => '2', 'name' => 'tags[_ids][]',
  3456. ]],
  3457. 'red',
  3458. '/label',
  3459. '/div',
  3460. '/div',
  3461. ];
  3462. $this->assertHtml($expected, $result);
  3463. }
  3464. /**
  3465. * testControlMagicSelectForTypeNumber method
  3466. *
  3467. * Test that magic control() selects are created for type=number.
  3468. */
  3469. public function testControlMagicSelectForTypeNumber(): void
  3470. {
  3471. $this->getTableLocator()->get('ValidateUsers', [
  3472. 'className' => ValidateUsersTable::class,
  3473. ]);
  3474. $entity = new Entity(['balance' => 1]);
  3475. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3476. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3477. $result = $this->Form->control('balance');
  3478. $expected = [
  3479. 'div' => ['class' => 'input select'],
  3480. 'label' => ['for' => 'balance'],
  3481. 'Balance',
  3482. '/label',
  3483. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3484. ['option' => ['value' => '0']],
  3485. 'nothing',
  3486. '/option',
  3487. ['option' => ['value' => '1', 'selected' => 'selected']],
  3488. 'some',
  3489. '/option',
  3490. ['option' => ['value' => '100']],
  3491. 'a lot',
  3492. '/option',
  3493. '/select',
  3494. '/div',
  3495. ];
  3496. $this->assertHtml($expected, $result);
  3497. }
  3498. /**
  3499. * testInvalidControlTypeOption method
  3500. *
  3501. * Test invalid 'input' type option to control() function.
  3502. */
  3503. public function testInvalidControlTypeOption(): void
  3504. {
  3505. $this->expectException(RuntimeException::class);
  3506. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3507. $this->Form->control('text', ['type' => 'input']);
  3508. }
  3509. /**
  3510. * testControlMagicSelectChangeToRadio method
  3511. *
  3512. * Test that magic control() selects can easily be converted into radio types without error.
  3513. */
  3514. public function testControlMagicSelectChangeToRadio(): void
  3515. {
  3516. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3517. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3518. $this->assertStringContainsString('input type="radio"', $result);
  3519. }
  3520. /**
  3521. * testFormControlSubmit method
  3522. *
  3523. * Test correct results for form::control() and type submit.
  3524. */
  3525. public function testFormControlSubmit(): void
  3526. {
  3527. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3528. $expected = [
  3529. 'div' => ['class' => 'submit'],
  3530. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3531. '/div',
  3532. ];
  3533. $this->assertHtml($expected, $result);
  3534. }
  3535. /**
  3536. * testFormControls method
  3537. *
  3538. * Test correct results from Form::controls().
  3539. */
  3540. public function testFormControlsLegendFieldset(): void
  3541. {
  3542. $this->Form->create($this->article);
  3543. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3544. $expected = [
  3545. '<fieldset',
  3546. '<legend',
  3547. 'The Legend',
  3548. '/legend',
  3549. '*/fieldset',
  3550. ];
  3551. $this->assertHtml($expected, $result);
  3552. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3553. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3554. $this->assertStringContainsString('<fieldset>', $result);
  3555. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3556. $this->assertStringNotContainsString('<legend>', $result);
  3557. $this->assertStringNotContainsString('<fieldset>', $result);
  3558. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3559. $this->assertStringNotContainsString('<legend>', $result);
  3560. $this->assertStringNotContainsString('<fieldset>', $result);
  3561. $this->Form->create($this->article);
  3562. $this->View->setRequest($this->View->getRequest()
  3563. ->withParam('prefix', 'admin')
  3564. ->withParam('action', 'admin_edit')
  3565. ->withParam('controller', 'articles'));
  3566. $result = $this->Form->allControls();
  3567. $expected = [
  3568. '<fieldset',
  3569. '<legend',
  3570. 'New Article',
  3571. '/legend',
  3572. '*/fieldset',
  3573. ];
  3574. $this->assertHtml($expected, $result);
  3575. $this->Form->create($this->article);
  3576. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3577. $expected = [
  3578. '<fieldset',
  3579. '<legend',
  3580. 'The Legend',
  3581. '/legend',
  3582. '*/fieldset',
  3583. ];
  3584. $this->assertHtml($expected, $result);
  3585. $this->Form->create($this->article);
  3586. $result = $this->Form->allControls([], [
  3587. 'fieldset' => [
  3588. 'class' => 'some-class some-other-class',
  3589. 'disabled' => true,
  3590. 'data-param' => 'a-param',
  3591. ],
  3592. 'legend' => 'The Legend',
  3593. ]);
  3594. $expected = [
  3595. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3596. '<legend',
  3597. 'The Legend',
  3598. '/legend',
  3599. '*/fieldset',
  3600. ];
  3601. $this->assertHtml($expected, $result);
  3602. }
  3603. /**
  3604. * testFormControls method
  3605. *
  3606. * Test the controls() method.
  3607. */
  3608. public function testFormControls(): void
  3609. {
  3610. $this->Form->create($this->article);
  3611. $result = $this->Form->allControls();
  3612. $expected = [
  3613. '<fieldset',
  3614. '<legend', 'New Article', '/legend',
  3615. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3616. ['div' => ['class' => 'input select required']],
  3617. '*/div',
  3618. ['div' => ['class' => 'input text required']],
  3619. '*/div',
  3620. ['div' => ['class' => 'input text']],
  3621. '*/div',
  3622. ['div' => ['class' => 'input text']],
  3623. '*/div',
  3624. '/fieldset',
  3625. ];
  3626. $this->assertHtml($expected, $result);
  3627. $result = $this->Form->allControls([
  3628. 'published' => ['type' => 'boolean'],
  3629. ]);
  3630. $expected = [
  3631. '<fieldset',
  3632. '<legend', 'New Article', '/legend',
  3633. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3634. ['div' => ['class' => 'input select required']],
  3635. '*/div',
  3636. ['div' => ['class' => 'input text required']],
  3637. '*/div',
  3638. ['div' => ['class' => 'input text']],
  3639. '*/div',
  3640. ['div' => ['class' => 'input boolean']],
  3641. '*/div',
  3642. '/fieldset',
  3643. ];
  3644. $this->assertHtml($expected, $result);
  3645. $this->Form->create($this->article);
  3646. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3647. $expected = [
  3648. 'fieldset' => [],
  3649. 'legend' => [],
  3650. 'Hello',
  3651. '/legend',
  3652. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3653. ['div' => ['class' => 'input select required']],
  3654. '*/div',
  3655. ['div' => ['class' => 'input text required']],
  3656. '*/div',
  3657. ['div' => ['class' => 'input text']],
  3658. '*/div',
  3659. ['div' => ['class' => 'input text']],
  3660. '*/div',
  3661. '/fieldset',
  3662. ];
  3663. $this->assertHtml($expected, $result);
  3664. $this->Form->create();
  3665. $expected = [
  3666. 'fieldset' => [],
  3667. ['div' => ['class' => 'input text']],
  3668. 'label' => ['for' => 'foo'],
  3669. 'Foo',
  3670. '/label',
  3671. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3672. '*/div',
  3673. '/fieldset',
  3674. ];
  3675. $result = $this->Form->allControls(
  3676. ['foo' => ['type' => 'text']],
  3677. ['legend' => false]
  3678. );
  3679. $this->assertHtml($expected, $result);
  3680. }
  3681. /**
  3682. * testFormControlsBlacklist method
  3683. */
  3684. public function testFormControlsBlacklist(): void
  3685. {
  3686. $this->Form->create($this->article);
  3687. $result = $this->Form->allControls([
  3688. 'id' => false,
  3689. ]);
  3690. $expected = [
  3691. '<fieldset',
  3692. '<legend', 'New Article', '/legend',
  3693. ['div' => ['class' => 'input select required']],
  3694. '*/div',
  3695. ['div' => ['class' => 'input text required']],
  3696. '*/div',
  3697. ['div' => ['class' => 'input text']],
  3698. '*/div',
  3699. ['div' => ['class' => 'input text']],
  3700. '*/div',
  3701. '/fieldset',
  3702. ];
  3703. $this->assertHtml($expected, $result);
  3704. $this->Form->create($this->article);
  3705. $result = $this->Form->allControls([
  3706. 'id' => [],
  3707. ]);
  3708. $expected = [
  3709. '<fieldset',
  3710. '<legend', 'New Article', '/legend',
  3711. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3712. ['div' => ['class' => 'input select required']],
  3713. '*/div',
  3714. ['div' => ['class' => 'input text required']],
  3715. '*/div',
  3716. ['div' => ['class' => 'input text']],
  3717. '*/div',
  3718. ['div' => ['class' => 'input text']],
  3719. '*/div',
  3720. '/fieldset',
  3721. ];
  3722. $this->assertHtml($expected, $result, true);
  3723. }
  3724. /**
  3725. * testSelectAsCheckbox method
  3726. *
  3727. * Test multi-select widget with checkbox formatting.
  3728. */
  3729. public function testSelectAsCheckbox(): void
  3730. {
  3731. $result = $this->Form->select(
  3732. 'Model.multi_field',
  3733. ['first', 'second', 'third'],
  3734. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3735. );
  3736. $expected = [
  3737. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3738. ['div' => ['class' => 'checkbox']],
  3739. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3740. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3741. 'first',
  3742. '/label',
  3743. '/div',
  3744. ['div' => ['class' => 'checkbox']],
  3745. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3746. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3747. 'second',
  3748. '/label',
  3749. '/div',
  3750. ['div' => ['class' => 'checkbox']],
  3751. ['label' => ['for' => 'model-multi-field-2']],
  3752. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3753. 'third',
  3754. '/label',
  3755. '/div',
  3756. ];
  3757. $this->assertHtml($expected, $result);
  3758. $result = $this->Form->select(
  3759. 'Model.multi_field',
  3760. ['1/2' => 'half'],
  3761. ['multiple' => 'checkbox']
  3762. );
  3763. $expected = [
  3764. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3765. ['div' => ['class' => 'checkbox']],
  3766. ['label' => ['for' => 'model-multi-field-1-2']],
  3767. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3768. 'half',
  3769. '/label',
  3770. '/div',
  3771. ];
  3772. $this->assertHtml($expected, $result);
  3773. }
  3774. /**
  3775. * testLabel method
  3776. *
  3777. * Test label generation.
  3778. */
  3779. public function testLabel(): void
  3780. {
  3781. $result = $this->Form->label('Person.name');
  3782. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3783. $this->assertHtml($expected, $result);
  3784. $result = $this->Form->label('Person.name');
  3785. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3786. $this->assertHtml($expected, $result);
  3787. $result = $this->Form->label('Person.first_name');
  3788. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3789. $this->assertHtml($expected, $result);
  3790. $result = $this->Form->label('Person.first_name', 'Your first name');
  3791. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3792. $this->assertHtml($expected, $result);
  3793. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3794. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3795. $this->assertHtml($expected, $result);
  3796. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3797. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3798. $this->assertHtml($expected, $result);
  3799. $result = $this->Form->label('Person.first_name', '');
  3800. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3801. $this->assertHtml($expected, $result);
  3802. $result = $this->Form->label('Person.2.name', '');
  3803. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3804. $this->assertHtml($expected, $result);
  3805. }
  3806. /**
  3807. * testLabelContainControl method
  3808. *
  3809. * Test that label() can accept an input with the correct template vars.
  3810. */
  3811. public function testLabelContainControl(): void
  3812. {
  3813. $this->Form->setTemplates([
  3814. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3815. ]);
  3816. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3817. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3818. ]);
  3819. $expected = [
  3820. 'label' => ['for' => 'person-accept-terms'],
  3821. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3822. 'Accept',
  3823. '/label',
  3824. ];
  3825. $this->assertHtml($expected, $result);
  3826. }
  3827. /**
  3828. * testTextbox method
  3829. *
  3830. * Test textbox element generation.
  3831. */
  3832. public function testTextbox(): void
  3833. {
  3834. $result = $this->Form->text('Model.field');
  3835. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3836. $this->assertHtml($expected, $result);
  3837. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3838. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3839. $this->assertHtml($expected, $result);
  3840. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3841. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3842. $this->assertHtml($expected, $result);
  3843. }
  3844. /**
  3845. * testTextBoxDataAndError method
  3846. *
  3847. * Test that text() hooks up with request data and error fields.
  3848. */
  3849. public function testTextBoxDataAndError(): void
  3850. {
  3851. $this->article['errors'] = [
  3852. 'Contact' => ['text' => 'wrong'],
  3853. ];
  3854. $this->View->setRequest($this->View->getRequest()
  3855. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3856. ->withData('Contact.text', 'test'));
  3857. $this->Form->create($this->article);
  3858. $result = $this->Form->text('Model.text');
  3859. $expected = [
  3860. 'input' => [
  3861. 'type' => 'text',
  3862. 'name' => 'Model[text]',
  3863. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3864. ],
  3865. ];
  3866. $this->assertHtml($expected, $result);
  3867. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3868. $expected = [
  3869. 'input' => [
  3870. 'type' => 'text',
  3871. 'name' => 'Contact[text]',
  3872. 'value' => 'test',
  3873. 'id' => 'theID',
  3874. 'class' => 'form-error',
  3875. ],
  3876. ];
  3877. $this->assertHtml($expected, $result);
  3878. }
  3879. /**
  3880. * testDefaultValue method
  3881. *
  3882. * Test default value setting.
  3883. */
  3884. public function testTextDefaultValue(): void
  3885. {
  3886. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3887. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3888. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3889. $this->assertHtml($expected, $result);
  3890. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3891. $this->Form->create();
  3892. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3893. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3894. $this->assertHtml($expected, $result);
  3895. $Articles = $this->getTableLocator()->get('Articles');
  3896. $title = $Articles->getSchema()->getColumn('title');
  3897. $Articles->getSchema()->addColumn(
  3898. 'title',
  3899. ['default' => 'default title', 'length' => 255] + $title
  3900. );
  3901. $entity = $Articles->newEmptyEntity();
  3902. $this->Form->create($entity);
  3903. // Get default value from schema
  3904. $result = $this->Form->text('title');
  3905. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3906. $this->assertHtml($expected, $result);
  3907. // Don't get value from schema
  3908. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3909. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3910. $this->assertHtml($expected, $result);
  3911. // Custom default value overrides default value from schema
  3912. $result = $this->Form->text('title', ['default' => 'override default']);
  3913. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3914. $this->assertHtml($expected, $result);
  3915. // Default value from schema is used only for new entities.
  3916. $entity->setNew(false);
  3917. $result = $this->Form->text('title');
  3918. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3919. $this->assertHtml($expected, $result);
  3920. }
  3921. /**
  3922. * testError method
  3923. *
  3924. * Test field error generation.
  3925. */
  3926. public function testError(): void
  3927. {
  3928. $this->article['errors'] = [
  3929. 'Article' => ['field' => 'email'],
  3930. ];
  3931. $this->Form->create($this->article);
  3932. $result = $this->Form->error('Article.field');
  3933. $expected = [
  3934. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3935. 'email',
  3936. '/div',
  3937. ];
  3938. $this->assertHtml($expected, $result);
  3939. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3940. $expected = [
  3941. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3942. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3943. '/div',
  3944. ];
  3945. $this->assertHtml($expected, $result);
  3946. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3947. $expected = [
  3948. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3949. '<strong', 'Badness!', '/strong',
  3950. '/div',
  3951. ];
  3952. $this->assertHtml($expected, $result);
  3953. }
  3954. /**
  3955. * testErrorRuleName method
  3956. *
  3957. * Test error translation can use rule names for translating.
  3958. */
  3959. public function testErrorRuleName(): void
  3960. {
  3961. $this->article['errors'] = [
  3962. 'Article' => [
  3963. 'field' => ['email' => 'Your email was not good'],
  3964. ],
  3965. ];
  3966. $this->Form->create($this->article);
  3967. $result = $this->Form->error('Article.field');
  3968. $expected = [
  3969. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3970. 'Your email was not good',
  3971. '/div',
  3972. ];
  3973. $this->assertHtml($expected, $result);
  3974. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3975. $expected = [
  3976. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3977. 'Email in use',
  3978. '/div',
  3979. ];
  3980. $this->assertHtml($expected, $result);
  3981. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3982. $expected = [
  3983. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3984. 'Email in use',
  3985. '/div',
  3986. ];
  3987. $this->assertHtml($expected, $result);
  3988. $result = $this->Form->error('Article.field', [
  3989. 'email' => 'Key is preferred',
  3990. 'Your email was not good' => 'Email in use',
  3991. ]);
  3992. $expected = [
  3993. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3994. 'Key is preferred',
  3995. '/div',
  3996. ];
  3997. $this->assertHtml($expected, $result);
  3998. }
  3999. /**
  4000. * testErrorMessages method
  4001. *
  4002. * Test error with nested lists.
  4003. */
  4004. public function testErrorMessages(): void
  4005. {
  4006. $this->article['errors'] = [
  4007. 'Article' => ['field' => 'email'],
  4008. ];
  4009. $this->Form->create($this->article);
  4010. $result = $this->Form->error('Article.field', [
  4011. 'email' => 'No good!',
  4012. ]);
  4013. $expected = [
  4014. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4015. 'No good!',
  4016. '/div',
  4017. ];
  4018. $this->assertHtml($expected, $result);
  4019. }
  4020. /**
  4021. * testErrorMultipleMessages method
  4022. *
  4023. * Test error() with multiple messages.
  4024. */
  4025. public function testErrorMultipleMessages(): void
  4026. {
  4027. $this->article['errors'] = [
  4028. 'field' => ['notBlank', 'email', 'Something else'],
  4029. ];
  4030. $this->Form->create($this->article);
  4031. $result = $this->Form->error('field', [
  4032. 'notBlank' => 'Cannot be empty',
  4033. 'email' => 'No good!',
  4034. ]);
  4035. $expected = [
  4036. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4037. 'ul' => [],
  4038. '<li', 'Cannot be empty', '/li',
  4039. '<li', 'No good!', '/li',
  4040. '<li', 'Something else', '/li',
  4041. '/ul',
  4042. '/div',
  4043. ];
  4044. $this->assertHtml($expected, $result);
  4045. }
  4046. /**
  4047. * testPassword method
  4048. *
  4049. * Test password element generation.
  4050. */
  4051. public function testPassword(): void
  4052. {
  4053. $this->article['errors'] = [
  4054. 'Contact' => [
  4055. 'passwd' => 1,
  4056. ],
  4057. ];
  4058. $this->Form->create($this->article);
  4059. $result = $this->Form->password('Contact.field');
  4060. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4061. $this->assertHtml($expected, $result);
  4062. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4063. $this->Form->create($this->article);
  4064. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4065. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4066. $this->assertHtml($expected, $result);
  4067. }
  4068. /**
  4069. * testRadio method
  4070. *
  4071. * Test radio element set generation.
  4072. */
  4073. public function testRadio(): void
  4074. {
  4075. $result = $this->Form->radio('Model.field', ['option A']);
  4076. $expected = [
  4077. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4078. 'label' => ['for' => 'model-field-0'],
  4079. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4080. 'option A',
  4081. '/label',
  4082. ];
  4083. $this->assertHtml($expected, $result);
  4084. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4085. $this->assertHtml($expected, $result);
  4086. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4087. $expected = [
  4088. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4089. ['label' => ['for' => 'model-field-0']],
  4090. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4091. 'option A',
  4092. '/label',
  4093. ['label' => ['for' => 'model-field-1']],
  4094. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4095. 'option B',
  4096. '/label',
  4097. ];
  4098. $this->assertHtml($expected, $result);
  4099. $result = $this->Form->radio(
  4100. 'Employee.gender',
  4101. ['male' => 'Male', 'female' => 'Female'],
  4102. ['form' => 'my-form']
  4103. );
  4104. $expected = [
  4105. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4106. ['label' => ['for' => 'employee-gender-male']],
  4107. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4108. 'Male',
  4109. '/label',
  4110. ['label' => ['for' => 'employee-gender-female']],
  4111. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4112. 'Female',
  4113. '/label',
  4114. ];
  4115. $this->assertHtml($expected, $result);
  4116. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4117. $expected = [
  4118. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4119. ['label' => ['for' => 'model-custom-0']],
  4120. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4121. 'option A',
  4122. '/label',
  4123. ['label' => ['for' => 'model-custom-1']],
  4124. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4125. 'option B',
  4126. '/label',
  4127. ];
  4128. $this->assertHtml($expected, $result);
  4129. $result = $this->Form->radio(
  4130. 'Employee.gender',
  4131. [
  4132. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4133. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4134. ]
  4135. );
  4136. $expected = [
  4137. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4138. ['label' => ['for' => 'employee-gender-male']],
  4139. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4140. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4141. 'Male',
  4142. '/label',
  4143. ['label' => ['for' => 'employee-gender-female']],
  4144. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4145. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4146. 'Female',
  4147. '/label',
  4148. ];
  4149. $this->assertHtml($expected, $result);
  4150. }
  4151. /**
  4152. * Test radio with complex options and empty disabled data.
  4153. */
  4154. public function testRadioComplexDisabled(): void
  4155. {
  4156. $options = [
  4157. ['value' => 'r', 'text' => 'red'],
  4158. ['value' => 'b', 'text' => 'blue'],
  4159. ];
  4160. $attrs = ['disabled' => []];
  4161. $result = $this->Form->radio('Model.field', $options, $attrs);
  4162. $expected = [
  4163. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4164. ['label' => ['for' => 'model-field-r']],
  4165. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4166. 'red',
  4167. '/label',
  4168. ['label' => ['for' => 'model-field-b']],
  4169. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4170. 'blue',
  4171. '/label',
  4172. ];
  4173. $this->assertHtml($expected, $result);
  4174. $attrs = ['disabled' => ['r']];
  4175. $result = $this->Form->radio('Model.field', $options, $attrs);
  4176. $this->assertStringContainsString('disabled="disabled"', $result);
  4177. }
  4178. /**
  4179. * testRadioDefaultValue method
  4180. *
  4181. * Test default value setting on radio() method.
  4182. */
  4183. public function testRadioDefaultValue(): void
  4184. {
  4185. $Articles = $this->getTableLocator()->get('Articles');
  4186. $title = $Articles->getSchema()->getColumn('title');
  4187. $Articles->getSchema()->addColumn(
  4188. 'title',
  4189. ['default' => '1'] + $title
  4190. );
  4191. $this->Form->create($Articles->newEmptyEntity());
  4192. $result = $this->Form->radio('title', ['option A', 'option B']);
  4193. $expected = [
  4194. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4195. ['label' => ['for' => 'title-0']],
  4196. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4197. 'option A',
  4198. '/label',
  4199. ['label' => ['for' => 'title-1']],
  4200. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4201. 'option B',
  4202. '/label',
  4203. ];
  4204. $this->assertHtml($expected, $result);
  4205. }
  4206. /**
  4207. * Test setting a hiddenField value on radio buttons.
  4208. */
  4209. public function testRadioHiddenFieldValue(): void
  4210. {
  4211. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4212. $expected = [
  4213. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4214. 'label' => ['for' => 'title-0'],
  4215. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4216. 'option A',
  4217. '/label',
  4218. ];
  4219. $this->assertHtml($expected, $result);
  4220. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4221. $expected = [
  4222. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4223. 'label' => ['for' => 'title-0'],
  4224. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4225. 'option A',
  4226. '/label',
  4227. ];
  4228. $this->assertHtml($expected, $result);
  4229. }
  4230. /**
  4231. * testControlRadio method
  4232. *
  4233. * Test that input works with radio types.
  4234. */
  4235. public function testControlRadio(): void
  4236. {
  4237. $result = $this->Form->control('test', [
  4238. 'type' => 'radio',
  4239. 'options' => ['A', 'B'],
  4240. ]);
  4241. $expected = [
  4242. ['div' => ['class' => 'input radio']],
  4243. '<label',
  4244. 'Test',
  4245. '/label',
  4246. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4247. ['label' => ['for' => 'test-0']],
  4248. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4249. 'A',
  4250. '/label',
  4251. ['label' => ['for' => 'test-1']],
  4252. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4253. 'B',
  4254. '/label',
  4255. '/div',
  4256. ];
  4257. $this->assertHtml($expected, $result);
  4258. $result = $this->Form->control('test', [
  4259. 'type' => 'radio',
  4260. 'options' => ['A', 'B'],
  4261. 'value' => '0',
  4262. ]);
  4263. $expected = [
  4264. ['div' => ['class' => 'input radio']],
  4265. '<label',
  4266. 'Test',
  4267. '/label',
  4268. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4269. ['label' => ['for' => 'test-0']],
  4270. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4271. 'A',
  4272. '/label',
  4273. ['label' => ['for' => 'test-1']],
  4274. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4275. 'B',
  4276. '/label',
  4277. '/div',
  4278. ];
  4279. $this->assertHtml($expected, $result);
  4280. $result = $this->Form->control('test', [
  4281. 'type' => 'radio',
  4282. 'options' => ['A', 'B'],
  4283. 'label' => false,
  4284. ]);
  4285. $expected = [
  4286. ['div' => ['class' => 'input radio']],
  4287. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4288. ['label' => ['for' => 'test-0']],
  4289. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4290. 'A',
  4291. '/label',
  4292. ['label' => ['for' => 'test-1']],
  4293. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4294. 'B',
  4295. '/label',
  4296. '/div',
  4297. ];
  4298. $this->assertHtml($expected, $result);
  4299. $result = $this->Form->control('accept', [
  4300. 'type' => 'radio',
  4301. 'options' => [
  4302. 1 => 'positive',
  4303. -1 => 'negative',
  4304. ],
  4305. 'label' => false,
  4306. ]);
  4307. $expected = [
  4308. ['div' => ['class' => 'input radio']],
  4309. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '']],
  4310. ['label' => ['for' => 'accept-1']],
  4311. ['input' => [
  4312. 'type' => 'radio',
  4313. 'name' => 'accept',
  4314. 'value' => '1',
  4315. 'id' => 'accept-1',
  4316. ]],
  4317. 'positive',
  4318. '/label',
  4319. ['label' => ['for' => 'accept--1']],
  4320. ['input' => [
  4321. 'type' => 'radio',
  4322. 'name' => 'accept',
  4323. 'value' => '-1',
  4324. 'id' => 'accept--1',
  4325. ]],
  4326. 'negative',
  4327. '/label',
  4328. '/div',
  4329. ];
  4330. $this->assertHtml($expected, $result);
  4331. }
  4332. /**
  4333. * testRadioNoLabel method
  4334. *
  4335. * Test that radio() works with label = false.
  4336. */
  4337. public function testRadioNoLabel(): void
  4338. {
  4339. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4340. $expected = [
  4341. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4342. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4343. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4344. ];
  4345. $this->assertHtml($expected, $result);
  4346. }
  4347. /**
  4348. * testRadioControlInsideLabel method
  4349. *
  4350. * Test generating radio input inside label ala twitter bootstrap.
  4351. */
  4352. public function testRadioControlInsideLabel(): void
  4353. {
  4354. $this->Form->setTemplates([
  4355. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4356. 'radioWrapper' => '{{label}}',
  4357. ]);
  4358. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4359. // phpcs:disable
  4360. $expected = [
  4361. ['input' => [
  4362. 'type' => 'hidden',
  4363. 'name' => 'Model[field]',
  4364. 'value' => ''
  4365. ]],
  4366. ['label' => ['for' => 'model-field-0']],
  4367. ['input' => [
  4368. 'type' => 'radio',
  4369. 'name' => 'Model[field]',
  4370. 'value' => '0',
  4371. 'id' => 'model-field-0'
  4372. ]],
  4373. 'option A',
  4374. '/label',
  4375. ['label' => ['for' => 'model-field-1']],
  4376. ['input' => [
  4377. 'type' => 'radio',
  4378. 'name' => 'Model[field]',
  4379. 'value' => '1',
  4380. 'id' => 'model-field-1'
  4381. ]],
  4382. 'option B',
  4383. '/label',
  4384. ];
  4385. // phpcs:enable
  4386. $this->assertHtml($expected, $result);
  4387. }
  4388. /**
  4389. * testRadioHiddenControlDisabling method
  4390. *
  4391. * Test disabling the hidden input for radio buttons.
  4392. */
  4393. public function testRadioHiddenControlDisabling(): void
  4394. {
  4395. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4396. $expected = [
  4397. 'label' => ['for' => 'model-1-field-0'],
  4398. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4399. 'option A',
  4400. '/label',
  4401. ];
  4402. $this->assertHtml($expected, $result);
  4403. }
  4404. /**
  4405. * testRadioOutOfRange method
  4406. *
  4407. * Test radio element set generation.
  4408. */
  4409. public function testRadioOutOfRange(): void
  4410. {
  4411. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4412. $expected = [
  4413. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4414. 'label' => ['for' => 'model-field-v'],
  4415. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4416. 'value',
  4417. '/label',
  4418. ];
  4419. $this->assertHtml($expected, $result);
  4420. }
  4421. /**
  4422. * testSelect method
  4423. *
  4424. * Test select element generation.
  4425. */
  4426. public function testSelect(): void
  4427. {
  4428. $result = $this->Form->select('Model.field', []);
  4429. $expected = [
  4430. 'select' => ['name' => 'Model[field]'],
  4431. '/select',
  4432. ];
  4433. $this->assertHtml($expected, $result);
  4434. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4435. $this->Form->create();
  4436. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4437. $expected = [
  4438. 'select' => ['name' => 'Model[field]'],
  4439. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4440. 'good',
  4441. '/option',
  4442. ['option' => ['value' => 'other']],
  4443. 'bad',
  4444. '/option',
  4445. '/select',
  4446. ];
  4447. $this->assertHtml($expected, $result);
  4448. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4449. $this->assertHtml($expected, $result);
  4450. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4451. $this->Form->create();
  4452. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4453. $expected = [
  4454. 'select' => ['name' => 'Model[field]'],
  4455. ['option' => ['value' => 'value']],
  4456. 'good',
  4457. '/option',
  4458. ['option' => ['value' => 'other']],
  4459. 'bad',
  4460. '/option',
  4461. '/select',
  4462. ];
  4463. $this->assertHtml($expected, $result);
  4464. $options = [
  4465. ['value' => 'first', 'text' => 'First'],
  4466. ['value' => 'first', 'text' => 'Another First'],
  4467. ];
  4468. $result = $this->Form->select(
  4469. 'Model.field',
  4470. $options,
  4471. ['escape' => false, 'empty' => false]
  4472. );
  4473. $expected = [
  4474. 'select' => ['name' => 'Model[field]'],
  4475. ['option' => ['value' => 'first']],
  4476. 'First',
  4477. '/option',
  4478. ['option' => ['value' => 'first']],
  4479. 'Another First',
  4480. '/option',
  4481. '/select',
  4482. ];
  4483. $this->assertHtml($expected, $result);
  4484. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4485. $this->Form->create();
  4486. $result = $this->Form->select(
  4487. 'Model.contact_id',
  4488. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4489. ['escape' => false, 'empty' => 'pick something']
  4490. );
  4491. $expected = [
  4492. 'select' => ['name' => 'Model[contact_id]'],
  4493. ['option' => ['value' => '']], 'pick something', '/option',
  4494. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4495. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4496. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4497. '/select',
  4498. ];
  4499. $this->assertHtml($expected, $result);
  4500. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4501. $this->Form->create();
  4502. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4503. $expected = [
  4504. 'select' => ['name' => 'Model[field]'],
  4505. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4506. ['option' => ['value' => '1']], 'Yes', '/option',
  4507. '/select',
  4508. ];
  4509. $this->assertHtml($expected, $result);
  4510. }
  4511. /**
  4512. * testSelectEscapeHtml method
  4513. *
  4514. * Test that select() escapes HTML.
  4515. */
  4516. public function testSelectEscapeHtml(): void
  4517. {
  4518. $result = $this->Form->select(
  4519. 'Model.field',
  4520. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4521. ['empty' => false]
  4522. );
  4523. $expected = [
  4524. 'select' => ['name' => 'Model[field]'],
  4525. ['option' => ['value' => 'first']],
  4526. 'first &quot;html&quot; &lt;chars&gt;',
  4527. '/option',
  4528. ['option' => ['value' => 'second']],
  4529. 'value',
  4530. '/option',
  4531. '/select',
  4532. ];
  4533. $this->assertHtml($expected, $result);
  4534. $result = $this->Form->select(
  4535. 'Model.field',
  4536. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4537. ['escape' => false, 'empty' => false]
  4538. );
  4539. $expected = [
  4540. 'select' => ['name' => 'Model[field]'],
  4541. ['option' => ['value' => 'first']],
  4542. 'first "html" <chars>',
  4543. '/option',
  4544. ['option' => ['value' => 'second']],
  4545. 'value',
  4546. '/option',
  4547. '/select',
  4548. ];
  4549. $this->assertHtml($expected, $result);
  4550. }
  4551. /**
  4552. * testSelectRequired method
  4553. *
  4554. * Test select() with required and disabled attributes.
  4555. */
  4556. public function testSelectRequired(): void
  4557. {
  4558. $this->article['required'] = [
  4559. 'user_id' => true,
  4560. ];
  4561. $this->Form->create($this->article);
  4562. $result = $this->Form->select('user_id', ['option A']);
  4563. $expected = [
  4564. 'select' => [
  4565. 'name' => 'user_id',
  4566. 'required' => 'required',
  4567. ],
  4568. ['option' => ['value' => '0']], 'option A', '/option',
  4569. '/select',
  4570. ];
  4571. $this->assertHtml($expected, $result);
  4572. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4573. $expected = [
  4574. 'select' => [
  4575. 'name' => 'user_id',
  4576. 'disabled' => 'disabled',
  4577. ],
  4578. ['option' => ['value' => '0']], 'option A', '/option',
  4579. '/select',
  4580. ];
  4581. $this->assertHtml($expected, $result);
  4582. }
  4583. public function testSelectEmptyWithRequiredFalse(): void
  4584. {
  4585. $Articles = $this->getTableLocator()->get('Articles');
  4586. $validator = $Articles->getValidator('default');
  4587. $validator->allowEmptyString('user_id');
  4588. $Articles->setValidator('default', $validator);
  4589. $entity = $Articles->newEmptyEntity();
  4590. $this->Form->create($entity);
  4591. $result = $this->Form->select('user_id', ['option A']);
  4592. $expected = [
  4593. 'select' => [
  4594. 'name' => 'user_id',
  4595. ],
  4596. ['option' => ['value' => '']], '/option',
  4597. ['option' => ['value' => '0']], 'option A', '/option',
  4598. '/select',
  4599. ];
  4600. $this->assertHtml($expected, $result);
  4601. }
  4602. /**
  4603. * testNestedSelect method
  4604. *
  4605. * Test select element generation with optgroups.
  4606. */
  4607. public function testNestedSelect(): void
  4608. {
  4609. $result = $this->Form->select(
  4610. 'Model.field',
  4611. [1 => 'One', 2 => 'Two', 'Three' => [
  4612. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4613. ]],
  4614. ['empty' => false]
  4615. );
  4616. $expected = [
  4617. 'select' => ['name' => 'Model[field]'],
  4618. ['option' => ['value' => 1]],
  4619. 'One',
  4620. '/option',
  4621. ['option' => ['value' => 2]],
  4622. 'Two',
  4623. '/option',
  4624. ['optgroup' => ['label' => 'Three']],
  4625. ['option' => ['value' => 3]],
  4626. 'Three',
  4627. '/option',
  4628. ['option' => ['value' => 4]],
  4629. 'Four',
  4630. '/option',
  4631. ['option' => ['value' => 5]],
  4632. 'Five',
  4633. '/option',
  4634. '/optgroup',
  4635. '/select',
  4636. ];
  4637. $this->assertHtml($expected, $result);
  4638. }
  4639. /**
  4640. * testSelectMultiple method
  4641. *
  4642. * Test generation of multiple select elements.
  4643. */
  4644. public function testSelectMultiple(): void
  4645. {
  4646. $options = ['first', 'second', 'third'];
  4647. $result = $this->Form->select(
  4648. 'Model.multi_field',
  4649. $options,
  4650. ['form' => 'my-form', 'multiple' => true]
  4651. );
  4652. $expected = [
  4653. 'input' => [
  4654. 'type' => 'hidden',
  4655. 'name' => 'Model[multi_field]',
  4656. 'value' => '',
  4657. 'form' => 'my-form',
  4658. ],
  4659. 'select' => [
  4660. 'name' => 'Model[multi_field][]',
  4661. 'multiple' => 'multiple',
  4662. 'form' => 'my-form',
  4663. ],
  4664. ['option' => ['value' => '0']],
  4665. 'first',
  4666. '/option',
  4667. ['option' => ['value' => '1']],
  4668. 'second',
  4669. '/option',
  4670. ['option' => ['value' => '2']],
  4671. 'third',
  4672. '/option',
  4673. '/select',
  4674. ];
  4675. $this->assertHtml($expected, $result);
  4676. $result = $this->Form->select(
  4677. 'Model.multi_field',
  4678. $options,
  4679. ['multiple' => 'multiple', 'form' => 'my-form']
  4680. );
  4681. $this->assertHtml($expected, $result);
  4682. $result = $this->Form->select(
  4683. 'Model.multi_field',
  4684. $options,
  4685. ['form' => 'my-form', 'multiple' => false]
  4686. );
  4687. $this->assertStringNotContainsString('multiple', $result);
  4688. }
  4689. /**
  4690. * testCheckboxZeroValue method
  4691. *
  4692. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4693. */
  4694. public function testCheckboxZeroValue(): void
  4695. {
  4696. $result = $this->Form->control('User.get_spam', [
  4697. 'type' => 'checkbox',
  4698. 'value' => '0',
  4699. 'hiddenField' => '1',
  4700. ]);
  4701. $expected = [
  4702. 'div' => ['class' => 'input checkbox'],
  4703. 'label' => ['for' => 'user-get-spam'],
  4704. ['input' => [
  4705. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4706. 'value' => '1',
  4707. ]],
  4708. ['input' => [
  4709. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4710. 'value' => '0', 'id' => 'user-get-spam',
  4711. ]],
  4712. 'Get Spam',
  4713. '/label',
  4714. '/div',
  4715. ];
  4716. $this->assertHtml($expected, $result);
  4717. $result = $this->Form->control('User.get_spam', [
  4718. 'type' => 'checkbox',
  4719. 'value' => '0',
  4720. 'hiddenField' => '',
  4721. ]);
  4722. $expected = [
  4723. 'div' => ['class' => 'input checkbox'],
  4724. 'label' => ['for' => 'user-get-spam'],
  4725. ['input' => [
  4726. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4727. 'value' => '',
  4728. ]],
  4729. ['input' => [
  4730. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4731. 'value' => '0', 'id' => 'user-get-spam',
  4732. ]],
  4733. 'Get Spam',
  4734. '/label',
  4735. '/div',
  4736. ];
  4737. $this->assertHtml($expected, $result);
  4738. }
  4739. /**
  4740. * testHabtmSelectBox method
  4741. *
  4742. * Test generation of habtm select boxes.
  4743. */
  4744. public function testHabtmSelectBox(): void
  4745. {
  4746. $options = [
  4747. 1 => 'blue',
  4748. 2 => 'red',
  4749. 3 => 'green',
  4750. ];
  4751. $tags = [
  4752. new Entity(['id' => 1, 'name' => 'blue']),
  4753. new Entity(['id' => 3, 'name' => 'green']),
  4754. ];
  4755. $article = new Article(['tags' => $tags]);
  4756. $this->Form->create($article);
  4757. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4758. $expected = [
  4759. 'div' => ['class' => 'input select'],
  4760. 'label' => ['for' => 'tags-ids'],
  4761. 'Tags',
  4762. '/label',
  4763. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4764. 'select' => [
  4765. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4766. 'multiple' => 'multiple',
  4767. ],
  4768. ['option' => ['value' => '1', 'selected' => 'selected']],
  4769. 'blue',
  4770. '/option',
  4771. ['option' => ['value' => '2']],
  4772. 'red',
  4773. '/option',
  4774. ['option' => ['value' => '3', 'selected' => 'selected']],
  4775. 'green',
  4776. '/option',
  4777. '/select',
  4778. '/div',
  4779. ];
  4780. $this->assertHtml($expected, $result);
  4781. // make sure only 50 is selected, and not 50f5c0cf
  4782. $options = [
  4783. '1' => 'blue',
  4784. '50f5c0cf' => 'red',
  4785. '50' => 'green',
  4786. ];
  4787. $tags = [
  4788. new Entity(['id' => 1, 'name' => 'blue']),
  4789. new Entity(['id' => 50, 'name' => 'green']),
  4790. ];
  4791. $article = new Article(['tags' => $tags]);
  4792. $this->Form->create($article);
  4793. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4794. $expected = [
  4795. 'div' => ['class' => 'input select'],
  4796. 'label' => ['for' => 'tags-ids'],
  4797. 'Tags',
  4798. '/label',
  4799. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4800. 'select' => [
  4801. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4802. 'multiple' => 'multiple',
  4803. ],
  4804. ['option' => ['value' => '1', 'selected' => 'selected']],
  4805. 'blue',
  4806. '/option',
  4807. ['option' => ['value' => '50f5c0cf']],
  4808. 'red',
  4809. '/option',
  4810. ['option' => ['value' => '50', 'selected' => 'selected']],
  4811. 'green',
  4812. '/option',
  4813. '/select',
  4814. '/div',
  4815. ];
  4816. $this->assertHtml($expected, $result);
  4817. $spacecraft = [
  4818. 1 => 'Orion',
  4819. 2 => 'Helios',
  4820. ];
  4821. $this->View->set('spacecraft', $spacecraft);
  4822. $this->Form->create();
  4823. $result = $this->Form->control('spacecraft._ids');
  4824. $expected = [
  4825. 'div' => ['class' => 'input select'],
  4826. 'label' => ['for' => 'spacecraft-ids'],
  4827. 'Spacecraft',
  4828. '/label',
  4829. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4830. 'select' => [
  4831. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4832. 'multiple' => 'multiple',
  4833. ],
  4834. ['option' => ['value' => '1']],
  4835. 'Orion',
  4836. '/option',
  4837. ['option' => ['value' => '2']],
  4838. 'Helios',
  4839. '/option',
  4840. '/select',
  4841. '/div',
  4842. ];
  4843. $this->assertHtml($expected, $result);
  4844. }
  4845. /**
  4846. * testErrorsForBelongsToManySelect method
  4847. *
  4848. * Tests that errors for belongsToMany select fields are being
  4849. * picked up properly.
  4850. */
  4851. public function testErrorsForBelongsToManySelect(): void
  4852. {
  4853. $spacecraft = [
  4854. 1 => 'Orion',
  4855. 2 => 'Helios',
  4856. ];
  4857. $this->View->set('spacecraft', $spacecraft);
  4858. $article = new Article();
  4859. $article->setError('spacecraft', ['Invalid']);
  4860. $this->Form->create($article);
  4861. $result = $this->Form->control('spacecraft._ids');
  4862. $expected = [
  4863. ['div' => ['class' => 'input select error']],
  4864. 'label' => ['for' => 'spacecraft-ids'],
  4865. 'Spacecraft',
  4866. '/label',
  4867. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4868. 'select' => [
  4869. 'name' => 'spacecraft[_ids][]',
  4870. 'id' => 'spacecraft-ids',
  4871. 'multiple' => 'multiple',
  4872. ],
  4873. ['option' => ['value' => '1']],
  4874. 'Orion',
  4875. '/option',
  4876. ['option' => ['value' => '2']],
  4877. 'Helios',
  4878. '/option',
  4879. '/select',
  4880. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4881. 'Invalid',
  4882. '/div',
  4883. '/div',
  4884. ];
  4885. $this->assertHtml($expected, $result);
  4886. }
  4887. /**
  4888. * testSelectMultipleCheckboxes method
  4889. *
  4890. * Test generation of multi select elements in checkbox format.
  4891. */
  4892. public function testSelectMultipleCheckboxes(): void
  4893. {
  4894. $result = $this->Form->select(
  4895. 'Model.multi_field',
  4896. ['first', 'second', 'third'],
  4897. ['multiple' => 'checkbox']
  4898. );
  4899. $expected = [
  4900. 'input' => [
  4901. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4902. ],
  4903. ['div' => ['class' => 'checkbox']],
  4904. ['label' => ['for' => 'model-multi-field-0']],
  4905. ['input' => [
  4906. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4907. 'value' => '0', 'id' => 'model-multi-field-0',
  4908. ]],
  4909. 'first',
  4910. '/label',
  4911. '/div',
  4912. ['div' => ['class' => 'checkbox']],
  4913. ['label' => ['for' => 'model-multi-field-1']],
  4914. ['input' => [
  4915. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4916. 'value' => '1', 'id' => 'model-multi-field-1',
  4917. ]],
  4918. 'second',
  4919. '/label',
  4920. '/div',
  4921. ['div' => ['class' => 'checkbox']],
  4922. ['label' => ['for' => 'model-multi-field-2']],
  4923. ['input' => [
  4924. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4925. 'value' => '2', 'id' => 'model-multi-field-2',
  4926. ]],
  4927. 'third',
  4928. '/label',
  4929. '/div',
  4930. ];
  4931. $this->assertHtml($expected, $result);
  4932. $result = $this->Form->select(
  4933. 'Model.multi_field',
  4934. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4935. ['multiple' => 'checkbox']
  4936. );
  4937. $expected = [
  4938. 'input' => [
  4939. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4940. ],
  4941. ['div' => ['class' => 'checkbox']],
  4942. ['label' => ['for' => 'model-multi-field-a+']],
  4943. ['input' => [
  4944. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4945. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  4946. ]],
  4947. 'first',
  4948. '/label',
  4949. '/div',
  4950. ['div' => ['class' => 'checkbox']],
  4951. ['label' => ['for' => 'model-multi-field-a++']],
  4952. ['input' => [
  4953. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4954. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  4955. ]],
  4956. 'second',
  4957. '/label',
  4958. '/div',
  4959. ['div' => ['class' => 'checkbox']],
  4960. ['label' => ['for' => 'model-multi-field-a+++']],
  4961. ['input' => [
  4962. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4963. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  4964. ]],
  4965. 'third',
  4966. '/label',
  4967. '/div',
  4968. ];
  4969. $this->assertHtml($expected, $result);
  4970. $result = $this->Form->select(
  4971. 'Model.multi_field',
  4972. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4973. ['multiple' => 'checkbox']
  4974. );
  4975. $expected = [
  4976. 'input' => [
  4977. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4978. ],
  4979. ['div' => ['class' => 'checkbox']],
  4980. ['label' => ['for' => 'model-multi-field-a-b']],
  4981. ['input' => [
  4982. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4983. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  4984. ]],
  4985. 'first',
  4986. '/label',
  4987. '/div',
  4988. ['div' => ['class' => 'checkbox']],
  4989. ['label' => ['for' => 'model-multi-field-a-b1']],
  4990. ['input' => [
  4991. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4992. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  4993. ]],
  4994. 'second',
  4995. '/label',
  4996. '/div',
  4997. ['div' => ['class' => 'checkbox']],
  4998. ['label' => ['for' => 'model-multi-field-a-b2']],
  4999. ['input' => [
  5000. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5001. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5002. ]],
  5003. 'third',
  5004. '/label',
  5005. '/div',
  5006. ];
  5007. $this->assertHtml($expected, $result);
  5008. }
  5009. /**
  5010. * testSelectMultipleCheckboxRequestData method
  5011. *
  5012. * Ensure that multiCheckbox reads from the request data.
  5013. */
  5014. public function testSelectMultipleCheckboxRequestData(): void
  5015. {
  5016. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5017. $result = $this->Form->select(
  5018. 'Model.tags',
  5019. ['1' => 'first', 'Array' => 'Array'],
  5020. ['multiple' => 'checkbox']
  5021. );
  5022. $expected = [
  5023. 'input' => [
  5024. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5025. ],
  5026. ['div' => ['class' => 'checkbox']],
  5027. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5028. ['input' => [
  5029. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5030. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5031. ]],
  5032. 'first',
  5033. '/label',
  5034. '/div',
  5035. ['div' => ['class' => 'checkbox']],
  5036. ['label' => ['for' => 'model-tags-array']],
  5037. ['input' => [
  5038. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5039. 'value' => 'Array', 'id' => 'model-tags-array',
  5040. ]],
  5041. 'Array',
  5042. '/label',
  5043. '/div',
  5044. ];
  5045. $this->assertHtml($expected, $result);
  5046. }
  5047. /**
  5048. * testSelectMultipleCheckboxSecurity method
  5049. *
  5050. * Checks the security hash array generated for multiple-input checkbox elements.
  5051. */
  5052. public function testSelectMultipleCheckboxSecurity(): void
  5053. {
  5054. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5055. $this->Form->create();
  5056. $this->Form->select(
  5057. 'Model.multi_field',
  5058. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5059. ['multiple' => 'checkbox']
  5060. );
  5061. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5062. $this->assertEquals(['Model.multi_field'], $fields);
  5063. $result = $this->Form->secure();
  5064. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5065. $hash = urlencode($hash . ':');
  5066. $this->assertStringContainsString('"' . $hash . '"', $result);
  5067. }
  5068. /**
  5069. * testSelectMultipleSecureWithNoOptions method
  5070. *
  5071. * Multiple select elements should always be secured as they always participate
  5072. * in the POST data.
  5073. */
  5074. public function testSelectMultipleSecureWithNoOptions(): void
  5075. {
  5076. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5077. $this->Form->create();
  5078. $this->Form->select(
  5079. 'Model.select',
  5080. [],
  5081. ['multiple' => true]
  5082. );
  5083. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5084. $this->assertEquals(['Model.select'], $result);
  5085. }
  5086. /**
  5087. * testSelectNoSecureWithNoOptions method
  5088. *
  5089. * When a select box has no options it should not be added to the fields list
  5090. * as it always fail post validation.
  5091. */
  5092. public function testSelectNoSecureWithNoOptions(): void
  5093. {
  5094. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5095. $this->Form->create();
  5096. $this->Form->select(
  5097. 'Model.select',
  5098. []
  5099. );
  5100. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5101. $this->assertEquals([], $result);
  5102. $this->Form->select(
  5103. 'Model.user_id',
  5104. [],
  5105. ['empty' => true]
  5106. );
  5107. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5108. $this->assertEquals(['Model.user_id'], $result);
  5109. }
  5110. /**
  5111. * testControlMultipleCheckboxes method
  5112. *
  5113. * Test control() resulting in multi select elements being generated.
  5114. */
  5115. public function testControlMultipleCheckboxes(): void
  5116. {
  5117. $result = $this->Form->control('Model.multi_field', [
  5118. 'options' => ['first', 'second', 'third'],
  5119. 'multiple' => 'checkbox',
  5120. ]);
  5121. $expected = [
  5122. ['div' => ['class' => 'input select']],
  5123. ['label' => ['for' => 'model-multi-field']],
  5124. 'Multi Field',
  5125. '/label',
  5126. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5127. ['div' => ['class' => 'checkbox']],
  5128. ['label' => ['for' => 'model-multi-field-0']],
  5129. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5130. 'first',
  5131. '/label',
  5132. '/div',
  5133. ['div' => ['class' => 'checkbox']],
  5134. ['label' => ['for' => 'model-multi-field-1']],
  5135. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5136. 'second',
  5137. '/label',
  5138. '/div',
  5139. ['div' => ['class' => 'checkbox']],
  5140. ['label' => ['for' => 'model-multi-field-2']],
  5141. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5142. 'third',
  5143. '/label',
  5144. '/div',
  5145. '/div',
  5146. ];
  5147. $this->assertHtml($expected, $result);
  5148. $result = $this->Form->control('Model.multi_field', [
  5149. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5150. 'multiple' => 'checkbox',
  5151. ]);
  5152. $expected = [
  5153. ['div' => ['class' => 'input select']],
  5154. ['label' => ['for' => 'model-multi-field']],
  5155. 'Multi Field',
  5156. '/label',
  5157. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5158. ['div' => ['class' => 'checkbox']],
  5159. ['label' => ['for' => 'model-multi-field-a']],
  5160. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5161. 'first',
  5162. '/label',
  5163. '/div',
  5164. ['div' => ['class' => 'checkbox']],
  5165. ['label' => ['for' => 'model-multi-field-b']],
  5166. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5167. 'second',
  5168. '/label',
  5169. '/div',
  5170. ['div' => ['class' => 'checkbox']],
  5171. ['label' => ['for' => 'model-multi-field-c']],
  5172. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5173. 'third',
  5174. '/label',
  5175. '/div',
  5176. '/div',
  5177. ];
  5178. $this->assertHtml($expected, $result);
  5179. }
  5180. /**
  5181. * testSelectHiddenFieldOmission method
  5182. *
  5183. * Test that select() with 'hiddenField' => false omits the hidden field.
  5184. */
  5185. public function testSelectHiddenFieldOmission(): void
  5186. {
  5187. $result = $this->Form->select(
  5188. 'Model.multi_field',
  5189. ['first', 'second'],
  5190. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5191. );
  5192. $this->assertStringNotContainsString('type="hidden"', $result);
  5193. }
  5194. /**
  5195. * testSelectCheckboxMultipleOverrideName method
  5196. *
  5197. * Test that select() with multiple = checkbox works with overriding name attribute.
  5198. */
  5199. public function testSelectCheckboxMultipleOverrideName(): void
  5200. {
  5201. $result = $this->Form->select('category', ['1', '2'], [
  5202. 'multiple' => 'checkbox',
  5203. 'name' => 'fish',
  5204. ]);
  5205. $expected = [
  5206. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5207. ['div' => ['class' => 'checkbox']],
  5208. ['label' => ['for' => 'fish-0']],
  5209. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5210. '1',
  5211. '/label',
  5212. '/div',
  5213. ['div' => ['class' => 'checkbox']],
  5214. ['label' => ['for' => 'fish-1']],
  5215. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5216. '2',
  5217. '/label',
  5218. '/div',
  5219. ];
  5220. $this->assertHtml($expected, $result);
  5221. $result = $this->Form->multiCheckbox(
  5222. 'category',
  5223. new Collection(['1', '2']),
  5224. ['name' => 'fish']
  5225. );
  5226. $this->assertHtml($expected, $result);
  5227. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5228. 'name' => 'fish',
  5229. ]);
  5230. $this->assertHtml($expected, $result);
  5231. }
  5232. /**
  5233. * testControlMultiCheckbox method
  5234. *
  5235. * Test that control() works with multicheckbox.
  5236. */
  5237. public function testControlMultiCheckbox(): void
  5238. {
  5239. $result = $this->Form->control('category', [
  5240. 'type' => 'multicheckbox',
  5241. 'options' => ['1', '2'],
  5242. ]);
  5243. $expected = [
  5244. ['div' => ['class' => 'input multicheckbox']],
  5245. '<label',
  5246. 'Category',
  5247. '/label',
  5248. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5249. ['div' => ['class' => 'checkbox']],
  5250. ['label' => ['for' => 'category-0']],
  5251. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5252. '1',
  5253. '/label',
  5254. '/div',
  5255. ['div' => ['class' => 'checkbox']],
  5256. ['label' => ['for' => 'category-1']],
  5257. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5258. '2',
  5259. '/label',
  5260. '/div',
  5261. '/div',
  5262. ];
  5263. $this->assertHtml($expected, $result);
  5264. }
  5265. /**
  5266. * testCheckbox method
  5267. *
  5268. * Test generation of checkboxes.
  5269. */
  5270. public function testCheckbox(): void
  5271. {
  5272. $result = $this->Form->checkbox('Model.field');
  5273. $expected = [
  5274. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5275. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5276. ];
  5277. $this->assertHtml($expected, $result);
  5278. $result = $this->Form->checkbox('Model.field', [
  5279. 'id' => 'theID',
  5280. 'value' => 'myvalue',
  5281. 'form' => 'my-form',
  5282. ]);
  5283. $expected = [
  5284. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5285. ['input' => [
  5286. 'type' => 'checkbox', 'name' => 'Model[field]',
  5287. 'value' => 'myvalue', 'id' => 'theID',
  5288. 'form' => 'my-form',
  5289. ]],
  5290. ];
  5291. $this->assertHtml($expected, $result);
  5292. }
  5293. /**
  5294. * testCheckboxDefaultValue method
  5295. *
  5296. * Test default value setting on checkbox() method.
  5297. */
  5298. public function testCheckboxDefaultValue(): void
  5299. {
  5300. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5301. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5302. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5303. $this->assertHtml($expected, $result);
  5304. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5305. $this->Form->create();
  5306. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5307. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5308. $this->assertHtml($expected, $result);
  5309. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5310. $this->Form->create();
  5311. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5312. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5313. $this->assertHtml($expected, $result);
  5314. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5315. $this->Form->create();
  5316. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5317. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5318. $this->assertHtml($expected, $result);
  5319. $Articles = $this->getTableLocator()->get('Articles');
  5320. $Articles->getSchema()->addColumn(
  5321. 'published',
  5322. ['type' => 'boolean', 'null' => false, 'default' => true]
  5323. );
  5324. $this->Form->create($Articles->newEmptyEntity());
  5325. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5326. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5327. $this->assertHtml($expected, $result);
  5328. }
  5329. /**
  5330. * testCheckboxCheckedAndError method
  5331. *
  5332. * Test checkbox being checked or having errors.
  5333. */
  5334. public function testCheckboxCheckedAndError(): void
  5335. {
  5336. $this->article['errors'] = [
  5337. 'published' => true,
  5338. ];
  5339. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5340. $this->Form->create($this->article);
  5341. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5342. $expected = [
  5343. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5344. ['input' => [
  5345. 'type' => 'checkbox',
  5346. 'name' => 'published',
  5347. 'value' => 'myvalue',
  5348. 'id' => 'theID',
  5349. 'checked' => 'checked',
  5350. 'class' => 'form-error',
  5351. ]],
  5352. ];
  5353. $this->assertHtml($expected, $result);
  5354. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5355. $this->Form->create($this->article);
  5356. $result = $this->Form->checkbox('published');
  5357. $expected = [
  5358. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5359. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5360. ];
  5361. $this->assertHtml($expected, $result);
  5362. }
  5363. /**
  5364. * testCheckboxCustomNameAttribute method
  5365. *
  5366. * Test checkbox() with a custom name attribute.
  5367. */
  5368. public function testCheckboxCustomNameAttribute(): void
  5369. {
  5370. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5371. $expected = [
  5372. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5373. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5374. ];
  5375. $this->assertHtml($expected, $result);
  5376. }
  5377. /**
  5378. * testCheckboxHiddenField method
  5379. *
  5380. * Test that the hidden input for checkboxes can be omitted or set to a
  5381. * specific value.
  5382. */
  5383. public function testCheckboxHiddenField(): void
  5384. {
  5385. $result = $this->Form->checkbox('UserForm.something', [
  5386. 'hiddenField' => false,
  5387. ]);
  5388. $expected = [
  5389. 'input' => [
  5390. 'type' => 'checkbox',
  5391. 'name' => 'UserForm[something]',
  5392. 'value' => '1',
  5393. ],
  5394. ];
  5395. $this->assertHtml($expected, $result);
  5396. $result = $this->Form->checkbox('UserForm.something', [
  5397. 'value' => 'Y',
  5398. 'hiddenField' => '',
  5399. ]);
  5400. $expected = [
  5401. ['input' => [
  5402. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5403. 'value' => '',
  5404. ]],
  5405. ['input' => [
  5406. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5407. 'value' => 'Y',
  5408. ]],
  5409. ];
  5410. $this->assertHtml($expected, $result);
  5411. $result = $this->Form->checkbox('UserForm.something', [
  5412. 'value' => 'Y',
  5413. 'hiddenField' => 'N',
  5414. ]);
  5415. $expected = [
  5416. ['input' => [
  5417. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5418. 'value' => 'N',
  5419. ]],
  5420. ['input' => [
  5421. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5422. 'value' => 'Y',
  5423. ]],
  5424. ];
  5425. $this->assertHtml($expected, $result);
  5426. }
  5427. /**
  5428. * testTime method
  5429. *
  5430. * Test the time type.
  5431. */
  5432. public function testTime(): void
  5433. {
  5434. $result = $this->Form->time('start_time', [
  5435. 'value' => '2014-03-08 16:30:00',
  5436. ]);
  5437. $expected = [
  5438. 'input' => [
  5439. 'type' => 'time',
  5440. 'name' => 'start_time',
  5441. 'value' => '16:30:00',
  5442. 'step' => '1',
  5443. ],
  5444. ];
  5445. $this->assertHtml($expected, $result);
  5446. }
  5447. /**
  5448. * testDate method
  5449. *
  5450. * Test the date type.
  5451. */
  5452. public function testDate(): void
  5453. {
  5454. $result = $this->Form->date('start_day', [
  5455. 'value' => '2014-03-08',
  5456. ]);
  5457. $expected = [
  5458. 'input' => [
  5459. 'type' => 'date',
  5460. 'name' => 'start_day',
  5461. 'value' => '2014-03-08',
  5462. ],
  5463. ];
  5464. $this->assertHtml($expected, $result);
  5465. $result = $this->Form->date('start_day', [
  5466. 'value' => new FrozenDate('2014-03-08'),
  5467. ]);
  5468. $this->assertHtml($expected, $result);
  5469. }
  5470. /**
  5471. * testDateTime method
  5472. */
  5473. public function testDateTime(): void
  5474. {
  5475. $result = $this->Form->dateTime('date', ['default' => true]);
  5476. $expected = [
  5477. 'input' => [
  5478. 'type' => 'datetime-local',
  5479. 'name' => 'date',
  5480. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5481. 'step' => '1',
  5482. ],
  5483. ];
  5484. $this->assertHtml($expected, $result);
  5485. }
  5486. /**
  5487. * testDateTimeSecured method
  5488. *
  5489. * Test that datetime fields are added to protected fields list.
  5490. */
  5491. public function testDateTimeSecured(): void
  5492. {
  5493. $this->View->setRequest(
  5494. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5495. );
  5496. $this->Form->create();
  5497. $this->Form->dateTime('date');
  5498. $expected = ['date'];
  5499. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5500. $this->assertEquals($expected, $result);
  5501. $this->Form->fields = [];
  5502. $this->Form->date('published');
  5503. $expected = ['date', 'published'];
  5504. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5505. $this->assertEquals($expected, $result);
  5506. }
  5507. /**
  5508. * testDateTimeSecuredDisabled method
  5509. *
  5510. * Test that datetime fields are added to protected fields list.
  5511. */
  5512. public function testDateTimeSecuredDisabled(): void
  5513. {
  5514. $this->View->setRequest(
  5515. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5516. );
  5517. $this->Form->create();
  5518. $this->Form->dateTime('date', ['secure' => false]);
  5519. $expected = [];
  5520. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5521. $this->assertEquals($expected, $result);
  5522. $this->Form->fields = [];
  5523. $this->Form->date('published', ['secure' => false]);
  5524. $expected = [];
  5525. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5526. $this->assertEquals($expected, $result);
  5527. }
  5528. /**
  5529. * testDatetimeWithDefault method
  5530. *
  5531. * Test that datetime() and default values work.
  5532. */
  5533. public function testDatetimeWithDefault(): void
  5534. {
  5535. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5536. $expected = [
  5537. 'input' => [
  5538. 'type' => 'datetime-local',
  5539. 'name' => 'updated',
  5540. 'value' => '2009-06-01T11:15:30',
  5541. 'step' => '1',
  5542. ],
  5543. ];
  5544. $this->assertHtml($expected, $result);
  5545. $result = $this->Form->dateTime('updated', [
  5546. 'default' => '2009-06-01 11:15:30',
  5547. ]);
  5548. $expected = [
  5549. 'input' => [
  5550. 'type' => 'datetime-local',
  5551. 'name' => 'updated',
  5552. 'value' => '2009-06-01T11:15:30',
  5553. 'step' => '1',
  5554. ],
  5555. ];
  5556. $this->assertHtml($expected, $result);
  5557. }
  5558. /**
  5559. * testMonth method
  5560. *
  5561. * Test generation of a month input.
  5562. */
  5563. public function testMonth(): void
  5564. {
  5565. $result = $this->Form->month('field', ['value' => '']);
  5566. $expected = [
  5567. 'input' => [
  5568. 'type' => 'month',
  5569. 'name' => 'field',
  5570. 'value' => '',
  5571. ],
  5572. ];
  5573. $this->assertHtml($expected, $result);
  5574. $this->View->setRequest(
  5575. $this->View->getRequest()->withData('release', '2050-02-10')
  5576. );
  5577. $this->Form->create();
  5578. $result = $this->Form->month('release');
  5579. $expected = [
  5580. 'input' => [
  5581. 'type' => 'month',
  5582. 'name' => 'release',
  5583. 'value' => '2050-02',
  5584. ],
  5585. ];
  5586. $this->assertHtml($expected, $result);
  5587. $this->View->setRequest(
  5588. $this->View->getRequest()->withData('release', '2050-03')
  5589. );
  5590. $this->Form->create();
  5591. $result = $this->Form->month('release');
  5592. $expected = [
  5593. 'input' => [
  5594. 'type' => 'month',
  5595. 'name' => 'release',
  5596. 'value' => '2050-03',
  5597. ],
  5598. ];
  5599. $this->assertHtml($expected, $result);
  5600. }
  5601. /**
  5602. * testYear method
  5603. *
  5604. * Test generation of a year input.
  5605. */
  5606. public function testYear(): void
  5607. {
  5608. $this->View->setRequest(
  5609. $this->View->getRequest()->withData('published', '2006')
  5610. );
  5611. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5612. $expected = [
  5613. ['select' => ['name' => 'field']],
  5614. ['option' => ['selected' => 'selected', 'value' => '']],
  5615. '/option',
  5616. ['option' => ['value' => '2007']],
  5617. '2007',
  5618. '/option',
  5619. ['option' => ['value' => '2006']],
  5620. '2006',
  5621. '/option',
  5622. '/select',
  5623. ];
  5624. $this->assertHtml($expected, $result);
  5625. $result = $this->Form->year('field', [
  5626. 'value' => '',
  5627. 'min' => 2006,
  5628. 'max' => 2007,
  5629. 'order' => 'asc',
  5630. ]);
  5631. $expected = [
  5632. ['select' => ['name' => 'field']],
  5633. ['option' => ['selected' => 'selected', 'value' => '']],
  5634. '/option',
  5635. ['option' => ['value' => '2006']],
  5636. '2006',
  5637. '/option',
  5638. ['option' => ['value' => '2007']],
  5639. '2007',
  5640. '/option',
  5641. '/select',
  5642. ];
  5643. $this->assertHtml($expected, $result);
  5644. $result = $this->Form->year('published', [
  5645. 'empty' => false,
  5646. 'min' => 2006,
  5647. 'max' => 2007,
  5648. ]);
  5649. $expected = [
  5650. ['select' => ['name' => 'published']],
  5651. ['option' => ['value' => '2007']],
  5652. '2007',
  5653. '/option',
  5654. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5655. '2006',
  5656. '/option',
  5657. '/select',
  5658. ];
  5659. $this->assertHtml($expected, $result);
  5660. $result = $this->Form->year('published', [
  5661. 'empty' => false,
  5662. 'value' => new FrozenDate('2008-01-12'),
  5663. 'min' => 2007,
  5664. 'max' => 2009,
  5665. ]);
  5666. $expected = [
  5667. ['select' => ['name' => 'published']],
  5668. ['option' => ['value' => '2009']],
  5669. '2009',
  5670. '/option',
  5671. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5672. '2008',
  5673. '/option',
  5674. ['option' => ['value' => '2007']],
  5675. '2007',
  5676. '/option',
  5677. '/select',
  5678. ];
  5679. $this->assertHtml($expected, $result);
  5680. $result = $this->Form->year('published', [
  5681. 'empty' => 'Published on',
  5682. ]);
  5683. $this->assertStringContainsString('Published on', $result);
  5684. }
  5685. /**
  5686. * testControlYearPreEpoch method
  5687. *
  5688. * Test minYear being prior to the unix epoch.
  5689. */
  5690. public function testControlYearPreEpoch(): void
  5691. {
  5692. $start = date('Y') - 80;
  5693. $end = date('Y') - 18;
  5694. $result = $this->Form->control('birth_year', [
  5695. 'type' => 'year',
  5696. 'label' => 'Birth Year',
  5697. 'min' => $start,
  5698. 'max' => $end,
  5699. ]);
  5700. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5701. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5702. $this->assertStringNotContainsString('value="00">00', $result);
  5703. }
  5704. /**
  5705. * test control() datetime & required attributes
  5706. */
  5707. public function testControlDatetimeRequired(): void
  5708. {
  5709. $result = $this->Form->control('birthday', [
  5710. 'type' => 'date',
  5711. 'required' => true,
  5712. ]);
  5713. $this->assertStringContainsString(
  5714. '<input type="date" name="birthday" required="required"',
  5715. $result
  5716. );
  5717. }
  5718. /**
  5719. * testYearAutoExpandRange method
  5720. */
  5721. public function testYearAutoExpandRange(): void
  5722. {
  5723. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5724. $result = $this->Form->year('birthday');
  5725. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5726. $result = $matches[1];
  5727. $expected = range(date('Y') + 5, 1930);
  5728. $this->assertEquals($expected, $result);
  5729. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5730. $this->Form->create();
  5731. $result = $this->Form->year('release');
  5732. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5733. $result = $matches[1];
  5734. $expected = range(2050, date('Y') - 5);
  5735. $this->assertEquals($expected, $result);
  5736. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5737. $this->Form->create();
  5738. $result = $this->Form->year('release', [
  5739. 'min' => 1890,
  5740. 'max' => 1900,
  5741. ]);
  5742. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5743. $result = $matches[1];
  5744. $expected = range(1900, 1881);
  5745. $this->assertEquals($expected, $result);
  5746. }
  5747. /**
  5748. * test control placeholder + label
  5749. */
  5750. public function testControlLabelAndPlaceholder(): void
  5751. {
  5752. $this->Form->create($this->article);
  5753. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5754. $expected = [
  5755. 'div' => ['class' => 'input text required'],
  5756. 'label' => ['for' => 'title'],
  5757. 'Title',
  5758. '/label',
  5759. 'input' => [
  5760. 'aria-required' => 'true',
  5761. 'type' => 'text',
  5762. 'required' => 'required',
  5763. 'placeholder' => 'Add title',
  5764. 'id' => 'title',
  5765. 'name' => 'title',
  5766. 'data-validity-message' => 'This field cannot be left empty',
  5767. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5768. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5769. ],
  5770. '/div',
  5771. ];
  5772. $this->assertHtml($expected, $result);
  5773. }
  5774. /**
  5775. * testControlLabelFalse method
  5776. *
  5777. * Test the label option being set to false.
  5778. */
  5779. public function testControlLabelFalse(): void
  5780. {
  5781. $this->Form->create($this->article);
  5782. $result = $this->Form->control('title', ['label' => false]);
  5783. $expected = [
  5784. 'div' => ['class' => 'input text required'],
  5785. 'input' => [
  5786. 'aria-required' => 'true',
  5787. 'type' => 'text',
  5788. 'required' => 'required',
  5789. 'id' => 'title',
  5790. 'name' => 'title',
  5791. 'data-validity-message' => 'This field cannot be left empty',
  5792. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5793. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5794. ],
  5795. '/div',
  5796. ];
  5797. $this->assertHtml($expected, $result);
  5798. $this->Form->create($this->article);
  5799. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5800. $expected['input'] += [
  5801. 'placeholder' => 'Add title',
  5802. 'aria-label' => 'Add title',
  5803. ];
  5804. $this->assertHtml($expected, $result);
  5805. }
  5806. /**
  5807. * testTextArea method
  5808. *
  5809. * Test generation of a textarea input.
  5810. */
  5811. public function testTextArea(): void
  5812. {
  5813. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5814. $result = $this->Form->textarea('field');
  5815. $expected = [
  5816. 'textarea' => ['name' => 'field', 'rows' => 5],
  5817. 'some test data',
  5818. '/textarea',
  5819. ];
  5820. $this->assertHtml($expected, $result);
  5821. $result = $this->Form->textarea('user.bio');
  5822. $expected = [
  5823. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5824. '/textarea',
  5825. ];
  5826. $this->assertHtml($expected, $result);
  5827. $this->View->setRequest($this->View->getRequest()
  5828. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5829. $this->Form->create();
  5830. $result = $this->Form->textarea('field');
  5831. $expected = [
  5832. 'textarea' => ['name' => 'field', 'rows' => 5],
  5833. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5834. '/textarea',
  5835. ];
  5836. $this->assertHtml($expected, $result);
  5837. $this->View->setRequest($this->View->getRequest()->withData(
  5838. 'Model.field',
  5839. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5840. ));
  5841. $this->Form->create();
  5842. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5843. $expected = [
  5844. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5845. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5846. '/textarea',
  5847. ];
  5848. $this->assertHtml($expected, $result);
  5849. $result = $this->Form->textarea('0.OtherModel.field');
  5850. $expected = [
  5851. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5852. '/textarea',
  5853. ];
  5854. $this->assertHtml($expected, $result);
  5855. }
  5856. /**
  5857. * testTextAreaWithStupidCharacters method
  5858. *
  5859. * Test text area with non-ascii characters.
  5860. */
  5861. public function testTextAreaWithStupidCharacters(): void
  5862. {
  5863. $result = $this->Form->textarea('Post.content', [
  5864. 'value' => 'GREAT®',
  5865. 'rows' => '15',
  5866. 'cols' => '75',
  5867. ]);
  5868. $expected = [
  5869. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5870. 'GREAT®',
  5871. '/textarea',
  5872. ];
  5873. $this->assertHtml($expected, $result);
  5874. }
  5875. /**
  5876. * testTextAreaMaxLength method
  5877. *
  5878. * Test textareas maxlength read from schema.
  5879. */
  5880. public function testTextAreaMaxLength(): void
  5881. {
  5882. $this->Form->create([
  5883. 'schema' => [
  5884. 'stuff' => ['type' => 'string', 'length' => 10],
  5885. ],
  5886. ]);
  5887. $result = $this->Form->control('other', ['type' => 'textarea']);
  5888. $expected = [
  5889. 'div' => ['class' => 'input textarea'],
  5890. 'label' => ['for' => 'other'],
  5891. 'Other',
  5892. '/label',
  5893. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5894. '/textarea',
  5895. '/div',
  5896. ];
  5897. $this->assertHtml($expected, $result);
  5898. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5899. $expected = [
  5900. 'div' => ['class' => 'input textarea'],
  5901. 'label' => ['for' => 'stuff'],
  5902. 'Stuff',
  5903. '/label',
  5904. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5905. '/textarea',
  5906. '/div',
  5907. ];
  5908. $this->assertHtml($expected, $result);
  5909. }
  5910. /**
  5911. * testHiddenField method
  5912. *
  5913. * Test generation of a hidden input.
  5914. */
  5915. public function testHidden(): void
  5916. {
  5917. $this->article['errors'] = [
  5918. 'field' => true,
  5919. ];
  5920. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5921. $this->Form->create($this->article);
  5922. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5923. $expected = [
  5924. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5925. $this->assertHtml($expected, $result);
  5926. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5927. $expected = [
  5928. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5929. ];
  5930. $this->assertHtml($expected, $result);
  5931. }
  5932. /**
  5933. * Test hidden() with various boolean values.
  5934. */
  5935. public function testHiddenBooleanValues(): void
  5936. {
  5937. $this->Form->create($this->article);
  5938. $result = $this->Form->hidden('field', ['value' => null]);
  5939. $expected = [
  5940. 'input' => ['type' => 'hidden', 'name' => 'field'],
  5941. ];
  5942. $this->assertHtml($expected, $result);
  5943. $result = $this->Form->hidden('field', ['value' => true]);
  5944. $expected = [
  5945. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  5946. ];
  5947. $this->assertHtml($expected, $result);
  5948. $result = $this->Form->hidden('field', ['value' => false]);
  5949. $expected = [
  5950. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  5951. ];
  5952. $this->assertHtml($expected, $result);
  5953. }
  5954. /**
  5955. * testFileUploadField method
  5956. *
  5957. * Test generation of a file upload input.
  5958. */
  5959. public function testFileUploadField(): void
  5960. {
  5961. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5962. $result = $this->Form->file('Model.upload');
  5963. $this->assertHtml($expected, $result);
  5964. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5965. 'name' => '', 'type' => '', 'tmp_name' => '',
  5966. 'error' => 4, 'size' => 0,
  5967. ]));
  5968. $result = $this->Form->file('Model.upload');
  5969. $this->assertHtml($expected, $result);
  5970. $this->View->setRequest(
  5971. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  5972. );
  5973. $result = $this->Form->file('Model.upload');
  5974. $this->assertHtml($expected, $result);
  5975. }
  5976. /**
  5977. * testFileUploadOnOtherModel method
  5978. *
  5979. * Test File upload input on a model not used in create().
  5980. */
  5981. public function testFileUploadOnOtherModel(): void
  5982. {
  5983. $this->Form->create($this->article, ['type' => 'file']);
  5984. $result = $this->Form->file('ValidateProfile.city');
  5985. $expected = [
  5986. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  5987. ];
  5988. $this->assertHtml($expected, $result);
  5989. }
  5990. /**
  5991. * testButton method
  5992. *
  5993. * Test generation of a form button.
  5994. */
  5995. public function testButton(): void
  5996. {
  5997. $result = $this->Form->button('Hi');
  5998. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5999. $this->assertHtml($expected, $result);
  6000. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6001. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6002. $this->assertHtml($expected, $result);
  6003. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6004. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6005. $this->assertHtml($expected, $result);
  6006. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6007. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6008. $this->assertHtml($expected, $result);
  6009. $result = $this->Form->button('No type', ['type' => false]);
  6010. $expected = ['button' => [], 'No type', '/button'];
  6011. $this->assertHtml($expected, $result);
  6012. $result = $this->Form->button('Upload Text', [
  6013. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6014. 'escape' => false,
  6015. ]);
  6016. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6017. }
  6018. /**
  6019. * testButtonUnlockedByDefault method
  6020. *
  6021. * Test that button() makes unlocked fields by default.
  6022. */
  6023. public function testButtonUnlockedByDefault(): void
  6024. {
  6025. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6026. $this->Form->create();
  6027. $this->Form->button('Save', ['name' => 'save']);
  6028. $this->Form->button('Clear');
  6029. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6030. $this->assertEquals(['save'], $result);
  6031. }
  6032. /**
  6033. * Test generation of a form button with confirm message.
  6034. */
  6035. public function testButtonWithConfirm(): void
  6036. {
  6037. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6038. $expected = ['button' => [
  6039. 'type' => 'submit',
  6040. 'data-confirm-message' => 'Confirm me!',
  6041. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6042. ], 'Hi', '/button'];
  6043. $this->assertHtml($expected, $result);
  6044. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6045. $expected = ['button' => [
  6046. 'type' => 'submit',
  6047. 'data-confirm-message' => 'Confirm "me"!',
  6048. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6049. ], 'Hi', '/button'];
  6050. $this->assertHtml($expected, $result);
  6051. }
  6052. /**
  6053. * testPostButton method
  6054. */
  6055. public function testPostButton(): void
  6056. {
  6057. $result = $this->Form->postButton('Hi', '/controller/action');
  6058. $expected = [
  6059. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6060. 'button' => ['type' => 'submit'],
  6061. 'Hi',
  6062. '/button',
  6063. '/form',
  6064. ];
  6065. $this->assertHtml($expected, $result);
  6066. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6067. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6068. }
  6069. /**
  6070. * testPostButtonMethodType method
  6071. */
  6072. public function testPostButtonMethodType(): void
  6073. {
  6074. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6075. $expected = [
  6076. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6077. 'div' => ['style' => 'display:none;'],
  6078. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6079. '/div',
  6080. 'button' => ['type' => 'submit'],
  6081. 'Hi',
  6082. '/button',
  6083. '/form',
  6084. ];
  6085. $this->assertHtml($expected, $result);
  6086. }
  6087. /**
  6088. * testPostButtonFormOptions method
  6089. */
  6090. public function testPostButtonFormOptions(): void
  6091. {
  6092. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6093. $expected = [
  6094. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6095. 'button' => ['type' => 'submit'],
  6096. 'Hi',
  6097. '/button',
  6098. '/form',
  6099. ];
  6100. $this->assertHtml($expected, $result);
  6101. }
  6102. /**
  6103. * testPostButtonNestedData method
  6104. *
  6105. * Test using postButton with N dimensional data.
  6106. */
  6107. public function testPostButtonNestedData(): void
  6108. {
  6109. $data = [
  6110. 'one' => [
  6111. 'two' => [
  6112. 3, 4, 5,
  6113. ],
  6114. ],
  6115. ];
  6116. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6117. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6118. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6119. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6120. }
  6121. /**
  6122. * testSecurePostButton method
  6123. *
  6124. * Test that postButton adds _Token fields.
  6125. */
  6126. public function testSecurePostButton(): void
  6127. {
  6128. $this->View->setRequest($this->View->getRequest()
  6129. ->withAttribute('csrfToken', 'testkey')
  6130. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6131. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6132. $tokenDebug = urlencode(json_encode([
  6133. '/posts/delete/1',
  6134. [],
  6135. [],
  6136. ]));
  6137. $expected = [
  6138. 'form' => [
  6139. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6140. ],
  6141. ['div' => ['style' => 'display:none;']],
  6142. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6143. '/div',
  6144. 'button' => ['type' => 'submit'],
  6145. 'Delete',
  6146. '/button',
  6147. ['div' => ['style' => 'display:none;']],
  6148. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6149. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6150. ['input' => [
  6151. 'type' => 'hidden',
  6152. 'name' => '_Token[debug]',
  6153. 'value' => $tokenDebug,
  6154. 'autocomplete' => 'off',
  6155. ]],
  6156. '/div',
  6157. '/form',
  6158. ];
  6159. $this->assertHtml($expected, $result);
  6160. }
  6161. /**
  6162. * testPostLink method
  6163. */
  6164. public function testPostLink(): void
  6165. {
  6166. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6167. $expected = [
  6168. 'form' => [
  6169. 'method' => 'post', 'action' => '/posts/delete/1',
  6170. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6171. ],
  6172. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6173. '/form',
  6174. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6175. 'Delete',
  6176. '/a',
  6177. ];
  6178. $this->assertHtml($expected, $result);
  6179. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6180. $expected = [
  6181. 'form' => [
  6182. 'method' => 'post', 'action' => '/posts/delete/1',
  6183. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6184. ],
  6185. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6186. '/form',
  6187. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6188. 'Delete',
  6189. '/a',
  6190. ];
  6191. $this->assertHtml($expected, $result);
  6192. $result = $this->Form->postLink(
  6193. 'Delete',
  6194. '/posts/delete/1',
  6195. ['target' => '_blank', 'class' => 'btn btn-danger']
  6196. );
  6197. $expected = [
  6198. 'form' => [
  6199. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6200. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6201. ],
  6202. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6203. '/form',
  6204. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6205. 'Delete',
  6206. '/a',
  6207. ];
  6208. $this->assertHtml($expected, $result);
  6209. }
  6210. /**
  6211. * testPostLinkWithConfirm method
  6212. *
  6213. * Test the confirm option for postLink().
  6214. */
  6215. public function testPostLinkWithConfirm(): void
  6216. {
  6217. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6218. $expected = [
  6219. 'form' => [
  6220. 'method' => 'post', 'action' => '/posts/delete/1',
  6221. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6222. ],
  6223. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6224. '/form',
  6225. 'a' => [
  6226. 'href' => '#',
  6227. 'data-confirm-message' => 'Confirm?',
  6228. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6229. ],
  6230. 'Delete',
  6231. '/a',
  6232. ];
  6233. $this->assertHtml($expected, $result);
  6234. $result = $this->Form->postLink(
  6235. 'Delete',
  6236. '/posts/delete/1',
  6237. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6238. );
  6239. $expected = [
  6240. 'form' => [
  6241. 'method' => 'post', 'action' => '/posts/delete/1',
  6242. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6243. ],
  6244. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6245. '/form',
  6246. 'a' => [
  6247. 'href' => '#',
  6248. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6249. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6250. ],
  6251. 'Delete',
  6252. '/a',
  6253. ];
  6254. $this->assertHtml($expected, $result);
  6255. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6256. $result = $this->Form->postLink(
  6257. 'Delete',
  6258. '/posts/delete/1',
  6259. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6260. );
  6261. $expected = [
  6262. 'form' => [
  6263. 'method' => 'post', 'action' => '/posts/delete/1',
  6264. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6265. ],
  6266. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6267. '/form',
  6268. 'a' => [
  6269. 'href' => '#',
  6270. 'data-confirm-message' => 'Confirm this deletion?',
  6271. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6272. ],
  6273. 'Delete',
  6274. '/a',
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. }
  6278. /**
  6279. * testPostLinkWithQuery method
  6280. *
  6281. * Test postLink() with query string args.
  6282. */
  6283. public function testPostLinkWithQuery(): void
  6284. {
  6285. $result = $this->Form->postLink(
  6286. 'Delete',
  6287. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6288. );
  6289. $expected = [
  6290. 'form' => [
  6291. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6292. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6293. ],
  6294. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6295. '/form',
  6296. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6297. 'Delete',
  6298. '/a',
  6299. ];
  6300. $this->assertHtml($expected, $result);
  6301. }
  6302. /**
  6303. * testPostLinkWithData method
  6304. *
  6305. * Test postLink with additional data.
  6306. */
  6307. public function testPostLinkWithData(): void
  6308. {
  6309. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6310. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6311. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6312. $this->Form->create($entity);
  6313. $this->Form->end();
  6314. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6315. $this->assertStringContainsString(
  6316. '<input type="hidden" name="name" value="show"',
  6317. $result,
  6318. 'should not contain entity data.'
  6319. );
  6320. }
  6321. /**
  6322. * testPostLinkSecurityHash method
  6323. *
  6324. * Test that security hashes for postLink include the url.
  6325. */
  6326. public function testPostLinkSecurityHash(): void
  6327. {
  6328. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6329. $hash .= '%3Aid';
  6330. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6331. $result = $this->Form->postLink(
  6332. 'Delete',
  6333. '/posts/delete/1',
  6334. ['data' => ['id' => 1]]
  6335. );
  6336. $tokenDebug = urlencode(json_encode([
  6337. '/posts/delete/1',
  6338. [
  6339. 'id' => 1,
  6340. ],
  6341. [],
  6342. ]));
  6343. $expected = [
  6344. 'form' => [
  6345. 'method' => 'post', 'action' => '/posts/delete/1',
  6346. 'name', 'style' => 'display:none;',
  6347. ],
  6348. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6349. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6350. 'div' => ['style' => 'display:none;'],
  6351. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6352. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6353. ['input' => [
  6354. 'type' => 'hidden',
  6355. 'name' => '_Token[debug]',
  6356. 'value' => $tokenDebug,
  6357. 'autocomplete' => 'off',
  6358. ]],
  6359. '/div',
  6360. '/form',
  6361. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6362. 'Delete',
  6363. '/a',
  6364. ];
  6365. $this->assertHtml($expected, $result);
  6366. }
  6367. /**
  6368. * testPostLinkSecurityHashBlockMode method
  6369. *
  6370. * Test that postLink doesn't modify the fields in the containing form.
  6371. *
  6372. * postLink() calls inside open forms should not modify the field list
  6373. * for the form.
  6374. */
  6375. public function testPostLinkSecurityHashBlockMode(): void
  6376. {
  6377. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6378. $hash .= '%3A';
  6379. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6380. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6381. $this->Form->control('title');
  6382. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6383. $result = $this->View->fetch('postLink');
  6384. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6385. $this->assertEquals(['title'], $fields);
  6386. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6387. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6388. $reflect->setAccessible(true);
  6389. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6390. }
  6391. /**
  6392. * testPostLinkSecurityHashNoDebugMode method
  6393. *
  6394. * Test that security does not include debug token if debug is false.
  6395. */
  6396. public function testPostLinkSecurityHashNoDebugMode(): void
  6397. {
  6398. Configure::write('debug', false);
  6399. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6400. $hash .= '%3Aid';
  6401. $this->View->setRequest($this->View->getRequest()
  6402. ->withAttribute('formTokenData', ['key' => 'test']));
  6403. $result = $this->Form->postLink(
  6404. 'Delete',
  6405. '/posts/delete/1',
  6406. ['data' => ['id' => 1]]
  6407. );
  6408. $expected = [
  6409. 'form' => [
  6410. 'method' => 'post', 'action' => '/posts/delete/1',
  6411. 'name', 'style' => 'display:none;',
  6412. ],
  6413. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6414. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6415. 'div' => ['style' => 'display:none;'],
  6416. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6417. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6418. '/div',
  6419. '/form',
  6420. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6421. 'Delete',
  6422. '/a',
  6423. ];
  6424. $this->assertHtml($expected, $result);
  6425. }
  6426. /**
  6427. * testPostLinkNestedData method
  6428. *
  6429. * Test using postLink with N dimensional data.
  6430. */
  6431. public function testPostLinkNestedData(): void
  6432. {
  6433. $data = [
  6434. 'one' => [
  6435. 'two' => [
  6436. 3, 4, 5,
  6437. ],
  6438. ],
  6439. ];
  6440. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6441. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6442. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6443. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6444. }
  6445. /**
  6446. * testPostLinkAfterGetForm method
  6447. *
  6448. * Test creating postLinks after a GET form.
  6449. */
  6450. public function testPostLinkAfterGetForm(): void
  6451. {
  6452. $this->View->setRequest($this->View->getRequest()
  6453. ->withAttribute('csrfToken', 'testkey')
  6454. ->withAttribute('formTokenData', []));
  6455. $this->Form->create($this->article, ['type' => 'get']);
  6456. $this->Form->end();
  6457. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6458. $tokenDebug = urlencode(json_encode([
  6459. '/posts/delete/1',
  6460. [],
  6461. [],
  6462. ]));
  6463. $expected = [
  6464. 'form' => [
  6465. 'method' => 'post', 'action' => '/posts/delete/1',
  6466. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6467. ],
  6468. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6469. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6470. 'div' => ['style' => 'display:none;'],
  6471. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6472. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6473. ['input' => [
  6474. 'type' => 'hidden', 'name' => '_Token[debug]',
  6475. 'value' => $tokenDebug,
  6476. 'autocomplete' => 'off',
  6477. ]],
  6478. '/div',
  6479. '/form',
  6480. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6481. 'Delete',
  6482. '/a',
  6483. ];
  6484. $this->assertHtml($expected, $result);
  6485. }
  6486. /**
  6487. * testPostLinkFormBuffer method
  6488. *
  6489. * Test that postLink adds form tags to view block.
  6490. */
  6491. public function testPostLinkFormBuffer(): void
  6492. {
  6493. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6494. $expected = [
  6495. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6496. 'Delete',
  6497. '/a',
  6498. ];
  6499. $this->assertHtml($expected, $result);
  6500. $result = $this->View->fetch('postLink');
  6501. $expected = [
  6502. 'form' => [
  6503. 'method' => 'post', 'action' => '/posts/delete/1',
  6504. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6505. ],
  6506. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6507. '/form',
  6508. ];
  6509. $this->assertHtml($expected, $result);
  6510. $result = $this->Form->postLink(
  6511. 'Delete',
  6512. '/posts/delete/2',
  6513. ['block' => true, 'method' => 'DELETE']
  6514. );
  6515. $expected = [
  6516. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6517. 'Delete',
  6518. '/a',
  6519. ];
  6520. $this->assertHtml($expected, $result);
  6521. $result = $this->View->fetch('postLink');
  6522. $expected = [
  6523. 'form' => [
  6524. 'method' => 'post', 'action' => '/posts/delete/1',
  6525. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6526. ],
  6527. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6528. '/form',
  6529. [
  6530. 'form' => [
  6531. 'method' => 'post', 'action' => '/posts/delete/2',
  6532. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6533. ],
  6534. ],
  6535. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6536. '/form',
  6537. ];
  6538. $this->assertHtml($expected, $result);
  6539. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6540. $expected = [
  6541. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6542. 'Delete',
  6543. '/a',
  6544. ];
  6545. $this->assertHtml($expected, $result);
  6546. $result = $this->View->fetch('foobar');
  6547. $expected = [
  6548. 'form' => [
  6549. 'method' => 'post', 'action' => '/posts/delete/1',
  6550. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6551. ],
  6552. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6553. '/form',
  6554. ];
  6555. $this->assertHtml($expected, $result);
  6556. }
  6557. /**
  6558. * testSubmitButton method
  6559. */
  6560. public function testSubmitButton(): void
  6561. {
  6562. $result = $this->Form->submit('');
  6563. $expected = [
  6564. 'div' => ['class' => 'submit'],
  6565. 'input' => ['type' => 'submit', 'value' => ''],
  6566. '/div',
  6567. ];
  6568. $this->assertHtml($expected, $result);
  6569. $result = $this->Form->submit('Test Submit');
  6570. $expected = [
  6571. 'div' => ['class' => 'submit'],
  6572. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6573. '/div',
  6574. ];
  6575. $this->assertHtml($expected, $result);
  6576. $result = $this->Form->submit('Next >');
  6577. $expected = [
  6578. 'div' => ['class' => 'submit'],
  6579. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6580. '/div',
  6581. ];
  6582. $this->assertHtml($expected, $result);
  6583. $result = $this->Form->submit('Next >', ['escape' => false]);
  6584. $expected = [
  6585. 'div' => ['class' => 'submit'],
  6586. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6587. '/div',
  6588. ];
  6589. $this->assertHtml($expected, $result);
  6590. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6591. $expected = [
  6592. 'div' => ['class' => 'submit'],
  6593. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6594. '/div',
  6595. ];
  6596. $this->assertHtml($expected, $result);
  6597. }
  6598. /**
  6599. * testSubmitImage method
  6600. *
  6601. * Test image submit types.
  6602. */
  6603. public function testSubmitImage(): void
  6604. {
  6605. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6606. $expected = [
  6607. 'div' => ['class' => 'submit'],
  6608. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6609. '/div',
  6610. ];
  6611. $this->assertHtml($expected, $result);
  6612. $result = $this->Form->submit('/relative/cake.power.gif');
  6613. $expected = [
  6614. 'div' => ['class' => 'submit'],
  6615. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6616. '/div',
  6617. ];
  6618. $this->assertHtml($expected, $result);
  6619. $result = $this->Form->submit('cake.power.gif');
  6620. $expected = [
  6621. 'div' => ['class' => 'submit'],
  6622. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6623. '/div',
  6624. ];
  6625. $this->assertHtml($expected, $result);
  6626. $result = $this->Form->submit('Not.an.image');
  6627. $expected = [
  6628. 'div' => ['class' => 'submit'],
  6629. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6630. '/div',
  6631. ];
  6632. $this->assertHtml($expected, $result);
  6633. }
  6634. /**
  6635. * testSubmitUnlockedByDefault method
  6636. *
  6637. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6638. * be submitted at a time.
  6639. */
  6640. public function testSubmitUnlockedByDefault(): void
  6641. {
  6642. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6643. $this->Form->create();
  6644. $this->Form->submit('Go go');
  6645. $this->Form->submit('Save', ['name' => 'save']);
  6646. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6647. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6648. }
  6649. /**
  6650. * testSubmitImageTimestamp method
  6651. *
  6652. * Test submit image with timestamps.
  6653. */
  6654. public function testSubmitImageTimestamp(): void
  6655. {
  6656. Configure::write('Asset.timestamp', 'force');
  6657. $result = $this->Form->submit('cake.power.gif');
  6658. $expected = [
  6659. 'div' => ['class' => 'submit'],
  6660. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6661. '/div',
  6662. ];
  6663. $this->assertHtml($expected, $result);
  6664. }
  6665. /**
  6666. * testDateTimeWithGetForms method
  6667. *
  6668. * Test that datetime() works with GET style forms.
  6669. */
  6670. public function testDateTimeWithGetForms(): void
  6671. {
  6672. $this->Form->create($this->article, ['type' => 'get']);
  6673. $result = $this->Form->datetime('created');
  6674. $expected = [
  6675. 'input' => [
  6676. 'type' => 'datetime-local',
  6677. 'name' => 'created',
  6678. 'value' => '',
  6679. 'step' => '1',
  6680. ],
  6681. ];
  6682. $this->assertHtml($expected, $result);
  6683. $result = $this->Form->datetime('created', ['default' => true]);
  6684. $expected = [
  6685. 'input' => [
  6686. 'type' => 'datetime-local',
  6687. 'name' => 'created',
  6688. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6689. 'step' => '1',
  6690. ],
  6691. ];
  6692. $this->assertHtml($expected, $result);
  6693. }
  6694. /**
  6695. * Provides fractional schema types
  6696. *
  6697. * @return array
  6698. */
  6699. public function fractionalTypeProvider(): array
  6700. {
  6701. return [
  6702. ['datetimefractional'],
  6703. ['timestampfractional'],
  6704. ['timestamptimezone'],
  6705. ];
  6706. }
  6707. /**
  6708. * testDateTimeWithFractional method
  6709. *
  6710. * Test that datetime() works with datetimefractional.
  6711. *
  6712. * @dataProvider fractionalTypeProvider
  6713. */
  6714. public function testDateTimeWithFractional(string $type): void
  6715. {
  6716. $this->Form->create([
  6717. 'schema' => [
  6718. 'created' => ['type' => $type],
  6719. ],
  6720. ]);
  6721. $result = $this->Form->datetime('created', [
  6722. 'val' => new FrozenTime('2019-09-27 02:52:43.123'),
  6723. ]);
  6724. $expected = [
  6725. 'input' => [
  6726. 'type' => 'datetime-local',
  6727. 'name' => 'created',
  6728. 'value' => '2019-09-27T02:52:43.123',
  6729. 'step' => '0.001',
  6730. ],
  6731. ];
  6732. $this->assertHtml($expected, $result);
  6733. }
  6734. /**
  6735. * testControlWithFractional method
  6736. *
  6737. * Test that control() works with datetimefractional.
  6738. *
  6739. * @dataProvider fractionalTypeProvider
  6740. */
  6741. public function testControlWithFractional(string $type): void
  6742. {
  6743. $this->Form->create([
  6744. 'schema' => [
  6745. 'created' => ['type' => $type],
  6746. ],
  6747. ]);
  6748. $result = $this->Form->control('created', [
  6749. 'val' => new FrozenTime('2019-09-27 02:52:43.123'),
  6750. ]);
  6751. $expected = [
  6752. 'div' => ['class' => 'input datetime'],
  6753. 'label' => ['for' => 'created'],
  6754. 'Created',
  6755. '/label',
  6756. 'input' => [
  6757. 'type' => 'datetime-local',
  6758. 'name' => 'created',
  6759. 'id' => 'created',
  6760. 'value' => '2019-09-27T02:52:43.123',
  6761. 'step' => '0.001',
  6762. ],
  6763. '/div',
  6764. ];
  6765. $this->assertHtml($expected, $result);
  6766. }
  6767. /**
  6768. * testForMagicControlNonExistentNotValidated method
  6769. */
  6770. public function testForMagicControlNonExistentNotValidated(): void
  6771. {
  6772. $this->Form->create($this->article);
  6773. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6774. $result = $this->Form->control('nonexistent_not_validated');
  6775. $expected = [
  6776. 'label' => ['for' => 'nonexistent-not-validated'],
  6777. 'Nonexistent Not Validated',
  6778. '/label',
  6779. 'input' => [
  6780. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6781. 'id' => 'nonexistent-not-validated',
  6782. ],
  6783. ];
  6784. $this->assertHtml($expected, $result);
  6785. $result = $this->Form->control('nonexistent_not_validated', [
  6786. 'val' => 'my value',
  6787. ]);
  6788. $expected = [
  6789. 'label' => ['for' => 'nonexistent-not-validated'],
  6790. 'Nonexistent Not Validated',
  6791. '/label',
  6792. 'input' => [
  6793. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6794. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6795. ],
  6796. ];
  6797. $this->assertHtml($expected, $result);
  6798. $this->View->setRequest(
  6799. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6800. );
  6801. $this->Form->create($this->article);
  6802. $result = $this->Form->control('nonexistent_not_validated');
  6803. $expected = [
  6804. 'label' => ['for' => 'nonexistent-not-validated'],
  6805. 'Nonexistent Not Validated',
  6806. '/label',
  6807. 'input' => [
  6808. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6809. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6810. ],
  6811. ];
  6812. $this->assertHtml($expected, $result);
  6813. }
  6814. /**
  6815. * testFormMagicControlLabel method
  6816. */
  6817. public function testFormMagicControlLabel(): void
  6818. {
  6819. $this->getTableLocator()->get('Contacts', [
  6820. 'className' => ContactsTable::class,
  6821. ]);
  6822. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6823. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6824. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6825. $expected = [
  6826. 'label' => ['for' => 'contacts-name'],
  6827. 'My label',
  6828. '/label',
  6829. 'input' => [
  6830. 'type' => 'text',
  6831. 'name' => 'Contacts[name]',
  6832. 'id' => 'contacts-name',
  6833. 'maxlength' => '255',
  6834. ],
  6835. ];
  6836. $this->assertHtml($expected, $result);
  6837. $result = $this->Form->control('name', [
  6838. 'label' => ['class' => 'mandatory'],
  6839. ]);
  6840. $expected = [
  6841. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6842. 'Name',
  6843. '/label',
  6844. 'input' => [
  6845. 'type' => 'text', 'name' => 'name',
  6846. 'id' => 'name', 'maxlength' => '255',
  6847. ],
  6848. ];
  6849. $this->assertHtml($expected, $result);
  6850. $result = $this->Form->control('name', [
  6851. 'div' => false,
  6852. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6853. ]);
  6854. $expected = [
  6855. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6856. 'My label',
  6857. '/label',
  6858. 'input' => [
  6859. 'type' => 'text', 'name' => 'name',
  6860. 'id' => 'name', 'maxlength' => '255',
  6861. ],
  6862. ];
  6863. $this->assertHtml($expected, $result);
  6864. $result = $this->Form->control('Contact.name', [
  6865. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6866. ]);
  6867. $expected = [
  6868. 'label' => ['for' => 'my_id'],
  6869. 'Name',
  6870. '/label',
  6871. 'input' => [
  6872. 'type' => 'text', 'name' => 'Contact[name]',
  6873. 'id' => 'my_id', 'maxlength' => '255',
  6874. ],
  6875. ];
  6876. $this->assertHtml($expected, $result);
  6877. $result = $this->Form->control('1.id');
  6878. $expected = ['input' => [
  6879. 'type' => 'hidden', 'name' => '1[id]',
  6880. 'id' => '1-id',
  6881. ]];
  6882. $this->assertHtml($expected, $result);
  6883. $result = $this->Form->control('1.name');
  6884. $expected = [
  6885. 'label' => ['for' => '1-name'],
  6886. 'Name',
  6887. '/label',
  6888. 'input' => [
  6889. 'type' => 'text', 'name' => '1[name]',
  6890. 'id' => '1-name', 'maxlength' => '255',
  6891. ],
  6892. ];
  6893. $this->assertHtml($expected, $result);
  6894. }
  6895. /**
  6896. * testFormEnd method
  6897. */
  6898. public function testFormEnd(): void
  6899. {
  6900. $this->assertSame('</form>', $this->Form->end());
  6901. }
  6902. /**
  6903. * testMultiRecordForm method
  6904. *
  6905. * Test the generation of fields for a multi record form.
  6906. */
  6907. public function testMultiRecordForm(): void
  6908. {
  6909. $articles = $this->getTableLocator()->get('Articles');
  6910. $articles->hasMany('Comments');
  6911. $comment = new Entity(['comment' => 'Value']);
  6912. $article = new Article(['comments' => [$comment]]);
  6913. $this->Form->create([$article]);
  6914. $result = $this->Form->control('0.comments.1.comment');
  6915. // phpcs:disable
  6916. $expected = [
  6917. 'div' => ['class' => 'input textarea'],
  6918. 'label' => ['for' => '0-comments-1-comment'],
  6919. 'Comment',
  6920. '/label',
  6921. 'textarea' => [
  6922. 'name',
  6923. 'id' => '0-comments-1-comment',
  6924. 'rows' => 5
  6925. ],
  6926. '/textarea',
  6927. '/div'
  6928. ];
  6929. // phpcs:enable
  6930. $this->assertHtml($expected, $result);
  6931. $result = $this->Form->control('0.comments.0.comment');
  6932. // phpcs:disable
  6933. $expected = [
  6934. 'div' => ['class' => 'input textarea'],
  6935. 'label' => ['for' => '0-comments-0-comment'],
  6936. 'Comment',
  6937. '/label',
  6938. 'textarea' => [
  6939. 'name',
  6940. 'id' => '0-comments-0-comment',
  6941. 'rows' => 5
  6942. ],
  6943. 'Value',
  6944. '/textarea',
  6945. '/div'
  6946. ];
  6947. // phpcs:enable
  6948. $this->assertHtml($expected, $result);
  6949. $comment->setError('comment', ['Not valid']);
  6950. $result = $this->Form->control('0.comments.0.comment');
  6951. // phpcs:disable
  6952. $expected = [
  6953. 'div' => ['class' => 'input textarea error'],
  6954. 'label' => ['for' => '0-comments-0-comment'],
  6955. 'Comment',
  6956. '/label',
  6957. 'textarea' => [
  6958. 'name',
  6959. 'class' => 'form-error',
  6960. 'id' => '0-comments-0-comment',
  6961. 'aria-invalid' => 'true',
  6962. 'aria-describedby' => '0-comments-0-comment-error',
  6963. 'rows' => 5
  6964. ],
  6965. 'Value',
  6966. '/textarea',
  6967. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  6968. 'Not valid',
  6969. '/div',
  6970. '/div'
  6971. ];
  6972. // phpcs:enable
  6973. $this->assertHtml($expected, $result);
  6974. $this->getTableLocator()->get('Comments')
  6975. ->getValidator('default')
  6976. ->allowEmptyString('comment', null, false);
  6977. $result = $this->Form->control('0.comments.1.comment');
  6978. // phpcs:disable
  6979. $expected = [
  6980. 'div' => ['class' => 'input textarea required'],
  6981. 'label' => ['for' => '0-comments-1-comment'],
  6982. 'Comment',
  6983. '/label',
  6984. 'textarea' => [
  6985. 'name',
  6986. 'aria-required' => 'true',
  6987. 'required' => 'required',
  6988. 'id' => '0-comments-1-comment',
  6989. 'rows' => 5,
  6990. 'data-validity-message' => 'This field cannot be left empty',
  6991. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  6992. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  6993. ],
  6994. '/textarea',
  6995. '/div'
  6996. ];
  6997. // phpcs:enable
  6998. $this->assertHtml($expected, $result);
  6999. }
  7000. /**
  7001. * testHtml5Controls method
  7002. *
  7003. * Test that some html5 inputs + FormHelper::__call() work.
  7004. */
  7005. public function testHtml5Controls(): void
  7006. {
  7007. $result = $this->Form->email('User.email');
  7008. $expected = [
  7009. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7010. ];
  7011. $this->assertHtml($expected, $result);
  7012. $result = $this->Form->search('User.query');
  7013. $expected = [
  7014. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7015. ];
  7016. $this->assertHtml($expected, $result);
  7017. $result = $this->Form->search('User.query', ['value' => 'test']);
  7018. $expected = [
  7019. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7020. ];
  7021. $this->assertHtml($expected, $result);
  7022. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7023. $expected = [
  7024. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7025. ];
  7026. $this->assertHtml($expected, $result);
  7027. }
  7028. /**
  7029. * testHtml5ControlWithControl method
  7030. *
  7031. * Test accessing html5 inputs through control().
  7032. */
  7033. public function testHtml5ControlWithControl(): void
  7034. {
  7035. $this->Form->create();
  7036. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7037. $result = $this->Form->control('website', [
  7038. 'type' => 'url',
  7039. 'val' => 'http://domain.tld',
  7040. 'label' => false,
  7041. ]);
  7042. $expected = [
  7043. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7044. ];
  7045. $this->assertHtml($expected, $result);
  7046. }
  7047. /**
  7048. * testHtml5ControlException method
  7049. *
  7050. * Test errors when field name is missing.
  7051. */
  7052. public function testHtml5ControlException(): void
  7053. {
  7054. $this->expectException(CakeException::class);
  7055. $this->Form->email();
  7056. }
  7057. /**
  7058. * tests fields that are required use custom validation messages
  7059. */
  7060. public function testHtml5ErrorMessage(): void
  7061. {
  7062. $this->Form->setConfig('autoSetCustomValidity', true);
  7063. $validator = (new Validator())
  7064. ->notEmptyString('email', 'Custom error message')
  7065. ->requirePresence('password')
  7066. ->alphaNumeric('password')
  7067. ->notBlank('phone');
  7068. $table = $this->getTableLocator()->get('Contacts', [
  7069. 'className' => ContactsTable::class,
  7070. ]);
  7071. $table->setValidator('default', $validator);
  7072. $contact = new Entity();
  7073. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7074. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7075. $result = $this->Form->control('password');
  7076. $expected = [
  7077. 'label' => ['for' => 'password'],
  7078. 'Password',
  7079. '/label',
  7080. 'input' => [
  7081. 'aria-required' => 'true',
  7082. 'id' => 'password',
  7083. 'name' => 'password',
  7084. 'type' => 'password',
  7085. 'value' => '',
  7086. 'required' => 'required',
  7087. 'data-validity-message' => 'This field cannot be left empty',
  7088. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7089. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7090. ],
  7091. ];
  7092. $this->assertHtml($expected, $result);
  7093. $result = $this->Form->control('phone');
  7094. $expected = [
  7095. 'label' => ['for' => 'phone'],
  7096. 'Phone',
  7097. '/label',
  7098. 'input' => [
  7099. 'aria-required' => 'true',
  7100. 'id' => 'phone',
  7101. 'name' => 'phone',
  7102. 'type' => 'tel',
  7103. 'value' => '',
  7104. 'maxlength' => 255,
  7105. 'required' => 'required',
  7106. 'data-validity-message' => 'This field cannot be left empty',
  7107. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7108. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7109. ],
  7110. ];
  7111. $this->assertHtml($expected, $result);
  7112. $result = $this->Form->control('email');
  7113. $expected = [
  7114. 'label' => ['for' => 'email'],
  7115. 'Email',
  7116. '/label',
  7117. 'input' => [
  7118. 'aria-required' => 'true',
  7119. 'id' => 'email',
  7120. 'name' => 'email',
  7121. 'type' => 'email',
  7122. 'value' => '',
  7123. 'maxlength' => 255,
  7124. 'required' => 'required',
  7125. 'data-validity-message' => 'Custom error message',
  7126. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7127. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7128. ],
  7129. ];
  7130. $this->assertHtml($expected, $result);
  7131. }
  7132. /**
  7133. * tests that custom validation messages are in templateVars
  7134. */
  7135. public function testHtml5ErrorMessageInTemplateVars(): void
  7136. {
  7137. $validator = (new Validator())
  7138. ->notEmptyString('email', 'Custom error "message" & entities')
  7139. ->requirePresence('password')
  7140. ->alphaNumeric('password')
  7141. ->notBlank('phone');
  7142. $table = $this->getTableLocator()->get('Contacts', [
  7143. 'className' => ContactsTable::class,
  7144. ]);
  7145. $table->setValidator('default', $validator);
  7146. $contact = new Entity();
  7147. $this->Form->setConfig('autoSetCustomValidity', false);
  7148. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7149. $this->Form->setTemplates([
  7150. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7151. 'inputContainer' => '{{content}}',
  7152. ]);
  7153. $result = $this->Form->control('password');
  7154. $expected = [
  7155. 'label' => ['for' => 'password'],
  7156. 'Password',
  7157. '/label',
  7158. 'input' => [
  7159. 'aria-required' => 'true',
  7160. 'id' => 'password',
  7161. 'name' => 'password',
  7162. 'type' => 'password',
  7163. 'value' => '',
  7164. 'required' => 'required',
  7165. 'data-message' => 'This field cannot be left empty',
  7166. ],
  7167. ];
  7168. $this->assertHtml($expected, $result);
  7169. $result = $this->Form->control('phone');
  7170. $expected = [
  7171. 'label' => ['for' => 'phone'],
  7172. 'Phone',
  7173. '/label',
  7174. 'input' => [
  7175. 'aria-required' => 'true',
  7176. 'id' => 'phone',
  7177. 'name' => 'phone',
  7178. 'type' => 'tel',
  7179. 'value' => '',
  7180. 'maxlength' => 255,
  7181. 'required' => 'required',
  7182. 'data-message' => 'This field cannot be left empty',
  7183. ],
  7184. ];
  7185. $this->assertHtml($expected, $result);
  7186. $result = $this->Form->control('email', [
  7187. 'templateVars' => [
  7188. 'custom' => 'data-custom="1"',
  7189. ],
  7190. ]);
  7191. $expected = [
  7192. 'label' => ['for' => 'email'],
  7193. 'Email',
  7194. '/label',
  7195. 'input' => [
  7196. 'aria-required' => 'true',
  7197. 'id' => 'email',
  7198. 'name' => 'email',
  7199. 'type' => 'email',
  7200. 'value' => '',
  7201. 'maxlength' => 255,
  7202. 'required' => 'required',
  7203. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7204. 'data-custom' => '1',
  7205. ],
  7206. ];
  7207. $this->assertHtml($expected, $result);
  7208. $this->Form->setConfig('autoSetCustomValidity', true);
  7209. }
  7210. /**
  7211. * testRequiredAttribute method
  7212. *
  7213. * Tests that formhelper sets required attributes.
  7214. */
  7215. public function testRequiredAttribute(): void
  7216. {
  7217. $this->article['required'] = [
  7218. 'title' => true,
  7219. 'body' => false,
  7220. ];
  7221. $this->Form->create($this->article);
  7222. $result = $this->Form->control('title');
  7223. $expected = [
  7224. 'div' => ['class' => 'input text required'],
  7225. 'label' => ['for' => 'title'],
  7226. 'Title',
  7227. '/label',
  7228. 'input' => [
  7229. 'type' => 'text',
  7230. 'name' => 'title',
  7231. 'id' => 'title',
  7232. 'aria-required' => 'true',
  7233. 'required' => 'required',
  7234. 'data-validity-message' => 'This field cannot be left empty',
  7235. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7236. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7237. ],
  7238. '/div',
  7239. ];
  7240. $this->assertHtml($expected, $result);
  7241. $result = $this->Form->control('title', ['required' => false]);
  7242. $this->assertStringNotContainsString('required', $result);
  7243. $result = $this->Form->control('body');
  7244. $expected = [
  7245. 'div' => ['class' => 'input text'],
  7246. 'label' => ['for' => 'body'],
  7247. 'Body',
  7248. '/label',
  7249. 'input' => [
  7250. 'type' => 'text',
  7251. 'name' => 'body',
  7252. 'id' => 'body',
  7253. ],
  7254. '/div',
  7255. ];
  7256. $this->assertHtml($expected, $result);
  7257. $result = $this->Form->control('body', ['required' => true]);
  7258. $this->assertStringContainsString('required', $result);
  7259. }
  7260. /**
  7261. * testControlsNotNested method
  7262. *
  7263. * Tests that it is possible to put inputs outside of the label.
  7264. */
  7265. public function testControlsNotNested(): void
  7266. {
  7267. $this->Form->setTemplates([
  7268. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7269. 'formGroup' => '{{input}}{{label}}',
  7270. ]);
  7271. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7272. $expected = [
  7273. 'div' => ['class' => 'input checkbox'],
  7274. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7275. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7276. 'label' => ['for' => 'foo'],
  7277. 'Foo',
  7278. '/label',
  7279. '/div',
  7280. ];
  7281. $this->assertHtml($expected, $result);
  7282. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7283. $expected = [
  7284. 'div' => ['class' => 'input checkbox'],
  7285. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7286. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7287. '/div',
  7288. ];
  7289. $this->assertHtml($expected, $result);
  7290. $result = $this->Form->control('confirm', [
  7291. 'type' => 'radio',
  7292. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7293. ]);
  7294. $expected = [
  7295. 'div' => ['class' => 'input radio'],
  7296. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7297. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7298. ['label' => ['for' => 'confirm-y']],
  7299. 'Yes',
  7300. '/label',
  7301. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7302. ['label' => ['for' => 'confirm-n']],
  7303. 'No',
  7304. '/label',
  7305. '<label',
  7306. 'Confirm',
  7307. '/label',
  7308. '/div',
  7309. ];
  7310. $this->assertHtml($expected, $result);
  7311. $result = $this->Form->select('category', ['1', '2'], [
  7312. 'multiple' => 'checkbox',
  7313. 'name' => 'fish',
  7314. ]);
  7315. $expected = [
  7316. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7317. ['div' => ['class' => 'checkbox']],
  7318. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7319. ['label' => ['for' => 'fish-0']],
  7320. '1',
  7321. '/label',
  7322. '/div',
  7323. ['div' => ['class' => 'checkbox']],
  7324. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7325. ['label' => ['for' => 'fish-1']],
  7326. '2',
  7327. '/label',
  7328. '/div',
  7329. ];
  7330. $this->assertHtml($expected, $result);
  7331. }
  7332. /**
  7333. * testControlContainerTemplates method
  7334. *
  7335. * Test that *Container templates are used by input.
  7336. */
  7337. public function testControlContainerTemplates(): void
  7338. {
  7339. $this->Form->setTemplates([
  7340. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7341. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7342. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7343. ]);
  7344. $this->article['errors'] = [
  7345. 'Article' => ['published' => 'error message'],
  7346. ];
  7347. $this->Form->create($this->article);
  7348. $result = $this->Form->control('accept', [
  7349. 'type' => 'checkbox',
  7350. ]);
  7351. $expected = [
  7352. 'div' => ['class' => 'check'],
  7353. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7354. 'label' => ['for' => 'accept'],
  7355. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7356. 'Accept',
  7357. '/label',
  7358. '/div',
  7359. ];
  7360. $this->assertHtml($expected, $result);
  7361. $result = $this->Form->control('accept', [
  7362. 'type' => 'radio',
  7363. 'options' => ['Y', 'N'],
  7364. ]);
  7365. $this->assertStringContainsString('<div class="rad">', $result);
  7366. $result = $this->Form->control('Article.published', [
  7367. 'type' => 'radio',
  7368. 'options' => ['Y', 'N'],
  7369. ]);
  7370. $this->assertStringContainsString('<div class="rad err">', $result);
  7371. }
  7372. /**
  7373. * testFormGroupTemplates method
  7374. *
  7375. * Test that *Container templates are used by input.
  7376. */
  7377. public function testFormGroupTemplates(): void
  7378. {
  7379. $this->Form->setTemplates([
  7380. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7381. ]);
  7382. $this->Form->create($this->article);
  7383. $result = $this->Form->control('accept', [
  7384. 'type' => 'radio',
  7385. 'options' => ['Y', 'N'],
  7386. ]);
  7387. $this->assertStringContainsString('<div class="radio">', $result);
  7388. }
  7389. /**
  7390. * testResetTemplates method
  7391. *
  7392. * Test resetting templates.
  7393. */
  7394. public function testResetTemplates(): void
  7395. {
  7396. $this->Form->setTemplates(['input' => '<input/>']);
  7397. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7398. $this->Form->resetTemplates();
  7399. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7400. }
  7401. /**
  7402. * testContext method
  7403. *
  7404. * Test the context method.
  7405. */
  7406. public function testContext(): void
  7407. {
  7408. $result = $this->Form->context();
  7409. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7410. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7411. $this->assertSame($mock, $this->Form->context($mock));
  7412. $this->assertSame($mock, $this->Form->context());
  7413. }
  7414. /**
  7415. * testAutoDomId method
  7416. */
  7417. public function testAutoDomId(): void
  7418. {
  7419. $result = $this->Form->text('field', ['id' => true]);
  7420. $expected = [
  7421. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7422. ];
  7423. $this->assertHtml($expected, $result);
  7424. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7425. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7426. $expected = [
  7427. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7428. ['label' => ['for' => 'field-0']],
  7429. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7430. 'option A',
  7431. '/label',
  7432. ['label' => ['for' => 'field-1']],
  7433. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7434. 'option B',
  7435. '/label',
  7436. ];
  7437. $this->assertHtml($expected, $result);
  7438. $result = $this->Form->select(
  7439. 'multi_field',
  7440. ['first', 'second'],
  7441. ['multiple' => 'checkbox', 'id' => true]
  7442. );
  7443. $expected = [
  7444. 'input' => [
  7445. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7446. ],
  7447. ['div' => ['class' => 'checkbox']],
  7448. ['label' => ['for' => 'multi-field-0']],
  7449. ['input' => [
  7450. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7451. 'value' => '0', 'id' => 'multi-field-0',
  7452. ]],
  7453. 'first',
  7454. '/label',
  7455. '/div',
  7456. ['div' => ['class' => 'checkbox']],
  7457. ['label' => ['for' => 'multi-field-1']],
  7458. ['input' => [
  7459. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7460. 'value' => '1', 'id' => 'multi-field-1',
  7461. ]],
  7462. 'second',
  7463. '/label',
  7464. '/div',
  7465. ];
  7466. $this->assertHtml($expected, $result);
  7467. }
  7468. /**
  7469. * Test the basic setters and getters for value sources
  7470. */
  7471. public function testFormValueSourcesSettersGetters(): void
  7472. {
  7473. $this->View->setRequest($this->View->getRequest()
  7474. ->withData('id', '1')
  7475. ->withQueryParams(['id' => '2']));
  7476. $expected = ['data', 'context'];
  7477. $result = $this->Form->getValueSources();
  7478. $this->assertEquals($expected, $result);
  7479. $this->Form->setValueSources(['context']);
  7480. $result = $this->Form->getSourceValue('id');
  7481. $this->assertNull($result);
  7482. $this->Form->setValueSources('query');
  7483. $expected = ['query'];
  7484. $result = $this->Form->getValueSources();
  7485. $this->assertEquals($expected, $result);
  7486. $expected = '2';
  7487. $result = $this->Form->getSourceValue('id');
  7488. $this->assertSame($expected, $result);
  7489. $this->Form->setValueSources(['data']);
  7490. $expected = '1';
  7491. $result = $this->Form->getSourceValue('id');
  7492. $this->assertSame($expected, $result);
  7493. $this->Form->setValueSources(['query', 'data']);
  7494. $expected = '2';
  7495. $result = $this->Form->getSourceValue('id');
  7496. $this->assertSame($expected, $result);
  7497. }
  7498. public function testValueSourcesValidation(): void
  7499. {
  7500. $this->expectException(InvalidArgumentException::class);
  7501. $this->expectExceptionMessage('Invalid value source(s): invalid, foo. Valid values are: context, data, query');
  7502. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7503. }
  7504. /**
  7505. * Tests the different input rendering values based on sources values switching
  7506. */
  7507. public function testFormValueSourcesSingleSwitchRendering(): void
  7508. {
  7509. $articles = $this->getTableLocator()->get('Articles');
  7510. $article = new Article();
  7511. $articles->patchEntity($article, ['id' => '3']);
  7512. $this->Form->create($article);
  7513. $this->Form->setValueSources(['context']);
  7514. $result = $this->Form->control('id');
  7515. $expected = [
  7516. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7517. ];
  7518. $this->assertHtml($expected, $result);
  7519. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7520. $this->Form->setValueSources(['query']);
  7521. $result = $this->Form->control('id');
  7522. $expected = [
  7523. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7524. ];
  7525. $this->assertHtml($expected, $result);
  7526. $this->View->setRequest($this->View->getRequest()
  7527. ->withQueryParams(['id' => '5a'])
  7528. ->withData('id', '5b'));
  7529. $this->Form->setValueSources(['context']);
  7530. $this->Form->create($article);
  7531. $result = $this->Form->control('id');
  7532. $expected = [
  7533. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7534. ];
  7535. $this->assertHtml($expected, $result);
  7536. $this->Form->setValueSources(['data']);
  7537. $this->Form->create($article);
  7538. $result = $this->Form->control('id');
  7539. $expected = [
  7540. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7541. ];
  7542. $this->assertHtml($expected, $result);
  7543. $this->Form->setValueSources(['query']);
  7544. $result = $this->Form->control('id');
  7545. $expected = [
  7546. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7547. ];
  7548. $this->assertHtml($expected, $result);
  7549. }
  7550. /**
  7551. * Tests the different input rendering values based on sources values switching while supplying
  7552. * an entity (base context) and multiple sources (such as data, query)
  7553. */
  7554. public function testFormValueSourcesListSwitchRendering(): void
  7555. {
  7556. $articles = $this->getTableLocator()->get('Articles');
  7557. $article = new Article();
  7558. $articles->patchEntity($article, ['id' => '3']);
  7559. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7560. $this->Form->create($article);
  7561. $this->Form->setValueSources(['context', 'query']);
  7562. $result = $this->Form->control('id');
  7563. $expected = [
  7564. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7565. ];
  7566. $this->assertHtml($expected, $result);
  7567. $this->Form->setValueSources(['query', 'context']);
  7568. $result = $this->Form->control('id');
  7569. $expected = [
  7570. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7571. ];
  7572. $this->assertHtml($expected, $result);
  7573. $this->Form->setValueSources(['data', 'query', 'context']);
  7574. $result = $this->Form->control('id');
  7575. $expected = [
  7576. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7577. ];
  7578. $this->assertHtml($expected, $result);
  7579. $this->View->setRequest($this->View->getRequest()
  7580. ->withData('id', '8')
  7581. ->withQueryParams(['id' => '9']));
  7582. $this->Form->setValueSources(['data', 'query', 'context']);
  7583. $result = $this->Form->control('id');
  7584. $expected = [
  7585. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7586. ];
  7587. $this->assertHtml($expected, $result);
  7588. }
  7589. /**
  7590. * Test the different form input renderings based on values sources switchings through form options
  7591. */
  7592. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7593. {
  7594. $articles = $this->getTableLocator()->get('Articles');
  7595. $article = new Article();
  7596. $articles->patchEntity($article, ['id' => '3']);
  7597. $this->View->setRequest(
  7598. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7599. );
  7600. $this->Form->create($article, ['valueSources' => 'query']);
  7601. $result = $this->Form->control('id');
  7602. $expected = [
  7603. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7604. ];
  7605. $this->assertHtml($expected, $result);
  7606. $result = $this->Form->getSourceValue('id');
  7607. $this->assertSame('5', $result);
  7608. $this->Form->setValueSources(['context']);
  7609. $this->Form->create($article, ['valueSources' => 'query']);
  7610. $result = $this->Form->control('id');
  7611. $expected = [
  7612. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7613. ];
  7614. $this->assertHtml($expected, $result);
  7615. $result = $this->Form->getSourceValue('id');
  7616. $this->assertSame('5', $result);
  7617. $this->Form->setValueSources(['query']);
  7618. $this->Form->create($article, ['valueSources' => 'data']);
  7619. $result = $this->Form->control('id');
  7620. $expected = [
  7621. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7622. ];
  7623. $this->assertHtml($expected, $result);
  7624. $result = $this->Form->getSourceValue('id');
  7625. $this->assertSame('4', $result);
  7626. $this->Form->setValueSources(['query']);
  7627. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7628. $result = $this->Form->control('id');
  7629. $expected = [
  7630. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7631. ];
  7632. $this->assertHtml($expected, $result);
  7633. $result = $this->Form->getSourceValue('id');
  7634. $this->assertSame(3, $result);
  7635. }
  7636. /**
  7637. * Test the different form input renderings based on values sources switchings through form options
  7638. */
  7639. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7640. {
  7641. $articles = $this->getTableLocator()->get('Articles');
  7642. $article = new Article();
  7643. $articles->patchEntity($article, ['id' => '3']);
  7644. $this->View->setRequest(
  7645. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7646. );
  7647. $this->Form->setValueSources(['context'])
  7648. ->create($article, ['valueSources' => ['query', 'data']]);
  7649. $result = $this->Form->control('id');
  7650. $expected = [
  7651. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7652. ];
  7653. $this->assertHtml($expected, $result);
  7654. $result = $this->Form->getSourceValue('id');
  7655. $this->assertSame('11', $result);
  7656. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7657. $this->Form->setValueSources(['context'])
  7658. ->create($article, ['valueSources' => ['query', 'data']]);
  7659. $result = $this->Form->control('id');
  7660. $expected = [
  7661. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7662. ];
  7663. $this->assertHtml($expected, $result);
  7664. $result = $this->Form->getSourceValue('id');
  7665. $this->assertSame('10', $result);
  7666. }
  7667. /**
  7668. * Test the different form values sources resetting through From::end();
  7669. */
  7670. public function testFormValueSourcesResetViaEnd(): void
  7671. {
  7672. $expected = ['data', 'context'];
  7673. $result = $this->Form->getValueSources();
  7674. $this->assertEquals($expected, $result);
  7675. $expected = ['query', 'context', 'data'];
  7676. $this->Form->setValueSources(['query', 'context', 'data']);
  7677. $result = $this->Form->getValueSources();
  7678. $this->assertEquals($expected, $result);
  7679. $this->Form->create();
  7680. $result = $this->Form->getValueSources();
  7681. $this->assertEquals($expected, $result);
  7682. $this->Form->end();
  7683. $result = $this->Form->getValueSources();
  7684. $this->assertEquals(['data', 'context'], $result);
  7685. }
  7686. /**
  7687. * Test sources values defaults handling
  7688. */
  7689. public function testFormValueSourcesDefaults(): void
  7690. {
  7691. $this->View->setRequest(
  7692. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7693. );
  7694. $this->Form->create();
  7695. $result = $this->Form->password('password');
  7696. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7697. $this->assertHtml($expected, $result);
  7698. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7699. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7700. $this->assertHtml($expected, $result);
  7701. $this->Form->setValueSources('query');
  7702. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7703. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7704. $this->assertHtml($expected, $result);
  7705. $this->Form->setValueSources('data');
  7706. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7707. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7708. $this->assertHtml($expected, $result);
  7709. }
  7710. /**
  7711. * Test sources values schema defaults handling
  7712. */
  7713. public function testSourcesValueDoesntExistPassThrough(): void
  7714. {
  7715. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7716. $articles = $this->getTableLocator()->get('Articles');
  7717. $entity = $articles->newEmptyEntity();
  7718. $this->Form->create($entity);
  7719. $this->Form->setValueSources(['query', 'context']);
  7720. $result = $this->Form->getSourceValue('category');
  7721. $this->assertSame('sesame-cookies', $result);
  7722. $this->Form->setValueSources(['context', 'query']);
  7723. $result = $this->Form->getSourceValue('category');
  7724. $this->assertSame('sesame-cookies', $result);
  7725. }
  7726. /**
  7727. * testNestedLabelInput method
  7728. *
  7729. * Test the `nestedInput` parameter
  7730. */
  7731. public function testNestedLabelInput(): void
  7732. {
  7733. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7734. $expected = [
  7735. 'div' => ['class' => 'input text'],
  7736. 'label' => ['for' => 'foo'],
  7737. ['input' => [
  7738. 'type' => 'text',
  7739. 'name' => 'foo',
  7740. 'id' => 'foo',
  7741. ]],
  7742. 'Foo',
  7743. '/label',
  7744. '/div',
  7745. ];
  7746. $this->assertHtml($expected, $result);
  7747. }
  7748. /**
  7749. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7750. *
  7751. * This test makes sure `false` excludes the label from the render
  7752. */
  7753. public function testControlLabelManipulationDisableLabels(): void
  7754. {
  7755. $result = $this->Form->control('test', [
  7756. 'type' => 'radio',
  7757. 'options' => ['A', 'B'],
  7758. 'labelOptions' => false,
  7759. ]);
  7760. $expected = [
  7761. ['div' => ['class' => 'input radio']],
  7762. '<label',
  7763. 'Test',
  7764. '/label',
  7765. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7766. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7767. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7768. '/div',
  7769. ];
  7770. $this->assertHtml($expected, $result);
  7771. $result = $this->Form->control('checkbox1', [
  7772. 'label' => 'My checkboxes',
  7773. 'multiple' => 'checkbox',
  7774. 'type' => 'select',
  7775. 'options' => [
  7776. ['text' => 'First Checkbox', 'value' => 1],
  7777. ['text' => 'Second Checkbox', 'value' => 2],
  7778. ],
  7779. 'labelOptions' => false,
  7780. ]);
  7781. $expected = [
  7782. ['div' => ['class' => 'input select']],
  7783. ['label' => ['for' => 'checkbox1']],
  7784. 'My checkboxes',
  7785. '/label',
  7786. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7787. ['div' => ['class' => 'checkbox']],
  7788. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7789. '/div',
  7790. ['div' => ['class' => 'checkbox']],
  7791. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7792. '/div',
  7793. '/div',
  7794. ];
  7795. $this->assertHtml($expected, $result);
  7796. }
  7797. /**
  7798. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7799. *
  7800. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7801. * added to the class if checked.
  7802. *
  7803. * Also checks to make sure any custom attributes are rendered correctly
  7804. */
  7805. public function testControlLabelManipulationRadios(): void
  7806. {
  7807. $result = $this->Form->control('test', [
  7808. 'type' => 'radio',
  7809. 'options' => ['A', 'B'],
  7810. 'labelOptions' => ['class' => 'custom-class'],
  7811. ]);
  7812. $expected = [
  7813. ['div' => ['class' => 'input radio']],
  7814. '<label',
  7815. 'Test',
  7816. '/label',
  7817. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7818. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7819. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7820. 'A',
  7821. '/label',
  7822. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7823. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7824. 'B',
  7825. '/label',
  7826. '/div',
  7827. ];
  7828. $this->assertHtml($expected, $result);
  7829. $result = $this->Form->control('test', [
  7830. 'type' => 'radio',
  7831. 'options' => ['A', 'B'],
  7832. 'value' => 1,
  7833. 'labelOptions' => ['class' => 'custom-class'],
  7834. ]);
  7835. $expected = [
  7836. ['div' => ['class' => 'input radio']],
  7837. '<label',
  7838. 'Test',
  7839. '/label',
  7840. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7841. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7842. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7843. 'A',
  7844. '/label',
  7845. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7846. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7847. 'B',
  7848. '/label',
  7849. '/div',
  7850. ];
  7851. $this->assertHtml($expected, $result);
  7852. $result = $this->Form->control('test', [
  7853. 'type' => 'radio',
  7854. 'options' => ['A', 'B'],
  7855. 'value' => 1,
  7856. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7857. ]);
  7858. $expected = [
  7859. ['div' => ['class' => 'input radio']],
  7860. '<label',
  7861. 'Test',
  7862. '/label',
  7863. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7864. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7865. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7866. 'A',
  7867. '/label',
  7868. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7869. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7870. 'B',
  7871. '/label',
  7872. '/div',
  7873. ];
  7874. $this->assertHtml($expected, $result);
  7875. $result = $this->Form->radio('test', ['A', 'B'], [
  7876. 'label' => [
  7877. 'class' => ['custom-class', 'another-class'],
  7878. 'data-name' => 'bob',
  7879. ],
  7880. 'value' => 1,
  7881. ]);
  7882. $expected = [
  7883. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  7884. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7885. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7886. 'A',
  7887. '/label',
  7888. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7889. ['input' => [
  7890. 'type' => 'radio',
  7891. 'name' => 'test',
  7892. 'value' => '1',
  7893. 'id' => 'test-1',
  7894. 'checked' => 'checked',
  7895. ]],
  7896. 'B',
  7897. '/label',
  7898. ];
  7899. $this->assertHtml($expected, $result);
  7900. }
  7901. /**
  7902. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7903. *
  7904. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7905. * added to the class if checked.
  7906. *
  7907. * Also checks to make sure any custom attributes are rendered correctly
  7908. */
  7909. public function testControlLabelManipulationCheckboxes(): void
  7910. {
  7911. $result = $this->Form->control('checkbox1', [
  7912. 'label' => 'My checkboxes',
  7913. 'multiple' => 'checkbox',
  7914. 'type' => 'select',
  7915. 'options' => [
  7916. ['text' => 'First Checkbox', 'value' => 1],
  7917. ['text' => 'Second Checkbox', 'value' => 2],
  7918. ],
  7919. 'labelOptions' => ['class' => 'custom-class'],
  7920. 'value' => ['1'],
  7921. ]);
  7922. $expected = [
  7923. ['div' => ['class' => 'input select']],
  7924. ['label' => ['for' => 'checkbox1']],
  7925. 'My checkboxes',
  7926. '/label',
  7927. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7928. ['div' => ['class' => 'checkbox']],
  7929. ['label' => [
  7930. 'class' => 'custom-class selected',
  7931. 'for' => 'checkbox1-1',
  7932. ]],
  7933. ['input' => [
  7934. 'type' => 'checkbox',
  7935. 'name' => 'checkbox1[]',
  7936. 'value' => '1',
  7937. 'id' => 'checkbox1-1',
  7938. 'checked' => 'checked',
  7939. ]],
  7940. 'First Checkbox',
  7941. '/label',
  7942. '/div',
  7943. ['div' => ['class' => 'checkbox']],
  7944. ['label' => [
  7945. 'class' => 'custom-class',
  7946. 'for' => 'checkbox1-2',
  7947. ]],
  7948. ['input' => [
  7949. 'type' => 'checkbox',
  7950. 'name' => 'checkbox1[]',
  7951. 'value' => '2',
  7952. 'id' => 'checkbox1-2',
  7953. ]],
  7954. 'Second Checkbox',
  7955. '/label',
  7956. '/div',
  7957. '/div',
  7958. ];
  7959. $this->assertHtml($expected, $result);
  7960. $result = $this->Form->control('checkbox1', [
  7961. 'label' => 'My checkboxes',
  7962. 'multiple' => 'checkbox',
  7963. 'type' => 'select',
  7964. 'options' => [
  7965. ['text' => 'First Checkbox', 'value' => 1],
  7966. ['text' => 'Second Checkbox', 'value' => 2],
  7967. ],
  7968. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  7969. 'value' => ['1'],
  7970. ]);
  7971. $expected = [
  7972. ['div' => ['class' => 'input select']],
  7973. ['label' => ['for' => 'checkbox1']],
  7974. 'My checkboxes',
  7975. '/label',
  7976. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7977. ['div' => ['class' => 'checkbox']],
  7978. ['label' => [
  7979. 'class' => 'custom-class another-class selected',
  7980. 'data-name' => 'bob',
  7981. 'for' => 'checkbox1-1',
  7982. ]],
  7983. ['input' => [
  7984. 'type' => 'checkbox',
  7985. 'name' => 'checkbox1[]',
  7986. 'value' => '1',
  7987. 'id' => 'checkbox1-1',
  7988. 'checked' => 'checked',
  7989. ]],
  7990. 'First Checkbox',
  7991. '/label',
  7992. '/div',
  7993. ['div' => ['class' => 'checkbox']],
  7994. ['label' => [
  7995. 'class' => 'custom-class another-class',
  7996. 'data-name' => 'bob',
  7997. 'for' => 'checkbox1-2',
  7998. ]],
  7999. ['input' => [
  8000. 'type' => 'checkbox',
  8001. 'name' => 'checkbox1[]',
  8002. 'value' => '2',
  8003. 'id' => 'checkbox1-2',
  8004. ]],
  8005. 'Second Checkbox',
  8006. '/label',
  8007. '/div',
  8008. '/div',
  8009. ];
  8010. $this->assertHtml($expected, $result);
  8011. }
  8012. /**
  8013. * testControlMaxLengthArrayContext method
  8014. *
  8015. * Test control() with maxlength attribute in Array Context.
  8016. */
  8017. public function testControlMaxLengthArrayContext(): void
  8018. {
  8019. $this->article['schema'] = [
  8020. 'title' => ['length' => 10],
  8021. ];
  8022. $this->Form->create($this->article);
  8023. $result = $this->Form->control('title');
  8024. $expected = [
  8025. 'div' => ['class'],
  8026. 'label' => ['for'],
  8027. 'Title',
  8028. '/label',
  8029. 'input' => [
  8030. 'aria-required' => 'true',
  8031. 'id',
  8032. 'name' => 'title',
  8033. 'type' => 'text',
  8034. 'required' => 'required',
  8035. 'maxlength' => 10,
  8036. 'data-validity-message' => 'This field cannot be left empty',
  8037. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8038. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8039. ],
  8040. '/div',
  8041. ];
  8042. $this->assertHtml($expected, $result);
  8043. }
  8044. /**
  8045. * testControlMaxLengthEntityContext method
  8046. *
  8047. * Test control() with maxlength attribute in Entity Context.
  8048. */
  8049. public function testControlMaxLengthEntityContext(): void
  8050. {
  8051. $this->article['schema']['title']['length'] = 45;
  8052. $validator = new Validator();
  8053. $validator->maxLength('title', 10);
  8054. $article = new EntityContext(
  8055. [
  8056. 'entity' => new Entity($this->article),
  8057. 'table' => new Table([
  8058. 'alias' => 'Articles',
  8059. 'schema' => $this->article['schema'],
  8060. 'validator' => $validator,
  8061. ]),
  8062. ]
  8063. );
  8064. $this->Form->create($article);
  8065. $result = $this->Form->control('title');
  8066. $expected = [
  8067. 'div' => ['class'],
  8068. 'label' => ['for'],
  8069. 'Title',
  8070. '/label',
  8071. 'input' => [
  8072. 'aria-required' => 'true',
  8073. 'id',
  8074. 'name' => 'title',
  8075. 'type' => 'text',
  8076. 'required' => 'required',
  8077. 'maxlength' => 10,
  8078. 'data-validity-message' => 'This field cannot be left empty',
  8079. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8080. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8081. ],
  8082. '/div',
  8083. ];
  8084. $this->assertHtml($expected, $result);
  8085. $this->article['schema']['title']['length'] = 45;
  8086. $validator = new Validator();
  8087. $validator->maxLength('title', 55);
  8088. $article = new EntityContext(
  8089. [
  8090. 'entity' => new Entity($this->article),
  8091. 'table' => new Table([
  8092. 'schema' => $this->article['schema'],
  8093. 'validator' => $validator,
  8094. 'alias' => 'Articles',
  8095. ]),
  8096. ]
  8097. );
  8098. $this->Form->create($article);
  8099. $result = $this->Form->control('title');
  8100. $expected = [
  8101. 'div' => ['class'],
  8102. 'label' => ['for'],
  8103. 'Title',
  8104. '/label',
  8105. 'input' => [
  8106. 'aria-required' => 'true',
  8107. 'id',
  8108. 'name' => 'title',
  8109. 'type' => 'text',
  8110. 'required' => 'required',
  8111. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8112. 'data-validity-message' => 'This field cannot be left empty',
  8113. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8114. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8115. ],
  8116. '/div',
  8117. ];
  8118. $this->assertHtml($expected, $result);
  8119. $this->article['schema']['title']['length'] = 45;
  8120. $validator = new Validator();
  8121. $validator->maxLength('title', 55);
  8122. $article = new EntityContext(
  8123. [
  8124. 'entity' => new Entity($this->article),
  8125. 'table' => new Table([
  8126. 'schema' => $this->article['schema'],
  8127. 'validator' => $validator,
  8128. 'alias' => 'Articles',
  8129. ]),
  8130. ]
  8131. );
  8132. $this->Form->create($article);
  8133. $result = $this->Form->control('title', ['maxlength' => 10]);
  8134. $expected = [
  8135. 'div' => ['class'],
  8136. 'label' => ['for'],
  8137. 'Title',
  8138. '/label',
  8139. 'input' => [
  8140. 'aria-required' => 'true',
  8141. 'id',
  8142. 'name' => 'title',
  8143. 'type' => 'text',
  8144. 'required' => 'required',
  8145. 'maxlength' => 10, // Length set in options should take highest precedence.
  8146. 'data-validity-message' => 'This field cannot be left empty',
  8147. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8148. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8149. ],
  8150. '/div',
  8151. ];
  8152. $this->assertHtml($expected, $result);
  8153. }
  8154. /**
  8155. * testControlMinMaxLengthEntityContext method
  8156. *
  8157. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8158. */
  8159. public function testControlMinMaxLengthEntityContext(): void
  8160. {
  8161. $validator = new Validator();
  8162. $validator->maxLength('title', 10);
  8163. $article = new EntityContext(
  8164. [
  8165. 'entity' => new Entity($this->article),
  8166. 'table' => new Table([
  8167. 'alias' => 'Articles',
  8168. 'schema' => $this->article['schema'],
  8169. 'validator' => $validator,
  8170. ]),
  8171. ]
  8172. );
  8173. $this->Form->create($article);
  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. /**
  8196. * testControlMaxLengthFormContext method
  8197. *
  8198. * Test control() with maxlength attribute in Form Context.
  8199. */
  8200. public function testControlMaxLengthFormContext(): void
  8201. {
  8202. $validator = new Validator();
  8203. $validator->maxLength('title', 10);
  8204. $form = new Form();
  8205. $form->setValidator('default', $validator);
  8206. $this->Form->create($form);
  8207. $result = $this->Form->control('title');
  8208. $expected = [
  8209. 'div' => ['class'],
  8210. 'label' => ['for'],
  8211. 'Title',
  8212. '/label',
  8213. 'input' => [
  8214. 'aria-required' => 'true',
  8215. 'id',
  8216. 'name' => 'title',
  8217. 'type' => 'text',
  8218. 'required' => 'required',
  8219. 'maxlength' => 10,
  8220. 'data-validity-message' => 'This field cannot be left empty',
  8221. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8222. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8223. ],
  8224. '/div',
  8225. ];
  8226. $this->assertHtml($expected, $result);
  8227. }
  8228. }