FormHelperTest.php 294 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944
  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\ORM\Entity;
  23. use Cake\ORM\Table;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\Utility\Security;
  27. use Cake\Validation\Validator;
  28. use Cake\View\Form\EntityContext;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\View;
  31. use Cake\View\Widget\WidgetLocator;
  32. use TestApp\Model\Entity\Article;
  33. use TestApp\Model\Table\ContactsTable;
  34. use TestApp\Model\Table\ValidateUsersTable;
  35. /**
  36. * FormHelperTest class
  37. *
  38. * @property \Cake\View\Helper\FormHelper $Form
  39. * @property \Cake\View\View $View
  40. */
  41. class FormHelperTest extends TestCase
  42. {
  43. /**
  44. * Fixtures to be used
  45. *
  46. * @var array
  47. */
  48. public $fixtures = ['core.Articles', 'core.Comments'];
  49. /**
  50. * Do not load the fixtures by default
  51. *
  52. * @var bool
  53. */
  54. public $autoFixtures = false;
  55. /**
  56. * @var array
  57. */
  58. protected $article = [];
  59. /**
  60. * setUp method
  61. *
  62. * @return void
  63. */
  64. public function setUp()
  65. {
  66. parent::setUp();
  67. Configure::write('Config.language', 'eng');
  68. Configure::write('App.base', '');
  69. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  70. $request = new ServerRequest([
  71. 'webroot' => '',
  72. 'base' => '',
  73. 'url' => '/articles/add',
  74. 'params' => [
  75. 'controller' => 'articles',
  76. 'action' => 'add',
  77. ],
  78. ]);
  79. $this->View = new View($request);
  80. $this->Form = new FormHelper($this->View);
  81. $this->dateRegex = [
  82. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  83. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  84. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  85. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  86. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  87. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  88. ];
  89. $this->article = [
  90. 'schema' => [
  91. 'id' => ['type' => 'integer'],
  92. 'author_id' => ['type' => 'integer', 'null' => true],
  93. 'title' => ['type' => 'string', 'null' => true],
  94. 'body' => 'text',
  95. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  96. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  97. ],
  98. 'required' => [
  99. 'author_id' => true,
  100. 'title' => true,
  101. ],
  102. ];
  103. Security::setSalt('foo!');
  104. Router::connect('/:controller', ['action' => 'index']);
  105. Router::connect('/:controller/:action/*');
  106. }
  107. /**
  108. * tearDown method
  109. *
  110. * @return void
  111. */
  112. public function tearDown()
  113. {
  114. parent::tearDown();
  115. unset($this->Form, $this->Controller, $this->View);
  116. $this->getTableLocator()->clear();
  117. }
  118. /**
  119. * Test construct() with the templates option.
  120. *
  121. * @return void
  122. */
  123. public function testConstructTemplatesFile()
  124. {
  125. $helper = new FormHelper($this->View, [
  126. 'templates' => 'htmlhelper_tags',
  127. ]);
  128. $result = $helper->control('name');
  129. $this->assertContains('<input', $result);
  130. }
  131. /**
  132. * Test that when specifying custom widgets the config array for that widget
  133. * is overwritten instead of merged.
  134. *
  135. * @return void
  136. */
  137. public function testConstructWithWidgets()
  138. {
  139. $config = [
  140. 'widgets' => [
  141. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  142. ],
  143. ];
  144. $helper = new FormHelper($this->View, $config);
  145. $locator = $helper->getWidgetLocator();
  146. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  147. }
  148. /**
  149. * Test that when specifying custom widgets config file and it should be
  150. * added to widgets array. WidgetLocator will load widgets in constructor.
  151. *
  152. * @return void
  153. */
  154. public function testConstructWithWidgetsConfig()
  155. {
  156. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  157. $locator = $helper->getWidgetLocator();
  158. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  159. }
  160. /**
  161. * Test setting the widget locator
  162. *
  163. * @return void
  164. */
  165. public function testSetAndGetWidgetLocator()
  166. {
  167. $helper = new FormHelper($this->View);
  168. $locator = new WidgetLocator($helper->templater(), $this->View);
  169. $helper->setWidgetLocator($locator);
  170. $this->assertSame($locator, $helper->getWidgetLocator());
  171. }
  172. /**
  173. * Test overridding grouped input types which controls generation of "for"
  174. * attribute of labels.
  175. *
  176. * @return void
  177. */
  178. public function testConstructWithGroupedInputTypes()
  179. {
  180. $helper = new FormHelper($this->View, [
  181. 'groupedInputTypes' => ['radio'],
  182. ]);
  183. $result = $helper->control('when', ['type' => 'datetime-local']);
  184. $this->assertContains('<label for="when">When</label>', $result);
  185. }
  186. /**
  187. * Test registering a new widget class and rendering it.
  188. *
  189. * @return void
  190. */
  191. public function testAddWidgetAndRenderWidget()
  192. {
  193. $data = [
  194. 'val' => 1,
  195. ];
  196. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  197. $this->Form->addWidget('test', $mock);
  198. $mock->expects($this->once())
  199. ->method('render')
  200. ->with($data)
  201. ->will($this->returnValue('HTML'));
  202. $result = $this->Form->widget('test', $data);
  203. $this->assertEquals('HTML', $result);
  204. }
  205. /**
  206. * Test that secureFields() of widget is called after calling render(),
  207. * not before.
  208. *
  209. * @return void
  210. */
  211. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  212. {
  213. $data = [
  214. 'val' => 1,
  215. 'name' => 'test',
  216. ];
  217. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  218. $this->Form->addWidget('test', $mock);
  219. $mock->expects($this->at(0))
  220. ->method('render')
  221. ->with($data)
  222. ->will($this->returnValue('HTML'));
  223. $mock->expects($this->at(1))
  224. ->method('secureFields')
  225. ->with($data)
  226. ->will($this->returnValue(['test']));
  227. $result = $this->Form->widget('test', $data + ['secure' => true]);
  228. $this->assertEquals('HTML', $result);
  229. }
  230. /**
  231. * Test that empty string is not added to secure fields list when
  232. * rendering input widget without name.
  233. *
  234. * @return void
  235. */
  236. public function testRenderingWidgetWithEmptyName()
  237. {
  238. $this->assertEquals([], $this->Form->fields);
  239. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  240. $this->assertEquals('<select name=""></select>', $result);
  241. $this->assertEquals([], $this->Form->fields);
  242. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  243. $this->assertEquals('<select name="0"></select>', $result);
  244. $this->assertEquals(['0'], $this->Form->fields);
  245. }
  246. /**
  247. * Test registering an invalid widget class.
  248. *
  249. * @return void
  250. */
  251. public function testAddWidgetInvalid()
  252. {
  253. $this->expectException(\RuntimeException::class);
  254. $mock = new \StdClass();
  255. $this->Form->addWidget('test', $mock);
  256. $this->Form->widget('test');
  257. }
  258. /**
  259. * Test adding a new context class.
  260. *
  261. * @return void
  262. */
  263. public function testAddContextProvider()
  264. {
  265. $context = 'My data';
  266. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  267. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  268. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  269. $this->assertEquals($context, $data['entity']);
  270. return $stub;
  271. });
  272. $this->Form->create($context);
  273. $result = $this->Form->context();
  274. $this->assertSame($stub, $result);
  275. }
  276. /**
  277. * Test replacing a context class.
  278. *
  279. * @return void
  280. */
  281. public function testAddContextProviderReplace()
  282. {
  283. $entity = new Article();
  284. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  285. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  286. return $stub;
  287. });
  288. $this->Form->create($entity);
  289. $result = $this->Form->context();
  290. $this->assertSame($stub, $result);
  291. }
  292. /**
  293. * Test overriding a context class.
  294. *
  295. * @return void
  296. */
  297. public function testAddContextProviderAdd()
  298. {
  299. $entity = new Article();
  300. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  301. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  302. if ($data['entity'] instanceof Entity) {
  303. return $stub;
  304. }
  305. });
  306. $this->Form->create($entity);
  307. $result = $this->Form->context();
  308. $this->assertSame($stub, $result);
  309. }
  310. /**
  311. * Test adding an invalid context class.
  312. *
  313. * @return void
  314. */
  315. public function testAddContextProviderInvalid()
  316. {
  317. $this->expectException(\RuntimeException::class);
  318. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  319. $context = 'My data';
  320. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  321. return new \StdClass();
  322. });
  323. $this->Form->create($context);
  324. }
  325. /**
  326. * Provides context options for create().
  327. *
  328. * @return array
  329. */
  330. public function contextSelectionProvider()
  331. {
  332. $entity = new Article();
  333. $collection = new Collection([$entity]);
  334. $emptyCollection = new Collection([]);
  335. $arrayObject = new \ArrayObject([]);
  336. $data = [
  337. 'schema' => [
  338. 'title' => ['type' => 'string'],
  339. ],
  340. ];
  341. $form = new Form();
  342. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  343. return [
  344. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  345. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  346. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  347. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  348. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  349. 'form' => [$form, 'Cake\View\Form\FormContext'],
  350. 'none' => [null, 'Cake\View\Form\NullContext'],
  351. 'false' => [false, 'Cake\View\Form\NullContext'],
  352. 'custom' => [$custom, get_class($custom)],
  353. ];
  354. }
  355. /**
  356. * Test default context selection in create()
  357. *
  358. * @dataProvider contextSelectionProvider
  359. * @return void
  360. */
  361. public function testCreateContextSelectionBuiltIn($data, $class)
  362. {
  363. $this->loadFixtures('Articles');
  364. $this->Form->create($data);
  365. $this->assertInstanceOf($class, $this->Form->context());
  366. }
  367. /**
  368. * Data provider for type option.
  369. *
  370. * @return array
  371. */
  372. public static function requestTypeProvider()
  373. {
  374. return [
  375. // type, method, override
  376. ['post', 'post', 'POST'],
  377. ['put', 'post', 'PUT'],
  378. ['patch', 'post', 'PATCH'],
  379. ['delete', 'post', 'DELETE'],
  380. ];
  381. }
  382. /**
  383. * Test creating file forms.
  384. *
  385. * @return void
  386. */
  387. public function testCreateFile()
  388. {
  389. $encoding = strtolower(Configure::read('App.encoding'));
  390. $result = $this->Form->create(false, ['type' => 'file']);
  391. $expected = [
  392. 'form' => [
  393. 'method' => 'post', 'action' => '/articles/add',
  394. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  395. ],
  396. 'div' => ['style' => 'display:none;'],
  397. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  398. '/div',
  399. ];
  400. $this->assertHtml($expected, $result);
  401. }
  402. /**
  403. * Test creating GET forms.
  404. *
  405. * @return void
  406. */
  407. public function testCreateGet()
  408. {
  409. $encoding = strtolower(Configure::read('App.encoding'));
  410. $result = $this->Form->create(false, ['type' => 'get']);
  411. $expected = ['form' => [
  412. 'method' => 'get', 'action' => '/articles/add',
  413. 'accept-charset' => $encoding,
  414. ]];
  415. $this->assertHtml($expected, $result);
  416. }
  417. /**
  418. * Test explicit method/enctype options.
  419. *
  420. * Explicit method overwrites inferred method from 'type'
  421. *
  422. * @return void
  423. */
  424. public function testCreateExplicitMethodEnctype()
  425. {
  426. $encoding = strtolower(Configure::read('App.encoding'));
  427. $result = $this->Form->create(false, [
  428. 'type' => 'get',
  429. 'method' => 'put',
  430. 'enctype' => 'multipart/form-data',
  431. ]);
  432. $expected = ['form' => [
  433. 'method' => 'put',
  434. 'action' => '/articles/add',
  435. 'enctype' => 'multipart/form-data',
  436. 'accept-charset' => $encoding,
  437. ]];
  438. $this->assertHtml($expected, $result);
  439. }
  440. /**
  441. * Test create() with the templates option.
  442. *
  443. * @return void
  444. */
  445. public function testCreateTemplatesArray()
  446. {
  447. $result = $this->Form->create($this->article, [
  448. 'templates' => [
  449. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  450. ],
  451. ]);
  452. $expected = [
  453. 'form' => [
  454. 'class' => 'form-horizontal',
  455. 'method' => 'post',
  456. 'action' => '/articles/add',
  457. 'accept-charset' => 'utf-8',
  458. ],
  459. ];
  460. $this->assertHtml($expected, $result);
  461. }
  462. /**
  463. * Test create() with the templates option.
  464. *
  465. * @return void
  466. */
  467. public function testCreateTemplatesFile()
  468. {
  469. $result = $this->Form->create($this->article, [
  470. 'templates' => 'htmlhelper_tags',
  471. ]);
  472. $expected = [
  473. 'start form',
  474. 'div' => ['class' => 'hidden'],
  475. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  476. '/div',
  477. ];
  478. $this->assertHtml($expected, $result);
  479. }
  480. /**
  481. * Test that create() and end() restore templates.
  482. *
  483. * @return void
  484. */
  485. public function testCreateEndRestoreTemplates()
  486. {
  487. $this->Form->create($this->article, [
  488. 'templates' => ['input' => 'custom input element'],
  489. ]);
  490. $this->Form->end();
  491. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  492. }
  493. /**
  494. * Test using template vars in various templates used by control() method.
  495. *
  496. * @return void
  497. */
  498. public function testControlTemplateVars()
  499. {
  500. $result = $this->Form->control('text', [
  501. 'templates' => [
  502. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  503. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  504. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  505. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  506. ],
  507. 'templateVars' => [
  508. 'forinput' => 'in-input',
  509. 'forlabel' => 'in-label',
  510. 'forgroup' => 'in-group',
  511. 'forcontainer' => 'in-container',
  512. ],
  513. ]);
  514. $expected = [
  515. 'div' => ['class'],
  516. 'label' => ['for'],
  517. 'Text in-label',
  518. '/label',
  519. 'in-group',
  520. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  521. 'in-container',
  522. '/div',
  523. ];
  524. $this->assertHtml($expected, $result);
  525. }
  526. /**
  527. * Test ensuring template variables work in template files loaded
  528. * during control().
  529. *
  530. * @return void
  531. */
  532. public function testControlTemplatesFromFile()
  533. {
  534. $result = $this->Form->control('title', [
  535. 'templates' => 'test_templates',
  536. 'templateVars' => [
  537. 'forcontainer' => 'container-data',
  538. ],
  539. ]);
  540. $expected = [
  541. 'div' => ['class'],
  542. 'label' => ['for'],
  543. 'Title',
  544. '/label',
  545. 'input' => ['name', 'type' => 'text', 'id'],
  546. 'container-data',
  547. '/div',
  548. ];
  549. $this->assertHtml($expected, $result);
  550. }
  551. /**
  552. * Test using template vars in inputSubmit and submitContainer template.
  553. *
  554. * @return void
  555. */
  556. public function testSubmitTemplateVars()
  557. {
  558. $this->Form->setTemplates([
  559. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  560. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  561. ]);
  562. $result = $this->Form->submit('Submit', [
  563. 'templateVars' => [
  564. 'forinput' => 'in-input',
  565. 'forcontainer' => 'in-container',
  566. ],
  567. ]);
  568. $expected = [
  569. 'div' => ['class'],
  570. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  571. 'in-container',
  572. '/div',
  573. ];
  574. $this->assertHtml($expected, $result);
  575. }
  576. /**
  577. * test the create() method
  578. *
  579. * @dataProvider requestTypeProvider
  580. * @return void
  581. */
  582. public function testCreateTypeOptions($type, $method, $override)
  583. {
  584. $encoding = strtolower(Configure::read('App.encoding'));
  585. $result = $this->Form->create(false, ['type' => $type]);
  586. $expected = [
  587. 'form' => [
  588. 'method' => $method, 'action' => '/articles/add',
  589. 'accept-charset' => $encoding,
  590. ],
  591. 'div' => ['style' => 'display:none;'],
  592. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  593. '/div',
  594. ];
  595. $this->assertHtml($expected, $result);
  596. }
  597. /**
  598. * Test using template vars in Create (formStart template)
  599. *
  600. * @return void
  601. */
  602. public function testCreateTemplateVars()
  603. {
  604. $result = $this->Form->create($this->article, [
  605. 'templates' => [
  606. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  607. ],
  608. 'templateVars' => ['header' => 'headertext'],
  609. ]);
  610. $expected = [
  611. 'h4' => ['class'],
  612. 'headertext',
  613. '/h4',
  614. 'form' => [
  615. 'method' => 'post',
  616. 'action' => '/articles/add',
  617. 'accept-charset' => 'utf-8',
  618. ],
  619. ];
  620. $this->assertHtml($expected, $result);
  621. }
  622. /**
  623. * Test opening a form for an update operation.
  624. *
  625. * @return void
  626. */
  627. public function testCreateUpdateForm()
  628. {
  629. $encoding = strtolower(Configure::read('App.encoding'));
  630. $this->View->setRequest($this->View->getRequest()
  631. ->withRequestTarget('/articles/edit/1')
  632. ->withParam('action', 'edit'));
  633. $this->article['defaults']['id'] = 1;
  634. $result = $this->Form->create($this->article);
  635. $expected = [
  636. 'form' => [
  637. 'method' => 'post',
  638. 'action' => '/articles/edit/1',
  639. 'accept-charset' => $encoding,
  640. ],
  641. 'div' => ['style' => 'display:none;'],
  642. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  643. '/div',
  644. ];
  645. $this->assertHtml($expected, $result);
  646. }
  647. /**
  648. * test create() with automatic url generation
  649. *
  650. * @return void
  651. */
  652. public function testCreateAutoUrl()
  653. {
  654. $encoding = strtolower(Configure::read('App.encoding'));
  655. $this->View->setRequest($this->View->getRequest()
  656. ->withRequestTarget('/articles/delete/10')
  657. ->withParam('action', 'delete'));
  658. $result = $this->Form->create($this->article);
  659. $expected = [
  660. 'form' => [
  661. 'method' => 'post', 'action' => '/articles/delete/10',
  662. 'accept-charset' => $encoding,
  663. ],
  664. 'div' => ['style' => 'display:none;'],
  665. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  666. '/div',
  667. ];
  668. $this->assertHtml($expected, $result);
  669. $this->article['defaults'] = ['id' => 1];
  670. $this->View->setRequest($this->View->getRequest()
  671. ->withRequestTarget('/articles/edit/1')
  672. ->withParam('action', 'delete'));
  673. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  674. $expected = [
  675. 'form' => [
  676. 'method' => 'post',
  677. 'action' => '/articles/edit/1',
  678. 'accept-charset' => $encoding,
  679. ],
  680. 'div' => ['style' => 'display:none;'],
  681. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  682. '/div',
  683. ];
  684. $this->assertHtml($expected, $result);
  685. $this->View->setRequest($this->View->getRequest()
  686. ->withParam('action', 'add'));
  687. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  688. $expected = [
  689. 'form' => [
  690. 'method' => 'post',
  691. 'action' => '/articles/publish/1',
  692. 'accept-charset' => $encoding,
  693. ],
  694. 'div' => ['style' => 'display:none;'],
  695. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  696. '/div',
  697. ];
  698. $this->assertHtml($expected, $result);
  699. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  700. $expected = [
  701. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  702. 'div' => ['style' => 'display:none;'],
  703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  704. '/div',
  705. ];
  706. $this->assertHtml($expected, $result);
  707. $this->View->setRequest($this->View->getRequest()
  708. ->withParam('controller', 'Pages'));
  709. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  710. $expected = [
  711. 'form' => [
  712. 'method' => 'post', 'action' => '/Pages/signup/1',
  713. 'accept-charset' => $encoding,
  714. ],
  715. 'div' => ['style' => 'display:none;'],
  716. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  717. '/div',
  718. ];
  719. $this->assertHtml($expected, $result);
  720. }
  721. /**
  722. * Test create() with no URL (no "action" attribute for <form> tag)
  723. *
  724. * @return void
  725. */
  726. public function testCreateNoUrl()
  727. {
  728. $result = $this->Form->create(false, ['url' => false]);
  729. $expected = [
  730. 'form' => [
  731. 'method' => 'post',
  732. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  733. ],
  734. 'div' => ['style' => 'display:none;'],
  735. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  736. '/div',
  737. ];
  738. $this->assertHtml($expected, $result);
  739. }
  740. /**
  741. * test create() with a custom route
  742. *
  743. * @return void
  744. */
  745. public function testCreateCustomRoute()
  746. {
  747. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  748. $encoding = strtolower(Configure::read('App.encoding'));
  749. $this->View->setRequest($this->View->getRequest()
  750. ->withParam('controller', 'users'));
  751. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  752. $expected = [
  753. 'form' => [
  754. 'method' => 'post', 'action' => '/login',
  755. 'accept-charset' => $encoding,
  756. ],
  757. 'div' => ['style' => 'display:none;'],
  758. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  759. '/div',
  760. ];
  761. $this->assertHtml($expected, $result);
  762. Router::connect(
  763. '/new-article',
  764. ['controller' => 'articles', 'action' => 'myaction'],
  765. ['_name' => 'my-route']
  766. );
  767. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  768. $expected = [
  769. 'form' => [
  770. 'method' => 'post', 'action' => '/new-article',
  771. 'accept-charset' => $encoding,
  772. ],
  773. 'div' => ['style' => 'display:none;'],
  774. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  775. '/div',
  776. ];
  777. $this->assertHtml($expected, $result);
  778. }
  779. /**
  780. * test automatic accept-charset overriding
  781. *
  782. * @return void
  783. */
  784. public function testCreateWithAcceptCharset()
  785. {
  786. $result = $this->Form->create(
  787. $this->article,
  788. [
  789. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  790. ]
  791. );
  792. $expected = [
  793. 'form' => [
  794. 'method' => 'post', 'action' => '/articles',
  795. 'accept-charset' => 'iso-8859-1',
  796. ],
  797. 'div' => ['style' => 'display:none;'],
  798. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  799. '/div',
  800. ];
  801. $this->assertHtml($expected, $result);
  802. }
  803. /**
  804. * Test base form URL when url param is passed with multiple parameters (&)
  805. *
  806. */
  807. public function testCreateQueryStringRequest()
  808. {
  809. $encoding = strtolower(Configure::read('App.encoding'));
  810. $result = $this->Form->create($this->article, [
  811. 'type' => 'post',
  812. 'escape' => false,
  813. 'url' => [
  814. 'controller' => 'controller',
  815. 'action' => 'action',
  816. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  817. ],
  818. ]);
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post',
  822. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  823. 'accept-charset' => $encoding,
  824. ],
  825. 'div' => ['style' => 'display:none;'],
  826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  827. '/div',
  828. ];
  829. $this->assertHtml($expected, $result);
  830. $result = $this->Form->create($this->article, [
  831. 'type' => 'post',
  832. 'url' => [
  833. 'controller' => 'controller',
  834. 'action' => 'action',
  835. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  836. ],
  837. ]);
  838. $this->assertHtml($expected, $result);
  839. }
  840. /**
  841. * test that create() doesn't cause errors by multiple id's being in the primary key
  842. * as could happen with multiple select or checkboxes.
  843. *
  844. * @return void
  845. */
  846. public function testCreateWithMultipleIdInData()
  847. {
  848. $encoding = strtolower(Configure::read('App.encoding'));
  849. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  850. $result = $this->Form->create($this->article);
  851. $expected = [
  852. 'form' => [
  853. 'method' => 'post',
  854. 'action' => '/articles/add',
  855. 'accept-charset' => $encoding,
  856. ],
  857. 'div' => ['style' => 'display:none;'],
  858. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  859. '/div',
  860. ];
  861. $this->assertHtml($expected, $result);
  862. }
  863. /**
  864. * test that create() doesn't add in extra passed params.
  865. *
  866. * @return void
  867. */
  868. public function testCreatePassedArgs()
  869. {
  870. $encoding = strtolower(Configure::read('App.encoding'));
  871. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  872. $result = $this->Form->create($this->article, [
  873. 'type' => 'post',
  874. 'escape' => false,
  875. 'url' => [
  876. 'action' => 'edit',
  877. 'myparam',
  878. ],
  879. ]);
  880. $expected = [
  881. 'form' => [
  882. 'method' => 'post',
  883. 'action' => '/articles/edit/myparam',
  884. 'accept-charset' => $encoding,
  885. ],
  886. 'div' => ['style' => 'display:none;'],
  887. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  888. '/div',
  889. ];
  890. $this->assertHtml($expected, $result);
  891. }
  892. /**
  893. * test creating a get form, and get form inputs.
  894. *
  895. * @return void
  896. */
  897. public function testGetFormCreate()
  898. {
  899. $encoding = strtolower(Configure::read('App.encoding'));
  900. $result = $this->Form->create($this->article, ['type' => 'get']);
  901. $expected = ['form' => [
  902. 'method' => 'get', 'action' => '/articles/add',
  903. 'accept-charset' => $encoding,
  904. ]];
  905. $this->assertHtml($expected, $result);
  906. $result = $this->Form->text('title');
  907. $expected = ['input' => [
  908. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  909. ]];
  910. $this->assertHtml($expected, $result);
  911. $result = $this->Form->password('password');
  912. $expected = ['input' => [
  913. 'name' => 'password', 'type' => 'password',
  914. ]];
  915. $this->assertHtml($expected, $result);
  916. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  917. $result = $this->Form->text('user_form');
  918. $expected = ['input' => [
  919. 'name' => 'user_form', 'type' => 'text',
  920. ]];
  921. $this->assertHtml($expected, $result);
  922. }
  923. /**
  924. * test get form, and inputs when the model param is false
  925. *
  926. * @return void
  927. */
  928. public function testGetFormWithFalseModel()
  929. {
  930. $encoding = strtolower(Configure::read('App.encoding'));
  931. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  932. $result = $this->Form->create(false, [
  933. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  934. ]);
  935. $expected = ['form' => [
  936. 'method' => 'get', 'action' => '/contact_test/add',
  937. 'accept-charset' => $encoding,
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Form->text('reason');
  941. $expected = [
  942. 'input' => ['type' => 'text', 'name' => 'reason'],
  943. ];
  944. $this->assertHtml($expected, $result);
  945. }
  946. /**
  947. * testFormCreateWithSecurity method
  948. *
  949. * Test form->create() with security key.
  950. *
  951. * @return void
  952. */
  953. public function testCreateWithSecurity()
  954. {
  955. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  956. $encoding = strtolower(Configure::read('App.encoding'));
  957. $result = $this->Form->create($this->article, [
  958. 'url' => '/articles/publish',
  959. ]);
  960. $expected = [
  961. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  962. 'div' => ['style' => 'display:none;'],
  963. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  964. ['input' => [
  965. 'type' => 'hidden',
  966. 'name' => '_csrfToken',
  967. 'value' => 'testKey',
  968. 'autocomplete' => 'off',
  969. ]],
  970. '/div',
  971. ];
  972. $this->assertHtml($expected, $result);
  973. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  974. $expected['form']['id'] = 'MyForm';
  975. $this->assertHtml($expected, $result);
  976. }
  977. /**
  978. * testFormCreateGetNoSecurity method
  979. *
  980. * Test form->create() with no security key as its a get form
  981. *
  982. * @return void
  983. */
  984. public function testCreateEndGetNoSecurity()
  985. {
  986. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  987. $article = new Article();
  988. $result = $this->Form->create($article, [
  989. 'type' => 'get',
  990. 'url' => '/contacts/add',
  991. ]);
  992. $this->assertNotContains('testKey', $result);
  993. $result = $this->Form->end();
  994. $this->assertNotContains('testKey', $result);
  995. }
  996. /**
  997. * test that create() clears the fields property so it starts fresh
  998. *
  999. * @return void
  1000. */
  1001. public function testCreateClearingFields()
  1002. {
  1003. $this->Form->fields = ['model_id'];
  1004. $this->Form->create($this->article);
  1005. $this->assertEquals([], $this->Form->fields);
  1006. }
  1007. /**
  1008. * Tests form hash generation with model-less data
  1009. *
  1010. * @return void
  1011. */
  1012. public function testValidateHashNoModel()
  1013. {
  1014. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1015. $fields = ['anything'];
  1016. $result = $this->Form->secure($fields);
  1017. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1018. $this->assertContains($hash, $result);
  1019. }
  1020. /**
  1021. * Tests that hidden fields generated for checkboxes don't get locked
  1022. *
  1023. * @return void
  1024. */
  1025. public function testNoCheckboxLocking()
  1026. {
  1027. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1028. $this->assertSame([], $this->Form->fields);
  1029. $this->Form->checkbox('check', ['value' => '1']);
  1030. $this->assertSame($this->Form->fields, ['check']);
  1031. }
  1032. /**
  1033. * testFormSecurityFields method
  1034. *
  1035. * Test generation of secure form hash generation.
  1036. *
  1037. * @return void
  1038. */
  1039. public function testFormSecurityFields()
  1040. {
  1041. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1042. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1043. $result = $this->Form->secure($fields);
  1044. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1045. $hash .= ':' . 'Model.valid';
  1046. $hash = urlencode($hash);
  1047. $tokenDebug = urlencode(json_encode([
  1048. '',
  1049. $fields,
  1050. [],
  1051. ]));
  1052. $expected = [
  1053. 'div' => ['style' => 'display:none;'],
  1054. ['input' => [
  1055. 'type' => 'hidden',
  1056. 'name' => '_Token[fields]',
  1057. 'value' => $hash,
  1058. 'autocomplete' => 'off',
  1059. ]],
  1060. ['input' => [
  1061. 'type' => 'hidden',
  1062. 'name' => '_Token[unlocked]',
  1063. 'value' => '',
  1064. 'autocomplete' => 'off',
  1065. ]],
  1066. ['input' => [
  1067. 'type' => 'hidden',
  1068. 'name' => '_Token[debug]',
  1069. 'value' => $tokenDebug,
  1070. 'autocomplete' => 'off',
  1071. ]],
  1072. '/div',
  1073. ];
  1074. $this->assertHtml($expected, $result);
  1075. }
  1076. /**
  1077. * testFormSecurityFields method
  1078. *
  1079. * Test debug token is not generated if debug is false
  1080. *
  1081. * @return void
  1082. */
  1083. public function testFormSecurityFieldsNoDebugMode()
  1084. {
  1085. Configure::write('debug', false);
  1086. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1087. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1088. $result = $this->Form->secure($fields);
  1089. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1090. $hash .= ':' . 'Model.valid';
  1091. $hash = urlencode($hash);
  1092. $expected = [
  1093. 'div' => ['style' => 'display:none;'],
  1094. ['input' => [
  1095. 'type' => 'hidden',
  1096. 'name' => '_Token[fields]',
  1097. 'autocomplete' => 'off',
  1098. 'value' => $hash,
  1099. ]],
  1100. ['input' => [
  1101. 'type' => 'hidden',
  1102. 'name' => '_Token[unlocked]',
  1103. 'autocomplete' => 'off',
  1104. 'value' => '',
  1105. ]],
  1106. '/div',
  1107. ];
  1108. $this->assertHtml($expected, $result);
  1109. }
  1110. /**
  1111. * Tests correct generation of number fields for smallint
  1112. *
  1113. * @return void
  1114. */
  1115. public function testTextFieldGenerationForSmallint()
  1116. {
  1117. $this->article['schema'] = [
  1118. 'foo' => [
  1119. 'type' => 'smallinteger',
  1120. 'null' => false,
  1121. 'default' => null,
  1122. 'length' => 10,
  1123. ],
  1124. ];
  1125. $this->Form->create($this->article);
  1126. $result = $this->Form->control('foo');
  1127. $this->assertContains('class="input number"', $result);
  1128. $this->assertContains('type="number"', $result);
  1129. }
  1130. /**
  1131. * Tests correct generation of number fields for tinyint
  1132. *
  1133. * @return void
  1134. */
  1135. public function testTextFieldGenerationForTinyint()
  1136. {
  1137. $this->article['schema'] = [
  1138. 'foo' => [
  1139. 'type' => 'tinyinteger',
  1140. 'null' => false,
  1141. 'default' => null,
  1142. 'length' => 10,
  1143. ],
  1144. ];
  1145. $this->Form->create($this->article);
  1146. $result = $this->Form->control('foo');
  1147. $this->assertContains('class="input number"', $result);
  1148. $this->assertContains('type="number"', $result);
  1149. }
  1150. /**
  1151. * Tests correct generation of number fields for double and float fields
  1152. *
  1153. * @return void
  1154. */
  1155. public function testTextFieldGenerationForFloats()
  1156. {
  1157. $this->article['schema'] = [
  1158. 'foo' => [
  1159. 'type' => 'float',
  1160. 'null' => false,
  1161. 'default' => null,
  1162. 'length' => 10,
  1163. ],
  1164. ];
  1165. $this->Form->create($this->article);
  1166. $result = $this->Form->control('foo');
  1167. $expected = [
  1168. 'div' => ['class' => 'input number'],
  1169. 'label' => ['for' => 'foo'],
  1170. 'Foo',
  1171. '/label',
  1172. ['input' => [
  1173. 'type' => 'number',
  1174. 'name' => 'foo',
  1175. 'id' => 'foo',
  1176. 'step' => 'any',
  1177. ]],
  1178. '/div',
  1179. ];
  1180. $this->assertHtml($expected, $result);
  1181. $result = $this->Form->control('foo', ['step' => 0.5]);
  1182. $expected = [
  1183. 'div' => ['class' => 'input number'],
  1184. 'label' => ['for' => 'foo'],
  1185. 'Foo',
  1186. '/label',
  1187. ['input' => [
  1188. 'type' => 'number',
  1189. 'name' => 'foo',
  1190. 'id' => 'foo',
  1191. 'step' => '0.5',
  1192. ]],
  1193. '/div',
  1194. ];
  1195. $this->assertHtml($expected, $result);
  1196. }
  1197. /**
  1198. * Tests correct generation of number fields for integer fields
  1199. *
  1200. * @return void
  1201. */
  1202. public function testTextFieldTypeNumberGenerationForIntegers()
  1203. {
  1204. $this->getTableLocator()->get('Contacts', [
  1205. 'className' => ContactsTable::class,
  1206. ]);
  1207. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1208. $result = $this->Form->control('age');
  1209. $expected = [
  1210. 'div' => ['class' => 'input number'],
  1211. 'label' => ['for' => 'age'],
  1212. 'Age',
  1213. '/label',
  1214. ['input' => [
  1215. 'type' => 'number', 'name' => 'age',
  1216. 'id' => 'age',
  1217. ]],
  1218. '/div',
  1219. ];
  1220. $this->assertHtml($expected, $result);
  1221. }
  1222. /**
  1223. * Tests correct generation of file upload fields for binary fields
  1224. *
  1225. * @return void
  1226. */
  1227. public function testFileUploadFieldTypeGenerationForBinaries()
  1228. {
  1229. $table = $this->getTableLocator()->get('Contacts', [
  1230. 'className' => ContactsTable::class,
  1231. ]);
  1232. $table->setSchema(['foo' => [
  1233. 'type' => 'binary',
  1234. 'null' => false,
  1235. 'default' => null,
  1236. 'length' => 1024,
  1237. ]]);
  1238. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1239. $result = $this->Form->control('foo');
  1240. $expected = [
  1241. 'div' => ['class' => 'input file'],
  1242. 'label' => ['for' => 'foo'],
  1243. 'Foo',
  1244. '/label',
  1245. ['input' => [
  1246. 'type' => 'file', 'name' => 'foo',
  1247. 'id' => 'foo',
  1248. ]],
  1249. '/div',
  1250. ];
  1251. $this->assertHtml($expected, $result);
  1252. }
  1253. /**
  1254. * testFormSecurityMultipleFields method
  1255. *
  1256. * Test secure() with multiple row form. Ensure hash is correct.
  1257. *
  1258. * @return void
  1259. */
  1260. public function testFormSecurityMultipleFields()
  1261. {
  1262. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1263. $fields = [
  1264. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1265. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1266. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1267. ];
  1268. $result = $this->Form->secure($fields);
  1269. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1270. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1271. $tokenDebug = urlencode(json_encode([
  1272. '',
  1273. $fields,
  1274. [],
  1275. ]));
  1276. $expected = [
  1277. 'div' => ['style' => 'display:none;'],
  1278. ['input' => [
  1279. 'type' => 'hidden',
  1280. 'name' => '_Token[fields]',
  1281. 'value' => $hash,
  1282. 'autocomplete' => 'off',
  1283. ]],
  1284. ['input' => [
  1285. 'type' => 'hidden',
  1286. 'name' => '_Token[unlocked]',
  1287. 'autocomplete' => 'off',
  1288. 'value' => '',
  1289. ]],
  1290. ['input' => [
  1291. 'type' => 'hidden',
  1292. 'name' => '_Token[debug]',
  1293. 'value' => $tokenDebug,
  1294. 'autocomplete' => 'off',
  1295. ]],
  1296. '/div',
  1297. ];
  1298. $this->assertHtml($expected, $result);
  1299. }
  1300. /**
  1301. * testFormSecurityMultipleSubmitButtons
  1302. *
  1303. * test form submit generation and ensure that _Token is only created on end()
  1304. *
  1305. * @return void
  1306. */
  1307. public function testFormSecurityMultipleSubmitButtons()
  1308. {
  1309. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1310. $this->Form->create($this->article);
  1311. $this->Form->text('Address.title');
  1312. $this->Form->text('Address.first_name');
  1313. $result = $this->Form->submit('Save', ['name' => 'save']);
  1314. $expected = [
  1315. 'div' => ['class' => 'submit'],
  1316. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1317. '/div',
  1318. ];
  1319. $this->assertHtml($expected, $result);
  1320. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1321. $expected = [
  1322. 'div' => ['class' => 'submit'],
  1323. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1324. '/div',
  1325. ];
  1326. $this->assertHtml($expected, $result);
  1327. $result = $this->Form->end();
  1328. $tokenDebug = urlencode(json_encode([
  1329. '/articles/add',
  1330. [
  1331. 'Address.title',
  1332. 'Address.first_name',
  1333. ],
  1334. ['save', 'cancel'],
  1335. ]));
  1336. $expected = [
  1337. 'div' => ['style' => 'display:none;'],
  1338. ['input' => [
  1339. 'type' => 'hidden',
  1340. 'name' => '_Token[fields]',
  1341. 'autocomplete',
  1342. 'value',
  1343. ]],
  1344. ['input' => [
  1345. 'type' => 'hidden',
  1346. 'name' => '_Token[unlocked]',
  1347. 'value' => 'cancel%7Csave',
  1348. 'autocomplete' => 'off',
  1349. ]],
  1350. ['input' => [
  1351. 'type' => 'hidden',
  1352. 'name' => '_Token[debug]',
  1353. 'value' => $tokenDebug,
  1354. 'autocomplete' => 'off',
  1355. ]],
  1356. '/div',
  1357. ];
  1358. $this->assertHtml($expected, $result);
  1359. }
  1360. /**
  1361. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1362. *
  1363. * @return void
  1364. */
  1365. public function testSecurityButtonNestedNamed()
  1366. {
  1367. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1368. $this->Form->create('Addresses');
  1369. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1370. $result = $this->Form->unlockField();
  1371. $this->assertEquals(['Address.button'], $result);
  1372. }
  1373. /**
  1374. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1375. *
  1376. * @return void
  1377. */
  1378. public function testSecuritySubmitNestedNamed()
  1379. {
  1380. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1381. $this->Form->create($this->article);
  1382. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1383. $result = $this->Form->unlockField();
  1384. $this->assertEquals(['Address.button'], $result);
  1385. }
  1386. /**
  1387. * Test that the correct fields are unlocked for image submits with no names.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testSecuritySubmitImageNoName()
  1392. {
  1393. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1394. $this->Form->create(false);
  1395. $result = $this->Form->submit('save.png');
  1396. $expected = [
  1397. 'div' => ['class' => 'submit'],
  1398. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1399. '/div',
  1400. ];
  1401. $this->assertHtml($expected, $result);
  1402. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1403. }
  1404. /**
  1405. * Test that the correct fields are unlocked for image submits with names.
  1406. *
  1407. * @return void
  1408. */
  1409. public function testSecuritySubmitImageName()
  1410. {
  1411. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1412. $this->Form->create(null);
  1413. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1414. $expected = [
  1415. 'div' => ['class' => 'submit'],
  1416. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1417. '/div',
  1418. ];
  1419. $this->assertHtml($expected, $result);
  1420. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1421. }
  1422. /**
  1423. * testFormSecurityMultipleControlFields method
  1424. *
  1425. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1426. *
  1427. * @return void
  1428. */
  1429. public function testFormSecurityMultipleControlFields()
  1430. {
  1431. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1432. $this->Form->create();
  1433. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1434. $this->Form->control('Addresses.0.title');
  1435. $this->Form->control('Addresses.0.first_name');
  1436. $this->Form->control('Addresses.0.last_name');
  1437. $this->Form->control('Addresses.0.address');
  1438. $this->Form->control('Addresses.0.city');
  1439. $this->Form->control('Addresses.0.phone');
  1440. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1441. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1442. $this->Form->control('Addresses.1.title');
  1443. $this->Form->control('Addresses.1.first_name');
  1444. $this->Form->control('Addresses.1.last_name');
  1445. $this->Form->control('Addresses.1.address');
  1446. $this->Form->control('Addresses.1.city');
  1447. $this->Form->control('Addresses.1.phone');
  1448. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1449. $result = $this->Form->secure($this->Form->fields);
  1450. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1451. $tokenDebug = urlencode(json_encode([
  1452. '/articles/add',
  1453. [
  1454. 'Addresses.0.id' => '123456',
  1455. 'Addresses.0.title',
  1456. 'Addresses.0.first_name',
  1457. 'Addresses.0.last_name',
  1458. 'Addresses.0.address',
  1459. 'Addresses.0.city',
  1460. 'Addresses.0.phone',
  1461. 'Addresses.0.primary',
  1462. 'Addresses.1.id' => '654321',
  1463. 'Addresses.1.title',
  1464. 'Addresses.1.first_name',
  1465. 'Addresses.1.last_name',
  1466. 'Addresses.1.address',
  1467. 'Addresses.1.city',
  1468. 'Addresses.1.phone',
  1469. 'Addresses.1.primary',
  1470. ],
  1471. [],
  1472. ]));
  1473. $expected = [
  1474. 'div' => ['style' => 'display:none;'],
  1475. ['input' => [
  1476. 'type' => 'hidden',
  1477. 'name' => '_Token[fields]',
  1478. 'value' => $hash,
  1479. 'autocomplete' => 'off',
  1480. ]],
  1481. ['input' => [
  1482. 'type' => 'hidden',
  1483. 'name' => '_Token[unlocked]',
  1484. 'autocomplete' => 'off',
  1485. 'value' => '',
  1486. ]],
  1487. ['input' => [
  1488. 'type' => 'hidden',
  1489. 'name' => '_Token[debug]',
  1490. 'value' => $tokenDebug,
  1491. 'autocomplete' => 'off',
  1492. ]],
  1493. '/div',
  1494. ];
  1495. $this->assertHtml($expected, $result);
  1496. }
  1497. /**
  1498. * testFormSecurityArrayFields method
  1499. *
  1500. * Test form security with Model.field.0 style inputs.
  1501. *
  1502. * @return void
  1503. */
  1504. public function testFormSecurityArrayFields()
  1505. {
  1506. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1507. $this->Form->create();
  1508. $this->Form->text('Address.primary.1');
  1509. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1510. $this->Form->text('Address.secondary.1.0');
  1511. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1512. }
  1513. /**
  1514. * testFormSecurityMultipleControlDisabledFields method
  1515. *
  1516. * Test secure form generation with multiple records and disabled fields.
  1517. *
  1518. * @return void
  1519. */
  1520. public function testFormSecurityMultipleControlDisabledFields()
  1521. {
  1522. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1523. 'unlockedFields' => ['first_name', 'address'],
  1524. ]));
  1525. $this->Form->create();
  1526. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1527. $this->Form->text('Addresses.0.title');
  1528. $this->Form->text('Addresses.0.first_name');
  1529. $this->Form->text('Addresses.0.last_name');
  1530. $this->Form->text('Addresses.0.address');
  1531. $this->Form->text('Addresses.0.city');
  1532. $this->Form->text('Addresses.0.phone');
  1533. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1534. $this->Form->text('Addresses.1.title');
  1535. $this->Form->text('Addresses.1.first_name');
  1536. $this->Form->text('Addresses.1.last_name');
  1537. $this->Form->text('Addresses.1.address');
  1538. $this->Form->text('Addresses.1.city');
  1539. $this->Form->text('Addresses.1.phone');
  1540. $result = $this->Form->secure($this->Form->fields);
  1541. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1542. $tokenDebug = urlencode(json_encode([
  1543. '/articles/add',
  1544. [
  1545. 'Addresses.0.id' => '123456',
  1546. 'Addresses.0.title',
  1547. 'Addresses.0.last_name',
  1548. 'Addresses.0.city',
  1549. 'Addresses.0.phone',
  1550. 'Addresses.1.id' => '654321',
  1551. 'Addresses.1.title',
  1552. 'Addresses.1.last_name',
  1553. 'Addresses.1.city',
  1554. 'Addresses.1.phone',
  1555. ],
  1556. [
  1557. 'first_name',
  1558. 'address',
  1559. ],
  1560. ]));
  1561. $expected = [
  1562. 'div' => ['style' => 'display:none;'],
  1563. ['input' => [
  1564. 'type' => 'hidden',
  1565. 'name' => '_Token[fields]',
  1566. 'autocomplete' => 'off',
  1567. 'value' => $hash,
  1568. ]],
  1569. ['input' => [
  1570. 'type' => 'hidden',
  1571. 'name' => '_Token[unlocked]',
  1572. 'autocomplete' => 'off',
  1573. 'value' => 'address%7Cfirst_name',
  1574. ]],
  1575. ['input' => [
  1576. 'type' => 'hidden',
  1577. 'name' => '_Token[debug]',
  1578. 'autocomplete' => 'off',
  1579. 'value' => $tokenDebug,
  1580. ]],
  1581. '/div',
  1582. ];
  1583. $this->assertHtml($expected, $result);
  1584. }
  1585. /**
  1586. * testFormSecurityControlDisabledFields method
  1587. *
  1588. * Test single record form with disabled fields.
  1589. *
  1590. * @return void
  1591. */
  1592. public function testFormSecurityControlUnlockedFields()
  1593. {
  1594. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1595. 'unlockedFields' => ['first_name', 'address'],
  1596. ]));
  1597. $this->Form->create();
  1598. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1599. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1600. $this->Form->text('Addresses.title');
  1601. $this->Form->text('Addresses.first_name');
  1602. $this->Form->text('Addresses.last_name');
  1603. $this->Form->text('Addresses.address');
  1604. $this->Form->text('Addresses.city');
  1605. $this->Form->text('Addresses.phone');
  1606. $result = $this->Form->fields;
  1607. $expected = [
  1608. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1609. 'Addresses.city', 'Addresses.phone',
  1610. ];
  1611. $this->assertEquals($expected, $result);
  1612. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1613. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1614. $tokenDebug = urlencode(json_encode([
  1615. '/articles/add',
  1616. [
  1617. 'Addresses.id' => '123456',
  1618. 'Addresses.title',
  1619. 'Addresses.last_name',
  1620. 'Addresses.city',
  1621. 'Addresses.phone',
  1622. ],
  1623. [
  1624. 'first_name',
  1625. 'address',
  1626. ],
  1627. ]));
  1628. $expected = [
  1629. 'div' => ['style' => 'display:none;'],
  1630. ['input' => [
  1631. 'type' => 'hidden',
  1632. 'name' => '_Token[fields]',
  1633. 'value' => $hash,
  1634. 'autocomplete' => 'off',
  1635. 'data-foo' => 'bar',
  1636. ]],
  1637. ['input' => [
  1638. 'type' => 'hidden',
  1639. 'name' => '_Token[unlocked]',
  1640. 'value' => 'address%7Cfirst_name',
  1641. 'autocomplete' => 'off',
  1642. 'data-foo' => 'bar',
  1643. ]],
  1644. ['input' => [
  1645. 'type' => 'hidden', 'name' => '_Token[debug]',
  1646. 'value' => $tokenDebug,
  1647. 'autocomplete' => 'off',
  1648. 'data-foo' => 'bar',
  1649. ]],
  1650. '/div',
  1651. ];
  1652. $this->assertHtml($expected, $result);
  1653. }
  1654. /**
  1655. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1656. *
  1657. * Test single record form with debugSecurity param.
  1658. *
  1659. * @return void
  1660. */
  1661. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1662. {
  1663. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1664. 'unlockedFields' => ['first_name', 'address'],
  1665. ]));
  1666. $this->Form->create();
  1667. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1668. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1669. $this->Form->text('Addresses.title');
  1670. $this->Form->text('Addresses.first_name');
  1671. $this->Form->text('Addresses.last_name');
  1672. $this->Form->text('Addresses.address');
  1673. $this->Form->text('Addresses.city');
  1674. $this->Form->text('Addresses.phone');
  1675. $result = $this->Form->fields;
  1676. $expected = [
  1677. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1678. 'Addresses.city', 'Addresses.phone',
  1679. ];
  1680. $this->assertEquals($expected, $result);
  1681. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1682. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1683. $tokenDebug = urlencode(json_encode([
  1684. '/articles/add',
  1685. [
  1686. 'Addresses.id' => '123456',
  1687. 'Addresses.title',
  1688. 'Addresses.last_name',
  1689. 'Addresses.city',
  1690. 'Addresses.phone',
  1691. ],
  1692. [
  1693. 'first_name',
  1694. 'address',
  1695. ],
  1696. ]));
  1697. $expected = [
  1698. 'div' => ['style' => 'display:none;'],
  1699. ['input' => [
  1700. 'type' => 'hidden',
  1701. 'name' => '_Token[fields]',
  1702. 'value' => $hash,
  1703. 'autocomplete' => 'off',
  1704. 'data-foo' => 'bar',
  1705. ]],
  1706. ['input' => [
  1707. 'type' => 'hidden',
  1708. 'name' => '_Token[unlocked]',
  1709. 'value' => 'address%7Cfirst_name',
  1710. 'autocomplete' => 'off',
  1711. 'data-foo' => 'bar',
  1712. ]],
  1713. ['input' => [
  1714. 'type' => 'hidden', 'name' => '_Token[debug]',
  1715. 'value' => $tokenDebug,
  1716. 'autocomplete' => 'off',
  1717. 'data-foo' => 'bar',
  1718. ]],
  1719. '/div',
  1720. ];
  1721. $this->assertHtml($expected, $result);
  1722. }
  1723. /**
  1724. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1725. *
  1726. * Debug is false, debugSecurity is true -> no debug
  1727. *
  1728. * @return void
  1729. */
  1730. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1731. {
  1732. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1733. 'unlockedFields' => ['first_name', 'address'],
  1734. ]));
  1735. $this->Form->create();
  1736. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1737. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1738. $this->Form->text('Addresses.title');
  1739. $this->Form->text('Addresses.first_name');
  1740. $this->Form->text('Addresses.last_name');
  1741. $this->Form->text('Addresses.address');
  1742. $this->Form->text('Addresses.city');
  1743. $this->Form->text('Addresses.phone');
  1744. $result = $this->Form->fields;
  1745. $expected = [
  1746. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1747. 'Addresses.city', 'Addresses.phone',
  1748. ];
  1749. $this->assertEquals($expected, $result);
  1750. Configure::write('debug', false);
  1751. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1752. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1753. $expected = [
  1754. 'div' => ['style' => 'display:none;'],
  1755. ['input' => [
  1756. 'type' => 'hidden',
  1757. 'name' => '_Token[fields]',
  1758. 'value' => $hash,
  1759. 'autocomplete' => 'off',
  1760. 'data-foo' => 'bar',
  1761. ]],
  1762. ['input' => [
  1763. 'type' => 'hidden',
  1764. 'name' => '_Token[unlocked]',
  1765. 'value' => 'address%7Cfirst_name',
  1766. 'autocomplete' => 'off',
  1767. 'data-foo' => 'bar',
  1768. ]],
  1769. '/div',
  1770. ];
  1771. $this->assertHtml($expected, $result);
  1772. }
  1773. /**
  1774. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1775. *
  1776. * Test single record form with debugSecurity param.
  1777. *
  1778. * @return void
  1779. */
  1780. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1781. {
  1782. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1783. 'unlockedFields' => ['first_name', 'address'],
  1784. ]));
  1785. $this->Form->create();
  1786. $this->assertEquals(
  1787. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1788. $this->Form->unlockField()
  1789. );
  1790. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1791. $this->Form->text('Addresses.title');
  1792. $this->Form->text('Addresses.first_name');
  1793. $this->Form->text('Addresses.last_name');
  1794. $this->Form->text('Addresses.address');
  1795. $this->Form->text('Addresses.city');
  1796. $this->Form->text('Addresses.phone');
  1797. $result = $this->Form->fields;
  1798. $expected = [
  1799. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1800. 'Addresses.city', 'Addresses.phone',
  1801. ];
  1802. $this->assertEquals($expected, $result);
  1803. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1804. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1805. $expected = [
  1806. 'div' => ['style' => 'display:none;'],
  1807. ['input' => [
  1808. 'type' => 'hidden',
  1809. 'name' => '_Token[fields]',
  1810. 'value' => $hash,
  1811. 'autocomplete' => 'off',
  1812. 'data-foo' => 'bar',
  1813. ]],
  1814. ['input' => [
  1815. 'type' => 'hidden',
  1816. 'name' => '_Token[unlocked]',
  1817. 'value' => 'address%7Cfirst_name',
  1818. 'autocomplete' => 'off',
  1819. 'data-foo' => 'bar',
  1820. ]],
  1821. '/div',
  1822. ];
  1823. $this->assertHtml($expected, $result);
  1824. }
  1825. /**
  1826. * testFormSecureWithCustomNameAttribute method
  1827. *
  1828. * Test securing inputs with custom name attributes.
  1829. *
  1830. * @return void
  1831. */
  1832. public function testFormSecureWithCustomNameAttribute()
  1833. {
  1834. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1835. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1836. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1837. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1838. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1839. }
  1840. /**
  1841. * testFormSecuredControl method
  1842. *
  1843. * Test generation of entire secure form, assertions made on control() output.
  1844. *
  1845. * @return void
  1846. */
  1847. public function testFormSecuredControl()
  1848. {
  1849. $this->View->setRequest($this->View->getRequest()
  1850. ->withParam('_Token', 'stuff')
  1851. ->withParam('_csrfToken', 'testKey'));
  1852. $this->article['schema'] = [
  1853. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1854. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1855. ];
  1856. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1857. $encoding = strtolower(Configure::read('App.encoding'));
  1858. $expected = [
  1859. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1860. 'div' => ['style' => 'display:none;'],
  1861. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1862. ['input' => [
  1863. 'type' => 'hidden',
  1864. 'name' => '_csrfToken',
  1865. 'value' => 'testKey',
  1866. 'autocomplete' => 'off',
  1867. ]],
  1868. '/div',
  1869. ];
  1870. $this->assertHtml($expected, $result);
  1871. $result = $this->Form->control('ratio');
  1872. $expected = [
  1873. 'div' => ['class'],
  1874. 'label' => ['for'],
  1875. 'Ratio',
  1876. '/label',
  1877. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1878. '/div',
  1879. ];
  1880. $this->assertHtml($expected, $result);
  1881. $result = $this->Form->control('population');
  1882. $expected = [
  1883. 'div' => ['class'],
  1884. 'label' => ['for'],
  1885. 'Population',
  1886. '/label',
  1887. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1888. '/div',
  1889. ];
  1890. $this->assertHtml($expected, $result);
  1891. $result = $this->Form->control('published', ['type' => 'text']);
  1892. $expected = [
  1893. 'div' => ['class' => 'input text'],
  1894. 'label' => ['for' => 'published'],
  1895. 'Published',
  1896. '/label',
  1897. ['input' => [
  1898. 'type' => 'text',
  1899. 'name' => 'published',
  1900. 'id' => 'published',
  1901. ]],
  1902. '/div',
  1903. ];
  1904. $this->assertHtml($expected, $result);
  1905. $result = $this->Form->control('other', ['type' => 'text']);
  1906. $expected = [
  1907. 'div' => ['class' => 'input text'],
  1908. 'label' => ['for' => 'other'],
  1909. 'Other',
  1910. '/label',
  1911. ['input' => [
  1912. 'type' => 'text',
  1913. 'name' => 'other',
  1914. 'id',
  1915. ]],
  1916. '/div',
  1917. ];
  1918. $this->assertHtml($expected, $result);
  1919. $result = $this->Form->hidden('stuff');
  1920. $expected = [
  1921. 'input' => [
  1922. 'type' => 'hidden',
  1923. 'name' => 'stuff',
  1924. ],
  1925. ];
  1926. $this->assertHtml($expected, $result);
  1927. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1928. $expected = ['input' => [
  1929. 'type' => 'hidden',
  1930. 'name' => 'hidden',
  1931. 'value' => '0',
  1932. ]];
  1933. $this->assertHtml($expected, $result);
  1934. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1935. $expected = [
  1936. 'div' => ['class' => 'input checkbox'],
  1937. ['input' => [
  1938. 'type' => 'hidden',
  1939. 'name' => 'something',
  1940. 'value' => '0',
  1941. ]],
  1942. 'label' => ['for' => 'something'],
  1943. ['input' => [
  1944. 'type' => 'checkbox',
  1945. 'name' => 'something',
  1946. 'value' => '1',
  1947. 'id' => 'something',
  1948. ]],
  1949. 'Something',
  1950. '/label',
  1951. '/div',
  1952. ];
  1953. $this->assertHtml($expected, $result);
  1954. $result = $this->Form->fields;
  1955. $expectedFields = [
  1956. 'ratio',
  1957. 'population',
  1958. 'published',
  1959. 'other',
  1960. 'stuff' => '',
  1961. 'hidden' => '0',
  1962. 'something',
  1963. ];
  1964. $this->assertEquals($expectedFields, $result);
  1965. $result = $this->Form->secure($this->Form->fields);
  1966. $tokenDebug = urlencode(json_encode([
  1967. '/articles/add',
  1968. $expectedFields,
  1969. [],
  1970. ]));
  1971. $expected = [
  1972. 'div' => ['style' => 'display:none;'],
  1973. ['input' => [
  1974. 'type' => 'hidden',
  1975. 'name' => '_Token[fields]',
  1976. 'value',
  1977. 'autocomplete',
  1978. ]],
  1979. ['input' => [
  1980. 'type' => 'hidden',
  1981. 'name' => '_Token[unlocked]',
  1982. 'value' => '',
  1983. 'autocomplete' => 'off',
  1984. ]],
  1985. ['input' => [
  1986. 'type' => 'hidden', 'name' => '_Token[debug]',
  1987. 'value' => $tokenDebug,
  1988. 'autocomplete' => 'off',
  1989. ]],
  1990. '/div',
  1991. ];
  1992. $this->assertHtml($expected, $result);
  1993. }
  1994. /**
  1995. * testSecuredControlCustomName method
  1996. *
  1997. * Test secured inputs with custom names.
  1998. *
  1999. * @return void
  2000. */
  2001. public function testSecuredControlCustomName()
  2002. {
  2003. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2004. $this->assertEquals([], $this->Form->fields);
  2005. $this->Form->text('text_input', [
  2006. 'name' => 'Option[General.default_role]',
  2007. ]);
  2008. $expected = ['Option.General.default_role'];
  2009. $this->assertEquals($expected, $this->Form->fields);
  2010. $this->Form->select('select_box', [1, 2], [
  2011. 'name' => 'Option[General.select_role]',
  2012. ]);
  2013. $expected[] = 'Option.General.select_role';
  2014. $this->assertEquals($expected, $this->Form->fields);
  2015. $this->Form->text('other.things[]');
  2016. $expected[] = 'other.things';
  2017. $this->assertEquals($expected, $this->Form->fields);
  2018. }
  2019. /**
  2020. * testSecuredControlDuplicate method
  2021. *
  2022. * Test that a hidden field followed by a visible field
  2023. * undoes the hidden field locking.
  2024. *
  2025. * @return void
  2026. */
  2027. public function testSecuredControlDuplicate()
  2028. {
  2029. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2030. $this->assertEquals([], $this->Form->fields);
  2031. $this->Form->control('text_val', [
  2032. 'type' => 'hidden',
  2033. 'value' => 'some text',
  2034. ]);
  2035. $expected = ['text_val' => 'some text'];
  2036. $this->assertEquals($expected, $this->Form->fields);
  2037. $this->Form->control('text_val', [
  2038. 'type' => 'text',
  2039. ]);
  2040. $expected = ['text_val'];
  2041. $this->assertEquals($expected, $this->Form->fields);
  2042. }
  2043. /**
  2044. * testFormSecuredFileControl method
  2045. *
  2046. * Tests that the correct keys are added to the field hash index.
  2047. *
  2048. * @return void
  2049. */
  2050. public function testFormSecuredFileControl()
  2051. {
  2052. $this->assertEquals([], $this->Form->fields);
  2053. $this->Form->file('Attachment.file');
  2054. $expected = [
  2055. 'Attachment.file.name', 'Attachment.file.type',
  2056. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2057. 'Attachment.file.size',
  2058. ];
  2059. $this->assertEquals($expected, $this->Form->fields);
  2060. }
  2061. /**
  2062. * testFormSecuredMultipleSelect method
  2063. *
  2064. * Test that multiple selects keys are added to field hash.
  2065. *
  2066. * @return void
  2067. */
  2068. public function testFormSecuredMultipleSelect()
  2069. {
  2070. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2071. $this->assertEquals([], $this->Form->fields);
  2072. $options = ['1' => 'one', '2' => 'two'];
  2073. $this->Form->select('Model.select', $options);
  2074. $expected = ['Model.select'];
  2075. $this->assertEquals($expected, $this->Form->fields);
  2076. $this->Form->fields = [];
  2077. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2078. $this->assertEquals($expected, $this->Form->fields);
  2079. }
  2080. /**
  2081. * testFormSecuredRadio method
  2082. *
  2083. * @return void
  2084. */
  2085. public function testFormSecuredRadio()
  2086. {
  2087. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2088. $this->assertEquals([], $this->Form->fields);
  2089. $options = ['1' => 'option1', '2' => 'option2'];
  2090. $this->Form->radio('Test.test', $options);
  2091. $expected = ['Test.test'];
  2092. $this->assertEquals($expected, $this->Form->fields);
  2093. $this->Form->radio('Test.all', $options, [
  2094. 'disabled' => ['option1', 'option2'],
  2095. ]);
  2096. $expected = ['Test.test', 'Test.all' => ''];
  2097. $this->assertEquals($expected, $this->Form->fields);
  2098. $this->Form->radio('Test.some', $options, [
  2099. 'disabled' => ['option1'],
  2100. ]);
  2101. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2102. $this->assertEquals($expected, $this->Form->fields);
  2103. }
  2104. /**
  2105. * testFormSecuredAndDisabledNotAssoc method
  2106. *
  2107. * Test that when disabled is in a list based attribute array it works.
  2108. *
  2109. * @return void
  2110. */
  2111. public function testFormSecuredAndDisabledNotAssoc()
  2112. {
  2113. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2114. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2115. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2116. $this->Form->text('Model.text', ['disabled']);
  2117. $this->Form->textarea('Model.textarea', ['disabled']);
  2118. $this->Form->password('Model.password', ['disabled']);
  2119. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2120. $expected = [
  2121. 'Model.radio' => '',
  2122. ];
  2123. $this->assertEquals($expected, $this->Form->fields);
  2124. }
  2125. /**
  2126. * testFormSecuredAndDisabled method
  2127. *
  2128. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2129. * hashing.
  2130. *
  2131. * @return void
  2132. */
  2133. public function testFormSecuredAndDisabled()
  2134. {
  2135. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2136. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2137. $this->Form->text('Model.text', ['disabled' => true]);
  2138. $this->Form->password('Model.text', ['disabled' => true]);
  2139. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2140. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2141. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2142. $this->Form->year('Model.year', ['disabled' => true]);
  2143. $this->Form->month('Model.month', ['disabled' => true]);
  2144. $this->Form->day('Model.day', ['disabled' => true]);
  2145. $this->Form->hour('Model.hour', ['disabled' => true]);
  2146. $this->Form->minute('Model.minute', ['disabled' => true]);
  2147. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2148. $expected = [
  2149. 'Model.radio' => '',
  2150. ];
  2151. $this->assertEquals($expected, $this->Form->fields);
  2152. }
  2153. /**
  2154. * testDisableSecurityUsingForm method
  2155. *
  2156. * @return void
  2157. */
  2158. public function testDisableSecurityUsingForm()
  2159. {
  2160. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2161. 'disabledFields' => [],
  2162. ]));
  2163. $this->Form->create();
  2164. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2165. $this->Form->text('Addresses.title');
  2166. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2167. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2168. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2169. $result = $this->Form->fields;
  2170. $expected = [
  2171. 'Addresses.id' => '123456', 'Addresses.title',
  2172. ];
  2173. $this->assertEquals($expected, $result);
  2174. }
  2175. /**
  2176. * testUnlockFieldAddsToList method
  2177. *
  2178. * Test disableField.
  2179. *
  2180. * @return void
  2181. */
  2182. public function testUnlockFieldAddsToList()
  2183. {
  2184. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2185. 'unlockedFields' => [],
  2186. ]));
  2187. $this->Form->unlockField('Contact.name');
  2188. $this->Form->text('Contact.name');
  2189. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2190. $this->assertEquals([], $this->Form->fields);
  2191. }
  2192. /**
  2193. * testUnlockFieldRemovingFromFields method
  2194. *
  2195. * Test unlockField removing from fields array.
  2196. *
  2197. * @return void
  2198. */
  2199. public function testUnlockFieldRemovingFromFields()
  2200. {
  2201. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2202. 'unlockedFields' => [],
  2203. ]));
  2204. $this->Form->create($this->article);
  2205. $this->Form->hidden('Article.id', ['value' => 1]);
  2206. $this->Form->text('Article.title');
  2207. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2208. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2209. $this->Form->unlockField('Article.title');
  2210. $this->Form->unlockField('Article.id');
  2211. $this->assertEquals([], $this->Form->fields);
  2212. }
  2213. /**
  2214. * testResetUnlockFields method
  2215. *
  2216. * Test reset unlockFields, when create new form.
  2217. *
  2218. * @return void
  2219. */
  2220. public function testResetUnlockFields()
  2221. {
  2222. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2223. 'key' => 'testKey',
  2224. 'unlockedFields' => [],
  2225. ]));
  2226. $this->Form->unlockField('Contact.id');
  2227. $this->Form->create('Contact');
  2228. $this->Form->hidden('Contact.id', ['value' => 1]);
  2229. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2230. $this->Form->end();
  2231. $this->Form->create('Contact');
  2232. $this->Form->hidden('Contact.id', ['value' => 1]);
  2233. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2234. }
  2235. /**
  2236. * testSecuredFormUrlIgnoresHost method
  2237. *
  2238. * Test that only the path + query elements of a form's URL show up in their hash.
  2239. *
  2240. * @return void
  2241. */
  2242. public function testSecuredFormUrlIgnoresHost()
  2243. {
  2244. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2245. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2246. $this->Form->create($this->article, [
  2247. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2248. ]);
  2249. $result = $this->Form->secure();
  2250. $this->assertContains($expected, $result);
  2251. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2252. $result = $this->Form->secure();
  2253. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2254. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2255. $result = $this->Form->secure();
  2256. $this->assertContains($expected, $result, 'URL path + query should work.');
  2257. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2258. $result = $this->Form->secure();
  2259. $this->assertNotContains($expected, $result, 'URL is different');
  2260. }
  2261. /**
  2262. * testSecuredFormUrlHasHtmlAndIdentifier method
  2263. *
  2264. * Test that URL, HTML and identifier show up in their hashes.
  2265. *
  2266. * @return void
  2267. */
  2268. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2269. {
  2270. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2271. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2272. $this->Form->create($this->article, [
  2273. 'url' => [
  2274. 'controller' => 'articles',
  2275. 'action' => 'view',
  2276. '?' => [
  2277. 'page' => 1,
  2278. 'limit' => 10,
  2279. 'html' => '<>"',
  2280. ],
  2281. '#' => 'result',
  2282. ],
  2283. ]);
  2284. $result = $this->Form->secure();
  2285. $this->assertContains($expected, $result);
  2286. $this->Form->create($this->article, [
  2287. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2288. ]);
  2289. $result = $this->Form->secure();
  2290. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2291. $this->Form->create($this->article, [
  2292. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2293. ]);
  2294. $result = $this->Form->secure();
  2295. $this->assertContains($expected, $result, 'URL path + query should work.');
  2296. }
  2297. /**
  2298. * testErrorMessageDisplay method
  2299. *
  2300. * Test error message display.
  2301. *
  2302. * @return void
  2303. */
  2304. public function testErrorMessageDisplay()
  2305. {
  2306. $this->article['errors'] = [
  2307. 'Article' => [
  2308. 'title' => 'error message',
  2309. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2310. ],
  2311. ];
  2312. $this->Form->create($this->article);
  2313. $result = $this->Form->control('Article.title');
  2314. $expected = [
  2315. 'div' => ['class' => 'input text error'],
  2316. 'label' => ['for' => 'article-title'],
  2317. 'Title',
  2318. '/label',
  2319. 'input' => [
  2320. 'type' => 'text', 'name' => 'Article[title]',
  2321. 'id' => 'article-title', 'class' => 'form-error',
  2322. ],
  2323. ['div' => ['class' => 'error-message']],
  2324. 'error message',
  2325. '/div',
  2326. '/div',
  2327. ];
  2328. $this->assertHtml($expected, $result);
  2329. $result = $this->Form->control('Article.title', [
  2330. 'templates' => [
  2331. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2332. ],
  2333. ]);
  2334. $expected = [
  2335. 'div' => ['class' => 'input text error'],
  2336. 'label' => ['for' => 'article-title'],
  2337. 'Title',
  2338. '/label',
  2339. 'input' => [
  2340. 'type' => 'text', 'name' => 'Article[title]',
  2341. 'id' => 'article-title', 'class' => 'form-error',
  2342. ],
  2343. '/div',
  2344. ];
  2345. $this->assertHtml($expected, $result);
  2346. $result = $this->Form->control('Article.content');
  2347. $expected = [
  2348. 'div' => ['class' => 'input text error'],
  2349. 'label' => ['for' => 'article-content'],
  2350. 'Content',
  2351. '/label',
  2352. 'input' => [
  2353. 'type' => 'text', 'name' => 'Article[content]',
  2354. 'id' => 'article-content', 'class' => 'form-error',
  2355. ],
  2356. ['div' => ['class' => 'error-message']],
  2357. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2358. '/div',
  2359. '/div',
  2360. ];
  2361. $this->assertHtml($expected, $result);
  2362. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2363. $expected = [
  2364. 'div' => ['class' => 'input text error'],
  2365. 'label' => ['for' => 'article-content'],
  2366. 'Content',
  2367. '/label',
  2368. 'input' => [
  2369. 'type' => 'text', 'name' => 'Article[content]',
  2370. 'id' => 'article-content', 'class' => 'form-error',
  2371. ],
  2372. ['div' => ['class' => 'error-message']],
  2373. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2374. '/div',
  2375. '/div',
  2376. ];
  2377. $this->assertHtml($expected, $result);
  2378. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2379. $expected = [
  2380. 'div' => ['class' => 'input text error'],
  2381. 'label' => ['for' => 'article-content'],
  2382. 'Content',
  2383. '/label',
  2384. 'input' => [
  2385. 'type' => 'text', 'name' => 'Article[content]',
  2386. 'id' => 'article-content', 'class' => 'form-error',
  2387. ],
  2388. ['div' => ['class' => 'error-message']],
  2389. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2390. '/div',
  2391. '/div',
  2392. ];
  2393. $this->assertHtml($expected, $result);
  2394. }
  2395. /**
  2396. * testEmptyErrorValidation method
  2397. *
  2398. * Test validation errors, when validation message is an empty string.
  2399. *
  2400. * @return void
  2401. */
  2402. public function testEmptyErrorValidation()
  2403. {
  2404. $this->article['errors'] = [
  2405. 'Article' => ['title' => ''],
  2406. ];
  2407. $this->Form->create($this->article);
  2408. $result = $this->Form->control('Article.title');
  2409. $expected = [
  2410. 'div' => ['class' => 'input text error'],
  2411. 'label' => ['for' => 'article-title'],
  2412. 'Title',
  2413. '/label',
  2414. 'input' => [
  2415. 'type' => 'text', 'name' => 'Article[title]',
  2416. 'id' => 'article-title', 'class' => 'form-error',
  2417. ],
  2418. ['div' => ['class' => 'error-message']],
  2419. [],
  2420. '/div',
  2421. '/div',
  2422. ];
  2423. $this->assertHtml($expected, $result);
  2424. }
  2425. /**
  2426. * testEmptyControlErrorValidation method
  2427. *
  2428. * Test validation errors, when calling control() overriding validation message by an empty string.
  2429. *
  2430. * @return void
  2431. */
  2432. public function testEmptyControlErrorValidation()
  2433. {
  2434. $this->article['errors'] = [
  2435. 'Article' => ['title' => 'error message'],
  2436. ];
  2437. $this->Form->create($this->article);
  2438. $result = $this->Form->control('Article.title', ['error' => '']);
  2439. $expected = [
  2440. 'div' => ['class' => 'input text error'],
  2441. 'label' => ['for' => 'article-title'],
  2442. 'Title',
  2443. '/label',
  2444. 'input' => [
  2445. 'type' => 'text', 'name' => 'Article[title]',
  2446. 'id' => 'article-title', 'class' => 'form-error',
  2447. ],
  2448. ['div' => ['class' => 'error-message']],
  2449. [],
  2450. '/div',
  2451. '/div',
  2452. ];
  2453. $this->assertHtml($expected, $result);
  2454. }
  2455. /**
  2456. * testControlErrorMessage method
  2457. *
  2458. * Test validation errors, when calling control() overriding validation messages.
  2459. *
  2460. * @return void
  2461. */
  2462. public function testControlErrorMessage()
  2463. {
  2464. $this->article['errors'] = [
  2465. 'title' => ['error message'],
  2466. ];
  2467. $this->Form->create($this->article);
  2468. $result = $this->Form->control('title', [
  2469. 'error' => 'Custom error!',
  2470. ]);
  2471. $expected = [
  2472. 'div' => ['class' => 'input text required error'],
  2473. 'label' => ['for' => 'title'],
  2474. 'Title',
  2475. '/label',
  2476. 'input' => [
  2477. 'type' => 'text', 'name' => 'title',
  2478. 'id' => 'title', 'class' => 'form-error',
  2479. 'required' => 'required',
  2480. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  2481. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  2482. ],
  2483. ['div' => ['class' => 'error-message']],
  2484. 'Custom error!',
  2485. '/div',
  2486. '/div',
  2487. ];
  2488. $this->assertHtml($expected, $result);
  2489. $result = $this->Form->control('title', [
  2490. 'error' => ['error message' => 'Custom error!'],
  2491. ]);
  2492. $expected = [
  2493. 'div' => ['class' => 'input text required error'],
  2494. 'label' => ['for' => 'title'],
  2495. 'Title',
  2496. '/label',
  2497. 'input' => [
  2498. 'type' => 'text',
  2499. 'name' => 'title',
  2500. 'id' => 'title',
  2501. 'class' => 'form-error',
  2502. 'required' => 'required',
  2503. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  2504. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  2505. ],
  2506. ['div' => ['class' => 'error-message']],
  2507. 'Custom error!',
  2508. '/div',
  2509. '/div',
  2510. ];
  2511. $this->assertHtml($expected, $result);
  2512. }
  2513. /**
  2514. * testFormValidationAssociated method
  2515. *
  2516. * Tests displaying errors for nested entities.
  2517. *
  2518. * @return void
  2519. */
  2520. public function testFormValidationAssociated()
  2521. {
  2522. $nested = new Entity(['foo' => 'bar']);
  2523. $nested->setError('foo', ['not a valid bar']);
  2524. $entity = new Entity(['nested' => $nested]);
  2525. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2526. $result = $this->Form->error('nested.foo');
  2527. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2528. }
  2529. /**
  2530. * testFormValidationAssociatedSecondLevel method
  2531. *
  2532. * Test form error display with associated model.
  2533. *
  2534. * @return void
  2535. */
  2536. public function testFormValidationAssociatedSecondLevel()
  2537. {
  2538. $inner = new Entity(['bar' => 'baz']);
  2539. $nested = new Entity(['foo' => $inner]);
  2540. $entity = new Entity(['nested' => $nested]);
  2541. $inner->setError('bar', ['not a valid one']);
  2542. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2543. $result = $this->Form->error('nested.foo.bar');
  2544. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2545. }
  2546. /**
  2547. * testFormValidationMultiRecord method
  2548. *
  2549. * Test form error display with multiple records.
  2550. *
  2551. * @return void
  2552. */
  2553. public function testFormValidationMultiRecord()
  2554. {
  2555. $one = new Entity();
  2556. $two = new Entity();
  2557. $this->getTableLocator()->get('Contacts', [
  2558. 'className' => ContactsTable::class,
  2559. ]);
  2560. $one->set('email', '');
  2561. $one->setError('email', ['invalid email']);
  2562. $two->set('name', '');
  2563. $two->setError('name', ['This is wrong']);
  2564. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2565. $result = $this->Form->control('0.email');
  2566. $expected = [
  2567. 'div' => ['class' => 'input email error'],
  2568. 'label' => ['for' => '0-email'],
  2569. 'Email',
  2570. '/label',
  2571. 'input' => [
  2572. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2573. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2574. ],
  2575. ['div' => ['class' => 'error-message']],
  2576. 'invalid email',
  2577. '/div',
  2578. '/div',
  2579. ];
  2580. $this->assertHtml($expected, $result);
  2581. $result = $this->Form->control('1.name');
  2582. $expected = [
  2583. 'div' => ['class' => 'input text error'],
  2584. 'label' => ['for' => '1-name'],
  2585. 'Name',
  2586. '/label',
  2587. 'input' => [
  2588. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2589. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2590. ],
  2591. ['div' => ['class' => 'error-message']],
  2592. 'This is wrong',
  2593. '/div',
  2594. '/div',
  2595. ];
  2596. $this->assertHtml($expected, $result);
  2597. }
  2598. /**
  2599. * testControl method
  2600. *
  2601. * Test various incarnations of control().
  2602. *
  2603. * @return void
  2604. */
  2605. public function testControl()
  2606. {
  2607. $this->getTableLocator()->get('ValidateUsers', [
  2608. 'className' => ValidateUsersTable::class,
  2609. ]);
  2610. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2611. $result = $this->Form->control('ValidateUsers.balance');
  2612. $expected = [
  2613. 'div' => ['class'],
  2614. 'label' => ['for'],
  2615. 'Balance',
  2616. '/label',
  2617. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2618. '/div',
  2619. ];
  2620. $this->assertHtml($expected, $result);
  2621. $result = $this->Form->control('ValidateUser.cost_decimal');
  2622. $expected = [
  2623. 'div' => ['class'],
  2624. 'label' => ['for'],
  2625. 'Cost Decimal',
  2626. '/label',
  2627. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2628. '/div',
  2629. ];
  2630. $this->assertHtml($expected, $result);
  2631. $result = $this->Form->control('ValidateUser.null_decimal');
  2632. $expected = [
  2633. 'div' => ['class'],
  2634. 'label' => ['for'],
  2635. 'Null Decimal',
  2636. '/label',
  2637. 'input' => ['name', 'type' => 'number', 'id'],
  2638. '/div',
  2639. ];
  2640. $this->assertHtml($expected, $result);
  2641. }
  2642. /**
  2643. * testControlCustomization method
  2644. *
  2645. * Tests the input method and passing custom options.
  2646. *
  2647. * @return void
  2648. */
  2649. public function testControlCustomization()
  2650. {
  2651. $this->getTableLocator()->get('Contacts', [
  2652. 'className' => ContactsTable::class,
  2653. ]);
  2654. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2655. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2656. $expected = [
  2657. 'div' => ['class' => 'input email'],
  2658. 'label' => ['for' => 'custom'],
  2659. 'Email',
  2660. '/label',
  2661. ['input' => [
  2662. 'type' => 'email', 'name' => 'Contact[email]',
  2663. 'id' => 'custom', 'maxlength' => 255,
  2664. ]],
  2665. '/div',
  2666. ];
  2667. $this->assertHtml($expected, $result);
  2668. $result = $this->Form->control('Contact.email', [
  2669. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2670. ]);
  2671. $expected = [
  2672. '<div',
  2673. 'label' => ['for' => 'contact-email'],
  2674. 'Email',
  2675. '/label',
  2676. ['input' => [
  2677. 'type' => 'email', 'name' => 'Contact[email]',
  2678. 'id' => 'contact-email', 'maxlength' => 255,
  2679. ]],
  2680. '/div',
  2681. ];
  2682. $this->assertHtml($expected, $result);
  2683. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2684. $expected = [
  2685. 'div' => ['class' => 'input text'],
  2686. 'label' => ['for' => 'contact-email'],
  2687. 'Email',
  2688. '/label',
  2689. ['input' => [
  2690. 'type' => 'text', 'name' => 'Contact[email]',
  2691. 'id' => 'contact-email', 'maxlength' => '255',
  2692. ]],
  2693. '/div',
  2694. ];
  2695. $this->assertHtml($expected, $result);
  2696. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2697. $expected = [
  2698. 'div' => ['class' => 'input text'],
  2699. 'label' => ['for' => 'contact-5-email'],
  2700. 'Email',
  2701. '/label',
  2702. ['input' => [
  2703. 'type' => 'text', 'name' => 'Contact[5][email]',
  2704. 'id' => 'contact-5-email', 'maxlength' => '255',
  2705. ]],
  2706. '/div',
  2707. ];
  2708. $this->assertHtml($expected, $result);
  2709. $result = $this->Form->control('Contact.password');
  2710. $expected = [
  2711. 'div' => ['class' => 'input password'],
  2712. 'label' => ['for' => 'contact-password'],
  2713. 'Password',
  2714. '/label',
  2715. ['input' => [
  2716. 'type' => 'password', 'name' => 'Contact[password]',
  2717. 'id' => 'contact-password',
  2718. ]],
  2719. '/div',
  2720. ];
  2721. $this->assertHtml($expected, $result);
  2722. $result = $this->Form->control('Contact.email', [
  2723. 'type' => 'file', 'class' => 'textbox',
  2724. ]);
  2725. $expected = [
  2726. 'div' => ['class' => 'input file'],
  2727. 'label' => ['for' => 'contact-email'],
  2728. 'Email',
  2729. '/label',
  2730. ['input' => [
  2731. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2732. 'id' => 'contact-email',
  2733. ]],
  2734. '/div',
  2735. ];
  2736. $this->assertHtml($expected, $result);
  2737. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2738. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2739. $result = $this->Form->control('phone');
  2740. $expected = [
  2741. 'div' => ['class' => 'input tel'],
  2742. 'label' => ['for' => 'phone'],
  2743. 'Phone',
  2744. '/label',
  2745. ['input' => [
  2746. 'type' => 'tel', 'name' => 'phone',
  2747. 'value' => 'Hello &amp; World &gt; weird chars',
  2748. 'id' => 'phone', 'maxlength' => 255,
  2749. ]],
  2750. '/div',
  2751. ];
  2752. $this->assertHtml($expected, $result);
  2753. $this->View->setRequest(
  2754. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2755. );
  2756. $this->Form->create();
  2757. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2758. $expected = [
  2759. 'div' => ['class' => 'input text'],
  2760. 'label' => ['for' => 'myId'],
  2761. 'Field',
  2762. '/label',
  2763. 'input' => [
  2764. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2765. 'value' => 'My value', 'id' => 'myId',
  2766. ],
  2767. '/div',
  2768. ];
  2769. $this->assertHtml($expected, $result);
  2770. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2771. $this->Form->create();
  2772. $entity->setError('field', 'Badness!');
  2773. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2774. $result = $this->Form->control('field');
  2775. $expected = [
  2776. 'div' => ['class' => 'input text error'],
  2777. 'label' => ['for' => 'field'],
  2778. 'Field',
  2779. '/label',
  2780. 'input' => [
  2781. 'type' => 'text', 'name' => 'field',
  2782. 'id' => 'field', 'class' => 'form-error',
  2783. ],
  2784. ['div' => ['class' => 'error-message']],
  2785. 'Badness!',
  2786. '/div',
  2787. '/div',
  2788. ];
  2789. $this->assertHtml($expected, $result);
  2790. $result = $this->Form->control('field', [
  2791. 'templates' => [
  2792. 'inputContainerError' => '{{content}}{{error}}',
  2793. 'error' => '<span class="error-message">{{content}}</span>',
  2794. ],
  2795. ]);
  2796. $expected = [
  2797. 'label' => ['for' => 'field'],
  2798. 'Field',
  2799. '/label',
  2800. 'input' => [
  2801. 'type' => 'text', 'name' => 'field',
  2802. 'id' => 'field', 'class' => 'form-error',
  2803. ],
  2804. ['span' => ['class' => 'error-message']],
  2805. 'Badness!',
  2806. '/span',
  2807. ];
  2808. $this->assertHtml($expected, $result);
  2809. $entity->setError('field', ['minLength'], true);
  2810. $result = $this->Form->control('field', [
  2811. 'error' => [
  2812. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2813. 'maxLength' => 'login too large',
  2814. ],
  2815. ]);
  2816. $expected = [
  2817. 'div' => ['class' => 'input text error'],
  2818. 'label' => ['for' => 'field'],
  2819. 'Field',
  2820. '/label',
  2821. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2822. ['div' => ['class' => 'error-message']],
  2823. 'Le login doit contenir au moins 2 caractères',
  2824. '/div',
  2825. '/div',
  2826. ];
  2827. $this->assertHtml($expected, $result);
  2828. $entity->setError('field', ['maxLength'], true);
  2829. $result = $this->Form->control('field', [
  2830. 'error' => [
  2831. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2832. 'maxLength' => 'login too large',
  2833. ],
  2834. ]);
  2835. $expected = [
  2836. 'div' => ['class' => 'input text error'],
  2837. 'label' => ['for' => 'field'],
  2838. 'Field',
  2839. '/label',
  2840. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2841. ['div' => ['class' => 'error-message']],
  2842. 'login too large',
  2843. '/div',
  2844. '/div',
  2845. ];
  2846. $this->assertHtml($expected, $result);
  2847. }
  2848. /**
  2849. * testControlWithTemplateFile method
  2850. *
  2851. * Test that control() accepts a template file.
  2852. *
  2853. * @return void
  2854. */
  2855. public function testControlWithTemplateFile()
  2856. {
  2857. $result = $this->Form->control('field', [
  2858. 'templates' => 'htmlhelper_tags',
  2859. ]);
  2860. $expected = [
  2861. 'label' => ['for' => 'field'],
  2862. 'Field',
  2863. '/label',
  2864. 'input' => [
  2865. 'type' => 'text', 'name' => 'field',
  2866. 'id' => 'field',
  2867. ],
  2868. ];
  2869. $this->assertHtml($expected, $result);
  2870. }
  2871. /**
  2872. * testNestedControlsEndWithBrackets method
  2873. *
  2874. * Test that nested inputs end with brackets.
  2875. *
  2876. * @return void
  2877. */
  2878. public function testNestedControlsEndWithBrackets()
  2879. {
  2880. $result = $this->Form->text('nested.text[]');
  2881. $expected = [
  2882. 'input' => [
  2883. 'type' => 'text', 'name' => 'nested[text][]',
  2884. ],
  2885. ];
  2886. $this->assertHtml($expected, $result);
  2887. $result = $this->Form->file('nested.file[]');
  2888. $expected = [
  2889. 'input' => [
  2890. 'type' => 'file', 'name' => 'nested[file][]',
  2891. ],
  2892. ];
  2893. $this->assertHtml($expected, $result);
  2894. }
  2895. /**
  2896. * testCreateIdPrefix method
  2897. *
  2898. * Test id prefix.
  2899. *
  2900. * @return void
  2901. */
  2902. public function testCreateIdPrefix()
  2903. {
  2904. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2905. $result = $this->Form->control('field');
  2906. $expected = [
  2907. 'div' => ['class' => 'input text'],
  2908. 'label' => ['for' => 'prefix-field'],
  2909. 'Field',
  2910. '/label',
  2911. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2912. '/div',
  2913. ];
  2914. $this->assertHtml($expected, $result);
  2915. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2916. $expected = [
  2917. 'div' => ['class' => 'input text'],
  2918. 'label' => ['for' => 'custom-id'],
  2919. 'Field',
  2920. '/label',
  2921. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2922. '/div',
  2923. ];
  2924. $this->assertHtml($expected, $result);
  2925. $result = $this->Form->radio('Model.field', ['option A']);
  2926. $expected = [
  2927. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2928. 'label' => ['for' => 'prefix-model-field-0'],
  2929. ['input' => [
  2930. 'type' => 'radio',
  2931. 'name' => 'Model[field]',
  2932. 'value' => '0',
  2933. 'id' => 'prefix-model-field-0',
  2934. ]],
  2935. 'option A',
  2936. '/label',
  2937. ];
  2938. $this->assertHtml($expected, $result);
  2939. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  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->select(
  2954. 'Model.multi_field',
  2955. ['first'],
  2956. ['multiple' => 'checkbox']
  2957. );
  2958. $expected = [
  2959. 'input' => [
  2960. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  2961. ],
  2962. ['div' => ['class' => 'checkbox']],
  2963. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2964. ['input' => [
  2965. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2966. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2967. ]],
  2968. 'first',
  2969. '/label',
  2970. '/div',
  2971. ];
  2972. $this->assertHtml($expected, $result);
  2973. $this->Form->end();
  2974. $result = $this->Form->control('field');
  2975. $expected = [
  2976. 'div' => ['class' => 'input text'],
  2977. 'label' => ['for' => 'field'],
  2978. 'Field',
  2979. '/label',
  2980. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2981. '/div',
  2982. ];
  2983. $this->assertHtml($expected, $result);
  2984. }
  2985. /**
  2986. * testControlZero method
  2987. *
  2988. * Test that inputs with 0 can be created.
  2989. *
  2990. * @return void
  2991. */
  2992. public function testControlZero()
  2993. {
  2994. $this->getTableLocator()->get('Contacts', [
  2995. 'className' => ContactsTable::class,
  2996. ]);
  2997. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2998. $result = $this->Form->control('0');
  2999. $expected = [
  3000. 'div' => ['class' => 'input text'],
  3001. 'label' => ['for' => '0'], '/label',
  3002. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3003. '/div',
  3004. ];
  3005. $this->assertHtml($expected, $result);
  3006. }
  3007. /**
  3008. * testControlCheckbox method
  3009. *
  3010. * Test control() with checkbox creation.
  3011. *
  3012. * @return void
  3013. */
  3014. public function testControlCheckbox()
  3015. {
  3016. $articles = $this->getTableLocator()->get('Articles');
  3017. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3018. $article = $articles->createEntity();
  3019. $this->Form->create($article);
  3020. $result = $this->Form->control('Articles.active');
  3021. $expected = [
  3022. 'div' => ['class' => 'input checkbox'],
  3023. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3024. 'label' => ['for' => 'articles-active'],
  3025. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3026. 'Active',
  3027. '/label',
  3028. '/div',
  3029. ];
  3030. $this->assertHtml($expected, $result);
  3031. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3032. $expected = [
  3033. 'div' => ['class' => 'input checkbox'],
  3034. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3035. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3036. '/div',
  3037. ];
  3038. $this->assertHtml($expected, $result);
  3039. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3040. $expected = [
  3041. 'div' => ['class' => 'input checkbox'],
  3042. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3043. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3044. '/div',
  3045. ];
  3046. $this->assertHtml($expected, $result);
  3047. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3048. $expected = [
  3049. 'div' => ['class' => 'input checkbox'],
  3050. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3051. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3052. '/div',
  3053. ];
  3054. $this->assertHtml($expected, $result);
  3055. $result = $this->Form->control('Articles.disabled', [
  3056. 'label' => 'Disabled',
  3057. 'type' => 'checkbox',
  3058. 'data-foo' => 'disabled',
  3059. ]);
  3060. $expected = [
  3061. 'div' => ['class' => 'input checkbox'],
  3062. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3063. 'label' => ['for' => 'articles-disabled'],
  3064. ['input' => [
  3065. 'type' => 'checkbox',
  3066. 'name' => 'Articles[disabled]',
  3067. 'value' => '1',
  3068. 'id' => 'articles-disabled',
  3069. 'data-foo' => 'disabled',
  3070. ]],
  3071. 'Disabled',
  3072. '/label',
  3073. '/div',
  3074. ];
  3075. $this->assertHtml($expected, $result);
  3076. $result = $this->Form->control('Articles.confirm', [
  3077. 'label' => 'Confirm <b>me</b>!',
  3078. 'type' => 'checkbox',
  3079. 'escape' => false,
  3080. ]);
  3081. $expected = [
  3082. 'div' => ['class' => 'input checkbox'],
  3083. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3084. 'label' => ['for' => 'articles-confirm'],
  3085. ['input' => [
  3086. 'type' => 'checkbox',
  3087. 'name' => 'Articles[confirm]',
  3088. 'value' => '1',
  3089. 'id' => 'articles-confirm',
  3090. ]],
  3091. 'Confirm <b>me</b>!',
  3092. '/label',
  3093. '/div',
  3094. ];
  3095. $this->assertHtml($expected, $result);
  3096. }
  3097. /**
  3098. * testControlHidden method
  3099. *
  3100. * Test that control() does not create wrapping div and label tag for hidden fields.
  3101. *
  3102. * @return void
  3103. */
  3104. public function testControlHidden()
  3105. {
  3106. $this->getTableLocator()->get('ValidateUsers', [
  3107. 'className' => ValidateUsersTable::class,
  3108. ]);
  3109. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3110. $result = $this->Form->control('ValidateUser.id');
  3111. $expected = [
  3112. 'input' => ['name', 'type' => 'hidden', 'id'],
  3113. ];
  3114. $this->assertHtml($expected, $result);
  3115. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3116. $expected = [
  3117. 'input' => ['name', 'type' => 'hidden', 'id'],
  3118. ];
  3119. $this->assertHtml($expected, $result);
  3120. }
  3121. /**
  3122. * testControlDatetime method
  3123. *
  3124. * Test form->control() with datetime.
  3125. *
  3126. * @return void
  3127. */
  3128. public function testControlDatetime()
  3129. {
  3130. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3131. ->setMethods(['datetime'])
  3132. ->setConstructorArgs([new View()])
  3133. ->getMock();
  3134. $this->Form->expects($this->once())->method('datetime')
  3135. ->with('prueba', [
  3136. 'type' => 'datetime',
  3137. 'timeFormat' => 24,
  3138. 'minYear' => 2008,
  3139. 'maxYear' => 2011,
  3140. 'interval' => 15,
  3141. 'options' => null,
  3142. 'empty' => false,
  3143. 'id' => 'prueba',
  3144. 'required' => false,
  3145. 'templateVars' => [],
  3146. ])
  3147. ->will($this->returnValue('This is it!'));
  3148. $result = $this->Form->control('prueba', [
  3149. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3150. 'maxYear' => 2011, 'interval' => 15,
  3151. ]);
  3152. $expected = [
  3153. 'div' => ['class' => 'input datetime'],
  3154. '<label',
  3155. 'Prueba',
  3156. '/label',
  3157. 'This is it!',
  3158. '/div',
  3159. ];
  3160. $this->assertHtml($expected, $result);
  3161. }
  3162. /**
  3163. * testControlDatetimeIdPrefix method
  3164. *
  3165. * Test form->control() with datetime with id prefix.
  3166. *
  3167. * @return void
  3168. */
  3169. public function testControlDatetimeIdPrefix()
  3170. {
  3171. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3172. ->setMethods(['datetime'])
  3173. ->setConstructorArgs([new View()])
  3174. ->getMock();
  3175. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3176. $this->Form->expects($this->once())->method('datetime')
  3177. ->with('prueba', [
  3178. 'type' => 'datetime',
  3179. 'timeFormat' => 24,
  3180. 'minYear' => 2008,
  3181. 'maxYear' => 2011,
  3182. 'interval' => 15,
  3183. 'options' => null,
  3184. 'empty' => false,
  3185. 'id' => 'prefix-prueba',
  3186. 'required' => false,
  3187. 'templateVars' => [],
  3188. ])
  3189. ->will($this->returnValue('This is it!'));
  3190. $result = $this->Form->control('prueba', [
  3191. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3192. 'maxYear' => 2011, 'interval' => 15,
  3193. ]);
  3194. $expected = [
  3195. 'div' => ['class' => 'input datetime'],
  3196. '<label',
  3197. 'Prueba',
  3198. '/label',
  3199. 'This is it!',
  3200. '/div',
  3201. ];
  3202. $this->assertHtml($expected, $result);
  3203. }
  3204. /**
  3205. * testControlCheckboxWithDisabledElements method
  3206. *
  3207. * Test generating checkboxes with disabled elements.
  3208. *
  3209. * @return void
  3210. */
  3211. public function testControlCheckboxWithDisabledElements()
  3212. {
  3213. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3214. $result = $this->Form->control('Contact.multiple', [
  3215. 'multiple' => 'checkbox',
  3216. 'disabled' => 'disabled',
  3217. 'options' => $options,
  3218. ]);
  3219. $expected = [
  3220. ['div' => ['class' => 'input select']],
  3221. ['label' => ['for' => 'contact-multiple']],
  3222. 'Multiple',
  3223. '/label',
  3224. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3225. ['div' => ['class' => 'checkbox']],
  3226. ['label' => ['for' => 'contact-multiple-1']],
  3227. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3228. 'One',
  3229. '/label',
  3230. '/div',
  3231. ['div' => ['class' => 'checkbox']],
  3232. ['label' => ['for' => 'contact-multiple-2']],
  3233. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3234. 'Two',
  3235. '/label',
  3236. '/div',
  3237. ['div' => ['class' => 'checkbox']],
  3238. ['label' => ['for' => 'contact-multiple-3']],
  3239. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3240. 'Three',
  3241. '/label',
  3242. '/div',
  3243. '/div',
  3244. ];
  3245. $this->assertHtml($expected, $result);
  3246. // make sure 50 does only disable 50, and not 50f5c0cf
  3247. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3248. $disabled = [50];
  3249. $expected = [
  3250. ['div' => ['class' => 'input select']],
  3251. ['label' => ['for' => 'contact-multiple']],
  3252. 'Multiple',
  3253. '/label',
  3254. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3255. ['div' => ['class' => 'checkbox']],
  3256. ['label' => ['for' => 'contact-multiple-50']],
  3257. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3258. 'Fifty',
  3259. '/label',
  3260. '/div',
  3261. ['div' => ['class' => 'checkbox']],
  3262. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3263. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3264. 'Stringy',
  3265. '/label',
  3266. '/div',
  3267. '/div',
  3268. ];
  3269. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3270. $this->assertHtml($expected, $result);
  3271. }
  3272. /**
  3273. * testControlWithLeadingInteger method
  3274. *
  3275. * Test input name with leading integer, ensure attributes are generated correctly.
  3276. *
  3277. * @return void
  3278. */
  3279. public function testControlWithLeadingInteger()
  3280. {
  3281. $result = $this->Form->text('0.Node.title');
  3282. $expected = [
  3283. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3284. ];
  3285. $this->assertHtml($expected, $result);
  3286. }
  3287. /**
  3288. * testControlSelectType method
  3289. *
  3290. * Test form->control() with select type inputs.
  3291. *
  3292. * @return void
  3293. */
  3294. public function testControlSelectType()
  3295. {
  3296. $result = $this->Form->control(
  3297. 'email',
  3298. [
  3299. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], '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' => 'è']],
  3310. 'Firést',
  3311. '/option',
  3312. ['option' => ['value' => 'é']],
  3313. 'Secoènd',
  3314. '/option',
  3315. '/select',
  3316. '/div',
  3317. ];
  3318. $this->assertHtml($expected, $result);
  3319. $result = $this->Form->control(
  3320. 'email',
  3321. [
  3322. 'options' => ['First', 'Second'], 'empty' => true]
  3323. );
  3324. $expected = [
  3325. 'div' => ['class' => 'input select'],
  3326. 'label' => ['for' => 'email'],
  3327. 'Email',
  3328. '/label',
  3329. ['select' => ['name' => 'email', 'id' => 'email']],
  3330. ['option' => ['value' => '']],
  3331. '/option',
  3332. ['option' => ['value' => '0']],
  3333. 'First',
  3334. '/option',
  3335. ['option' => ['value' => '1']],
  3336. 'Second',
  3337. '/option',
  3338. '/select',
  3339. '/div',
  3340. ];
  3341. $this->assertHtml($expected, $result);
  3342. $result = $this->Form->control('email', [
  3343. 'type' => 'select',
  3344. 'options' => new \ArrayObject(['First', 'Second']),
  3345. 'empty' => true,
  3346. ]);
  3347. $this->assertHtml($expected, $result);
  3348. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3349. $this->View->setRequest(
  3350. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3351. );
  3352. $this->Form->create();
  3353. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3354. $expected = [
  3355. 'div' => ['class' => 'input select'],
  3356. 'label' => ['for' => 'model-user-id'],
  3357. 'User',
  3358. '/label',
  3359. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3360. ['option' => ['value' => '']],
  3361. '/option',
  3362. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3363. 'good',
  3364. '/option',
  3365. ['option' => ['value' => 'other']],
  3366. 'bad',
  3367. '/option',
  3368. '/select',
  3369. '/div',
  3370. ];
  3371. $this->assertHtml($expected, $result);
  3372. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3373. $this->View->setRequest(
  3374. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3375. );
  3376. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3377. $expected = [
  3378. 'div' => ['class' => 'input select'],
  3379. 'label' => ['for' => 'thing-user-id'],
  3380. 'User',
  3381. '/label',
  3382. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3383. ['option' => ['value' => '']],
  3384. 'Some Empty',
  3385. '/option',
  3386. ['option' => ['value' => 'value']],
  3387. 'good',
  3388. '/option',
  3389. ['option' => ['value' => 'other']],
  3390. 'bad',
  3391. '/option',
  3392. '/select',
  3393. '/div',
  3394. ];
  3395. $this->assertHtml($expected, $result);
  3396. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3397. $this->View->setRequest(
  3398. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3399. );
  3400. $this->Form->create();
  3401. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3402. $expected = [
  3403. 'div' => ['class' => 'input select'],
  3404. 'label' => ['for' => 'thing-user-id'],
  3405. 'User',
  3406. '/label',
  3407. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3408. ['option' => ['value' => '']],
  3409. 'Some Empty',
  3410. '/option',
  3411. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3412. 'good',
  3413. '/option',
  3414. ['option' => ['value' => 'other']],
  3415. 'bad',
  3416. '/option',
  3417. '/select',
  3418. '/div',
  3419. ];
  3420. $this->assertHtml($expected, $result);
  3421. $result = $this->Form->control('Publisher.id', [
  3422. 'label' => 'Publisher',
  3423. 'type' => 'select',
  3424. 'multiple' => 'checkbox',
  3425. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3426. ]);
  3427. $expected = [
  3428. ['div' => ['class' => 'input select']],
  3429. ['label' => ['for' => 'publisher-id']],
  3430. 'Publisher',
  3431. '/label',
  3432. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3433. ['div' => ['class' => 'checkbox']],
  3434. ['label' => ['for' => 'publisher-id-value-1']],
  3435. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3436. 'Label 1',
  3437. '/label',
  3438. '/div',
  3439. ['div' => ['class' => 'checkbox']],
  3440. ['label' => ['for' => 'publisher-id-value-2']],
  3441. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3442. 'Label 2',
  3443. '/label',
  3444. '/div',
  3445. '/div',
  3446. ];
  3447. $this->assertHtml($expected, $result);
  3448. }
  3449. /**
  3450. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3451. *
  3452. * Test that control() and a non standard primary key makes a hidden input by default.
  3453. *
  3454. * @return void
  3455. */
  3456. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3457. {
  3458. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3459. $this->Form->create($this->article);
  3460. $result = $this->Form->control('title');
  3461. $expected = [
  3462. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3463. ];
  3464. $this->assertHtml($expected, $result);
  3465. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3466. $this->Form->create($this->article);
  3467. $result = $this->Form->control('title');
  3468. $expected = [
  3469. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3470. ];
  3471. $this->assertHtml($expected, $result);
  3472. $result = $this->Form->control('body');
  3473. $expected = [
  3474. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3475. ];
  3476. $this->assertHtml($expected, $result);
  3477. }
  3478. /**
  3479. * testControlOverridingMagicSelectType method
  3480. *
  3481. * Test that overriding the magic select type widget is possible.
  3482. *
  3483. * @return void
  3484. */
  3485. public function testControlOverridingMagicSelectType()
  3486. {
  3487. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3488. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3489. $expected = [
  3490. 'div' => ['class' => 'input text'],
  3491. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3492. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3493. '/div',
  3494. ];
  3495. $this->assertHtml($expected, $result);
  3496. //Check that magic types still work for plural/singular vars
  3497. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3498. $result = $this->Form->control('Model.type');
  3499. $expected = [
  3500. 'div' => ['class' => 'input select'],
  3501. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3502. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3503. ['option' => ['value' => 'value']], 'good', '/option',
  3504. ['option' => ['value' => 'other']], 'bad', '/option',
  3505. '/select',
  3506. '/div',
  3507. ];
  3508. $this->assertHtml($expected, $result);
  3509. }
  3510. /**
  3511. * testControlMagicTypeDoesNotOverride method
  3512. *
  3513. * Test that inferred types do not override developer input.
  3514. *
  3515. * @return void
  3516. */
  3517. public function testControlMagicTypeDoesNotOverride()
  3518. {
  3519. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3520. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3521. $expected = [
  3522. 'div' => ['class' => 'input checkbox'],
  3523. ['input' => [
  3524. 'type' => 'hidden',
  3525. 'name' => 'Model[user]',
  3526. 'value' => 0,
  3527. ]],
  3528. 'label' => ['for' => 'model-user'],
  3529. ['input' => [
  3530. 'name' => 'Model[user]',
  3531. 'type' => 'checkbox',
  3532. 'id' => 'model-user',
  3533. 'value' => 1,
  3534. ]],
  3535. 'User',
  3536. '/label',
  3537. '/div',
  3538. ];
  3539. $this->assertHtml($expected, $result);
  3540. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3541. $options = [
  3542. 1 => 'blue',
  3543. 2 => 'red',
  3544. ];
  3545. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3546. $expected = [
  3547. 'div' => ['class' => 'input select'],
  3548. 'label' => ['for' => 'tags-ids'],
  3549. 'Tags',
  3550. '/label',
  3551. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3552. ['div' => ['class' => 'checkbox']],
  3553. ['label' => ['for' => 'tags-ids-1']],
  3554. ['input' => [
  3555. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3556. 'value' => '1', 'name' => 'tags[_ids][]',
  3557. ]],
  3558. 'blue',
  3559. '/label',
  3560. '/div',
  3561. ['div' => ['class' => 'checkbox']],
  3562. ['label' => ['for' => 'tags-ids-2']],
  3563. ['input' => [
  3564. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3565. 'value' => '2', 'name' => 'tags[_ids][]',
  3566. ]],
  3567. 'red',
  3568. '/label',
  3569. '/div',
  3570. '/div',
  3571. ];
  3572. $this->assertHtml($expected, $result);
  3573. }
  3574. /**
  3575. * testControlMagicSelectForTypeNumber method
  3576. *
  3577. * Test that magic control() selects are created for type=number.
  3578. *
  3579. * @return void
  3580. */
  3581. public function testControlMagicSelectForTypeNumber()
  3582. {
  3583. $this->getTableLocator()->get('ValidateUsers', [
  3584. 'className' => ValidateUsersTable::class,
  3585. ]);
  3586. $entity = new Entity(['balance' => 1]);
  3587. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3588. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3589. $result = $this->Form->control('balance');
  3590. $expected = [
  3591. 'div' => ['class' => 'input select'],
  3592. 'label' => ['for' => 'balance'],
  3593. 'Balance',
  3594. '/label',
  3595. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3596. ['option' => ['value' => '0']],
  3597. 'nothing',
  3598. '/option',
  3599. ['option' => ['value' => '1', 'selected' => 'selected']],
  3600. 'some',
  3601. '/option',
  3602. ['option' => ['value' => '100']],
  3603. 'a lot',
  3604. '/option',
  3605. '/select',
  3606. '/div',
  3607. ];
  3608. $this->assertHtml($expected, $result);
  3609. }
  3610. /**
  3611. * testInvalidControlTypeOption method
  3612. *
  3613. * Test invalid 'input' type option to control() function.
  3614. *
  3615. * @return void
  3616. */
  3617. public function testInvalidControlTypeOption()
  3618. {
  3619. $this->expectException(\RuntimeException::class);
  3620. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3621. $this->Form->control('text', ['type' => 'input']);
  3622. }
  3623. /**
  3624. * testControlMagicSelectChangeToRadio method
  3625. *
  3626. * Test that magic control() selects can easily be converted into radio types without error.
  3627. *
  3628. * @return void
  3629. */
  3630. public function testControlMagicSelectChangeToRadio()
  3631. {
  3632. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3633. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3634. $this->assertContains('input type="radio"', $result);
  3635. }
  3636. /**
  3637. * testFormControlSubmit method
  3638. *
  3639. * Test correct results for form::control() and type submit.
  3640. *
  3641. * @return void
  3642. */
  3643. public function testFormControlSubmit()
  3644. {
  3645. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3646. $expected = [
  3647. 'div' => ['class' => 'submit'],
  3648. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3649. '/div',
  3650. ];
  3651. $this->assertHtml($expected, $result);
  3652. }
  3653. /**
  3654. * testFormControls method
  3655. *
  3656. * Test correct results from Form::controls().
  3657. *
  3658. * @return void
  3659. */
  3660. public function testFormControlsLegendFieldset()
  3661. {
  3662. $this->Form->create($this->article);
  3663. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3664. $expected = [
  3665. '<fieldset',
  3666. '<legend',
  3667. 'The Legend',
  3668. '/legend',
  3669. '*/fieldset',
  3670. ];
  3671. $this->assertHtml($expected, $result);
  3672. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3673. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3674. $this->assertContains('<fieldset>', $result);
  3675. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3676. $this->assertNotContains('<legend>', $result);
  3677. $this->assertNotContains('<fieldset>', $result);
  3678. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3679. $this->assertNotContains('<legend>', $result);
  3680. $this->assertNotContains('<fieldset>', $result);
  3681. $this->Form->create($this->article);
  3682. $this->View->setRequest($this->View->getRequest()
  3683. ->withParam('prefix', 'admin')
  3684. ->withParam('action', 'admin_edit')
  3685. ->withParam('controller', 'articles'));
  3686. $result = $this->Form->allControls();
  3687. $expected = [
  3688. '<fieldset',
  3689. '<legend',
  3690. 'New Article',
  3691. '/legend',
  3692. '*/fieldset',
  3693. ];
  3694. $this->assertHtml($expected, $result);
  3695. $this->Form->create($this->article);
  3696. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3697. $expected = [
  3698. '<fieldset',
  3699. '<legend',
  3700. 'The Legend',
  3701. '/legend',
  3702. '*/fieldset',
  3703. ];
  3704. $this->assertHtml($expected, $result);
  3705. $this->Form->create($this->article);
  3706. $result = $this->Form->allControls([], [
  3707. 'fieldset' => [
  3708. 'class' => 'some-class some-other-class',
  3709. 'disabled' => true,
  3710. 'data-param' => 'a-param',
  3711. ],
  3712. 'legend' => 'The Legend',
  3713. ]);
  3714. $expected = [
  3715. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3716. '<legend',
  3717. 'The Legend',
  3718. '/legend',
  3719. '*/fieldset',
  3720. ];
  3721. $this->assertHtml($expected, $result);
  3722. }
  3723. /**
  3724. * testFormControls method
  3725. *
  3726. * Test the controls() method.
  3727. *
  3728. * @return void
  3729. */
  3730. public function testFormControls()
  3731. {
  3732. $this->Form->create($this->article);
  3733. $result = $this->Form->allControls();
  3734. $expected = [
  3735. '<fieldset',
  3736. '<legend', 'New Article', '/legend',
  3737. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3738. ['div' => ['class' => 'input select required']],
  3739. '*/div',
  3740. ['div' => ['class' => 'input text required']],
  3741. '*/div',
  3742. ['div' => ['class' => 'input text']],
  3743. '*/div',
  3744. ['div' => ['class' => 'input text']],
  3745. '*/div',
  3746. '/fieldset',
  3747. ];
  3748. $this->assertHtml($expected, $result);
  3749. $result = $this->Form->allControls([
  3750. 'published' => ['type' => 'boolean'],
  3751. ]);
  3752. $expected = [
  3753. '<fieldset',
  3754. '<legend', 'New Article', '/legend',
  3755. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3756. ['div' => ['class' => 'input select required']],
  3757. '*/div',
  3758. ['div' => ['class' => 'input text required']],
  3759. '*/div',
  3760. ['div' => ['class' => 'input text']],
  3761. '*/div',
  3762. ['div' => ['class' => 'input boolean']],
  3763. '*/div',
  3764. '/fieldset',
  3765. ];
  3766. $this->assertHtml($expected, $result);
  3767. $this->Form->create($this->article);
  3768. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3769. $expected = [
  3770. 'fieldset' => [],
  3771. 'legend' => [],
  3772. 'Hello',
  3773. '/legend',
  3774. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3775. ['div' => ['class' => 'input select required']],
  3776. '*/div',
  3777. ['div' => ['class' => 'input text required']],
  3778. '*/div',
  3779. ['div' => ['class' => 'input text']],
  3780. '*/div',
  3781. ['div' => ['class' => 'input text']],
  3782. '*/div',
  3783. '/fieldset',
  3784. ];
  3785. $this->assertHtml($expected, $result);
  3786. $this->Form->create(false);
  3787. $expected = [
  3788. 'fieldset' => [],
  3789. ['div' => ['class' => 'input text']],
  3790. 'label' => ['for' => 'foo'],
  3791. 'Foo',
  3792. '/label',
  3793. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3794. '*/div',
  3795. '/fieldset',
  3796. ];
  3797. $result = $this->Form->allControls(
  3798. ['foo' => ['type' => 'text']],
  3799. ['legend' => false]
  3800. );
  3801. $this->assertHtml($expected, $result);
  3802. }
  3803. /**
  3804. * testFormControlsBlacklist method
  3805. *
  3806. * @return void
  3807. */
  3808. public function testFormControlsBlacklist()
  3809. {
  3810. $this->Form->create($this->article);
  3811. $result = $this->Form->allControls([
  3812. 'id' => false,
  3813. ]);
  3814. $expected = [
  3815. '<fieldset',
  3816. '<legend', 'New Article', '/legend',
  3817. ['div' => ['class' => 'input select required']],
  3818. '*/div',
  3819. ['div' => ['class' => 'input text required']],
  3820. '*/div',
  3821. ['div' => ['class' => 'input text']],
  3822. '*/div',
  3823. ['div' => ['class' => 'input text']],
  3824. '*/div',
  3825. '/fieldset',
  3826. ];
  3827. $this->assertHtml($expected, $result);
  3828. $this->Form->create($this->article);
  3829. $result = $this->Form->allControls([
  3830. 'id' => [],
  3831. ]);
  3832. $expected = [
  3833. '<fieldset',
  3834. '<legend', 'New Article', '/legend',
  3835. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3836. ['div' => ['class' => 'input select required']],
  3837. '*/div',
  3838. ['div' => ['class' => 'input text required']],
  3839. '*/div',
  3840. ['div' => ['class' => 'input text']],
  3841. '*/div',
  3842. ['div' => ['class' => 'input text']],
  3843. '*/div',
  3844. '/fieldset',
  3845. ];
  3846. $this->assertHtml($expected, $result, true);
  3847. }
  3848. /**
  3849. * testSelectAsCheckbox method
  3850. *
  3851. * Test multi-select widget with checkbox formatting.
  3852. *
  3853. * @return void
  3854. */
  3855. public function testSelectAsCheckbox()
  3856. {
  3857. $result = $this->Form->select(
  3858. 'Model.multi_field',
  3859. ['first', 'second', 'third'],
  3860. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3861. );
  3862. $expected = [
  3863. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3864. ['div' => ['class' => 'checkbox']],
  3865. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3866. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3867. 'first',
  3868. '/label',
  3869. '/div',
  3870. ['div' => ['class' => 'checkbox']],
  3871. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3872. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3873. 'second',
  3874. '/label',
  3875. '/div',
  3876. ['div' => ['class' => 'checkbox']],
  3877. ['label' => ['for' => 'model-multi-field-2']],
  3878. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3879. 'third',
  3880. '/label',
  3881. '/div',
  3882. ];
  3883. $this->assertHtml($expected, $result);
  3884. $result = $this->Form->select(
  3885. 'Model.multi_field',
  3886. ['1/2' => 'half'],
  3887. ['multiple' => 'checkbox']
  3888. );
  3889. $expected = [
  3890. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3891. ['div' => ['class' => 'checkbox']],
  3892. ['label' => ['for' => 'model-multi-field-1-2']],
  3893. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3894. 'half',
  3895. '/label',
  3896. '/div',
  3897. ];
  3898. $this->assertHtml($expected, $result);
  3899. }
  3900. /**
  3901. * testLabel method
  3902. *
  3903. * Test label generation.
  3904. *
  3905. * @return void
  3906. */
  3907. public function testLabel()
  3908. {
  3909. $result = $this->Form->label('Person.name');
  3910. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3911. $this->assertHtml($expected, $result);
  3912. $result = $this->Form->label('Person.name');
  3913. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3914. $this->assertHtml($expected, $result);
  3915. $result = $this->Form->label('Person.first_name');
  3916. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3917. $this->assertHtml($expected, $result);
  3918. $result = $this->Form->label('Person.first_name', 'Your first name');
  3919. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3920. $this->assertHtml($expected, $result);
  3921. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3922. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3923. $this->assertHtml($expected, $result);
  3924. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3925. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3926. $this->assertHtml($expected, $result);
  3927. $result = $this->Form->label('Person.first_name', '');
  3928. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3929. $this->assertHtml($expected, $result);
  3930. $result = $this->Form->label('Person.2.name', '');
  3931. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3932. $this->assertHtml($expected, $result);
  3933. }
  3934. /**
  3935. * testLabelContainControl method
  3936. *
  3937. * Test that label() can accept an input with the correct template vars.
  3938. *
  3939. * @return void
  3940. */
  3941. public function testLabelContainControl()
  3942. {
  3943. $this->Form->setTemplates([
  3944. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3945. ]);
  3946. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3947. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3948. ]);
  3949. $expected = [
  3950. 'label' => ['for' => 'person-accept-terms'],
  3951. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3952. 'Accept',
  3953. '/label',
  3954. ];
  3955. $this->assertHtml($expected, $result);
  3956. }
  3957. /**
  3958. * testTextbox method
  3959. *
  3960. * Test textbox element generation.
  3961. *
  3962. * @return void
  3963. */
  3964. public function testTextbox()
  3965. {
  3966. $result = $this->Form->text('Model.field');
  3967. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3968. $this->assertHtml($expected, $result);
  3969. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3970. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3971. $this->assertHtml($expected, $result);
  3972. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3973. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3974. $this->assertHtml($expected, $result);
  3975. }
  3976. /**
  3977. * testTextBoxDataAndError method
  3978. *
  3979. * Test that text() hooks up with request data and error fields.
  3980. *
  3981. * @return void
  3982. */
  3983. public function testTextBoxDataAndError()
  3984. {
  3985. $this->article['errors'] = [
  3986. 'Contact' => ['text' => 'wrong'],
  3987. ];
  3988. $this->View->setRequest($this->View->getRequest()
  3989. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3990. ->withData('Contact.text', 'test'));
  3991. $this->Form->create($this->article);
  3992. $result = $this->Form->text('Model.text');
  3993. $expected = [
  3994. 'input' => [
  3995. 'type' => 'text',
  3996. 'name' => 'Model[text]',
  3997. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3998. ],
  3999. ];
  4000. $this->assertHtml($expected, $result);
  4001. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4002. $expected = [
  4003. 'input' => [
  4004. 'type' => 'text',
  4005. 'name' => 'Contact[text]',
  4006. 'value' => 'test',
  4007. 'id' => 'theID',
  4008. 'class' => 'form-error',
  4009. ],
  4010. ];
  4011. $this->assertHtml($expected, $result);
  4012. }
  4013. /**
  4014. * testDefaultValue method
  4015. *
  4016. * Test default value setting.
  4017. *
  4018. * @return void
  4019. */
  4020. public function testTextDefaultValue()
  4021. {
  4022. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4023. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4024. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4025. $this->assertHtml($expected, $result);
  4026. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4027. $this->Form->create();
  4028. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4029. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4030. $this->assertHtml($expected, $result);
  4031. $Articles = $this->getTableLocator()->get('Articles');
  4032. $title = $Articles->getSchema()->getColumn('title');
  4033. $Articles->getSchema()->addColumn(
  4034. 'title',
  4035. ['default' => 'default title'] + $title
  4036. );
  4037. $entity = $Articles->createEntity();
  4038. $this->Form->create($entity);
  4039. // Get default value from schema
  4040. $result = $this->Form->text('title');
  4041. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4042. $this->assertHtml($expected, $result);
  4043. // Don't get value from schema
  4044. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4045. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4046. $this->assertHtml($expected, $result);
  4047. // Custom default value overrides default value from schema
  4048. $result = $this->Form->text('title', ['default' => 'override default']);
  4049. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4050. $this->assertHtml($expected, $result);
  4051. // Default value from schema is used only for new entities.
  4052. $entity->isNew(false);
  4053. $result = $this->Form->text('title');
  4054. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4055. $this->assertHtml($expected, $result);
  4056. }
  4057. /**
  4058. * testError method
  4059. *
  4060. * Test field error generation.
  4061. *
  4062. * @return void
  4063. */
  4064. public function testError()
  4065. {
  4066. $this->article['errors'] = [
  4067. 'Article' => ['field' => 'email'],
  4068. ];
  4069. $this->Form->create($this->article);
  4070. $result = $this->Form->error('Article.field');
  4071. $expected = [
  4072. ['div' => ['class' => 'error-message']],
  4073. 'email',
  4074. '/div',
  4075. ];
  4076. $this->assertHtml($expected, $result);
  4077. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4078. $expected = [
  4079. ['div' => ['class' => 'error-message']],
  4080. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4081. '/div',
  4082. ];
  4083. $this->assertHtml($expected, $result);
  4084. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4085. $expected = [
  4086. ['div' => ['class' => 'error-message']],
  4087. '<strong', 'Badness!', '/strong',
  4088. '/div',
  4089. ];
  4090. $this->assertHtml($expected, $result);
  4091. }
  4092. /**
  4093. * testErrorRuleName method
  4094. *
  4095. * Test error translation can use rule names for translating.
  4096. *
  4097. * @return void
  4098. */
  4099. public function testErrorRuleName()
  4100. {
  4101. $this->article['errors'] = [
  4102. 'Article' => [
  4103. 'field' => ['email' => 'Your email was not good'],
  4104. ],
  4105. ];
  4106. $this->Form->create($this->article);
  4107. $result = $this->Form->error('Article.field');
  4108. $expected = [
  4109. ['div' => ['class' => 'error-message']],
  4110. 'Your email was not good',
  4111. '/div',
  4112. ];
  4113. $this->assertHtml($expected, $result);
  4114. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4115. $expected = [
  4116. ['div' => ['class' => 'error-message']],
  4117. 'Email in use',
  4118. '/div',
  4119. ];
  4120. $this->assertHtml($expected, $result);
  4121. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4122. $expected = [
  4123. ['div' => ['class' => 'error-message']],
  4124. 'Email in use',
  4125. '/div',
  4126. ];
  4127. $this->assertHtml($expected, $result);
  4128. $result = $this->Form->error('Article.field', [
  4129. 'email' => 'Key is preferred',
  4130. 'Your email was not good' => 'Email in use',
  4131. ]);
  4132. $expected = [
  4133. ['div' => ['class' => 'error-message']],
  4134. 'Key is preferred',
  4135. '/div',
  4136. ];
  4137. $this->assertHtml($expected, $result);
  4138. }
  4139. /**
  4140. * testErrorMessages method
  4141. *
  4142. * Test error with nested lists.
  4143. *
  4144. * @return void
  4145. */
  4146. public function testErrorMessages()
  4147. {
  4148. $this->article['errors'] = [
  4149. 'Article' => ['field' => 'email'],
  4150. ];
  4151. $this->Form->create($this->article);
  4152. $result = $this->Form->error('Article.field', [
  4153. 'email' => 'No good!',
  4154. ]);
  4155. $expected = [
  4156. 'div' => ['class' => 'error-message'],
  4157. 'No good!',
  4158. '/div',
  4159. ];
  4160. $this->assertHtml($expected, $result);
  4161. }
  4162. /**
  4163. * testErrorMultipleMessages method
  4164. *
  4165. * Test error() with multiple messages.
  4166. *
  4167. * @return void
  4168. */
  4169. public function testErrorMultipleMessages()
  4170. {
  4171. $this->article['errors'] = [
  4172. 'field' => ['notBlank', 'email', 'Something else'],
  4173. ];
  4174. $this->Form->create($this->article);
  4175. $result = $this->Form->error('field', [
  4176. 'notBlank' => 'Cannot be empty',
  4177. 'email' => 'No good!',
  4178. ]);
  4179. $expected = [
  4180. 'div' => ['class' => 'error-message'],
  4181. 'ul' => [],
  4182. '<li', 'Cannot be empty', '/li',
  4183. '<li', 'No good!', '/li',
  4184. '<li', 'Something else', '/li',
  4185. '/ul',
  4186. '/div',
  4187. ];
  4188. $this->assertHtml($expected, $result);
  4189. }
  4190. /**
  4191. * testPassword method
  4192. *
  4193. * Test password element generation.
  4194. *
  4195. * @return void
  4196. */
  4197. public function testPassword()
  4198. {
  4199. $this->article['errors'] = [
  4200. 'Contact' => [
  4201. 'passwd' => 1,
  4202. ],
  4203. ];
  4204. $this->Form->create($this->article);
  4205. $result = $this->Form->password('Contact.field');
  4206. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4207. $this->assertHtml($expected, $result);
  4208. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4209. $this->Form->create($this->article);
  4210. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4211. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4212. $this->assertHtml($expected, $result);
  4213. }
  4214. /**
  4215. * testRadio method
  4216. *
  4217. * Test radio element set generation.
  4218. *
  4219. * @return void
  4220. */
  4221. public function testRadio()
  4222. {
  4223. $result = $this->Form->radio('Model.field', ['option A']);
  4224. $expected = [
  4225. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4226. 'label' => ['for' => 'model-field-0'],
  4227. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4228. 'option A',
  4229. '/label',
  4230. ];
  4231. $this->assertHtml($expected, $result);
  4232. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4233. $this->assertHtml($expected, $result);
  4234. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4235. $expected = [
  4236. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4237. ['label' => ['for' => 'model-field-0']],
  4238. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4239. 'option A',
  4240. '/label',
  4241. ['label' => ['for' => 'model-field-1']],
  4242. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4243. 'option B',
  4244. '/label',
  4245. ];
  4246. $this->assertHtml($expected, $result);
  4247. $result = $this->Form->radio(
  4248. 'Employee.gender',
  4249. ['male' => 'Male', 'female' => 'Female'],
  4250. ['form' => 'my-form']
  4251. );
  4252. $expected = [
  4253. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4254. ['label' => ['for' => 'employee-gender-male']],
  4255. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4256. 'Male',
  4257. '/label',
  4258. ['label' => ['for' => 'employee-gender-female']],
  4259. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4260. 'Female',
  4261. '/label',
  4262. ];
  4263. $this->assertHtml($expected, $result);
  4264. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4265. $expected = [
  4266. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4267. ['label' => ['for' => 'model-custom-0']],
  4268. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4269. 'option A',
  4270. '/label',
  4271. ['label' => ['for' => 'model-custom-1']],
  4272. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4273. 'option B',
  4274. '/label',
  4275. ];
  4276. $this->assertHtml($expected, $result);
  4277. $result = $this->Form->radio(
  4278. 'Employee.gender',
  4279. [
  4280. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4281. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4282. ]
  4283. );
  4284. $expected = [
  4285. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4286. ['label' => ['for' => 'employee-gender-male']],
  4287. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4288. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4289. 'Male',
  4290. '/label',
  4291. ['label' => ['for' => 'employee-gender-female']],
  4292. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4293. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4294. 'Female',
  4295. '/label',
  4296. ];
  4297. $this->assertHtml($expected, $result);
  4298. }
  4299. /**
  4300. * Test radio with complex options and empty disabled data.
  4301. *
  4302. * @return void
  4303. */
  4304. public function testRadioComplexDisabled()
  4305. {
  4306. $options = [
  4307. ['value' => 'r', 'text' => 'red'],
  4308. ['value' => 'b', 'text' => 'blue'],
  4309. ];
  4310. $attrs = ['disabled' => []];
  4311. $result = $this->Form->radio('Model.field', $options, $attrs);
  4312. $expected = [
  4313. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4314. ['label' => ['for' => 'model-field-r']],
  4315. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4316. 'red',
  4317. '/label',
  4318. ['label' => ['for' => 'model-field-b']],
  4319. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4320. 'blue',
  4321. '/label',
  4322. ];
  4323. $this->assertHtml($expected, $result);
  4324. $attrs = ['disabled' => ['r']];
  4325. $result = $this->Form->radio('Model.field', $options, $attrs);
  4326. $this->assertContains('disabled="disabled"', $result);
  4327. }
  4328. /**
  4329. * testRadioDefaultValue method
  4330. *
  4331. * Test default value setting on radio() method.
  4332. *
  4333. * @return void
  4334. */
  4335. public function testRadioDefaultValue()
  4336. {
  4337. $Articles = $this->getTableLocator()->get('Articles');
  4338. $title = $Articles->getSchema()->getColumn('title');
  4339. $Articles->getSchema()->addColumn(
  4340. 'title',
  4341. ['default' => '1'] + $title
  4342. );
  4343. $this->Form->create($Articles->createEntity());
  4344. $result = $this->Form->radio('title', ['option A', 'option B']);
  4345. $expected = [
  4346. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4347. ['label' => ['for' => 'title-0']],
  4348. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4349. 'option A',
  4350. '/label',
  4351. ['label' => ['for' => 'title-1']],
  4352. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4353. 'option B',
  4354. '/label',
  4355. ];
  4356. $this->assertHtml($expected, $result);
  4357. }
  4358. /**
  4359. * Test setting a hiddenField value on radio buttons.
  4360. *
  4361. * @return void
  4362. */
  4363. public function testRadioHiddenFieldValue()
  4364. {
  4365. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4366. $expected = [
  4367. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4368. 'label' => ['for' => 'title-0'],
  4369. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4370. 'option A',
  4371. '/label',
  4372. ];
  4373. $this->assertHtml($expected, $result);
  4374. }
  4375. /**
  4376. * testControlRadio method
  4377. *
  4378. * Test that input works with radio types.
  4379. *
  4380. * @return void
  4381. */
  4382. public function testControlRadio()
  4383. {
  4384. $result = $this->Form->control('test', [
  4385. 'type' => 'radio',
  4386. 'options' => ['A', 'B'],
  4387. ]);
  4388. $expected = [
  4389. ['div' => ['class' => 'input radio']],
  4390. '<label',
  4391. 'Test',
  4392. '/label',
  4393. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4394. ['label' => ['for' => 'test-0']],
  4395. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4396. 'A',
  4397. '/label',
  4398. ['label' => ['for' => 'test-1']],
  4399. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4400. 'B',
  4401. '/label',
  4402. '/div',
  4403. ];
  4404. $this->assertHtml($expected, $result);
  4405. $result = $this->Form->control('test', [
  4406. 'type' => 'radio',
  4407. 'options' => ['A', 'B'],
  4408. 'value' => '0',
  4409. ]);
  4410. $expected = [
  4411. ['div' => ['class' => 'input radio']],
  4412. '<label',
  4413. 'Test',
  4414. '/label',
  4415. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4416. ['label' => ['for' => 'test-0']],
  4417. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4418. 'A',
  4419. '/label',
  4420. ['label' => ['for' => 'test-1']],
  4421. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4422. 'B',
  4423. '/label',
  4424. '/div',
  4425. ];
  4426. $this->assertHtml($expected, $result);
  4427. $result = $this->Form->control('test', [
  4428. 'type' => 'radio',
  4429. 'options' => ['A', 'B'],
  4430. 'label' => false,
  4431. ]);
  4432. $expected = [
  4433. ['div' => ['class' => 'input radio']],
  4434. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4435. ['label' => ['for' => 'test-0']],
  4436. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4437. 'A',
  4438. '/label',
  4439. ['label' => ['for' => 'test-1']],
  4440. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4441. 'B',
  4442. '/label',
  4443. '/div',
  4444. ];
  4445. $this->assertHtml($expected, $result);
  4446. }
  4447. /**
  4448. * testRadioNoLabel method
  4449. *
  4450. * Test that radio() works with label = false.
  4451. *
  4452. * @return void
  4453. */
  4454. public function testRadioNoLabel()
  4455. {
  4456. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4457. $expected = [
  4458. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4459. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4460. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4461. ];
  4462. $this->assertHtml($expected, $result);
  4463. }
  4464. /**
  4465. * testRadioControlInsideLabel method
  4466. *
  4467. * Test generating radio input inside label ala twitter bootstrap.
  4468. *
  4469. * @return void
  4470. */
  4471. public function testRadioControlInsideLabel()
  4472. {
  4473. $this->Form->setTemplates([
  4474. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4475. 'radioWrapper' => '{{label}}',
  4476. ]);
  4477. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4478. // phpcs:disable
  4479. $expected = [
  4480. ['input' => [
  4481. 'type' => 'hidden',
  4482. 'name' => 'Model[field]',
  4483. 'value' => ''
  4484. ]],
  4485. ['label' => ['for' => 'model-field-0']],
  4486. ['input' => [
  4487. 'type' => 'radio',
  4488. 'name' => 'Model[field]',
  4489. 'value' => '0',
  4490. 'id' => 'model-field-0'
  4491. ]],
  4492. 'option A',
  4493. '/label',
  4494. ['label' => ['for' => 'model-field-1']],
  4495. ['input' => [
  4496. 'type' => 'radio',
  4497. 'name' => 'Model[field]',
  4498. 'value' => '1',
  4499. 'id' => 'model-field-1'
  4500. ]],
  4501. 'option B',
  4502. '/label',
  4503. ];
  4504. // phpcs:enable
  4505. $this->assertHtml($expected, $result);
  4506. }
  4507. /**
  4508. * testRadioHiddenControlDisabling method
  4509. *
  4510. * Test disabling the hidden input for radio buttons.
  4511. *
  4512. * @return void
  4513. */
  4514. public function testRadioHiddenControlDisabling()
  4515. {
  4516. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4517. $expected = [
  4518. 'label' => ['for' => 'model-1-field-0'],
  4519. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4520. 'option A',
  4521. '/label',
  4522. ];
  4523. $this->assertHtml($expected, $result);
  4524. }
  4525. /**
  4526. * testRadioOutOfRange method
  4527. *
  4528. * Test radio element set generation.
  4529. *
  4530. * @return void
  4531. */
  4532. public function testRadioOutOfRange()
  4533. {
  4534. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4535. $expected = [
  4536. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4537. 'label' => ['for' => 'model-field-v'],
  4538. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4539. 'value',
  4540. '/label',
  4541. ];
  4542. $this->assertHtml($expected, $result);
  4543. }
  4544. /**
  4545. * testSelect method
  4546. *
  4547. * Test select element generation.
  4548. *
  4549. * @return void
  4550. */
  4551. public function testSelect()
  4552. {
  4553. $result = $this->Form->select('Model.field', []);
  4554. $expected = [
  4555. 'select' => ['name' => 'Model[field]'],
  4556. '/select',
  4557. ];
  4558. $this->assertHtml($expected, $result);
  4559. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4560. $this->Form->create();
  4561. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4562. $expected = [
  4563. 'select' => ['name' => 'Model[field]'],
  4564. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4565. 'good',
  4566. '/option',
  4567. ['option' => ['value' => 'other']],
  4568. 'bad',
  4569. '/option',
  4570. '/select',
  4571. ];
  4572. $this->assertHtml($expected, $result);
  4573. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4574. $this->assertHtml($expected, $result);
  4575. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4576. $this->Form->create();
  4577. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4578. $expected = [
  4579. 'select' => ['name' => 'Model[field]'],
  4580. ['option' => ['value' => 'value']],
  4581. 'good',
  4582. '/option',
  4583. ['option' => ['value' => 'other']],
  4584. 'bad',
  4585. '/option',
  4586. '/select',
  4587. ];
  4588. $this->assertHtml($expected, $result);
  4589. $options = [
  4590. ['value' => 'first', 'text' => 'First'],
  4591. ['value' => 'first', 'text' => 'Another First'],
  4592. ];
  4593. $result = $this->Form->select(
  4594. 'Model.field',
  4595. $options,
  4596. ['escape' => false, 'empty' => false]
  4597. );
  4598. $expected = [
  4599. 'select' => ['name' => 'Model[field]'],
  4600. ['option' => ['value' => 'first']],
  4601. 'First',
  4602. '/option',
  4603. ['option' => ['value' => 'first']],
  4604. 'Another First',
  4605. '/option',
  4606. '/select',
  4607. ];
  4608. $this->assertHtml($expected, $result);
  4609. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4610. $this->Form->create();
  4611. $result = $this->Form->select(
  4612. 'Model.contact_id',
  4613. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4614. ['escape' => false, 'empty' => 'pick something']
  4615. );
  4616. $expected = [
  4617. 'select' => ['name' => 'Model[contact_id]'],
  4618. ['option' => ['value' => '']], 'pick something', '/option',
  4619. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4620. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4621. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4622. '/select',
  4623. ];
  4624. $this->assertHtml($expected, $result);
  4625. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4626. $this->Form->create();
  4627. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4628. $expected = [
  4629. 'select' => ['name' => 'Model[field]'],
  4630. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4631. ['option' => ['value' => '1']], 'Yes', '/option',
  4632. '/select',
  4633. ];
  4634. $this->assertHtml($expected, $result);
  4635. }
  4636. /**
  4637. * testSelectEscapeHtml method
  4638. *
  4639. * Test that select() escapes HTML.
  4640. *
  4641. * @return void
  4642. */
  4643. public function testSelectEscapeHtml()
  4644. {
  4645. $result = $this->Form->select(
  4646. 'Model.field',
  4647. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4648. ['empty' => false]
  4649. );
  4650. $expected = [
  4651. 'select' => ['name' => 'Model[field]'],
  4652. ['option' => ['value' => 'first']],
  4653. 'first &quot;html&quot; &lt;chars&gt;',
  4654. '/option',
  4655. ['option' => ['value' => 'second']],
  4656. 'value',
  4657. '/option',
  4658. '/select',
  4659. ];
  4660. $this->assertHtml($expected, $result);
  4661. $result = $this->Form->select(
  4662. 'Model.field',
  4663. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4664. ['escape' => false, 'empty' => false]
  4665. );
  4666. $expected = [
  4667. 'select' => ['name' => 'Model[field]'],
  4668. ['option' => ['value' => 'first']],
  4669. 'first "html" <chars>',
  4670. '/option',
  4671. ['option' => ['value' => 'second']],
  4672. 'value',
  4673. '/option',
  4674. '/select',
  4675. ];
  4676. $this->assertHtml($expected, $result);
  4677. }
  4678. /**
  4679. * testSelectRequired method
  4680. *
  4681. * Test select() with required and disabled attributes.
  4682. *
  4683. * @return void
  4684. */
  4685. public function testSelectRequired()
  4686. {
  4687. $this->article['required'] = [
  4688. 'user_id' => true,
  4689. ];
  4690. $this->Form->create($this->article);
  4691. $result = $this->Form->select('user_id', ['option A']);
  4692. $expected = [
  4693. 'select' => [
  4694. 'name' => 'user_id',
  4695. 'required' => 'required',
  4696. ],
  4697. ['option' => ['value' => '0']], 'option A', '/option',
  4698. '/select',
  4699. ];
  4700. $this->assertHtml($expected, $result);
  4701. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4702. $expected = [
  4703. 'select' => [
  4704. 'name' => 'user_id',
  4705. 'disabled' => 'disabled',
  4706. ],
  4707. ['option' => ['value' => '0']], 'option A', '/option',
  4708. '/select',
  4709. ];
  4710. $this->assertHtml($expected, $result);
  4711. }
  4712. /**
  4713. * testNestedSelect method
  4714. *
  4715. * Test select element generation with optgroups.
  4716. *
  4717. * @return void
  4718. */
  4719. public function testNestedSelect()
  4720. {
  4721. $result = $this->Form->select(
  4722. 'Model.field',
  4723. [1 => 'One', 2 => 'Two', 'Three' => [
  4724. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4725. ]],
  4726. ['empty' => false]
  4727. );
  4728. $expected = [
  4729. 'select' => ['name' => 'Model[field]'],
  4730. ['option' => ['value' => 1]],
  4731. 'One',
  4732. '/option',
  4733. ['option' => ['value' => 2]],
  4734. 'Two',
  4735. '/option',
  4736. ['optgroup' => ['label' => 'Three']],
  4737. ['option' => ['value' => 3]],
  4738. 'Three',
  4739. '/option',
  4740. ['option' => ['value' => 4]],
  4741. 'Four',
  4742. '/option',
  4743. ['option' => ['value' => 5]],
  4744. 'Five',
  4745. '/option',
  4746. '/optgroup',
  4747. '/select',
  4748. ];
  4749. $this->assertHtml($expected, $result);
  4750. }
  4751. /**
  4752. * testSelectMultiple method
  4753. *
  4754. * Test generation of multiple select elements.
  4755. *
  4756. * @return void
  4757. */
  4758. public function testSelectMultiple()
  4759. {
  4760. $options = ['first', 'second', 'third'];
  4761. $result = $this->Form->select(
  4762. 'Model.multi_field',
  4763. $options,
  4764. ['form' => 'my-form', 'multiple' => true]
  4765. );
  4766. $expected = [
  4767. 'input' => [
  4768. 'type' => 'hidden',
  4769. 'name' => 'Model[multi_field]',
  4770. 'value' => '',
  4771. 'form' => 'my-form',
  4772. ],
  4773. 'select' => [
  4774. 'name' => 'Model[multi_field][]',
  4775. 'multiple' => 'multiple',
  4776. 'form' => 'my-form',
  4777. ],
  4778. ['option' => ['value' => '0']],
  4779. 'first',
  4780. '/option',
  4781. ['option' => ['value' => '1']],
  4782. 'second',
  4783. '/option',
  4784. ['option' => ['value' => '2']],
  4785. 'third',
  4786. '/option',
  4787. '/select',
  4788. ];
  4789. $this->assertHtml($expected, $result);
  4790. $result = $this->Form->select(
  4791. 'Model.multi_field',
  4792. $options,
  4793. ['multiple' => 'multiple', 'form' => 'my-form']
  4794. );
  4795. $this->assertHtml($expected, $result);
  4796. $result = $this->Form->select(
  4797. 'Model.multi_field',
  4798. $options,
  4799. ['form' => 'my-form', 'multiple' => false]
  4800. );
  4801. $this->assertNotContains('multiple', $result);
  4802. }
  4803. /**
  4804. * testCheckboxZeroValue method
  4805. *
  4806. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4807. *
  4808. * @return void
  4809. */
  4810. public function testCheckboxZeroValue()
  4811. {
  4812. $result = $this->Form->control('User.get_spam', [
  4813. 'type' => 'checkbox',
  4814. 'value' => '0',
  4815. 'hiddenField' => '1',
  4816. ]);
  4817. $expected = [
  4818. 'div' => ['class' => 'input checkbox'],
  4819. 'label' => ['for' => 'user-get-spam'],
  4820. ['input' => [
  4821. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4822. 'value' => '1',
  4823. ]],
  4824. ['input' => [
  4825. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4826. 'value' => '0', 'id' => 'user-get-spam',
  4827. ]],
  4828. 'Get Spam',
  4829. '/label',
  4830. '/div',
  4831. ];
  4832. $this->assertHtml($expected, $result);
  4833. }
  4834. /**
  4835. * testHabtmSelectBox method
  4836. *
  4837. * Test generation of habtm select boxes.
  4838. *
  4839. * @return void
  4840. */
  4841. public function testHabtmSelectBox()
  4842. {
  4843. $this->loadFixtures('Articles');
  4844. $options = [
  4845. 1 => 'blue',
  4846. 2 => 'red',
  4847. 3 => 'green',
  4848. ];
  4849. $tags = [
  4850. new Entity(['id' => 1, 'name' => 'blue']),
  4851. new Entity(['id' => 3, 'name' => 'green']),
  4852. ];
  4853. $article = new Article(['tags' => $tags]);
  4854. $this->Form->create($article);
  4855. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4856. $expected = [
  4857. 'div' => ['class' => 'input select'],
  4858. 'label' => ['for' => 'tags-ids'],
  4859. 'Tags',
  4860. '/label',
  4861. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4862. 'select' => [
  4863. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4864. 'multiple' => 'multiple',
  4865. ],
  4866. ['option' => ['value' => '1', 'selected' => 'selected']],
  4867. 'blue',
  4868. '/option',
  4869. ['option' => ['value' => '2']],
  4870. 'red',
  4871. '/option',
  4872. ['option' => ['value' => '3', 'selected' => 'selected']],
  4873. 'green',
  4874. '/option',
  4875. '/select',
  4876. '/div',
  4877. ];
  4878. $this->assertHtml($expected, $result);
  4879. // make sure only 50 is selected, and not 50f5c0cf
  4880. $options = [
  4881. '1' => 'blue',
  4882. '50f5c0cf' => 'red',
  4883. '50' => 'green',
  4884. ];
  4885. $tags = [
  4886. new Entity(['id' => 1, 'name' => 'blue']),
  4887. new Entity(['id' => 50, 'name' => 'green']),
  4888. ];
  4889. $article = new Article(['tags' => $tags]);
  4890. $this->Form->create($article);
  4891. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4892. $expected = [
  4893. 'div' => ['class' => 'input select'],
  4894. 'label' => ['for' => 'tags-ids'],
  4895. 'Tags',
  4896. '/label',
  4897. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4898. 'select' => [
  4899. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4900. 'multiple' => 'multiple',
  4901. ],
  4902. ['option' => ['value' => '1', 'selected' => 'selected']],
  4903. 'blue',
  4904. '/option',
  4905. ['option' => ['value' => '50f5c0cf']],
  4906. 'red',
  4907. '/option',
  4908. ['option' => ['value' => '50', 'selected' => 'selected']],
  4909. 'green',
  4910. '/option',
  4911. '/select',
  4912. '/div',
  4913. ];
  4914. $this->assertHtml($expected, $result);
  4915. $spacecraft = [
  4916. 1 => 'Orion',
  4917. 2 => 'Helios',
  4918. ];
  4919. $this->View->set('spacecraft', $spacecraft);
  4920. $this->Form->create();
  4921. $result = $this->Form->control('spacecraft._ids');
  4922. $expected = [
  4923. 'div' => ['class' => 'input select'],
  4924. 'label' => ['for' => 'spacecraft-ids'],
  4925. 'Spacecraft',
  4926. '/label',
  4927. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4928. 'select' => [
  4929. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4930. 'multiple' => 'multiple',
  4931. ],
  4932. ['option' => ['value' => '1']],
  4933. 'Orion',
  4934. '/option',
  4935. ['option' => ['value' => '2']],
  4936. 'Helios',
  4937. '/option',
  4938. '/select',
  4939. '/div',
  4940. ];
  4941. $this->assertHtml($expected, $result);
  4942. }
  4943. /**
  4944. * testErrorsForBelongsToManySelect method
  4945. *
  4946. * Tests that errors for belongsToMany select fields are being
  4947. * picked up properly.
  4948. *
  4949. * @return void
  4950. */
  4951. public function testErrorsForBelongsToManySelect()
  4952. {
  4953. $spacecraft = [
  4954. 1 => 'Orion',
  4955. 2 => 'Helios',
  4956. ];
  4957. $this->View->set('spacecraft', $spacecraft);
  4958. $article = new Article();
  4959. $article->setError('spacecraft', ['Invalid']);
  4960. $this->Form->create($article);
  4961. $result = $this->Form->control('spacecraft._ids');
  4962. $expected = [
  4963. ['div' => ['class' => 'input select error']],
  4964. 'label' => ['for' => 'spacecraft-ids'],
  4965. 'Spacecraft',
  4966. '/label',
  4967. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4968. 'select' => [
  4969. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4970. 'multiple' => 'multiple',
  4971. ],
  4972. ['option' => ['value' => '1']],
  4973. 'Orion',
  4974. '/option',
  4975. ['option' => ['value' => '2']],
  4976. 'Helios',
  4977. '/option',
  4978. '/select',
  4979. ['div' => ['class' => 'error-message']],
  4980. 'Invalid',
  4981. '/div',
  4982. '/div',
  4983. ];
  4984. $this->assertHtml($expected, $result);
  4985. }
  4986. /**
  4987. * testSelectMultipleCheckboxes method
  4988. *
  4989. * Test generation of multi select elements in checkbox format.
  4990. *
  4991. * @return void
  4992. */
  4993. public function testSelectMultipleCheckboxes()
  4994. {
  4995. $result = $this->Form->select(
  4996. 'Model.multi_field',
  4997. ['first', 'second', 'third'],
  4998. ['multiple' => 'checkbox']
  4999. );
  5000. $expected = [
  5001. 'input' => [
  5002. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5003. ],
  5004. ['div' => ['class' => 'checkbox']],
  5005. ['label' => ['for' => 'model-multi-field-0']],
  5006. ['input' => [
  5007. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5008. 'value' => '0', 'id' => 'model-multi-field-0',
  5009. ]],
  5010. 'first',
  5011. '/label',
  5012. '/div',
  5013. ['div' => ['class' => 'checkbox']],
  5014. ['label' => ['for' => 'model-multi-field-1']],
  5015. ['input' => [
  5016. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5017. 'value' => '1', 'id' => 'model-multi-field-1',
  5018. ]],
  5019. 'second',
  5020. '/label',
  5021. '/div',
  5022. ['div' => ['class' => 'checkbox']],
  5023. ['label' => ['for' => 'model-multi-field-2']],
  5024. ['input' => [
  5025. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5026. 'value' => '2', 'id' => 'model-multi-field-2',
  5027. ]],
  5028. 'third',
  5029. '/label',
  5030. '/div',
  5031. ];
  5032. $this->assertHtml($expected, $result);
  5033. $result = $this->Form->select(
  5034. 'Model.multi_field',
  5035. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5036. ['multiple' => 'checkbox']
  5037. );
  5038. $expected = [
  5039. 'input' => [
  5040. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5041. ],
  5042. ['div' => ['class' => 'checkbox']],
  5043. ['label' => ['for' => 'model-multi-field-a+']],
  5044. ['input' => [
  5045. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5046. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5047. ]],
  5048. 'first',
  5049. '/label',
  5050. '/div',
  5051. ['div' => ['class' => 'checkbox']],
  5052. ['label' => ['for' => 'model-multi-field-a++']],
  5053. ['input' => [
  5054. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5055. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5056. ]],
  5057. 'second',
  5058. '/label',
  5059. '/div',
  5060. ['div' => ['class' => 'checkbox']],
  5061. ['label' => ['for' => 'model-multi-field-a+++']],
  5062. ['input' => [
  5063. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5064. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5065. ]],
  5066. 'third',
  5067. '/label',
  5068. '/div',
  5069. ];
  5070. $this->assertHtml($expected, $result);
  5071. $result = $this->Form->select(
  5072. 'Model.multi_field',
  5073. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5074. ['multiple' => 'checkbox']
  5075. );
  5076. $expected = [
  5077. 'input' => [
  5078. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5079. ],
  5080. ['div' => ['class' => 'checkbox']],
  5081. ['label' => ['for' => 'model-multi-field-a-b']],
  5082. ['input' => [
  5083. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5084. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5085. ]],
  5086. 'first',
  5087. '/label',
  5088. '/div',
  5089. ['div' => ['class' => 'checkbox']],
  5090. ['label' => ['for' => 'model-multi-field-a-b1']],
  5091. ['input' => [
  5092. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5093. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5094. ]],
  5095. 'second',
  5096. '/label',
  5097. '/div',
  5098. ['div' => ['class' => 'checkbox']],
  5099. ['label' => ['for' => 'model-multi-field-a-b2']],
  5100. ['input' => [
  5101. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5102. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5103. ]],
  5104. 'third',
  5105. '/label',
  5106. '/div',
  5107. ];
  5108. $this->assertHtml($expected, $result);
  5109. }
  5110. /**
  5111. * testSelectMultipleCheckboxRequestData method
  5112. *
  5113. * Ensure that multiCheckbox reads from the request data.
  5114. *
  5115. * @return void
  5116. */
  5117. public function testSelectMultipleCheckboxRequestData()
  5118. {
  5119. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5120. $result = $this->Form->select(
  5121. 'Model.tags',
  5122. ['1' => 'first', 'Array' => 'Array'],
  5123. ['multiple' => 'checkbox']
  5124. );
  5125. $expected = [
  5126. 'input' => [
  5127. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5128. ],
  5129. ['div' => ['class' => 'checkbox']],
  5130. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5131. ['input' => [
  5132. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5133. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5134. ]],
  5135. 'first',
  5136. '/label',
  5137. '/div',
  5138. ['div' => ['class' => 'checkbox']],
  5139. ['label' => ['for' => 'model-tags-array']],
  5140. ['input' => [
  5141. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5142. 'value' => 'Array', 'id' => 'model-tags-array',
  5143. ]],
  5144. 'Array',
  5145. '/label',
  5146. '/div',
  5147. ];
  5148. $this->assertHtml($expected, $result);
  5149. }
  5150. /**
  5151. * testSelectMultipleCheckboxSecurity method
  5152. *
  5153. * Checks the security hash array generated for multiple-input checkbox elements.
  5154. *
  5155. * @return void
  5156. */
  5157. public function testSelectMultipleCheckboxSecurity()
  5158. {
  5159. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5160. $this->assertEquals([], $this->Form->fields);
  5161. $this->Form->select(
  5162. 'Model.multi_field',
  5163. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5164. ['multiple' => 'checkbox']
  5165. );
  5166. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5167. $result = $this->Form->secure($this->Form->fields);
  5168. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5169. $this->assertContains('"' . $key . '"', $result);
  5170. }
  5171. /**
  5172. * testSelectMultipleSecureWithNoOptions method
  5173. *
  5174. * Multiple select elements should always be secured as they always participate
  5175. * in the POST data.
  5176. *
  5177. * @return void
  5178. */
  5179. public function testSelectMultipleSecureWithNoOptions()
  5180. {
  5181. $this->assertEquals([], $this->Form->fields);
  5182. $this->Form->select(
  5183. 'Model.select',
  5184. [],
  5185. ['multiple' => true]
  5186. );
  5187. $this->assertEquals(['Model.select'], $this->Form->fields);
  5188. }
  5189. /**
  5190. * testSelectNoSecureWithNoOptions method
  5191. *
  5192. * When a select box has no options it should not be added to the fields list
  5193. * as it always fail post validation.
  5194. *
  5195. * @return void
  5196. */
  5197. public function testSelectNoSecureWithNoOptions()
  5198. {
  5199. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5200. $this->assertEquals([], $this->Form->fields);
  5201. $this->Form->select(
  5202. 'Model.select',
  5203. []
  5204. );
  5205. $this->assertEquals([], $this->Form->fields);
  5206. $this->Form->select(
  5207. 'Model.user_id',
  5208. [],
  5209. ['empty' => true]
  5210. );
  5211. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5212. }
  5213. /**
  5214. * testControlMultipleCheckboxes method
  5215. *
  5216. * Test control() resulting in multi select elements being generated.
  5217. *
  5218. * @return void
  5219. */
  5220. public function testControlMultipleCheckboxes()
  5221. {
  5222. $result = $this->Form->control('Model.multi_field', [
  5223. 'options' => ['first', 'second', 'third'],
  5224. 'multiple' => 'checkbox',
  5225. ]);
  5226. $expected = [
  5227. ['div' => ['class' => 'input select']],
  5228. ['label' => ['for' => 'model-multi-field']],
  5229. 'Multi Field',
  5230. '/label',
  5231. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5232. ['div' => ['class' => 'checkbox']],
  5233. ['label' => ['for' => 'model-multi-field-0']],
  5234. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5235. 'first',
  5236. '/label',
  5237. '/div',
  5238. ['div' => ['class' => 'checkbox']],
  5239. ['label' => ['for' => 'model-multi-field-1']],
  5240. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5241. 'second',
  5242. '/label',
  5243. '/div',
  5244. ['div' => ['class' => 'checkbox']],
  5245. ['label' => ['for' => 'model-multi-field-2']],
  5246. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5247. 'third',
  5248. '/label',
  5249. '/div',
  5250. '/div',
  5251. ];
  5252. $this->assertHtml($expected, $result);
  5253. $result = $this->Form->control('Model.multi_field', [
  5254. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5255. 'multiple' => 'checkbox',
  5256. ]);
  5257. $expected = [
  5258. ['div' => ['class' => 'input select']],
  5259. ['label' => ['for' => 'model-multi-field']],
  5260. 'Multi Field',
  5261. '/label',
  5262. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5263. ['div' => ['class' => 'checkbox']],
  5264. ['label' => ['for' => 'model-multi-field-a']],
  5265. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5266. 'first',
  5267. '/label',
  5268. '/div',
  5269. ['div' => ['class' => 'checkbox']],
  5270. ['label' => ['for' => 'model-multi-field-b']],
  5271. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5272. 'second',
  5273. '/label',
  5274. '/div',
  5275. ['div' => ['class' => 'checkbox']],
  5276. ['label' => ['for' => 'model-multi-field-c']],
  5277. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5278. 'third',
  5279. '/label',
  5280. '/div',
  5281. '/div',
  5282. ];
  5283. $this->assertHtml($expected, $result);
  5284. }
  5285. /**
  5286. * testSelectHiddenFieldOmission method
  5287. *
  5288. * Test that select() with 'hiddenField' => false omits the hidden field.
  5289. *
  5290. * @return void
  5291. */
  5292. public function testSelectHiddenFieldOmission()
  5293. {
  5294. $result = $this->Form->select(
  5295. 'Model.multi_field',
  5296. ['first', 'second'],
  5297. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5298. );
  5299. $this->assertNotContains('type="hidden"', $result);
  5300. }
  5301. /**
  5302. * testSelectCheckboxMultipleOverrideName method
  5303. *
  5304. * Test that select() with multiple = checkbox works with overriding name attribute.
  5305. *
  5306. * @return void
  5307. */
  5308. public function testSelectCheckboxMultipleOverrideName()
  5309. {
  5310. $result = $this->Form->select('category', ['1', '2'], [
  5311. 'multiple' => 'checkbox',
  5312. 'name' => 'fish',
  5313. ]);
  5314. $expected = [
  5315. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5316. ['div' => ['class' => 'checkbox']],
  5317. ['label' => ['for' => 'fish-0']],
  5318. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5319. '1',
  5320. '/label',
  5321. '/div',
  5322. ['div' => ['class' => 'checkbox']],
  5323. ['label' => ['for' => 'fish-1']],
  5324. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5325. '2',
  5326. '/label',
  5327. '/div',
  5328. ];
  5329. $this->assertHtml($expected, $result);
  5330. $result = $this->Form->multiCheckbox(
  5331. 'category',
  5332. new Collection(['1', '2']),
  5333. ['name' => 'fish']
  5334. );
  5335. $this->assertHtml($expected, $result);
  5336. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5337. 'name' => 'fish',
  5338. ]);
  5339. $this->assertHtml($expected, $result);
  5340. }
  5341. /**
  5342. * testControlMultiCheckbox method
  5343. *
  5344. * Test that control() works with multicheckbox.
  5345. *
  5346. * @return void
  5347. */
  5348. public function testControlMultiCheckbox()
  5349. {
  5350. $result = $this->Form->control('category', [
  5351. 'type' => 'multicheckbox',
  5352. 'options' => ['1', '2'],
  5353. ]);
  5354. $expected = [
  5355. ['div' => ['class' => 'input multicheckbox']],
  5356. '<label',
  5357. 'Category',
  5358. '/label',
  5359. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5360. ['div' => ['class' => 'checkbox']],
  5361. ['label' => ['for' => 'category-0']],
  5362. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5363. '1',
  5364. '/label',
  5365. '/div',
  5366. ['div' => ['class' => 'checkbox']],
  5367. ['label' => ['for' => 'category-1']],
  5368. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5369. '2',
  5370. '/label',
  5371. '/div',
  5372. '/div',
  5373. ];
  5374. $this->assertHtml($expected, $result);
  5375. }
  5376. /**
  5377. * testCheckbox method
  5378. *
  5379. * Test generation of checkboxes.
  5380. *
  5381. * @return void
  5382. */
  5383. public function testCheckbox()
  5384. {
  5385. $result = $this->Form->checkbox('Model.field');
  5386. $expected = [
  5387. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5388. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5389. ];
  5390. $this->assertHtml($expected, $result);
  5391. $result = $this->Form->checkbox('Model.field', [
  5392. 'id' => 'theID',
  5393. 'value' => 'myvalue',
  5394. 'form' => 'my-form',
  5395. ]);
  5396. $expected = [
  5397. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5398. ['input' => [
  5399. 'type' => 'checkbox', 'name' => 'Model[field]',
  5400. 'value' => 'myvalue', 'id' => 'theID',
  5401. 'form' => 'my-form',
  5402. ]],
  5403. ];
  5404. $this->assertHtml($expected, $result);
  5405. }
  5406. /**
  5407. * testCheckboxDefaultValue method
  5408. *
  5409. * Test default value setting on checkbox() method.
  5410. *
  5411. * @return void
  5412. */
  5413. public function testCheckboxDefaultValue()
  5414. {
  5415. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5416. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5417. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5418. $this->assertHtml($expected, $result);
  5419. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5420. $this->Form->create();
  5421. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5422. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5423. $this->assertHtml($expected, $result);
  5424. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5425. $this->Form->create();
  5426. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5427. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5428. $this->assertHtml($expected, $result);
  5429. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5430. $this->Form->create();
  5431. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5432. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5433. $this->assertHtml($expected, $result);
  5434. $Articles = $this->getTableLocator()->get('Articles');
  5435. $Articles->getSchema()->addColumn(
  5436. 'published',
  5437. ['type' => 'boolean', 'null' => false, 'default' => true]
  5438. );
  5439. $this->Form->create($Articles->createEntity());
  5440. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5441. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5442. $this->assertHtml($expected, $result);
  5443. }
  5444. /**
  5445. * testCheckboxCheckedAndError method
  5446. *
  5447. * Test checkbox being checked or having errors.
  5448. *
  5449. * @return void
  5450. */
  5451. public function testCheckboxCheckedAndError()
  5452. {
  5453. $this->article['errors'] = [
  5454. 'published' => true,
  5455. ];
  5456. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5457. $this->Form->create($this->article);
  5458. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5459. $expected = [
  5460. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5461. ['input' => [
  5462. 'type' => 'checkbox',
  5463. 'name' => 'published',
  5464. 'value' => 'myvalue',
  5465. 'id' => 'theID',
  5466. 'checked' => 'checked',
  5467. 'class' => 'form-error',
  5468. ]],
  5469. ];
  5470. $this->assertHtml($expected, $result);
  5471. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5472. $this->Form->create($this->article);
  5473. $result = $this->Form->checkbox('published');
  5474. $expected = [
  5475. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5476. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5477. ];
  5478. $this->assertHtml($expected, $result);
  5479. }
  5480. /**
  5481. * testCheckboxCustomNameAttribute method
  5482. *
  5483. * Test checkbox() with a custom name attribute.
  5484. *
  5485. * @return void
  5486. */
  5487. public function testCheckboxCustomNameAttribute()
  5488. {
  5489. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5490. $expected = [
  5491. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5492. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5493. ];
  5494. $this->assertHtml($expected, $result);
  5495. }
  5496. /**
  5497. * testCheckboxHiddenField method
  5498. *
  5499. * Test that the hidden input for checkboxes can be omitted or set to a
  5500. * specific value.
  5501. *
  5502. * @return void
  5503. */
  5504. public function testCheckboxHiddenField()
  5505. {
  5506. $result = $this->Form->checkbox('UserForm.something', [
  5507. 'hiddenField' => false,
  5508. ]);
  5509. $expected = [
  5510. 'input' => [
  5511. 'type' => 'checkbox',
  5512. 'name' => 'UserForm[something]',
  5513. 'value' => '1',
  5514. ],
  5515. ];
  5516. $this->assertHtml($expected, $result);
  5517. $result = $this->Form->checkbox('UserForm.something', [
  5518. 'value' => 'Y',
  5519. 'hiddenField' => 'N',
  5520. ]);
  5521. $expected = [
  5522. ['input' => [
  5523. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5524. 'value' => 'N',
  5525. ]],
  5526. ['input' => [
  5527. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5528. 'value' => 'Y',
  5529. ]],
  5530. ];
  5531. $this->assertHtml($expected, $result);
  5532. }
  5533. /**
  5534. * testTime method
  5535. *
  5536. * Test the time type.
  5537. *
  5538. * @return void
  5539. */
  5540. public function testTime()
  5541. {
  5542. $result = $this->Form->time('start_time', [
  5543. 'value' => '2014-03-08 16:30:00',
  5544. ]);
  5545. $expected = [
  5546. 'input' => [
  5547. 'type' => 'time',
  5548. 'name' => 'start_time',
  5549. 'value' => '16:30:00',
  5550. ],
  5551. ];
  5552. $this->assertHtml($expected, $result);
  5553. }
  5554. /**
  5555. * testDate method
  5556. *
  5557. * Test the date type.
  5558. *
  5559. * @return void
  5560. */
  5561. public function testDate()
  5562. {
  5563. $result = $this->Form->date('start_day', [
  5564. 'value' => '2014-03-08',
  5565. ]);
  5566. $expected = [
  5567. 'input' => [
  5568. 'type' => 'date',
  5569. 'name' => 'start_day',
  5570. 'value' => '2014-03-08',
  5571. ],
  5572. ];
  5573. $this->assertHtml($expected, $result);
  5574. $result = $this->Form->date('start_day', [
  5575. 'value' => new Date('2014-03-08'),
  5576. ]);
  5577. $this->assertHtml($expected, $result);
  5578. }
  5579. /**
  5580. * testDateTime method
  5581. *
  5582. * @return void
  5583. */
  5584. public function testDateTime()
  5585. {
  5586. $result = $this->Form->dateTime('date', ['default' => true]);
  5587. $expected = [
  5588. 'input' => [
  5589. 'type' => 'datetime-local',
  5590. 'name' => 'date',
  5591. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}(:\d{2})?/',
  5592. ],
  5593. ];
  5594. $this->assertHtml($expected, $result);
  5595. }
  5596. /**
  5597. * testDateTimeSecured method
  5598. *
  5599. * Test that datetime fields are added to protected fields list.
  5600. *
  5601. * @return void
  5602. */
  5603. public function testDateTimeSecured()
  5604. {
  5605. $this->View->setRequest(
  5606. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5607. );
  5608. $this->Form->dateTime('date');
  5609. $expected = ['date'];
  5610. $this->assertEquals($expected, $this->Form->fields);
  5611. $this->Form->fields = [];
  5612. $this->Form->date('published');
  5613. $expected = ['published'];
  5614. $this->assertEquals($expected, $this->Form->fields);
  5615. }
  5616. /**
  5617. * testDateTimeSecuredDisabled method
  5618. *
  5619. * Test that datetime fields are added to protected fields list.
  5620. *
  5621. * @return void
  5622. */
  5623. public function testDateTimeSecuredDisabled()
  5624. {
  5625. $this->View->setRequest(
  5626. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5627. );
  5628. $this->Form->dateTime('date', ['secure' => false]);
  5629. $expected = [];
  5630. $this->assertEquals($expected, $this->Form->fields);
  5631. $this->Form->fields = [];
  5632. $this->Form->date('published', ['secure' => false]);
  5633. $expected = [];
  5634. $this->assertEquals($expected, $this->Form->fields);
  5635. }
  5636. /**
  5637. * testDatetimeWithDefault method
  5638. *
  5639. * Test that datetime() and default values work.
  5640. *
  5641. * @return void
  5642. */
  5643. public function testDatetimeWithDefault()
  5644. {
  5645. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5646. $expected = [
  5647. 'input' => [
  5648. 'type' => 'datetime-local',
  5649. 'name' => 'updated',
  5650. 'value' => '2009-06-01T11:15:30',
  5651. ],
  5652. ];
  5653. $this->assertHtml($expected, $result);
  5654. $result = $this->Form->dateTime('updated', [
  5655. 'default' => '2009-06-01 11:15:30',
  5656. ]);
  5657. $expected = [
  5658. 'input' => [
  5659. 'type' => 'datetime-local',
  5660. 'name' => 'updated',
  5661. 'value' => '2009-06-01T11:15:30',
  5662. ],
  5663. ];
  5664. $this->assertHtml($expected, $result);
  5665. }
  5666. /**
  5667. * testMonth method
  5668. *
  5669. * Test generation of a month input.
  5670. *
  5671. * @return void
  5672. */
  5673. public function testMonth()
  5674. {
  5675. $result = $this->Form->month('field', ['value' => '']);
  5676. $expected = [
  5677. 'input' => [
  5678. 'type' => 'month',
  5679. 'name' => 'field',
  5680. 'value' => '',
  5681. ],
  5682. ];
  5683. $this->assertHtml($expected, $result);
  5684. $this->View->setRequest(
  5685. $this->View->getRequest()->withData('release', '2050-02-10')
  5686. );
  5687. $this->Form->create();
  5688. $result = $this->Form->month('release');
  5689. $expected = [
  5690. 'input' => [
  5691. 'type' => 'month',
  5692. 'name' => 'release',
  5693. 'value' => '2050-02',
  5694. ],
  5695. ];
  5696. $this->assertHtml($expected, $result);
  5697. $this->View->setRequest(
  5698. $this->View->getRequest()->withData('release', '2050-03')
  5699. );
  5700. $this->Form->create();
  5701. $result = $this->Form->month('release');
  5702. $expected = [
  5703. 'input' => [
  5704. 'type' => 'month',
  5705. 'name' => 'release',
  5706. 'value' => '2050-03',
  5707. ],
  5708. ];
  5709. $this->assertHtml($expected, $result);
  5710. }
  5711. /**
  5712. * testYear method
  5713. *
  5714. * Test generation of a year input.
  5715. *
  5716. * @return void
  5717. */
  5718. public function testYear()
  5719. {
  5720. $this->View->setRequest(
  5721. $this->View->getRequest()->withData('published', '2006')
  5722. );
  5723. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5724. $expected = [
  5725. ['select' => ['name' => 'field']],
  5726. ['option' => ['selected' => 'selected', 'value' => '']],
  5727. '/option',
  5728. ['option' => ['value' => '2007']],
  5729. '2007',
  5730. '/option',
  5731. ['option' => ['value' => '2006']],
  5732. '2006',
  5733. '/option',
  5734. '/select',
  5735. ];
  5736. $this->assertHtml($expected, $result);
  5737. $result = $this->Form->year('field', [
  5738. 'value' => '',
  5739. 'min' => 2006,
  5740. 'max' => 2007,
  5741. 'order' => 'asc',
  5742. ]);
  5743. $expected = [
  5744. ['select' => ['name' => 'field']],
  5745. ['option' => ['selected' => 'selected', 'value' => '']],
  5746. '/option',
  5747. ['option' => ['value' => '2006']],
  5748. '2006',
  5749. '/option',
  5750. ['option' => ['value' => '2007']],
  5751. '2007',
  5752. '/option',
  5753. '/select',
  5754. ];
  5755. $this->assertHtml($expected, $result);
  5756. $result = $this->Form->year('published', [
  5757. 'empty' => false,
  5758. 'min' => 2006,
  5759. 'max' => 2007,
  5760. ]);
  5761. $expected = [
  5762. ['select' => ['name' => 'published']],
  5763. ['option' => ['value' => '2007']],
  5764. '2007',
  5765. '/option',
  5766. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5767. '2006',
  5768. '/option',
  5769. '/select',
  5770. ];
  5771. $this->assertHtml($expected, $result);
  5772. $result = $this->Form->year('published', [
  5773. 'empty' => 'Published on',
  5774. ]);
  5775. $this->assertContains('Published on', $result);
  5776. }
  5777. /**
  5778. * testControlYearPreEpoch method
  5779. *
  5780. * Test minYear being prior to the unix epoch.
  5781. *
  5782. * @return void
  5783. */
  5784. public function testControlYearPreEpoch()
  5785. {
  5786. $start = date('Y') - 80;
  5787. $end = date('Y') - 18;
  5788. $result = $this->Form->control('birth_year', [
  5789. 'type' => 'year',
  5790. 'label' => 'Birth Year',
  5791. 'min' => $start,
  5792. 'max' => $end,
  5793. ]);
  5794. $this->assertContains('value="' . $start . '">' . $start, $result);
  5795. $this->assertContains('value="' . $end . '">' . $end, $result);
  5796. $this->assertNotContains('value="00">00', $result);
  5797. }
  5798. /**
  5799. * test control() datetime & required attributes
  5800. *
  5801. * @return void
  5802. */
  5803. public function testControlDatetimeRequired()
  5804. {
  5805. $result = $this->Form->control('birthday', [
  5806. 'type' => 'date',
  5807. 'required' => true,
  5808. ]);
  5809. $this->assertContains(
  5810. '<input type="date" name="birthday" required="required"',
  5811. $result
  5812. );
  5813. }
  5814. /**
  5815. * testYearAutoExpandRange method
  5816. *
  5817. * @return void
  5818. */
  5819. public function testYearAutoExpandRange()
  5820. {
  5821. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5822. $result = $this->Form->year('birthday');
  5823. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5824. $result = $matches[1];
  5825. $expected = range(date('Y') + 5, 1930);
  5826. $this->assertEquals($expected, $result);
  5827. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5828. $this->Form->create();
  5829. $result = $this->Form->year('release');
  5830. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5831. $result = $matches[1];
  5832. $expected = range(2050, date('Y') - 5);
  5833. $this->assertEquals($expected, $result);
  5834. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5835. $this->Form->create();
  5836. $result = $this->Form->year('release', [
  5837. 'min' => 1890,
  5838. 'max' => 1900,
  5839. ]);
  5840. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5841. $result = $matches[1];
  5842. $expected = range(1900, 1881);
  5843. $this->assertEquals($expected, $result);
  5844. }
  5845. /**
  5846. * testControlLabelFalse method
  5847. *
  5848. * Test the label option being set to false.
  5849. *
  5850. * @return void
  5851. */
  5852. public function testControlLabelFalse()
  5853. {
  5854. $this->Form->create($this->article);
  5855. $result = $this->Form->control('title', ['label' => false]);
  5856. $expected = [
  5857. 'div' => ['class' => 'input text required'],
  5858. 'input' => [
  5859. 'type' => 'text',
  5860. 'required' => 'required',
  5861. 'id' => 'title',
  5862. 'name' => 'title',
  5863. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  5864. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  5865. ],
  5866. '/div',
  5867. ];
  5868. $this->assertHtml($expected, $result);
  5869. }
  5870. /**
  5871. * testTextArea method
  5872. *
  5873. * Test generation of a textarea input.
  5874. *
  5875. * @return void
  5876. */
  5877. public function testTextArea()
  5878. {
  5879. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5880. $result = $this->Form->textarea('field');
  5881. $expected = [
  5882. 'textarea' => ['name' => 'field', 'rows' => 5],
  5883. 'some test data',
  5884. '/textarea',
  5885. ];
  5886. $this->assertHtml($expected, $result);
  5887. $result = $this->Form->textarea('user.bio');
  5888. $expected = [
  5889. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5890. '/textarea',
  5891. ];
  5892. $this->assertHtml($expected, $result);
  5893. $this->View->setRequest($this->View->getRequest()
  5894. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5895. $this->Form->create();
  5896. $result = $this->Form->textarea('field');
  5897. $expected = [
  5898. 'textarea' => ['name' => 'field', 'rows' => 5],
  5899. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5900. '/textarea',
  5901. ];
  5902. $this->assertHtml($expected, $result);
  5903. $this->View->setRequest($this->View->getRequest()->withData(
  5904. 'Model.field',
  5905. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5906. ));
  5907. $this->Form->create();
  5908. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5909. $expected = [
  5910. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5911. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5912. '/textarea',
  5913. ];
  5914. $this->assertHtml($expected, $result);
  5915. $result = $this->Form->textarea('0.OtherModel.field');
  5916. $expected = [
  5917. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5918. '/textarea',
  5919. ];
  5920. $this->assertHtml($expected, $result);
  5921. }
  5922. /**
  5923. * testTextAreaWithStupidCharacters method
  5924. *
  5925. * Test text area with non-ascii characters.
  5926. *
  5927. * @return void
  5928. */
  5929. public function testTextAreaWithStupidCharacters()
  5930. {
  5931. $result = $this->Form->textarea('Post.content', [
  5932. 'value' => 'GREAT®',
  5933. 'rows' => '15',
  5934. 'cols' => '75',
  5935. ]);
  5936. $expected = [
  5937. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5938. 'GREAT®',
  5939. '/textarea',
  5940. ];
  5941. $this->assertHtml($expected, $result);
  5942. }
  5943. /**
  5944. * testTextAreaMaxLength method
  5945. *
  5946. * Test textareas maxlength read from schema.
  5947. *
  5948. * @return void
  5949. */
  5950. public function testTextAreaMaxLength()
  5951. {
  5952. $this->Form->create([
  5953. 'schema' => [
  5954. 'stuff' => ['type' => 'string', 'length' => 10],
  5955. ],
  5956. ]);
  5957. $result = $this->Form->control('other', ['type' => 'textarea']);
  5958. $expected = [
  5959. 'div' => ['class' => 'input textarea'],
  5960. 'label' => ['for' => 'other'],
  5961. 'Other',
  5962. '/label',
  5963. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5964. '/textarea',
  5965. '/div',
  5966. ];
  5967. $this->assertHtml($expected, $result);
  5968. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5969. $expected = [
  5970. 'div' => ['class' => 'input textarea'],
  5971. 'label' => ['for' => 'stuff'],
  5972. 'Stuff',
  5973. '/label',
  5974. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5975. '/textarea',
  5976. '/div',
  5977. ];
  5978. $this->assertHtml($expected, $result);
  5979. }
  5980. /**
  5981. * testHiddenField method
  5982. *
  5983. * Test generation of a hidden input.
  5984. *
  5985. * @return void
  5986. */
  5987. public function testHiddenField()
  5988. {
  5989. $this->article['errors'] = [
  5990. 'field' => true,
  5991. ];
  5992. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5993. $this->Form->create($this->article);
  5994. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5995. $expected = [
  5996. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5997. $this->assertHtml($expected, $result);
  5998. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5999. $expected = [
  6000. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6001. ];
  6002. $this->assertHtml($expected, $result);
  6003. }
  6004. /**
  6005. * testFileUploadField method
  6006. *
  6007. * Test generation of a file upload input.
  6008. *
  6009. * @return void
  6010. */
  6011. public function testFileUploadField()
  6012. {
  6013. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6014. $result = $this->Form->file('Model.upload');
  6015. $this->assertHtml($expected, $result);
  6016. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6017. 'name' => '', 'type' => '', 'tmp_name' => '',
  6018. 'error' => 4, 'size' => 0,
  6019. ]));
  6020. $result = $this->Form->file('Model.upload');
  6021. $this->assertHtml($expected, $result);
  6022. $this->View->setRequest(
  6023. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6024. );
  6025. $result = $this->Form->file('Model.upload');
  6026. $this->assertHtml($expected, $result);
  6027. }
  6028. /**
  6029. * testFileUploadOnOtherModel method
  6030. *
  6031. * Test File upload input on a model not used in create().
  6032. *
  6033. * @return void
  6034. */
  6035. public function testFileUploadOnOtherModel()
  6036. {
  6037. $this->Form->create($this->article, ['type' => 'file']);
  6038. $result = $this->Form->file('ValidateProfile.city');
  6039. $expected = [
  6040. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6041. ];
  6042. $this->assertHtml($expected, $result);
  6043. }
  6044. /**
  6045. * testButton method
  6046. *
  6047. * Test generation of a form button.
  6048. *
  6049. * @return void
  6050. */
  6051. public function testButton()
  6052. {
  6053. $result = $this->Form->button('Hi');
  6054. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6055. $this->assertHtml($expected, $result);
  6056. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6057. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6058. $this->assertHtml($expected, $result);
  6059. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6060. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6061. $this->assertHtml($expected, $result);
  6062. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6063. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6064. $this->assertHtml($expected, $result);
  6065. $result = $this->Form->button('No type', ['type' => false]);
  6066. $expected = ['button' => [], 'No type', '/button'];
  6067. $this->assertHtml($expected, $result);
  6068. $result = $this->Form->button('Upload Text', [
  6069. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6070. 'escape' => false,
  6071. ]);
  6072. $this->assertNotRegExp('/\&039/', $result);
  6073. }
  6074. /**
  6075. * testButtonUnlockedByDefault method
  6076. *
  6077. * Test that button() makes unlocked fields by default.
  6078. *
  6079. * @return void
  6080. */
  6081. public function testButtonUnlockedByDefault()
  6082. {
  6083. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6084. $this->Form->button('Save', ['name' => 'save']);
  6085. $this->Form->button('Clear');
  6086. $result = $this->Form->unlockField();
  6087. $this->assertEquals(['save'], $result);
  6088. }
  6089. /**
  6090. * Test generation of a form button with confirm message.
  6091. *
  6092. * @return void
  6093. */
  6094. public function testButtonWithConfirm()
  6095. {
  6096. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6097. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6098. $this->assertHtml($expected, $result);
  6099. }
  6100. /**
  6101. * testPostButton method
  6102. *
  6103. * @return void
  6104. */
  6105. public function testPostButton()
  6106. {
  6107. $result = $this->Form->postButton('Hi', '/controller/action');
  6108. $expected = [
  6109. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6110. 'div' => ['style' => 'display:none;'],
  6111. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6112. '/div',
  6113. 'button' => ['type' => 'submit'],
  6114. 'Hi',
  6115. '/button',
  6116. '/form',
  6117. ];
  6118. $this->assertHtml($expected, $result);
  6119. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6120. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6121. }
  6122. /**
  6123. * testPostButtonMethodType method
  6124. *
  6125. * @return void
  6126. */
  6127. public function testPostButtonMethodType()
  6128. {
  6129. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6130. $expected = [
  6131. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6132. 'div' => ['style' => 'display:none;'],
  6133. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6134. '/div',
  6135. 'button' => ['type' => 'submit'],
  6136. 'Hi',
  6137. '/button',
  6138. '/form',
  6139. ];
  6140. $this->assertHtml($expected, $result);
  6141. }
  6142. /**
  6143. * testPostButtonFormOptions method
  6144. *
  6145. * @return void
  6146. */
  6147. public function testPostButtonFormOptions()
  6148. {
  6149. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6150. $expected = [
  6151. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6152. 'div' => ['style' => 'display:none;'],
  6153. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6154. '/div',
  6155. 'button' => ['type' => 'submit'],
  6156. 'Hi',
  6157. '/button',
  6158. '/form',
  6159. ];
  6160. $this->assertHtml($expected, $result);
  6161. }
  6162. /**
  6163. * testPostButtonNestedData method
  6164. *
  6165. * Test using postButton with N dimensional data.
  6166. *
  6167. * @return void
  6168. */
  6169. public function testPostButtonNestedData()
  6170. {
  6171. $data = [
  6172. 'one' => [
  6173. 'two' => [
  6174. 3, 4, 5,
  6175. ],
  6176. ],
  6177. ];
  6178. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6179. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6180. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6181. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6182. }
  6183. /**
  6184. * testSecurePostButton method
  6185. *
  6186. * Test that postButton adds _Token fields.
  6187. *
  6188. * @return void
  6189. */
  6190. public function testSecurePostButton()
  6191. {
  6192. $this->View->setRequest($this->View->getRequest()
  6193. ->withParam('_csrfToken', 'testkey')
  6194. ->withParam('_Token.unlockedFields', []));
  6195. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6196. $tokenDebug = urlencode(json_encode([
  6197. '/posts/delete/1',
  6198. [],
  6199. [],
  6200. ]));
  6201. $expected = [
  6202. 'form' => [
  6203. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6204. ],
  6205. ['div' => ['style' => 'display:none;']],
  6206. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6207. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6208. '/div',
  6209. 'button' => ['type' => 'submit'],
  6210. 'Delete',
  6211. '/button',
  6212. ['div' => ['style' => 'display:none;']],
  6213. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6214. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6215. ['input' => [
  6216. 'type' => 'hidden',
  6217. 'name' => '_Token[debug]',
  6218. 'value' => $tokenDebug,
  6219. 'autocomplete' => 'off',
  6220. ]],
  6221. '/div',
  6222. '/form',
  6223. ];
  6224. $this->assertHtml($expected, $result);
  6225. }
  6226. /**
  6227. * testPostLink method
  6228. *
  6229. * @return void
  6230. */
  6231. public function testPostLink()
  6232. {
  6233. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6234. $expected = [
  6235. 'form' => [
  6236. 'method' => 'post', 'action' => '/posts/delete/1',
  6237. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6238. ],
  6239. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6240. '/form',
  6241. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6242. 'Delete',
  6243. '/a',
  6244. ];
  6245. $this->assertHtml($expected, $result);
  6246. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6247. $expected = [
  6248. 'form' => [
  6249. 'method' => 'post', 'action' => '/posts/delete/1',
  6250. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6251. ],
  6252. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6253. '/form',
  6254. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6255. 'Delete',
  6256. '/a',
  6257. ];
  6258. $this->assertHtml($expected, $result);
  6259. $result = $this->Form->postLink(
  6260. 'Delete',
  6261. '/posts/delete/1',
  6262. ['target' => '_blank', 'class' => 'btn btn-danger']
  6263. );
  6264. $expected = [
  6265. 'form' => [
  6266. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6267. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6268. ],
  6269. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6270. '/form',
  6271. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6272. 'Delete',
  6273. '/a',
  6274. ];
  6275. $this->assertHtml($expected, $result);
  6276. }
  6277. /**
  6278. * testPostLinkWithConfirm method
  6279. *
  6280. * Test the confirm option for postLink().
  6281. *
  6282. * @return void
  6283. */
  6284. public function testPostLinkWithConfirm()
  6285. {
  6286. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6287. $expected = [
  6288. 'form' => [
  6289. 'method' => 'post', 'action' => '/posts/delete/1',
  6290. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6291. ],
  6292. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6293. '/form',
  6294. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6295. 'Delete',
  6296. '/a',
  6297. ];
  6298. $this->assertHtml($expected, $result);
  6299. $result = $this->Form->postLink(
  6300. 'Delete',
  6301. '/posts/delete/1',
  6302. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6303. );
  6304. $expected = [
  6305. 'form' => [
  6306. 'method' => 'post', 'action' => '/posts/delete/1',
  6307. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6308. ],
  6309. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6310. '/form',
  6311. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6312. 'Delete',
  6313. '/a',
  6314. ];
  6315. $this->assertHtml($expected, $result);
  6316. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  6317. $result = $this->Form->postLink(
  6318. 'Delete',
  6319. '/posts/delete/1',
  6320. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6321. );
  6322. $expected = [
  6323. 'form' => [
  6324. 'method' => 'post', 'action' => '/posts/delete/1',
  6325. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6326. ],
  6327. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6328. '/form',
  6329. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  6330. 'Delete',
  6331. '/a',
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. }
  6335. /**
  6336. * testPostLinkWithQuery method
  6337. *
  6338. * Test postLink() with query string args.
  6339. *
  6340. * @return void
  6341. */
  6342. public function testPostLinkWithQuery()
  6343. {
  6344. $result = $this->Form->postLink(
  6345. 'Delete',
  6346. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6347. );
  6348. $expected = [
  6349. 'form' => [
  6350. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6351. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6352. ],
  6353. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6354. '/form',
  6355. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6356. 'Delete',
  6357. '/a',
  6358. ];
  6359. $this->assertHtml($expected, $result);
  6360. }
  6361. /**
  6362. * testPostLinkWithData method
  6363. *
  6364. * Test postLink with additional data.
  6365. *
  6366. * @return void
  6367. */
  6368. public function testPostLinkWithData()
  6369. {
  6370. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6371. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6372. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6373. $this->Form->create($entity);
  6374. $this->Form->end();
  6375. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6376. $this->assertContains(
  6377. '<input type="hidden" name="name" value="show"',
  6378. $result,
  6379. 'should not contain entity data.'
  6380. );
  6381. }
  6382. /**
  6383. * testPostLinkSecurityHash method
  6384. *
  6385. * Test that security hashes for postLink include the url.
  6386. *
  6387. * @return void
  6388. */
  6389. public function testPostLinkSecurityHash()
  6390. {
  6391. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6392. $hash .= '%3Aid';
  6393. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  6394. $result = $this->Form->postLink(
  6395. 'Delete',
  6396. '/posts/delete/1',
  6397. ['data' => ['id' => 1]]
  6398. );
  6399. $tokenDebug = urlencode(json_encode([
  6400. '/posts/delete/1',
  6401. [
  6402. 'id' => 1,
  6403. ],
  6404. [],
  6405. ]));
  6406. $expected = [
  6407. 'form' => [
  6408. 'method' => 'post', 'action' => '/posts/delete/1',
  6409. 'name', 'style' => 'display:none;',
  6410. ],
  6411. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6412. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6413. 'div' => ['style' => 'display:none;'],
  6414. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6415. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6416. ['input' => [
  6417. 'type' => 'hidden',
  6418. 'name' => '_Token[debug]',
  6419. 'value' => $tokenDebug,
  6420. 'autocomplete' => 'off',
  6421. ]],
  6422. '/div',
  6423. '/form',
  6424. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6425. 'Delete',
  6426. '/a',
  6427. ];
  6428. $this->assertHtml($expected, $result);
  6429. }
  6430. /**
  6431. * testPostLinkSecurityHashBlockMode method
  6432. *
  6433. * Test that postLink doesn't modify the fields in the containing form.
  6434. *
  6435. * postLink() calls inside open forms should not modify the field list
  6436. * for the form.
  6437. *
  6438. * @return void
  6439. */
  6440. public function testPostLinkSecurityHashBlockMode()
  6441. {
  6442. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6443. $hash .= '%3A';
  6444. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  6445. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6446. $this->Form->control('title');
  6447. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6448. $result = $this->View->fetch('postLink');
  6449. $this->assertEquals(['title'], $this->Form->fields);
  6450. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6451. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6452. }
  6453. /**
  6454. * testPostLinkSecurityHashNoDebugMode method
  6455. *
  6456. * Test that security does not include debug token if debug is false.
  6457. *
  6458. * @return void
  6459. */
  6460. public function testPostLinkSecurityHashNoDebugMode()
  6461. {
  6462. Configure::write('debug', false);
  6463. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6464. $hash .= '%3Aid';
  6465. $this->View->setRequest($this->View->getRequest()
  6466. ->withParam('_Token.key', 'test'));
  6467. $result = $this->Form->postLink(
  6468. 'Delete',
  6469. '/posts/delete/1',
  6470. ['data' => ['id' => 1]]
  6471. );
  6472. $expected = [
  6473. 'form' => [
  6474. 'method' => 'post', 'action' => '/posts/delete/1',
  6475. 'name', 'style' => 'display:none;',
  6476. ],
  6477. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6478. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6479. 'div' => ['style' => 'display:none;'],
  6480. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6481. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6482. '/div',
  6483. '/form',
  6484. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6485. 'Delete',
  6486. '/a',
  6487. ];
  6488. $this->assertHtml($expected, $result);
  6489. }
  6490. /**
  6491. * testPostLinkNestedData method
  6492. *
  6493. * Test using postLink with N dimensional data.
  6494. *
  6495. * @return void
  6496. */
  6497. public function testPostLinkNestedData()
  6498. {
  6499. $data = [
  6500. 'one' => [
  6501. 'two' => [
  6502. 3, 4, 5,
  6503. ],
  6504. ],
  6505. ];
  6506. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6507. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6508. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6509. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6510. }
  6511. /**
  6512. * testPostLinkAfterGetForm method
  6513. *
  6514. * Test creating postLinks after a GET form.
  6515. *
  6516. * @return void
  6517. */
  6518. public function testPostLinkAfterGetForm()
  6519. {
  6520. $this->View->setRequest($this->View->getRequest()
  6521. ->withParam('_csrfToken', 'testkey')
  6522. ->withParam('_Token', 'val'));
  6523. $this->Form->create($this->article, ['type' => 'get']);
  6524. $this->Form->end();
  6525. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6526. $tokenDebug = urlencode(json_encode([
  6527. '/posts/delete/1',
  6528. [],
  6529. [],
  6530. ]));
  6531. $expected = [
  6532. 'form' => [
  6533. 'method' => 'post', 'action' => '/posts/delete/1',
  6534. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6535. ],
  6536. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6537. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6538. 'div' => ['style' => 'display:none;'],
  6539. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6540. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6541. ['input' => [
  6542. 'type' => 'hidden', 'name' => '_Token[debug]',
  6543. 'value' => $tokenDebug,
  6544. 'autocomplete' => 'off',
  6545. ]],
  6546. '/div',
  6547. '/form',
  6548. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6549. 'Delete',
  6550. '/a',
  6551. ];
  6552. $this->assertHtml($expected, $result);
  6553. }
  6554. /**
  6555. * testPostLinkFormBuffer method
  6556. *
  6557. * Test that postLink adds form tags to view block.
  6558. *
  6559. * @return void
  6560. */
  6561. public function testPostLinkFormBuffer()
  6562. {
  6563. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6564. $expected = [
  6565. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6566. 'Delete',
  6567. '/a',
  6568. ];
  6569. $this->assertHtml($expected, $result);
  6570. $result = $this->View->fetch('postLink');
  6571. $expected = [
  6572. 'form' => [
  6573. 'method' => 'post', 'action' => '/posts/delete/1',
  6574. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6575. ],
  6576. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6577. '/form',
  6578. ];
  6579. $this->assertHtml($expected, $result);
  6580. $result = $this->Form->postLink(
  6581. 'Delete',
  6582. '/posts/delete/2',
  6583. ['block' => true, 'method' => 'DELETE']
  6584. );
  6585. $expected = [
  6586. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6587. 'Delete',
  6588. '/a',
  6589. ];
  6590. $this->assertHtml($expected, $result);
  6591. $result = $this->View->fetch('postLink');
  6592. $expected = [
  6593. 'form' => [
  6594. 'method' => 'post', 'action' => '/posts/delete/1',
  6595. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6596. ],
  6597. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6598. '/form',
  6599. [
  6600. 'form' => [
  6601. 'method' => 'post', 'action' => '/posts/delete/2',
  6602. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6603. ],
  6604. ],
  6605. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6606. '/form',
  6607. ];
  6608. $this->assertHtml($expected, $result);
  6609. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6610. $expected = [
  6611. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6612. 'Delete',
  6613. '/a',
  6614. ];
  6615. $this->assertHtml($expected, $result);
  6616. $result = $this->View->fetch('foobar');
  6617. $expected = [
  6618. 'form' => [
  6619. 'method' => 'post', 'action' => '/posts/delete/1',
  6620. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6621. ],
  6622. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6623. '/form',
  6624. ];
  6625. $this->assertHtml($expected, $result);
  6626. }
  6627. /**
  6628. * testSubmitButton method
  6629. *
  6630. * @return void
  6631. */
  6632. public function testSubmitButton()
  6633. {
  6634. $result = $this->Form->submit('');
  6635. $expected = [
  6636. 'div' => ['class' => 'submit'],
  6637. 'input' => ['type' => 'submit', 'value' => ''],
  6638. '/div',
  6639. ];
  6640. $this->assertHtml($expected, $result);
  6641. $result = $this->Form->submit('Test Submit');
  6642. $expected = [
  6643. 'div' => ['class' => 'submit'],
  6644. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6645. '/div',
  6646. ];
  6647. $this->assertHtml($expected, $result);
  6648. $result = $this->Form->submit('Next >');
  6649. $expected = [
  6650. 'div' => ['class' => 'submit'],
  6651. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6652. '/div',
  6653. ];
  6654. $this->assertHtml($expected, $result);
  6655. $result = $this->Form->submit('Next >', ['escape' => false]);
  6656. $expected = [
  6657. 'div' => ['class' => 'submit'],
  6658. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6659. '/div',
  6660. ];
  6661. $this->assertHtml($expected, $result);
  6662. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6663. $expected = [
  6664. 'div' => ['class' => 'submit'],
  6665. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6666. '/div',
  6667. ];
  6668. $this->assertHtml($expected, $result);
  6669. }
  6670. /**
  6671. * testSubmitImage method
  6672. *
  6673. * Test image submit types.
  6674. *
  6675. * @return void
  6676. */
  6677. public function testSubmitImage()
  6678. {
  6679. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6680. $expected = [
  6681. 'div' => ['class' => 'submit'],
  6682. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6683. '/div',
  6684. ];
  6685. $this->assertHtml($expected, $result);
  6686. $result = $this->Form->submit('/relative/cake.power.gif');
  6687. $expected = [
  6688. 'div' => ['class' => 'submit'],
  6689. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6690. '/div',
  6691. ];
  6692. $this->assertHtml($expected, $result);
  6693. $result = $this->Form->submit('cake.power.gif');
  6694. $expected = [
  6695. 'div' => ['class' => 'submit'],
  6696. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6697. '/div',
  6698. ];
  6699. $this->assertHtml($expected, $result);
  6700. $result = $this->Form->submit('Not.an.image');
  6701. $expected = [
  6702. 'div' => ['class' => 'submit'],
  6703. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6704. '/div',
  6705. ];
  6706. $this->assertHtml($expected, $result);
  6707. }
  6708. /**
  6709. * testSubmitUnlockedByDefault method
  6710. *
  6711. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6712. * be submitted at a time.
  6713. *
  6714. * @return void
  6715. */
  6716. public function testSubmitUnlockedByDefault()
  6717. {
  6718. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  6719. $this->Form->submit('Go go');
  6720. $this->Form->submit('Save', ['name' => 'save']);
  6721. $result = $this->Form->unlockField();
  6722. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6723. }
  6724. /**
  6725. * testSubmitImageTimestamp method
  6726. *
  6727. * Test submit image with timestamps.
  6728. *
  6729. * @return void
  6730. */
  6731. public function testSubmitImageTimestamp()
  6732. {
  6733. Configure::write('Asset.timestamp', 'force');
  6734. $result = $this->Form->submit('cake.power.gif');
  6735. $expected = [
  6736. 'div' => ['class' => 'submit'],
  6737. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6738. '/div',
  6739. ];
  6740. $this->assertHtml($expected, $result);
  6741. }
  6742. /**
  6743. * testDateTimeWithGetForms method
  6744. *
  6745. * Test that datetime() works with GET style forms.
  6746. *
  6747. * @return void
  6748. */
  6749. public function testDateTimeWithGetForms()
  6750. {
  6751. $this->Form->create($this->article, ['type' => 'get']);
  6752. $result = $this->Form->datetime('created');
  6753. $expected = [
  6754. 'input' => [
  6755. 'type' => 'datetime-local',
  6756. 'name' => 'created',
  6757. 'value' => '',
  6758. ],
  6759. ];
  6760. $this->assertHtml($expected, $result);
  6761. $result = $this->Form->datetime('created', ['default' => true]);
  6762. $expected = [
  6763. 'input' => [
  6764. 'type' => 'datetime-local',
  6765. 'name' => 'created',
  6766. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}(:\d{2})?/',
  6767. ],
  6768. ];
  6769. $this->assertHtml($expected, $result);
  6770. }
  6771. /**
  6772. * testForMagicControlNonExistingNorValidated method
  6773. *
  6774. * @return void
  6775. */
  6776. public function testForMagicControlNonExistingNorValidated()
  6777. {
  6778. $this->Form->create($this->article);
  6779. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6780. $result = $this->Form->control('non_existing_nor_validated');
  6781. $expected = [
  6782. 'label' => ['for' => 'non-existing-nor-validated'],
  6783. 'Non Existing Nor Validated',
  6784. '/label',
  6785. 'input' => [
  6786. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6787. 'id' => 'non-existing-nor-validated',
  6788. ],
  6789. ];
  6790. $this->assertHtml($expected, $result);
  6791. $result = $this->Form->control('non_existing_nor_validated', [
  6792. 'val' => 'my value',
  6793. ]);
  6794. $expected = [
  6795. 'label' => ['for' => 'non-existing-nor-validated'],
  6796. 'Non Existing Nor Validated',
  6797. '/label',
  6798. 'input' => [
  6799. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6800. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  6801. ],
  6802. ];
  6803. $this->assertHtml($expected, $result);
  6804. $this->View->setRequest(
  6805. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  6806. );
  6807. $this->Form->create($this->article);
  6808. $result = $this->Form->control('non_existing_nor_validated');
  6809. $expected = [
  6810. 'label' => ['for' => 'non-existing-nor-validated'],
  6811. 'Non Existing Nor Validated',
  6812. '/label',
  6813. 'input' => [
  6814. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6815. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  6816. ],
  6817. ];
  6818. $this->assertHtml($expected, $result);
  6819. }
  6820. /**
  6821. * testFormMagicControlLabel method
  6822. *
  6823. * @return void
  6824. */
  6825. public function testFormMagicControlLabel()
  6826. {
  6827. $this->getTableLocator()->get('Contacts', [
  6828. 'className' => ContactsTable::class,
  6829. ]);
  6830. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6831. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6832. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6833. $expected = [
  6834. 'label' => ['for' => 'contacts-name'],
  6835. 'My label',
  6836. '/label',
  6837. 'input' => [
  6838. 'type' => 'text', 'name' => 'Contacts[name]',
  6839. 'id' => 'contacts-name', 'maxlength' => '255',
  6840. ],
  6841. ];
  6842. $this->assertHtml($expected, $result);
  6843. $result = $this->Form->control('name', [
  6844. 'label' => ['class' => 'mandatory'],
  6845. ]);
  6846. $expected = [
  6847. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6848. 'Name',
  6849. '/label',
  6850. 'input' => [
  6851. 'type' => 'text', 'name' => 'name',
  6852. 'id' => 'name', 'maxlength' => '255',
  6853. ],
  6854. ];
  6855. $this->assertHtml($expected, $result);
  6856. $result = $this->Form->control('name', [
  6857. 'div' => false,
  6858. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6859. ]);
  6860. $expected = [
  6861. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6862. 'My label',
  6863. '/label',
  6864. 'input' => [
  6865. 'type' => 'text', 'name' => 'name',
  6866. 'id' => 'name', 'maxlength' => '255',
  6867. ],
  6868. ];
  6869. $this->assertHtml($expected, $result);
  6870. $result = $this->Form->control('Contact.name', [
  6871. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6872. ]);
  6873. $expected = [
  6874. 'label' => ['for' => 'my_id'],
  6875. 'Name',
  6876. '/label',
  6877. 'input' => [
  6878. 'type' => 'text', 'name' => 'Contact[name]',
  6879. 'id' => 'my_id', 'maxlength' => '255',
  6880. ],
  6881. ];
  6882. $this->assertHtml($expected, $result);
  6883. $result = $this->Form->control('1.id');
  6884. $expected = ['input' => [
  6885. 'type' => 'hidden', 'name' => '1[id]',
  6886. 'id' => '1-id',
  6887. ]];
  6888. $this->assertHtml($expected, $result);
  6889. $result = $this->Form->control('1.name');
  6890. $expected = [
  6891. 'label' => ['for' => '1-name'],
  6892. 'Name',
  6893. '/label',
  6894. 'input' => [
  6895. 'type' => 'text', 'name' => '1[name]',
  6896. 'id' => '1-name', 'maxlength' => '255',
  6897. ],
  6898. ];
  6899. $this->assertHtml($expected, $result);
  6900. }
  6901. /**
  6902. * testFormEnd method
  6903. *
  6904. * @return void
  6905. */
  6906. public function testFormEnd()
  6907. {
  6908. $this->assertEquals('</form>', $this->Form->end());
  6909. }
  6910. /**
  6911. * testMultiRecordForm method
  6912. *
  6913. * Test the generation of fields for a multi record form.
  6914. *
  6915. * @return void
  6916. */
  6917. public function testMultiRecordForm()
  6918. {
  6919. $this->loadFixtures('Articles', 'Comments');
  6920. $articles = $this->getTableLocator()->get('Articles');
  6921. $articles->hasMany('Comments');
  6922. $comment = new Entity(['comment' => 'Value']);
  6923. $article = new Article(['comments' => [$comment]]);
  6924. $this->Form->create([$article]);
  6925. $result = $this->Form->control('0.comments.1.comment');
  6926. // phpcs:disable
  6927. $expected = [
  6928. 'div' => ['class' => 'input textarea'],
  6929. 'label' => ['for' => '0-comments-1-comment'],
  6930. 'Comment',
  6931. '/label',
  6932. 'textarea' => [
  6933. 'name',
  6934. 'id' => '0-comments-1-comment',
  6935. 'rows' => 5
  6936. ],
  6937. '/textarea',
  6938. '/div'
  6939. ];
  6940. // phpcs:enable
  6941. $this->assertHtml($expected, $result);
  6942. $result = $this->Form->control('0.comments.0.comment');
  6943. // phpcs:disable
  6944. $expected = [
  6945. 'div' => ['class' => 'input textarea'],
  6946. 'label' => ['for' => '0-comments-0-comment'],
  6947. 'Comment',
  6948. '/label',
  6949. 'textarea' => [
  6950. 'name',
  6951. 'id' => '0-comments-0-comment',
  6952. 'rows' => 5
  6953. ],
  6954. 'Value',
  6955. '/textarea',
  6956. '/div'
  6957. ];
  6958. // phpcs:enable
  6959. $this->assertHtml($expected, $result);
  6960. $comment->setError('comment', ['Not valid']);
  6961. $result = $this->Form->control('0.comments.0.comment');
  6962. // phpcs:disable
  6963. $expected = [
  6964. 'div' => ['class' => 'input textarea error'],
  6965. 'label' => ['for' => '0-comments-0-comment'],
  6966. 'Comment',
  6967. '/label',
  6968. 'textarea' => [
  6969. 'name',
  6970. 'class' => 'form-error',
  6971. 'id' => '0-comments-0-comment',
  6972. 'rows' => 5
  6973. ],
  6974. 'Value',
  6975. '/textarea',
  6976. ['div' => ['class' => 'error-message']],
  6977. 'Not valid',
  6978. '/div',
  6979. '/div'
  6980. ];
  6981. // phpcs:enable
  6982. $this->assertHtml($expected, $result);
  6983. $this->getTableLocator()->get('Comments')
  6984. ->getValidator('default')
  6985. ->allowEmpty('comment', false);
  6986. $result = $this->Form->control('0.comments.1.comment');
  6987. // phpcs:disable
  6988. $expected = [
  6989. 'div' => ['class' => 'input textarea required'],
  6990. 'label' => ['for' => '0-comments-1-comment'],
  6991. 'Comment',
  6992. '/label',
  6993. 'textarea' => [
  6994. 'name',
  6995. 'required' => 'required',
  6996. 'id' => '0-comments-1-comment',
  6997. 'rows' => 5,
  6998. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  6999. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7000. ],
  7001. '/textarea',
  7002. '/div'
  7003. ];
  7004. // phpcs:enable
  7005. $this->assertHtml($expected, $result);
  7006. }
  7007. /**
  7008. * testHtml5Controls method
  7009. *
  7010. * Test that some html5 inputs + FormHelper::__call() work.
  7011. *
  7012. * @return void
  7013. */
  7014. public function testHtml5Controls()
  7015. {
  7016. $result = $this->Form->email('User.email');
  7017. $expected = [
  7018. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7019. ];
  7020. $this->assertHtml($expected, $result);
  7021. $result = $this->Form->search('User.query');
  7022. $expected = [
  7023. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7024. ];
  7025. $this->assertHtml($expected, $result);
  7026. $result = $this->Form->search('User.query', ['value' => 'test']);
  7027. $expected = [
  7028. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7029. ];
  7030. $this->assertHtml($expected, $result);
  7031. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7032. $expected = [
  7033. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7034. ];
  7035. $this->assertHtml($expected, $result);
  7036. }
  7037. /**
  7038. * testHtml5ControlWithControl method
  7039. *
  7040. * Test accessing html5 inputs through control().
  7041. *
  7042. * @return void
  7043. */
  7044. public function testHtml5ControlWithControl()
  7045. {
  7046. $this->Form->create();
  7047. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7048. $result = $this->Form->control('website', [
  7049. 'type' => 'url',
  7050. 'val' => 'http://domain.tld',
  7051. 'label' => false,
  7052. ]);
  7053. $expected = [
  7054. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7055. ];
  7056. $this->assertHtml($expected, $result);
  7057. }
  7058. /**
  7059. * testHtml5ControlException method
  7060. *
  7061. * Test errors when field name is missing.
  7062. *
  7063. * @return void
  7064. */
  7065. public function testHtml5ControlException()
  7066. {
  7067. $this->expectException(\Cake\Core\Exception\Exception::class);
  7068. $this->Form->email();
  7069. }
  7070. /**
  7071. * tests fields that are required use custom validation messages
  7072. *
  7073. * @return void
  7074. */
  7075. public function testHtml5ErrorMessage()
  7076. {
  7077. $this->Form->setConfig('autoSetCustomValidity', true);
  7078. $validator = (new \Cake\Validation\Validator())
  7079. ->requirePresence('email', true, 'Custom error message')
  7080. ->requirePresence('password')
  7081. ->alphaNumeric('password')
  7082. ->notBlank('phone');
  7083. $table = $this->getTableLocator()->get('Contacts', [
  7084. 'className' => ContactsTable::class,
  7085. ]);
  7086. $table->setValidator('default', $validator);
  7087. $contact = new Entity();
  7088. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7089. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7090. $result = $this->Form->control('password');
  7091. $expected = [
  7092. 'label' => ['for' => 'password'],
  7093. 'Password',
  7094. '/label',
  7095. 'input' => [
  7096. 'id' => 'password',
  7097. 'name' => 'password',
  7098. 'type' => 'password',
  7099. 'value' => '',
  7100. 'required' => 'required',
  7101. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7102. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  7103. ],
  7104. ];
  7105. $this->assertHtml($expected, $result);
  7106. $result = $this->Form->control('phone');
  7107. $expected = [
  7108. 'label' => ['for' => 'phone'],
  7109. 'Phone',
  7110. '/label',
  7111. 'input' => [
  7112. 'id' => 'phone',
  7113. 'name' => 'phone',
  7114. 'type' => 'tel',
  7115. 'value' => '',
  7116. 'maxlength' => 255,
  7117. 'required' => 'required',
  7118. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7119. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7120. ],
  7121. ];
  7122. $this->assertHtml($expected, $result);
  7123. $result = $this->Form->control('email');
  7124. $expected = [
  7125. 'label' => ['for' => 'email'],
  7126. 'Email',
  7127. '/label',
  7128. 'input' => [
  7129. 'id' => 'email',
  7130. 'name' => 'email',
  7131. 'type' => 'email',
  7132. 'value' => '',
  7133. 'maxlength' => 255,
  7134. 'required' => 'required',
  7135. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7136. 'oninvalid' => 'this.setCustomValidity(&#039;Custom error message&#039;)',
  7137. ],
  7138. ];
  7139. $this->assertHtml($expected, $result);
  7140. }
  7141. /**
  7142. * tests that custom validation messages are in templateVars
  7143. *
  7144. * @return void
  7145. */
  7146. public function testHtml5ErrorMessageInTemplateVars()
  7147. {
  7148. $validator = (new \Cake\Validation\Validator())
  7149. ->requirePresence('email', true, 'Custom error "message" & entities')
  7150. ->requirePresence('password')
  7151. ->alphaNumeric('password')
  7152. ->notBlank('phone');
  7153. $table = $this->getTableLocator()->get('Contacts', [
  7154. 'className' => ContactsTable::class,
  7155. ]);
  7156. $table->setValidator('default', $validator);
  7157. $contact = new Entity();
  7158. $this->Form->setConfig('autoSetCustomValidity', false);
  7159. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7160. $this->Form->setTemplates([
  7161. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7162. 'inputContainer' => '{{content}}',
  7163. ]);
  7164. $result = $this->Form->control('password');
  7165. $expected = [
  7166. 'label' => ['for' => 'password'],
  7167. 'Password',
  7168. '/label',
  7169. 'input' => [
  7170. 'id' => 'password',
  7171. 'name' => 'password',
  7172. 'type' => 'password',
  7173. 'value' => '',
  7174. 'required' => 'required',
  7175. 'data-message' => 'This field is required',
  7176. ],
  7177. ];
  7178. $this->assertHtml($expected, $result);
  7179. $result = $this->Form->control('phone');
  7180. $expected = [
  7181. 'label' => ['for' => 'phone'],
  7182. 'Phone',
  7183. '/label',
  7184. 'input' => [
  7185. 'id' => 'phone',
  7186. 'name' => 'phone',
  7187. 'type' => 'tel',
  7188. 'value' => '',
  7189. 'maxlength' => 255,
  7190. 'required' => 'required',
  7191. 'data-message' => 'This field cannot be left empty',
  7192. ],
  7193. ];
  7194. $this->assertHtml($expected, $result);
  7195. $result = $this->Form->control('email', [
  7196. 'templateVars' => [
  7197. 'custom' => 'data-custom="1"',
  7198. ],
  7199. ]);
  7200. $expected = [
  7201. 'label' => ['for' => 'email'],
  7202. 'Email',
  7203. '/label',
  7204. 'input' => [
  7205. 'id' => 'email',
  7206. 'name' => 'email',
  7207. 'type' => 'email',
  7208. 'value' => '',
  7209. 'maxlength' => 255,
  7210. 'required' => 'required',
  7211. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7212. 'data-custom' => '1',
  7213. ],
  7214. ];
  7215. $this->assertHtml($expected, $result);
  7216. $this->Form->setConfig('autoSetCustomValidity', true);
  7217. }
  7218. /**
  7219. * testRequiredAttribute method
  7220. *
  7221. * Tests that formhelper sets required attributes.
  7222. *
  7223. * @return void
  7224. */
  7225. public function testRequiredAttribute()
  7226. {
  7227. $this->article['required'] = [
  7228. 'title' => true,
  7229. 'body' => false,
  7230. ];
  7231. $this->Form->create($this->article);
  7232. $result = $this->Form->control('title');
  7233. $expected = [
  7234. 'div' => ['class' => 'input text required'],
  7235. 'label' => ['for' => 'title'],
  7236. 'Title',
  7237. '/label',
  7238. 'input' => [
  7239. 'type' => 'text',
  7240. 'name' => 'title',
  7241. 'id' => 'title',
  7242. 'required' => 'required',
  7243. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  7244. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7245. ],
  7246. '/div',
  7247. ];
  7248. $this->assertHtml($expected, $result);
  7249. $result = $this->Form->control('title', ['required' => false]);
  7250. $this->assertNotContains('required', $result);
  7251. $result = $this->Form->control('body');
  7252. $expected = [
  7253. 'div' => ['class' => 'input text'],
  7254. 'label' => ['for' => 'body'],
  7255. 'Body',
  7256. '/label',
  7257. 'input' => [
  7258. 'type' => 'text',
  7259. 'name' => 'body',
  7260. 'id' => 'body',
  7261. ],
  7262. '/div',
  7263. ];
  7264. $this->assertHtml($expected, $result);
  7265. $result = $this->Form->control('body', ['required' => true]);
  7266. $this->assertContains('required', $result);
  7267. }
  7268. /**
  7269. * testControlsNotNested method
  7270. *
  7271. * Tests that it is possible to put inputs outside of the label.
  7272. *
  7273. * @return void
  7274. */
  7275. public function testControlsNotNested()
  7276. {
  7277. $this->Form->setTemplates([
  7278. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7279. 'formGroup' => '{{input}}{{label}}',
  7280. ]);
  7281. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7282. $expected = [
  7283. 'div' => ['class' => 'input checkbox'],
  7284. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7285. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7286. 'label' => ['for' => 'foo'],
  7287. 'Foo',
  7288. '/label',
  7289. '/div',
  7290. ];
  7291. $this->assertHtml($expected, $result);
  7292. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7293. $expected = [
  7294. 'div' => ['class' => 'input checkbox'],
  7295. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7296. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7297. '/div',
  7298. ];
  7299. $this->assertHtml($expected, $result);
  7300. $result = $this->Form->control('confirm', [
  7301. 'type' => 'radio',
  7302. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7303. ]);
  7304. $expected = [
  7305. 'div' => ['class' => 'input radio'],
  7306. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7307. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7308. ['label' => ['for' => 'confirm-y']],
  7309. 'Yes',
  7310. '/label',
  7311. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7312. ['label' => ['for' => 'confirm-n']],
  7313. 'No',
  7314. '/label',
  7315. '<label',
  7316. 'Confirm',
  7317. '/label',
  7318. '/div',
  7319. ];
  7320. $this->assertHtml($expected, $result);
  7321. $result = $this->Form->select('category', ['1', '2'], [
  7322. 'multiple' => 'checkbox',
  7323. 'name' => 'fish',
  7324. ]);
  7325. $expected = [
  7326. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7327. ['div' => ['class' => 'checkbox']],
  7328. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7329. ['label' => ['for' => 'fish-0']],
  7330. '1',
  7331. '/label',
  7332. '/div',
  7333. ['div' => ['class' => 'checkbox']],
  7334. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7335. ['label' => ['for' => 'fish-1']],
  7336. '2',
  7337. '/label',
  7338. '/div',
  7339. ];
  7340. $this->assertHtml($expected, $result);
  7341. }
  7342. /**
  7343. * testControlContainerTemplates method
  7344. *
  7345. * Test that *Container templates are used by input.
  7346. *
  7347. * @return void
  7348. */
  7349. public function testControlContainerTemplates()
  7350. {
  7351. $this->Form->setTemplates([
  7352. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7353. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7354. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7355. ]);
  7356. $this->article['errors'] = [
  7357. 'Article' => ['published' => 'error message'],
  7358. ];
  7359. $this->Form->create($this->article);
  7360. $result = $this->Form->control('accept', [
  7361. 'type' => 'checkbox',
  7362. ]);
  7363. $expected = [
  7364. 'div' => ['class' => 'check'],
  7365. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7366. 'label' => ['for' => 'accept'],
  7367. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7368. 'Accept',
  7369. '/label',
  7370. '/div',
  7371. ];
  7372. $this->assertHtml($expected, $result);
  7373. $result = $this->Form->control('accept', [
  7374. 'type' => 'radio',
  7375. 'options' => ['Y', 'N'],
  7376. ]);
  7377. $this->assertContains('<div class="rad">', $result);
  7378. $result = $this->Form->control('Article.published', [
  7379. 'type' => 'radio',
  7380. 'options' => ['Y', 'N'],
  7381. ]);
  7382. $this->assertContains('<div class="rad err">', $result);
  7383. }
  7384. /**
  7385. * testFormGroupTemplates method
  7386. *
  7387. * Test that *Container templates are used by input.
  7388. *
  7389. * @return void
  7390. */
  7391. public function testFormGroupTemplates()
  7392. {
  7393. $this->Form->setTemplates([
  7394. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7395. ]);
  7396. $this->Form->create($this->article);
  7397. $result = $this->Form->control('accept', [
  7398. 'type' => 'radio',
  7399. 'options' => ['Y', 'N'],
  7400. ]);
  7401. $this->assertContains('<div class="radio">', $result);
  7402. }
  7403. /**
  7404. * testResetTemplates method
  7405. *
  7406. * Test resetting templates.
  7407. *
  7408. * @return void
  7409. */
  7410. public function testResetTemplates()
  7411. {
  7412. $this->Form->setTemplates(['input' => '<input/>']);
  7413. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7414. $this->Form->resetTemplates();
  7415. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7416. }
  7417. /**
  7418. * testContext method
  7419. *
  7420. * Test the context method.
  7421. *
  7422. * @return void
  7423. */
  7424. public function testContext()
  7425. {
  7426. $result = $this->Form->context();
  7427. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7428. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7429. $this->assertSame($mock, $this->Form->context($mock));
  7430. $this->assertSame($mock, $this->Form->context());
  7431. }
  7432. /**
  7433. * testAutoDomId method
  7434. *
  7435. * @return void
  7436. */
  7437. public function testAutoDomId()
  7438. {
  7439. $result = $this->Form->text('field', ['id' => true]);
  7440. $expected = [
  7441. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7442. ];
  7443. $this->assertHtml($expected, $result);
  7444. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7445. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7446. $expected = [
  7447. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7448. ['label' => ['for' => 'field-0']],
  7449. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7450. 'option A',
  7451. '/label',
  7452. ['label' => ['for' => 'field-1']],
  7453. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7454. 'option B',
  7455. '/label',
  7456. ];
  7457. $this->assertHtml($expected, $result);
  7458. $result = $this->Form->select(
  7459. 'multi_field',
  7460. ['first', 'second'],
  7461. ['multiple' => 'checkbox', 'id' => true]
  7462. );
  7463. $expected = [
  7464. 'input' => [
  7465. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7466. ],
  7467. ['div' => ['class' => 'checkbox']],
  7468. ['label' => ['for' => 'multi-field-0']],
  7469. ['input' => [
  7470. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7471. 'value' => '0', 'id' => 'multi-field-0',
  7472. ]],
  7473. 'first',
  7474. '/label',
  7475. '/div',
  7476. ['div' => ['class' => 'checkbox']],
  7477. ['label' => ['for' => 'multi-field-1']],
  7478. ['input' => [
  7479. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7480. 'value' => '1', 'id' => 'multi-field-1',
  7481. ]],
  7482. 'second',
  7483. '/label',
  7484. '/div',
  7485. ];
  7486. $this->assertHtml($expected, $result);
  7487. }
  7488. /**
  7489. * Test the basic setters and getters for value sources
  7490. *
  7491. * @return void
  7492. */
  7493. public function testFormValueSourcesSettersGetters()
  7494. {
  7495. $this->View->setRequest($this->View->getRequest()
  7496. ->withData('id', '1')
  7497. ->withQueryParams(['id' => '2']));
  7498. $expected = ['context'];
  7499. $result = $this->Form->getValueSources();
  7500. $this->assertEquals($expected, $result);
  7501. $expected = ['query', 'data', 'context'];
  7502. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7503. $result = $this->Form->getValueSources();
  7504. $this->assertEquals($expected, $result);
  7505. $this->Form->setValueSources(['context']);
  7506. $expected = '1';
  7507. $result = $this->Form->getSourceValue('id');
  7508. $this->assertEquals($expected, $result);
  7509. $this->Form->setValueSources('query');
  7510. $expected = ['query'];
  7511. $result = $this->Form->getValueSources();
  7512. $this->assertEquals($expected, $result);
  7513. $expected = '2';
  7514. $result = $this->Form->getSourceValue('id');
  7515. $this->assertEquals($expected, $result);
  7516. $this->Form->setValueSources(['data']);
  7517. $expected = '1';
  7518. $result = $this->Form->getSourceValue('id');
  7519. $this->assertEquals($expected, $result);
  7520. $this->Form->setValueSources(['query', 'data']);
  7521. $expected = '2';
  7522. $result = $this->Form->getSourceValue('id');
  7523. $this->assertEquals($expected, $result);
  7524. }
  7525. /**
  7526. * Tests the different input rendering values based on sources values switching
  7527. *
  7528. * @return void
  7529. */
  7530. public function testFormValueSourcesSingleSwitchRendering()
  7531. {
  7532. $this->loadFixtures('Articles');
  7533. $articles = $this->getTableLocator()->get('Articles');
  7534. $article = new Article();
  7535. $articles->patchEntity($article, ['id' => '3']);
  7536. $this->Form->create($article);
  7537. $this->Form->setValueSources(['context']);
  7538. $result = $this->Form->control('id');
  7539. $expected = [
  7540. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7541. ];
  7542. $this->assertHtml($expected, $result);
  7543. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7544. $this->Form->setValueSources(['query']);
  7545. $result = $this->Form->control('id');
  7546. $expected = [
  7547. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7548. ];
  7549. $this->assertHtml($expected, $result);
  7550. $this->View->setRequest($this->View->getRequest()
  7551. ->withQueryParams(['id' => '5a'])
  7552. ->withData('id', '5b'));
  7553. $this->Form->setValueSources(['context']);
  7554. $this->Form->create($article);
  7555. $result = $this->Form->control('id');
  7556. $expected = [
  7557. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7558. ];
  7559. $this->assertHtml($expected, $result);
  7560. $this->Form->setValueSources(['data']);
  7561. $this->Form->create($article);
  7562. $result = $this->Form->control('id');
  7563. $expected = [
  7564. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7565. ];
  7566. $this->assertHtml($expected, $result);
  7567. $this->Form->setValueSources(['query']);
  7568. $result = $this->Form->control('id');
  7569. $expected = [
  7570. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7571. ];
  7572. $this->assertHtml($expected, $result);
  7573. }
  7574. /**
  7575. * Tests the different input rendering values based on sources values switching while supplying
  7576. * an entity (base context) and multiple sources (such as data, query)
  7577. *
  7578. * @return void
  7579. */
  7580. public function testFormValueSourcesListSwitchRendering()
  7581. {
  7582. $this->loadFixtures('Articles');
  7583. $articles = $this->getTableLocator()->get('Articles');
  7584. $article = new Article();
  7585. $articles->patchEntity($article, ['id' => '3']);
  7586. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7587. $this->Form->create($article);
  7588. $this->Form->setValueSources(['context', 'query']);
  7589. $result = $this->Form->control('id');
  7590. $expected = [
  7591. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7592. ];
  7593. $this->assertHtml($expected, $result);
  7594. $this->Form->setValueSources(['query', 'context']);
  7595. $result = $this->Form->control('id');
  7596. $expected = [
  7597. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7598. ];
  7599. $this->assertHtml($expected, $result);
  7600. $this->Form->setValueSources(['data', 'query', 'context']);
  7601. $result = $this->Form->control('id');
  7602. $expected = [
  7603. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7604. ];
  7605. $this->assertHtml($expected, $result);
  7606. $this->View->setRequest($this->View->getRequest()
  7607. ->withData('id', '8')
  7608. ->withQueryParams(['id' => '9']));
  7609. $this->Form->setValueSources(['data', 'query', 'context']);
  7610. $result = $this->Form->control('id');
  7611. $expected = [
  7612. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7613. ];
  7614. $this->assertHtml($expected, $result);
  7615. }
  7616. /**
  7617. * Test the different form input renderings based on values sources switchings through form options
  7618. *
  7619. * @return void
  7620. */
  7621. public function testFormValueSourcesSwitchViaOptionsRendering()
  7622. {
  7623. $this->loadFixtures('Articles');
  7624. $articles = $this->getTableLocator()->get('Articles');
  7625. $article = new Article();
  7626. $articles->patchEntity($article, ['id' => '3']);
  7627. $this->View->setRequest(
  7628. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  7629. );
  7630. $this->Form->create($article, ['valueSources' => 'query']);
  7631. $result = $this->Form->control('id');
  7632. $expected = [
  7633. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7634. ];
  7635. $this->assertHtml($expected, $result);
  7636. $result = $this->Form->getSourceValue('id');
  7637. $this->assertEquals('5', $result);
  7638. $this->Form->setValueSources(['context']);
  7639. $this->Form->create($article, ['valueSources' => 'query']);
  7640. $result = $this->Form->control('id');
  7641. $expected = [
  7642. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7643. ];
  7644. $this->assertHtml($expected, $result);
  7645. $result = $this->Form->getSourceValue('id');
  7646. $this->assertEquals('5', $result);
  7647. $this->Form->setValueSources(['query']);
  7648. $this->Form->create($article, ['valueSources' => 'data']);
  7649. $result = $this->Form->control('id');
  7650. $expected = [
  7651. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7652. ];
  7653. $this->assertHtml($expected, $result);
  7654. $result = $this->Form->getSourceValue('id');
  7655. $this->assertEquals('4', $result);
  7656. $this->Form->setValueSources(['query']);
  7657. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7658. $result = $this->Form->control('id');
  7659. $expected = [
  7660. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7661. ];
  7662. $this->assertHtml($expected, $result);
  7663. $result = $this->Form->getSourceValue('id');
  7664. $this->assertEquals('4', $result);
  7665. }
  7666. /**
  7667. * Test the different form input renderings based on values sources switchings through form options
  7668. *
  7669. * @return void
  7670. */
  7671. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7672. {
  7673. $this->loadFixtures('Articles');
  7674. $articles = $this->getTableLocator()->get('Articles');
  7675. $article = new Article();
  7676. $articles->patchEntity($article, ['id' => '3']);
  7677. $this->View->setRequest(
  7678. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  7679. );
  7680. $this->Form->setValueSources(['context'])
  7681. ->create($article, ['valueSources' => ['query', 'data']]);
  7682. $result = $this->Form->control('id');
  7683. $expected = [
  7684. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7685. ];
  7686. $this->assertHtml($expected, $result);
  7687. $result = $this->Form->getSourceValue('id');
  7688. $this->assertEquals('11', $result);
  7689. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7690. $this->Form->setValueSources(['context'])
  7691. ->create($article, ['valueSources' => ['query', 'data']]);
  7692. $result = $this->Form->control('id');
  7693. $expected = [
  7694. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7695. ];
  7696. $this->assertHtml($expected, $result);
  7697. $result = $this->Form->getSourceValue('id');
  7698. $this->assertEquals('10', $result);
  7699. }
  7700. /**
  7701. * Test the different form values sources resetting through From::end();
  7702. *
  7703. * @return void
  7704. */
  7705. public function testFormValueSourcesResetViaEnd()
  7706. {
  7707. $expected = ['context'];
  7708. $result = $this->Form->getValueSources();
  7709. $this->assertEquals($expected, $result);
  7710. $expected = ['query', 'context', 'data'];
  7711. $this->Form->setValueSources(['query', 'context', 'data']);
  7712. $result = $this->Form->getValueSources();
  7713. $this->assertEquals($expected, $result);
  7714. $this->Form->create();
  7715. $result = $this->Form->getValueSources();
  7716. $this->assertEquals($expected, $result);
  7717. $this->Form->end();
  7718. $result = $this->Form->getValueSources();
  7719. $this->assertEquals(['context'], $result);
  7720. }
  7721. /**
  7722. * Test sources values defaults handling
  7723. *
  7724. * @return void
  7725. */
  7726. public function testFormValueSourcesDefaults()
  7727. {
  7728. $this->View->setRequest(
  7729. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7730. );
  7731. $this->Form->create();
  7732. $result = $this->Form->password('password');
  7733. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7734. $this->assertHtml($expected, $result);
  7735. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7736. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7737. $this->assertHtml($expected, $result);
  7738. $this->Form->setValueSources('query');
  7739. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7740. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7741. $this->assertHtml($expected, $result);
  7742. $this->Form->setValueSources('data');
  7743. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7744. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7745. $this->assertHtml($expected, $result);
  7746. }
  7747. /**
  7748. * Test sources values schema defaults handling
  7749. *
  7750. * @return void
  7751. */
  7752. public function testSourcesValueDoesntExistPassThrough()
  7753. {
  7754. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7755. $articles = $this->getTableLocator()->get('Articles');
  7756. $entity = $articles->createEntity();
  7757. $this->Form->create($entity);
  7758. $this->Form->setValueSources(['query', 'context']);
  7759. $result = $this->Form->getSourceValue('category');
  7760. $this->assertEquals('sesame-cookies', $result);
  7761. $this->Form->setValueSources(['context', 'query']);
  7762. $result = $this->Form->getSourceValue('category');
  7763. $this->assertEquals('sesame-cookies', $result);
  7764. }
  7765. /**
  7766. * testNestedLabelInput method
  7767. *
  7768. * Test the `nestedInput` parameter
  7769. *
  7770. * @return void
  7771. */
  7772. public function testNestedLabelInput()
  7773. {
  7774. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7775. $expected = [
  7776. 'div' => ['class' => 'input text'],
  7777. 'label' => ['for' => 'foo'],
  7778. ['input' => [
  7779. 'type' => 'text',
  7780. 'name' => 'foo',
  7781. 'id' => 'foo',
  7782. ]],
  7783. 'Foo',
  7784. '/label',
  7785. '/div',
  7786. ];
  7787. $this->assertHtml($expected, $result);
  7788. }
  7789. /**
  7790. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7791. *
  7792. * This test makes sure `false` excludes the label from the render
  7793. *
  7794. * @return void
  7795. */
  7796. public function testControlLabelManipulationDisableLabels()
  7797. {
  7798. $result = $this->Form->control('test', [
  7799. 'type' => 'radio',
  7800. 'options' => ['A', 'B'],
  7801. 'labelOptions' => false,
  7802. ]);
  7803. $expected = [
  7804. ['div' => ['class' => 'input radio']],
  7805. '<label',
  7806. 'Test',
  7807. '/label',
  7808. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7809. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7810. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7811. '/div',
  7812. ];
  7813. $this->assertHtml($expected, $result);
  7814. $result = $this->Form->control('checkbox1', [
  7815. 'label' => 'My checkboxes',
  7816. 'multiple' => 'checkbox',
  7817. 'type' => 'select',
  7818. 'options' => [
  7819. ['text' => 'First Checkbox', 'value' => 1],
  7820. ['text' => 'Second Checkbox', 'value' => 2],
  7821. ],
  7822. 'labelOptions' => false,
  7823. ]);
  7824. $expected = [
  7825. ['div' => ['class' => 'input select']],
  7826. ['label' => ['for' => 'checkbox1']],
  7827. 'My checkboxes',
  7828. '/label',
  7829. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7830. ['div' => ['class' => 'checkbox']],
  7831. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7832. '/div',
  7833. ['div' => ['class' => 'checkbox']],
  7834. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7835. '/div',
  7836. '/div',
  7837. ];
  7838. $this->assertHtml($expected, $result);
  7839. }
  7840. /**
  7841. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7842. *
  7843. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7844. * added to the class if checked.
  7845. *
  7846. * Also checks to make sure any custom attributes are rendered correctly
  7847. *
  7848. * @return void
  7849. */
  7850. public function testControlLabelManipulationRadios()
  7851. {
  7852. $result = $this->Form->control('test', [
  7853. 'type' => 'radio',
  7854. 'options' => ['A', 'B'],
  7855. 'labelOptions' => ['class' => 'custom-class'],
  7856. ]);
  7857. $expected = [
  7858. ['div' => ['class' => 'input radio']],
  7859. '<label',
  7860. 'Test',
  7861. '/label',
  7862. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7863. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7864. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7865. 'A',
  7866. '/label',
  7867. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7868. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7869. 'B',
  7870. '/label',
  7871. '/div',
  7872. ];
  7873. $this->assertHtml($expected, $result);
  7874. $result = $this->Form->control('test', [
  7875. 'type' => 'radio',
  7876. 'options' => ['A', 'B'],
  7877. 'value' => 1,
  7878. 'labelOptions' => ['class' => 'custom-class'],
  7879. ]);
  7880. $expected = [
  7881. ['div' => ['class' => 'input radio']],
  7882. '<label',
  7883. 'Test',
  7884. '/label',
  7885. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7886. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7887. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7888. 'A',
  7889. '/label',
  7890. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7891. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7892. 'B',
  7893. '/label',
  7894. '/div',
  7895. ];
  7896. $this->assertHtml($expected, $result);
  7897. $result = $this->Form->control('test', [
  7898. 'type' => 'radio',
  7899. 'options' => ['A', 'B'],
  7900. 'value' => 1,
  7901. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7902. ]);
  7903. $expected = [
  7904. ['div' => ['class' => 'input radio']],
  7905. '<label',
  7906. 'Test',
  7907. '/label',
  7908. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7909. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7910. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7911. 'A',
  7912. '/label',
  7913. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7914. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7915. 'B',
  7916. '/label',
  7917. '/div',
  7918. ];
  7919. $this->assertHtml($expected, $result);
  7920. $result = $this->Form->radio('test', ['A', 'B'], [
  7921. 'label' => [
  7922. 'class' => ['custom-class', 'another-class'],
  7923. 'data-name' => 'bob',
  7924. ],
  7925. 'value' => 1,
  7926. ]);
  7927. $expected = [
  7928. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  7929. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7930. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7931. 'A',
  7932. '/label',
  7933. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7934. ['input' => [
  7935. 'type' => 'radio',
  7936. 'name' => 'test',
  7937. 'value' => '1',
  7938. 'id' => 'test-1',
  7939. 'checked' => 'checked',
  7940. ]],
  7941. 'B',
  7942. '/label',
  7943. ];
  7944. $this->assertHtml($expected, $result);
  7945. }
  7946. /**
  7947. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7948. *
  7949. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7950. * added to the class if checked.
  7951. *
  7952. * Also checks to make sure any custom attributes are rendered correctly
  7953. *
  7954. * @return void
  7955. */
  7956. public function testControlLabelManipulationCheckboxes()
  7957. {
  7958. $result = $this->Form->control('checkbox1', [
  7959. 'label' => 'My checkboxes',
  7960. 'multiple' => 'checkbox',
  7961. 'type' => 'select',
  7962. 'options' => [
  7963. ['text' => 'First Checkbox', 'value' => 1],
  7964. ['text' => 'Second Checkbox', 'value' => 2],
  7965. ],
  7966. 'labelOptions' => ['class' => 'custom-class'],
  7967. 'value' => ['1'],
  7968. ]);
  7969. $expected = [
  7970. ['div' => ['class' => 'input select']],
  7971. ['label' => ['for' => 'checkbox1']],
  7972. 'My checkboxes',
  7973. '/label',
  7974. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7975. ['div' => ['class' => 'checkbox']],
  7976. ['label' => [
  7977. 'class' => 'custom-class selected',
  7978. 'for' => 'checkbox1-1',
  7979. ]],
  7980. ['input' => [
  7981. 'type' => 'checkbox',
  7982. 'name' => 'checkbox1[]',
  7983. 'value' => '1',
  7984. 'id' => 'checkbox1-1',
  7985. 'checked' => 'checked',
  7986. ]],
  7987. 'First Checkbox',
  7988. '/label',
  7989. '/div',
  7990. ['div' => ['class' => 'checkbox']],
  7991. ['label' => [
  7992. 'class' => 'custom-class',
  7993. 'for' => 'checkbox1-2',
  7994. ]],
  7995. ['input' => [
  7996. 'type' => 'checkbox',
  7997. 'name' => 'checkbox1[]',
  7998. 'value' => '2',
  7999. 'id' => 'checkbox1-2',
  8000. ]],
  8001. 'Second Checkbox',
  8002. '/label',
  8003. '/div',
  8004. '/div',
  8005. ];
  8006. $this->assertHtml($expected, $result);
  8007. $result = $this->Form->control('checkbox1', [
  8008. 'label' => 'My checkboxes',
  8009. 'multiple' => 'checkbox',
  8010. 'type' => 'select',
  8011. 'options' => [
  8012. ['text' => 'First Checkbox', 'value' => 1],
  8013. ['text' => 'Second Checkbox', 'value' => 2],
  8014. ],
  8015. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8016. 'value' => ['1'],
  8017. ]);
  8018. $expected = [
  8019. ['div' => ['class' => 'input select']],
  8020. ['label' => ['for' => 'checkbox1']],
  8021. 'My checkboxes',
  8022. '/label',
  8023. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8024. ['div' => ['class' => 'checkbox']],
  8025. ['label' => [
  8026. 'class' => 'custom-class another-class selected',
  8027. 'data-name' => 'bob',
  8028. 'for' => 'checkbox1-1',
  8029. ]],
  8030. ['input' => [
  8031. 'type' => 'checkbox',
  8032. 'name' => 'checkbox1[]',
  8033. 'value' => '1',
  8034. 'id' => 'checkbox1-1',
  8035. 'checked' => 'checked',
  8036. ]],
  8037. 'First Checkbox',
  8038. '/label',
  8039. '/div',
  8040. ['div' => ['class' => 'checkbox']],
  8041. ['label' => [
  8042. 'class' => 'custom-class another-class',
  8043. 'data-name' => 'bob',
  8044. 'for' => 'checkbox1-2',
  8045. ]],
  8046. ['input' => [
  8047. 'type' => 'checkbox',
  8048. 'name' => 'checkbox1[]',
  8049. 'value' => '2',
  8050. 'id' => 'checkbox1-2',
  8051. ]],
  8052. 'Second Checkbox',
  8053. '/label',
  8054. '/div',
  8055. '/div',
  8056. ];
  8057. $this->assertHtml($expected, $result);
  8058. }
  8059. /**
  8060. * testControlMaxLengthArrayContext method
  8061. *
  8062. * Test control() with maxlength attribute in Array Context.
  8063. *
  8064. * @return void
  8065. */
  8066. public function testControlMaxLengthArrayContext()
  8067. {
  8068. $this->article['schema'] = [
  8069. 'title' => ['length' => 10],
  8070. ];
  8071. $this->Form->create($this->article);
  8072. $result = $this->Form->control('title');
  8073. $expected = [
  8074. 'div' => ['class'],
  8075. 'label' => ['for'],
  8076. 'Title',
  8077. '/label',
  8078. 'input' => [
  8079. 'id',
  8080. 'name' => 'title',
  8081. 'type' => 'text',
  8082. 'required' => 'required',
  8083. 'maxlength' => 10,
  8084. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  8085. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8086. ],
  8087. '/div',
  8088. ];
  8089. $this->assertHtml($expected, $result);
  8090. }
  8091. /**
  8092. * testControlMaxLengthEntityContext method
  8093. *
  8094. * Test control() with maxlength attribute in Entity Context.
  8095. *
  8096. * @return void
  8097. */
  8098. public function testControlMaxLengthEntityContext()
  8099. {
  8100. $this->article['schema']['title']['length'] = 45;
  8101. $validator = new Validator();
  8102. $validator->maxLength('title', 10);
  8103. $article = new EntityContext(
  8104. new ServerRequest(),
  8105. [
  8106. 'entity' => new Entity($this->article),
  8107. 'table' => new Table([
  8108. 'alias' => 'Articles',
  8109. 'schema' => $this->article['schema'],
  8110. 'validator' => $validator,
  8111. ]),
  8112. ]
  8113. );
  8114. $this->Form->create($article);
  8115. $result = $this->Form->control('title');
  8116. $expected = [
  8117. 'div' => ['class'],
  8118. 'label' => ['for'],
  8119. 'Title',
  8120. '/label',
  8121. 'input' => [
  8122. 'id',
  8123. 'name' => 'title',
  8124. 'type' => 'text',
  8125. 'required' => 'required',
  8126. 'maxlength' => 10,
  8127. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8128. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8129. ],
  8130. '/div',
  8131. ];
  8132. $this->assertHtml($expected, $result);
  8133. $this->article['schema']['title']['length'] = 45;
  8134. $validator = new Validator();
  8135. $validator->maxLength('title', 55);
  8136. $article = new EntityContext(
  8137. new ServerRequest(),
  8138. [
  8139. 'entity' => new Entity($this->article),
  8140. 'table' => new Table([
  8141. 'schema' => $this->article['schema'],
  8142. 'validator' => $validator,
  8143. 'alias' => 'Articles',
  8144. ]),
  8145. ]
  8146. );
  8147. $this->Form->create($article);
  8148. $result = $this->Form->control('title');
  8149. $expected = [
  8150. 'div' => ['class'],
  8151. 'label' => ['for'],
  8152. 'Title',
  8153. '/label',
  8154. 'input' => [
  8155. 'id',
  8156. 'name' => 'title',
  8157. 'type' => 'text',
  8158. 'required' => 'required',
  8159. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8160. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8161. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8162. ],
  8163. '/div',
  8164. ];
  8165. $this->assertHtml($expected, $result);
  8166. $this->article['schema']['title']['length'] = 45;
  8167. $validator = new Validator();
  8168. $validator->maxLength('title', 55);
  8169. $article = new EntityContext(
  8170. new ServerRequest(),
  8171. [
  8172. 'entity' => new Entity($this->article),
  8173. 'table' => new Table([
  8174. 'schema' => $this->article['schema'],
  8175. 'validator' => $validator,
  8176. 'alias' => 'Articles',
  8177. ]),
  8178. ]
  8179. );
  8180. $this->Form->create($article);
  8181. $result = $this->Form->control('title', ['maxlength' => 10]);
  8182. $expected = [
  8183. 'div' => ['class'],
  8184. 'label' => ['for'],
  8185. 'Title',
  8186. '/label',
  8187. 'input' => [
  8188. 'id',
  8189. 'name' => 'title',
  8190. 'type' => 'text',
  8191. 'required' => 'required',
  8192. 'maxlength' => 10, // Length set in options should take highest precedence.
  8193. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8194. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8195. ],
  8196. '/div',
  8197. ];
  8198. $this->assertHtml($expected, $result);
  8199. }
  8200. /**
  8201. * testControlMinMaxLengthEntityContext method
  8202. *
  8203. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8204. *
  8205. * @return void
  8206. */
  8207. public function testControlMinMaxLengthEntityContext()
  8208. {
  8209. $validator = new Validator();
  8210. $validator->maxLength('title', 10);
  8211. $article = new EntityContext(
  8212. new ServerRequest(),
  8213. [
  8214. 'entity' => new Entity($this->article),
  8215. 'table' => new Table([
  8216. 'alias' => 'Articles',
  8217. 'schema' => $this->article['schema'],
  8218. 'validator' => $validator,
  8219. ]),
  8220. ]
  8221. );
  8222. $this->Form->create($article);
  8223. $result = $this->Form->control('title');
  8224. $expected = [
  8225. 'div' => ['class'],
  8226. 'label' => ['for'],
  8227. 'Title',
  8228. '/label',
  8229. 'input' => [
  8230. 'id',
  8231. 'name' => 'title',
  8232. 'type' => 'text',
  8233. 'required' => 'required',
  8234. 'maxlength' => 10,
  8235. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8236. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8237. ],
  8238. '/div',
  8239. ];
  8240. $this->assertHtml($expected, $result);
  8241. }
  8242. /**
  8243. * testControlMaxLengthFormContext method
  8244. *
  8245. * Test control() with maxlength attribute in Form Context.
  8246. *
  8247. * @return void
  8248. */
  8249. public function testControlMaxLengthFormContext()
  8250. {
  8251. $validator = new Validator();
  8252. $validator->maxLength('title', 10);
  8253. $form = new Form();
  8254. $form->setValidator('default', $validator);
  8255. $this->Form->create($form);
  8256. $result = $this->Form->control('title');
  8257. $expected = [
  8258. 'div' => ['class'],
  8259. 'label' => ['for'],
  8260. 'Title',
  8261. '/label',
  8262. 'input' => [
  8263. 'id',
  8264. 'name' => 'title',
  8265. 'type' => 'text',
  8266. 'required' => 'required',
  8267. 'maxlength' => 10,
  8268. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  8269. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  8270. ],
  8271. '/div',
  8272. ];
  8273. $this->assertHtml($expected, $result);
  8274. }
  8275. }