FormHelperTest.php 297 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959
  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 Cake\Collection\Collection;
  18. use Cake\Core\Configure;
  19. use Cake\Form\Form;
  20. use Cake\Http\ServerRequest;
  21. use Cake\I18n\Date;
  22. use Cake\I18n\FrozenTime;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. use Cake\Validation\Validator;
  29. use Cake\View\Form\EntityContext;
  30. use Cake\View\Helper\FormHelper;
  31. use Cake\View\View;
  32. use Cake\View\Widget\WidgetLocator;
  33. use ReflectionProperty;
  34. use TestApp\Model\Entity\Article;
  35. use TestApp\Model\Table\ContactsTable;
  36. use TestApp\Model\Table\ValidateUsersTable;
  37. /**
  38. * FormHelperTest class
  39. *
  40. * @property \Cake\View\Helper\FormHelper $Form
  41. * @property \Cake\View\View $View
  42. */
  43. class FormHelperTest extends TestCase
  44. {
  45. /**
  46. * Fixtures to be used
  47. *
  48. * @var array
  49. */
  50. protected $fixtures = ['core.Articles', 'core.Comments'];
  51. /**
  52. * Do not load the fixtures by default
  53. *
  54. * @var bool
  55. */
  56. public $autoFixtures = false;
  57. /**
  58. * @var array
  59. */
  60. protected $article = [];
  61. /**
  62. * setUp method
  63. *
  64. * @return void
  65. */
  66. public function setUp(): void
  67. {
  68. parent::setUp();
  69. Configure::write('Config.language', 'eng');
  70. Configure::write('App.base', '');
  71. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  72. $request = new ServerRequest([
  73. 'webroot' => '',
  74. 'base' => '',
  75. 'url' => '/articles/add',
  76. 'params' => [
  77. 'controller' => 'articles',
  78. 'action' => 'add',
  79. 'plugin' => null,
  80. ],
  81. ]);
  82. $this->View = new View($request);
  83. Router::reload();
  84. Router::setRequest($request);
  85. $this->Form = new FormHelper($this->View);
  86. $this->dateRegex = [
  87. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  88. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  89. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  90. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  91. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  92. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  93. ];
  94. $this->article = [
  95. 'schema' => [
  96. 'id' => ['type' => 'integer'],
  97. 'author_id' => ['type' => 'integer', 'null' => true],
  98. 'title' => ['type' => 'string', 'null' => true],
  99. 'body' => 'text',
  100. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  101. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  102. ],
  103. 'required' => [
  104. 'author_id' => true,
  105. 'title' => true,
  106. ],
  107. ];
  108. Security::setSalt('foo!');
  109. Router::connect('/:controller', ['action' => 'index']);
  110. Router::connect('/:controller/:action/*');
  111. }
  112. /**
  113. * tearDown method
  114. *
  115. * @return void
  116. */
  117. public function tearDown(): void
  118. {
  119. parent::tearDown();
  120. unset($this->Form, $this->Controller, $this->View);
  121. $this->getTableLocator()->clear();
  122. }
  123. /**
  124. * Test construct() with the templates option.
  125. *
  126. * @return void
  127. */
  128. public function testConstructTemplatesFile()
  129. {
  130. $helper = new FormHelper($this->View, [
  131. 'templates' => 'htmlhelper_tags',
  132. ]);
  133. $result = $helper->control('name');
  134. $this->assertStringContainsString('<input', $result);
  135. }
  136. /**
  137. * Test that when specifying custom widgets the config array for that widget
  138. * is overwritten instead of merged.
  139. *
  140. * @return void
  141. */
  142. public function testConstructWithWidgets()
  143. {
  144. $config = [
  145. 'widgets' => [
  146. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  147. ],
  148. ];
  149. $helper = new FormHelper($this->View, $config);
  150. $locator = $helper->getWidgetLocator();
  151. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  152. }
  153. /**
  154. * Test that when specifying custom widgets config file and it should be
  155. * added to widgets array. WidgetLocator will load widgets in constructor.
  156. *
  157. * @return void
  158. */
  159. public function testConstructWithWidgetsConfig()
  160. {
  161. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  162. $locator = $helper->getWidgetLocator();
  163. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  164. }
  165. /**
  166. * Test setting the widget locator
  167. *
  168. * @return void
  169. */
  170. public function testSetAndGetWidgetLocator()
  171. {
  172. $helper = new FormHelper($this->View);
  173. $locator = new WidgetLocator($helper->templater(), $this->View);
  174. $helper->setWidgetLocator($locator);
  175. $this->assertSame($locator, $helper->getWidgetLocator());
  176. }
  177. /**
  178. * Test overridding grouped input types which controls generation of "for"
  179. * attribute of labels.
  180. *
  181. * @return void
  182. */
  183. public function testConstructWithGroupedInputTypes()
  184. {
  185. $helper = new FormHelper($this->View, [
  186. 'groupedInputTypes' => ['radio'],
  187. ]);
  188. $result = $helper->control('when', ['type' => 'datetime-local']);
  189. $this->assertStringContainsString('<label for="when">When</label>', $result);
  190. }
  191. /**
  192. * Test registering a new widget class and rendering it.
  193. *
  194. * @return void
  195. */
  196. public function testAddWidgetAndRenderWidget()
  197. {
  198. $data = [
  199. 'val' => 1,
  200. ];
  201. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  202. $this->Form->addWidget('test', $mock);
  203. $mock->expects($this->once())
  204. ->method('render')
  205. ->with($data)
  206. ->will($this->returnValue('HTML'));
  207. $result = $this->Form->widget('test', $data);
  208. $this->assertSame('HTML', $result);
  209. }
  210. /**
  211. * Test that secureFields() of widget is called after calling render(),
  212. * not before.
  213. *
  214. * @return void
  215. */
  216. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  217. {
  218. $data = [
  219. 'val' => 1,
  220. 'name' => 'test',
  221. ];
  222. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  223. $this->Form->addWidget('test', $mock);
  224. $mock->expects($this->at(0))
  225. ->method('render')
  226. ->with($data)
  227. ->will($this->returnValue('HTML'));
  228. $mock->expects($this->at(1))
  229. ->method('secureFields')
  230. ->with($data)
  231. ->will($this->returnValue(['test']));
  232. $result = $this->Form->widget('test', $data + ['secure' => true]);
  233. $this->assertSame('HTML', $result);
  234. }
  235. /**
  236. * Test that empty string is not added to secure fields list when
  237. * rendering input widget without name.
  238. *
  239. * @return void
  240. */
  241. public function testRenderingWidgetWithEmptyName()
  242. {
  243. $this->assertEquals([], $this->Form->fields);
  244. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  245. $this->assertSame('<select name=""></select>', $result);
  246. $this->assertEquals([], $this->Form->fields);
  247. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  248. $this->assertSame('<select name="0"></select>', $result);
  249. $this->assertEquals(['0'], $this->Form->fields);
  250. }
  251. /**
  252. * Test registering an invalid widget class.
  253. *
  254. * @return void
  255. */
  256. public function testAddWidgetInvalid()
  257. {
  258. $this->expectException(\RuntimeException::class);
  259. $mock = new \StdClass();
  260. $this->Form->addWidget('test', $mock);
  261. $this->Form->widget('test');
  262. }
  263. /**
  264. * Test adding a new context class.
  265. *
  266. * @return void
  267. */
  268. public function testAddContextProvider()
  269. {
  270. $context = 'My data';
  271. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  272. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  273. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  274. $this->assertEquals($context, $data['entity']);
  275. return $stub;
  276. });
  277. $this->Form->create($context);
  278. $result = $this->Form->context();
  279. $this->assertSame($stub, $result);
  280. }
  281. /**
  282. * Test replacing a context class.
  283. *
  284. * @return void
  285. */
  286. public function testAddContextProviderReplace()
  287. {
  288. $entity = new Article();
  289. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  290. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  291. return $stub;
  292. });
  293. $this->Form->create($entity);
  294. $result = $this->Form->context();
  295. $this->assertSame($stub, $result);
  296. }
  297. /**
  298. * Test overriding a context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProviderAdd()
  303. {
  304. $entity = new Article();
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  307. if ($data['entity'] instanceof Entity) {
  308. return $stub;
  309. }
  310. });
  311. $this->Form->create($entity);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test adding an invalid context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderInvalid()
  321. {
  322. $this->expectException(\TypeError::class);
  323. $this->expectExceptionMessage('Return value of Cake\View\Form\ContextFactory::get() must implement interface Cake\View\Form\ContextInterface, instance of stdClass returned');
  324. $context = 'My data';
  325. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  326. return new \StdClass();
  327. });
  328. $this->Form->create($context);
  329. }
  330. /**
  331. * Provides context options for create().
  332. *
  333. * @return array
  334. */
  335. public function contextSelectionProvider()
  336. {
  337. $entity = new Article();
  338. $collection = new Collection([$entity]);
  339. $emptyCollection = new Collection([]);
  340. $arrayObject = new \ArrayObject([]);
  341. $data = [
  342. 'schema' => [
  343. 'title' => ['type' => 'string'],
  344. ],
  345. ];
  346. $form = new Form();
  347. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  348. return [
  349. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  350. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  351. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  352. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  353. 'form' => [$form, 'Cake\View\Form\FormContext'],
  354. 'none' => [null, 'Cake\View\Form\NullContext'],
  355. 'custom' => [$custom, get_class($custom)],
  356. ];
  357. }
  358. /**
  359. * Test default context selection in create()
  360. *
  361. * @dataProvider contextSelectionProvider
  362. * @return void
  363. */
  364. public function testCreateContextSelectionBuiltIn($data, $class)
  365. {
  366. $this->loadFixtures('Articles');
  367. $this->Form->create($data);
  368. $this->assertInstanceOf($class, $this->Form->context());
  369. }
  370. /**
  371. * Data provider for type option.
  372. *
  373. * @return array
  374. */
  375. public static function requestTypeProvider()
  376. {
  377. return [
  378. // type, method, override
  379. ['post', 'post', 'POST'],
  380. ['put', 'post', 'PUT'],
  381. ['patch', 'post', 'PATCH'],
  382. ['delete', 'post', 'DELETE'],
  383. ];
  384. }
  385. /**
  386. * Test creating file forms.
  387. *
  388. * @return void
  389. */
  390. public function testCreateFile()
  391. {
  392. $encoding = strtolower(Configure::read('App.encoding'));
  393. $result = $this->Form->create(null, ['type' => 'file']);
  394. $expected = [
  395. 'form' => [
  396. 'method' => 'post', 'action' => '/articles/add',
  397. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  398. ],
  399. 'div' => ['style' => 'display:none;'],
  400. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  401. '/div',
  402. ];
  403. $this->assertHtml($expected, $result);
  404. }
  405. /**
  406. * Test creating GET forms.
  407. *
  408. * @return void
  409. */
  410. public function testCreateGet()
  411. {
  412. $encoding = strtolower(Configure::read('App.encoding'));
  413. $result = $this->Form->create(null, ['type' => 'get']);
  414. $expected = ['form' => [
  415. 'method' => 'get', 'action' => '/articles/add',
  416. 'accept-charset' => $encoding,
  417. ]];
  418. $this->assertHtml($expected, $result);
  419. }
  420. /**
  421. * Test explicit method/enctype options.
  422. *
  423. * Explicit method overwrites inferred method from 'type'
  424. *
  425. * @return void
  426. */
  427. public function testCreateExplicitMethodEnctype()
  428. {
  429. $encoding = strtolower(Configure::read('App.encoding'));
  430. $result = $this->Form->create(null, [
  431. 'type' => 'get',
  432. 'method' => 'put',
  433. 'enctype' => 'multipart/form-data',
  434. ]);
  435. $expected = ['form' => [
  436. 'method' => 'put',
  437. 'action' => '/articles/add',
  438. 'enctype' => 'multipart/form-data',
  439. 'accept-charset' => $encoding,
  440. ]];
  441. $this->assertHtml($expected, $result);
  442. }
  443. /**
  444. * Test create() with the templates option.
  445. *
  446. * @return void
  447. */
  448. public function testCreateTemplatesArray()
  449. {
  450. $result = $this->Form->create($this->article, [
  451. 'templates' => [
  452. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  453. ],
  454. ]);
  455. $expected = [
  456. 'form' => [
  457. 'class' => 'form-horizontal',
  458. 'method' => 'post',
  459. 'action' => '/articles/add',
  460. 'accept-charset' => 'utf-8',
  461. ],
  462. ];
  463. $this->assertHtml($expected, $result);
  464. }
  465. /**
  466. * Test create() with the templates option.
  467. *
  468. * @return void
  469. */
  470. public function testCreateTemplatesFile()
  471. {
  472. $result = $this->Form->create($this->article, [
  473. 'templates' => 'htmlhelper_tags',
  474. ]);
  475. $expected = [
  476. 'start form',
  477. 'div' => ['class' => 'hidden'],
  478. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  479. '/div',
  480. ];
  481. $this->assertHtml($expected, $result);
  482. }
  483. /**
  484. * Test that create() and end() restore templates.
  485. *
  486. * @return void
  487. */
  488. public function testCreateEndRestoreTemplates()
  489. {
  490. $this->Form->create($this->article, [
  491. 'templates' => ['input' => 'custom input element'],
  492. ]);
  493. $this->Form->end();
  494. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  495. }
  496. /**
  497. * Test using template vars in various templates used by control() method.
  498. *
  499. * @return void
  500. */
  501. public function testControlTemplateVars()
  502. {
  503. $result = $this->Form->control('text', [
  504. 'templates' => [
  505. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  506. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  507. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  508. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  509. ],
  510. 'templateVars' => [
  511. 'forinput' => 'in-input',
  512. 'forlabel' => 'in-label',
  513. 'forgroup' => 'in-group',
  514. 'forcontainer' => 'in-container',
  515. ],
  516. ]);
  517. $expected = [
  518. 'div' => ['class'],
  519. 'label' => ['for'],
  520. 'Text in-label',
  521. '/label',
  522. 'in-group',
  523. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  524. 'in-container',
  525. '/div',
  526. ];
  527. $this->assertHtml($expected, $result);
  528. }
  529. /**
  530. * Test ensuring template variables work in template files loaded
  531. * during control().
  532. *
  533. * @return void
  534. */
  535. public function testControlTemplatesFromFile()
  536. {
  537. $result = $this->Form->control('title', [
  538. 'templates' => 'test_templates',
  539. 'templateVars' => [
  540. 'forcontainer' => 'container-data',
  541. ],
  542. ]);
  543. $expected = [
  544. 'div' => ['class'],
  545. 'label' => ['for'],
  546. 'Title',
  547. '/label',
  548. 'input' => ['name', 'type' => 'text', 'id'],
  549. 'container-data',
  550. '/div',
  551. ];
  552. $this->assertHtml($expected, $result);
  553. }
  554. /**
  555. * Test using template vars in inputSubmit and submitContainer template.
  556. *
  557. * @return void
  558. */
  559. public function testSubmitTemplateVars()
  560. {
  561. $this->Form->setTemplates([
  562. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  563. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  564. ]);
  565. $result = $this->Form->submit('Submit', [
  566. 'templateVars' => [
  567. 'forinput' => 'in-input',
  568. 'forcontainer' => 'in-container',
  569. ],
  570. ]);
  571. $expected = [
  572. 'div' => ['class'],
  573. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  574. 'in-container',
  575. '/div',
  576. ];
  577. $this->assertHtml($expected, $result);
  578. }
  579. /**
  580. * test the create() method
  581. *
  582. * @dataProvider requestTypeProvider
  583. * @return void
  584. */
  585. public function testCreateTypeOptions($type, $method, $override)
  586. {
  587. $encoding = strtolower(Configure::read('App.encoding'));
  588. $result = $this->Form->create(null, ['type' => $type]);
  589. $expected = [
  590. 'form' => [
  591. 'method' => $method, 'action' => '/articles/add',
  592. 'accept-charset' => $encoding,
  593. ],
  594. 'div' => ['style' => 'display:none;'],
  595. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  596. '/div',
  597. ];
  598. $this->assertHtml($expected, $result);
  599. }
  600. /**
  601. * Test using template vars in Create (formStart template)
  602. *
  603. * @return void
  604. */
  605. public function testCreateTemplateVars()
  606. {
  607. $result = $this->Form->create($this->article, [
  608. 'templates' => [
  609. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  610. ],
  611. 'templateVars' => ['header' => 'headertext'],
  612. ]);
  613. $expected = [
  614. 'h4' => ['class'],
  615. 'headertext',
  616. '/h4',
  617. 'form' => [
  618. 'method' => 'post',
  619. 'action' => '/articles/add',
  620. 'accept-charset' => 'utf-8',
  621. ],
  622. ];
  623. $this->assertHtml($expected, $result);
  624. }
  625. /**
  626. * Test opening a form for an update operation.
  627. *
  628. * @return void
  629. */
  630. public function testCreateUpdateForm()
  631. {
  632. $encoding = strtolower(Configure::read('App.encoding'));
  633. $this->View->setRequest($this->View->getRequest()
  634. ->withRequestTarget('/articles/edit/1')
  635. ->withParam('action', 'edit'));
  636. $this->article['defaults']['id'] = 1;
  637. $result = $this->Form->create($this->article);
  638. $expected = [
  639. 'form' => [
  640. 'method' => 'post',
  641. 'action' => '/articles/edit/1',
  642. 'accept-charset' => $encoding,
  643. ],
  644. 'div' => ['style' => 'display:none;'],
  645. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  646. '/div',
  647. ];
  648. $this->assertHtml($expected, $result);
  649. }
  650. /**
  651. * test create() with automatic url generation
  652. *
  653. * @return void
  654. */
  655. public function testCreateAutoUrl()
  656. {
  657. $encoding = strtolower(Configure::read('App.encoding'));
  658. $this->View->setRequest($this->View->getRequest()
  659. ->withRequestTarget('/articles/delete/10')
  660. ->withParam('action', 'delete'));
  661. $result = $this->Form->create($this->article);
  662. $expected = [
  663. 'form' => [
  664. 'method' => 'post', 'action' => '/articles/delete/10',
  665. 'accept-charset' => $encoding,
  666. ],
  667. 'div' => ['style' => 'display:none;'],
  668. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  669. '/div',
  670. ];
  671. $this->assertHtml($expected, $result);
  672. $this->article['defaults'] = ['id' => 1];
  673. $this->View->setRequest($this->View->getRequest()
  674. ->withRequestTarget('/articles/edit/1')
  675. ->withParam('action', 'delete'));
  676. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  677. $expected = [
  678. 'form' => [
  679. 'method' => 'post',
  680. 'action' => '/articles/edit/1',
  681. 'accept-charset' => $encoding,
  682. ],
  683. 'div' => ['style' => 'display:none;'],
  684. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  685. '/div',
  686. ];
  687. $this->assertHtml($expected, $result);
  688. $this->View->setRequest($this->View->getRequest()
  689. ->withParam('action', 'add'));
  690. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  691. $expected = [
  692. 'form' => [
  693. 'method' => 'post',
  694. 'action' => '/articles/publish/1',
  695. 'accept-charset' => $encoding,
  696. ],
  697. 'div' => ['style' => 'display:none;'],
  698. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  699. '/div',
  700. ];
  701. $this->assertHtml($expected, $result);
  702. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  703. $expected = [
  704. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  705. 'div' => ['style' => 'display:none;'],
  706. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  707. '/div',
  708. ];
  709. $this->assertHtml($expected, $result);
  710. $this->View->setRequest($this->View->getRequest()
  711. ->withParam('controller', 'Pages'));
  712. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  713. $expected = [
  714. 'form' => [
  715. 'method' => 'post', 'action' => '/Pages/signup/1',
  716. 'accept-charset' => $encoding,
  717. ],
  718. 'div' => ['style' => 'display:none;'],
  719. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  720. '/div',
  721. ];
  722. $this->assertHtml($expected, $result);
  723. }
  724. /**
  725. * Test create() with no URL (no "action" attribute for <form> tag)
  726. *
  727. * @return void
  728. */
  729. public function testCreateNoUrl()
  730. {
  731. $result = $this->Form->create(null, ['url' => false]);
  732. $expected = [
  733. 'form' => [
  734. 'method' => 'post',
  735. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  736. ],
  737. 'div' => ['style' => 'display:none;'],
  738. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  739. '/div',
  740. ];
  741. $this->assertHtml($expected, $result);
  742. }
  743. /**
  744. * test create() with a custom route
  745. *
  746. * @return void
  747. */
  748. public function testCreateCustomRoute()
  749. {
  750. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  751. $encoding = strtolower(Configure::read('App.encoding'));
  752. $this->View->setRequest($this->View->getRequest()
  753. ->withParam('controller', 'users'));
  754. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  755. $expected = [
  756. 'form' => [
  757. 'method' => 'post', 'action' => '/login',
  758. 'accept-charset' => $encoding,
  759. ],
  760. 'div' => ['style' => 'display:none;'],
  761. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  762. '/div',
  763. ];
  764. $this->assertHtml($expected, $result);
  765. Router::connect(
  766. '/new-article',
  767. ['controller' => 'articles', 'action' => 'myaction'],
  768. ['_name' => 'my-route']
  769. );
  770. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  771. $expected = [
  772. 'form' => [
  773. 'method' => 'post', 'action' => '/new-article',
  774. 'accept-charset' => $encoding,
  775. ],
  776. 'div' => ['style' => 'display:none;'],
  777. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  778. '/div',
  779. ];
  780. $this->assertHtml($expected, $result);
  781. }
  782. /**
  783. * test automatic accept-charset overriding
  784. *
  785. * @return void
  786. */
  787. public function testCreateWithAcceptCharset()
  788. {
  789. $result = $this->Form->create(
  790. $this->article,
  791. [
  792. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  793. ]
  794. );
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post', 'action' => '/articles',
  798. 'accept-charset' => 'iso-8859-1',
  799. ],
  800. 'div' => ['style' => 'display:none;'],
  801. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  802. '/div',
  803. ];
  804. $this->assertHtml($expected, $result);
  805. }
  806. /**
  807. * Test base form URL when url param is passed with multiple parameters (&)
  808. *
  809. */
  810. public function testCreateQueryStringRequest()
  811. {
  812. $encoding = strtolower(Configure::read('App.encoding'));
  813. $result = $this->Form->create($this->article, [
  814. 'type' => 'post',
  815. 'escape' => false,
  816. 'url' => [
  817. 'controller' => 'controller',
  818. 'action' => 'action',
  819. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  820. ],
  821. ]);
  822. $expected = [
  823. 'form' => [
  824. 'method' => 'post',
  825. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  826. 'accept-charset' => $encoding,
  827. ],
  828. 'div' => ['style' => 'display:none;'],
  829. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  830. '/div',
  831. ];
  832. $this->assertHtml($expected, $result);
  833. $result = $this->Form->create($this->article, [
  834. 'type' => 'post',
  835. 'url' => [
  836. 'controller' => 'controller',
  837. 'action' => 'action',
  838. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  839. ],
  840. ]);
  841. $this->assertHtml($expected, $result);
  842. }
  843. /**
  844. * test that create() doesn't cause errors by multiple id's being in the primary key
  845. * as could happen with multiple select or checkboxes.
  846. *
  847. * @return void
  848. */
  849. public function testCreateWithMultipleIdInData()
  850. {
  851. $encoding = strtolower(Configure::read('App.encoding'));
  852. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  853. $result = $this->Form->create($this->article);
  854. $expected = [
  855. 'form' => [
  856. 'method' => 'post',
  857. 'action' => '/articles/add',
  858. 'accept-charset' => $encoding,
  859. ],
  860. 'div' => ['style' => 'display:none;'],
  861. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  862. '/div',
  863. ];
  864. $this->assertHtml($expected, $result);
  865. }
  866. /**
  867. * test that create() doesn't add in extra passed params.
  868. *
  869. * @return void
  870. */
  871. public function testCreatePassedArgs()
  872. {
  873. $encoding = strtolower(Configure::read('App.encoding'));
  874. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  875. $result = $this->Form->create($this->article, [
  876. 'type' => 'post',
  877. 'escape' => false,
  878. 'url' => [
  879. 'action' => 'edit',
  880. 'myparam',
  881. ],
  882. ]);
  883. $expected = [
  884. 'form' => [
  885. 'method' => 'post',
  886. 'action' => '/articles/edit/myparam',
  887. 'accept-charset' => $encoding,
  888. ],
  889. 'div' => ['style' => 'display:none;'],
  890. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  891. '/div',
  892. ];
  893. $this->assertHtml($expected, $result);
  894. }
  895. /**
  896. * test creating a get form, and get form inputs.
  897. *
  898. * @return void
  899. */
  900. public function testGetFormCreate()
  901. {
  902. $encoding = strtolower(Configure::read('App.encoding'));
  903. $result = $this->Form->create($this->article, ['type' => 'get']);
  904. $expected = ['form' => [
  905. 'method' => 'get', 'action' => '/articles/add',
  906. 'accept-charset' => $encoding,
  907. ]];
  908. $this->assertHtml($expected, $result);
  909. $result = $this->Form->text('title');
  910. $expected = ['input' => [
  911. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  912. ]];
  913. $this->assertHtml($expected, $result);
  914. $result = $this->Form->password('password');
  915. $expected = ['input' => [
  916. 'name' => 'password', 'type' => 'password',
  917. ]];
  918. $this->assertHtml($expected, $result);
  919. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  920. $result = $this->Form->text('user_form');
  921. $expected = ['input' => [
  922. 'name' => 'user_form', 'type' => 'text',
  923. ]];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test get form, and inputs when the model param is false
  928. *
  929. * @return void
  930. */
  931. public function testGetFormWithFalseModel()
  932. {
  933. $encoding = strtolower(Configure::read('App.encoding'));
  934. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  935. $result = $this->Form->create(null, [
  936. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  937. ]);
  938. $expected = ['form' => [
  939. 'method' => 'get', 'action' => '/contact_test/add',
  940. 'accept-charset' => $encoding,
  941. ]];
  942. $this->assertHtml($expected, $result);
  943. $result = $this->Form->text('reason');
  944. $expected = [
  945. 'input' => ['type' => 'text', 'name' => 'reason'],
  946. ];
  947. $this->assertHtml($expected, $result);
  948. }
  949. /**
  950. * testFormCreateWithSecurity method
  951. *
  952. * Test form->create() with security key.
  953. *
  954. * @return void
  955. */
  956. public function testCreateWithSecurity()
  957. {
  958. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  959. $encoding = strtolower(Configure::read('App.encoding'));
  960. $result = $this->Form->create($this->article, [
  961. 'url' => '/articles/publish',
  962. ]);
  963. $expected = [
  964. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  965. 'div' => ['style' => 'display:none;'],
  966. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  967. ['input' => [
  968. 'type' => 'hidden',
  969. 'name' => '_csrfToken',
  970. 'value' => 'testKey',
  971. 'autocomplete' => 'off',
  972. ]],
  973. '/div',
  974. ];
  975. $this->assertHtml($expected, $result);
  976. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  977. $expected['form']['id'] = 'MyForm';
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormCreateGetNoSecurity method
  982. *
  983. * Test form->create() with no security key as its a get form
  984. *
  985. * @return void
  986. */
  987. public function testCreateEndGetNoSecurity()
  988. {
  989. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  990. $article = new Article();
  991. $result = $this->Form->create($article, [
  992. 'type' => 'get',
  993. 'url' => '/contacts/add',
  994. ]);
  995. $this->assertStringNotContainsString('testKey', $result);
  996. $result = $this->Form->end();
  997. $this->assertStringNotContainsString('testKey', $result);
  998. }
  999. /**
  1000. * test that create() clears the fields property so it starts fresh
  1001. *
  1002. * @return void
  1003. */
  1004. public function testCreateClearingFields()
  1005. {
  1006. $this->Form->fields = ['model_id'];
  1007. $this->Form->create($this->article);
  1008. $this->assertEquals([], $this->Form->fields);
  1009. }
  1010. /**
  1011. * Tests form hash generation with model-less data
  1012. *
  1013. * @return void
  1014. */
  1015. public function testValidateHashNoModel()
  1016. {
  1017. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'foo'));
  1018. $fields = ['anything'];
  1019. $result = $this->Form->secure($fields);
  1020. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1021. $this->assertStringContainsString($hash, $result);
  1022. }
  1023. /**
  1024. * Tests that hidden fields generated for checkboxes don't get locked
  1025. *
  1026. * @return void
  1027. */
  1028. public function testNoCheckboxLocking()
  1029. {
  1030. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'foo'));
  1031. $this->assertSame([], $this->Form->fields);
  1032. $this->Form->checkbox('check', ['value' => '1']);
  1033. $this->assertSame($this->Form->fields, ['check']);
  1034. }
  1035. /**
  1036. * testFormSecurityFields method
  1037. *
  1038. * Test generation of secure form hash generation.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testFormSecurityFields()
  1043. {
  1044. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1045. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1046. $result = $this->Form->secure($fields);
  1047. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1048. $hash .= ':' . 'Model.valid';
  1049. $hash = urlencode($hash);
  1050. $tokenDebug = urlencode(json_encode([
  1051. '',
  1052. $fields,
  1053. [],
  1054. ]));
  1055. $expected = [
  1056. 'div' => ['style' => 'display:none;'],
  1057. ['input' => [
  1058. 'type' => 'hidden',
  1059. 'name' => '_Token[fields]',
  1060. 'value' => $hash,
  1061. 'autocomplete' => 'off',
  1062. ]],
  1063. ['input' => [
  1064. 'type' => 'hidden',
  1065. 'name' => '_Token[unlocked]',
  1066. 'value' => '',
  1067. 'autocomplete' => 'off',
  1068. ]],
  1069. ['input' => [
  1070. 'type' => 'hidden',
  1071. 'name' => '_Token[debug]',
  1072. 'value' => $tokenDebug,
  1073. 'autocomplete' => 'off',
  1074. ]],
  1075. '/div',
  1076. ];
  1077. $this->assertHtml($expected, $result);
  1078. }
  1079. /**
  1080. * testFormSecurityFields method
  1081. *
  1082. * Test debug token is not generated if debug is false
  1083. *
  1084. * @return void
  1085. */
  1086. public function testFormSecurityFieldsNoDebugMode()
  1087. {
  1088. Configure::write('debug', false);
  1089. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1090. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1091. $result = $this->Form->secure($fields);
  1092. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1093. $hash .= ':' . 'Model.valid';
  1094. $hash = urlencode($hash);
  1095. $expected = [
  1096. 'div' => ['style' => 'display:none;'],
  1097. ['input' => [
  1098. 'type' => 'hidden',
  1099. 'name' => '_Token[fields]',
  1100. 'autocomplete' => 'off',
  1101. 'value' => $hash,
  1102. ]],
  1103. ['input' => [
  1104. 'type' => 'hidden',
  1105. 'name' => '_Token[unlocked]',
  1106. 'autocomplete' => 'off',
  1107. 'value' => '',
  1108. ]],
  1109. '/div',
  1110. ];
  1111. $this->assertHtml($expected, $result);
  1112. }
  1113. /**
  1114. * Tests correct generation of number fields for smallint
  1115. *
  1116. * @return void
  1117. */
  1118. public function testTextFieldGenerationForSmallint()
  1119. {
  1120. $this->article['schema'] = [
  1121. 'foo' => [
  1122. 'type' => 'smallinteger',
  1123. 'null' => false,
  1124. 'default' => null,
  1125. 'length' => 10,
  1126. ],
  1127. ];
  1128. $this->Form->create($this->article);
  1129. $result = $this->Form->control('foo');
  1130. $this->assertStringContainsString('class="input number"', $result);
  1131. $this->assertStringContainsString('type="number"', $result);
  1132. }
  1133. /**
  1134. * Tests correct generation of number fields for tinyint
  1135. *
  1136. * @return void
  1137. */
  1138. public function testTextFieldGenerationForTinyint()
  1139. {
  1140. $this->article['schema'] = [
  1141. 'foo' => [
  1142. 'type' => 'tinyinteger',
  1143. 'null' => false,
  1144. 'default' => null,
  1145. 'length' => 10,
  1146. ],
  1147. ];
  1148. $this->Form->create($this->article);
  1149. $result = $this->Form->control('foo');
  1150. $this->assertStringContainsString('class="input number"', $result);
  1151. $this->assertStringContainsString('type="number"', $result);
  1152. }
  1153. /**
  1154. * Tests correct generation of number fields for double and float fields
  1155. *
  1156. * @return void
  1157. */
  1158. public function testTextFieldGenerationForFloats()
  1159. {
  1160. $this->article['schema'] = [
  1161. 'foo' => [
  1162. 'type' => 'float',
  1163. 'null' => false,
  1164. 'default' => null,
  1165. 'length' => 10,
  1166. ],
  1167. ];
  1168. $this->Form->create($this->article);
  1169. $result = $this->Form->control('foo');
  1170. $expected = [
  1171. 'div' => ['class' => 'input number'],
  1172. 'label' => ['for' => 'foo'],
  1173. 'Foo',
  1174. '/label',
  1175. ['input' => [
  1176. 'type' => 'number',
  1177. 'name' => 'foo',
  1178. 'id' => 'foo',
  1179. 'step' => 'any',
  1180. ]],
  1181. '/div',
  1182. ];
  1183. $this->assertHtml($expected, $result);
  1184. $result = $this->Form->control('foo', ['step' => 0.5]);
  1185. $expected = [
  1186. 'div' => ['class' => 'input number'],
  1187. 'label' => ['for' => 'foo'],
  1188. 'Foo',
  1189. '/label',
  1190. ['input' => [
  1191. 'type' => 'number',
  1192. 'name' => 'foo',
  1193. 'id' => 'foo',
  1194. 'step' => '0.5',
  1195. ]],
  1196. '/div',
  1197. ];
  1198. $this->assertHtml($expected, $result);
  1199. }
  1200. /**
  1201. * Tests correct generation of number fields for integer fields
  1202. *
  1203. * @return void
  1204. */
  1205. public function testTextFieldTypeNumberGenerationForIntegers()
  1206. {
  1207. $this->getTableLocator()->get('Contacts', [
  1208. 'className' => ContactsTable::class,
  1209. ]);
  1210. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1211. $result = $this->Form->control('age');
  1212. $expected = [
  1213. 'div' => ['class' => 'input number'],
  1214. 'label' => ['for' => 'age'],
  1215. 'Age',
  1216. '/label',
  1217. ['input' => [
  1218. 'type' => 'number', 'name' => 'age',
  1219. 'id' => 'age',
  1220. ]],
  1221. '/div',
  1222. ];
  1223. $this->assertHtml($expected, $result);
  1224. }
  1225. /**
  1226. * Tests correct generation of file upload fields for binary fields
  1227. *
  1228. * @return void
  1229. */
  1230. public function testFileUploadFieldTypeGenerationForBinaries()
  1231. {
  1232. $table = $this->getTableLocator()->get('Contacts', [
  1233. 'className' => ContactsTable::class,
  1234. ]);
  1235. $table->setSchema(['foo' => [
  1236. 'type' => 'binary',
  1237. 'null' => false,
  1238. 'default' => null,
  1239. 'length' => 1024,
  1240. ]]);
  1241. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1242. $result = $this->Form->control('foo');
  1243. $expected = [
  1244. 'div' => ['class' => 'input file'],
  1245. 'label' => ['for' => 'foo'],
  1246. 'Foo',
  1247. '/label',
  1248. ['input' => [
  1249. 'type' => 'file', 'name' => 'foo',
  1250. 'id' => 'foo',
  1251. ]],
  1252. '/div',
  1253. ];
  1254. $this->assertHtml($expected, $result);
  1255. }
  1256. /**
  1257. * testFormSecurityMultipleFields method
  1258. *
  1259. * Test secure() with multiple row form. Ensure hash is correct.
  1260. *
  1261. * @return void
  1262. */
  1263. public function testFormSecurityMultipleFields()
  1264. {
  1265. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'foo'));
  1266. $fields = [
  1267. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1268. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1269. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1270. ];
  1271. $result = $this->Form->secure($fields);
  1272. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1273. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1274. $tokenDebug = urlencode(json_encode([
  1275. '',
  1276. $fields,
  1277. [],
  1278. ]));
  1279. $expected = [
  1280. 'div' => ['style' => 'display:none;'],
  1281. ['input' => [
  1282. 'type' => 'hidden',
  1283. 'name' => '_Token[fields]',
  1284. 'value' => $hash,
  1285. 'autocomplete' => 'off',
  1286. ]],
  1287. ['input' => [
  1288. 'type' => 'hidden',
  1289. 'name' => '_Token[unlocked]',
  1290. 'autocomplete' => 'off',
  1291. 'value' => '',
  1292. ]],
  1293. ['input' => [
  1294. 'type' => 'hidden',
  1295. 'name' => '_Token[debug]',
  1296. 'value' => $tokenDebug,
  1297. 'autocomplete' => 'off',
  1298. ]],
  1299. '/div',
  1300. ];
  1301. $this->assertHtml($expected, $result);
  1302. }
  1303. /**
  1304. * testFormSecurityMultipleSubmitButtons
  1305. *
  1306. * test form submit generation and ensure that _Token is only created on end()
  1307. *
  1308. * @return void
  1309. */
  1310. public function testFormSecurityMultipleSubmitButtons()
  1311. {
  1312. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1313. $this->Form->create($this->article);
  1314. $this->Form->text('Address.title');
  1315. $this->Form->text('Address.first_name');
  1316. $result = $this->Form->submit('Save', ['name' => 'save']);
  1317. $expected = [
  1318. 'div' => ['class' => 'submit'],
  1319. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1320. '/div',
  1321. ];
  1322. $this->assertHtml($expected, $result);
  1323. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1324. $expected = [
  1325. 'div' => ['class' => 'submit'],
  1326. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1327. '/div',
  1328. ];
  1329. $this->assertHtml($expected, $result);
  1330. $result = $this->Form->end();
  1331. $tokenDebug = urlencode(json_encode([
  1332. '/articles/add',
  1333. [
  1334. 'Address.title',
  1335. 'Address.first_name',
  1336. ],
  1337. ['save', 'cancel'],
  1338. ]));
  1339. $expected = [
  1340. 'div' => ['style' => 'display:none;'],
  1341. ['input' => [
  1342. 'type' => 'hidden',
  1343. 'name' => '_Token[fields]',
  1344. 'autocomplete',
  1345. 'value',
  1346. ]],
  1347. ['input' => [
  1348. 'type' => 'hidden',
  1349. 'name' => '_Token[unlocked]',
  1350. 'value' => 'cancel%7Csave',
  1351. 'autocomplete' => 'off',
  1352. ]],
  1353. ['input' => [
  1354. 'type' => 'hidden',
  1355. 'name' => '_Token[debug]',
  1356. 'value' => $tokenDebug,
  1357. 'autocomplete' => 'off',
  1358. ]],
  1359. '/div',
  1360. ];
  1361. $this->assertHtml($expected, $result);
  1362. }
  1363. /**
  1364. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1365. *
  1366. * @return void
  1367. */
  1368. public function testSecurityButtonNestedNamed()
  1369. {
  1370. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  1371. $this->Form->create();
  1372. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1373. $result = $this->Form->unlockField();
  1374. $this->assertEquals(['Address.button'], $result);
  1375. }
  1376. /**
  1377. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1378. *
  1379. * @return void
  1380. */
  1381. public function testSecuritySubmitNestedNamed()
  1382. {
  1383. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1384. $this->Form->create($this->article);
  1385. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1386. $result = $this->Form->unlockField();
  1387. $this->assertEquals(['Address.button'], $result);
  1388. }
  1389. /**
  1390. * Test that the correct fields are unlocked for image submits with no names.
  1391. *
  1392. * @return void
  1393. */
  1394. public function testSecuritySubmitImageNoName()
  1395. {
  1396. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1397. $this->Form->create();
  1398. $result = $this->Form->submit('save.png');
  1399. $expected = [
  1400. 'div' => ['class' => 'submit'],
  1401. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1402. '/div',
  1403. ];
  1404. $this->assertHtml($expected, $result);
  1405. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1406. }
  1407. /**
  1408. * Test that the correct fields are unlocked for image submits with names.
  1409. *
  1410. * @return void
  1411. */
  1412. public function testSecuritySubmitImageName()
  1413. {
  1414. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1415. $this->Form->create(null);
  1416. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1417. $expected = [
  1418. 'div' => ['class' => 'submit'],
  1419. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1420. '/div',
  1421. ];
  1422. $this->assertHtml($expected, $result);
  1423. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1424. }
  1425. /**
  1426. * testFormSecurityMultipleControlFields method
  1427. *
  1428. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1429. *
  1430. * @return void
  1431. */
  1432. public function testFormSecurityMultipleControlFields()
  1433. {
  1434. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1435. $this->Form->create();
  1436. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1437. $this->Form->control('Addresses.0.title');
  1438. $this->Form->control('Addresses.0.first_name');
  1439. $this->Form->control('Addresses.0.last_name');
  1440. $this->Form->control('Addresses.0.address');
  1441. $this->Form->control('Addresses.0.city');
  1442. $this->Form->control('Addresses.0.phone');
  1443. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1444. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1445. $this->Form->control('Addresses.1.title');
  1446. $this->Form->control('Addresses.1.first_name');
  1447. $this->Form->control('Addresses.1.last_name');
  1448. $this->Form->control('Addresses.1.address');
  1449. $this->Form->control('Addresses.1.city');
  1450. $this->Form->control('Addresses.1.phone');
  1451. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1452. $result = $this->Form->secure($this->Form->fields);
  1453. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1454. $tokenDebug = urlencode(json_encode([
  1455. '/articles/add',
  1456. [
  1457. 'Addresses.0.id' => '123456',
  1458. 'Addresses.0.title',
  1459. 'Addresses.0.first_name',
  1460. 'Addresses.0.last_name',
  1461. 'Addresses.0.address',
  1462. 'Addresses.0.city',
  1463. 'Addresses.0.phone',
  1464. 'Addresses.0.primary',
  1465. 'Addresses.1.id' => '654321',
  1466. 'Addresses.1.title',
  1467. 'Addresses.1.first_name',
  1468. 'Addresses.1.last_name',
  1469. 'Addresses.1.address',
  1470. 'Addresses.1.city',
  1471. 'Addresses.1.phone',
  1472. 'Addresses.1.primary',
  1473. ],
  1474. [],
  1475. ]));
  1476. $expected = [
  1477. 'div' => ['style' => 'display:none;'],
  1478. ['input' => [
  1479. 'type' => 'hidden',
  1480. 'name' => '_Token[fields]',
  1481. 'value' => $hash,
  1482. 'autocomplete' => 'off',
  1483. ]],
  1484. ['input' => [
  1485. 'type' => 'hidden',
  1486. 'name' => '_Token[unlocked]',
  1487. 'autocomplete' => 'off',
  1488. 'value' => '',
  1489. ]],
  1490. ['input' => [
  1491. 'type' => 'hidden',
  1492. 'name' => '_Token[debug]',
  1493. 'value' => $tokenDebug,
  1494. 'autocomplete' => 'off',
  1495. ]],
  1496. '/div',
  1497. ];
  1498. $this->assertHtml($expected, $result);
  1499. }
  1500. /**
  1501. * testFormSecurityArrayFields method
  1502. *
  1503. * Test form security with Model.field.0 style inputs.
  1504. *
  1505. * @return void
  1506. */
  1507. public function testFormSecurityArrayFields()
  1508. {
  1509. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1510. $this->Form->create();
  1511. $this->Form->text('Address.primary.1');
  1512. $this->assertSame('Address.primary', $this->Form->fields[0]);
  1513. $this->Form->text('Address.secondary.1.0');
  1514. $this->assertSame('Address.secondary', $this->Form->fields[1]);
  1515. }
  1516. /**
  1517. * testFormSecurityMultipleControlDisabledFields method
  1518. *
  1519. * Test secure form generation with multiple records and disabled fields.
  1520. *
  1521. * @return void
  1522. */
  1523. public function testFormSecurityMultipleControlDisabledFields()
  1524. {
  1525. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  1526. 'unlockedFields' => ['first_name', 'address'],
  1527. ]));
  1528. $this->Form->create();
  1529. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1530. $this->Form->text('Addresses.0.title');
  1531. $this->Form->text('Addresses.0.first_name');
  1532. $this->Form->text('Addresses.0.last_name');
  1533. $this->Form->text('Addresses.0.address');
  1534. $this->Form->text('Addresses.0.city');
  1535. $this->Form->text('Addresses.0.phone');
  1536. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1537. $this->Form->text('Addresses.1.title');
  1538. $this->Form->text('Addresses.1.first_name');
  1539. $this->Form->text('Addresses.1.last_name');
  1540. $this->Form->text('Addresses.1.address');
  1541. $this->Form->text('Addresses.1.city');
  1542. $this->Form->text('Addresses.1.phone');
  1543. $result = $this->Form->secure($this->Form->fields);
  1544. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1545. $tokenDebug = urlencode(json_encode([
  1546. '/articles/add',
  1547. [
  1548. 'Addresses.0.id' => '123456',
  1549. 'Addresses.0.title',
  1550. 'Addresses.0.last_name',
  1551. 'Addresses.0.city',
  1552. 'Addresses.0.phone',
  1553. 'Addresses.1.id' => '654321',
  1554. 'Addresses.1.title',
  1555. 'Addresses.1.last_name',
  1556. 'Addresses.1.city',
  1557. 'Addresses.1.phone',
  1558. ],
  1559. [
  1560. 'first_name',
  1561. 'address',
  1562. ],
  1563. ]));
  1564. $expected = [
  1565. 'div' => ['style' => 'display:none;'],
  1566. ['input' => [
  1567. 'type' => 'hidden',
  1568. 'name' => '_Token[fields]',
  1569. 'autocomplete' => 'off',
  1570. 'value' => $hash,
  1571. ]],
  1572. ['input' => [
  1573. 'type' => 'hidden',
  1574. 'name' => '_Token[unlocked]',
  1575. 'autocomplete' => 'off',
  1576. 'value' => 'address%7Cfirst_name',
  1577. ]],
  1578. ['input' => [
  1579. 'type' => 'hidden',
  1580. 'name' => '_Token[debug]',
  1581. 'autocomplete' => 'off',
  1582. 'value' => $tokenDebug,
  1583. ]],
  1584. '/div',
  1585. ];
  1586. $this->assertHtml($expected, $result);
  1587. }
  1588. /**
  1589. * testFormSecurityControlDisabledFields method
  1590. *
  1591. * Test single record form with disabled fields.
  1592. *
  1593. * @return void
  1594. */
  1595. public function testFormSecurityControlUnlockedFields()
  1596. {
  1597. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  1598. 'unlockedFields' => ['first_name', 'address'],
  1599. ]));
  1600. $this->Form->create();
  1601. $this->assertEquals(
  1602. $this->View->getRequest()->getAttribute('_formToken'),
  1603. ['unlockedFields' => $this->Form->unlockField()]
  1604. );
  1605. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1606. $this->Form->text('Addresses.title');
  1607. $this->Form->text('Addresses.first_name');
  1608. $this->Form->text('Addresses.last_name');
  1609. $this->Form->text('Addresses.address');
  1610. $this->Form->text('Addresses.city');
  1611. $this->Form->text('Addresses.phone');
  1612. $result = $this->Form->fields;
  1613. $expected = [
  1614. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1615. 'Addresses.city', 'Addresses.phone',
  1616. ];
  1617. $this->assertEquals($expected, $result);
  1618. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1619. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1620. $tokenDebug = urlencode(json_encode([
  1621. '/articles/add',
  1622. [
  1623. 'Addresses.id' => '123456',
  1624. 'Addresses.title',
  1625. 'Addresses.last_name',
  1626. 'Addresses.city',
  1627. 'Addresses.phone',
  1628. ],
  1629. [
  1630. 'first_name',
  1631. 'address',
  1632. ],
  1633. ]));
  1634. $expected = [
  1635. 'div' => ['style' => 'display:none;'],
  1636. ['input' => [
  1637. 'type' => 'hidden',
  1638. 'name' => '_Token[fields]',
  1639. 'value' => $hash,
  1640. 'autocomplete' => 'off',
  1641. 'data-foo' => 'bar',
  1642. ]],
  1643. ['input' => [
  1644. 'type' => 'hidden',
  1645. 'name' => '_Token[unlocked]',
  1646. 'value' => 'address%7Cfirst_name',
  1647. 'autocomplete' => 'off',
  1648. 'data-foo' => 'bar',
  1649. ]],
  1650. ['input' => [
  1651. 'type' => 'hidden', 'name' => '_Token[debug]',
  1652. 'value' => $tokenDebug,
  1653. 'autocomplete' => 'off',
  1654. 'data-foo' => 'bar',
  1655. ]],
  1656. '/div',
  1657. ];
  1658. $this->assertHtml($expected, $result);
  1659. }
  1660. /**
  1661. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1662. *
  1663. * Test single record form with debugSecurity param.
  1664. *
  1665. * @return void
  1666. */
  1667. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1668. {
  1669. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  1670. 'unlockedFields' => ['first_name', 'address'],
  1671. ]));
  1672. $this->Form->create();
  1673. $this->assertEquals(
  1674. $this->View->getRequest()->getAttribute('_formToken'),
  1675. ['unlockedFields' => $this->Form->unlockField()]
  1676. );
  1677. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1678. $this->Form->text('Addresses.title');
  1679. $this->Form->text('Addresses.first_name');
  1680. $this->Form->text('Addresses.last_name');
  1681. $this->Form->text('Addresses.address');
  1682. $this->Form->text('Addresses.city');
  1683. $this->Form->text('Addresses.phone');
  1684. $result = $this->Form->fields;
  1685. $expected = [
  1686. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1687. 'Addresses.city', 'Addresses.phone',
  1688. ];
  1689. $this->assertEquals($expected, $result);
  1690. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1691. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1692. $tokenDebug = urlencode(json_encode([
  1693. '/articles/add',
  1694. [
  1695. 'Addresses.id' => '123456',
  1696. 'Addresses.title',
  1697. 'Addresses.last_name',
  1698. 'Addresses.city',
  1699. 'Addresses.phone',
  1700. ],
  1701. [
  1702. 'first_name',
  1703. 'address',
  1704. ],
  1705. ]));
  1706. $expected = [
  1707. 'div' => ['style' => 'display:none;'],
  1708. ['input' => [
  1709. 'type' => 'hidden',
  1710. 'name' => '_Token[fields]',
  1711. 'value' => $hash,
  1712. 'autocomplete' => 'off',
  1713. 'data-foo' => 'bar',
  1714. ]],
  1715. ['input' => [
  1716. 'type' => 'hidden',
  1717. 'name' => '_Token[unlocked]',
  1718. 'value' => 'address%7Cfirst_name',
  1719. 'autocomplete' => 'off',
  1720. 'data-foo' => 'bar',
  1721. ]],
  1722. ['input' => [
  1723. 'type' => 'hidden', 'name' => '_Token[debug]',
  1724. 'value' => $tokenDebug,
  1725. 'autocomplete' => 'off',
  1726. 'data-foo' => 'bar',
  1727. ]],
  1728. '/div',
  1729. ];
  1730. $this->assertHtml($expected, $result);
  1731. }
  1732. /**
  1733. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1734. *
  1735. * Debug is false, debugSecurity is true -> no debug
  1736. *
  1737. * @return void
  1738. */
  1739. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1740. {
  1741. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  1742. 'unlockedFields' => ['first_name', 'address'],
  1743. ]));
  1744. $this->Form->create();
  1745. $this->assertEquals(
  1746. $this->View->getRequest()->getAttribute('_formToken'),
  1747. ['unlockedFields' => $this->Form->unlockField()]
  1748. );
  1749. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1750. $this->Form->text('Addresses.title');
  1751. $this->Form->text('Addresses.first_name');
  1752. $this->Form->text('Addresses.last_name');
  1753. $this->Form->text('Addresses.address');
  1754. $this->Form->text('Addresses.city');
  1755. $this->Form->text('Addresses.phone');
  1756. $result = $this->Form->fields;
  1757. $expected = [
  1758. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1759. 'Addresses.city', 'Addresses.phone',
  1760. ];
  1761. $this->assertEquals($expected, $result);
  1762. Configure::write('debug', false);
  1763. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1764. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1765. $expected = [
  1766. 'div' => ['style' => 'display:none;'],
  1767. ['input' => [
  1768. 'type' => 'hidden',
  1769. 'name' => '_Token[fields]',
  1770. 'value' => $hash,
  1771. 'autocomplete' => 'off',
  1772. 'data-foo' => 'bar',
  1773. ]],
  1774. ['input' => [
  1775. 'type' => 'hidden',
  1776. 'name' => '_Token[unlocked]',
  1777. 'value' => 'address%7Cfirst_name',
  1778. 'autocomplete' => 'off',
  1779. 'data-foo' => 'bar',
  1780. ]],
  1781. '/div',
  1782. ];
  1783. $this->assertHtml($expected, $result);
  1784. }
  1785. /**
  1786. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1787. *
  1788. * Test single record form with debugSecurity param.
  1789. *
  1790. * @return void
  1791. */
  1792. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1793. {
  1794. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  1795. 'unlockedFields' => ['first_name', 'address'],
  1796. ]));
  1797. $this->Form->create();
  1798. $this->assertEquals(
  1799. $this->View->getRequest()->getAttribute('_formToken'),
  1800. ['unlockedFields' => $this->Form->unlockField()]
  1801. );
  1802. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1803. $this->Form->text('Addresses.title');
  1804. $this->Form->text('Addresses.first_name');
  1805. $this->Form->text('Addresses.last_name');
  1806. $this->Form->text('Addresses.address');
  1807. $this->Form->text('Addresses.city');
  1808. $this->Form->text('Addresses.phone');
  1809. $result = $this->Form->fields;
  1810. $expected = [
  1811. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1812. 'Addresses.city', 'Addresses.phone',
  1813. ];
  1814. $this->assertEquals($expected, $result);
  1815. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1816. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1817. $expected = [
  1818. 'div' => ['style' => 'display:none;'],
  1819. ['input' => [
  1820. 'type' => 'hidden',
  1821. 'name' => '_Token[fields]',
  1822. 'value' => $hash,
  1823. 'autocomplete' => 'off',
  1824. 'data-foo' => 'bar',
  1825. ]],
  1826. ['input' => [
  1827. 'type' => 'hidden',
  1828. 'name' => '_Token[unlocked]',
  1829. 'value' => 'address%7Cfirst_name',
  1830. 'autocomplete' => 'off',
  1831. 'data-foo' => 'bar',
  1832. ]],
  1833. '/div',
  1834. ];
  1835. $this->assertHtml($expected, $result);
  1836. }
  1837. /**
  1838. * testFormSecureWithCustomNameAttribute method
  1839. *
  1840. * Test securing inputs with custom name attributes.
  1841. *
  1842. * @return void
  1843. */
  1844. public function testFormSecureWithCustomNameAttribute()
  1845. {
  1846. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  1847. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1848. $this->assertSame('User.custom', $this->Form->fields[0]);
  1849. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1850. $this->assertSame('User.custom.another.value', $this->Form->fields[1]);
  1851. }
  1852. /**
  1853. * testFormSecuredControl method
  1854. *
  1855. * Test generation of entire secure form, assertions made on control() output.
  1856. *
  1857. * @return void
  1858. */
  1859. public function testFormSecuredControl()
  1860. {
  1861. $this->View->setRequest($this->View->getRequest()
  1862. ->withAttribute('_formToken', 'stuff')
  1863. ->withAttribute('csrfToken', 'testKey'));
  1864. $this->article['schema'] = [
  1865. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1866. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1867. ];
  1868. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1869. $encoding = strtolower(Configure::read('App.encoding'));
  1870. $expected = [
  1871. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1872. 'div' => ['style' => 'display:none;'],
  1873. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1874. ['input' => [
  1875. 'type' => 'hidden',
  1876. 'name' => '_csrfToken',
  1877. 'value' => 'testKey',
  1878. 'autocomplete' => 'off',
  1879. ]],
  1880. '/div',
  1881. ];
  1882. $this->assertHtml($expected, $result);
  1883. $result = $this->Form->control('ratio');
  1884. $expected = [
  1885. 'div' => ['class'],
  1886. 'label' => ['for'],
  1887. 'Ratio',
  1888. '/label',
  1889. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1890. '/div',
  1891. ];
  1892. $this->assertHtml($expected, $result);
  1893. $result = $this->Form->control('population');
  1894. $expected = [
  1895. 'div' => ['class'],
  1896. 'label' => ['for'],
  1897. 'Population',
  1898. '/label',
  1899. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1900. '/div',
  1901. ];
  1902. $this->assertHtml($expected, $result);
  1903. $result = $this->Form->control('published', ['type' => 'text']);
  1904. $expected = [
  1905. 'div' => ['class' => 'input text'],
  1906. 'label' => ['for' => 'published'],
  1907. 'Published',
  1908. '/label',
  1909. ['input' => [
  1910. 'type' => 'text',
  1911. 'name' => 'published',
  1912. 'id' => 'published',
  1913. ]],
  1914. '/div',
  1915. ];
  1916. $this->assertHtml($expected, $result);
  1917. $result = $this->Form->control('other', ['type' => 'text']);
  1918. $expected = [
  1919. 'div' => ['class' => 'input text'],
  1920. 'label' => ['for' => 'other'],
  1921. 'Other',
  1922. '/label',
  1923. ['input' => [
  1924. 'type' => 'text',
  1925. 'name' => 'other',
  1926. 'id',
  1927. ]],
  1928. '/div',
  1929. ];
  1930. $this->assertHtml($expected, $result);
  1931. $result = $this->Form->hidden('stuff');
  1932. $expected = [
  1933. 'input' => [
  1934. 'type' => 'hidden',
  1935. 'name' => 'stuff',
  1936. ],
  1937. ];
  1938. $this->assertHtml($expected, $result);
  1939. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1940. $expected = ['input' => [
  1941. 'type' => 'hidden',
  1942. 'name' => 'hidden',
  1943. 'value' => '0',
  1944. ]];
  1945. $this->assertHtml($expected, $result);
  1946. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1947. $expected = [
  1948. 'div' => ['class' => 'input checkbox'],
  1949. ['input' => [
  1950. 'type' => 'hidden',
  1951. 'name' => 'something',
  1952. 'value' => '0',
  1953. ]],
  1954. 'label' => ['for' => 'something'],
  1955. ['input' => [
  1956. 'type' => 'checkbox',
  1957. 'name' => 'something',
  1958. 'value' => '1',
  1959. 'id' => 'something',
  1960. ]],
  1961. 'Something',
  1962. '/label',
  1963. '/div',
  1964. ];
  1965. $this->assertHtml($expected, $result);
  1966. $result = $this->Form->fields;
  1967. $expectedFields = [
  1968. 'ratio',
  1969. 'population',
  1970. 'published',
  1971. 'other',
  1972. 'stuff' => '',
  1973. 'hidden' => '0',
  1974. 'something',
  1975. ];
  1976. $this->assertEquals($expectedFields, $result);
  1977. $result = $this->Form->secure($this->Form->fields);
  1978. $tokenDebug = urlencode(json_encode([
  1979. '/articles/add',
  1980. $expectedFields,
  1981. [],
  1982. ]));
  1983. $expected = [
  1984. 'div' => ['style' => 'display:none;'],
  1985. ['input' => [
  1986. 'type' => 'hidden',
  1987. 'name' => '_Token[fields]',
  1988. 'value',
  1989. 'autocomplete',
  1990. ]],
  1991. ['input' => [
  1992. 'type' => 'hidden',
  1993. 'name' => '_Token[unlocked]',
  1994. 'value' => '',
  1995. 'autocomplete' => 'off',
  1996. ]],
  1997. ['input' => [
  1998. 'type' => 'hidden', 'name' => '_Token[debug]',
  1999. 'value' => $tokenDebug,
  2000. 'autocomplete' => 'off',
  2001. ]],
  2002. '/div',
  2003. ];
  2004. $this->assertHtml($expected, $result);
  2005. }
  2006. /**
  2007. * testSecuredControlCustomName method
  2008. *
  2009. * Test secured inputs with custom names.
  2010. *
  2011. * @return void
  2012. */
  2013. public function testSecuredControlCustomName()
  2014. {
  2015. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  2016. $this->assertEquals([], $this->Form->fields);
  2017. $this->Form->text('text_input', [
  2018. 'name' => 'Option[General.default_role]',
  2019. ]);
  2020. $expected = ['Option.General.default_role'];
  2021. $this->assertEquals($expected, $this->Form->fields);
  2022. $this->Form->select('select_box', [1, 2], [
  2023. 'name' => 'Option[General.select_role]',
  2024. ]);
  2025. $expected[] = 'Option.General.select_role';
  2026. $this->assertEquals($expected, $this->Form->fields);
  2027. $this->Form->text('other.things[]');
  2028. $expected[] = 'other.things';
  2029. $this->assertEquals($expected, $this->Form->fields);
  2030. }
  2031. /**
  2032. * testSecuredControlDuplicate method
  2033. *
  2034. * Test that a hidden field followed by a visible field
  2035. * undoes the hidden field locking.
  2036. *
  2037. * @return void
  2038. */
  2039. public function testSecuredControlDuplicate()
  2040. {
  2041. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  2042. $this->assertEquals([], $this->Form->fields);
  2043. $this->Form->control('text_val', [
  2044. 'type' => 'hidden',
  2045. 'value' => 'some text',
  2046. ]);
  2047. $expected = ['text_val' => 'some text'];
  2048. $this->assertEquals($expected, $this->Form->fields);
  2049. $this->Form->control('text_val', [
  2050. 'type' => 'text',
  2051. ]);
  2052. $expected = ['text_val'];
  2053. $this->assertEquals($expected, $this->Form->fields);
  2054. }
  2055. /**
  2056. * testFormSecuredFileControl method
  2057. *
  2058. * Tests that the correct keys are added to the field hash index.
  2059. *
  2060. * @return void
  2061. */
  2062. public function testFormSecuredFileControl()
  2063. {
  2064. $this->assertEquals([], $this->Form->fields);
  2065. $this->Form->file('Attachment.file');
  2066. $expected = [
  2067. 'Attachment.file.name', 'Attachment.file.type',
  2068. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2069. 'Attachment.file.size',
  2070. ];
  2071. $this->assertEquals($expected, $this->Form->fields);
  2072. }
  2073. /**
  2074. * testFormSecuredMultipleSelect method
  2075. *
  2076. * Test that multiple selects keys are added to field hash.
  2077. *
  2078. * @return void
  2079. */
  2080. public function testFormSecuredMultipleSelect()
  2081. {
  2082. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  2083. $this->assertEquals([], $this->Form->fields);
  2084. $options = ['1' => 'one', '2' => 'two'];
  2085. $this->Form->select('Model.select', $options);
  2086. $expected = ['Model.select'];
  2087. $this->assertEquals($expected, $this->Form->fields);
  2088. $this->Form->fields = [];
  2089. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2090. $this->assertEquals($expected, $this->Form->fields);
  2091. }
  2092. /**
  2093. * testFormSecuredRadio method
  2094. *
  2095. * @return void
  2096. */
  2097. public function testFormSecuredRadio()
  2098. {
  2099. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  2100. $this->assertEquals([], $this->Form->fields);
  2101. $options = ['1' => 'option1', '2' => 'option2'];
  2102. $this->Form->radio('Test.test', $options);
  2103. $expected = ['Test.test'];
  2104. $this->assertEquals($expected, $this->Form->fields);
  2105. $this->Form->radio('Test.all', $options, [
  2106. 'disabled' => ['option1', 'option2'],
  2107. ]);
  2108. $expected = ['Test.test', 'Test.all' => ''];
  2109. $this->assertEquals($expected, $this->Form->fields);
  2110. $this->Form->radio('Test.some', $options, [
  2111. 'disabled' => ['option1'],
  2112. ]);
  2113. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2114. $this->assertEquals($expected, $this->Form->fields);
  2115. }
  2116. /**
  2117. * testFormSecuredAndDisabledNotAssoc method
  2118. *
  2119. * Test that when disabled is in a list based attribute array it works.
  2120. *
  2121. * @return void
  2122. */
  2123. public function testFormSecuredAndDisabledNotAssoc()
  2124. {
  2125. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  2126. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2127. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2128. $this->Form->text('Model.text', ['disabled']);
  2129. $this->Form->textarea('Model.textarea', ['disabled']);
  2130. $this->Form->password('Model.password', ['disabled']);
  2131. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2132. $expected = [
  2133. 'Model.radio' => '',
  2134. ];
  2135. $this->assertEquals($expected, $this->Form->fields);
  2136. }
  2137. /**
  2138. * testFormSecuredAndDisabled method
  2139. *
  2140. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2141. * hashing.
  2142. *
  2143. * @return void
  2144. */
  2145. public function testFormSecuredAndDisabled()
  2146. {
  2147. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  2148. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2149. $this->Form->text('Model.text', ['disabled' => true]);
  2150. $this->Form->password('Model.text', ['disabled' => true]);
  2151. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2152. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2153. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2154. $this->Form->year('Model.year', ['disabled' => true]);
  2155. $this->Form->month('Model.month', ['disabled' => true]);
  2156. $this->Form->day('Model.day', ['disabled' => true]);
  2157. $this->Form->hour('Model.hour', ['disabled' => true]);
  2158. $this->Form->minute('Model.minute', ['disabled' => true]);
  2159. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2160. $expected = [
  2161. 'Model.radio' => '',
  2162. ];
  2163. $this->assertEquals($expected, $this->Form->fields);
  2164. }
  2165. /**
  2166. * testDisableSecurityUsingForm method
  2167. *
  2168. * @return void
  2169. */
  2170. public function testDisableSecurityUsingForm()
  2171. {
  2172. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  2173. 'disabledFields' => [],
  2174. ]));
  2175. $this->Form->create();
  2176. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2177. $this->Form->text('Addresses.title');
  2178. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2179. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2180. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2181. $result = $this->Form->fields;
  2182. $expected = [
  2183. 'Addresses.id' => '123456', 'Addresses.title',
  2184. ];
  2185. $this->assertEquals($expected, $result);
  2186. }
  2187. /**
  2188. * testUnlockFieldAddsToList method
  2189. *
  2190. * Test disableField.
  2191. *
  2192. * @return void
  2193. */
  2194. public function testUnlockFieldAddsToList()
  2195. {
  2196. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  2197. 'unlockedFields' => [],
  2198. ]));
  2199. $this->Form->unlockField('Contact.name');
  2200. $this->Form->text('Contact.name');
  2201. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2202. $this->assertEquals([], $this->Form->fields);
  2203. }
  2204. /**
  2205. * testUnlockFieldRemovingFromFields method
  2206. *
  2207. * Test unlockField removing from fields array.
  2208. *
  2209. * @return void
  2210. */
  2211. public function testUnlockFieldRemovingFromFields()
  2212. {
  2213. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  2214. 'unlockedFields' => [],
  2215. ]));
  2216. $this->Form->create($this->article);
  2217. $this->Form->hidden('Article.id', ['value' => 1]);
  2218. $this->Form->text('Article.title');
  2219. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2220. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2221. $this->Form->unlockField('Article.title');
  2222. $this->Form->unlockField('Article.id');
  2223. $this->assertEquals([], $this->Form->fields);
  2224. }
  2225. /**
  2226. * testResetUnlockFields method
  2227. *
  2228. * Test reset unlockFields, when create new form.
  2229. *
  2230. * @return void
  2231. */
  2232. public function testResetUnlockFields()
  2233. {
  2234. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', [
  2235. 'key' => 'testKey',
  2236. 'unlockedFields' => [],
  2237. ]));
  2238. $this->Form->unlockField('Contact.id');
  2239. $this->Form->create();
  2240. $this->Form->hidden('Contact.id', ['value' => 1]);
  2241. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2242. $this->Form->end();
  2243. $this->Form->create(null);
  2244. $this->Form->hidden('Contact.id', ['value' => 1]);
  2245. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2246. }
  2247. /**
  2248. * testSecuredFormUrlIgnoresHost method
  2249. *
  2250. * Test that only the path + query elements of a form's URL show up in their hash.
  2251. *
  2252. * @return void
  2253. */
  2254. public function testSecuredFormUrlIgnoresHost()
  2255. {
  2256. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', ['key' => 'testKey']));
  2257. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2258. $this->Form->create($this->article, [
  2259. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2260. ]);
  2261. $result = $this->Form->secure();
  2262. $this->assertStringContainsString($expected, $result);
  2263. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2264. $result = $this->Form->secure();
  2265. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2266. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2267. $result = $this->Form->secure();
  2268. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2269. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2270. $result = $this->Form->secure();
  2271. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2272. }
  2273. /**
  2274. * testSecuredFormUrlHasHtmlAndIdentifier method
  2275. *
  2276. * Test that URL, HTML and identifier show up in their hashes.
  2277. *
  2278. * @return void
  2279. */
  2280. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2281. {
  2282. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  2283. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2284. $this->Form->create($this->article, [
  2285. 'url' => [
  2286. 'controller' => 'articles',
  2287. 'action' => 'view',
  2288. '?' => [
  2289. 'page' => 1,
  2290. 'limit' => 10,
  2291. 'html' => '<>"',
  2292. ],
  2293. '#' => 'result',
  2294. ],
  2295. ]);
  2296. $result = $this->Form->secure();
  2297. $this->assertStringContainsString($expected, $result);
  2298. $this->Form->create($this->article, [
  2299. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2300. ]);
  2301. $result = $this->Form->secure();
  2302. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2303. $this->Form->create($this->article, [
  2304. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2305. ]);
  2306. $result = $this->Form->secure();
  2307. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2308. }
  2309. /**
  2310. * testErrorMessageDisplay method
  2311. *
  2312. * Test error message display.
  2313. *
  2314. * @return void
  2315. */
  2316. public function testErrorMessageDisplay()
  2317. {
  2318. $this->article['errors'] = [
  2319. 'Article' => [
  2320. 'title' => 'error message',
  2321. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2322. ],
  2323. ];
  2324. $this->Form->create($this->article);
  2325. $result = $this->Form->control('Article.title');
  2326. $expected = [
  2327. 'div' => ['class' => 'input text error'],
  2328. 'label' => ['for' => 'article-title'],
  2329. 'Title',
  2330. '/label',
  2331. 'input' => [
  2332. 'type' => 'text', 'name' => 'Article[title]',
  2333. 'id' => 'article-title', 'class' => 'form-error',
  2334. ],
  2335. ['div' => ['class' => 'error-message']],
  2336. 'error message',
  2337. '/div',
  2338. '/div',
  2339. ];
  2340. $this->assertHtml($expected, $result);
  2341. $result = $this->Form->control('Article.title', [
  2342. 'templates' => [
  2343. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2344. ],
  2345. ]);
  2346. $expected = [
  2347. 'div' => ['class' => 'input text error'],
  2348. 'label' => ['for' => 'article-title'],
  2349. 'Title',
  2350. '/label',
  2351. 'input' => [
  2352. 'type' => 'text', 'name' => 'Article[title]',
  2353. 'id' => 'article-title', 'class' => 'form-error',
  2354. ],
  2355. '/div',
  2356. ];
  2357. $this->assertHtml($expected, $result);
  2358. $result = $this->Form->control('Article.content');
  2359. $expected = [
  2360. 'div' => ['class' => 'input text error'],
  2361. 'label' => ['for' => 'article-content'],
  2362. 'Content',
  2363. '/label',
  2364. 'input' => [
  2365. 'type' => 'text', 'name' => 'Article[content]',
  2366. 'id' => 'article-content', 'class' => 'form-error',
  2367. ],
  2368. ['div' => ['class' => 'error-message']],
  2369. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2370. '/div',
  2371. '/div',
  2372. ];
  2373. $this->assertHtml($expected, $result);
  2374. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2375. $expected = [
  2376. 'div' => ['class' => 'input text error'],
  2377. 'label' => ['for' => 'article-content'],
  2378. 'Content',
  2379. '/label',
  2380. 'input' => [
  2381. 'type' => 'text', 'name' => 'Article[content]',
  2382. 'id' => 'article-content', 'class' => 'form-error',
  2383. ],
  2384. ['div' => ['class' => 'error-message']],
  2385. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2386. '/div',
  2387. '/div',
  2388. ];
  2389. $this->assertHtml($expected, $result);
  2390. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2391. $expected = [
  2392. 'div' => ['class' => 'input text error'],
  2393. 'label' => ['for' => 'article-content'],
  2394. 'Content',
  2395. '/label',
  2396. 'input' => [
  2397. 'type' => 'text', 'name' => 'Article[content]',
  2398. 'id' => 'article-content', 'class' => 'form-error',
  2399. ],
  2400. ['div' => ['class' => 'error-message']],
  2401. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2402. '/div',
  2403. '/div',
  2404. ];
  2405. $this->assertHtml($expected, $result);
  2406. }
  2407. /**
  2408. * testEmptyErrorValidation method
  2409. *
  2410. * Test validation errors, when validation message is an empty string.
  2411. *
  2412. * @return void
  2413. */
  2414. public function testEmptyErrorValidation()
  2415. {
  2416. $this->article['errors'] = [
  2417. 'Article' => ['title' => ''],
  2418. ];
  2419. $this->Form->create($this->article);
  2420. $result = $this->Form->control('Article.title');
  2421. $expected = [
  2422. 'div' => ['class' => 'input text error'],
  2423. 'label' => ['for' => 'article-title'],
  2424. 'Title',
  2425. '/label',
  2426. 'input' => [
  2427. 'type' => 'text', 'name' => 'Article[title]',
  2428. 'id' => 'article-title', 'class' => 'form-error',
  2429. ],
  2430. ['div' => ['class' => 'error-message']],
  2431. [],
  2432. '/div',
  2433. '/div',
  2434. ];
  2435. $this->assertHtml($expected, $result);
  2436. }
  2437. /**
  2438. * testEmptyControlErrorValidation method
  2439. *
  2440. * Test validation errors, when calling control() overriding validation message by an empty string.
  2441. *
  2442. * @return void
  2443. */
  2444. public function testEmptyControlErrorValidation()
  2445. {
  2446. $this->article['errors'] = [
  2447. 'Article' => ['title' => 'error message'],
  2448. ];
  2449. $this->Form->create($this->article);
  2450. $result = $this->Form->control('Article.title', ['error' => '']);
  2451. $expected = [
  2452. 'div' => ['class' => 'input text error'],
  2453. 'label' => ['for' => 'article-title'],
  2454. 'Title',
  2455. '/label',
  2456. 'input' => [
  2457. 'type' => 'text', 'name' => 'Article[title]',
  2458. 'id' => 'article-title', 'class' => 'form-error',
  2459. ],
  2460. ['div' => ['class' => 'error-message']],
  2461. [],
  2462. '/div',
  2463. '/div',
  2464. ];
  2465. $this->assertHtml($expected, $result);
  2466. }
  2467. /**
  2468. * testControlErrorMessage method
  2469. *
  2470. * Test validation errors, when calling control() overriding validation messages.
  2471. *
  2472. * @return void
  2473. */
  2474. public function testControlErrorMessage()
  2475. {
  2476. $this->article['errors'] = [
  2477. 'title' => ['error message'],
  2478. ];
  2479. $this->Form->create($this->article);
  2480. $result = $this->Form->control('title', [
  2481. 'error' => 'Custom error!',
  2482. ]);
  2483. $expected = [
  2484. 'div' => ['class' => 'input text required error'],
  2485. 'label' => ['for' => 'title'],
  2486. 'Title',
  2487. '/label',
  2488. 'input' => [
  2489. 'type' => 'text', 'name' => 'title',
  2490. 'id' => 'title', 'class' => 'form-error',
  2491. 'required' => 'required',
  2492. 'data-validity-message' => 'This field cannot be left empty',
  2493. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2494. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2495. ],
  2496. ['div' => ['class' => 'error-message']],
  2497. 'Custom error!',
  2498. '/div',
  2499. '/div',
  2500. ];
  2501. $this->assertHtml($expected, $result);
  2502. $result = $this->Form->control('title', [
  2503. 'error' => ['error message' => 'Custom error!'],
  2504. ]);
  2505. $expected = [
  2506. 'div' => ['class' => 'input text required error'],
  2507. 'label' => ['for' => 'title'],
  2508. 'Title',
  2509. '/label',
  2510. 'input' => [
  2511. 'type' => 'text',
  2512. 'name' => 'title',
  2513. 'id' => 'title',
  2514. 'class' => 'form-error',
  2515. 'required' => 'required',
  2516. 'data-validity-message' => 'This field cannot be left empty',
  2517. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2518. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2519. ],
  2520. ['div' => ['class' => 'error-message']],
  2521. 'Custom error!',
  2522. '/div',
  2523. '/div',
  2524. ];
  2525. $this->assertHtml($expected, $result);
  2526. }
  2527. /**
  2528. * testFormValidationAssociated method
  2529. *
  2530. * Tests displaying errors for nested entities.
  2531. *
  2532. * @return void
  2533. */
  2534. public function testFormValidationAssociated()
  2535. {
  2536. $nested = new Entity(['foo' => 'bar']);
  2537. $nested->setError('foo', ['not a valid bar']);
  2538. $entity = new Entity(['nested' => $nested]);
  2539. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2540. $result = $this->Form->error('nested.foo');
  2541. $this->assertSame('<div class="error-message">not a valid bar</div>', $result);
  2542. }
  2543. /**
  2544. * testFormValidationAssociatedSecondLevel method
  2545. *
  2546. * Test form error display with associated model.
  2547. *
  2548. * @return void
  2549. */
  2550. public function testFormValidationAssociatedSecondLevel()
  2551. {
  2552. $inner = new Entity(['bar' => 'baz']);
  2553. $nested = new Entity(['foo' => $inner]);
  2554. $entity = new Entity(['nested' => $nested]);
  2555. $inner->setError('bar', ['not a valid one']);
  2556. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2557. $result = $this->Form->error('nested.foo.bar');
  2558. $this->assertSame('<div class="error-message">not a valid one</div>', $result);
  2559. }
  2560. /**
  2561. * testFormValidationMultiRecord method
  2562. *
  2563. * Test form error display with multiple records.
  2564. *
  2565. * @return void
  2566. */
  2567. public function testFormValidationMultiRecord()
  2568. {
  2569. $one = new Entity();
  2570. $two = new Entity();
  2571. $this->getTableLocator()->get('Contacts', [
  2572. 'className' => ContactsTable::class,
  2573. ]);
  2574. $one->set('email', '');
  2575. $one->setError('email', ['invalid email']);
  2576. $two->set('name', '');
  2577. $two->setError('name', ['This is wrong']);
  2578. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2579. $result = $this->Form->control('0.email');
  2580. $expected = [
  2581. 'div' => ['class' => 'input email error'],
  2582. 'label' => ['for' => '0-email'],
  2583. 'Email',
  2584. '/label',
  2585. 'input' => [
  2586. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2587. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2588. ],
  2589. ['div' => ['class' => 'error-message']],
  2590. 'invalid email',
  2591. '/div',
  2592. '/div',
  2593. ];
  2594. $this->assertHtml($expected, $result);
  2595. $result = $this->Form->control('1.name');
  2596. $expected = [
  2597. 'div' => ['class' => 'input text error'],
  2598. 'label' => ['for' => '1-name'],
  2599. 'Name',
  2600. '/label',
  2601. 'input' => [
  2602. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2603. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2604. ],
  2605. ['div' => ['class' => 'error-message']],
  2606. 'This is wrong',
  2607. '/div',
  2608. '/div',
  2609. ];
  2610. $this->assertHtml($expected, $result);
  2611. }
  2612. /**
  2613. * testControl method
  2614. *
  2615. * Test various incarnations of control().
  2616. *
  2617. * @return void
  2618. */
  2619. public function testControl()
  2620. {
  2621. $this->getTableLocator()->get('ValidateUsers', [
  2622. 'className' => ValidateUsersTable::class,
  2623. ]);
  2624. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2625. $result = $this->Form->control('ValidateUsers.balance');
  2626. $expected = [
  2627. 'div' => ['class'],
  2628. 'label' => ['for'],
  2629. 'Balance',
  2630. '/label',
  2631. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2632. '/div',
  2633. ];
  2634. $this->assertHtml($expected, $result);
  2635. $result = $this->Form->control('ValidateUser.cost_decimal');
  2636. $expected = [
  2637. 'div' => ['class'],
  2638. 'label' => ['for'],
  2639. 'Cost Decimal',
  2640. '/label',
  2641. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2642. '/div',
  2643. ];
  2644. $this->assertHtml($expected, $result);
  2645. $result = $this->Form->control('ValidateUser.null_decimal');
  2646. $expected = [
  2647. 'div' => ['class'],
  2648. 'label' => ['for'],
  2649. 'Null Decimal',
  2650. '/label',
  2651. 'input' => ['name', 'type' => 'number', 'id'],
  2652. '/div',
  2653. ];
  2654. $this->assertHtml($expected, $result);
  2655. }
  2656. /**
  2657. * testControlCustomization method
  2658. *
  2659. * Tests the input method and passing custom options.
  2660. *
  2661. * @return void
  2662. */
  2663. public function testControlCustomization()
  2664. {
  2665. $this->getTableLocator()->get('Contacts', [
  2666. 'className' => ContactsTable::class,
  2667. ]);
  2668. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2669. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2670. $expected = [
  2671. 'div' => ['class' => 'input email'],
  2672. 'label' => ['for' => 'custom'],
  2673. 'Email',
  2674. '/label',
  2675. ['input' => [
  2676. 'type' => 'email', 'name' => 'Contact[email]',
  2677. 'id' => 'custom', 'maxlength' => 255,
  2678. ]],
  2679. '/div',
  2680. ];
  2681. $this->assertHtml($expected, $result);
  2682. $result = $this->Form->control('Contact.email', [
  2683. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2684. ]);
  2685. $expected = [
  2686. '<div',
  2687. 'label' => ['for' => 'contact-email'],
  2688. 'Email',
  2689. '/label',
  2690. ['input' => [
  2691. 'type' => 'email', 'name' => 'Contact[email]',
  2692. 'id' => 'contact-email', 'maxlength' => 255,
  2693. ]],
  2694. '/div',
  2695. ];
  2696. $this->assertHtml($expected, $result);
  2697. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2698. $expected = [
  2699. 'div' => ['class' => 'input text'],
  2700. 'label' => ['for' => 'contact-email'],
  2701. 'Email',
  2702. '/label',
  2703. ['input' => [
  2704. 'type' => 'text', 'name' => 'Contact[email]',
  2705. 'id' => 'contact-email', 'maxlength' => '255',
  2706. ]],
  2707. '/div',
  2708. ];
  2709. $this->assertHtml($expected, $result);
  2710. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2711. $expected = [
  2712. 'div' => ['class' => 'input text'],
  2713. 'label' => ['for' => 'contact-5-email'],
  2714. 'Email',
  2715. '/label',
  2716. ['input' => [
  2717. 'type' => 'text', 'name' => 'Contact[5][email]',
  2718. 'id' => 'contact-5-email', 'maxlength' => '255',
  2719. ]],
  2720. '/div',
  2721. ];
  2722. $this->assertHtml($expected, $result);
  2723. $result = $this->Form->control('Contact.password');
  2724. $expected = [
  2725. 'div' => ['class' => 'input password'],
  2726. 'label' => ['for' => 'contact-password'],
  2727. 'Password',
  2728. '/label',
  2729. ['input' => [
  2730. 'type' => 'password', 'name' => 'Contact[password]',
  2731. 'id' => 'contact-password',
  2732. ]],
  2733. '/div',
  2734. ];
  2735. $this->assertHtml($expected, $result);
  2736. $result = $this->Form->control('Contact.email', [
  2737. 'type' => 'file', 'class' => 'textbox',
  2738. ]);
  2739. $expected = [
  2740. 'div' => ['class' => 'input file'],
  2741. 'label' => ['for' => 'contact-email'],
  2742. 'Email',
  2743. '/label',
  2744. ['input' => [
  2745. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2746. 'id' => 'contact-email',
  2747. ]],
  2748. '/div',
  2749. ];
  2750. $this->assertHtml($expected, $result);
  2751. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2752. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2753. $result = $this->Form->control('phone');
  2754. $expected = [
  2755. 'div' => ['class' => 'input tel'],
  2756. 'label' => ['for' => 'phone'],
  2757. 'Phone',
  2758. '/label',
  2759. ['input' => [
  2760. 'type' => 'tel', 'name' => 'phone',
  2761. 'value' => 'Hello &amp; World &gt; weird chars',
  2762. 'id' => 'phone', 'maxlength' => 255,
  2763. ]],
  2764. '/div',
  2765. ];
  2766. $this->assertHtml($expected, $result);
  2767. $this->View->setRequest(
  2768. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2769. );
  2770. $this->Form->create();
  2771. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2772. $expected = [
  2773. 'div' => ['class' => 'input text'],
  2774. 'label' => ['for' => 'myId'],
  2775. 'Field',
  2776. '/label',
  2777. 'input' => [
  2778. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2779. 'value' => 'My value', 'id' => 'myId',
  2780. ],
  2781. '/div',
  2782. ];
  2783. $this->assertHtml($expected, $result);
  2784. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2785. $this->Form->create();
  2786. $entity->setError('field', 'Badness!');
  2787. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2788. $result = $this->Form->control('field');
  2789. $expected = [
  2790. 'div' => ['class' => 'input text error'],
  2791. 'label' => ['for' => 'field'],
  2792. 'Field',
  2793. '/label',
  2794. 'input' => [
  2795. 'type' => 'text', 'name' => 'field',
  2796. 'id' => 'field', 'class' => 'form-error',
  2797. ],
  2798. ['div' => ['class' => 'error-message']],
  2799. 'Badness!',
  2800. '/div',
  2801. '/div',
  2802. ];
  2803. $this->assertHtml($expected, $result);
  2804. $result = $this->Form->control('field', [
  2805. 'templates' => [
  2806. 'inputContainerError' => '{{content}}{{error}}',
  2807. 'error' => '<span class="error-message">{{content}}</span>',
  2808. ],
  2809. ]);
  2810. $expected = [
  2811. 'label' => ['for' => 'field'],
  2812. 'Field',
  2813. '/label',
  2814. 'input' => [
  2815. 'type' => 'text', 'name' => 'field',
  2816. 'id' => 'field', 'class' => 'form-error',
  2817. ],
  2818. ['span' => ['class' => 'error-message']],
  2819. 'Badness!',
  2820. '/span',
  2821. ];
  2822. $this->assertHtml($expected, $result);
  2823. $entity->setError('field', ['minLength'], true);
  2824. $result = $this->Form->control('field', [
  2825. 'error' => [
  2826. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2827. 'maxLength' => 'login too large',
  2828. ],
  2829. ]);
  2830. $expected = [
  2831. 'div' => ['class' => 'input text error'],
  2832. 'label' => ['for' => 'field'],
  2833. 'Field',
  2834. '/label',
  2835. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2836. ['div' => ['class' => 'error-message']],
  2837. 'Le login doit contenir au moins 2 caractères',
  2838. '/div',
  2839. '/div',
  2840. ];
  2841. $this->assertHtml($expected, $result);
  2842. $entity->setError('field', ['maxLength'], true);
  2843. $result = $this->Form->control('field', [
  2844. 'error' => [
  2845. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2846. 'maxLength' => 'login too large',
  2847. ],
  2848. ]);
  2849. $expected = [
  2850. 'div' => ['class' => 'input text error'],
  2851. 'label' => ['for' => 'field'],
  2852. 'Field',
  2853. '/label',
  2854. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2855. ['div' => ['class' => 'error-message']],
  2856. 'login too large',
  2857. '/div',
  2858. '/div',
  2859. ];
  2860. $this->assertHtml($expected, $result);
  2861. }
  2862. /**
  2863. * testControlWithTemplateFile method
  2864. *
  2865. * Test that control() accepts a template file.
  2866. *
  2867. * @return void
  2868. */
  2869. public function testControlWithTemplateFile()
  2870. {
  2871. $result = $this->Form->control('field', [
  2872. 'templates' => 'htmlhelper_tags',
  2873. ]);
  2874. $expected = [
  2875. 'label' => ['for' => 'field'],
  2876. 'Field',
  2877. '/label',
  2878. 'input' => [
  2879. 'type' => 'text', 'name' => 'field',
  2880. 'id' => 'field',
  2881. ],
  2882. ];
  2883. $this->assertHtml($expected, $result);
  2884. }
  2885. /**
  2886. * testNestedControlsEndWithBrackets method
  2887. *
  2888. * Test that nested inputs end with brackets.
  2889. *
  2890. * @return void
  2891. */
  2892. public function testNestedControlsEndWithBrackets()
  2893. {
  2894. $result = $this->Form->text('nested.text[]');
  2895. $expected = [
  2896. 'input' => [
  2897. 'type' => 'text', 'name' => 'nested[text][]',
  2898. ],
  2899. ];
  2900. $this->assertHtml($expected, $result);
  2901. $result = $this->Form->file('nested.file[]');
  2902. $expected = [
  2903. 'input' => [
  2904. 'type' => 'file', 'name' => 'nested[file][]',
  2905. ],
  2906. ];
  2907. $this->assertHtml($expected, $result);
  2908. }
  2909. /**
  2910. * testCreateIdPrefix method
  2911. *
  2912. * Test id prefix.
  2913. *
  2914. * @return void
  2915. */
  2916. public function testCreateIdPrefix()
  2917. {
  2918. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2919. $result = $this->Form->control('field');
  2920. $expected = [
  2921. 'div' => ['class' => 'input text'],
  2922. 'label' => ['for' => 'prefix-field'],
  2923. 'Field',
  2924. '/label',
  2925. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2926. '/div',
  2927. ];
  2928. $this->assertHtml($expected, $result);
  2929. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2930. $expected = [
  2931. 'div' => ['class' => 'input text'],
  2932. 'label' => ['for' => 'custom-id'],
  2933. 'Field',
  2934. '/label',
  2935. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2936. '/div',
  2937. ];
  2938. $this->assertHtml($expected, $result);
  2939. $result = $this->Form->radio('Model.field', ['option A']);
  2940. $expected = [
  2941. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2942. 'label' => ['for' => 'prefix-model-field-0'],
  2943. ['input' => [
  2944. 'type' => 'radio',
  2945. 'name' => 'Model[field]',
  2946. 'value' => '0',
  2947. 'id' => 'prefix-model-field-0',
  2948. ]],
  2949. 'option A',
  2950. '/label',
  2951. ];
  2952. $this->assertHtml($expected, $result);
  2953. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2954. $expected = [
  2955. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2956. 'label' => ['for' => 'prefix-model-field-0'],
  2957. ['input' => [
  2958. 'type' => 'radio',
  2959. 'name' => 'Model[field]',
  2960. 'value' => '0',
  2961. 'id' => 'prefix-model-field-0',
  2962. ]],
  2963. 'option A',
  2964. '/label',
  2965. ];
  2966. $this->assertHtml($expected, $result);
  2967. $result = $this->Form->select(
  2968. 'Model.multi_field',
  2969. ['first'],
  2970. ['multiple' => 'checkbox']
  2971. );
  2972. $expected = [
  2973. 'input' => [
  2974. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  2975. ],
  2976. ['div' => ['class' => 'checkbox']],
  2977. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2978. ['input' => [
  2979. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2980. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2981. ]],
  2982. 'first',
  2983. '/label',
  2984. '/div',
  2985. ];
  2986. $this->assertHtml($expected, $result);
  2987. $this->Form->end();
  2988. $result = $this->Form->control('field');
  2989. $expected = [
  2990. 'div' => ['class' => 'input text'],
  2991. 'label' => ['for' => 'field'],
  2992. 'Field',
  2993. '/label',
  2994. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2995. '/div',
  2996. ];
  2997. $this->assertHtml($expected, $result);
  2998. }
  2999. /**
  3000. * testControlZero method
  3001. *
  3002. * Test that inputs with 0 can be created.
  3003. *
  3004. * @return void
  3005. */
  3006. public function testControlZero()
  3007. {
  3008. $this->getTableLocator()->get('Contacts', [
  3009. 'className' => ContactsTable::class,
  3010. ]);
  3011. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3012. $result = $this->Form->control('0');
  3013. $expected = [
  3014. 'div' => ['class' => 'input text'],
  3015. 'label' => ['for' => '0'], '/label',
  3016. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3017. '/div',
  3018. ];
  3019. $this->assertHtml($expected, $result);
  3020. }
  3021. /**
  3022. * testControlCheckbox method
  3023. *
  3024. * Test control() with checkbox creation.
  3025. *
  3026. * @return void
  3027. */
  3028. public function testControlCheckbox()
  3029. {
  3030. $articles = $this->getTableLocator()->get('Articles');
  3031. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3032. $article = $articles->newEmptyEntity();
  3033. $this->Form->create($article);
  3034. $result = $this->Form->control('Articles.active');
  3035. $expected = [
  3036. 'div' => ['class' => 'input checkbox'],
  3037. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3038. 'label' => ['for' => 'articles-active'],
  3039. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3040. 'Active',
  3041. '/label',
  3042. '/div',
  3043. ];
  3044. $this->assertHtml($expected, $result);
  3045. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3046. $expected = [
  3047. 'div' => ['class' => 'input checkbox'],
  3048. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3049. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3050. '/div',
  3051. ];
  3052. $this->assertHtml($expected, $result);
  3053. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3054. $expected = [
  3055. 'div' => ['class' => 'input checkbox'],
  3056. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3057. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3058. '/div',
  3059. ];
  3060. $this->assertHtml($expected, $result);
  3061. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3062. $expected = [
  3063. 'div' => ['class' => 'input checkbox'],
  3064. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3065. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3066. '/div',
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. $result = $this->Form->control('Articles.disabled', [
  3070. 'label' => 'Disabled',
  3071. 'type' => 'checkbox',
  3072. 'data-foo' => 'disabled',
  3073. ]);
  3074. $expected = [
  3075. 'div' => ['class' => 'input checkbox'],
  3076. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3077. 'label' => ['for' => 'articles-disabled'],
  3078. ['input' => [
  3079. 'type' => 'checkbox',
  3080. 'name' => 'Articles[disabled]',
  3081. 'value' => '1',
  3082. 'id' => 'articles-disabled',
  3083. 'data-foo' => 'disabled',
  3084. ]],
  3085. 'Disabled',
  3086. '/label',
  3087. '/div',
  3088. ];
  3089. $this->assertHtml($expected, $result);
  3090. $result = $this->Form->control('Articles.confirm', [
  3091. 'label' => 'Confirm <b>me</b>!',
  3092. 'type' => 'checkbox',
  3093. 'escape' => false,
  3094. ]);
  3095. $expected = [
  3096. 'div' => ['class' => 'input checkbox'],
  3097. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3098. 'label' => ['for' => 'articles-confirm'],
  3099. ['input' => [
  3100. 'type' => 'checkbox',
  3101. 'name' => 'Articles[confirm]',
  3102. 'value' => '1',
  3103. 'id' => 'articles-confirm',
  3104. ]],
  3105. 'Confirm <b>me</b>!',
  3106. '/label',
  3107. '/div',
  3108. ];
  3109. $this->assertHtml($expected, $result);
  3110. }
  3111. /**
  3112. * testControlHidden method
  3113. *
  3114. * Test that control() does not create wrapping div and label tag for hidden fields.
  3115. *
  3116. * @return void
  3117. */
  3118. public function testControlHidden()
  3119. {
  3120. $this->getTableLocator()->get('ValidateUsers', [
  3121. 'className' => ValidateUsersTable::class,
  3122. ]);
  3123. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3124. $result = $this->Form->control('ValidateUser.id');
  3125. $expected = [
  3126. 'input' => ['name', 'type' => 'hidden', 'id'],
  3127. ];
  3128. $this->assertHtml($expected, $result);
  3129. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3130. $expected = [
  3131. 'input' => ['name', 'type' => 'hidden', 'id'],
  3132. ];
  3133. $this->assertHtml($expected, $result);
  3134. }
  3135. /**
  3136. * testControlDatetime method
  3137. *
  3138. * Test form->control() with datetime.
  3139. *
  3140. * @return void
  3141. */
  3142. public function testControlDatetime()
  3143. {
  3144. $result = $this->Form->control('prueba', [
  3145. 'type' => 'datetime',
  3146. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3147. ]);
  3148. $expected = [
  3149. 'div' => ['class' => 'input datetime'],
  3150. 'label' => ['for' => 'prueba'],
  3151. 'Prueba',
  3152. '/label',
  3153. 'input' => ['name' => 'prueba', 'id' => 'prueba', 'type' => 'datetime-local', 'value' => '2019-09-27T02:52:43'],
  3154. '/div',
  3155. ];
  3156. $this->assertHtml($expected, $result);
  3157. }
  3158. /**
  3159. * testControlDatetimeIdPrefix method
  3160. *
  3161. * Test form->control() with datetime with id prefix.
  3162. *
  3163. * @return void
  3164. */
  3165. public function testControlDatetimeIdPrefix()
  3166. {
  3167. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3168. $result = $this->Form->control('prueba', [
  3169. 'type' => 'datetime',
  3170. ]);
  3171. $expected = [
  3172. 'div' => ['class' => 'input datetime'],
  3173. 'label' => ['for' => 'prefix-prueba'],
  3174. 'Prueba',
  3175. '/label',
  3176. 'input' => ['name' => 'prueba', 'id' => 'prefix-prueba', 'type' => 'datetime-local', 'value' => ''],
  3177. '/div',
  3178. ];
  3179. $this->assertHtml($expected, $result);
  3180. }
  3181. /**
  3182. * testControlCheckboxWithDisabledElements method
  3183. *
  3184. * Test generating checkboxes with disabled elements.
  3185. *
  3186. * @return void
  3187. */
  3188. public function testControlCheckboxWithDisabledElements()
  3189. {
  3190. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3191. $result = $this->Form->control('Contact.multiple', [
  3192. 'multiple' => 'checkbox',
  3193. 'disabled' => 'disabled',
  3194. 'options' => $options,
  3195. ]);
  3196. $expected = [
  3197. ['div' => ['class' => 'input select']],
  3198. ['label' => ['for' => 'contact-multiple']],
  3199. 'Multiple',
  3200. '/label',
  3201. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3202. ['div' => ['class' => 'checkbox']],
  3203. ['label' => ['for' => 'contact-multiple-1']],
  3204. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3205. 'One',
  3206. '/label',
  3207. '/div',
  3208. ['div' => ['class' => 'checkbox']],
  3209. ['label' => ['for' => 'contact-multiple-2']],
  3210. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3211. 'Two',
  3212. '/label',
  3213. '/div',
  3214. ['div' => ['class' => 'checkbox']],
  3215. ['label' => ['for' => 'contact-multiple-3']],
  3216. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3217. 'Three',
  3218. '/label',
  3219. '/div',
  3220. '/div',
  3221. ];
  3222. $this->assertHtml($expected, $result);
  3223. // make sure 50 does only disable 50, and not 50f5c0cf
  3224. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3225. $disabled = [50];
  3226. $expected = [
  3227. ['div' => ['class' => 'input select']],
  3228. ['label' => ['for' => 'contact-multiple']],
  3229. 'Multiple',
  3230. '/label',
  3231. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3232. ['div' => ['class' => 'checkbox']],
  3233. ['label' => ['for' => 'contact-multiple-50']],
  3234. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3235. 'Fifty',
  3236. '/label',
  3237. '/div',
  3238. ['div' => ['class' => 'checkbox']],
  3239. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3240. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3241. 'Stringy',
  3242. '/label',
  3243. '/div',
  3244. '/div',
  3245. ];
  3246. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3247. $this->assertHtml($expected, $result);
  3248. }
  3249. /**
  3250. * testControlWithLeadingInteger method
  3251. *
  3252. * Test input name with leading integer, ensure attributes are generated correctly.
  3253. *
  3254. * @return void
  3255. */
  3256. public function testControlWithLeadingInteger()
  3257. {
  3258. $result = $this->Form->text('0.Node.title');
  3259. $expected = [
  3260. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3261. ];
  3262. $this->assertHtml($expected, $result);
  3263. }
  3264. /**
  3265. * testControlSelectType method
  3266. *
  3267. * Test form->control() with select type inputs.
  3268. *
  3269. * @return void
  3270. */
  3271. public function testControlSelectType()
  3272. {
  3273. $result = $this->Form->control(
  3274. 'email',
  3275. [
  3276. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3277. );
  3278. $expected = [
  3279. 'div' => ['class' => 'input select'],
  3280. 'label' => ['for' => 'email'],
  3281. 'Email',
  3282. '/label',
  3283. ['select' => ['name' => 'email', 'id' => 'email']],
  3284. ['option' => ['value' => '']],
  3285. '/option',
  3286. ['option' => ['value' => 'è']],
  3287. 'Firést',
  3288. '/option',
  3289. ['option' => ['value' => 'é']],
  3290. 'Secoènd',
  3291. '/option',
  3292. '/select',
  3293. '/div',
  3294. ];
  3295. $this->assertHtml($expected, $result);
  3296. $result = $this->Form->control(
  3297. 'email',
  3298. [
  3299. 'options' => ['First', 'Second'], 'empty' => true]
  3300. );
  3301. $expected = [
  3302. 'div' => ['class' => 'input select'],
  3303. 'label' => ['for' => 'email'],
  3304. 'Email',
  3305. '/label',
  3306. ['select' => ['name' => 'email', 'id' => 'email']],
  3307. ['option' => ['value' => '']],
  3308. '/option',
  3309. ['option' => ['value' => '0']],
  3310. 'First',
  3311. '/option',
  3312. ['option' => ['value' => '1']],
  3313. 'Second',
  3314. '/option',
  3315. '/select',
  3316. '/div',
  3317. ];
  3318. $this->assertHtml($expected, $result);
  3319. $result = $this->Form->control('email', [
  3320. 'type' => 'select',
  3321. 'options' => new \ArrayObject(['First', 'Second']),
  3322. 'empty' => true,
  3323. ]);
  3324. $this->assertHtml($expected, $result);
  3325. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3326. $this->View->setRequest(
  3327. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3328. );
  3329. $this->Form->create();
  3330. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3331. $expected = [
  3332. 'div' => ['class' => 'input select'],
  3333. 'label' => ['for' => 'model-user-id'],
  3334. 'User',
  3335. '/label',
  3336. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3337. ['option' => ['value' => '']],
  3338. '/option',
  3339. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3340. 'good',
  3341. '/option',
  3342. ['option' => ['value' => 'other']],
  3343. 'bad',
  3344. '/option',
  3345. '/select',
  3346. '/div',
  3347. ];
  3348. $this->assertHtml($expected, $result);
  3349. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3350. $this->View->setRequest(
  3351. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3352. );
  3353. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3354. $expected = [
  3355. 'div' => ['class' => 'input select'],
  3356. 'label' => ['for' => 'thing-user-id'],
  3357. 'User',
  3358. '/label',
  3359. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3360. ['option' => ['value' => '']],
  3361. 'Some Empty',
  3362. '/option',
  3363. ['option' => ['value' => 'value']],
  3364. 'good',
  3365. '/option',
  3366. ['option' => ['value' => 'other']],
  3367. 'bad',
  3368. '/option',
  3369. '/select',
  3370. '/div',
  3371. ];
  3372. $this->assertHtml($expected, $result);
  3373. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3374. $this->View->setRequest(
  3375. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3376. );
  3377. $this->Form->create();
  3378. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3379. $expected = [
  3380. 'div' => ['class' => 'input select'],
  3381. 'label' => ['for' => 'thing-user-id'],
  3382. 'User',
  3383. '/label',
  3384. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3385. ['option' => ['value' => '']],
  3386. 'Some Empty',
  3387. '/option',
  3388. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3389. 'good',
  3390. '/option',
  3391. ['option' => ['value' => 'other']],
  3392. 'bad',
  3393. '/option',
  3394. '/select',
  3395. '/div',
  3396. ];
  3397. $this->assertHtml($expected, $result);
  3398. $result = $this->Form->control('Publisher.id', [
  3399. 'label' => 'Publisher',
  3400. 'type' => 'select',
  3401. 'multiple' => 'checkbox',
  3402. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3403. ]);
  3404. $expected = [
  3405. ['div' => ['class' => 'input select']],
  3406. ['label' => ['for' => 'publisher-id']],
  3407. 'Publisher',
  3408. '/label',
  3409. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3410. ['div' => ['class' => 'checkbox']],
  3411. ['label' => ['for' => 'publisher-id-value-1']],
  3412. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3413. 'Label 1',
  3414. '/label',
  3415. '/div',
  3416. ['div' => ['class' => 'checkbox']],
  3417. ['label' => ['for' => 'publisher-id-value-2']],
  3418. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3419. 'Label 2',
  3420. '/label',
  3421. '/div',
  3422. '/div',
  3423. ];
  3424. $this->assertHtml($expected, $result);
  3425. }
  3426. /**
  3427. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3428. *
  3429. * Test that control() and a non standard primary key makes a hidden input by default.
  3430. *
  3431. * @return void
  3432. */
  3433. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3434. {
  3435. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3436. $this->Form->create($this->article);
  3437. $result = $this->Form->control('title');
  3438. $expected = [
  3439. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3440. ];
  3441. $this->assertHtml($expected, $result);
  3442. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3443. $this->Form->create($this->article);
  3444. $result = $this->Form->control('title');
  3445. $expected = [
  3446. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3447. ];
  3448. $this->assertHtml($expected, $result);
  3449. $result = $this->Form->control('body');
  3450. $expected = [
  3451. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3452. ];
  3453. $this->assertHtml($expected, $result);
  3454. }
  3455. /**
  3456. * testControlOverridingMagicSelectType method
  3457. *
  3458. * Test that overriding the magic select type widget is possible.
  3459. *
  3460. * @return void
  3461. */
  3462. public function testControlOverridingMagicSelectType()
  3463. {
  3464. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3465. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3466. $expected = [
  3467. 'div' => ['class' => 'input text'],
  3468. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3469. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3470. '/div',
  3471. ];
  3472. $this->assertHtml($expected, $result);
  3473. //Check that magic types still work for plural/singular vars
  3474. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3475. $result = $this->Form->control('Model.type');
  3476. $expected = [
  3477. 'div' => ['class' => 'input select'],
  3478. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3479. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3480. ['option' => ['value' => 'value']], 'good', '/option',
  3481. ['option' => ['value' => 'other']], 'bad', '/option',
  3482. '/select',
  3483. '/div',
  3484. ];
  3485. $this->assertHtml($expected, $result);
  3486. }
  3487. /**
  3488. * testControlMagicTypeDoesNotOverride method
  3489. *
  3490. * Test that inferred types do not override developer input.
  3491. *
  3492. * @return void
  3493. */
  3494. public function testControlMagicTypeDoesNotOverride()
  3495. {
  3496. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3497. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3498. $expected = [
  3499. 'div' => ['class' => 'input checkbox'],
  3500. ['input' => [
  3501. 'type' => 'hidden',
  3502. 'name' => 'Model[user]',
  3503. 'value' => 0,
  3504. ]],
  3505. 'label' => ['for' => 'model-user'],
  3506. ['input' => [
  3507. 'name' => 'Model[user]',
  3508. 'type' => 'checkbox',
  3509. 'id' => 'model-user',
  3510. 'value' => 1,
  3511. ]],
  3512. 'User',
  3513. '/label',
  3514. '/div',
  3515. ];
  3516. $this->assertHtml($expected, $result);
  3517. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3518. $options = [
  3519. 1 => 'blue',
  3520. 2 => 'red',
  3521. ];
  3522. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3523. $expected = [
  3524. 'div' => ['class' => 'input select'],
  3525. 'label' => ['for' => 'tags-ids'],
  3526. 'Tags',
  3527. '/label',
  3528. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3529. ['div' => ['class' => 'checkbox']],
  3530. ['label' => ['for' => 'tags-ids-1']],
  3531. ['input' => [
  3532. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3533. 'value' => '1', 'name' => 'tags[_ids][]',
  3534. ]],
  3535. 'blue',
  3536. '/label',
  3537. '/div',
  3538. ['div' => ['class' => 'checkbox']],
  3539. ['label' => ['for' => 'tags-ids-2']],
  3540. ['input' => [
  3541. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3542. 'value' => '2', 'name' => 'tags[_ids][]',
  3543. ]],
  3544. 'red',
  3545. '/label',
  3546. '/div',
  3547. '/div',
  3548. ];
  3549. $this->assertHtml($expected, $result);
  3550. }
  3551. /**
  3552. * testControlMagicSelectForTypeNumber method
  3553. *
  3554. * Test that magic control() selects are created for type=number.
  3555. *
  3556. * @return void
  3557. */
  3558. public function testControlMagicSelectForTypeNumber()
  3559. {
  3560. $this->getTableLocator()->get('ValidateUsers', [
  3561. 'className' => ValidateUsersTable::class,
  3562. ]);
  3563. $entity = new Entity(['balance' => 1]);
  3564. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3565. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3566. $result = $this->Form->control('balance');
  3567. $expected = [
  3568. 'div' => ['class' => 'input select'],
  3569. 'label' => ['for' => 'balance'],
  3570. 'Balance',
  3571. '/label',
  3572. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3573. ['option' => ['value' => '0']],
  3574. 'nothing',
  3575. '/option',
  3576. ['option' => ['value' => '1', 'selected' => 'selected']],
  3577. 'some',
  3578. '/option',
  3579. ['option' => ['value' => '100']],
  3580. 'a lot',
  3581. '/option',
  3582. '/select',
  3583. '/div',
  3584. ];
  3585. $this->assertHtml($expected, $result);
  3586. }
  3587. /**
  3588. * testInvalidControlTypeOption method
  3589. *
  3590. * Test invalid 'input' type option to control() function.
  3591. *
  3592. * @return void
  3593. */
  3594. public function testInvalidControlTypeOption()
  3595. {
  3596. $this->expectException(\RuntimeException::class);
  3597. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3598. $this->Form->control('text', ['type' => 'input']);
  3599. }
  3600. /**
  3601. * testControlMagicSelectChangeToRadio method
  3602. *
  3603. * Test that magic control() selects can easily be converted into radio types without error.
  3604. *
  3605. * @return void
  3606. */
  3607. public function testControlMagicSelectChangeToRadio()
  3608. {
  3609. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3610. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3611. $this->assertStringContainsString('input type="radio"', $result);
  3612. }
  3613. /**
  3614. * testFormControlSubmit method
  3615. *
  3616. * Test correct results for form::control() and type submit.
  3617. *
  3618. * @return void
  3619. */
  3620. public function testFormControlSubmit()
  3621. {
  3622. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3623. $expected = [
  3624. 'div' => ['class' => 'submit'],
  3625. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3626. '/div',
  3627. ];
  3628. $this->assertHtml($expected, $result);
  3629. }
  3630. /**
  3631. * testFormControls method
  3632. *
  3633. * Test correct results from Form::controls().
  3634. *
  3635. * @return void
  3636. */
  3637. public function testFormControlsLegendFieldset()
  3638. {
  3639. $this->Form->create($this->article);
  3640. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3641. $expected = [
  3642. '<fieldset',
  3643. '<legend',
  3644. 'The Legend',
  3645. '/legend',
  3646. '*/fieldset',
  3647. ];
  3648. $this->assertHtml($expected, $result);
  3649. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3650. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3651. $this->assertStringContainsString('<fieldset>', $result);
  3652. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3653. $this->assertStringNotContainsString('<legend>', $result);
  3654. $this->assertStringNotContainsString('<fieldset>', $result);
  3655. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3656. $this->assertStringNotContainsString('<legend>', $result);
  3657. $this->assertStringNotContainsString('<fieldset>', $result);
  3658. $this->Form->create($this->article);
  3659. $this->View->setRequest($this->View->getRequest()
  3660. ->withParam('prefix', 'admin')
  3661. ->withParam('action', 'admin_edit')
  3662. ->withParam('controller', 'articles'));
  3663. $result = $this->Form->allControls();
  3664. $expected = [
  3665. '<fieldset',
  3666. '<legend',
  3667. 'New Article',
  3668. '/legend',
  3669. '*/fieldset',
  3670. ];
  3671. $this->assertHtml($expected, $result);
  3672. $this->Form->create($this->article);
  3673. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3674. $expected = [
  3675. '<fieldset',
  3676. '<legend',
  3677. 'The Legend',
  3678. '/legend',
  3679. '*/fieldset',
  3680. ];
  3681. $this->assertHtml($expected, $result);
  3682. $this->Form->create($this->article);
  3683. $result = $this->Form->allControls([], [
  3684. 'fieldset' => [
  3685. 'class' => 'some-class some-other-class',
  3686. 'disabled' => true,
  3687. 'data-param' => 'a-param',
  3688. ],
  3689. 'legend' => 'The Legend',
  3690. ]);
  3691. $expected = [
  3692. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3693. '<legend',
  3694. 'The Legend',
  3695. '/legend',
  3696. '*/fieldset',
  3697. ];
  3698. $this->assertHtml($expected, $result);
  3699. }
  3700. /**
  3701. * testFormControls method
  3702. *
  3703. * Test the controls() method.
  3704. *
  3705. * @return void
  3706. */
  3707. public function testFormControls()
  3708. {
  3709. $this->Form->create($this->article);
  3710. $result = $this->Form->allControls();
  3711. $expected = [
  3712. '<fieldset',
  3713. '<legend', 'New Article', '/legend',
  3714. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3715. ['div' => ['class' => 'input select required']],
  3716. '*/div',
  3717. ['div' => ['class' => 'input text required']],
  3718. '*/div',
  3719. ['div' => ['class' => 'input text']],
  3720. '*/div',
  3721. ['div' => ['class' => 'input text']],
  3722. '*/div',
  3723. '/fieldset',
  3724. ];
  3725. $this->assertHtml($expected, $result);
  3726. $result = $this->Form->allControls([
  3727. 'published' => ['type' => 'boolean'],
  3728. ]);
  3729. $expected = [
  3730. '<fieldset',
  3731. '<legend', 'New Article', '/legend',
  3732. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3733. ['div' => ['class' => 'input select required']],
  3734. '*/div',
  3735. ['div' => ['class' => 'input text required']],
  3736. '*/div',
  3737. ['div' => ['class' => 'input text']],
  3738. '*/div',
  3739. ['div' => ['class' => 'input boolean']],
  3740. '*/div',
  3741. '/fieldset',
  3742. ];
  3743. $this->assertHtml($expected, $result);
  3744. $this->Form->create($this->article);
  3745. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3746. $expected = [
  3747. 'fieldset' => [],
  3748. 'legend' => [],
  3749. 'Hello',
  3750. '/legend',
  3751. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3752. ['div' => ['class' => 'input select required']],
  3753. '*/div',
  3754. ['div' => ['class' => 'input text required']],
  3755. '*/div',
  3756. ['div' => ['class' => 'input text']],
  3757. '*/div',
  3758. ['div' => ['class' => 'input text']],
  3759. '*/div',
  3760. '/fieldset',
  3761. ];
  3762. $this->assertHtml($expected, $result);
  3763. $this->Form->create();
  3764. $expected = [
  3765. 'fieldset' => [],
  3766. ['div' => ['class' => 'input text']],
  3767. 'label' => ['for' => 'foo'],
  3768. 'Foo',
  3769. '/label',
  3770. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3771. '*/div',
  3772. '/fieldset',
  3773. ];
  3774. $result = $this->Form->allControls(
  3775. ['foo' => ['type' => 'text']],
  3776. ['legend' => false]
  3777. );
  3778. $this->assertHtml($expected, $result);
  3779. }
  3780. /**
  3781. * testFormControlsBlacklist method
  3782. *
  3783. * @return void
  3784. */
  3785. public function testFormControlsBlacklist()
  3786. {
  3787. $this->Form->create($this->article);
  3788. $result = $this->Form->allControls([
  3789. 'id' => false,
  3790. ]);
  3791. $expected = [
  3792. '<fieldset',
  3793. '<legend', 'New Article', '/legend',
  3794. ['div' => ['class' => 'input select required']],
  3795. '*/div',
  3796. ['div' => ['class' => 'input text required']],
  3797. '*/div',
  3798. ['div' => ['class' => 'input text']],
  3799. '*/div',
  3800. ['div' => ['class' => 'input text']],
  3801. '*/div',
  3802. '/fieldset',
  3803. ];
  3804. $this->assertHtml($expected, $result);
  3805. $this->Form->create($this->article);
  3806. $result = $this->Form->allControls([
  3807. 'id' => [],
  3808. ]);
  3809. $expected = [
  3810. '<fieldset',
  3811. '<legend', 'New Article', '/legend',
  3812. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3813. ['div' => ['class' => 'input select required']],
  3814. '*/div',
  3815. ['div' => ['class' => 'input text required']],
  3816. '*/div',
  3817. ['div' => ['class' => 'input text']],
  3818. '*/div',
  3819. ['div' => ['class' => 'input text']],
  3820. '*/div',
  3821. '/fieldset',
  3822. ];
  3823. $this->assertHtml($expected, $result, true);
  3824. }
  3825. /**
  3826. * testSelectAsCheckbox method
  3827. *
  3828. * Test multi-select widget with checkbox formatting.
  3829. *
  3830. * @return void
  3831. */
  3832. public function testSelectAsCheckbox()
  3833. {
  3834. $result = $this->Form->select(
  3835. 'Model.multi_field',
  3836. ['first', 'second', 'third'],
  3837. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3838. );
  3839. $expected = [
  3840. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3841. ['div' => ['class' => 'checkbox']],
  3842. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3843. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3844. 'first',
  3845. '/label',
  3846. '/div',
  3847. ['div' => ['class' => 'checkbox']],
  3848. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3849. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3850. 'second',
  3851. '/label',
  3852. '/div',
  3853. ['div' => ['class' => 'checkbox']],
  3854. ['label' => ['for' => 'model-multi-field-2']],
  3855. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3856. 'third',
  3857. '/label',
  3858. '/div',
  3859. ];
  3860. $this->assertHtml($expected, $result);
  3861. $result = $this->Form->select(
  3862. 'Model.multi_field',
  3863. ['1/2' => 'half'],
  3864. ['multiple' => 'checkbox']
  3865. );
  3866. $expected = [
  3867. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3868. ['div' => ['class' => 'checkbox']],
  3869. ['label' => ['for' => 'model-multi-field-1-2']],
  3870. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3871. 'half',
  3872. '/label',
  3873. '/div',
  3874. ];
  3875. $this->assertHtml($expected, $result);
  3876. }
  3877. /**
  3878. * testLabel method
  3879. *
  3880. * Test label generation.
  3881. *
  3882. * @return void
  3883. */
  3884. public function testLabel()
  3885. {
  3886. $result = $this->Form->label('Person.name');
  3887. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3888. $this->assertHtml($expected, $result);
  3889. $result = $this->Form->label('Person.name');
  3890. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3891. $this->assertHtml($expected, $result);
  3892. $result = $this->Form->label('Person.first_name');
  3893. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3894. $this->assertHtml($expected, $result);
  3895. $result = $this->Form->label('Person.first_name', 'Your first name');
  3896. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3897. $this->assertHtml($expected, $result);
  3898. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3899. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3900. $this->assertHtml($expected, $result);
  3901. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3902. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3903. $this->assertHtml($expected, $result);
  3904. $result = $this->Form->label('Person.first_name', '');
  3905. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3906. $this->assertHtml($expected, $result);
  3907. $result = $this->Form->label('Person.2.name', '');
  3908. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3909. $this->assertHtml($expected, $result);
  3910. }
  3911. /**
  3912. * testLabelContainControl method
  3913. *
  3914. * Test that label() can accept an input with the correct template vars.
  3915. *
  3916. * @return void
  3917. */
  3918. public function testLabelContainControl()
  3919. {
  3920. $this->Form->setTemplates([
  3921. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3922. ]);
  3923. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3924. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3925. ]);
  3926. $expected = [
  3927. 'label' => ['for' => 'person-accept-terms'],
  3928. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3929. 'Accept',
  3930. '/label',
  3931. ];
  3932. $this->assertHtml($expected, $result);
  3933. }
  3934. /**
  3935. * testTextbox method
  3936. *
  3937. * Test textbox element generation.
  3938. *
  3939. * @return void
  3940. */
  3941. public function testTextbox()
  3942. {
  3943. $result = $this->Form->text('Model.field');
  3944. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3945. $this->assertHtml($expected, $result);
  3946. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3947. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3948. $this->assertHtml($expected, $result);
  3949. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3950. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3951. $this->assertHtml($expected, $result);
  3952. }
  3953. /**
  3954. * testTextBoxDataAndError method
  3955. *
  3956. * Test that text() hooks up with request data and error fields.
  3957. *
  3958. * @return void
  3959. */
  3960. public function testTextBoxDataAndError()
  3961. {
  3962. $this->article['errors'] = [
  3963. 'Contact' => ['text' => 'wrong'],
  3964. ];
  3965. $this->View->setRequest($this->View->getRequest()
  3966. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3967. ->withData('Contact.text', 'test'));
  3968. $this->Form->create($this->article);
  3969. $result = $this->Form->text('Model.text');
  3970. $expected = [
  3971. 'input' => [
  3972. 'type' => 'text',
  3973. 'name' => 'Model[text]',
  3974. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3975. ],
  3976. ];
  3977. $this->assertHtml($expected, $result);
  3978. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3979. $expected = [
  3980. 'input' => [
  3981. 'type' => 'text',
  3982. 'name' => 'Contact[text]',
  3983. 'value' => 'test',
  3984. 'id' => 'theID',
  3985. 'class' => 'form-error',
  3986. ],
  3987. ];
  3988. $this->assertHtml($expected, $result);
  3989. }
  3990. /**
  3991. * testDefaultValue method
  3992. *
  3993. * Test default value setting.
  3994. *
  3995. * @return void
  3996. */
  3997. public function testTextDefaultValue()
  3998. {
  3999. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4000. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4001. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4002. $this->assertHtml($expected, $result);
  4003. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4004. $this->Form->create();
  4005. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4006. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4007. $this->assertHtml($expected, $result);
  4008. $Articles = $this->getTableLocator()->get('Articles');
  4009. $title = $Articles->getSchema()->getColumn('title');
  4010. $Articles->getSchema()->addColumn(
  4011. 'title',
  4012. ['default' => 'default title'] + $title
  4013. );
  4014. $entity = $Articles->newEmptyEntity();
  4015. $this->Form->create($entity);
  4016. // Get default value from schema
  4017. $result = $this->Form->text('title');
  4018. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4019. $this->assertHtml($expected, $result);
  4020. // Don't get value from schema
  4021. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4022. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4023. $this->assertHtml($expected, $result);
  4024. // Custom default value overrides default value from schema
  4025. $result = $this->Form->text('title', ['default' => 'override default']);
  4026. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4027. $this->assertHtml($expected, $result);
  4028. // Default value from schema is used only for new entities.
  4029. $entity->setNew(false);
  4030. $result = $this->Form->text('title');
  4031. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4032. $this->assertHtml($expected, $result);
  4033. }
  4034. /**
  4035. * testError method
  4036. *
  4037. * Test field error generation.
  4038. *
  4039. * @return void
  4040. */
  4041. public function testError()
  4042. {
  4043. $this->article['errors'] = [
  4044. 'Article' => ['field' => 'email'],
  4045. ];
  4046. $this->Form->create($this->article);
  4047. $result = $this->Form->error('Article.field');
  4048. $expected = [
  4049. ['div' => ['class' => 'error-message']],
  4050. 'email',
  4051. '/div',
  4052. ];
  4053. $this->assertHtml($expected, $result);
  4054. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4055. $expected = [
  4056. ['div' => ['class' => 'error-message']],
  4057. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4058. '/div',
  4059. ];
  4060. $this->assertHtml($expected, $result);
  4061. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4062. $expected = [
  4063. ['div' => ['class' => 'error-message']],
  4064. '<strong', 'Badness!', '/strong',
  4065. '/div',
  4066. ];
  4067. $this->assertHtml($expected, $result);
  4068. }
  4069. /**
  4070. * testErrorRuleName method
  4071. *
  4072. * Test error translation can use rule names for translating.
  4073. *
  4074. * @return void
  4075. */
  4076. public function testErrorRuleName()
  4077. {
  4078. $this->article['errors'] = [
  4079. 'Article' => [
  4080. 'field' => ['email' => 'Your email was not good'],
  4081. ],
  4082. ];
  4083. $this->Form->create($this->article);
  4084. $result = $this->Form->error('Article.field');
  4085. $expected = [
  4086. ['div' => ['class' => 'error-message']],
  4087. 'Your email was not good',
  4088. '/div',
  4089. ];
  4090. $this->assertHtml($expected, $result);
  4091. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4092. $expected = [
  4093. ['div' => ['class' => 'error-message']],
  4094. 'Email in use',
  4095. '/div',
  4096. ];
  4097. $this->assertHtml($expected, $result);
  4098. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4099. $expected = [
  4100. ['div' => ['class' => 'error-message']],
  4101. 'Email in use',
  4102. '/div',
  4103. ];
  4104. $this->assertHtml($expected, $result);
  4105. $result = $this->Form->error('Article.field', [
  4106. 'email' => 'Key is preferred',
  4107. 'Your email was not good' => 'Email in use',
  4108. ]);
  4109. $expected = [
  4110. ['div' => ['class' => 'error-message']],
  4111. 'Key is preferred',
  4112. '/div',
  4113. ];
  4114. $this->assertHtml($expected, $result);
  4115. }
  4116. /**
  4117. * testErrorMessages method
  4118. *
  4119. * Test error with nested lists.
  4120. *
  4121. * @return void
  4122. */
  4123. public function testErrorMessages()
  4124. {
  4125. $this->article['errors'] = [
  4126. 'Article' => ['field' => 'email'],
  4127. ];
  4128. $this->Form->create($this->article);
  4129. $result = $this->Form->error('Article.field', [
  4130. 'email' => 'No good!',
  4131. ]);
  4132. $expected = [
  4133. 'div' => ['class' => 'error-message'],
  4134. 'No good!',
  4135. '/div',
  4136. ];
  4137. $this->assertHtml($expected, $result);
  4138. }
  4139. /**
  4140. * testErrorMultipleMessages method
  4141. *
  4142. * Test error() with multiple messages.
  4143. *
  4144. * @return void
  4145. */
  4146. public function testErrorMultipleMessages()
  4147. {
  4148. $this->article['errors'] = [
  4149. 'field' => ['notBlank', 'email', 'Something else'],
  4150. ];
  4151. $this->Form->create($this->article);
  4152. $result = $this->Form->error('field', [
  4153. 'notBlank' => 'Cannot be empty',
  4154. 'email' => 'No good!',
  4155. ]);
  4156. $expected = [
  4157. 'div' => ['class' => 'error-message'],
  4158. 'ul' => [],
  4159. '<li', 'Cannot be empty', '/li',
  4160. '<li', 'No good!', '/li',
  4161. '<li', 'Something else', '/li',
  4162. '/ul',
  4163. '/div',
  4164. ];
  4165. $this->assertHtml($expected, $result);
  4166. }
  4167. /**
  4168. * testPassword method
  4169. *
  4170. * Test password element generation.
  4171. *
  4172. * @return void
  4173. */
  4174. public function testPassword()
  4175. {
  4176. $this->article['errors'] = [
  4177. 'Contact' => [
  4178. 'passwd' => 1,
  4179. ],
  4180. ];
  4181. $this->Form->create($this->article);
  4182. $result = $this->Form->password('Contact.field');
  4183. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4184. $this->assertHtml($expected, $result);
  4185. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4186. $this->Form->create($this->article);
  4187. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4188. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4189. $this->assertHtml($expected, $result);
  4190. }
  4191. /**
  4192. * testRadio method
  4193. *
  4194. * Test radio element set generation.
  4195. *
  4196. * @return void
  4197. */
  4198. public function testRadio()
  4199. {
  4200. $result = $this->Form->radio('Model.field', ['option A']);
  4201. $expected = [
  4202. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4203. 'label' => ['for' => 'model-field-0'],
  4204. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4205. 'option A',
  4206. '/label',
  4207. ];
  4208. $this->assertHtml($expected, $result);
  4209. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4210. $this->assertHtml($expected, $result);
  4211. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4212. $expected = [
  4213. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4214. ['label' => ['for' => 'model-field-0']],
  4215. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4216. 'option A',
  4217. '/label',
  4218. ['label' => ['for' => 'model-field-1']],
  4219. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4220. 'option B',
  4221. '/label',
  4222. ];
  4223. $this->assertHtml($expected, $result);
  4224. $result = $this->Form->radio(
  4225. 'Employee.gender',
  4226. ['male' => 'Male', 'female' => 'Female'],
  4227. ['form' => 'my-form']
  4228. );
  4229. $expected = [
  4230. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4231. ['label' => ['for' => 'employee-gender-male']],
  4232. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4233. 'Male',
  4234. '/label',
  4235. ['label' => ['for' => 'employee-gender-female']],
  4236. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4237. 'Female',
  4238. '/label',
  4239. ];
  4240. $this->assertHtml($expected, $result);
  4241. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4242. $expected = [
  4243. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4244. ['label' => ['for' => 'model-custom-0']],
  4245. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4246. 'option A',
  4247. '/label',
  4248. ['label' => ['for' => 'model-custom-1']],
  4249. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4250. 'option B',
  4251. '/label',
  4252. ];
  4253. $this->assertHtml($expected, $result);
  4254. $result = $this->Form->radio(
  4255. 'Employee.gender',
  4256. [
  4257. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4258. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4259. ]
  4260. );
  4261. $expected = [
  4262. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4263. ['label' => ['for' => 'employee-gender-male']],
  4264. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4265. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4266. 'Male',
  4267. '/label',
  4268. ['label' => ['for' => 'employee-gender-female']],
  4269. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4270. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4271. 'Female',
  4272. '/label',
  4273. ];
  4274. $this->assertHtml($expected, $result);
  4275. }
  4276. /**
  4277. * Test radio with complex options and empty disabled data.
  4278. *
  4279. * @return void
  4280. */
  4281. public function testRadioComplexDisabled()
  4282. {
  4283. $options = [
  4284. ['value' => 'r', 'text' => 'red'],
  4285. ['value' => 'b', 'text' => 'blue'],
  4286. ];
  4287. $attrs = ['disabled' => []];
  4288. $result = $this->Form->radio('Model.field', $options, $attrs);
  4289. $expected = [
  4290. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4291. ['label' => ['for' => 'model-field-r']],
  4292. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4293. 'red',
  4294. '/label',
  4295. ['label' => ['for' => 'model-field-b']],
  4296. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4297. 'blue',
  4298. '/label',
  4299. ];
  4300. $this->assertHtml($expected, $result);
  4301. $attrs = ['disabled' => ['r']];
  4302. $result = $this->Form->radio('Model.field', $options, $attrs);
  4303. $this->assertStringContainsString('disabled="disabled"', $result);
  4304. }
  4305. /**
  4306. * testRadioDefaultValue method
  4307. *
  4308. * Test default value setting on radio() method.
  4309. *
  4310. * @return void
  4311. */
  4312. public function testRadioDefaultValue()
  4313. {
  4314. $Articles = $this->getTableLocator()->get('Articles');
  4315. $title = $Articles->getSchema()->getColumn('title');
  4316. $Articles->getSchema()->addColumn(
  4317. 'title',
  4318. ['default' => '1'] + $title
  4319. );
  4320. $this->Form->create($Articles->newEmptyEntity());
  4321. $result = $this->Form->radio('title', ['option A', 'option B']);
  4322. $expected = [
  4323. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4324. ['label' => ['for' => 'title-0']],
  4325. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4326. 'option A',
  4327. '/label',
  4328. ['label' => ['for' => 'title-1']],
  4329. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4330. 'option B',
  4331. '/label',
  4332. ];
  4333. $this->assertHtml($expected, $result);
  4334. }
  4335. /**
  4336. * Test setting a hiddenField value on radio buttons.
  4337. *
  4338. * @return void
  4339. */
  4340. public function testRadioHiddenFieldValue()
  4341. {
  4342. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4343. $expected = [
  4344. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4345. 'label' => ['for' => 'title-0'],
  4346. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4347. 'option A',
  4348. '/label',
  4349. ];
  4350. $this->assertHtml($expected, $result);
  4351. }
  4352. /**
  4353. * testControlRadio method
  4354. *
  4355. * Test that input works with radio types.
  4356. *
  4357. * @return void
  4358. */
  4359. public function testControlRadio()
  4360. {
  4361. $result = $this->Form->control('test', [
  4362. 'type' => 'radio',
  4363. 'options' => ['A', 'B'],
  4364. ]);
  4365. $expected = [
  4366. ['div' => ['class' => 'input radio']],
  4367. '<label',
  4368. 'Test',
  4369. '/label',
  4370. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4371. ['label' => ['for' => 'test-0']],
  4372. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4373. 'A',
  4374. '/label',
  4375. ['label' => ['for' => 'test-1']],
  4376. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4377. 'B',
  4378. '/label',
  4379. '/div',
  4380. ];
  4381. $this->assertHtml($expected, $result);
  4382. $result = $this->Form->control('test', [
  4383. 'type' => 'radio',
  4384. 'options' => ['A', 'B'],
  4385. 'value' => '0',
  4386. ]);
  4387. $expected = [
  4388. ['div' => ['class' => 'input radio']],
  4389. '<label',
  4390. 'Test',
  4391. '/label',
  4392. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4393. ['label' => ['for' => 'test-0']],
  4394. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4395. 'A',
  4396. '/label',
  4397. ['label' => ['for' => 'test-1']],
  4398. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4399. 'B',
  4400. '/label',
  4401. '/div',
  4402. ];
  4403. $this->assertHtml($expected, $result);
  4404. $result = $this->Form->control('test', [
  4405. 'type' => 'radio',
  4406. 'options' => ['A', 'B'],
  4407. 'label' => false,
  4408. ]);
  4409. $expected = [
  4410. ['div' => ['class' => 'input radio']],
  4411. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4412. ['label' => ['for' => 'test-0']],
  4413. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4414. 'A',
  4415. '/label',
  4416. ['label' => ['for' => 'test-1']],
  4417. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4418. 'B',
  4419. '/label',
  4420. '/div',
  4421. ];
  4422. $this->assertHtml($expected, $result);
  4423. }
  4424. /**
  4425. * testRadioNoLabel method
  4426. *
  4427. * Test that radio() works with label = false.
  4428. *
  4429. * @return void
  4430. */
  4431. public function testRadioNoLabel()
  4432. {
  4433. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4434. $expected = [
  4435. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4436. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4437. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4438. ];
  4439. $this->assertHtml($expected, $result);
  4440. }
  4441. /**
  4442. * testRadioControlInsideLabel method
  4443. *
  4444. * Test generating radio input inside label ala twitter bootstrap.
  4445. *
  4446. * @return void
  4447. */
  4448. public function testRadioControlInsideLabel()
  4449. {
  4450. $this->Form->setTemplates([
  4451. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4452. 'radioWrapper' => '{{label}}',
  4453. ]);
  4454. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4455. // phpcs:disable
  4456. $expected = [
  4457. ['input' => [
  4458. 'type' => 'hidden',
  4459. 'name' => 'Model[field]',
  4460. 'value' => ''
  4461. ]],
  4462. ['label' => ['for' => 'model-field-0']],
  4463. ['input' => [
  4464. 'type' => 'radio',
  4465. 'name' => 'Model[field]',
  4466. 'value' => '0',
  4467. 'id' => 'model-field-0'
  4468. ]],
  4469. 'option A',
  4470. '/label',
  4471. ['label' => ['for' => 'model-field-1']],
  4472. ['input' => [
  4473. 'type' => 'radio',
  4474. 'name' => 'Model[field]',
  4475. 'value' => '1',
  4476. 'id' => 'model-field-1'
  4477. ]],
  4478. 'option B',
  4479. '/label',
  4480. ];
  4481. // phpcs:enable
  4482. $this->assertHtml($expected, $result);
  4483. }
  4484. /**
  4485. * testRadioHiddenControlDisabling method
  4486. *
  4487. * Test disabling the hidden input for radio buttons.
  4488. *
  4489. * @return void
  4490. */
  4491. public function testRadioHiddenControlDisabling()
  4492. {
  4493. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4494. $expected = [
  4495. 'label' => ['for' => 'model-1-field-0'],
  4496. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4497. 'option A',
  4498. '/label',
  4499. ];
  4500. $this->assertHtml($expected, $result);
  4501. }
  4502. /**
  4503. * testRadioOutOfRange method
  4504. *
  4505. * Test radio element set generation.
  4506. *
  4507. * @return void
  4508. */
  4509. public function testRadioOutOfRange()
  4510. {
  4511. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4512. $expected = [
  4513. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4514. 'label' => ['for' => 'model-field-v'],
  4515. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4516. 'value',
  4517. '/label',
  4518. ];
  4519. $this->assertHtml($expected, $result);
  4520. }
  4521. /**
  4522. * testSelect method
  4523. *
  4524. * Test select element generation.
  4525. *
  4526. * @return void
  4527. */
  4528. public function testSelect()
  4529. {
  4530. $result = $this->Form->select('Model.field', []);
  4531. $expected = [
  4532. 'select' => ['name' => 'Model[field]'],
  4533. '/select',
  4534. ];
  4535. $this->assertHtml($expected, $result);
  4536. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4537. $this->Form->create();
  4538. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4539. $expected = [
  4540. 'select' => ['name' => 'Model[field]'],
  4541. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4542. 'good',
  4543. '/option',
  4544. ['option' => ['value' => 'other']],
  4545. 'bad',
  4546. '/option',
  4547. '/select',
  4548. ];
  4549. $this->assertHtml($expected, $result);
  4550. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4551. $this->assertHtml($expected, $result);
  4552. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4553. $this->Form->create();
  4554. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4555. $expected = [
  4556. 'select' => ['name' => 'Model[field]'],
  4557. ['option' => ['value' => 'value']],
  4558. 'good',
  4559. '/option',
  4560. ['option' => ['value' => 'other']],
  4561. 'bad',
  4562. '/option',
  4563. '/select',
  4564. ];
  4565. $this->assertHtml($expected, $result);
  4566. $options = [
  4567. ['value' => 'first', 'text' => 'First'],
  4568. ['value' => 'first', 'text' => 'Another First'],
  4569. ];
  4570. $result = $this->Form->select(
  4571. 'Model.field',
  4572. $options,
  4573. ['escape' => false, 'empty' => false]
  4574. );
  4575. $expected = [
  4576. 'select' => ['name' => 'Model[field]'],
  4577. ['option' => ['value' => 'first']],
  4578. 'First',
  4579. '/option',
  4580. ['option' => ['value' => 'first']],
  4581. 'Another First',
  4582. '/option',
  4583. '/select',
  4584. ];
  4585. $this->assertHtml($expected, $result);
  4586. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4587. $this->Form->create();
  4588. $result = $this->Form->select(
  4589. 'Model.contact_id',
  4590. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4591. ['escape' => false, 'empty' => 'pick something']
  4592. );
  4593. $expected = [
  4594. 'select' => ['name' => 'Model[contact_id]'],
  4595. ['option' => ['value' => '']], 'pick something', '/option',
  4596. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4597. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4598. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4599. '/select',
  4600. ];
  4601. $this->assertHtml($expected, $result);
  4602. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4603. $this->Form->create();
  4604. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4605. $expected = [
  4606. 'select' => ['name' => 'Model[field]'],
  4607. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4608. ['option' => ['value' => '1']], 'Yes', '/option',
  4609. '/select',
  4610. ];
  4611. $this->assertHtml($expected, $result);
  4612. }
  4613. /**
  4614. * testSelectEscapeHtml method
  4615. *
  4616. * Test that select() escapes HTML.
  4617. *
  4618. * @return void
  4619. */
  4620. public function testSelectEscapeHtml()
  4621. {
  4622. $result = $this->Form->select(
  4623. 'Model.field',
  4624. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4625. ['empty' => false]
  4626. );
  4627. $expected = [
  4628. 'select' => ['name' => 'Model[field]'],
  4629. ['option' => ['value' => 'first']],
  4630. 'first &quot;html&quot; &lt;chars&gt;',
  4631. '/option',
  4632. ['option' => ['value' => 'second']],
  4633. 'value',
  4634. '/option',
  4635. '/select',
  4636. ];
  4637. $this->assertHtml($expected, $result);
  4638. $result = $this->Form->select(
  4639. 'Model.field',
  4640. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4641. ['escape' => false, 'empty' => false]
  4642. );
  4643. $expected = [
  4644. 'select' => ['name' => 'Model[field]'],
  4645. ['option' => ['value' => 'first']],
  4646. 'first "html" <chars>',
  4647. '/option',
  4648. ['option' => ['value' => 'second']],
  4649. 'value',
  4650. '/option',
  4651. '/select',
  4652. ];
  4653. $this->assertHtml($expected, $result);
  4654. }
  4655. /**
  4656. * testSelectRequired method
  4657. *
  4658. * Test select() with required and disabled attributes.
  4659. *
  4660. * @return void
  4661. */
  4662. public function testSelectRequired()
  4663. {
  4664. $this->article['required'] = [
  4665. 'user_id' => true,
  4666. ];
  4667. $this->Form->create($this->article);
  4668. $result = $this->Form->select('user_id', ['option A']);
  4669. $expected = [
  4670. 'select' => [
  4671. 'name' => 'user_id',
  4672. 'required' => 'required',
  4673. ],
  4674. ['option' => ['value' => '0']], 'option A', '/option',
  4675. '/select',
  4676. ];
  4677. $this->assertHtml($expected, $result);
  4678. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4679. $expected = [
  4680. 'select' => [
  4681. 'name' => 'user_id',
  4682. 'disabled' => 'disabled',
  4683. ],
  4684. ['option' => ['value' => '0']], 'option A', '/option',
  4685. '/select',
  4686. ];
  4687. $this->assertHtml($expected, $result);
  4688. }
  4689. /**
  4690. * testNestedSelect method
  4691. *
  4692. * Test select element generation with optgroups.
  4693. *
  4694. * @return void
  4695. */
  4696. public function testNestedSelect()
  4697. {
  4698. $result = $this->Form->select(
  4699. 'Model.field',
  4700. [1 => 'One', 2 => 'Two', 'Three' => [
  4701. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4702. ]],
  4703. ['empty' => false]
  4704. );
  4705. $expected = [
  4706. 'select' => ['name' => 'Model[field]'],
  4707. ['option' => ['value' => 1]],
  4708. 'One',
  4709. '/option',
  4710. ['option' => ['value' => 2]],
  4711. 'Two',
  4712. '/option',
  4713. ['optgroup' => ['label' => 'Three']],
  4714. ['option' => ['value' => 3]],
  4715. 'Three',
  4716. '/option',
  4717. ['option' => ['value' => 4]],
  4718. 'Four',
  4719. '/option',
  4720. ['option' => ['value' => 5]],
  4721. 'Five',
  4722. '/option',
  4723. '/optgroup',
  4724. '/select',
  4725. ];
  4726. $this->assertHtml($expected, $result);
  4727. }
  4728. /**
  4729. * testSelectMultiple method
  4730. *
  4731. * Test generation of multiple select elements.
  4732. *
  4733. * @return void
  4734. */
  4735. public function testSelectMultiple()
  4736. {
  4737. $options = ['first', 'second', 'third'];
  4738. $result = $this->Form->select(
  4739. 'Model.multi_field',
  4740. $options,
  4741. ['form' => 'my-form', 'multiple' => true]
  4742. );
  4743. $expected = [
  4744. 'input' => [
  4745. 'type' => 'hidden',
  4746. 'name' => 'Model[multi_field]',
  4747. 'value' => '',
  4748. 'form' => 'my-form',
  4749. ],
  4750. 'select' => [
  4751. 'name' => 'Model[multi_field][]',
  4752. 'multiple' => 'multiple',
  4753. 'form' => 'my-form',
  4754. ],
  4755. ['option' => ['value' => '0']],
  4756. 'first',
  4757. '/option',
  4758. ['option' => ['value' => '1']],
  4759. 'second',
  4760. '/option',
  4761. ['option' => ['value' => '2']],
  4762. 'third',
  4763. '/option',
  4764. '/select',
  4765. ];
  4766. $this->assertHtml($expected, $result);
  4767. $result = $this->Form->select(
  4768. 'Model.multi_field',
  4769. $options,
  4770. ['multiple' => 'multiple', 'form' => 'my-form']
  4771. );
  4772. $this->assertHtml($expected, $result);
  4773. $result = $this->Form->select(
  4774. 'Model.multi_field',
  4775. $options,
  4776. ['form' => 'my-form', 'multiple' => false]
  4777. );
  4778. $this->assertStringNotContainsString('multiple', $result);
  4779. }
  4780. /**
  4781. * testCheckboxZeroValue method
  4782. *
  4783. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4784. *
  4785. * @return void
  4786. */
  4787. public function testCheckboxZeroValue()
  4788. {
  4789. $result = $this->Form->control('User.get_spam', [
  4790. 'type' => 'checkbox',
  4791. 'value' => '0',
  4792. 'hiddenField' => '1',
  4793. ]);
  4794. $expected = [
  4795. 'div' => ['class' => 'input checkbox'],
  4796. 'label' => ['for' => 'user-get-spam'],
  4797. ['input' => [
  4798. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4799. 'value' => '1',
  4800. ]],
  4801. ['input' => [
  4802. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4803. 'value' => '0', 'id' => 'user-get-spam',
  4804. ]],
  4805. 'Get Spam',
  4806. '/label',
  4807. '/div',
  4808. ];
  4809. $this->assertHtml($expected, $result);
  4810. }
  4811. /**
  4812. * testHabtmSelectBox method
  4813. *
  4814. * Test generation of habtm select boxes.
  4815. *
  4816. * @return void
  4817. */
  4818. public function testHabtmSelectBox()
  4819. {
  4820. $this->loadFixtures('Articles');
  4821. $options = [
  4822. 1 => 'blue',
  4823. 2 => 'red',
  4824. 3 => 'green',
  4825. ];
  4826. $tags = [
  4827. new Entity(['id' => 1, 'name' => 'blue']),
  4828. new Entity(['id' => 3, 'name' => 'green']),
  4829. ];
  4830. $article = new Article(['tags' => $tags]);
  4831. $this->Form->create($article);
  4832. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4833. $expected = [
  4834. 'div' => ['class' => 'input select'],
  4835. 'label' => ['for' => 'tags-ids'],
  4836. 'Tags',
  4837. '/label',
  4838. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4839. 'select' => [
  4840. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4841. 'multiple' => 'multiple',
  4842. ],
  4843. ['option' => ['value' => '1', 'selected' => 'selected']],
  4844. 'blue',
  4845. '/option',
  4846. ['option' => ['value' => '2']],
  4847. 'red',
  4848. '/option',
  4849. ['option' => ['value' => '3', 'selected' => 'selected']],
  4850. 'green',
  4851. '/option',
  4852. '/select',
  4853. '/div',
  4854. ];
  4855. $this->assertHtml($expected, $result);
  4856. // make sure only 50 is selected, and not 50f5c0cf
  4857. $options = [
  4858. '1' => 'blue',
  4859. '50f5c0cf' => 'red',
  4860. '50' => 'green',
  4861. ];
  4862. $tags = [
  4863. new Entity(['id' => 1, 'name' => 'blue']),
  4864. new Entity(['id' => 50, 'name' => 'green']),
  4865. ];
  4866. $article = new Article(['tags' => $tags]);
  4867. $this->Form->create($article);
  4868. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4869. $expected = [
  4870. 'div' => ['class' => 'input select'],
  4871. 'label' => ['for' => 'tags-ids'],
  4872. 'Tags',
  4873. '/label',
  4874. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4875. 'select' => [
  4876. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4877. 'multiple' => 'multiple',
  4878. ],
  4879. ['option' => ['value' => '1', 'selected' => 'selected']],
  4880. 'blue',
  4881. '/option',
  4882. ['option' => ['value' => '50f5c0cf']],
  4883. 'red',
  4884. '/option',
  4885. ['option' => ['value' => '50', 'selected' => 'selected']],
  4886. 'green',
  4887. '/option',
  4888. '/select',
  4889. '/div',
  4890. ];
  4891. $this->assertHtml($expected, $result);
  4892. $spacecraft = [
  4893. 1 => 'Orion',
  4894. 2 => 'Helios',
  4895. ];
  4896. $this->View->set('spacecraft', $spacecraft);
  4897. $this->Form->create();
  4898. $result = $this->Form->control('spacecraft._ids');
  4899. $expected = [
  4900. 'div' => ['class' => 'input select'],
  4901. 'label' => ['for' => 'spacecraft-ids'],
  4902. 'Spacecraft',
  4903. '/label',
  4904. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4905. 'select' => [
  4906. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4907. 'multiple' => 'multiple',
  4908. ],
  4909. ['option' => ['value' => '1']],
  4910. 'Orion',
  4911. '/option',
  4912. ['option' => ['value' => '2']],
  4913. 'Helios',
  4914. '/option',
  4915. '/select',
  4916. '/div',
  4917. ];
  4918. $this->assertHtml($expected, $result);
  4919. }
  4920. /**
  4921. * testErrorsForBelongsToManySelect method
  4922. *
  4923. * Tests that errors for belongsToMany select fields are being
  4924. * picked up properly.
  4925. *
  4926. * @return void
  4927. */
  4928. public function testErrorsForBelongsToManySelect()
  4929. {
  4930. $spacecraft = [
  4931. 1 => 'Orion',
  4932. 2 => 'Helios',
  4933. ];
  4934. $this->View->set('spacecraft', $spacecraft);
  4935. $article = new Article();
  4936. $article->setError('spacecraft', ['Invalid']);
  4937. $this->Form->create($article);
  4938. $result = $this->Form->control('spacecraft._ids');
  4939. $expected = [
  4940. ['div' => ['class' => 'input select error']],
  4941. 'label' => ['for' => 'spacecraft-ids'],
  4942. 'Spacecraft',
  4943. '/label',
  4944. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4945. 'select' => [
  4946. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4947. 'multiple' => 'multiple',
  4948. ],
  4949. ['option' => ['value' => '1']],
  4950. 'Orion',
  4951. '/option',
  4952. ['option' => ['value' => '2']],
  4953. 'Helios',
  4954. '/option',
  4955. '/select',
  4956. ['div' => ['class' => 'error-message']],
  4957. 'Invalid',
  4958. '/div',
  4959. '/div',
  4960. ];
  4961. $this->assertHtml($expected, $result);
  4962. }
  4963. /**
  4964. * testSelectMultipleCheckboxes method
  4965. *
  4966. * Test generation of multi select elements in checkbox format.
  4967. *
  4968. * @return void
  4969. */
  4970. public function testSelectMultipleCheckboxes()
  4971. {
  4972. $result = $this->Form->select(
  4973. 'Model.multi_field',
  4974. ['first', 'second', 'third'],
  4975. ['multiple' => 'checkbox']
  4976. );
  4977. $expected = [
  4978. 'input' => [
  4979. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4980. ],
  4981. ['div' => ['class' => 'checkbox']],
  4982. ['label' => ['for' => 'model-multi-field-0']],
  4983. ['input' => [
  4984. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4985. 'value' => '0', 'id' => 'model-multi-field-0',
  4986. ]],
  4987. 'first',
  4988. '/label',
  4989. '/div',
  4990. ['div' => ['class' => 'checkbox']],
  4991. ['label' => ['for' => 'model-multi-field-1']],
  4992. ['input' => [
  4993. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4994. 'value' => '1', 'id' => 'model-multi-field-1',
  4995. ]],
  4996. 'second',
  4997. '/label',
  4998. '/div',
  4999. ['div' => ['class' => 'checkbox']],
  5000. ['label' => ['for' => 'model-multi-field-2']],
  5001. ['input' => [
  5002. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5003. 'value' => '2', 'id' => 'model-multi-field-2',
  5004. ]],
  5005. 'third',
  5006. '/label',
  5007. '/div',
  5008. ];
  5009. $this->assertHtml($expected, $result);
  5010. $result = $this->Form->select(
  5011. 'Model.multi_field',
  5012. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5013. ['multiple' => 'checkbox']
  5014. );
  5015. $expected = [
  5016. 'input' => [
  5017. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5018. ],
  5019. ['div' => ['class' => 'checkbox']],
  5020. ['label' => ['for' => 'model-multi-field-a+']],
  5021. ['input' => [
  5022. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5023. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5024. ]],
  5025. 'first',
  5026. '/label',
  5027. '/div',
  5028. ['div' => ['class' => 'checkbox']],
  5029. ['label' => ['for' => 'model-multi-field-a++']],
  5030. ['input' => [
  5031. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5032. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5033. ]],
  5034. 'second',
  5035. '/label',
  5036. '/div',
  5037. ['div' => ['class' => 'checkbox']],
  5038. ['label' => ['for' => 'model-multi-field-a+++']],
  5039. ['input' => [
  5040. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5041. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5042. ]],
  5043. 'third',
  5044. '/label',
  5045. '/div',
  5046. ];
  5047. $this->assertHtml($expected, $result);
  5048. $result = $this->Form->select(
  5049. 'Model.multi_field',
  5050. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5051. ['multiple' => 'checkbox']
  5052. );
  5053. $expected = [
  5054. 'input' => [
  5055. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5056. ],
  5057. ['div' => ['class' => 'checkbox']],
  5058. ['label' => ['for' => 'model-multi-field-a-b']],
  5059. ['input' => [
  5060. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5061. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5062. ]],
  5063. 'first',
  5064. '/label',
  5065. '/div',
  5066. ['div' => ['class' => 'checkbox']],
  5067. ['label' => ['for' => 'model-multi-field-a-b1']],
  5068. ['input' => [
  5069. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5070. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5071. ]],
  5072. 'second',
  5073. '/label',
  5074. '/div',
  5075. ['div' => ['class' => 'checkbox']],
  5076. ['label' => ['for' => 'model-multi-field-a-b2']],
  5077. ['input' => [
  5078. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5079. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5080. ]],
  5081. 'third',
  5082. '/label',
  5083. '/div',
  5084. ];
  5085. $this->assertHtml($expected, $result);
  5086. }
  5087. /**
  5088. * testSelectMultipleCheckboxRequestData method
  5089. *
  5090. * Ensure that multiCheckbox reads from the request data.
  5091. *
  5092. * @return void
  5093. */
  5094. public function testSelectMultipleCheckboxRequestData()
  5095. {
  5096. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5097. $result = $this->Form->select(
  5098. 'Model.tags',
  5099. ['1' => 'first', 'Array' => 'Array'],
  5100. ['multiple' => 'checkbox']
  5101. );
  5102. $expected = [
  5103. 'input' => [
  5104. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5105. ],
  5106. ['div' => ['class' => 'checkbox']],
  5107. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5108. ['input' => [
  5109. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5110. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5111. ]],
  5112. 'first',
  5113. '/label',
  5114. '/div',
  5115. ['div' => ['class' => 'checkbox']],
  5116. ['label' => ['for' => 'model-tags-array']],
  5117. ['input' => [
  5118. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5119. 'value' => 'Array', 'id' => 'model-tags-array',
  5120. ]],
  5121. 'Array',
  5122. '/label',
  5123. '/div',
  5124. ];
  5125. $this->assertHtml($expected, $result);
  5126. }
  5127. /**
  5128. * testSelectMultipleCheckboxSecurity method
  5129. *
  5130. * Checks the security hash array generated for multiple-input checkbox elements.
  5131. *
  5132. * @return void
  5133. */
  5134. public function testSelectMultipleCheckboxSecurity()
  5135. {
  5136. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testKey'));
  5137. $this->assertEquals([], $this->Form->fields);
  5138. $this->Form->select(
  5139. 'Model.multi_field',
  5140. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5141. ['multiple' => 'checkbox']
  5142. );
  5143. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5144. $result = $this->Form->secure($this->Form->fields);
  5145. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5146. $this->assertStringContainsString('"' . $key . '"', $result);
  5147. }
  5148. /**
  5149. * testSelectMultipleSecureWithNoOptions method
  5150. *
  5151. * Multiple select elements should always be secured as they always participate
  5152. * in the POST data.
  5153. *
  5154. * @return void
  5155. */
  5156. public function testSelectMultipleSecureWithNoOptions()
  5157. {
  5158. $this->assertEquals([], $this->Form->fields);
  5159. $this->Form->select(
  5160. 'Model.select',
  5161. [],
  5162. ['multiple' => true]
  5163. );
  5164. $this->assertEquals(['Model.select'], $this->Form->fields);
  5165. }
  5166. /**
  5167. * testSelectNoSecureWithNoOptions method
  5168. *
  5169. * When a select box has no options it should not be added to the fields list
  5170. * as it always fail post validation.
  5171. *
  5172. * @return void
  5173. */
  5174. public function testSelectNoSecureWithNoOptions()
  5175. {
  5176. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'testkey'));
  5177. $this->assertEquals([], $this->Form->fields);
  5178. $this->Form->select(
  5179. 'Model.select',
  5180. []
  5181. );
  5182. $this->assertEquals([], $this->Form->fields);
  5183. $this->Form->select(
  5184. 'Model.user_id',
  5185. [],
  5186. ['empty' => true]
  5187. );
  5188. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5189. }
  5190. /**
  5191. * testControlMultipleCheckboxes method
  5192. *
  5193. * Test control() resulting in multi select elements being generated.
  5194. *
  5195. * @return void
  5196. */
  5197. public function testControlMultipleCheckboxes()
  5198. {
  5199. $result = $this->Form->control('Model.multi_field', [
  5200. 'options' => ['first', 'second', 'third'],
  5201. 'multiple' => 'checkbox',
  5202. ]);
  5203. $expected = [
  5204. ['div' => ['class' => 'input select']],
  5205. ['label' => ['for' => 'model-multi-field']],
  5206. 'Multi Field',
  5207. '/label',
  5208. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5209. ['div' => ['class' => 'checkbox']],
  5210. ['label' => ['for' => 'model-multi-field-0']],
  5211. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5212. 'first',
  5213. '/label',
  5214. '/div',
  5215. ['div' => ['class' => 'checkbox']],
  5216. ['label' => ['for' => 'model-multi-field-1']],
  5217. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5218. 'second',
  5219. '/label',
  5220. '/div',
  5221. ['div' => ['class' => 'checkbox']],
  5222. ['label' => ['for' => 'model-multi-field-2']],
  5223. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5224. 'third',
  5225. '/label',
  5226. '/div',
  5227. '/div',
  5228. ];
  5229. $this->assertHtml($expected, $result);
  5230. $result = $this->Form->control('Model.multi_field', [
  5231. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5232. 'multiple' => 'checkbox',
  5233. ]);
  5234. $expected = [
  5235. ['div' => ['class' => 'input select']],
  5236. ['label' => ['for' => 'model-multi-field']],
  5237. 'Multi Field',
  5238. '/label',
  5239. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5240. ['div' => ['class' => 'checkbox']],
  5241. ['label' => ['for' => 'model-multi-field-a']],
  5242. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5243. 'first',
  5244. '/label',
  5245. '/div',
  5246. ['div' => ['class' => 'checkbox']],
  5247. ['label' => ['for' => 'model-multi-field-b']],
  5248. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5249. 'second',
  5250. '/label',
  5251. '/div',
  5252. ['div' => ['class' => 'checkbox']],
  5253. ['label' => ['for' => 'model-multi-field-c']],
  5254. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5255. 'third',
  5256. '/label',
  5257. '/div',
  5258. '/div',
  5259. ];
  5260. $this->assertHtml($expected, $result);
  5261. }
  5262. /**
  5263. * testSelectHiddenFieldOmission method
  5264. *
  5265. * Test that select() with 'hiddenField' => false omits the hidden field.
  5266. *
  5267. * @return void
  5268. */
  5269. public function testSelectHiddenFieldOmission()
  5270. {
  5271. $result = $this->Form->select(
  5272. 'Model.multi_field',
  5273. ['first', 'second'],
  5274. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5275. );
  5276. $this->assertStringNotContainsString('type="hidden"', $result);
  5277. }
  5278. /**
  5279. * testSelectCheckboxMultipleOverrideName method
  5280. *
  5281. * Test that select() with multiple = checkbox works with overriding name attribute.
  5282. *
  5283. * @return void
  5284. */
  5285. public function testSelectCheckboxMultipleOverrideName()
  5286. {
  5287. $result = $this->Form->select('category', ['1', '2'], [
  5288. 'multiple' => 'checkbox',
  5289. 'name' => 'fish',
  5290. ]);
  5291. $expected = [
  5292. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5293. ['div' => ['class' => 'checkbox']],
  5294. ['label' => ['for' => 'fish-0']],
  5295. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5296. '1',
  5297. '/label',
  5298. '/div',
  5299. ['div' => ['class' => 'checkbox']],
  5300. ['label' => ['for' => 'fish-1']],
  5301. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5302. '2',
  5303. '/label',
  5304. '/div',
  5305. ];
  5306. $this->assertHtml($expected, $result);
  5307. $result = $this->Form->multiCheckbox(
  5308. 'category',
  5309. new Collection(['1', '2']),
  5310. ['name' => 'fish']
  5311. );
  5312. $this->assertHtml($expected, $result);
  5313. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5314. 'name' => 'fish',
  5315. ]);
  5316. $this->assertHtml($expected, $result);
  5317. }
  5318. /**
  5319. * testControlMultiCheckbox method
  5320. *
  5321. * Test that control() works with multicheckbox.
  5322. *
  5323. * @return void
  5324. */
  5325. public function testControlMultiCheckbox()
  5326. {
  5327. $result = $this->Form->control('category', [
  5328. 'type' => 'multicheckbox',
  5329. 'options' => ['1', '2'],
  5330. ]);
  5331. $expected = [
  5332. ['div' => ['class' => 'input multicheckbox']],
  5333. '<label',
  5334. 'Category',
  5335. '/label',
  5336. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5337. ['div' => ['class' => 'checkbox']],
  5338. ['label' => ['for' => 'category-0']],
  5339. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5340. '1',
  5341. '/label',
  5342. '/div',
  5343. ['div' => ['class' => 'checkbox']],
  5344. ['label' => ['for' => 'category-1']],
  5345. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5346. '2',
  5347. '/label',
  5348. '/div',
  5349. '/div',
  5350. ];
  5351. $this->assertHtml($expected, $result);
  5352. }
  5353. /**
  5354. * testCheckbox method
  5355. *
  5356. * Test generation of checkboxes.
  5357. *
  5358. * @return void
  5359. */
  5360. public function testCheckbox()
  5361. {
  5362. $result = $this->Form->checkbox('Model.field');
  5363. $expected = [
  5364. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5365. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5366. ];
  5367. $this->assertHtml($expected, $result);
  5368. $result = $this->Form->checkbox('Model.field', [
  5369. 'id' => 'theID',
  5370. 'value' => 'myvalue',
  5371. 'form' => 'my-form',
  5372. ]);
  5373. $expected = [
  5374. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5375. ['input' => [
  5376. 'type' => 'checkbox', 'name' => 'Model[field]',
  5377. 'value' => 'myvalue', 'id' => 'theID',
  5378. 'form' => 'my-form',
  5379. ]],
  5380. ];
  5381. $this->assertHtml($expected, $result);
  5382. }
  5383. /**
  5384. * testCheckboxDefaultValue method
  5385. *
  5386. * Test default value setting on checkbox() method.
  5387. *
  5388. * @return void
  5389. */
  5390. public function testCheckboxDefaultValue()
  5391. {
  5392. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5393. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5394. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5395. $this->assertHtml($expected, $result);
  5396. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5397. $this->Form->create();
  5398. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5399. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5400. $this->assertHtml($expected, $result);
  5401. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5402. $this->Form->create();
  5403. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5404. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5405. $this->assertHtml($expected, $result);
  5406. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5407. $this->Form->create();
  5408. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5409. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5410. $this->assertHtml($expected, $result);
  5411. $Articles = $this->getTableLocator()->get('Articles');
  5412. $Articles->getSchema()->addColumn(
  5413. 'published',
  5414. ['type' => 'boolean', 'null' => false, 'default' => true]
  5415. );
  5416. $this->Form->create($Articles->newEmptyEntity());
  5417. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5418. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5419. $this->assertHtml($expected, $result);
  5420. }
  5421. /**
  5422. * testCheckboxCheckedAndError method
  5423. *
  5424. * Test checkbox being checked or having errors.
  5425. *
  5426. * @return void
  5427. */
  5428. public function testCheckboxCheckedAndError()
  5429. {
  5430. $this->article['errors'] = [
  5431. 'published' => true,
  5432. ];
  5433. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5434. $this->Form->create($this->article);
  5435. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5436. $expected = [
  5437. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5438. ['input' => [
  5439. 'type' => 'checkbox',
  5440. 'name' => 'published',
  5441. 'value' => 'myvalue',
  5442. 'id' => 'theID',
  5443. 'checked' => 'checked',
  5444. 'class' => 'form-error',
  5445. ]],
  5446. ];
  5447. $this->assertHtml($expected, $result);
  5448. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5449. $this->Form->create($this->article);
  5450. $result = $this->Form->checkbox('published');
  5451. $expected = [
  5452. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5453. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5454. ];
  5455. $this->assertHtml($expected, $result);
  5456. }
  5457. /**
  5458. * testCheckboxCustomNameAttribute method
  5459. *
  5460. * Test checkbox() with a custom name attribute.
  5461. *
  5462. * @return void
  5463. */
  5464. public function testCheckboxCustomNameAttribute()
  5465. {
  5466. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5467. $expected = [
  5468. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5469. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5470. ];
  5471. $this->assertHtml($expected, $result);
  5472. }
  5473. /**
  5474. * testCheckboxHiddenField method
  5475. *
  5476. * Test that the hidden input for checkboxes can be omitted or set to a
  5477. * specific value.
  5478. *
  5479. * @return void
  5480. */
  5481. public function testCheckboxHiddenField()
  5482. {
  5483. $result = $this->Form->checkbox('UserForm.something', [
  5484. 'hiddenField' => false,
  5485. ]);
  5486. $expected = [
  5487. 'input' => [
  5488. 'type' => 'checkbox',
  5489. 'name' => 'UserForm[something]',
  5490. 'value' => '1',
  5491. ],
  5492. ];
  5493. $this->assertHtml($expected, $result);
  5494. $result = $this->Form->checkbox('UserForm.something', [
  5495. 'value' => 'Y',
  5496. 'hiddenField' => 'N',
  5497. ]);
  5498. $expected = [
  5499. ['input' => [
  5500. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5501. 'value' => 'N',
  5502. ]],
  5503. ['input' => [
  5504. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5505. 'value' => 'Y',
  5506. ]],
  5507. ];
  5508. $this->assertHtml($expected, $result);
  5509. }
  5510. /**
  5511. * testTime method
  5512. *
  5513. * Test the time type.
  5514. *
  5515. * @return void
  5516. */
  5517. public function testTime()
  5518. {
  5519. $result = $this->Form->time('start_time', [
  5520. 'value' => '2014-03-08 16:30:00',
  5521. ]);
  5522. $expected = [
  5523. 'input' => [
  5524. 'type' => 'time',
  5525. 'name' => 'start_time',
  5526. 'value' => '16:30:00',
  5527. ],
  5528. ];
  5529. $this->assertHtml($expected, $result);
  5530. }
  5531. /**
  5532. * testDate method
  5533. *
  5534. * Test the date type.
  5535. *
  5536. * @return void
  5537. */
  5538. public function testDate()
  5539. {
  5540. $result = $this->Form->date('start_day', [
  5541. 'value' => '2014-03-08',
  5542. ]);
  5543. $expected = [
  5544. 'input' => [
  5545. 'type' => 'date',
  5546. 'name' => 'start_day',
  5547. 'value' => '2014-03-08',
  5548. ],
  5549. ];
  5550. $this->assertHtml($expected, $result);
  5551. $result = $this->Form->date('start_day', [
  5552. 'value' => new Date('2014-03-08'),
  5553. ]);
  5554. $this->assertHtml($expected, $result);
  5555. }
  5556. /**
  5557. * testDateTime method
  5558. *
  5559. * @return void
  5560. */
  5561. public function testDateTime()
  5562. {
  5563. $result = $this->Form->dateTime('date', ['default' => true]);
  5564. $expected = [
  5565. 'input' => [
  5566. 'type' => 'datetime-local',
  5567. 'name' => 'date',
  5568. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}(:\d{2})?/',
  5569. ],
  5570. ];
  5571. $this->assertHtml($expected, $result);
  5572. }
  5573. /**
  5574. * testDateTimeSecured method
  5575. *
  5576. * Test that datetime fields are added to protected fields list.
  5577. *
  5578. * @return void
  5579. */
  5580. public function testDateTimeSecured()
  5581. {
  5582. $this->View->setRequest(
  5583. $this->View->getRequest()->withAttribute('_formToken', ['unlockedFields' => []])
  5584. );
  5585. $this->Form->dateTime('date');
  5586. $expected = ['date'];
  5587. $this->assertEquals($expected, $this->Form->fields);
  5588. $this->Form->fields = [];
  5589. $this->Form->date('published');
  5590. $expected = ['published'];
  5591. $this->assertEquals($expected, $this->Form->fields);
  5592. }
  5593. /**
  5594. * testDateTimeSecuredDisabled method
  5595. *
  5596. * Test that datetime fields are added to protected fields list.
  5597. *
  5598. * @return void
  5599. */
  5600. public function testDateTimeSecuredDisabled()
  5601. {
  5602. $this->View->setRequest(
  5603. $this->View->getRequest()->withAttribute('_formToken', ['unlockedFields' => []])
  5604. );
  5605. $this->Form->dateTime('date', ['secure' => false]);
  5606. $expected = [];
  5607. $this->assertEquals($expected, $this->Form->fields);
  5608. $this->Form->fields = [];
  5609. $this->Form->date('published', ['secure' => false]);
  5610. $expected = [];
  5611. $this->assertEquals($expected, $this->Form->fields);
  5612. }
  5613. /**
  5614. * testDatetimeWithDefault method
  5615. *
  5616. * Test that datetime() and default values work.
  5617. *
  5618. * @return void
  5619. */
  5620. public function testDatetimeWithDefault()
  5621. {
  5622. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5623. $expected = [
  5624. 'input' => [
  5625. 'type' => 'datetime-local',
  5626. 'name' => 'updated',
  5627. 'value' => '2009-06-01T11:15:30',
  5628. ],
  5629. ];
  5630. $this->assertHtml($expected, $result);
  5631. $result = $this->Form->dateTime('updated', [
  5632. 'default' => '2009-06-01 11:15:30',
  5633. ]);
  5634. $expected = [
  5635. 'input' => [
  5636. 'type' => 'datetime-local',
  5637. 'name' => 'updated',
  5638. 'value' => '2009-06-01T11:15:30',
  5639. ],
  5640. ];
  5641. $this->assertHtml($expected, $result);
  5642. }
  5643. /**
  5644. * testMonth method
  5645. *
  5646. * Test generation of a month input.
  5647. *
  5648. * @return void
  5649. */
  5650. public function testMonth()
  5651. {
  5652. $result = $this->Form->month('field', ['value' => '']);
  5653. $expected = [
  5654. 'input' => [
  5655. 'type' => 'month',
  5656. 'name' => 'field',
  5657. 'value' => '',
  5658. ],
  5659. ];
  5660. $this->assertHtml($expected, $result);
  5661. $this->View->setRequest(
  5662. $this->View->getRequest()->withData('release', '2050-02-10')
  5663. );
  5664. $this->Form->create();
  5665. $result = $this->Form->month('release');
  5666. $expected = [
  5667. 'input' => [
  5668. 'type' => 'month',
  5669. 'name' => 'release',
  5670. 'value' => '2050-02',
  5671. ],
  5672. ];
  5673. $this->assertHtml($expected, $result);
  5674. $this->View->setRequest(
  5675. $this->View->getRequest()->withData('release', '2050-03')
  5676. );
  5677. $this->Form->create();
  5678. $result = $this->Form->month('release');
  5679. $expected = [
  5680. 'input' => [
  5681. 'type' => 'month',
  5682. 'name' => 'release',
  5683. 'value' => '2050-03',
  5684. ],
  5685. ];
  5686. $this->assertHtml($expected, $result);
  5687. }
  5688. /**
  5689. * testYear method
  5690. *
  5691. * Test generation of a year input.
  5692. *
  5693. * @return void
  5694. */
  5695. public function testYear()
  5696. {
  5697. $this->View->setRequest(
  5698. $this->View->getRequest()->withData('published', '2006')
  5699. );
  5700. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5701. $expected = [
  5702. ['select' => ['name' => 'field']],
  5703. ['option' => ['selected' => 'selected', 'value' => '']],
  5704. '/option',
  5705. ['option' => ['value' => '2007']],
  5706. '2007',
  5707. '/option',
  5708. ['option' => ['value' => '2006']],
  5709. '2006',
  5710. '/option',
  5711. '/select',
  5712. ];
  5713. $this->assertHtml($expected, $result);
  5714. $result = $this->Form->year('field', [
  5715. 'value' => '',
  5716. 'min' => 2006,
  5717. 'max' => 2007,
  5718. 'order' => 'asc',
  5719. ]);
  5720. $expected = [
  5721. ['select' => ['name' => 'field']],
  5722. ['option' => ['selected' => 'selected', 'value' => '']],
  5723. '/option',
  5724. ['option' => ['value' => '2006']],
  5725. '2006',
  5726. '/option',
  5727. ['option' => ['value' => '2007']],
  5728. '2007',
  5729. '/option',
  5730. '/select',
  5731. ];
  5732. $this->assertHtml($expected, $result);
  5733. $result = $this->Form->year('published', [
  5734. 'empty' => false,
  5735. 'min' => 2006,
  5736. 'max' => 2007,
  5737. ]);
  5738. $expected = [
  5739. ['select' => ['name' => 'published']],
  5740. ['option' => ['value' => '2007']],
  5741. '2007',
  5742. '/option',
  5743. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5744. '2006',
  5745. '/option',
  5746. '/select',
  5747. ];
  5748. $this->assertHtml($expected, $result);
  5749. $result = $this->Form->year('published', [
  5750. 'empty' => 'Published on',
  5751. ]);
  5752. $this->assertStringContainsString('Published on', $result);
  5753. }
  5754. /**
  5755. * testControlYearPreEpoch method
  5756. *
  5757. * Test minYear being prior to the unix epoch.
  5758. *
  5759. * @return void
  5760. */
  5761. public function testControlYearPreEpoch()
  5762. {
  5763. $start = date('Y') - 80;
  5764. $end = date('Y') - 18;
  5765. $result = $this->Form->control('birth_year', [
  5766. 'type' => 'year',
  5767. 'label' => 'Birth Year',
  5768. 'min' => $start,
  5769. 'max' => $end,
  5770. ]);
  5771. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5772. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5773. $this->assertStringNotContainsString('value="00">00', $result);
  5774. }
  5775. /**
  5776. * test control() datetime & required attributes
  5777. *
  5778. * @return void
  5779. */
  5780. public function testControlDatetimeRequired()
  5781. {
  5782. $result = $this->Form->control('birthday', [
  5783. 'type' => 'date',
  5784. 'required' => true,
  5785. ]);
  5786. $this->assertStringContainsString(
  5787. '<input type="date" name="birthday" required="required"',
  5788. $result
  5789. );
  5790. }
  5791. /**
  5792. * testYearAutoExpandRange method
  5793. *
  5794. * @return void
  5795. */
  5796. public function testYearAutoExpandRange()
  5797. {
  5798. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5799. $result = $this->Form->year('birthday');
  5800. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5801. $result = $matches[1];
  5802. $expected = range(date('Y') + 5, 1930);
  5803. $this->assertEquals($expected, $result);
  5804. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5805. $this->Form->create();
  5806. $result = $this->Form->year('release');
  5807. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5808. $result = $matches[1];
  5809. $expected = range(2050, date('Y') - 5);
  5810. $this->assertEquals($expected, $result);
  5811. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5812. $this->Form->create();
  5813. $result = $this->Form->year('release', [
  5814. 'min' => 1890,
  5815. 'max' => 1900,
  5816. ]);
  5817. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5818. $result = $matches[1];
  5819. $expected = range(1900, 1881);
  5820. $this->assertEquals($expected, $result);
  5821. }
  5822. /**
  5823. * testControlLabelFalse method
  5824. *
  5825. * Test the label option being set to false.
  5826. *
  5827. * @return void
  5828. */
  5829. public function testControlLabelFalse()
  5830. {
  5831. $this->Form->create($this->article);
  5832. $result = $this->Form->control('title', ['label' => false]);
  5833. $expected = [
  5834. 'div' => ['class' => 'input text required'],
  5835. 'input' => [
  5836. 'type' => 'text',
  5837. 'required' => 'required',
  5838. 'id' => 'title',
  5839. 'name' => 'title',
  5840. 'data-validity-message' => 'This field cannot be left empty',
  5841. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5842. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5843. ],
  5844. '/div',
  5845. ];
  5846. $this->assertHtml($expected, $result);
  5847. }
  5848. /**
  5849. * testTextArea method
  5850. *
  5851. * Test generation of a textarea input.
  5852. *
  5853. * @return void
  5854. */
  5855. public function testTextArea()
  5856. {
  5857. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5858. $result = $this->Form->textarea('field');
  5859. $expected = [
  5860. 'textarea' => ['name' => 'field', 'rows' => 5],
  5861. 'some test data',
  5862. '/textarea',
  5863. ];
  5864. $this->assertHtml($expected, $result);
  5865. $result = $this->Form->textarea('user.bio');
  5866. $expected = [
  5867. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5868. '/textarea',
  5869. ];
  5870. $this->assertHtml($expected, $result);
  5871. $this->View->setRequest($this->View->getRequest()
  5872. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5873. $this->Form->create();
  5874. $result = $this->Form->textarea('field');
  5875. $expected = [
  5876. 'textarea' => ['name' => 'field', 'rows' => 5],
  5877. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5878. '/textarea',
  5879. ];
  5880. $this->assertHtml($expected, $result);
  5881. $this->View->setRequest($this->View->getRequest()->withData(
  5882. 'Model.field',
  5883. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5884. ));
  5885. $this->Form->create();
  5886. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5887. $expected = [
  5888. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5889. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5890. '/textarea',
  5891. ];
  5892. $this->assertHtml($expected, $result);
  5893. $result = $this->Form->textarea('0.OtherModel.field');
  5894. $expected = [
  5895. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5896. '/textarea',
  5897. ];
  5898. $this->assertHtml($expected, $result);
  5899. }
  5900. /**
  5901. * testTextAreaWithStupidCharacters method
  5902. *
  5903. * Test text area with non-ascii characters.
  5904. *
  5905. * @return void
  5906. */
  5907. public function testTextAreaWithStupidCharacters()
  5908. {
  5909. $result = $this->Form->textarea('Post.content', [
  5910. 'value' => 'GREAT®',
  5911. 'rows' => '15',
  5912. 'cols' => '75',
  5913. ]);
  5914. $expected = [
  5915. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5916. 'GREAT®',
  5917. '/textarea',
  5918. ];
  5919. $this->assertHtml($expected, $result);
  5920. }
  5921. /**
  5922. * testTextAreaMaxLength method
  5923. *
  5924. * Test textareas maxlength read from schema.
  5925. *
  5926. * @return void
  5927. */
  5928. public function testTextAreaMaxLength()
  5929. {
  5930. $this->Form->create([
  5931. 'schema' => [
  5932. 'stuff' => ['type' => 'string', 'length' => 10],
  5933. ],
  5934. ]);
  5935. $result = $this->Form->control('other', ['type' => 'textarea']);
  5936. $expected = [
  5937. 'div' => ['class' => 'input textarea'],
  5938. 'label' => ['for' => 'other'],
  5939. 'Other',
  5940. '/label',
  5941. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5942. '/textarea',
  5943. '/div',
  5944. ];
  5945. $this->assertHtml($expected, $result);
  5946. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5947. $expected = [
  5948. 'div' => ['class' => 'input textarea'],
  5949. 'label' => ['for' => 'stuff'],
  5950. 'Stuff',
  5951. '/label',
  5952. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5953. '/textarea',
  5954. '/div',
  5955. ];
  5956. $this->assertHtml($expected, $result);
  5957. }
  5958. /**
  5959. * testHiddenField method
  5960. *
  5961. * Test generation of a hidden input.
  5962. *
  5963. * @return void
  5964. */
  5965. public function testHiddenField()
  5966. {
  5967. $this->article['errors'] = [
  5968. 'field' => true,
  5969. ];
  5970. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5971. $this->Form->create($this->article);
  5972. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5973. $expected = [
  5974. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5975. $this->assertHtml($expected, $result);
  5976. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5977. $expected = [
  5978. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5979. ];
  5980. $this->assertHtml($expected, $result);
  5981. }
  5982. /**
  5983. * testFileUploadField method
  5984. *
  5985. * Test generation of a file upload input.
  5986. *
  5987. * @return void
  5988. */
  5989. public function testFileUploadField()
  5990. {
  5991. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5992. $result = $this->Form->file('Model.upload');
  5993. $this->assertHtml($expected, $result);
  5994. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5995. 'name' => '', 'type' => '', 'tmp_name' => '',
  5996. 'error' => 4, 'size' => 0,
  5997. ]));
  5998. $result = $this->Form->file('Model.upload');
  5999. $this->assertHtml($expected, $result);
  6000. $this->View->setRequest(
  6001. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6002. );
  6003. $result = $this->Form->file('Model.upload');
  6004. $this->assertHtml($expected, $result);
  6005. }
  6006. /**
  6007. * testFileUploadOnOtherModel method
  6008. *
  6009. * Test File upload input on a model not used in create().
  6010. *
  6011. * @return void
  6012. */
  6013. public function testFileUploadOnOtherModel()
  6014. {
  6015. $this->Form->create($this->article, ['type' => 'file']);
  6016. $result = $this->Form->file('ValidateProfile.city');
  6017. $expected = [
  6018. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6019. ];
  6020. $this->assertHtml($expected, $result);
  6021. }
  6022. /**
  6023. * testButton method
  6024. *
  6025. * Test generation of a form button.
  6026. *
  6027. * @return void
  6028. */
  6029. public function testButton()
  6030. {
  6031. $result = $this->Form->button('Hi');
  6032. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6033. $this->assertHtml($expected, $result);
  6034. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6035. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6036. $this->assertHtml($expected, $result);
  6037. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6038. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6039. $this->assertHtml($expected, $result);
  6040. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6041. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6042. $this->assertHtml($expected, $result);
  6043. $result = $this->Form->button('No type', ['type' => false]);
  6044. $expected = ['button' => [], 'No type', '/button'];
  6045. $this->assertHtml($expected, $result);
  6046. $result = $this->Form->button('Upload Text', [
  6047. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6048. 'escape' => false,
  6049. ]);
  6050. $this->assertNotRegExp('/\&039/', $result);
  6051. }
  6052. /**
  6053. * testButtonUnlockedByDefault method
  6054. *
  6055. * Test that button() makes unlocked fields by default.
  6056. *
  6057. * @return void
  6058. */
  6059. public function testButtonUnlockedByDefault()
  6060. {
  6061. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'secured'));
  6062. $this->Form->button('Save', ['name' => 'save']);
  6063. $this->Form->button('Clear');
  6064. $result = $this->Form->unlockField();
  6065. $this->assertEquals(['save'], $result);
  6066. }
  6067. /**
  6068. * Test generation of a form button with confirm message.
  6069. *
  6070. * @return void
  6071. */
  6072. public function testButtonWithConfirm()
  6073. {
  6074. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6075. $expected = ['button' => [
  6076. 'type' => 'submit',
  6077. 'data-confirm-message' => 'Confirm me!',
  6078. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6079. ], 'Hi', '/button'];
  6080. $this->assertHtml($expected, $result);
  6081. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6082. $expected = ['button' => [
  6083. 'type' => 'submit',
  6084. 'data-confirm-message' => 'Confirm "me"!',
  6085. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6086. ], 'Hi', '/button'];
  6087. $this->assertHtml($expected, $result);
  6088. }
  6089. /**
  6090. * testPostButton method
  6091. *
  6092. * @return void
  6093. */
  6094. public function testPostButton()
  6095. {
  6096. $result = $this->Form->postButton('Hi', '/controller/action');
  6097. $expected = [
  6098. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6099. 'div' => ['style' => 'display:none;'],
  6100. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6101. '/div',
  6102. 'button' => ['type' => 'submit'],
  6103. 'Hi',
  6104. '/button',
  6105. '/form',
  6106. ];
  6107. $this->assertHtml($expected, $result);
  6108. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6109. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6110. }
  6111. /**
  6112. * testPostButtonMethodType method
  6113. *
  6114. * @return void
  6115. */
  6116. public function testPostButtonMethodType()
  6117. {
  6118. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6119. $expected = [
  6120. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6121. 'div' => ['style' => 'display:none;'],
  6122. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6123. '/div',
  6124. 'button' => ['type' => 'submit'],
  6125. 'Hi',
  6126. '/button',
  6127. '/form',
  6128. ];
  6129. $this->assertHtml($expected, $result);
  6130. }
  6131. /**
  6132. * testPostButtonFormOptions method
  6133. *
  6134. * @return void
  6135. */
  6136. public function testPostButtonFormOptions()
  6137. {
  6138. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6139. $expected = [
  6140. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6141. 'div' => ['style' => 'display:none;'],
  6142. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6143. '/div',
  6144. 'button' => ['type' => 'submit'],
  6145. 'Hi',
  6146. '/button',
  6147. '/form',
  6148. ];
  6149. $this->assertHtml($expected, $result);
  6150. }
  6151. /**
  6152. * testPostButtonNestedData method
  6153. *
  6154. * Test using postButton with N dimensional data.
  6155. *
  6156. * @return void
  6157. */
  6158. public function testPostButtonNestedData()
  6159. {
  6160. $data = [
  6161. 'one' => [
  6162. 'two' => [
  6163. 3, 4, 5,
  6164. ],
  6165. ],
  6166. ];
  6167. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6168. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6169. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6170. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6171. }
  6172. /**
  6173. * testSecurePostButton method
  6174. *
  6175. * Test that postButton adds _Token fields.
  6176. *
  6177. * @return void
  6178. */
  6179. public function testSecurePostButton()
  6180. {
  6181. $this->View->setRequest($this->View->getRequest()
  6182. ->withAttribute('csrfToken', 'testkey')
  6183. ->withAttribute('_formToken', ['unlockedFields' => []]));
  6184. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6185. $tokenDebug = urlencode(json_encode([
  6186. '/posts/delete/1',
  6187. [],
  6188. [],
  6189. ]));
  6190. $expected = [
  6191. 'form' => [
  6192. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6193. ],
  6194. ['div' => ['style' => 'display:none;']],
  6195. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6196. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6197. '/div',
  6198. 'button' => ['type' => 'submit'],
  6199. 'Delete',
  6200. '/button',
  6201. ['div' => ['style' => 'display:none;']],
  6202. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6203. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6204. ['input' => [
  6205. 'type' => 'hidden',
  6206. 'name' => '_Token[debug]',
  6207. 'value' => $tokenDebug,
  6208. 'autocomplete' => 'off',
  6209. ]],
  6210. '/div',
  6211. '/form',
  6212. ];
  6213. $this->assertHtml($expected, $result);
  6214. }
  6215. /**
  6216. * testPostLink method
  6217. *
  6218. * @return void
  6219. */
  6220. public function testPostLink()
  6221. {
  6222. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6223. $expected = [
  6224. 'form' => [
  6225. 'method' => 'post', 'action' => '/posts/delete/1',
  6226. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6227. ],
  6228. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6229. '/form',
  6230. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6231. 'Delete',
  6232. '/a',
  6233. ];
  6234. $this->assertHtml($expected, $result);
  6235. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6236. $expected = [
  6237. 'form' => [
  6238. 'method' => 'post', 'action' => '/posts/delete/1',
  6239. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6240. ],
  6241. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6242. '/form',
  6243. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6244. 'Delete',
  6245. '/a',
  6246. ];
  6247. $this->assertHtml($expected, $result);
  6248. $result = $this->Form->postLink(
  6249. 'Delete',
  6250. '/posts/delete/1',
  6251. ['target' => '_blank', 'class' => 'btn btn-danger']
  6252. );
  6253. $expected = [
  6254. 'form' => [
  6255. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6256. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6257. ],
  6258. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6259. '/form',
  6260. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6261. 'Delete',
  6262. '/a',
  6263. ];
  6264. $this->assertHtml($expected, $result);
  6265. }
  6266. /**
  6267. * testPostLinkWithConfirm method
  6268. *
  6269. * Test the confirm option for postLink().
  6270. *
  6271. * @return void
  6272. */
  6273. public function testPostLinkWithConfirm()
  6274. {
  6275. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6276. $expected = [
  6277. 'form' => [
  6278. 'method' => 'post', 'action' => '/posts/delete/1',
  6279. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6280. ],
  6281. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6282. '/form',
  6283. 'a' => [
  6284. 'href' => '#',
  6285. 'data-confirm-message' => 'Confirm?',
  6286. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6287. ],
  6288. 'Delete',
  6289. '/a',
  6290. ];
  6291. $this->assertHtml($expected, $result);
  6292. $result = $this->Form->postLink(
  6293. 'Delete',
  6294. '/posts/delete/1',
  6295. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6296. );
  6297. $expected = [
  6298. 'form' => [
  6299. 'method' => 'post', 'action' => '/posts/delete/1',
  6300. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6301. ],
  6302. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6303. '/form',
  6304. 'a' => [
  6305. 'href' => '#',
  6306. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6307. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6308. ],
  6309. 'Delete',
  6310. '/a',
  6311. ];
  6312. $this->assertHtml($expected, $result);
  6313. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6314. $result = $this->Form->postLink(
  6315. 'Delete',
  6316. '/posts/delete/1',
  6317. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6318. );
  6319. $expected = [
  6320. 'form' => [
  6321. 'method' => 'post', 'action' => '/posts/delete/1',
  6322. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6323. ],
  6324. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6325. '/form',
  6326. 'a' => [
  6327. 'href' => '#',
  6328. 'data-confirm-message' => 'Confirm this deletion?',
  6329. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6330. ],
  6331. 'Delete',
  6332. '/a',
  6333. ];
  6334. $this->assertHtml($expected, $result);
  6335. }
  6336. /**
  6337. * testPostLinkWithQuery method
  6338. *
  6339. * Test postLink() with query string args.
  6340. *
  6341. * @return void
  6342. */
  6343. public function testPostLinkWithQuery()
  6344. {
  6345. $result = $this->Form->postLink(
  6346. 'Delete',
  6347. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6348. );
  6349. $expected = [
  6350. 'form' => [
  6351. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6352. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6353. ],
  6354. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6355. '/form',
  6356. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6357. 'Delete',
  6358. '/a',
  6359. ];
  6360. $this->assertHtml($expected, $result);
  6361. }
  6362. /**
  6363. * testPostLinkWithData method
  6364. *
  6365. * Test postLink with additional data.
  6366. *
  6367. * @return void
  6368. */
  6369. public function testPostLinkWithData()
  6370. {
  6371. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6372. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6373. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6374. $this->Form->create($entity);
  6375. $this->Form->end();
  6376. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6377. $this->assertStringContainsString(
  6378. '<input type="hidden" name="name" value="show"',
  6379. $result,
  6380. 'should not contain entity data.'
  6381. );
  6382. }
  6383. /**
  6384. * testPostLinkSecurityHash method
  6385. *
  6386. * Test that security hashes for postLink include the url.
  6387. *
  6388. * @return void
  6389. */
  6390. public function testPostLinkSecurityHash()
  6391. {
  6392. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6393. $hash .= '%3Aid';
  6394. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', ['key' => 'test']));
  6395. $result = $this->Form->postLink(
  6396. 'Delete',
  6397. '/posts/delete/1',
  6398. ['data' => ['id' => 1]]
  6399. );
  6400. $tokenDebug = urlencode(json_encode([
  6401. '/posts/delete/1',
  6402. [
  6403. 'id' => 1,
  6404. ],
  6405. [],
  6406. ]));
  6407. $expected = [
  6408. 'form' => [
  6409. 'method' => 'post', 'action' => '/posts/delete/1',
  6410. 'name', 'style' => 'display:none;',
  6411. ],
  6412. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6413. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6414. 'div' => ['style' => 'display:none;'],
  6415. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6416. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6417. ['input' => [
  6418. 'type' => 'hidden',
  6419. 'name' => '_Token[debug]',
  6420. 'value' => $tokenDebug,
  6421. 'autocomplete' => 'off',
  6422. ]],
  6423. '/div',
  6424. '/form',
  6425. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6426. 'Delete',
  6427. '/a',
  6428. ];
  6429. $this->assertHtml($expected, $result);
  6430. }
  6431. /**
  6432. * testPostLinkSecurityHashBlockMode method
  6433. *
  6434. * Test that postLink doesn't modify the fields in the containing form.
  6435. *
  6436. * postLink() calls inside open forms should not modify the field list
  6437. * for the form.
  6438. *
  6439. * @return void
  6440. */
  6441. public function testPostLinkSecurityHashBlockMode()
  6442. {
  6443. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6444. $hash .= '%3A';
  6445. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', ['key' => 'test']));
  6446. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6447. $this->Form->control('title');
  6448. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6449. $result = $this->View->fetch('postLink');
  6450. $this->assertEquals(['title'], $this->Form->fields);
  6451. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6452. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6453. $reflect->setAccessible(true);
  6454. $this->assertEquals('/articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6455. }
  6456. /**
  6457. * testPostLinkSecurityHashNoDebugMode method
  6458. *
  6459. * Test that security does not include debug token if debug is false.
  6460. *
  6461. * @return void
  6462. */
  6463. public function testPostLinkSecurityHashNoDebugMode()
  6464. {
  6465. Configure::write('debug', false);
  6466. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6467. $hash .= '%3Aid';
  6468. $this->View->setRequest($this->View->getRequest()
  6469. ->withAttribute('_formToken', ['key' => 'test']));
  6470. $result = $this->Form->postLink(
  6471. 'Delete',
  6472. '/posts/delete/1',
  6473. ['data' => ['id' => 1]]
  6474. );
  6475. $expected = [
  6476. 'form' => [
  6477. 'method' => 'post', 'action' => '/posts/delete/1',
  6478. 'name', 'style' => 'display:none;',
  6479. ],
  6480. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6481. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6482. 'div' => ['style' => 'display:none;'],
  6483. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6484. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6485. '/div',
  6486. '/form',
  6487. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6488. 'Delete',
  6489. '/a',
  6490. ];
  6491. $this->assertHtml($expected, $result);
  6492. }
  6493. /**
  6494. * testPostLinkNestedData method
  6495. *
  6496. * Test using postLink with N dimensional data.
  6497. *
  6498. * @return void
  6499. */
  6500. public function testPostLinkNestedData()
  6501. {
  6502. $data = [
  6503. 'one' => [
  6504. 'two' => [
  6505. 3, 4, 5,
  6506. ],
  6507. ],
  6508. ];
  6509. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6510. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6511. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6512. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6513. }
  6514. /**
  6515. * testPostLinkAfterGetForm method
  6516. *
  6517. * Test creating postLinks after a GET form.
  6518. *
  6519. * @return void
  6520. */
  6521. public function testPostLinkAfterGetForm()
  6522. {
  6523. $this->View->setRequest($this->View->getRequest()
  6524. ->withAttribute('csrfToken', 'testkey')
  6525. ->withAttribute('_formToken', 'val'));
  6526. $this->Form->create($this->article, ['type' => 'get']);
  6527. $this->Form->end();
  6528. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6529. $tokenDebug = urlencode(json_encode([
  6530. '/posts/delete/1',
  6531. [],
  6532. [],
  6533. ]));
  6534. $expected = [
  6535. 'form' => [
  6536. 'method' => 'post', 'action' => '/posts/delete/1',
  6537. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6538. ],
  6539. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6540. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6541. 'div' => ['style' => 'display:none;'],
  6542. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6543. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6544. ['input' => [
  6545. 'type' => 'hidden', 'name' => '_Token[debug]',
  6546. 'value' => $tokenDebug,
  6547. 'autocomplete' => 'off',
  6548. ]],
  6549. '/div',
  6550. '/form',
  6551. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6552. 'Delete',
  6553. '/a',
  6554. ];
  6555. $this->assertHtml($expected, $result);
  6556. }
  6557. /**
  6558. * testPostLinkFormBuffer method
  6559. *
  6560. * Test that postLink adds form tags to view block.
  6561. *
  6562. * @return void
  6563. */
  6564. public function testPostLinkFormBuffer()
  6565. {
  6566. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6567. $expected = [
  6568. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6569. 'Delete',
  6570. '/a',
  6571. ];
  6572. $this->assertHtml($expected, $result);
  6573. $result = $this->View->fetch('postLink');
  6574. $expected = [
  6575. 'form' => [
  6576. 'method' => 'post', 'action' => '/posts/delete/1',
  6577. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6578. ],
  6579. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6580. '/form',
  6581. ];
  6582. $this->assertHtml($expected, $result);
  6583. $result = $this->Form->postLink(
  6584. 'Delete',
  6585. '/posts/delete/2',
  6586. ['block' => true, 'method' => 'DELETE']
  6587. );
  6588. $expected = [
  6589. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6590. 'Delete',
  6591. '/a',
  6592. ];
  6593. $this->assertHtml($expected, $result);
  6594. $result = $this->View->fetch('postLink');
  6595. $expected = [
  6596. 'form' => [
  6597. 'method' => 'post', 'action' => '/posts/delete/1',
  6598. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6599. ],
  6600. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6601. '/form',
  6602. [
  6603. 'form' => [
  6604. 'method' => 'post', 'action' => '/posts/delete/2',
  6605. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6606. ],
  6607. ],
  6608. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6609. '/form',
  6610. ];
  6611. $this->assertHtml($expected, $result);
  6612. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6613. $expected = [
  6614. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6615. 'Delete',
  6616. '/a',
  6617. ];
  6618. $this->assertHtml($expected, $result);
  6619. $result = $this->View->fetch('foobar');
  6620. $expected = [
  6621. 'form' => [
  6622. 'method' => 'post', 'action' => '/posts/delete/1',
  6623. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6624. ],
  6625. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6626. '/form',
  6627. ];
  6628. $this->assertHtml($expected, $result);
  6629. }
  6630. /**
  6631. * testSubmitButton method
  6632. *
  6633. * @return void
  6634. */
  6635. public function testSubmitButton()
  6636. {
  6637. $result = $this->Form->submit('');
  6638. $expected = [
  6639. 'div' => ['class' => 'submit'],
  6640. 'input' => ['type' => 'submit', 'value' => ''],
  6641. '/div',
  6642. ];
  6643. $this->assertHtml($expected, $result);
  6644. $result = $this->Form->submit('Test Submit');
  6645. $expected = [
  6646. 'div' => ['class' => 'submit'],
  6647. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6648. '/div',
  6649. ];
  6650. $this->assertHtml($expected, $result);
  6651. $result = $this->Form->submit('Next >');
  6652. $expected = [
  6653. 'div' => ['class' => 'submit'],
  6654. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6655. '/div',
  6656. ];
  6657. $this->assertHtml($expected, $result);
  6658. $result = $this->Form->submit('Next >', ['escape' => false]);
  6659. $expected = [
  6660. 'div' => ['class' => 'submit'],
  6661. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6662. '/div',
  6663. ];
  6664. $this->assertHtml($expected, $result);
  6665. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6666. $expected = [
  6667. 'div' => ['class' => 'submit'],
  6668. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6669. '/div',
  6670. ];
  6671. $this->assertHtml($expected, $result);
  6672. }
  6673. /**
  6674. * testSubmitImage method
  6675. *
  6676. * Test image submit types.
  6677. *
  6678. * @return void
  6679. */
  6680. public function testSubmitImage()
  6681. {
  6682. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6683. $expected = [
  6684. 'div' => ['class' => 'submit'],
  6685. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6686. '/div',
  6687. ];
  6688. $this->assertHtml($expected, $result);
  6689. $result = $this->Form->submit('/relative/cake.power.gif');
  6690. $expected = [
  6691. 'div' => ['class' => 'submit'],
  6692. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6693. '/div',
  6694. ];
  6695. $this->assertHtml($expected, $result);
  6696. $result = $this->Form->submit('cake.power.gif');
  6697. $expected = [
  6698. 'div' => ['class' => 'submit'],
  6699. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6700. '/div',
  6701. ];
  6702. $this->assertHtml($expected, $result);
  6703. $result = $this->Form->submit('Not.an.image');
  6704. $expected = [
  6705. 'div' => ['class' => 'submit'],
  6706. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6707. '/div',
  6708. ];
  6709. $this->assertHtml($expected, $result);
  6710. }
  6711. /**
  6712. * testSubmitUnlockedByDefault method
  6713. *
  6714. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6715. * be submitted at a time.
  6716. *
  6717. * @return void
  6718. */
  6719. public function testSubmitUnlockedByDefault()
  6720. {
  6721. $this->View->setRequest($this->View->getRequest()->withAttribute('_formToken', 'secured'));
  6722. $this->Form->submit('Go go');
  6723. $this->Form->submit('Save', ['name' => 'save']);
  6724. $result = $this->Form->unlockField();
  6725. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6726. }
  6727. /**
  6728. * testSubmitImageTimestamp method
  6729. *
  6730. * Test submit image with timestamps.
  6731. *
  6732. * @return void
  6733. */
  6734. public function testSubmitImageTimestamp()
  6735. {
  6736. Configure::write('Asset.timestamp', 'force');
  6737. $result = $this->Form->submit('cake.power.gif');
  6738. $expected = [
  6739. 'div' => ['class' => 'submit'],
  6740. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6741. '/div',
  6742. ];
  6743. $this->assertHtml($expected, $result);
  6744. }
  6745. /**
  6746. * testDateTimeWithGetForms method
  6747. *
  6748. * Test that datetime() works with GET style forms.
  6749. *
  6750. * @return void
  6751. */
  6752. public function testDateTimeWithGetForms()
  6753. {
  6754. $this->Form->create($this->article, ['type' => 'get']);
  6755. $result = $this->Form->datetime('created');
  6756. $expected = [
  6757. 'input' => [
  6758. 'type' => 'datetime-local',
  6759. 'name' => 'created',
  6760. 'value' => '',
  6761. ],
  6762. ];
  6763. $this->assertHtml($expected, $result);
  6764. $result = $this->Form->datetime('created', ['default' => true]);
  6765. $expected = [
  6766. 'input' => [
  6767. 'type' => 'datetime-local',
  6768. 'name' => 'created',
  6769. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}(:\d{2})?/',
  6770. ],
  6771. ];
  6772. $this->assertHtml($expected, $result);
  6773. }
  6774. /**
  6775. * testForMagicControlNonExistingNorValidated method
  6776. *
  6777. * @return void
  6778. */
  6779. public function testForMagicControlNonExistingNorValidated()
  6780. {
  6781. $this->Form->create($this->article);
  6782. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6783. $result = $this->Form->control('non_existing_nor_validated');
  6784. $expected = [
  6785. 'label' => ['for' => 'non-existing-nor-validated'],
  6786. 'Non Existing Nor Validated',
  6787. '/label',
  6788. 'input' => [
  6789. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6790. 'id' => 'non-existing-nor-validated',
  6791. ],
  6792. ];
  6793. $this->assertHtml($expected, $result);
  6794. $result = $this->Form->control('non_existing_nor_validated', [
  6795. 'val' => 'my value',
  6796. ]);
  6797. $expected = [
  6798. 'label' => ['for' => 'non-existing-nor-validated'],
  6799. 'Non Existing Nor Validated',
  6800. '/label',
  6801. 'input' => [
  6802. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6803. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  6804. ],
  6805. ];
  6806. $this->assertHtml($expected, $result);
  6807. $this->View->setRequest(
  6808. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  6809. );
  6810. $this->Form->create($this->article);
  6811. $result = $this->Form->control('non_existing_nor_validated');
  6812. $expected = [
  6813. 'label' => ['for' => 'non-existing-nor-validated'],
  6814. 'Non Existing Nor Validated',
  6815. '/label',
  6816. 'input' => [
  6817. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6818. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  6819. ],
  6820. ];
  6821. $this->assertHtml($expected, $result);
  6822. }
  6823. /**
  6824. * testFormMagicControlLabel method
  6825. *
  6826. * @return void
  6827. */
  6828. public function testFormMagicControlLabel()
  6829. {
  6830. $this->getTableLocator()->get('Contacts', [
  6831. 'className' => ContactsTable::class,
  6832. ]);
  6833. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6834. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6835. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6836. $expected = [
  6837. 'label' => ['for' => 'contacts-name'],
  6838. 'My label',
  6839. '/label',
  6840. 'input' => [
  6841. 'type' => 'text', 'name' => 'Contacts[name]',
  6842. 'id' => 'contacts-name', 'maxlength' => '255',
  6843. ],
  6844. ];
  6845. $this->assertHtml($expected, $result);
  6846. $result = $this->Form->control('name', [
  6847. 'label' => ['class' => 'mandatory'],
  6848. ]);
  6849. $expected = [
  6850. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6851. 'Name',
  6852. '/label',
  6853. 'input' => [
  6854. 'type' => 'text', 'name' => 'name',
  6855. 'id' => 'name', 'maxlength' => '255',
  6856. ],
  6857. ];
  6858. $this->assertHtml($expected, $result);
  6859. $result = $this->Form->control('name', [
  6860. 'div' => false,
  6861. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6862. ]);
  6863. $expected = [
  6864. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6865. 'My label',
  6866. '/label',
  6867. 'input' => [
  6868. 'type' => 'text', 'name' => 'name',
  6869. 'id' => 'name', 'maxlength' => '255',
  6870. ],
  6871. ];
  6872. $this->assertHtml($expected, $result);
  6873. $result = $this->Form->control('Contact.name', [
  6874. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6875. ]);
  6876. $expected = [
  6877. 'label' => ['for' => 'my_id'],
  6878. 'Name',
  6879. '/label',
  6880. 'input' => [
  6881. 'type' => 'text', 'name' => 'Contact[name]',
  6882. 'id' => 'my_id', 'maxlength' => '255',
  6883. ],
  6884. ];
  6885. $this->assertHtml($expected, $result);
  6886. $result = $this->Form->control('1.id');
  6887. $expected = ['input' => [
  6888. 'type' => 'hidden', 'name' => '1[id]',
  6889. 'id' => '1-id',
  6890. ]];
  6891. $this->assertHtml($expected, $result);
  6892. $result = $this->Form->control('1.name');
  6893. $expected = [
  6894. 'label' => ['for' => '1-name'],
  6895. 'Name',
  6896. '/label',
  6897. 'input' => [
  6898. 'type' => 'text', 'name' => '1[name]',
  6899. 'id' => '1-name', 'maxlength' => '255',
  6900. ],
  6901. ];
  6902. $this->assertHtml($expected, $result);
  6903. }
  6904. /**
  6905. * testFormEnd method
  6906. *
  6907. * @return void
  6908. */
  6909. public function testFormEnd()
  6910. {
  6911. $this->assertSame('</form>', $this->Form->end());
  6912. }
  6913. /**
  6914. * testMultiRecordForm method
  6915. *
  6916. * Test the generation of fields for a multi record form.
  6917. *
  6918. * @return void
  6919. */
  6920. public function testMultiRecordForm()
  6921. {
  6922. $this->loadFixtures('Articles', 'Comments');
  6923. $articles = $this->getTableLocator()->get('Articles');
  6924. $articles->hasMany('Comments');
  6925. $comment = new Entity(['comment' => 'Value']);
  6926. $article = new Article(['comments' => [$comment]]);
  6927. $this->Form->create([$article]);
  6928. $result = $this->Form->control('0.comments.1.comment');
  6929. // phpcs:disable
  6930. $expected = [
  6931. 'div' => ['class' => 'input textarea'],
  6932. 'label' => ['for' => '0-comments-1-comment'],
  6933. 'Comment',
  6934. '/label',
  6935. 'textarea' => [
  6936. 'name',
  6937. 'id' => '0-comments-1-comment',
  6938. 'rows' => 5
  6939. ],
  6940. '/textarea',
  6941. '/div'
  6942. ];
  6943. // phpcs:enable
  6944. $this->assertHtml($expected, $result);
  6945. $result = $this->Form->control('0.comments.0.comment');
  6946. // phpcs:disable
  6947. $expected = [
  6948. 'div' => ['class' => 'input textarea'],
  6949. 'label' => ['for' => '0-comments-0-comment'],
  6950. 'Comment',
  6951. '/label',
  6952. 'textarea' => [
  6953. 'name',
  6954. 'id' => '0-comments-0-comment',
  6955. 'rows' => 5
  6956. ],
  6957. 'Value',
  6958. '/textarea',
  6959. '/div'
  6960. ];
  6961. // phpcs:enable
  6962. $this->assertHtml($expected, $result);
  6963. $comment->setError('comment', ['Not valid']);
  6964. $result = $this->Form->control('0.comments.0.comment');
  6965. // phpcs:disable
  6966. $expected = [
  6967. 'div' => ['class' => 'input textarea error'],
  6968. 'label' => ['for' => '0-comments-0-comment'],
  6969. 'Comment',
  6970. '/label',
  6971. 'textarea' => [
  6972. 'name',
  6973. 'class' => 'form-error',
  6974. 'id' => '0-comments-0-comment',
  6975. 'rows' => 5
  6976. ],
  6977. 'Value',
  6978. '/textarea',
  6979. ['div' => ['class' => 'error-message']],
  6980. 'Not valid',
  6981. '/div',
  6982. '/div'
  6983. ];
  6984. // phpcs:enable
  6985. $this->assertHtml($expected, $result);
  6986. $this->getTableLocator()->get('Comments')
  6987. ->getValidator('default')
  6988. ->allowEmpty('comment', false);
  6989. $result = $this->Form->control('0.comments.1.comment');
  6990. // phpcs:disable
  6991. $expected = [
  6992. 'div' => ['class' => 'input textarea required'],
  6993. 'label' => ['for' => '0-comments-1-comment'],
  6994. 'Comment',
  6995. '/label',
  6996. 'textarea' => [
  6997. 'name',
  6998. 'required' => 'required',
  6999. 'id' => '0-comments-1-comment',
  7000. 'rows' => 5,
  7001. 'data-validity-message' => 'This field cannot be left empty',
  7002. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7003. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7004. ],
  7005. '/textarea',
  7006. '/div'
  7007. ];
  7008. // phpcs:enable
  7009. $this->assertHtml($expected, $result);
  7010. }
  7011. /**
  7012. * testHtml5Controls method
  7013. *
  7014. * Test that some html5 inputs + FormHelper::__call() work.
  7015. *
  7016. * @return void
  7017. */
  7018. public function testHtml5Controls()
  7019. {
  7020. $result = $this->Form->email('User.email');
  7021. $expected = [
  7022. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7023. ];
  7024. $this->assertHtml($expected, $result);
  7025. $result = $this->Form->search('User.query');
  7026. $expected = [
  7027. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7028. ];
  7029. $this->assertHtml($expected, $result);
  7030. $result = $this->Form->search('User.query', ['value' => 'test']);
  7031. $expected = [
  7032. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7033. ];
  7034. $this->assertHtml($expected, $result);
  7035. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7036. $expected = [
  7037. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7038. ];
  7039. $this->assertHtml($expected, $result);
  7040. }
  7041. /**
  7042. * testHtml5ControlWithControl method
  7043. *
  7044. * Test accessing html5 inputs through control().
  7045. *
  7046. * @return void
  7047. */
  7048. public function testHtml5ControlWithControl()
  7049. {
  7050. $this->Form->create();
  7051. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7052. $result = $this->Form->control('website', [
  7053. 'type' => 'url',
  7054. 'val' => 'http://domain.tld',
  7055. 'label' => false,
  7056. ]);
  7057. $expected = [
  7058. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7059. ];
  7060. $this->assertHtml($expected, $result);
  7061. }
  7062. /**
  7063. * testHtml5ControlException method
  7064. *
  7065. * Test errors when field name is missing.
  7066. *
  7067. * @return void
  7068. */
  7069. public function testHtml5ControlException()
  7070. {
  7071. $this->expectException(\Cake\Core\Exception\Exception::class);
  7072. $this->Form->email();
  7073. }
  7074. /**
  7075. * tests fields that are required use custom validation messages
  7076. *
  7077. * @return void
  7078. */
  7079. public function testHtml5ErrorMessage()
  7080. {
  7081. $this->Form->setConfig('autoSetCustomValidity', true);
  7082. $validator = (new \Cake\Validation\Validator())
  7083. ->notEmpty('email', 'Custom error message')
  7084. ->requirePresence('password')
  7085. ->alphaNumeric('password')
  7086. ->notBlank('phone');
  7087. $table = $this->getTableLocator()->get('Contacts', [
  7088. 'className' => ContactsTable::class,
  7089. ]);
  7090. $table->setValidator('default', $validator);
  7091. $contact = new Entity();
  7092. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7093. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7094. $result = $this->Form->control('password');
  7095. $expected = [
  7096. 'label' => ['for' => 'password'],
  7097. 'Password',
  7098. '/label',
  7099. 'input' => [
  7100. 'id' => 'password',
  7101. 'name' => 'password',
  7102. 'type' => 'password',
  7103. 'value' => '',
  7104. 'required' => 'required',
  7105. 'data-validity-message' => 'This field cannot be left empty',
  7106. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7107. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7108. ],
  7109. ];
  7110. $this->assertHtml($expected, $result);
  7111. $result = $this->Form->control('phone');
  7112. $expected = [
  7113. 'label' => ['for' => 'phone'],
  7114. 'Phone',
  7115. '/label',
  7116. 'input' => [
  7117. 'id' => 'phone',
  7118. 'name' => 'phone',
  7119. 'type' => 'tel',
  7120. 'value' => '',
  7121. 'maxlength' => 255,
  7122. 'required' => 'required',
  7123. 'data-validity-message' => 'This field cannot be left empty',
  7124. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7125. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7126. ],
  7127. ];
  7128. $this->assertHtml($expected, $result);
  7129. $result = $this->Form->control('email');
  7130. $expected = [
  7131. 'label' => ['for' => 'email'],
  7132. 'Email',
  7133. '/label',
  7134. 'input' => [
  7135. 'id' => 'email',
  7136. 'name' => 'email',
  7137. 'type' => 'email',
  7138. 'value' => '',
  7139. 'maxlength' => 255,
  7140. 'required' => 'required',
  7141. 'data-validity-message' => 'Custom error message',
  7142. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7143. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7144. ],
  7145. ];
  7146. $this->assertHtml($expected, $result);
  7147. }
  7148. /**
  7149. * tests that custom validation messages are in templateVars
  7150. *
  7151. * @return void
  7152. */
  7153. public function testHtml5ErrorMessageInTemplateVars()
  7154. {
  7155. $validator = (new \Cake\Validation\Validator())
  7156. ->notEmpty('email', 'Custom error "message" & entities')
  7157. ->requirePresence('password')
  7158. ->alphaNumeric('password')
  7159. ->notBlank('phone');
  7160. $table = $this->getTableLocator()->get('Contacts', [
  7161. 'className' => ContactsTable::class,
  7162. ]);
  7163. $table->setValidator('default', $validator);
  7164. $contact = new Entity();
  7165. $this->Form->setConfig('autoSetCustomValidity', false);
  7166. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7167. $this->Form->setTemplates([
  7168. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7169. 'inputContainer' => '{{content}}',
  7170. ]);
  7171. $result = $this->Form->control('password');
  7172. $expected = [
  7173. 'label' => ['for' => 'password'],
  7174. 'Password',
  7175. '/label',
  7176. 'input' => [
  7177. 'id' => 'password',
  7178. 'name' => 'password',
  7179. 'type' => 'password',
  7180. 'value' => '',
  7181. 'required' => 'required',
  7182. 'data-message' => 'This field cannot be left empty',
  7183. ],
  7184. ];
  7185. $this->assertHtml($expected, $result);
  7186. $result = $this->Form->control('phone');
  7187. $expected = [
  7188. 'label' => ['for' => 'phone'],
  7189. 'Phone',
  7190. '/label',
  7191. 'input' => [
  7192. 'id' => 'phone',
  7193. 'name' => 'phone',
  7194. 'type' => 'tel',
  7195. 'value' => '',
  7196. 'maxlength' => 255,
  7197. 'required' => 'required',
  7198. 'data-message' => 'This field cannot be left empty',
  7199. ],
  7200. ];
  7201. $this->assertHtml($expected, $result);
  7202. $result = $this->Form->control('email', [
  7203. 'templateVars' => [
  7204. 'custom' => 'data-custom="1"',
  7205. ],
  7206. ]);
  7207. $expected = [
  7208. 'label' => ['for' => 'email'],
  7209. 'Email',
  7210. '/label',
  7211. 'input' => [
  7212. 'id' => 'email',
  7213. 'name' => 'email',
  7214. 'type' => 'email',
  7215. 'value' => '',
  7216. 'maxlength' => 255,
  7217. 'required' => 'required',
  7218. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7219. 'data-custom' => '1',
  7220. ],
  7221. ];
  7222. $this->assertHtml($expected, $result);
  7223. $this->Form->setConfig('autoSetCustomValidity', true);
  7224. }
  7225. /**
  7226. * testRequiredAttribute method
  7227. *
  7228. * Tests that formhelper sets required attributes.
  7229. *
  7230. * @return void
  7231. */
  7232. public function testRequiredAttribute()
  7233. {
  7234. $this->article['required'] = [
  7235. 'title' => true,
  7236. 'body' => false,
  7237. ];
  7238. $this->Form->create($this->article);
  7239. $result = $this->Form->control('title');
  7240. $expected = [
  7241. 'div' => ['class' => 'input text required'],
  7242. 'label' => ['for' => 'title'],
  7243. 'Title',
  7244. '/label',
  7245. 'input' => [
  7246. 'type' => 'text',
  7247. 'name' => 'title',
  7248. 'id' => 'title',
  7249. 'required' => 'required',
  7250. 'data-validity-message' => 'This field cannot be left empty',
  7251. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7252. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7253. ],
  7254. '/div',
  7255. ];
  7256. $this->assertHtml($expected, $result);
  7257. $result = $this->Form->control('title', ['required' => false]);
  7258. $this->assertStringNotContainsString('required', $result);
  7259. $result = $this->Form->control('body');
  7260. $expected = [
  7261. 'div' => ['class' => 'input text'],
  7262. 'label' => ['for' => 'body'],
  7263. 'Body',
  7264. '/label',
  7265. 'input' => [
  7266. 'type' => 'text',
  7267. 'name' => 'body',
  7268. 'id' => 'body',
  7269. ],
  7270. '/div',
  7271. ];
  7272. $this->assertHtml($expected, $result);
  7273. $result = $this->Form->control('body', ['required' => true]);
  7274. $this->assertStringContainsString('required', $result);
  7275. }
  7276. /**
  7277. * testControlsNotNested method
  7278. *
  7279. * Tests that it is possible to put inputs outside of the label.
  7280. *
  7281. * @return void
  7282. */
  7283. public function testControlsNotNested()
  7284. {
  7285. $this->Form->setTemplates([
  7286. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7287. 'formGroup' => '{{input}}{{label}}',
  7288. ]);
  7289. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7290. $expected = [
  7291. 'div' => ['class' => 'input checkbox'],
  7292. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7293. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7294. 'label' => ['for' => 'foo'],
  7295. 'Foo',
  7296. '/label',
  7297. '/div',
  7298. ];
  7299. $this->assertHtml($expected, $result);
  7300. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7301. $expected = [
  7302. 'div' => ['class' => 'input checkbox'],
  7303. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7304. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7305. '/div',
  7306. ];
  7307. $this->assertHtml($expected, $result);
  7308. $result = $this->Form->control('confirm', [
  7309. 'type' => 'radio',
  7310. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7311. ]);
  7312. $expected = [
  7313. 'div' => ['class' => 'input radio'],
  7314. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7315. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7316. ['label' => ['for' => 'confirm-y']],
  7317. 'Yes',
  7318. '/label',
  7319. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7320. ['label' => ['for' => 'confirm-n']],
  7321. 'No',
  7322. '/label',
  7323. '<label',
  7324. 'Confirm',
  7325. '/label',
  7326. '/div',
  7327. ];
  7328. $this->assertHtml($expected, $result);
  7329. $result = $this->Form->select('category', ['1', '2'], [
  7330. 'multiple' => 'checkbox',
  7331. 'name' => 'fish',
  7332. ]);
  7333. $expected = [
  7334. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7335. ['div' => ['class' => 'checkbox']],
  7336. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7337. ['label' => ['for' => 'fish-0']],
  7338. '1',
  7339. '/label',
  7340. '/div',
  7341. ['div' => ['class' => 'checkbox']],
  7342. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7343. ['label' => ['for' => 'fish-1']],
  7344. '2',
  7345. '/label',
  7346. '/div',
  7347. ];
  7348. $this->assertHtml($expected, $result);
  7349. }
  7350. /**
  7351. * testControlContainerTemplates method
  7352. *
  7353. * Test that *Container templates are used by input.
  7354. *
  7355. * @return void
  7356. */
  7357. public function testControlContainerTemplates()
  7358. {
  7359. $this->Form->setTemplates([
  7360. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7361. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7362. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7363. ]);
  7364. $this->article['errors'] = [
  7365. 'Article' => ['published' => 'error message'],
  7366. ];
  7367. $this->Form->create($this->article);
  7368. $result = $this->Form->control('accept', [
  7369. 'type' => 'checkbox',
  7370. ]);
  7371. $expected = [
  7372. 'div' => ['class' => 'check'],
  7373. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7374. 'label' => ['for' => 'accept'],
  7375. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7376. 'Accept',
  7377. '/label',
  7378. '/div',
  7379. ];
  7380. $this->assertHtml($expected, $result);
  7381. $result = $this->Form->control('accept', [
  7382. 'type' => 'radio',
  7383. 'options' => ['Y', 'N'],
  7384. ]);
  7385. $this->assertStringContainsString('<div class="rad">', $result);
  7386. $result = $this->Form->control('Article.published', [
  7387. 'type' => 'radio',
  7388. 'options' => ['Y', 'N'],
  7389. ]);
  7390. $this->assertStringContainsString('<div class="rad err">', $result);
  7391. }
  7392. /**
  7393. * testFormGroupTemplates method
  7394. *
  7395. * Test that *Container templates are used by input.
  7396. *
  7397. * @return void
  7398. */
  7399. public function testFormGroupTemplates()
  7400. {
  7401. $this->Form->setTemplates([
  7402. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7403. ]);
  7404. $this->Form->create($this->article);
  7405. $result = $this->Form->control('accept', [
  7406. 'type' => 'radio',
  7407. 'options' => ['Y', 'N'],
  7408. ]);
  7409. $this->assertStringContainsString('<div class="radio">', $result);
  7410. }
  7411. /**
  7412. * testResetTemplates method
  7413. *
  7414. * Test resetting templates.
  7415. *
  7416. * @return void
  7417. */
  7418. public function testResetTemplates()
  7419. {
  7420. $this->Form->setTemplates(['input' => '<input/>']);
  7421. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7422. $this->Form->resetTemplates();
  7423. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7424. }
  7425. /**
  7426. * testContext method
  7427. *
  7428. * Test the context method.
  7429. *
  7430. * @return void
  7431. */
  7432. public function testContext()
  7433. {
  7434. $result = $this->Form->context();
  7435. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7436. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7437. $this->assertSame($mock, $this->Form->context($mock));
  7438. $this->assertSame($mock, $this->Form->context());
  7439. }
  7440. /**
  7441. * testAutoDomId method
  7442. *
  7443. * @return void
  7444. */
  7445. public function testAutoDomId()
  7446. {
  7447. $result = $this->Form->text('field', ['id' => true]);
  7448. $expected = [
  7449. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7450. ];
  7451. $this->assertHtml($expected, $result);
  7452. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7453. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7454. $expected = [
  7455. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7456. ['label' => ['for' => 'field-0']],
  7457. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7458. 'option A',
  7459. '/label',
  7460. ['label' => ['for' => 'field-1']],
  7461. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7462. 'option B',
  7463. '/label',
  7464. ];
  7465. $this->assertHtml($expected, $result);
  7466. $result = $this->Form->select(
  7467. 'multi_field',
  7468. ['first', 'second'],
  7469. ['multiple' => 'checkbox', 'id' => true]
  7470. );
  7471. $expected = [
  7472. 'input' => [
  7473. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7474. ],
  7475. ['div' => ['class' => 'checkbox']],
  7476. ['label' => ['for' => 'multi-field-0']],
  7477. ['input' => [
  7478. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7479. 'value' => '0', 'id' => 'multi-field-0',
  7480. ]],
  7481. 'first',
  7482. '/label',
  7483. '/div',
  7484. ['div' => ['class' => 'checkbox']],
  7485. ['label' => ['for' => 'multi-field-1']],
  7486. ['input' => [
  7487. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7488. 'value' => '1', 'id' => 'multi-field-1',
  7489. ]],
  7490. 'second',
  7491. '/label',
  7492. '/div',
  7493. ];
  7494. $this->assertHtml($expected, $result);
  7495. }
  7496. /**
  7497. * Test the basic setters and getters for value sources
  7498. *
  7499. * @return void
  7500. */
  7501. public function testFormValueSourcesSettersGetters()
  7502. {
  7503. $this->View->setRequest($this->View->getRequest()
  7504. ->withData('id', '1')
  7505. ->withQueryParams(['id' => '2']));
  7506. $expected = ['context'];
  7507. $result = $this->Form->getValueSources();
  7508. $this->assertEquals($expected, $result);
  7509. $expected = ['query', 'data', 'context'];
  7510. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7511. $result = $this->Form->getValueSources();
  7512. $this->assertEquals($expected, $result);
  7513. $this->Form->setValueSources(['context']);
  7514. $expected = '1';
  7515. $result = $this->Form->getSourceValue('id');
  7516. $this->assertEquals($expected, $result);
  7517. $this->Form->setValueSources('query');
  7518. $expected = ['query'];
  7519. $result = $this->Form->getValueSources();
  7520. $this->assertEquals($expected, $result);
  7521. $expected = '2';
  7522. $result = $this->Form->getSourceValue('id');
  7523. $this->assertEquals($expected, $result);
  7524. $this->Form->setValueSources(['data']);
  7525. $expected = '1';
  7526. $result = $this->Form->getSourceValue('id');
  7527. $this->assertEquals($expected, $result);
  7528. $this->Form->setValueSources(['query', 'data']);
  7529. $expected = '2';
  7530. $result = $this->Form->getSourceValue('id');
  7531. $this->assertEquals($expected, $result);
  7532. }
  7533. /**
  7534. * Tests the different input rendering values based on sources values switching
  7535. *
  7536. * @return void
  7537. */
  7538. public function testFormValueSourcesSingleSwitchRendering()
  7539. {
  7540. $this->loadFixtures('Articles');
  7541. $articles = $this->getTableLocator()->get('Articles');
  7542. $article = new Article();
  7543. $articles->patchEntity($article, ['id' => '3']);
  7544. $this->Form->create($article);
  7545. $this->Form->setValueSources(['context']);
  7546. $result = $this->Form->control('id');
  7547. $expected = [
  7548. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7549. ];
  7550. $this->assertHtml($expected, $result);
  7551. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7552. $this->Form->setValueSources(['query']);
  7553. $result = $this->Form->control('id');
  7554. $expected = [
  7555. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7556. ];
  7557. $this->assertHtml($expected, $result);
  7558. $this->View->setRequest($this->View->getRequest()
  7559. ->withQueryParams(['id' => '5a'])
  7560. ->withData('id', '5b'));
  7561. $this->Form->setValueSources(['context']);
  7562. $this->Form->create($article);
  7563. $result = $this->Form->control('id');
  7564. $expected = [
  7565. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7566. ];
  7567. $this->assertHtml($expected, $result);
  7568. $this->Form->setValueSources(['data']);
  7569. $this->Form->create($article);
  7570. $result = $this->Form->control('id');
  7571. $expected = [
  7572. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7573. ];
  7574. $this->assertHtml($expected, $result);
  7575. $this->Form->setValueSources(['query']);
  7576. $result = $this->Form->control('id');
  7577. $expected = [
  7578. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7579. ];
  7580. $this->assertHtml($expected, $result);
  7581. }
  7582. /**
  7583. * Tests the different input rendering values based on sources values switching while supplying
  7584. * an entity (base context) and multiple sources (such as data, query)
  7585. *
  7586. * @return void
  7587. */
  7588. public function testFormValueSourcesListSwitchRendering()
  7589. {
  7590. $this->loadFixtures('Articles');
  7591. $articles = $this->getTableLocator()->get('Articles');
  7592. $article = new Article();
  7593. $articles->patchEntity($article, ['id' => '3']);
  7594. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7595. $this->Form->create($article);
  7596. $this->Form->setValueSources(['context', 'query']);
  7597. $result = $this->Form->control('id');
  7598. $expected = [
  7599. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7600. ];
  7601. $this->assertHtml($expected, $result);
  7602. $this->Form->setValueSources(['query', 'context']);
  7603. $result = $this->Form->control('id');
  7604. $expected = [
  7605. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7606. ];
  7607. $this->assertHtml($expected, $result);
  7608. $this->Form->setValueSources(['data', 'query', 'context']);
  7609. $result = $this->Form->control('id');
  7610. $expected = [
  7611. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7612. ];
  7613. $this->assertHtml($expected, $result);
  7614. $this->View->setRequest($this->View->getRequest()
  7615. ->withData('id', '8')
  7616. ->withQueryParams(['id' => '9']));
  7617. $this->Form->setValueSources(['data', 'query', 'context']);
  7618. $result = $this->Form->control('id');
  7619. $expected = [
  7620. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7621. ];
  7622. $this->assertHtml($expected, $result);
  7623. }
  7624. /**
  7625. * Test the different form input renderings based on values sources switchings through form options
  7626. *
  7627. * @return void
  7628. */
  7629. public function testFormValueSourcesSwitchViaOptionsRendering()
  7630. {
  7631. $this->loadFixtures('Articles');
  7632. $articles = $this->getTableLocator()->get('Articles');
  7633. $article = new Article();
  7634. $articles->patchEntity($article, ['id' => '3']);
  7635. $this->View->setRequest(
  7636. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7637. );
  7638. $this->Form->create($article, ['valueSources' => 'query']);
  7639. $result = $this->Form->control('id');
  7640. $expected = [
  7641. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7642. ];
  7643. $this->assertHtml($expected, $result);
  7644. $result = $this->Form->getSourceValue('id');
  7645. $this->assertSame('5', $result);
  7646. $this->Form->setValueSources(['context']);
  7647. $this->Form->create($article, ['valueSources' => 'query']);
  7648. $result = $this->Form->control('id');
  7649. $expected = [
  7650. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7651. ];
  7652. $this->assertHtml($expected, $result);
  7653. $result = $this->Form->getSourceValue('id');
  7654. $this->assertSame('5', $result);
  7655. $this->Form->setValueSources(['query']);
  7656. $this->Form->create($article, ['valueSources' => 'data']);
  7657. $result = $this->Form->control('id');
  7658. $expected = [
  7659. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7660. ];
  7661. $this->assertHtml($expected, $result);
  7662. $result = $this->Form->getSourceValue('id');
  7663. $this->assertSame('4', $result);
  7664. $this->Form->setValueSources(['query']);
  7665. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7666. $result = $this->Form->control('id');
  7667. $expected = [
  7668. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7669. ];
  7670. $this->assertHtml($expected, $result);
  7671. $result = $this->Form->getSourceValue('id');
  7672. $this->assertSame('4', $result);
  7673. }
  7674. /**
  7675. * Test the different form input renderings based on values sources switchings through form options
  7676. *
  7677. * @return void
  7678. */
  7679. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7680. {
  7681. $this->loadFixtures('Articles');
  7682. $articles = $this->getTableLocator()->get('Articles');
  7683. $article = new Article();
  7684. $articles->patchEntity($article, ['id' => '3']);
  7685. $this->View->setRequest(
  7686. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7687. );
  7688. $this->Form->setValueSources(['context'])
  7689. ->create($article, ['valueSources' => ['query', 'data']]);
  7690. $result = $this->Form->control('id');
  7691. $expected = [
  7692. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7693. ];
  7694. $this->assertHtml($expected, $result);
  7695. $result = $this->Form->getSourceValue('id');
  7696. $this->assertSame('11', $result);
  7697. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7698. $this->Form->setValueSources(['context'])
  7699. ->create($article, ['valueSources' => ['query', 'data']]);
  7700. $result = $this->Form->control('id');
  7701. $expected = [
  7702. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7703. ];
  7704. $this->assertHtml($expected, $result);
  7705. $result = $this->Form->getSourceValue('id');
  7706. $this->assertSame('10', $result);
  7707. }
  7708. /**
  7709. * Test the different form values sources resetting through From::end();
  7710. *
  7711. * @return void
  7712. */
  7713. public function testFormValueSourcesResetViaEnd()
  7714. {
  7715. $expected = ['context'];
  7716. $result = $this->Form->getValueSources();
  7717. $this->assertEquals($expected, $result);
  7718. $expected = ['query', 'context', 'data'];
  7719. $this->Form->setValueSources(['query', 'context', 'data']);
  7720. $result = $this->Form->getValueSources();
  7721. $this->assertEquals($expected, $result);
  7722. $this->Form->create();
  7723. $result = $this->Form->getValueSources();
  7724. $this->assertEquals($expected, $result);
  7725. $this->Form->end();
  7726. $result = $this->Form->getValueSources();
  7727. $this->assertEquals(['context'], $result);
  7728. }
  7729. /**
  7730. * Test sources values defaults handling
  7731. *
  7732. * @return void
  7733. */
  7734. public function testFormValueSourcesDefaults()
  7735. {
  7736. $this->View->setRequest(
  7737. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7738. );
  7739. $this->Form->create();
  7740. $result = $this->Form->password('password');
  7741. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7742. $this->assertHtml($expected, $result);
  7743. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7744. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7745. $this->assertHtml($expected, $result);
  7746. $this->Form->setValueSources('query');
  7747. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7748. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7749. $this->assertHtml($expected, $result);
  7750. $this->Form->setValueSources('data');
  7751. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7752. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7753. $this->assertHtml($expected, $result);
  7754. }
  7755. /**
  7756. * Test sources values schema defaults handling
  7757. *
  7758. * @return void
  7759. */
  7760. public function testSourcesValueDoesntExistPassThrough()
  7761. {
  7762. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7763. $articles = $this->getTableLocator()->get('Articles');
  7764. $entity = $articles->newEmptyEntity();
  7765. $this->Form->create($entity);
  7766. $this->Form->setValueSources(['query', 'context']);
  7767. $result = $this->Form->getSourceValue('category');
  7768. $this->assertSame('sesame-cookies', $result);
  7769. $this->Form->setValueSources(['context', 'query']);
  7770. $result = $this->Form->getSourceValue('category');
  7771. $this->assertSame('sesame-cookies', $result);
  7772. }
  7773. /**
  7774. * testNestedLabelInput method
  7775. *
  7776. * Test the `nestedInput` parameter
  7777. *
  7778. * @return void
  7779. */
  7780. public function testNestedLabelInput()
  7781. {
  7782. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7783. $expected = [
  7784. 'div' => ['class' => 'input text'],
  7785. 'label' => ['for' => 'foo'],
  7786. ['input' => [
  7787. 'type' => 'text',
  7788. 'name' => 'foo',
  7789. 'id' => 'foo',
  7790. ]],
  7791. 'Foo',
  7792. '/label',
  7793. '/div',
  7794. ];
  7795. $this->assertHtml($expected, $result);
  7796. }
  7797. /**
  7798. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7799. *
  7800. * This test makes sure `false` excludes the label from the render
  7801. *
  7802. * @return void
  7803. */
  7804. public function testControlLabelManipulationDisableLabels()
  7805. {
  7806. $result = $this->Form->control('test', [
  7807. 'type' => 'radio',
  7808. 'options' => ['A', 'B'],
  7809. 'labelOptions' => false,
  7810. ]);
  7811. $expected = [
  7812. ['div' => ['class' => 'input radio']],
  7813. '<label',
  7814. 'Test',
  7815. '/label',
  7816. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7817. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7818. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7819. '/div',
  7820. ];
  7821. $this->assertHtml($expected, $result);
  7822. $result = $this->Form->control('checkbox1', [
  7823. 'label' => 'My checkboxes',
  7824. 'multiple' => 'checkbox',
  7825. 'type' => 'select',
  7826. 'options' => [
  7827. ['text' => 'First Checkbox', 'value' => 1],
  7828. ['text' => 'Second Checkbox', 'value' => 2],
  7829. ],
  7830. 'labelOptions' => false,
  7831. ]);
  7832. $expected = [
  7833. ['div' => ['class' => 'input select']],
  7834. ['label' => ['for' => 'checkbox1']],
  7835. 'My checkboxes',
  7836. '/label',
  7837. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7838. ['div' => ['class' => 'checkbox']],
  7839. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7840. '/div',
  7841. ['div' => ['class' => 'checkbox']],
  7842. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7843. '/div',
  7844. '/div',
  7845. ];
  7846. $this->assertHtml($expected, $result);
  7847. }
  7848. /**
  7849. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7850. *
  7851. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7852. * added to the class if checked.
  7853. *
  7854. * Also checks to make sure any custom attributes are rendered correctly
  7855. *
  7856. * @return void
  7857. */
  7858. public function testControlLabelManipulationRadios()
  7859. {
  7860. $result = $this->Form->control('test', [
  7861. 'type' => 'radio',
  7862. 'options' => ['A', 'B'],
  7863. 'labelOptions' => ['class' => 'custom-class'],
  7864. ]);
  7865. $expected = [
  7866. ['div' => ['class' => 'input radio']],
  7867. '<label',
  7868. 'Test',
  7869. '/label',
  7870. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7871. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7872. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7873. 'A',
  7874. '/label',
  7875. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7876. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7877. 'B',
  7878. '/label',
  7879. '/div',
  7880. ];
  7881. $this->assertHtml($expected, $result);
  7882. $result = $this->Form->control('test', [
  7883. 'type' => 'radio',
  7884. 'options' => ['A', 'B'],
  7885. 'value' => 1,
  7886. 'labelOptions' => ['class' => 'custom-class'],
  7887. ]);
  7888. $expected = [
  7889. ['div' => ['class' => 'input radio']],
  7890. '<label',
  7891. 'Test',
  7892. '/label',
  7893. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7894. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7895. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7896. 'A',
  7897. '/label',
  7898. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7899. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7900. 'B',
  7901. '/label',
  7902. '/div',
  7903. ];
  7904. $this->assertHtml($expected, $result);
  7905. $result = $this->Form->control('test', [
  7906. 'type' => 'radio',
  7907. 'options' => ['A', 'B'],
  7908. 'value' => 1,
  7909. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7910. ]);
  7911. $expected = [
  7912. ['div' => ['class' => 'input radio']],
  7913. '<label',
  7914. 'Test',
  7915. '/label',
  7916. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7917. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7918. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7919. 'A',
  7920. '/label',
  7921. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7922. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7923. 'B',
  7924. '/label',
  7925. '/div',
  7926. ];
  7927. $this->assertHtml($expected, $result);
  7928. $result = $this->Form->radio('test', ['A', 'B'], [
  7929. 'label' => [
  7930. 'class' => ['custom-class', 'another-class'],
  7931. 'data-name' => 'bob',
  7932. ],
  7933. 'value' => 1,
  7934. ]);
  7935. $expected = [
  7936. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  7937. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7938. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7939. 'A',
  7940. '/label',
  7941. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7942. ['input' => [
  7943. 'type' => 'radio',
  7944. 'name' => 'test',
  7945. 'value' => '1',
  7946. 'id' => 'test-1',
  7947. 'checked' => 'checked',
  7948. ]],
  7949. 'B',
  7950. '/label',
  7951. ];
  7952. $this->assertHtml($expected, $result);
  7953. }
  7954. /**
  7955. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7956. *
  7957. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7958. * added to the class if checked.
  7959. *
  7960. * Also checks to make sure any custom attributes are rendered correctly
  7961. *
  7962. * @return void
  7963. */
  7964. public function testControlLabelManipulationCheckboxes()
  7965. {
  7966. $result = $this->Form->control('checkbox1', [
  7967. 'label' => 'My checkboxes',
  7968. 'multiple' => 'checkbox',
  7969. 'type' => 'select',
  7970. 'options' => [
  7971. ['text' => 'First Checkbox', 'value' => 1],
  7972. ['text' => 'Second Checkbox', 'value' => 2],
  7973. ],
  7974. 'labelOptions' => ['class' => 'custom-class'],
  7975. 'value' => ['1'],
  7976. ]);
  7977. $expected = [
  7978. ['div' => ['class' => 'input select']],
  7979. ['label' => ['for' => 'checkbox1']],
  7980. 'My checkboxes',
  7981. '/label',
  7982. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7983. ['div' => ['class' => 'checkbox']],
  7984. ['label' => [
  7985. 'class' => 'custom-class selected',
  7986. 'for' => 'checkbox1-1',
  7987. ]],
  7988. ['input' => [
  7989. 'type' => 'checkbox',
  7990. 'name' => 'checkbox1[]',
  7991. 'value' => '1',
  7992. 'id' => 'checkbox1-1',
  7993. 'checked' => 'checked',
  7994. ]],
  7995. 'First Checkbox',
  7996. '/label',
  7997. '/div',
  7998. ['div' => ['class' => 'checkbox']],
  7999. ['label' => [
  8000. 'class' => 'custom-class',
  8001. 'for' => 'checkbox1-2',
  8002. ]],
  8003. ['input' => [
  8004. 'type' => 'checkbox',
  8005. 'name' => 'checkbox1[]',
  8006. 'value' => '2',
  8007. 'id' => 'checkbox1-2',
  8008. ]],
  8009. 'Second Checkbox',
  8010. '/label',
  8011. '/div',
  8012. '/div',
  8013. ];
  8014. $this->assertHtml($expected, $result);
  8015. $result = $this->Form->control('checkbox1', [
  8016. 'label' => 'My checkboxes',
  8017. 'multiple' => 'checkbox',
  8018. 'type' => 'select',
  8019. 'options' => [
  8020. ['text' => 'First Checkbox', 'value' => 1],
  8021. ['text' => 'Second Checkbox', 'value' => 2],
  8022. ],
  8023. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8024. 'value' => ['1'],
  8025. ]);
  8026. $expected = [
  8027. ['div' => ['class' => 'input select']],
  8028. ['label' => ['for' => 'checkbox1']],
  8029. 'My checkboxes',
  8030. '/label',
  8031. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8032. ['div' => ['class' => 'checkbox']],
  8033. ['label' => [
  8034. 'class' => 'custom-class another-class selected',
  8035. 'data-name' => 'bob',
  8036. 'for' => 'checkbox1-1',
  8037. ]],
  8038. ['input' => [
  8039. 'type' => 'checkbox',
  8040. 'name' => 'checkbox1[]',
  8041. 'value' => '1',
  8042. 'id' => 'checkbox1-1',
  8043. 'checked' => 'checked',
  8044. ]],
  8045. 'First Checkbox',
  8046. '/label',
  8047. '/div',
  8048. ['div' => ['class' => 'checkbox']],
  8049. ['label' => [
  8050. 'class' => 'custom-class another-class',
  8051. 'data-name' => 'bob',
  8052. 'for' => 'checkbox1-2',
  8053. ]],
  8054. ['input' => [
  8055. 'type' => 'checkbox',
  8056. 'name' => 'checkbox1[]',
  8057. 'value' => '2',
  8058. 'id' => 'checkbox1-2',
  8059. ]],
  8060. 'Second Checkbox',
  8061. '/label',
  8062. '/div',
  8063. '/div',
  8064. ];
  8065. $this->assertHtml($expected, $result);
  8066. }
  8067. /**
  8068. * testControlMaxLengthArrayContext method
  8069. *
  8070. * Test control() with maxlength attribute in Array Context.
  8071. *
  8072. * @return void
  8073. */
  8074. public function testControlMaxLengthArrayContext()
  8075. {
  8076. $this->article['schema'] = [
  8077. 'title' => ['length' => 10],
  8078. ];
  8079. $this->Form->create($this->article);
  8080. $result = $this->Form->control('title');
  8081. $expected = [
  8082. 'div' => ['class'],
  8083. 'label' => ['for'],
  8084. 'Title',
  8085. '/label',
  8086. 'input' => [
  8087. 'id',
  8088. 'name' => 'title',
  8089. 'type' => 'text',
  8090. 'required' => 'required',
  8091. 'maxlength' => 10,
  8092. 'data-validity-message' => 'This field cannot be left empty',
  8093. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8094. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8095. ],
  8096. '/div',
  8097. ];
  8098. $this->assertHtml($expected, $result);
  8099. }
  8100. /**
  8101. * testControlMaxLengthEntityContext method
  8102. *
  8103. * Test control() with maxlength attribute in Entity Context.
  8104. *
  8105. * @return void
  8106. */
  8107. public function testControlMaxLengthEntityContext()
  8108. {
  8109. $this->article['schema']['title']['length'] = 45;
  8110. $validator = new Validator();
  8111. $validator->maxLength('title', 10);
  8112. $article = new EntityContext(
  8113. new ServerRequest(),
  8114. [
  8115. 'entity' => new Entity($this->article),
  8116. 'table' => new Table([
  8117. 'alias' => 'Articles',
  8118. 'schema' => $this->article['schema'],
  8119. 'validator' => $validator,
  8120. ]),
  8121. ]
  8122. );
  8123. $this->Form->create($article);
  8124. $result = $this->Form->control('title');
  8125. $expected = [
  8126. 'div' => ['class'],
  8127. 'label' => ['for'],
  8128. 'Title',
  8129. '/label',
  8130. 'input' => [
  8131. 'id',
  8132. 'name' => 'title',
  8133. 'type' => 'text',
  8134. 'required' => 'required',
  8135. 'maxlength' => 10,
  8136. 'data-validity-message' => 'This field cannot be left empty',
  8137. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8138. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8139. ],
  8140. '/div',
  8141. ];
  8142. $this->assertHtml($expected, $result);
  8143. $this->article['schema']['title']['length'] = 45;
  8144. $validator = new Validator();
  8145. $validator->maxLength('title', 55);
  8146. $article = new EntityContext(
  8147. new ServerRequest(),
  8148. [
  8149. 'entity' => new Entity($this->article),
  8150. 'table' => new Table([
  8151. 'schema' => $this->article['schema'],
  8152. 'validator' => $validator,
  8153. 'alias' => 'Articles',
  8154. ]),
  8155. ]
  8156. );
  8157. $this->Form->create($article);
  8158. $result = $this->Form->control('title');
  8159. $expected = [
  8160. 'div' => ['class'],
  8161. 'label' => ['for'],
  8162. 'Title',
  8163. '/label',
  8164. 'input' => [
  8165. 'id',
  8166. 'name' => 'title',
  8167. 'type' => 'text',
  8168. 'required' => 'required',
  8169. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8170. 'data-validity-message' => 'This field cannot be left empty',
  8171. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8172. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8173. ],
  8174. '/div',
  8175. ];
  8176. $this->assertHtml($expected, $result);
  8177. $this->article['schema']['title']['length'] = 45;
  8178. $validator = new Validator();
  8179. $validator->maxLength('title', 55);
  8180. $article = new EntityContext(
  8181. new ServerRequest(),
  8182. [
  8183. 'entity' => new Entity($this->article),
  8184. 'table' => new Table([
  8185. 'schema' => $this->article['schema'],
  8186. 'validator' => $validator,
  8187. 'alias' => 'Articles',
  8188. ]),
  8189. ]
  8190. );
  8191. $this->Form->create($article);
  8192. $result = $this->Form->control('title', ['maxlength' => 10]);
  8193. $expected = [
  8194. 'div' => ['class'],
  8195. 'label' => ['for'],
  8196. 'Title',
  8197. '/label',
  8198. 'input' => [
  8199. 'id',
  8200. 'name' => 'title',
  8201. 'type' => 'text',
  8202. 'required' => 'required',
  8203. 'maxlength' => 10, // Length set in options should take highest precedence.
  8204. 'data-validity-message' => 'This field cannot be left empty',
  8205. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8206. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8207. ],
  8208. '/div',
  8209. ];
  8210. $this->assertHtml($expected, $result);
  8211. }
  8212. /**
  8213. * testControlMinMaxLengthEntityContext method
  8214. *
  8215. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8216. *
  8217. * @return void
  8218. */
  8219. public function testControlMinMaxLengthEntityContext()
  8220. {
  8221. $validator = new Validator();
  8222. $validator->maxLength('title', 10);
  8223. $article = new EntityContext(
  8224. new ServerRequest(),
  8225. [
  8226. 'entity' => new Entity($this->article),
  8227. 'table' => new Table([
  8228. 'alias' => 'Articles',
  8229. 'schema' => $this->article['schema'],
  8230. 'validator' => $validator,
  8231. ]),
  8232. ]
  8233. );
  8234. $this->Form->create($article);
  8235. $result = $this->Form->control('title');
  8236. $expected = [
  8237. 'div' => ['class'],
  8238. 'label' => ['for'],
  8239. 'Title',
  8240. '/label',
  8241. 'input' => [
  8242. 'id',
  8243. 'name' => 'title',
  8244. 'type' => 'text',
  8245. 'required' => 'required',
  8246. 'maxlength' => 10,
  8247. 'data-validity-message' => 'This field cannot be left empty',
  8248. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8249. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8250. ],
  8251. '/div',
  8252. ];
  8253. $this->assertHtml($expected, $result);
  8254. }
  8255. /**
  8256. * testControlMaxLengthFormContext method
  8257. *
  8258. * Test control() with maxlength attribute in Form Context.
  8259. *
  8260. * @return void
  8261. */
  8262. public function testControlMaxLengthFormContext()
  8263. {
  8264. $validator = new Validator();
  8265. $validator->maxLength('title', 10);
  8266. $form = new Form();
  8267. $form->setValidator('default', $validator);
  8268. $this->Form->create($form);
  8269. $result = $this->Form->control('title');
  8270. $expected = [
  8271. 'div' => ['class'],
  8272. 'label' => ['for'],
  8273. 'Title',
  8274. '/label',
  8275. 'input' => [
  8276. 'id',
  8277. 'name' => 'title',
  8278. 'type' => 'text',
  8279. 'required' => 'required',
  8280. 'maxlength' => 10,
  8281. 'data-validity-message' => 'This field cannot be left empty',
  8282. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8283. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8284. ],
  8285. '/div',
  8286. ];
  8287. $this->assertHtml($expected, $result);
  8288. }
  8289. }