FormHelperTest.php 308 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->setSchema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->setSchema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::setSalt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @return void
  289. */
  290. public function testAddWidgetInvalid()
  291. {
  292. $this->expectException(\RuntimeException::class);
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @return void
  353. */
  354. public function testAddContextProviderInvalid()
  355. {
  356. $this->expectException(\RuntimeException::class);
  357. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->setTemplates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request = $this->Form->request
  670. ->withRequestTarget('/articles/edit/1')
  671. ->withParam('action', 'edit');
  672. $this->article['defaults']['id'] = 1;
  673. $result = $this->Form->create($this->article);
  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. }
  686. /**
  687. * test create() with automatic url generation
  688. *
  689. * @return void
  690. */
  691. public function testCreateAutoUrl()
  692. {
  693. $encoding = strtolower(Configure::read('App.encoding'));
  694. $this->Form->request = $this->Form->request
  695. ->withRequestTarget('/articles/delete/10')
  696. ->withParam('action', 'delete');
  697. $result = $this->Form->create($this->article);
  698. $expected = [
  699. 'form' => [
  700. 'method' => 'post', 'action' => '/articles/delete/10',
  701. 'accept-charset' => $encoding
  702. ],
  703. 'div' => ['style' => 'display:none;'],
  704. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  705. '/div'
  706. ];
  707. $this->assertHtml($expected, $result);
  708. $this->article['defaults'] = ['id' => 1];
  709. $this->Form->request = $this->Form->request
  710. ->withRequestTarget('/articles/edit/1')
  711. ->withParam('action', 'delete');
  712. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  713. $expected = [
  714. 'form' => [
  715. 'method' => 'post',
  716. 'action' => '/articles/edit/1',
  717. 'accept-charset' => $encoding
  718. ],
  719. 'div' => ['style' => 'display:none;'],
  720. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  721. '/div'
  722. ];
  723. $this->assertHtml($expected, $result);
  724. $this->Form->request = $this->Form->request
  725. ->withParam('action', 'add');
  726. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  727. $expected = [
  728. 'form' => [
  729. 'method' => 'post',
  730. 'action' => '/articles/publish/1',
  731. 'accept-charset' => $encoding
  732. ],
  733. 'div' => ['style' => 'display:none;'],
  734. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  735. '/div'
  736. ];
  737. $this->assertHtml($expected, $result);
  738. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  739. $expected = [
  740. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  741. 'div' => ['style' => 'display:none;'],
  742. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  743. '/div'
  744. ];
  745. $this->assertHtml($expected, $result);
  746. $this->Form->request = $this->Form->request
  747. ->withParam('controller', 'Pages');
  748. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  749. $expected = [
  750. 'form' => [
  751. 'method' => 'post', 'action' => '/Pages/signup/1',
  752. 'accept-charset' => $encoding
  753. ],
  754. 'div' => ['style' => 'display:none;'],
  755. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  756. '/div'
  757. ];
  758. $this->assertHtml($expected, $result);
  759. }
  760. /**
  761. * Test create() with no URL (no "action" attribute for <form> tag)
  762. *
  763. * @return void
  764. */
  765. public function testCreateNoUrl()
  766. {
  767. $result = $this->Form->create(false, ['url' => false]);
  768. $expected = [
  769. 'form' => [
  770. 'method' => 'post',
  771. 'accept-charset' => strtolower(Configure::read('App.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 create() with a custom route
  781. *
  782. * @return void
  783. */
  784. public function testCreateCustomRoute()
  785. {
  786. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  787. $encoding = strtolower(Configure::read('App.encoding'));
  788. $this->Form->request = $this->Form->request
  789. ->withParam('controller', 'users');
  790. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  791. $expected = [
  792. 'form' => [
  793. 'method' => 'post', 'action' => '/login',
  794. 'accept-charset' => $encoding
  795. ],
  796. 'div' => ['style' => 'display:none;'],
  797. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  798. '/div'
  799. ];
  800. $this->assertHtml($expected, $result);
  801. Router::connect(
  802. '/new-article',
  803. ['controller' => 'articles', 'action' => 'myaction'],
  804. ['_name' => 'my-route']
  805. );
  806. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  807. $expected = [
  808. 'form' => [
  809. 'method' => 'post', 'action' => '/new-article',
  810. 'accept-charset' => $encoding,
  811. ],
  812. 'div' => ['style' => 'display:none;'],
  813. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  814. '/div'
  815. ];
  816. $this->assertHtml($expected, $result);
  817. }
  818. /**
  819. * test automatic accept-charset overriding
  820. *
  821. * @return void
  822. */
  823. public function testCreateWithAcceptCharset()
  824. {
  825. $result = $this->Form->create(
  826. $this->article,
  827. [
  828. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  829. ]
  830. );
  831. $expected = [
  832. 'form' => [
  833. 'method' => 'post', 'action' => '/articles',
  834. 'accept-charset' => 'iso-8859-1'
  835. ],
  836. 'div' => ['style' => 'display:none;'],
  837. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  838. '/div'
  839. ];
  840. $this->assertHtml($expected, $result);
  841. }
  842. /**
  843. * Test base form URL when url param is passed with multiple parameters (&)
  844. *
  845. */
  846. public function testCreateQueryStringRequest()
  847. {
  848. $encoding = strtolower(Configure::read('App.encoding'));
  849. $result = $this->Form->create($this->article, [
  850. 'type' => 'post',
  851. 'escape' => false,
  852. 'url' => [
  853. 'controller' => 'controller',
  854. 'action' => 'action',
  855. '?' => ['param1' => 'value1', 'param2' => 'value2']
  856. ]
  857. ]);
  858. $expected = [
  859. 'form' => [
  860. 'method' => 'post',
  861. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  862. 'accept-charset' => $encoding
  863. ],
  864. 'div' => ['style' => 'display:none;'],
  865. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  866. '/div'
  867. ];
  868. $this->assertHtml($expected, $result);
  869. $result = $this->Form->create($this->article, [
  870. 'type' => 'post',
  871. 'url' => [
  872. 'controller' => 'controller',
  873. 'action' => 'action',
  874. '?' => ['param1' => 'value1', 'param2' => 'value2']
  875. ]
  876. ]);
  877. $this->assertHtml($expected, $result);
  878. }
  879. /**
  880. * test that create() doesn't cause errors by multiple id's being in the primary key
  881. * as could happen with multiple select or checkboxes.
  882. *
  883. * @return void
  884. */
  885. public function testCreateWithMultipleIdInData()
  886. {
  887. $encoding = strtolower(Configure::read('App.encoding'));
  888. $this->Form->request = $this->Form->request->withData('Article.id', [1, 2]);
  889. $result = $this->Form->create($this->article);
  890. $expected = [
  891. 'form' => [
  892. 'method' => 'post',
  893. 'action' => '/articles/add',
  894. 'accept-charset' => $encoding
  895. ],
  896. 'div' => ['style' => 'display:none;'],
  897. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  898. '/div'
  899. ];
  900. $this->assertHtml($expected, $result);
  901. }
  902. /**
  903. * test that create() doesn't add in extra passed params.
  904. *
  905. * @return void
  906. */
  907. public function testCreatePassedArgs()
  908. {
  909. $encoding = strtolower(Configure::read('App.encoding'));
  910. $this->Form->request = $this->Form->request->withData('Article.id', 1);
  911. $result = $this->Form->create($this->article, [
  912. 'type' => 'post',
  913. 'escape' => false,
  914. 'url' => [
  915. 'action' => 'edit',
  916. 'myparam'
  917. ]
  918. ]);
  919. $expected = [
  920. 'form' => [
  921. 'method' => 'post',
  922. 'action' => '/articles/edit/myparam',
  923. 'accept-charset' => $encoding
  924. ],
  925. 'div' => ['style' => 'display:none;'],
  926. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  927. '/div'
  928. ];
  929. $this->assertHtml($expected, $result);
  930. }
  931. /**
  932. * test creating a get form, and get form inputs.
  933. *
  934. * @return void
  935. */
  936. public function testGetFormCreate()
  937. {
  938. $encoding = strtolower(Configure::read('App.encoding'));
  939. $result = $this->Form->create($this->article, ['type' => 'get']);
  940. $expected = ['form' => [
  941. 'method' => 'get', 'action' => '/articles/add',
  942. 'accept-charset' => $encoding
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->text('title');
  946. $expected = ['input' => [
  947. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $result = $this->Form->password('password');
  951. $expected = ['input' => [
  952. 'name' => 'password', 'type' => 'password'
  953. ]];
  954. $this->assertHtml($expected, $result);
  955. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  956. $result = $this->Form->text('user_form');
  957. $expected = ['input' => [
  958. 'name' => 'user_form', 'type' => 'text'
  959. ]];
  960. $this->assertHtml($expected, $result);
  961. }
  962. /**
  963. * test get form, and inputs when the model param is false
  964. *
  965. * @return void
  966. */
  967. public function testGetFormWithFalseModel()
  968. {
  969. $encoding = strtolower(Configure::read('App.encoding'));
  970. $this->Form->request = $this->Form->request->withParam('controller', 'contact_test');
  971. $result = $this->Form->create(false, [
  972. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  973. ]);
  974. $expected = ['form' => [
  975. 'method' => 'get', 'action' => '/contact_test/add',
  976. 'accept-charset' => $encoding
  977. ]];
  978. $this->assertHtml($expected, $result);
  979. $result = $this->Form->text('reason');
  980. $expected = [
  981. 'input' => ['type' => 'text', 'name' => 'reason']
  982. ];
  983. $this->assertHtml($expected, $result);
  984. }
  985. /**
  986. * testFormCreateWithSecurity method
  987. *
  988. * Test form->create() with security key.
  989. *
  990. * @return void
  991. */
  992. public function testCreateWithSecurity()
  993. {
  994. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  995. $encoding = strtolower(Configure::read('App.encoding'));
  996. $result = $this->Form->create($this->article, [
  997. 'url' => '/articles/publish',
  998. ]);
  999. $expected = [
  1000. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1001. 'div' => ['style' => 'display:none;'],
  1002. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1003. ['input' => [
  1004. 'type' => 'hidden',
  1005. 'name' => '_csrfToken',
  1006. 'value' => 'testKey',
  1007. 'autocomplete' => 'off',
  1008. ]],
  1009. '/div'
  1010. ];
  1011. $this->assertHtml($expected, $result);
  1012. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1013. $expected['form']['id'] = 'MyForm';
  1014. $this->assertHtml($expected, $result);
  1015. }
  1016. /**
  1017. * testFormCreateGetNoSecurity method
  1018. *
  1019. * Test form->create() with no security key as its a get form
  1020. *
  1021. * @return void
  1022. */
  1023. public function testCreateEndGetNoSecurity()
  1024. {
  1025. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1026. $article = new Article();
  1027. $result = $this->Form->create($article, [
  1028. 'type' => 'get',
  1029. 'url' => '/contacts/add'
  1030. ]);
  1031. $this->assertNotContains('testKey', $result);
  1032. $result = $this->Form->end();
  1033. $this->assertNotContains('testKey', $result);
  1034. }
  1035. /**
  1036. * test that create() clears the fields property so it starts fresh
  1037. *
  1038. * @return void
  1039. */
  1040. public function testCreateClearingFields()
  1041. {
  1042. $this->Form->fields = ['model_id'];
  1043. $this->Form->create($this->article);
  1044. $this->assertEquals([], $this->Form->fields);
  1045. }
  1046. /**
  1047. * Tests form hash generation with model-less data
  1048. *
  1049. * @return void
  1050. */
  1051. public function testValidateHashNoModel()
  1052. {
  1053. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1054. $fields = ['anything'];
  1055. $result = $this->Form->secure($fields);
  1056. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1057. $this->assertContains($hash, $result);
  1058. }
  1059. /**
  1060. * Tests that hidden fields generated for checkboxes don't get locked
  1061. *
  1062. * @return void
  1063. */
  1064. public function testNoCheckboxLocking()
  1065. {
  1066. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1067. $this->assertSame([], $this->Form->fields);
  1068. $this->Form->checkbox('check', ['value' => '1']);
  1069. $this->assertSame($this->Form->fields, ['check']);
  1070. }
  1071. /**
  1072. * testFormSecurityFields method
  1073. *
  1074. * Test generation of secure form hash generation.
  1075. *
  1076. * @return void
  1077. */
  1078. public function testFormSecurityFields()
  1079. {
  1080. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1081. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1082. $result = $this->Form->secure($fields);
  1083. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1084. $hash .= ':' . 'Model.valid';
  1085. $hash = urlencode($hash);
  1086. $tokenDebug = urlencode(json_encode([
  1087. '',
  1088. $fields,
  1089. []
  1090. ]));
  1091. $expected = [
  1092. 'div' => ['style' => 'display:none;'],
  1093. ['input' => [
  1094. 'type' => 'hidden',
  1095. 'name' => '_Token[fields]',
  1096. 'value' => $hash,
  1097. 'autocomplete' => 'off',
  1098. ]],
  1099. ['input' => [
  1100. 'type' => 'hidden',
  1101. 'name' => '_Token[unlocked]',
  1102. 'value' => '',
  1103. 'autocomplete' => 'off',
  1104. ]],
  1105. ['input' => [
  1106. 'type' => 'hidden',
  1107. 'name' => '_Token[debug]',
  1108. 'value' => $tokenDebug,
  1109. 'autocomplete' => 'off',
  1110. ]],
  1111. '/div'
  1112. ];
  1113. $this->assertHtml($expected, $result);
  1114. }
  1115. /**
  1116. * testFormSecurityFields method
  1117. *
  1118. * Test debug token is not generated if debug is false
  1119. *
  1120. * @return void
  1121. */
  1122. public function testFormSecurityFieldsNoDebugMode()
  1123. {
  1124. Configure::write('debug', false);
  1125. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1126. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1127. $result = $this->Form->secure($fields);
  1128. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1129. $hash .= ':' . 'Model.valid';
  1130. $hash = urlencode($hash);
  1131. $expected = [
  1132. 'div' => ['style' => 'display:none;'],
  1133. ['input' => [
  1134. 'type' => 'hidden',
  1135. 'name' => '_Token[fields]',
  1136. 'autocomplete' => 'off',
  1137. 'value' => $hash
  1138. ]],
  1139. ['input' => [
  1140. 'type' => 'hidden',
  1141. 'name' => '_Token[unlocked]',
  1142. 'autocomplete' => 'off',
  1143. 'value' => '',
  1144. ]],
  1145. '/div'
  1146. ];
  1147. $this->assertHtml($expected, $result);
  1148. }
  1149. /**
  1150. * Tests correct generation of number fields for smallint
  1151. *
  1152. * @return void
  1153. */
  1154. public function testTextFieldGenerationForSmallint()
  1155. {
  1156. $this->article['schema'] = [
  1157. 'foo' => [
  1158. 'type' => 'smallinteger',
  1159. 'null' => false,
  1160. 'default' => null,
  1161. 'length' => 10
  1162. ]
  1163. ];
  1164. $this->Form->create($this->article);
  1165. $result = $this->Form->control('foo');
  1166. $this->assertContains('class="input number"', $result);
  1167. $this->assertContains('type="number"', $result);
  1168. }
  1169. /**
  1170. * Tests correct generation of number fields for tinyint
  1171. *
  1172. * @return void
  1173. */
  1174. public function testTextFieldGenerationForTinyint()
  1175. {
  1176. $this->article['schema'] = [
  1177. 'foo' => [
  1178. 'type' => 'tinyinteger',
  1179. 'null' => false,
  1180. 'default' => null,
  1181. 'length' => 10
  1182. ]
  1183. ];
  1184. $this->Form->create($this->article);
  1185. $result = $this->Form->control('foo');
  1186. $this->assertContains('class="input number"', $result);
  1187. $this->assertContains('type="number"', $result);
  1188. }
  1189. /**
  1190. * Tests correct generation of number fields for double and float fields
  1191. *
  1192. * @return void
  1193. */
  1194. public function testTextFieldGenerationForFloats()
  1195. {
  1196. $this->article['schema'] = [
  1197. 'foo' => [
  1198. 'type' => 'float',
  1199. 'null' => false,
  1200. 'default' => null,
  1201. 'length' => 10
  1202. ]
  1203. ];
  1204. $this->Form->create($this->article);
  1205. $result = $this->Form->control('foo');
  1206. $expected = [
  1207. 'div' => ['class' => 'input number'],
  1208. 'label' => ['for' => 'foo'],
  1209. 'Foo',
  1210. '/label',
  1211. ['input' => [
  1212. 'type' => 'number',
  1213. 'name' => 'foo',
  1214. 'id' => 'foo',
  1215. 'step' => 'any'
  1216. ]],
  1217. '/div'
  1218. ];
  1219. $this->assertHtml($expected, $result);
  1220. $result = $this->Form->control('foo', ['step' => 0.5]);
  1221. $expected = [
  1222. 'div' => ['class' => 'input number'],
  1223. 'label' => ['for' => 'foo'],
  1224. 'Foo',
  1225. '/label',
  1226. ['input' => [
  1227. 'type' => 'number',
  1228. 'name' => 'foo',
  1229. 'id' => 'foo',
  1230. 'step' => '0.5'
  1231. ]],
  1232. '/div'
  1233. ];
  1234. $this->assertHtml($expected, $result);
  1235. }
  1236. /**
  1237. * Tests correct generation of number fields for integer fields
  1238. *
  1239. * @return void
  1240. */
  1241. public function testTextFieldTypeNumberGenerationForIntegers()
  1242. {
  1243. TableRegistry::get('Contacts', [
  1244. 'className' => __NAMESPACE__ . '\ContactsTable'
  1245. ]);
  1246. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1247. $result = $this->Form->control('age');
  1248. $expected = [
  1249. 'div' => ['class' => 'input number'],
  1250. 'label' => ['for' => 'age'],
  1251. 'Age',
  1252. '/label',
  1253. ['input' => [
  1254. 'type' => 'number', 'name' => 'age',
  1255. 'id' => 'age'
  1256. ]],
  1257. '/div'
  1258. ];
  1259. $this->assertHtml($expected, $result);
  1260. }
  1261. /**
  1262. * Tests correct generation of file upload fields for binary fields
  1263. *
  1264. * @return void
  1265. */
  1266. public function testFileUploadFieldTypeGenerationForBinaries()
  1267. {
  1268. $table = TableRegistry::get('Contacts', [
  1269. 'className' => __NAMESPACE__ . '\ContactsTable'
  1270. ]);
  1271. $table->setSchema(['foo' => [
  1272. 'type' => 'binary',
  1273. 'null' => false,
  1274. 'default' => null,
  1275. 'length' => 1024
  1276. ]]);
  1277. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1278. $result = $this->Form->control('foo');
  1279. $expected = [
  1280. 'div' => ['class' => 'input file'],
  1281. 'label' => ['for' => 'foo'],
  1282. 'Foo',
  1283. '/label',
  1284. ['input' => [
  1285. 'type' => 'file', 'name' => 'foo',
  1286. 'id' => 'foo'
  1287. ]],
  1288. '/div'
  1289. ];
  1290. $this->assertHtml($expected, $result);
  1291. }
  1292. /**
  1293. * testFormSecurityMultipleFields method
  1294. *
  1295. * Test secure() with multiple row form. Ensure hash is correct.
  1296. *
  1297. * @return void
  1298. */
  1299. public function testFormSecurityMultipleFields()
  1300. {
  1301. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1302. $fields = [
  1303. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1304. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1305. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1306. ];
  1307. $result = $this->Form->secure($fields);
  1308. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1309. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1310. $tokenDebug = urlencode(json_encode([
  1311. '',
  1312. $fields,
  1313. []
  1314. ]));
  1315. $expected = [
  1316. 'div' => ['style' => 'display:none;'],
  1317. ['input' => [
  1318. 'type' => 'hidden',
  1319. 'name' => '_Token[fields]',
  1320. 'value' => $hash,
  1321. 'autocomplete' => 'off',
  1322. ]],
  1323. ['input' => [
  1324. 'type' => 'hidden',
  1325. 'name' => '_Token[unlocked]',
  1326. 'autocomplete' => 'off',
  1327. 'value' => ''
  1328. ]],
  1329. ['input' => [
  1330. 'type' => 'hidden',
  1331. 'name' => '_Token[debug]',
  1332. 'value' => $tokenDebug,
  1333. 'autocomplete' => 'off',
  1334. ]],
  1335. '/div'
  1336. ];
  1337. $this->assertHtml($expected, $result);
  1338. }
  1339. /**
  1340. * testFormSecurityMultipleSubmitButtons
  1341. *
  1342. * test form submit generation and ensure that _Token is only created on end()
  1343. *
  1344. * @return void
  1345. */
  1346. public function testFormSecurityMultipleSubmitButtons()
  1347. {
  1348. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1349. $this->Form->create($this->article);
  1350. $this->Form->text('Address.title');
  1351. $this->Form->text('Address.first_name');
  1352. $result = $this->Form->submit('Save', ['name' => 'save']);
  1353. $expected = [
  1354. 'div' => ['class' => 'submit'],
  1355. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1356. '/div',
  1357. ];
  1358. $this->assertHtml($expected, $result);
  1359. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1360. $expected = [
  1361. 'div' => ['class' => 'submit'],
  1362. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1363. '/div',
  1364. ];
  1365. $this->assertHtml($expected, $result);
  1366. $result = $this->Form->end();
  1367. $tokenDebug = urlencode(json_encode([
  1368. '/articles/add',
  1369. [
  1370. 'Address.title',
  1371. 'Address.first_name',
  1372. ],
  1373. ['save', 'cancel']
  1374. ]));
  1375. $expected = [
  1376. 'div' => ['style' => 'display:none;'],
  1377. ['input' => [
  1378. 'type' => 'hidden',
  1379. 'name' => '_Token[fields]',
  1380. 'autocomplete',
  1381. 'value'
  1382. ]],
  1383. ['input' => [
  1384. 'type' => 'hidden',
  1385. 'name' => '_Token[unlocked]',
  1386. 'value' => 'cancel%7Csave',
  1387. 'autocomplete' => 'off',
  1388. ]],
  1389. ['input' => [
  1390. 'type' => 'hidden',
  1391. 'name' => '_Token[debug]',
  1392. 'value' => $tokenDebug,
  1393. 'autocomplete' => 'off',
  1394. ]],
  1395. '/div'
  1396. ];
  1397. $this->assertHtml($expected, $result);
  1398. }
  1399. /**
  1400. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1401. *
  1402. * @return void
  1403. */
  1404. public function testSecurityButtonNestedNamed()
  1405. {
  1406. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1407. $this->Form->create('Addresses');
  1408. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1409. $result = $this->Form->unlockField();
  1410. $this->assertEquals(['Address.button'], $result);
  1411. }
  1412. /**
  1413. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1414. *
  1415. * @return void
  1416. */
  1417. public function testSecuritySubmitNestedNamed()
  1418. {
  1419. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1420. $this->Form->create($this->article);
  1421. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1422. $result = $this->Form->unlockField();
  1423. $this->assertEquals(['Address.button'], $result);
  1424. }
  1425. /**
  1426. * Test that the correct fields are unlocked for image submits with no names.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testSecuritySubmitImageNoName()
  1431. {
  1432. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1433. $this->Form->create(false);
  1434. $result = $this->Form->submit('save.png');
  1435. $expected = [
  1436. 'div' => ['class' => 'submit'],
  1437. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1438. '/div'
  1439. ];
  1440. $this->assertHtml($expected, $result);
  1441. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1442. }
  1443. /**
  1444. * Test that the correct fields are unlocked for image submits with names.
  1445. *
  1446. * @return void
  1447. */
  1448. public function testSecuritySubmitImageName()
  1449. {
  1450. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1451. $this->Form->create(null);
  1452. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1453. $expected = [
  1454. 'div' => ['class' => 'submit'],
  1455. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1456. '/div'
  1457. ];
  1458. $this->assertHtml($expected, $result);
  1459. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1460. }
  1461. /**
  1462. * testFormSecurityMultipleControlFields method
  1463. *
  1464. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1465. *
  1466. * @return void
  1467. */
  1468. public function testFormSecurityMultipleControlFields()
  1469. {
  1470. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1471. $this->Form->create();
  1472. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1473. $this->Form->control('Addresses.0.title');
  1474. $this->Form->control('Addresses.0.first_name');
  1475. $this->Form->control('Addresses.0.last_name');
  1476. $this->Form->control('Addresses.0.address');
  1477. $this->Form->control('Addresses.0.city');
  1478. $this->Form->control('Addresses.0.phone');
  1479. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1480. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1481. $this->Form->control('Addresses.1.title');
  1482. $this->Form->control('Addresses.1.first_name');
  1483. $this->Form->control('Addresses.1.last_name');
  1484. $this->Form->control('Addresses.1.address');
  1485. $this->Form->control('Addresses.1.city');
  1486. $this->Form->control('Addresses.1.phone');
  1487. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1488. $result = $this->Form->secure($this->Form->fields);
  1489. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1490. $tokenDebug = urlencode(json_encode([
  1491. '/articles/add',
  1492. [
  1493. 'Addresses.0.id' => '123456',
  1494. 'Addresses.0.title',
  1495. 'Addresses.0.first_name',
  1496. 'Addresses.0.last_name',
  1497. 'Addresses.0.address',
  1498. 'Addresses.0.city',
  1499. 'Addresses.0.phone',
  1500. 'Addresses.0.primary',
  1501. 'Addresses.1.id' => '654321',
  1502. 'Addresses.1.title',
  1503. 'Addresses.1.first_name',
  1504. 'Addresses.1.last_name',
  1505. 'Addresses.1.address',
  1506. 'Addresses.1.city',
  1507. 'Addresses.1.phone',
  1508. 'Addresses.1.primary',
  1509. ],
  1510. []
  1511. ]));
  1512. $expected = [
  1513. 'div' => ['style' => 'display:none;'],
  1514. ['input' => [
  1515. 'type' => 'hidden',
  1516. 'name' => '_Token[fields]',
  1517. 'value' => $hash,
  1518. 'autocomplete' => 'off',
  1519. ]],
  1520. ['input' => [
  1521. 'type' => 'hidden',
  1522. 'name' => '_Token[unlocked]',
  1523. 'autocomplete' => 'off',
  1524. 'value' => ''
  1525. ]],
  1526. ['input' => [
  1527. 'type' => 'hidden',
  1528. 'name' => '_Token[debug]',
  1529. 'value' => $tokenDebug,
  1530. 'autocomplete' => 'off',
  1531. ]],
  1532. '/div'
  1533. ];
  1534. $this->assertHtml($expected, $result);
  1535. }
  1536. /**
  1537. * testFormSecurityArrayFields method
  1538. *
  1539. * Test form security with Model.field.0 style inputs.
  1540. *
  1541. * @return void
  1542. */
  1543. public function testFormSecurityArrayFields()
  1544. {
  1545. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1546. $this->Form->create();
  1547. $this->Form->text('Address.primary.1');
  1548. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1549. $this->Form->text('Address.secondary.1.0');
  1550. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1551. }
  1552. /**
  1553. * testFormSecurityMultipleControlDisabledFields method
  1554. *
  1555. * Test secure form generation with multiple records and disabled fields.
  1556. *
  1557. * @return void
  1558. */
  1559. public function testFormSecurityMultipleControlDisabledFields()
  1560. {
  1561. $this->Form->request = $this->Form->request->withParam('_Token', [
  1562. 'unlockedFields' => ['first_name', 'address']
  1563. ]);
  1564. $this->Form->create();
  1565. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1566. $this->Form->text('Addresses.0.title');
  1567. $this->Form->text('Addresses.0.first_name');
  1568. $this->Form->text('Addresses.0.last_name');
  1569. $this->Form->text('Addresses.0.address');
  1570. $this->Form->text('Addresses.0.city');
  1571. $this->Form->text('Addresses.0.phone');
  1572. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1573. $this->Form->text('Addresses.1.title');
  1574. $this->Form->text('Addresses.1.first_name');
  1575. $this->Form->text('Addresses.1.last_name');
  1576. $this->Form->text('Addresses.1.address');
  1577. $this->Form->text('Addresses.1.city');
  1578. $this->Form->text('Addresses.1.phone');
  1579. $result = $this->Form->secure($this->Form->fields);
  1580. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1581. $tokenDebug = urlencode(json_encode([
  1582. '/articles/add',
  1583. [
  1584. 'Addresses.0.id' => '123456',
  1585. 'Addresses.0.title',
  1586. 'Addresses.0.last_name',
  1587. 'Addresses.0.city',
  1588. 'Addresses.0.phone',
  1589. 'Addresses.1.id' => '654321',
  1590. 'Addresses.1.title',
  1591. 'Addresses.1.last_name',
  1592. 'Addresses.1.city',
  1593. 'Addresses.1.phone'
  1594. ],
  1595. [
  1596. 'first_name',
  1597. 'address'
  1598. ]
  1599. ]));
  1600. $expected = [
  1601. 'div' => ['style' => 'display:none;'],
  1602. ['input' => [
  1603. 'type' => 'hidden',
  1604. 'name' => '_Token[fields]',
  1605. 'autocomplete' => 'off',
  1606. 'value' => $hash
  1607. ]],
  1608. ['input' => [
  1609. 'type' => 'hidden',
  1610. 'name' => '_Token[unlocked]',
  1611. 'autocomplete' => 'off',
  1612. 'value' => 'address%7Cfirst_name',
  1613. ]],
  1614. ['input' => [
  1615. 'type' => 'hidden',
  1616. 'name' => '_Token[debug]',
  1617. 'autocomplete' => 'off',
  1618. 'value' => $tokenDebug,
  1619. ]],
  1620. '/div'
  1621. ];
  1622. $this->assertHtml($expected, $result);
  1623. }
  1624. /**
  1625. * testFormSecurityControlDisabledFields method
  1626. *
  1627. * Test single record form with disabled fields.
  1628. *
  1629. * @return void
  1630. */
  1631. public function testFormSecurityControlUnlockedFields()
  1632. {
  1633. $this->Form->request = $this->Form->request->withParam('_Token', [
  1634. 'unlockedFields' => ['first_name', 'address']
  1635. ]);
  1636. $this->Form->create();
  1637. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1638. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1639. $this->Form->text('Addresses.title');
  1640. $this->Form->text('Addresses.first_name');
  1641. $this->Form->text('Addresses.last_name');
  1642. $this->Form->text('Addresses.address');
  1643. $this->Form->text('Addresses.city');
  1644. $this->Form->text('Addresses.phone');
  1645. $result = $this->Form->fields;
  1646. $expected = [
  1647. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1648. 'Addresses.city', 'Addresses.phone'
  1649. ];
  1650. $this->assertEquals($expected, $result);
  1651. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1652. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1653. $tokenDebug = urlencode(json_encode([
  1654. '/articles/add',
  1655. [
  1656. 'Addresses.id' => '123456',
  1657. 'Addresses.title',
  1658. 'Addresses.last_name',
  1659. 'Addresses.city',
  1660. 'Addresses.phone'
  1661. ],
  1662. [
  1663. 'first_name',
  1664. 'address'
  1665. ]
  1666. ]));
  1667. $expected = [
  1668. 'div' => ['style' => 'display:none;'],
  1669. ['input' => [
  1670. 'type' => 'hidden',
  1671. 'name' => '_Token[fields]',
  1672. 'value' => $hash,
  1673. 'autocomplete' => 'off',
  1674. 'data-foo' => 'bar',
  1675. ]],
  1676. ['input' => [
  1677. 'type' => 'hidden',
  1678. 'name' => '_Token[unlocked]',
  1679. 'value' => 'address%7Cfirst_name',
  1680. 'autocomplete' => 'off',
  1681. 'data-foo' => 'bar',
  1682. ]],
  1683. ['input' => [
  1684. 'type' => 'hidden', 'name' => '_Token[debug]',
  1685. 'value' => $tokenDebug,
  1686. 'autocomplete' => 'off',
  1687. 'data-foo' => 'bar'
  1688. ]],
  1689. '/div'
  1690. ];
  1691. $this->assertHtml($expected, $result);
  1692. }
  1693. /**
  1694. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1695. *
  1696. * Test single record form with debugSecurity param.
  1697. *
  1698. * @return void
  1699. */
  1700. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1701. {
  1702. $this->Form->request = $this->Form->request->withParam('_Token', [
  1703. 'unlockedFields' => ['first_name', 'address']
  1704. ]);
  1705. $this->Form->create();
  1706. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1707. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1708. $this->Form->text('Addresses.title');
  1709. $this->Form->text('Addresses.first_name');
  1710. $this->Form->text('Addresses.last_name');
  1711. $this->Form->text('Addresses.address');
  1712. $this->Form->text('Addresses.city');
  1713. $this->Form->text('Addresses.phone');
  1714. $result = $this->Form->fields;
  1715. $expected = [
  1716. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1717. 'Addresses.city', 'Addresses.phone'
  1718. ];
  1719. $this->assertEquals($expected, $result);
  1720. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1721. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1722. $tokenDebug = urlencode(json_encode([
  1723. '/articles/add',
  1724. [
  1725. 'Addresses.id' => '123456',
  1726. 'Addresses.title',
  1727. 'Addresses.last_name',
  1728. 'Addresses.city',
  1729. 'Addresses.phone'
  1730. ],
  1731. [
  1732. 'first_name',
  1733. 'address'
  1734. ]
  1735. ]));
  1736. $expected = [
  1737. 'div' => ['style' => 'display:none;'],
  1738. ['input' => [
  1739. 'type' => 'hidden',
  1740. 'name' => '_Token[fields]',
  1741. 'value' => $hash,
  1742. 'autocomplete' => 'off',
  1743. 'data-foo' => 'bar',
  1744. ]],
  1745. ['input' => [
  1746. 'type' => 'hidden',
  1747. 'name' => '_Token[unlocked]',
  1748. 'value' => 'address%7Cfirst_name',
  1749. 'autocomplete' => 'off',
  1750. 'data-foo' => 'bar',
  1751. ]],
  1752. ['input' => [
  1753. 'type' => 'hidden', 'name' => '_Token[debug]',
  1754. 'value' => $tokenDebug,
  1755. 'autocomplete' => 'off',
  1756. 'data-foo' => 'bar'
  1757. ]],
  1758. '/div'
  1759. ];
  1760. $this->assertHtml($expected, $result);
  1761. }
  1762. /**
  1763. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1764. *
  1765. * Debug is false, debugSecurity is true -> no debug
  1766. *
  1767. * @return void
  1768. */
  1769. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1770. {
  1771. $this->Form->request = $this->Form->request->withParam('_Token', [
  1772. 'unlockedFields' => ['first_name', 'address']
  1773. ]);
  1774. $this->Form->create();
  1775. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1776. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1777. $this->Form->text('Addresses.title');
  1778. $this->Form->text('Addresses.first_name');
  1779. $this->Form->text('Addresses.last_name');
  1780. $this->Form->text('Addresses.address');
  1781. $this->Form->text('Addresses.city');
  1782. $this->Form->text('Addresses.phone');
  1783. $result = $this->Form->fields;
  1784. $expected = [
  1785. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1786. 'Addresses.city', 'Addresses.phone'
  1787. ];
  1788. $this->assertEquals($expected, $result);
  1789. Configure::write('debug', false);
  1790. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1791. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1792. $expected = [
  1793. 'div' => ['style' => 'display:none;'],
  1794. ['input' => [
  1795. 'type' => 'hidden',
  1796. 'name' => '_Token[fields]',
  1797. 'value' => $hash,
  1798. 'autocomplete' => 'off',
  1799. 'data-foo' => 'bar',
  1800. ]],
  1801. ['input' => [
  1802. 'type' => 'hidden',
  1803. 'name' => '_Token[unlocked]',
  1804. 'value' => 'address%7Cfirst_name',
  1805. 'autocomplete' => 'off',
  1806. 'data-foo' => 'bar',
  1807. ]],
  1808. '/div'
  1809. ];
  1810. $this->assertHtml($expected, $result);
  1811. }
  1812. /**
  1813. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1814. *
  1815. * Test single record form with debugSecurity param.
  1816. *
  1817. * @return void
  1818. */
  1819. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1820. {
  1821. $this->Form->request = $this->Form->request->withParam('_Token', [
  1822. 'unlockedFields' => ['first_name', 'address']
  1823. ]);
  1824. $this->Form->create();
  1825. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1826. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1827. $this->Form->text('Addresses.title');
  1828. $this->Form->text('Addresses.first_name');
  1829. $this->Form->text('Addresses.last_name');
  1830. $this->Form->text('Addresses.address');
  1831. $this->Form->text('Addresses.city');
  1832. $this->Form->text('Addresses.phone');
  1833. $result = $this->Form->fields;
  1834. $expected = [
  1835. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1836. 'Addresses.city', 'Addresses.phone'
  1837. ];
  1838. $this->assertEquals($expected, $result);
  1839. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1840. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1841. $expected = [
  1842. 'div' => ['style' => 'display:none;'],
  1843. ['input' => [
  1844. 'type' => 'hidden',
  1845. 'name' => '_Token[fields]',
  1846. 'value' => $hash,
  1847. 'autocomplete' => 'off',
  1848. 'data-foo' => 'bar',
  1849. ]],
  1850. ['input' => [
  1851. 'type' => 'hidden',
  1852. 'name' => '_Token[unlocked]',
  1853. 'value' => 'address%7Cfirst_name',
  1854. 'autocomplete' => 'off',
  1855. 'data-foo' => 'bar',
  1856. ]],
  1857. '/div'
  1858. ];
  1859. $this->assertHtml($expected, $result);
  1860. }
  1861. /**
  1862. * testFormSecureWithCustomNameAttribute method
  1863. *
  1864. * Test securing inputs with custom name attributes.
  1865. *
  1866. * @return void
  1867. */
  1868. public function testFormSecureWithCustomNameAttribute()
  1869. {
  1870. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1871. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1872. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1873. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1874. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1875. }
  1876. /**
  1877. * testFormSecuredControl method
  1878. *
  1879. * Test generation of entire secure form, assertions made on control() output.
  1880. *
  1881. * @return void
  1882. */
  1883. public function testFormSecuredControl()
  1884. {
  1885. $this->Form->request = $this->Form->request
  1886. ->withParam('_Token', 'stuff')
  1887. ->withParam('_csrfToken', 'testKey');
  1888. $this->article['schema'] = [
  1889. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1890. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1891. ];
  1892. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1893. $encoding = strtolower(Configure::read('App.encoding'));
  1894. $expected = [
  1895. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1896. 'div' => ['style' => 'display:none;'],
  1897. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1898. ['input' => [
  1899. 'type' => 'hidden',
  1900. 'name' => '_csrfToken',
  1901. 'value' => 'testKey',
  1902. 'autocomplete' => 'off'
  1903. ]],
  1904. '/div'
  1905. ];
  1906. $this->assertHtml($expected, $result);
  1907. $result = $this->Form->control('ratio');
  1908. $expected = [
  1909. 'div' => ['class'],
  1910. 'label' => ['for'],
  1911. 'Ratio',
  1912. '/label',
  1913. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1914. '/div',
  1915. ];
  1916. $this->assertHtml($expected, $result);
  1917. $result = $this->Form->control('population');
  1918. $expected = [
  1919. 'div' => ['class'],
  1920. 'label' => ['for'],
  1921. 'Population',
  1922. '/label',
  1923. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1924. '/div',
  1925. ];
  1926. $this->assertHtml($expected, $result);
  1927. $result = $this->Form->control('published', ['type' => 'text']);
  1928. $expected = [
  1929. 'div' => ['class' => 'input text'],
  1930. 'label' => ['for' => 'published'],
  1931. 'Published',
  1932. '/label',
  1933. ['input' => [
  1934. 'type' => 'text',
  1935. 'name' => 'published',
  1936. 'id' => 'published'
  1937. ]],
  1938. '/div'
  1939. ];
  1940. $this->assertHtml($expected, $result);
  1941. $result = $this->Form->control('other', ['type' => 'text']);
  1942. $expected = [
  1943. 'div' => ['class' => 'input text'],
  1944. 'label' => ['for' => 'other'],
  1945. 'Other',
  1946. '/label',
  1947. ['input' => [
  1948. 'type' => 'text',
  1949. 'name' => 'other',
  1950. 'id',
  1951. ]],
  1952. '/div'
  1953. ];
  1954. $this->assertHtml($expected, $result);
  1955. $result = $this->Form->hidden('stuff');
  1956. $expected = [
  1957. 'input' => [
  1958. 'type' => 'hidden',
  1959. 'name' => 'stuff'
  1960. ]
  1961. ];
  1962. $this->assertHtml($expected, $result);
  1963. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1964. $expected = ['input' => [
  1965. 'type' => 'hidden',
  1966. 'name' => 'hidden',
  1967. 'value' => '0'
  1968. ]];
  1969. $this->assertHtml($expected, $result);
  1970. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1971. $expected = [
  1972. 'div' => ['class' => 'input checkbox'],
  1973. ['input' => [
  1974. 'type' => 'hidden',
  1975. 'name' => 'something',
  1976. 'value' => '0'
  1977. ]],
  1978. 'label' => ['for' => 'something'],
  1979. ['input' => [
  1980. 'type' => 'checkbox',
  1981. 'name' => 'something',
  1982. 'value' => '1',
  1983. 'id' => 'something'
  1984. ]],
  1985. 'Something',
  1986. '/label',
  1987. '/div'
  1988. ];
  1989. $this->assertHtml($expected, $result);
  1990. $result = $this->Form->fields;
  1991. $expectedFields = [
  1992. 'ratio',
  1993. 'population',
  1994. 'published',
  1995. 'other',
  1996. 'stuff' => '',
  1997. 'hidden' => '0',
  1998. 'something'
  1999. ];
  2000. $this->assertEquals($expectedFields, $result);
  2001. $result = $this->Form->secure($this->Form->fields);
  2002. $tokenDebug = urlencode(json_encode([
  2003. '/articles/add',
  2004. $expectedFields,
  2005. []
  2006. ]));
  2007. $expected = [
  2008. 'div' => ['style' => 'display:none;'],
  2009. ['input' => [
  2010. 'type' => 'hidden',
  2011. 'name' => '_Token[fields]',
  2012. 'value',
  2013. 'autocomplete'
  2014. ]],
  2015. ['input' => [
  2016. 'type' => 'hidden',
  2017. 'name' => '_Token[unlocked]',
  2018. 'value' => '',
  2019. 'autocomplete' => 'off'
  2020. ]],
  2021. ['input' => [
  2022. 'type' => 'hidden', 'name' => '_Token[debug]',
  2023. 'value' => $tokenDebug,
  2024. 'autocomplete' => 'off'
  2025. ]],
  2026. '/div'
  2027. ];
  2028. $this->assertHtml($expected, $result);
  2029. }
  2030. /**
  2031. * testSecuredControlCustomName method
  2032. *
  2033. * Test secured inputs with custom names.
  2034. *
  2035. * @return void
  2036. */
  2037. public function testSecuredControlCustomName()
  2038. {
  2039. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2040. $this->assertEquals([], $this->Form->fields);
  2041. $this->Form->text('text_input', [
  2042. 'name' => 'Option[General.default_role]',
  2043. ]);
  2044. $expected = ['Option.General.default_role'];
  2045. $this->assertEquals($expected, $this->Form->fields);
  2046. $this->Form->select('select_box', [1, 2], [
  2047. 'name' => 'Option[General.select_role]',
  2048. ]);
  2049. $expected[] = 'Option.General.select_role';
  2050. $this->assertEquals($expected, $this->Form->fields);
  2051. $this->Form->text('other.things[]');
  2052. $expected[] = 'other.things';
  2053. $this->assertEquals($expected, $this->Form->fields);
  2054. }
  2055. /**
  2056. * testSecuredControlDuplicate method
  2057. *
  2058. * Test that a hidden field followed by a visible field
  2059. * undoes the hidden field locking.
  2060. *
  2061. * @return void
  2062. */
  2063. public function testSecuredControlDuplicate()
  2064. {
  2065. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2066. $this->assertEquals([], $this->Form->fields);
  2067. $this->Form->control('text_val', [
  2068. 'type' => 'hidden',
  2069. 'value' => 'some text',
  2070. ]);
  2071. $expected = ['text_val' => 'some text'];
  2072. $this->assertEquals($expected, $this->Form->fields);
  2073. $this->Form->control('text_val', [
  2074. 'type' => 'text',
  2075. ]);
  2076. $expected = ['text_val'];
  2077. $this->assertEquals($expected, $this->Form->fields);
  2078. }
  2079. /**
  2080. * testFormSecuredFileControl method
  2081. *
  2082. * Tests that the correct keys are added to the field hash index.
  2083. *
  2084. * @return void
  2085. */
  2086. public function testFormSecuredFileControl()
  2087. {
  2088. $this->assertEquals([], $this->Form->fields);
  2089. $this->Form->file('Attachment.file');
  2090. $expected = [
  2091. 'Attachment.file.name', 'Attachment.file.type',
  2092. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2093. 'Attachment.file.size'
  2094. ];
  2095. $this->assertEquals($expected, $this->Form->fields);
  2096. }
  2097. /**
  2098. * testFormSecuredMultipleSelect method
  2099. *
  2100. * Test that multiple selects keys are added to field hash.
  2101. *
  2102. * @return void
  2103. */
  2104. public function testFormSecuredMultipleSelect()
  2105. {
  2106. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  2107. $this->assertEquals([], $this->Form->fields);
  2108. $options = ['1' => 'one', '2' => 'two'];
  2109. $this->Form->select('Model.select', $options);
  2110. $expected = ['Model.select'];
  2111. $this->assertEquals($expected, $this->Form->fields);
  2112. $this->Form->fields = [];
  2113. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2114. $this->assertEquals($expected, $this->Form->fields);
  2115. }
  2116. /**
  2117. * testFormSecuredRadio method
  2118. *
  2119. * @return void
  2120. */
  2121. public function testFormSecuredRadio()
  2122. {
  2123. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2124. $this->assertEquals([], $this->Form->fields);
  2125. $options = ['1' => 'option1', '2' => 'option2'];
  2126. $this->Form->radio('Test.test', $options);
  2127. $expected = ['Test.test'];
  2128. $this->assertEquals($expected, $this->Form->fields);
  2129. $this->Form->radio('Test.all', $options, [
  2130. 'disabled' => ['option1', 'option2']
  2131. ]);
  2132. $expected = ['Test.test', 'Test.all' => ''];
  2133. $this->assertEquals($expected, $this->Form->fields);
  2134. $this->Form->radio('Test.some', $options, [
  2135. 'disabled' => ['option1']
  2136. ]);
  2137. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2138. $this->assertEquals($expected, $this->Form->fields);
  2139. }
  2140. /**
  2141. * testFormSecuredAndDisabledNotAssoc method
  2142. *
  2143. * Test that when disabled is in a list based attribute array it works.
  2144. *
  2145. * @return void
  2146. */
  2147. public function testFormSecuredAndDisabledNotAssoc()
  2148. {
  2149. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2150. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2151. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2152. $this->Form->text('Model.text', ['disabled']);
  2153. $this->Form->textarea('Model.textarea', ['disabled']);
  2154. $this->Form->password('Model.password', ['disabled']);
  2155. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2156. $expected = [
  2157. 'Model.radio' => ''
  2158. ];
  2159. $this->assertEquals($expected, $this->Form->fields);
  2160. }
  2161. /**
  2162. * testFormSecuredAndDisabled method
  2163. *
  2164. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2165. * hashing.
  2166. *
  2167. * @return void
  2168. */
  2169. public function testFormSecuredAndDisabled()
  2170. {
  2171. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2172. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2173. $this->Form->text('Model.text', ['disabled' => true]);
  2174. $this->Form->password('Model.text', ['disabled' => true]);
  2175. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2176. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2177. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2178. $this->Form->year('Model.year', ['disabled' => true]);
  2179. $this->Form->month('Model.month', ['disabled' => true]);
  2180. $this->Form->day('Model.day', ['disabled' => true]);
  2181. $this->Form->hour('Model.hour', ['disabled' => true]);
  2182. $this->Form->minute('Model.minute', ['disabled' => true]);
  2183. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2184. $expected = [
  2185. 'Model.radio' => ''
  2186. ];
  2187. $this->assertEquals($expected, $this->Form->fields);
  2188. }
  2189. /**
  2190. * testDisableSecurityUsingForm method
  2191. *
  2192. * @return void
  2193. */
  2194. public function testDisableSecurityUsingForm()
  2195. {
  2196. $this->Form->request = $this->Form->request->withParam('_Token', [
  2197. 'disabledFields' => []
  2198. ]);
  2199. $this->Form->create();
  2200. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2201. $this->Form->text('Addresses.title');
  2202. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2203. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2204. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2205. $result = $this->Form->fields;
  2206. $expected = [
  2207. 'Addresses.id' => '123456', 'Addresses.title',
  2208. ];
  2209. $this->assertEquals($expected, $result);
  2210. }
  2211. /**
  2212. * testUnlockFieldAddsToList method
  2213. *
  2214. * Test disableField.
  2215. *
  2216. * @return void
  2217. */
  2218. public function testUnlockFieldAddsToList()
  2219. {
  2220. $this->Form->request = $this->Form->request->withParam('_Token', [
  2221. 'unlockedFields' => []
  2222. ]);
  2223. $this->Form->unlockField('Contact.name');
  2224. $this->Form->text('Contact.name');
  2225. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2226. $this->assertEquals([], $this->Form->fields);
  2227. }
  2228. /**
  2229. * testUnlockFieldRemovingFromFields method
  2230. *
  2231. * Test unlockField removing from fields array.
  2232. *
  2233. * @return void
  2234. */
  2235. public function testUnlockFieldRemovingFromFields()
  2236. {
  2237. $this->Form->request = $this->Form->request->withParam('_Token', [
  2238. 'unlockedFields' => []
  2239. ]);
  2240. $this->Form->create($this->article);
  2241. $this->Form->hidden('Article.id', ['value' => 1]);
  2242. $this->Form->text('Article.title');
  2243. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2244. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2245. $this->Form->unlockField('Article.title');
  2246. $this->Form->unlockField('Article.id');
  2247. $this->assertEquals([], $this->Form->fields);
  2248. }
  2249. /**
  2250. * testResetUnlockFields method
  2251. *
  2252. * Test reset unlockFields, when create new form.
  2253. *
  2254. * @return void
  2255. */
  2256. public function testResetUnlockFields()
  2257. {
  2258. $this->Form->request = $this->Form->request->withParam('_Token', [
  2259. 'key' => 'testKey',
  2260. 'unlockedFields' => []
  2261. ]);
  2262. $this->Form->unlockField('Contact.id');
  2263. $this->Form->create('Contact');
  2264. $this->Form->hidden('Contact.id', ['value' => 1]);
  2265. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2266. $this->Form->end();
  2267. $this->Form->create('Contact');
  2268. $this->Form->hidden('Contact.id', ['value' => 1]);
  2269. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2270. }
  2271. /**
  2272. * testSecuredFormUrlIgnoresHost method
  2273. *
  2274. * Test that only the path + query elements of a form's URL show up in their hash.
  2275. *
  2276. * @return void
  2277. */
  2278. public function testSecuredFormUrlIgnoresHost()
  2279. {
  2280. $this->Form->request = $this->Form->request->withParam('_Token', ['key' => 'testKey']);
  2281. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2282. $this->Form->create($this->article, [
  2283. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2284. ]);
  2285. $result = $this->Form->secure();
  2286. $this->assertContains($expected, $result);
  2287. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2288. $result = $this->Form->secure();
  2289. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2290. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2291. $result = $this->Form->secure();
  2292. $this->assertContains($expected, $result, 'URL path + query should work.');
  2293. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2294. $result = $this->Form->secure();
  2295. $this->assertNotContains($expected, $result, 'URL is different');
  2296. }
  2297. /**
  2298. * testSecuredFormUrlHasHtmlAndIdentifier method
  2299. *
  2300. * Test that URL, HTML and identifier show up in their hashes.
  2301. *
  2302. * @return void
  2303. */
  2304. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2305. {
  2306. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2307. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2308. $this->Form->create($this->article, [
  2309. 'url' => [
  2310. 'controller' => 'articles',
  2311. 'action' => 'view',
  2312. '?' => [
  2313. 'page' => 1,
  2314. 'limit' => 10,
  2315. 'html' => '<>"',
  2316. ],
  2317. '#' => 'result',
  2318. ],
  2319. ]);
  2320. $result = $this->Form->secure();
  2321. $this->assertContains($expected, $result);
  2322. $this->Form->create($this->article, [
  2323. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2324. ]);
  2325. $result = $this->Form->secure();
  2326. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2327. $this->Form->create($this->article, [
  2328. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2329. ]);
  2330. $result = $this->Form->secure();
  2331. $this->assertContains($expected, $result, 'URL path + query should work.');
  2332. }
  2333. /**
  2334. * testErrorMessageDisplay method
  2335. *
  2336. * Test error message display.
  2337. *
  2338. * @return void
  2339. */
  2340. public function testErrorMessageDisplay()
  2341. {
  2342. $this->article['errors'] = [
  2343. 'Article' => [
  2344. 'title' => 'error message',
  2345. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2346. ]
  2347. ];
  2348. $this->Form->create($this->article);
  2349. $result = $this->Form->control('Article.title');
  2350. $expected = [
  2351. 'div' => ['class' => 'input text error'],
  2352. 'label' => ['for' => 'article-title'],
  2353. 'Title',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text', 'name' => 'Article[title]',
  2357. 'id' => 'article-title', 'class' => 'form-error'
  2358. ],
  2359. ['div' => ['class' => 'error-message']],
  2360. 'error message',
  2361. '/div',
  2362. '/div'
  2363. ];
  2364. $this->assertHtml($expected, $result);
  2365. $result = $this->Form->control('Article.title', [
  2366. 'templates' => [
  2367. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2368. ]
  2369. ]);
  2370. $expected = [
  2371. 'div' => ['class' => 'input text error'],
  2372. 'label' => ['for' => 'article-title'],
  2373. 'Title',
  2374. '/label',
  2375. 'input' => [
  2376. 'type' => 'text', 'name' => 'Article[title]',
  2377. 'id' => 'article-title', 'class' => 'form-error'
  2378. ],
  2379. '/div'
  2380. ];
  2381. $this->assertHtml($expected, $result);
  2382. $result = $this->Form->control('Article.content');
  2383. $expected = [
  2384. 'div' => ['class' => 'input text error'],
  2385. 'label' => ['for' => 'article-content'],
  2386. 'Content',
  2387. '/label',
  2388. 'input' => [
  2389. 'type' => 'text', 'name' => 'Article[content]',
  2390. 'id' => 'article-content', 'class' => 'form-error'
  2391. ],
  2392. ['div' => ['class' => 'error-message']],
  2393. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2394. '/div',
  2395. '/div'
  2396. ];
  2397. $this->assertHtml($expected, $result);
  2398. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2399. $expected = [
  2400. 'div' => ['class' => 'input text error'],
  2401. 'label' => ['for' => 'article-content'],
  2402. 'Content',
  2403. '/label',
  2404. 'input' => [
  2405. 'type' => 'text', 'name' => 'Article[content]',
  2406. 'id' => 'article-content', 'class' => 'form-error'
  2407. ],
  2408. ['div' => ['class' => 'error-message']],
  2409. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2410. '/div',
  2411. '/div'
  2412. ];
  2413. $this->assertHtml($expected, $result);
  2414. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2415. $expected = [
  2416. 'div' => ['class' => 'input text error'],
  2417. 'label' => ['for' => 'article-content'],
  2418. 'Content',
  2419. '/label',
  2420. 'input' => [
  2421. 'type' => 'text', 'name' => 'Article[content]',
  2422. 'id' => 'article-content', 'class' => 'form-error'
  2423. ],
  2424. ['div' => ['class' => 'error-message']],
  2425. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2426. '/div',
  2427. '/div'
  2428. ];
  2429. $this->assertHtml($expected, $result);
  2430. }
  2431. /**
  2432. * testEmptyErrorValidation method
  2433. *
  2434. * Test validation errors, when validation message is an empty string.
  2435. *
  2436. * @return void
  2437. */
  2438. public function testEmptyErrorValidation()
  2439. {
  2440. $this->article['errors'] = [
  2441. 'Article' => ['title' => '']
  2442. ];
  2443. $this->Form->create($this->article);
  2444. $result = $this->Form->control('Article.title');
  2445. $expected = [
  2446. 'div' => ['class' => 'input text error'],
  2447. 'label' => ['for' => 'article-title'],
  2448. 'Title',
  2449. '/label',
  2450. 'input' => [
  2451. 'type' => 'text', 'name' => 'Article[title]',
  2452. 'id' => 'article-title', 'class' => 'form-error'
  2453. ],
  2454. ['div' => ['class' => 'error-message']],
  2455. [],
  2456. '/div',
  2457. '/div'
  2458. ];
  2459. $this->assertHtml($expected, $result);
  2460. }
  2461. /**
  2462. * testEmptyControlErrorValidation method
  2463. *
  2464. * Test validation errors, when calling control() overriding validation message by an empty string.
  2465. *
  2466. * @return void
  2467. */
  2468. public function testEmptyControlErrorValidation()
  2469. {
  2470. $this->article['errors'] = [
  2471. 'Article' => ['title' => 'error message']
  2472. ];
  2473. $this->Form->create($this->article);
  2474. $result = $this->Form->control('Article.title', ['error' => '']);
  2475. $expected = [
  2476. 'div' => ['class' => 'input text error'],
  2477. 'label' => ['for' => 'article-title'],
  2478. 'Title',
  2479. '/label',
  2480. 'input' => [
  2481. 'type' => 'text', 'name' => 'Article[title]',
  2482. 'id' => 'article-title', 'class' => 'form-error'
  2483. ],
  2484. ['div' => ['class' => 'error-message']],
  2485. [],
  2486. '/div',
  2487. '/div'
  2488. ];
  2489. $this->assertHtml($expected, $result);
  2490. }
  2491. /**
  2492. * testControlErrorMessage method
  2493. *
  2494. * Test validation errors, when calling control() overriding validation messages.
  2495. *
  2496. * @return void
  2497. */
  2498. public function testControlErrorMessage()
  2499. {
  2500. $this->article['errors'] = [
  2501. 'title' => ['error message']
  2502. ];
  2503. $this->Form->create($this->article);
  2504. $result = $this->Form->control('title', [
  2505. 'error' => 'Custom error!'
  2506. ]);
  2507. $expected = [
  2508. 'div' => ['class' => 'input text required error'],
  2509. 'label' => ['for' => 'title'],
  2510. 'Title',
  2511. '/label',
  2512. 'input' => [
  2513. 'type' => 'text', 'name' => 'title',
  2514. 'id' => 'title', 'class' => 'form-error',
  2515. 'required' => 'required',
  2516. ],
  2517. ['div' => ['class' => 'error-message']],
  2518. 'Custom error!',
  2519. '/div',
  2520. '/div'
  2521. ];
  2522. $this->assertHtml($expected, $result);
  2523. $result = $this->Form->control('title', [
  2524. 'error' => ['error message' => 'Custom error!']
  2525. ]);
  2526. $expected = [
  2527. 'div' => ['class' => 'input text required error'],
  2528. 'label' => ['for' => 'title'],
  2529. 'Title',
  2530. '/label',
  2531. 'input' => [
  2532. 'type' => 'text',
  2533. 'name' => 'title',
  2534. 'id' => 'title',
  2535. 'class' => 'form-error',
  2536. 'required' => 'required'
  2537. ],
  2538. ['div' => ['class' => 'error-message']],
  2539. 'Custom error!',
  2540. '/div',
  2541. '/div'
  2542. ];
  2543. $this->assertHtml($expected, $result);
  2544. }
  2545. /**
  2546. * testFormValidationAssociated method
  2547. *
  2548. * Tests displaying errors for nested entities.
  2549. *
  2550. * @return void
  2551. */
  2552. public function testFormValidationAssociated()
  2553. {
  2554. $nested = new Entity(['foo' => 'bar']);
  2555. $nested->setError('foo', ['not a valid bar']);
  2556. $entity = new Entity(['nested' => $nested]);
  2557. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2558. $result = $this->Form->error('nested.foo');
  2559. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2560. }
  2561. /**
  2562. * testFormValidationAssociatedSecondLevel method
  2563. *
  2564. * Test form error display with associated model.
  2565. *
  2566. * @return void
  2567. */
  2568. public function testFormValidationAssociatedSecondLevel()
  2569. {
  2570. $inner = new Entity(['bar' => 'baz']);
  2571. $nested = new Entity(['foo' => $inner]);
  2572. $entity = new Entity(['nested' => $nested]);
  2573. $inner->setError('bar', ['not a valid one']);
  2574. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2575. $result = $this->Form->error('nested.foo.bar');
  2576. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2577. }
  2578. /**
  2579. * testFormValidationMultiRecord method
  2580. *
  2581. * Test form error display with multiple records.
  2582. *
  2583. * @return void
  2584. */
  2585. public function testFormValidationMultiRecord()
  2586. {
  2587. $one = new Entity();
  2588. $two = new Entity();
  2589. TableRegistry::get('Contacts', [
  2590. 'className' => __NAMESPACE__ . '\ContactsTable'
  2591. ]);
  2592. $one->set('email', '');
  2593. $one->setError('email', ['invalid email']);
  2594. $two->set('name', '');
  2595. $two->setError('name', ['This is wrong']);
  2596. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2597. $result = $this->Form->control('0.email');
  2598. $expected = [
  2599. 'div' => ['class' => 'input email error'],
  2600. 'label' => ['for' => '0-email'],
  2601. 'Email',
  2602. '/label',
  2603. 'input' => [
  2604. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2605. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2606. ],
  2607. ['div' => ['class' => 'error-message']],
  2608. 'invalid email',
  2609. '/div',
  2610. '/div'
  2611. ];
  2612. $this->assertHtml($expected, $result);
  2613. $result = $this->Form->control('1.name');
  2614. $expected = [
  2615. 'div' => ['class' => 'input text error'],
  2616. 'label' => ['for' => '1-name'],
  2617. 'Name',
  2618. '/label',
  2619. 'input' => [
  2620. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2621. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2622. ],
  2623. ['div' => ['class' => 'error-message']],
  2624. 'This is wrong',
  2625. '/div',
  2626. '/div'
  2627. ];
  2628. $this->assertHtml($expected, $result);
  2629. }
  2630. /**
  2631. * testControl method
  2632. *
  2633. * Test various incarnations of control().
  2634. *
  2635. * @return void
  2636. */
  2637. public function testControl()
  2638. {
  2639. TableRegistry::get('ValidateUsers', [
  2640. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2641. ]);
  2642. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2643. $result = $this->Form->control('ValidateUsers.balance');
  2644. $expected = [
  2645. 'div' => ['class'],
  2646. 'label' => ['for'],
  2647. 'Balance',
  2648. '/label',
  2649. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2650. '/div',
  2651. ];
  2652. $this->assertHtml($expected, $result);
  2653. $result = $this->Form->control('ValidateUser.cost_decimal');
  2654. $expected = [
  2655. 'div' => ['class'],
  2656. 'label' => ['for'],
  2657. 'Cost Decimal',
  2658. '/label',
  2659. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2660. '/div',
  2661. ];
  2662. $this->assertHtml($expected, $result);
  2663. $result = $this->Form->control('ValidateUser.null_decimal');
  2664. $expected = [
  2665. 'div' => ['class'],
  2666. 'label' => ['for'],
  2667. 'Null Decimal',
  2668. '/label',
  2669. 'input' => ['name', 'type' => 'number', 'id'],
  2670. '/div',
  2671. ];
  2672. $this->assertHtml($expected, $result);
  2673. }
  2674. /**
  2675. * testControlCustomization method
  2676. *
  2677. * Tests the input method and passing custom options.
  2678. *
  2679. * @return void
  2680. */
  2681. public function testControlCustomization()
  2682. {
  2683. TableRegistry::get('Contacts', [
  2684. 'className' => __NAMESPACE__ . '\ContactsTable'
  2685. ]);
  2686. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2687. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2688. $expected = [
  2689. 'div' => ['class' => 'input email'],
  2690. 'label' => ['for' => 'custom'],
  2691. 'Email',
  2692. '/label',
  2693. ['input' => [
  2694. 'type' => 'email', 'name' => 'Contact[email]',
  2695. 'id' => 'custom', 'maxlength' => 255
  2696. ]],
  2697. '/div'
  2698. ];
  2699. $this->assertHtml($expected, $result);
  2700. $result = $this->Form->control('Contact.email', [
  2701. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2702. ]);
  2703. $expected = [
  2704. '<div',
  2705. 'label' => ['for' => 'contact-email'],
  2706. 'Email',
  2707. '/label',
  2708. ['input' => [
  2709. 'type' => 'email', 'name' => 'Contact[email]',
  2710. 'id' => 'contact-email', 'maxlength' => 255
  2711. ]],
  2712. '/div'
  2713. ];
  2714. $this->assertHtml($expected, $result);
  2715. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2716. $expected = [
  2717. 'div' => ['class' => 'input text'],
  2718. 'label' => ['for' => 'contact-email'],
  2719. 'Email',
  2720. '/label',
  2721. ['input' => [
  2722. 'type' => 'text', 'name' => 'Contact[email]',
  2723. 'id' => 'contact-email', 'maxlength' => '255'
  2724. ]],
  2725. '/div'
  2726. ];
  2727. $this->assertHtml($expected, $result);
  2728. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2729. $expected = [
  2730. 'div' => ['class' => 'input text'],
  2731. 'label' => ['for' => 'contact-5-email'],
  2732. 'Email',
  2733. '/label',
  2734. ['input' => [
  2735. 'type' => 'text', 'name' => 'Contact[5][email]',
  2736. 'id' => 'contact-5-email', 'maxlength' => '255'
  2737. ]],
  2738. '/div'
  2739. ];
  2740. $this->assertHtml($expected, $result);
  2741. $result = $this->Form->control('Contact.password');
  2742. $expected = [
  2743. 'div' => ['class' => 'input password'],
  2744. 'label' => ['for' => 'contact-password'],
  2745. 'Password',
  2746. '/label',
  2747. ['input' => [
  2748. 'type' => 'password', 'name' => 'Contact[password]',
  2749. 'id' => 'contact-password'
  2750. ]],
  2751. '/div'
  2752. ];
  2753. $this->assertHtml($expected, $result);
  2754. $result = $this->Form->control('Contact.email', [
  2755. 'type' => 'file', 'class' => 'textbox'
  2756. ]);
  2757. $expected = [
  2758. 'div' => ['class' => 'input file'],
  2759. 'label' => ['for' => 'contact-email'],
  2760. 'Email',
  2761. '/label',
  2762. ['input' => [
  2763. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2764. 'id' => 'contact-email'
  2765. ]],
  2766. '/div'
  2767. ];
  2768. $this->assertHtml($expected, $result);
  2769. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2770. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2771. $result = $this->Form->control('phone');
  2772. $expected = [
  2773. 'div' => ['class' => 'input tel'],
  2774. 'label' => ['for' => 'phone'],
  2775. 'Phone',
  2776. '/label',
  2777. ['input' => [
  2778. 'type' => 'tel', 'name' => 'phone',
  2779. 'value' => 'Hello &amp; World &gt; weird chars',
  2780. 'id' => 'phone', 'maxlength' => 255
  2781. ]],
  2782. '/div'
  2783. ];
  2784. $this->assertHtml($expected, $result);
  2785. $this->Form->request = $this->Form->request->withData('Model.0.OtherModel.field', 'My value');
  2786. $this->Form->create();
  2787. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2788. $expected = [
  2789. 'div' => ['class' => 'input text'],
  2790. 'label' => ['for' => 'myId'],
  2791. 'Field',
  2792. '/label',
  2793. 'input' => [
  2794. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2795. 'value' => 'My value', 'id' => 'myId'
  2796. ],
  2797. '/div'
  2798. ];
  2799. $this->assertHtml($expected, $result);
  2800. $this->Form->request = $this->Form->request->withParsedBody([]);
  2801. $this->Form->create();
  2802. $entity->setError('field', 'Badness!');
  2803. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2804. $result = $this->Form->control('field');
  2805. $expected = [
  2806. 'div' => ['class' => 'input text error'],
  2807. 'label' => ['for' => 'field'],
  2808. 'Field',
  2809. '/label',
  2810. 'input' => [
  2811. 'type' => 'text', 'name' => 'field',
  2812. 'id' => 'field', 'class' => 'form-error'
  2813. ],
  2814. ['div' => ['class' => 'error-message']],
  2815. 'Badness!',
  2816. '/div',
  2817. '/div'
  2818. ];
  2819. $this->assertHtml($expected, $result);
  2820. $result = $this->Form->control('field', [
  2821. 'templates' => [
  2822. 'inputContainerError' => '{{content}}{{error}}',
  2823. 'error' => '<span class="error-message">{{content}}</span>'
  2824. ]
  2825. ]);
  2826. $expected = [
  2827. 'label' => ['for' => 'field'],
  2828. 'Field',
  2829. '/label',
  2830. 'input' => [
  2831. 'type' => 'text', 'name' => 'field',
  2832. 'id' => 'field', 'class' => 'form-error'
  2833. ],
  2834. ['span' => ['class' => 'error-message']],
  2835. 'Badness!',
  2836. '/span'
  2837. ];
  2838. $this->assertHtml($expected, $result);
  2839. $entity->setError('field', ['minLength'], true);
  2840. $result = $this->Form->control('field', [
  2841. 'error' => [
  2842. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2843. 'maxLength' => 'login too large'
  2844. ]
  2845. ]);
  2846. $expected = [
  2847. 'div' => ['class' => 'input text error'],
  2848. 'label' => ['for' => 'field'],
  2849. 'Field',
  2850. '/label',
  2851. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2852. ['div' => ['class' => 'error-message']],
  2853. 'Le login doit contenir au moins 2 caractères',
  2854. '/div',
  2855. '/div'
  2856. ];
  2857. $this->assertHtml($expected, $result);
  2858. $entity->setError('field', ['maxLength'], true);
  2859. $result = $this->Form->control('field', [
  2860. 'error' => [
  2861. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2862. 'maxLength' => 'login too large',
  2863. ]
  2864. ]);
  2865. $expected = [
  2866. 'div' => ['class' => 'input text error'],
  2867. 'label' => ['for' => 'field'],
  2868. 'Field',
  2869. '/label',
  2870. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2871. ['div' => ['class' => 'error-message']],
  2872. 'login too large',
  2873. '/div',
  2874. '/div'
  2875. ];
  2876. $this->assertHtml($expected, $result);
  2877. }
  2878. /**
  2879. * testControlWithTemplateFile method
  2880. *
  2881. * Test that control() accepts a template file.
  2882. *
  2883. * @return void
  2884. */
  2885. public function testControlWithTemplateFile()
  2886. {
  2887. $result = $this->Form->control('field', [
  2888. 'templates' => 'htmlhelper_tags'
  2889. ]);
  2890. $expected = [
  2891. 'label' => ['for' => 'field'],
  2892. 'Field',
  2893. '/label',
  2894. 'input' => [
  2895. 'type' => 'text', 'name' => 'field',
  2896. 'id' => 'field'
  2897. ],
  2898. ];
  2899. $this->assertHtml($expected, $result);
  2900. }
  2901. /**
  2902. * testNestedControlsEndWithBrackets method
  2903. *
  2904. * Test that nested inputs end with brackets.
  2905. *
  2906. * @return void
  2907. */
  2908. public function testNestedControlsEndWithBrackets()
  2909. {
  2910. $result = $this->Form->text('nested.text[]');
  2911. $expected = [
  2912. 'input' => [
  2913. 'type' => 'text', 'name' => 'nested[text][]'
  2914. ],
  2915. ];
  2916. $this->assertHtml($expected, $result);
  2917. $result = $this->Form->file('nested.file[]');
  2918. $expected = [
  2919. 'input' => [
  2920. 'type' => 'file', 'name' => 'nested[file][]'
  2921. ],
  2922. ];
  2923. $this->assertHtml($expected, $result);
  2924. }
  2925. /**
  2926. * testCreateIdPrefix method
  2927. *
  2928. * Test id prefix.
  2929. *
  2930. * @return void
  2931. */
  2932. public function testCreateIdPrefix()
  2933. {
  2934. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2935. $result = $this->Form->control('field');
  2936. $expected = [
  2937. 'div' => ['class' => 'input text'],
  2938. 'label' => ['for' => 'prefix-field'],
  2939. 'Field',
  2940. '/label',
  2941. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2942. '/div'
  2943. ];
  2944. $this->assertHtml($expected, $result);
  2945. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2946. $expected = [
  2947. 'div' => ['class' => 'input text'],
  2948. 'label' => ['for' => 'custom-id'],
  2949. 'Field',
  2950. '/label',
  2951. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2952. '/div'
  2953. ];
  2954. $this->assertHtml($expected, $result);
  2955. $result = $this->Form->radio('Model.field', ['option A']);
  2956. $expected = [
  2957. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2958. 'label' => ['for' => 'prefix-model-field-0'],
  2959. ['input' => [
  2960. 'type' => 'radio',
  2961. 'name' => 'Model[field]',
  2962. 'value' => '0',
  2963. 'id' => 'prefix-model-field-0'
  2964. ]],
  2965. 'option A',
  2966. '/label'
  2967. ];
  2968. $this->assertHtml($expected, $result);
  2969. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2970. $expected = [
  2971. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2972. 'label' => ['for' => 'prefix-model-field-0'],
  2973. ['input' => [
  2974. 'type' => 'radio',
  2975. 'name' => 'Model[field]',
  2976. 'value' => '0',
  2977. 'id' => 'prefix-model-field-0'
  2978. ]],
  2979. 'option A',
  2980. '/label'
  2981. ];
  2982. $this->assertHtml($expected, $result);
  2983. $result = $this->Form->select(
  2984. 'Model.multi_field',
  2985. ['first'],
  2986. ['multiple' => 'checkbox']
  2987. );
  2988. $expected = [
  2989. 'input' => [
  2990. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2991. ],
  2992. ['div' => ['class' => 'checkbox']],
  2993. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2994. ['input' => [
  2995. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2996. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2997. ]],
  2998. 'first',
  2999. '/label',
  3000. '/div',
  3001. ];
  3002. $this->assertHtml($expected, $result);
  3003. $this->Form->end();
  3004. $result = $this->Form->control('field');
  3005. $expected = [
  3006. 'div' => ['class' => 'input text'],
  3007. 'label' => ['for' => 'field'],
  3008. 'Field',
  3009. '/label',
  3010. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3011. '/div'
  3012. ];
  3013. $this->assertHtml($expected, $result);
  3014. }
  3015. /**
  3016. * testControlZero method
  3017. *
  3018. * Test that inputs with 0 can be created.
  3019. *
  3020. * @return void
  3021. */
  3022. public function testControlZero()
  3023. {
  3024. TableRegistry::get('Contacts', [
  3025. 'className' => __NAMESPACE__ . '\ContactsTable'
  3026. ]);
  3027. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3028. $result = $this->Form->control('0');
  3029. $expected = [
  3030. 'div' => ['class' => 'input text'],
  3031. 'label' => ['for' => '0'], '/label',
  3032. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3033. '/div'
  3034. ];
  3035. $this->assertHtml($expected, $result);
  3036. }
  3037. /**
  3038. * testControlCheckbox method
  3039. *
  3040. * Test control() with checkbox creation.
  3041. *
  3042. * @return void
  3043. */
  3044. public function testControlCheckbox()
  3045. {
  3046. $articles = TableRegistry::get('Articles');
  3047. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3048. $article = $articles->newEntity();
  3049. $this->Form->create($article);
  3050. $result = $this->Form->control('Articles.active');
  3051. $expected = [
  3052. 'div' => ['class' => 'input checkbox'],
  3053. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3054. 'label' => ['for' => 'articles-active'],
  3055. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3056. 'Active',
  3057. '/label',
  3058. '/div'
  3059. ];
  3060. $this->assertHtml($expected, $result);
  3061. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3062. $expected = [
  3063. 'div' => ['class' => 'input checkbox'],
  3064. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3065. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3066. '/div'
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3070. $expected = [
  3071. 'div' => ['class' => 'input checkbox'],
  3072. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3073. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3074. '/div'
  3075. ];
  3076. $this->assertHtml($expected, $result);
  3077. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3078. $expected = [
  3079. 'div' => ['class' => 'input checkbox'],
  3080. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3081. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3082. '/div'
  3083. ];
  3084. $this->assertHtml($expected, $result);
  3085. $result = $this->Form->control('Articles.disabled', [
  3086. 'label' => 'Disabled',
  3087. 'type' => 'checkbox',
  3088. 'data-foo' => 'disabled'
  3089. ]);
  3090. $expected = [
  3091. 'div' => ['class' => 'input checkbox'],
  3092. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3093. 'label' => ['for' => 'articles-disabled'],
  3094. ['input' => [
  3095. 'type' => 'checkbox',
  3096. 'name' => 'Articles[disabled]',
  3097. 'value' => '1',
  3098. 'id' => 'articles-disabled',
  3099. 'data-foo' => 'disabled'
  3100. ]],
  3101. 'Disabled',
  3102. '/label',
  3103. '/div'
  3104. ];
  3105. $this->assertHtml($expected, $result);
  3106. $result = $this->Form->control('Articles.confirm', [
  3107. 'label' => 'Confirm <b>me</b>!',
  3108. 'type' => 'checkbox',
  3109. 'escape' => false
  3110. ]);
  3111. $expected = [
  3112. 'div' => ['class' => 'input checkbox'],
  3113. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3114. 'label' => ['for' => 'articles-confirm'],
  3115. ['input' => [
  3116. 'type' => 'checkbox',
  3117. 'name' => 'Articles[confirm]',
  3118. 'value' => '1',
  3119. 'id' => 'articles-confirm',
  3120. ]],
  3121. 'Confirm <b>me</b>!',
  3122. '/label',
  3123. '/div'
  3124. ];
  3125. $this->assertHtml($expected, $result);
  3126. }
  3127. /**
  3128. * testControlHidden method
  3129. *
  3130. * Test that control() does not create wrapping div and label tag for hidden fields.
  3131. *
  3132. * @return void
  3133. */
  3134. public function testControlHidden()
  3135. {
  3136. TableRegistry::get('ValidateUsers', [
  3137. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3138. ]);
  3139. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3140. $result = $this->Form->control('ValidateUser.id');
  3141. $expected = [
  3142. 'input' => ['name', 'type' => 'hidden', 'id']
  3143. ];
  3144. $this->assertHtml($expected, $result);
  3145. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3146. $expected = [
  3147. 'input' => ['name', 'type' => 'hidden', 'id']
  3148. ];
  3149. $this->assertHtml($expected, $result);
  3150. }
  3151. /**
  3152. * testControlDatetime method
  3153. *
  3154. * Test form->control() with datetime.
  3155. *
  3156. * @return void
  3157. */
  3158. public function testControlDatetime()
  3159. {
  3160. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3161. ->setMethods(['datetime'])
  3162. ->setConstructorArgs([new View()])
  3163. ->getMock();
  3164. $this->Form->expects($this->once())->method('datetime')
  3165. ->with('prueba', [
  3166. 'type' => 'datetime',
  3167. 'timeFormat' => 24,
  3168. 'minYear' => 2008,
  3169. 'maxYear' => 2011,
  3170. 'interval' => 15,
  3171. 'options' => null,
  3172. 'empty' => false,
  3173. 'id' => 'prueba',
  3174. 'required' => false,
  3175. 'templateVars' => []
  3176. ])
  3177. ->will($this->returnValue('This is it!'));
  3178. $result = $this->Form->control('prueba', [
  3179. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3180. 'maxYear' => 2011, 'interval' => 15
  3181. ]);
  3182. $expected = [
  3183. 'div' => ['class' => 'input datetime'],
  3184. '<label',
  3185. 'Prueba',
  3186. '/label',
  3187. 'This is it!',
  3188. '/div'
  3189. ];
  3190. $this->assertHtml($expected, $result);
  3191. }
  3192. /**
  3193. * testControlDatetimeIdPrefix method
  3194. *
  3195. * Test form->control() with datetime with id prefix.
  3196. *
  3197. * @return void
  3198. */
  3199. public function testControlDatetimeIdPrefix()
  3200. {
  3201. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3202. ->setMethods(['datetime'])
  3203. ->setConstructorArgs([new View()])
  3204. ->getMock();
  3205. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3206. $this->Form->expects($this->once())->method('datetime')
  3207. ->with('prueba', [
  3208. 'type' => 'datetime',
  3209. 'timeFormat' => 24,
  3210. 'minYear' => 2008,
  3211. 'maxYear' => 2011,
  3212. 'interval' => 15,
  3213. 'options' => null,
  3214. 'empty' => false,
  3215. 'id' => 'prefix-prueba',
  3216. 'required' => false,
  3217. 'templateVars' => []
  3218. ])
  3219. ->will($this->returnValue('This is it!'));
  3220. $result = $this->Form->control('prueba', [
  3221. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3222. 'maxYear' => 2011, 'interval' => 15
  3223. ]);
  3224. $expected = [
  3225. 'div' => ['class' => 'input datetime'],
  3226. '<label',
  3227. 'Prueba',
  3228. '/label',
  3229. 'This is it!',
  3230. '/div'
  3231. ];
  3232. $this->assertHtml($expected, $result);
  3233. }
  3234. /**
  3235. * testControlCheckboxWithDisabledElements method
  3236. *
  3237. * Test generating checkboxes with disabled elements.
  3238. *
  3239. * @return void
  3240. */
  3241. public function testControlCheckboxWithDisabledElements()
  3242. {
  3243. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3244. $result = $this->Form->control('Contact.multiple', [
  3245. 'multiple' => 'checkbox',
  3246. 'disabled' => 'disabled',
  3247. 'options' => $options
  3248. ]);
  3249. $expected = [
  3250. ['div' => ['class' => 'input select']],
  3251. ['label' => ['for' => 'contact-multiple']],
  3252. 'Multiple',
  3253. '/label',
  3254. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3255. ['div' => ['class' => 'checkbox']],
  3256. ['label' => ['for' => 'contact-multiple-1']],
  3257. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3258. 'One',
  3259. '/label',
  3260. '/div',
  3261. ['div' => ['class' => 'checkbox']],
  3262. ['label' => ['for' => 'contact-multiple-2']],
  3263. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3264. 'Two',
  3265. '/label',
  3266. '/div',
  3267. ['div' => ['class' => 'checkbox']],
  3268. ['label' => ['for' => 'contact-multiple-3']],
  3269. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3270. 'Three',
  3271. '/label',
  3272. '/div',
  3273. '/div'
  3274. ];
  3275. $this->assertHtml($expected, $result);
  3276. // make sure 50 does only disable 50, and not 50f5c0cf
  3277. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3278. $disabled = [50];
  3279. $expected = [
  3280. ['div' => ['class' => 'input select']],
  3281. ['label' => ['for' => 'contact-multiple']],
  3282. 'Multiple',
  3283. '/label',
  3284. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3285. ['div' => ['class' => 'checkbox']],
  3286. ['label' => ['for' => 'contact-multiple-50']],
  3287. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3288. 'Fifty',
  3289. '/label',
  3290. '/div',
  3291. ['div' => ['class' => 'checkbox']],
  3292. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3293. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3294. 'Stringy',
  3295. '/label',
  3296. '/div',
  3297. '/div'
  3298. ];
  3299. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3300. $this->assertHtml($expected, $result);
  3301. }
  3302. /**
  3303. * testControlWithLeadingInteger method
  3304. *
  3305. * Test input name with leading integer, ensure attributes are generated correctly.
  3306. *
  3307. * @return void
  3308. */
  3309. public function testControlWithLeadingInteger()
  3310. {
  3311. $result = $this->Form->text('0.Node.title');
  3312. $expected = [
  3313. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3314. ];
  3315. $this->assertHtml($expected, $result);
  3316. }
  3317. /**
  3318. * testControlSelectType method
  3319. *
  3320. * Test form->control() with select type inputs.
  3321. *
  3322. * @return void
  3323. */
  3324. public function testControlSelectType()
  3325. {
  3326. $result = $this->Form->control(
  3327. 'email',
  3328. [
  3329. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3330. );
  3331. $expected = [
  3332. 'div' => ['class' => 'input select'],
  3333. 'label' => ['for' => 'email'],
  3334. 'Email',
  3335. '/label',
  3336. ['select' => ['name' => 'email', 'id' => 'email']],
  3337. ['option' => ['value' => '']],
  3338. '/option',
  3339. ['option' => ['value' => 'è']],
  3340. 'Firést',
  3341. '/option',
  3342. ['option' => ['value' => 'é']],
  3343. 'Secoènd',
  3344. '/option',
  3345. '/select',
  3346. '/div'
  3347. ];
  3348. $this->assertHtml($expected, $result);
  3349. $result = $this->Form->control(
  3350. 'email',
  3351. [
  3352. 'options' => ['First', 'Second'], 'empty' => true]
  3353. );
  3354. $expected = [
  3355. 'div' => ['class' => 'input select'],
  3356. 'label' => ['for' => 'email'],
  3357. 'Email',
  3358. '/label',
  3359. ['select' => ['name' => 'email', 'id' => 'email']],
  3360. ['option' => ['value' => '']],
  3361. '/option',
  3362. ['option' => ['value' => '0']],
  3363. 'First',
  3364. '/option',
  3365. ['option' => ['value' => '1']],
  3366. 'Second',
  3367. '/option',
  3368. '/select',
  3369. '/div'
  3370. ];
  3371. $this->assertHtml($expected, $result);
  3372. $result = $this->Form->control('email', [
  3373. 'type' => 'select',
  3374. 'options' => new \ArrayObject(['First', 'Second']),
  3375. 'empty' => true
  3376. ]);
  3377. $this->assertHtml($expected, $result);
  3378. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3379. $this->Form->request = $this->Form->request->withData('Model', ['user_id' => 'value']);
  3380. $this->Form->create();
  3381. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3382. $expected = [
  3383. 'div' => ['class' => 'input select'],
  3384. 'label' => ['for' => 'model-user-id'],
  3385. 'User',
  3386. '/label',
  3387. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3388. ['option' => ['value' => '']],
  3389. '/option',
  3390. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3391. 'good',
  3392. '/option',
  3393. ['option' => ['value' => 'other']],
  3394. 'bad',
  3395. '/option',
  3396. '/select',
  3397. '/div'
  3398. ];
  3399. $this->assertHtml($expected, $result);
  3400. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3401. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => null]);
  3402. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3403. $expected = [
  3404. 'div' => ['class' => 'input select'],
  3405. 'label' => ['for' => 'thing-user-id'],
  3406. 'User',
  3407. '/label',
  3408. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3409. ['option' => ['value' => '']],
  3410. 'Some Empty',
  3411. '/option',
  3412. ['option' => ['value' => 'value']],
  3413. 'good',
  3414. '/option',
  3415. ['option' => ['value' => 'other']],
  3416. 'bad',
  3417. '/option',
  3418. '/select',
  3419. '/div'
  3420. ];
  3421. $this->assertHtml($expected, $result);
  3422. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3423. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => 'value']);
  3424. $this->Form->create();
  3425. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3426. $expected = [
  3427. 'div' => ['class' => 'input select'],
  3428. 'label' => ['for' => 'thing-user-id'],
  3429. 'User',
  3430. '/label',
  3431. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3432. ['option' => ['value' => '']],
  3433. 'Some Empty',
  3434. '/option',
  3435. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3436. 'good',
  3437. '/option',
  3438. ['option' => ['value' => 'other']],
  3439. 'bad',
  3440. '/option',
  3441. '/select',
  3442. '/div'
  3443. ];
  3444. $this->assertHtml($expected, $result);
  3445. $result = $this->Form->control('Publisher.id', [
  3446. 'label' => 'Publisher',
  3447. 'type' => 'select',
  3448. 'multiple' => 'checkbox',
  3449. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3450. ]);
  3451. $expected = [
  3452. ['div' => ['class' => 'input select']],
  3453. ['label' => ['for' => 'publisher-id']],
  3454. 'Publisher',
  3455. '/label',
  3456. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3457. ['div' => ['class' => 'checkbox']],
  3458. ['label' => ['for' => 'publisher-id-value-1']],
  3459. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3460. 'Label 1',
  3461. '/label',
  3462. '/div',
  3463. ['div' => ['class' => 'checkbox']],
  3464. ['label' => ['for' => 'publisher-id-value-2']],
  3465. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3466. 'Label 2',
  3467. '/label',
  3468. '/div',
  3469. '/div'
  3470. ];
  3471. $this->assertHtml($expected, $result);
  3472. }
  3473. /**
  3474. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3475. *
  3476. * Test that control() and a non standard primary key makes a hidden input by default.
  3477. *
  3478. * @return void
  3479. */
  3480. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3481. {
  3482. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3483. $this->Form->create($this->article);
  3484. $result = $this->Form->control('title');
  3485. $expected = [
  3486. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3487. ];
  3488. $this->assertHtml($expected, $result);
  3489. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3490. $this->Form->create($this->article);
  3491. $result = $this->Form->control('title');
  3492. $expected = [
  3493. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3494. ];
  3495. $this->assertHtml($expected, $result);
  3496. $result = $this->Form->control('body');
  3497. $expected = [
  3498. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3499. ];
  3500. $this->assertHtml($expected, $result);
  3501. }
  3502. /**
  3503. * testControlOverridingMagicSelectType method
  3504. *
  3505. * Test that overriding the magic select type widget is possible.
  3506. *
  3507. * @return void
  3508. */
  3509. public function testControlOverridingMagicSelectType()
  3510. {
  3511. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3512. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3513. $expected = [
  3514. 'div' => ['class' => 'input text'],
  3515. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3516. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3517. '/div'
  3518. ];
  3519. $this->assertHtml($expected, $result);
  3520. //Check that magic types still work for plural/singular vars
  3521. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3522. $result = $this->Form->control('Model.type');
  3523. $expected = [
  3524. 'div' => ['class' => 'input select'],
  3525. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3526. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3527. ['option' => ['value' => 'value']], 'good', '/option',
  3528. ['option' => ['value' => 'other']], 'bad', '/option',
  3529. '/select',
  3530. '/div'
  3531. ];
  3532. $this->assertHtml($expected, $result);
  3533. }
  3534. /**
  3535. * testControlMagicTypeDoesNotOverride method
  3536. *
  3537. * Test that inferred types do not override developer input.
  3538. *
  3539. * @return void
  3540. */
  3541. public function testControlMagicTypeDoesNotOverride()
  3542. {
  3543. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3544. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3545. $expected = [
  3546. 'div' => ['class' => 'input checkbox'],
  3547. ['input' => [
  3548. 'type' => 'hidden',
  3549. 'name' => 'Model[user]',
  3550. 'value' => 0,
  3551. ]],
  3552. 'label' => ['for' => 'model-user'],
  3553. ['input' => [
  3554. 'name' => 'Model[user]',
  3555. 'type' => 'checkbox',
  3556. 'id' => 'model-user',
  3557. 'value' => 1
  3558. ]],
  3559. 'User',
  3560. '/label',
  3561. '/div'
  3562. ];
  3563. $this->assertHtml($expected, $result);
  3564. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3565. $options = [
  3566. 1 => 'blue',
  3567. 2 => 'red'
  3568. ];
  3569. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3570. $expected = [
  3571. 'div' => ['class' => 'input select'],
  3572. 'label' => ['for' => 'tags-ids'],
  3573. 'Tags',
  3574. '/label',
  3575. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3576. ['div' => ['class' => 'checkbox']],
  3577. ['label' => ['for' => 'tags-ids-1']],
  3578. ['input' => [
  3579. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3580. 'value' => '1', 'name' => 'tags[_ids][]'
  3581. ]],
  3582. 'blue',
  3583. '/label',
  3584. '/div',
  3585. ['div' => ['class' => 'checkbox']],
  3586. ['label' => ['for' => 'tags-ids-2']],
  3587. ['input' => [
  3588. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3589. 'value' => '2', 'name' => 'tags[_ids][]'
  3590. ]],
  3591. 'red',
  3592. '/label',
  3593. '/div',
  3594. '/div'
  3595. ];
  3596. $this->assertHtml($expected, $result);
  3597. }
  3598. /**
  3599. * testControlMagicSelectForTypeNumber method
  3600. *
  3601. * Test that magic control() selects are created for type=number.
  3602. *
  3603. * @return void
  3604. */
  3605. public function testControlMagicSelectForTypeNumber()
  3606. {
  3607. TableRegistry::get('ValidateUsers', [
  3608. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3609. ]);
  3610. $entity = new Entity(['balance' => 1]);
  3611. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3612. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3613. $result = $this->Form->control('balance');
  3614. $expected = [
  3615. 'div' => ['class' => 'input select'],
  3616. 'label' => ['for' => 'balance'],
  3617. 'Balance',
  3618. '/label',
  3619. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3620. ['option' => ['value' => '0']],
  3621. 'nothing',
  3622. '/option',
  3623. ['option' => ['value' => '1', 'selected' => 'selected']],
  3624. 'some',
  3625. '/option',
  3626. ['option' => ['value' => '100']],
  3627. 'a lot',
  3628. '/option',
  3629. '/select',
  3630. '/div'
  3631. ];
  3632. $this->assertHtml($expected, $result);
  3633. }
  3634. /**
  3635. * testInvalidControlTypeOption method
  3636. *
  3637. * Test invalid 'input' type option to control() function.
  3638. *
  3639. * @return void
  3640. */
  3641. public function testInvalidControlTypeOption()
  3642. {
  3643. $this->expectException(\RuntimeException::class);
  3644. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3645. $this->Form->control('text', ['type' => 'input']);
  3646. }
  3647. /**
  3648. * testControlMagicSelectChangeToRadio method
  3649. *
  3650. * Test that magic control() selects can easily be converted into radio types without error.
  3651. *
  3652. * @return void
  3653. */
  3654. public function testControlMagicSelectChangeToRadio()
  3655. {
  3656. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3657. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3658. $this->assertContains('input type="radio"', $result);
  3659. }
  3660. /**
  3661. * testFormControlSubmit method
  3662. *
  3663. * Test correct results for form::control() and type submit.
  3664. *
  3665. * @return void
  3666. */
  3667. public function testFormControlSubmit()
  3668. {
  3669. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3670. $expected = [
  3671. 'div' => ['class' => 'submit'],
  3672. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3673. '/div'
  3674. ];
  3675. $this->assertHtml($expected, $result);
  3676. }
  3677. /**
  3678. * testFormControls method
  3679. *
  3680. * Test correct results from Form::controls().
  3681. *
  3682. * @return void
  3683. */
  3684. public function testFormControlsLegendFieldset()
  3685. {
  3686. $this->Form->create($this->article);
  3687. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3688. $expected = [
  3689. '<fieldset',
  3690. '<legend',
  3691. 'The Legend',
  3692. '/legend',
  3693. '*/fieldset',
  3694. ];
  3695. $this->assertHtml($expected, $result);
  3696. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3697. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3698. $this->assertContains('<fieldset>', $result);
  3699. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3700. $this->assertNotContains('<legend>', $result);
  3701. $this->assertNotContains('<fieldset>', $result);
  3702. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3703. $this->assertNotContains('<legend>', $result);
  3704. $this->assertNotContains('<fieldset>', $result);
  3705. $this->Form->create($this->article);
  3706. $this->Form->request = $this->Form->request
  3707. ->withParam('prefix', 'admin')
  3708. ->withParam('action', 'admin_edit')
  3709. ->withParam('controller', 'articles');
  3710. $result = $this->Form->allControls();
  3711. $expected = [
  3712. '<fieldset',
  3713. '<legend',
  3714. 'New Article',
  3715. '/legend',
  3716. '*/fieldset',
  3717. ];
  3718. $this->assertHtml($expected, $result);
  3719. $this->Form->create($this->article);
  3720. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3721. $expected = [
  3722. '<fieldset',
  3723. '<legend',
  3724. 'The Legend',
  3725. '/legend',
  3726. '*/fieldset',
  3727. ];
  3728. $this->assertHtml($expected, $result);
  3729. $this->Form->create($this->article);
  3730. $result = $this->Form->allControls([], [
  3731. 'fieldset' => [
  3732. 'class' => 'some-class some-other-class',
  3733. 'disabled' => true,
  3734. 'data-param' => 'a-param'
  3735. ],
  3736. 'legend' => 'The Legend'
  3737. ]);
  3738. $expected = [
  3739. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3740. '<legend',
  3741. 'The Legend',
  3742. '/legend',
  3743. '*/fieldset',
  3744. ];
  3745. $this->assertHtml($expected, $result);
  3746. }
  3747. /**
  3748. * testFormControls method
  3749. *
  3750. * Test the controls() method.
  3751. *
  3752. * @return void
  3753. */
  3754. public function testFormControls()
  3755. {
  3756. $this->Form->create($this->article);
  3757. $result = $this->Form->allControls();
  3758. $expected = [
  3759. '<fieldset',
  3760. '<legend', 'New Article', '/legend',
  3761. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3762. ['div' => ['class' => 'input select required']],
  3763. '*/div',
  3764. ['div' => ['class' => 'input text required']],
  3765. '*/div',
  3766. ['div' => ['class' => 'input text']],
  3767. '*/div',
  3768. ['div' => ['class' => 'input text']],
  3769. '*/div',
  3770. '/fieldset',
  3771. ];
  3772. $this->assertHtml($expected, $result);
  3773. $result = $this->Form->allControls([
  3774. 'published' => ['type' => 'boolean']
  3775. ]);
  3776. $expected = [
  3777. '<fieldset',
  3778. '<legend', 'New Article', '/legend',
  3779. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3780. ['div' => ['class' => 'input select required']],
  3781. '*/div',
  3782. ['div' => ['class' => 'input text required']],
  3783. '*/div',
  3784. ['div' => ['class' => 'input text']],
  3785. '*/div',
  3786. ['div' => ['class' => 'input boolean']],
  3787. '*/div',
  3788. '/fieldset',
  3789. ];
  3790. $this->assertHtml($expected, $result);
  3791. $this->Form->create($this->article);
  3792. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3793. $expected = [
  3794. 'fieldset' => [],
  3795. 'legend' => [],
  3796. 'Hello',
  3797. '/legend',
  3798. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3799. ['div' => ['class' => 'input select required']],
  3800. '*/div',
  3801. ['div' => ['class' => 'input text required']],
  3802. '*/div',
  3803. ['div' => ['class' => 'input text']],
  3804. '*/div',
  3805. ['div' => ['class' => 'input text']],
  3806. '*/div',
  3807. '/fieldset'
  3808. ];
  3809. $this->assertHtml($expected, $result);
  3810. $this->Form->create(false);
  3811. $expected = [
  3812. 'fieldset' => [],
  3813. ['div' => ['class' => 'input text']],
  3814. 'label' => ['for' => 'foo'],
  3815. 'Foo',
  3816. '/label',
  3817. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3818. '*/div',
  3819. '/fieldset'
  3820. ];
  3821. $result = $this->Form->allControls(
  3822. ['foo' => ['type' => 'text']],
  3823. ['legend' => false]
  3824. );
  3825. $this->assertHtml($expected, $result);
  3826. }
  3827. /**
  3828. * testFormControlsBlacklist method
  3829. *
  3830. * @return void
  3831. */
  3832. public function testFormControlsBlacklist()
  3833. {
  3834. $this->Form->create($this->article);
  3835. $result = $this->Form->allControls([
  3836. 'id' => false
  3837. ]);
  3838. $expected = [
  3839. '<fieldset',
  3840. '<legend', 'New Article', '/legend',
  3841. ['div' => ['class' => 'input select required']],
  3842. '*/div',
  3843. ['div' => ['class' => 'input text required']],
  3844. '*/div',
  3845. ['div' => ['class' => 'input text']],
  3846. '*/div',
  3847. ['div' => ['class' => 'input text']],
  3848. '*/div',
  3849. '/fieldset',
  3850. ];
  3851. $this->assertHtml($expected, $result);
  3852. $this->Form->create($this->article);
  3853. $result = $this->Form->allControls([
  3854. 'id' => []
  3855. ]);
  3856. $expected = [
  3857. '<fieldset',
  3858. '<legend', 'New Article', '/legend',
  3859. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3860. ['div' => ['class' => 'input select required']],
  3861. '*/div',
  3862. ['div' => ['class' => 'input text required']],
  3863. '*/div',
  3864. ['div' => ['class' => 'input text']],
  3865. '*/div',
  3866. ['div' => ['class' => 'input text']],
  3867. '*/div',
  3868. '/fieldset',
  3869. ];
  3870. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3871. }
  3872. /**
  3873. * testSelectAsCheckbox method
  3874. *
  3875. * Test multi-select widget with checkbox formatting.
  3876. *
  3877. * @return void
  3878. */
  3879. public function testSelectAsCheckbox()
  3880. {
  3881. $result = $this->Form->select(
  3882. 'Model.multi_field',
  3883. ['first', 'second', 'third'],
  3884. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3885. );
  3886. $expected = [
  3887. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3888. ['div' => ['class' => 'checkbox']],
  3889. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3890. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3891. 'first',
  3892. '/label',
  3893. '/div',
  3894. ['div' => ['class' => 'checkbox']],
  3895. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3896. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3897. 'second',
  3898. '/label',
  3899. '/div',
  3900. ['div' => ['class' => 'checkbox']],
  3901. ['label' => ['for' => 'model-multi-field-2']],
  3902. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3903. 'third',
  3904. '/label',
  3905. '/div',
  3906. ];
  3907. $this->assertHtml($expected, $result);
  3908. $result = $this->Form->select(
  3909. 'Model.multi_field',
  3910. ['1/2' => 'half'],
  3911. ['multiple' => 'checkbox']
  3912. );
  3913. $expected = [
  3914. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3915. ['div' => ['class' => 'checkbox']],
  3916. ['label' => ['for' => 'model-multi-field-1-2']],
  3917. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3918. 'half',
  3919. '/label',
  3920. '/div',
  3921. ];
  3922. $this->assertHtml($expected, $result);
  3923. }
  3924. /**
  3925. * testLabel method
  3926. *
  3927. * Test label generation.
  3928. *
  3929. * @return void
  3930. */
  3931. public function testLabel()
  3932. {
  3933. $result = $this->Form->label('Person.name');
  3934. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3935. $this->assertHtml($expected, $result);
  3936. $result = $this->Form->label('Person.name');
  3937. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3938. $this->assertHtml($expected, $result);
  3939. $result = $this->Form->label('Person.first_name');
  3940. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3941. $this->assertHtml($expected, $result);
  3942. $result = $this->Form->label('Person.first_name', 'Your first name');
  3943. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3944. $this->assertHtml($expected, $result);
  3945. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3946. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3947. $this->assertHtml($expected, $result);
  3948. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3949. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3950. $this->assertHtml($expected, $result);
  3951. $result = $this->Form->label('Person.first_name', '');
  3952. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3953. $this->assertHtml($expected, $result);
  3954. $result = $this->Form->label('Person.2.name', '');
  3955. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3956. $this->assertHtml($expected, $result);
  3957. }
  3958. /**
  3959. * testLabelContainControl method
  3960. *
  3961. * Test that label() can accept an input with the correct template vars.
  3962. *
  3963. * @return void
  3964. */
  3965. public function testLabelContainControl()
  3966. {
  3967. $this->Form->setTemplates([
  3968. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3969. ]);
  3970. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3971. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3972. ]);
  3973. $expected = [
  3974. 'label' => ['for' => 'person-accept-terms'],
  3975. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3976. 'Accept',
  3977. '/label',
  3978. ];
  3979. $this->assertHtml($expected, $result);
  3980. }
  3981. /**
  3982. * testTextbox method
  3983. *
  3984. * Test textbox element generation.
  3985. *
  3986. * @return void
  3987. */
  3988. public function testTextbox()
  3989. {
  3990. $result = $this->Form->text('Model.field');
  3991. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3992. $this->assertHtml($expected, $result);
  3993. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3994. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3995. $this->assertHtml($expected, $result);
  3996. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3997. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3998. $this->assertHtml($expected, $result);
  3999. }
  4000. /**
  4001. * testTextBoxDataAndError method
  4002. *
  4003. * Test that text() hooks up with request data and error fields.
  4004. *
  4005. * @return void
  4006. */
  4007. public function testTextBoxDataAndError()
  4008. {
  4009. $this->article['errors'] = [
  4010. 'Contact' => ['text' => 'wrong']
  4011. ];
  4012. $this->Form->request = $this->Form->request
  4013. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4014. ->withData('Contact.text', 'test');
  4015. $this->Form->create($this->article);
  4016. $result = $this->Form->text('Model.text');
  4017. $expected = [
  4018. 'input' => [
  4019. 'type' => 'text',
  4020. 'name' => 'Model[text]',
  4021. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4022. ]
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4026. $expected = [
  4027. 'input' => [
  4028. 'type' => 'text',
  4029. 'name' => 'Contact[text]',
  4030. 'value' => 'test',
  4031. 'id' => 'theID',
  4032. 'class' => 'form-error'
  4033. ]
  4034. ];
  4035. $this->assertHtml($expected, $result);
  4036. }
  4037. /**
  4038. * testDefaultValue method
  4039. *
  4040. * Test default value setting.
  4041. *
  4042. * @return void
  4043. */
  4044. public function testTextDefaultValue()
  4045. {
  4046. $this->Form->request = $this->Form->request->withData('Model.field', 'test');
  4047. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4048. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4049. $this->assertHtml($expected, $result);
  4050. $this->Form->request = $this->Form->request->withParsedBody([]);
  4051. $this->Form->create();
  4052. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4053. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4054. $this->assertHtml($expected, $result);
  4055. $Articles = TableRegistry::get('Articles');
  4056. $title = $Articles->getSchema()->getColumn('title');
  4057. $Articles->getSchema()->addColumn(
  4058. 'title',
  4059. ['default' => 'default title'] + $title
  4060. );
  4061. $entity = $Articles->newEntity();
  4062. $this->Form->create($entity);
  4063. // Get default value from schema
  4064. $result = $this->Form->text('title');
  4065. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4066. $this->assertHtml($expected, $result);
  4067. // Don't get value from schema
  4068. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4069. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4070. $this->assertHtml($expected, $result);
  4071. // Custom default value overrides default value from schema
  4072. $result = $this->Form->text('title', ['default' => 'override default']);
  4073. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4074. $this->assertHtml($expected, $result);
  4075. // Default value from schema is used only for new entities.
  4076. $entity->isNew(false);
  4077. $result = $this->Form->text('title');
  4078. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4079. $this->assertHtml($expected, $result);
  4080. }
  4081. /**
  4082. * testError method
  4083. *
  4084. * Test field error generation.
  4085. *
  4086. * @return void
  4087. */
  4088. public function testError()
  4089. {
  4090. $this->article['errors'] = [
  4091. 'Article' => ['field' => 'email']
  4092. ];
  4093. $this->Form->create($this->article);
  4094. $result = $this->Form->error('Article.field');
  4095. $expected = [
  4096. ['div' => ['class' => 'error-message']],
  4097. 'email',
  4098. '/div',
  4099. ];
  4100. $this->assertHtml($expected, $result);
  4101. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4102. $expected = [
  4103. ['div' => ['class' => 'error-message']],
  4104. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4105. '/div',
  4106. ];
  4107. $this->assertHtml($expected, $result);
  4108. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4109. $expected = [
  4110. ['div' => ['class' => 'error-message']],
  4111. '<strong', 'Badness!', '/strong',
  4112. '/div',
  4113. ];
  4114. $this->assertHtml($expected, $result);
  4115. }
  4116. /**
  4117. * testErrorRuleName method
  4118. *
  4119. * Test error translation can use rule names for translating.
  4120. *
  4121. * @return void
  4122. */
  4123. public function testErrorRuleName()
  4124. {
  4125. $this->article['errors'] = [
  4126. 'Article' => [
  4127. 'field' => ['email' => 'Your email was not good']
  4128. ]
  4129. ];
  4130. $this->Form->create($this->article);
  4131. $result = $this->Form->error('Article.field');
  4132. $expected = [
  4133. ['div' => ['class' => 'error-message']],
  4134. 'Your email was not good',
  4135. '/div',
  4136. ];
  4137. $this->assertHtml($expected, $result);
  4138. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4139. $expected = [
  4140. ['div' => ['class' => 'error-message']],
  4141. 'Email in use',
  4142. '/div',
  4143. ];
  4144. $this->assertHtml($expected, $result);
  4145. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4146. $expected = [
  4147. ['div' => ['class' => 'error-message']],
  4148. 'Email in use',
  4149. '/div',
  4150. ];
  4151. $this->assertHtml($expected, $result);
  4152. $result = $this->Form->error('Article.field', [
  4153. 'email' => 'Key is preferred',
  4154. 'Your email was not good' => 'Email in use'
  4155. ]);
  4156. $expected = [
  4157. ['div' => ['class' => 'error-message']],
  4158. 'Key is preferred',
  4159. '/div',
  4160. ];
  4161. $this->assertHtml($expected, $result);
  4162. }
  4163. /**
  4164. * testErrorMessages method
  4165. *
  4166. * Test error with nested lists.
  4167. *
  4168. * @return void
  4169. */
  4170. public function testErrorMessages()
  4171. {
  4172. $this->article['errors'] = [
  4173. 'Article' => ['field' => 'email']
  4174. ];
  4175. $this->Form->create($this->article);
  4176. $result = $this->Form->error('Article.field', [
  4177. 'email' => 'No good!'
  4178. ]);
  4179. $expected = [
  4180. 'div' => ['class' => 'error-message'],
  4181. 'No good!',
  4182. '/div'
  4183. ];
  4184. $this->assertHtml($expected, $result);
  4185. }
  4186. /**
  4187. * testErrorMultipleMessages method
  4188. *
  4189. * Test error() with multiple messages.
  4190. *
  4191. * @return void
  4192. */
  4193. public function testErrorMultipleMessages()
  4194. {
  4195. $this->article['errors'] = [
  4196. 'field' => ['notBlank', 'email', 'Something else']
  4197. ];
  4198. $this->Form->create($this->article);
  4199. $result = $this->Form->error('field', [
  4200. 'notBlank' => 'Cannot be empty',
  4201. 'email' => 'No good!'
  4202. ]);
  4203. $expected = [
  4204. 'div' => ['class' => 'error-message'],
  4205. 'ul' => [],
  4206. '<li', 'Cannot be empty', '/li',
  4207. '<li', 'No good!', '/li',
  4208. '<li', 'Something else', '/li',
  4209. '/ul',
  4210. '/div'
  4211. ];
  4212. $this->assertHtml($expected, $result);
  4213. }
  4214. /**
  4215. * testPassword method
  4216. *
  4217. * Test password element generation.
  4218. *
  4219. * @return void
  4220. */
  4221. public function testPassword()
  4222. {
  4223. $this->article['errors'] = [
  4224. 'Contact' => [
  4225. 'passwd' => 1
  4226. ]
  4227. ];
  4228. $this->Form->create($this->article);
  4229. $result = $this->Form->password('Contact.field');
  4230. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4231. $this->assertHtml($expected, $result);
  4232. $this->Form->request = $this->Form->request->withData('Contact.passwd', 'test');
  4233. $this->Form->create($this->article);
  4234. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4235. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4236. $this->assertHtml($expected, $result);
  4237. }
  4238. /**
  4239. * testRadio method
  4240. *
  4241. * Test radio element set generation.
  4242. *
  4243. * @return void
  4244. */
  4245. public function testRadio()
  4246. {
  4247. $result = $this->Form->radio('Model.field', ['option A']);
  4248. $expected = [
  4249. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4250. 'label' => ['for' => 'model-field-0'],
  4251. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4252. 'option A',
  4253. '/label'
  4254. ];
  4255. $this->assertHtml($expected, $result);
  4256. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4257. $this->assertHtml($expected, $result);
  4258. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4259. $expected = [
  4260. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4261. ['label' => ['for' => 'model-field-0']],
  4262. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4263. 'option A',
  4264. '/label',
  4265. ['label' => ['for' => 'model-field-1']],
  4266. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4267. 'option B',
  4268. '/label',
  4269. ];
  4270. $this->assertHtml($expected, $result);
  4271. $result = $this->Form->radio(
  4272. 'Employee.gender',
  4273. ['male' => 'Male', 'female' => 'Female'],
  4274. ['form' => 'my-form']
  4275. );
  4276. $expected = [
  4277. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4278. ['label' => ['for' => 'employee-gender-male']],
  4279. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4280. 'Male',
  4281. '/label',
  4282. ['label' => ['for' => 'employee-gender-female']],
  4283. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4284. 'Female',
  4285. '/label',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4289. $expected = [
  4290. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4291. ['label' => ['for' => 'model-custom-0']],
  4292. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4293. 'option A',
  4294. '/label',
  4295. ['label' => ['for' => 'model-custom-1']],
  4296. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4297. 'option B',
  4298. '/label',
  4299. ];
  4300. $this->assertHtml($expected, $result);
  4301. $result = $this->Form->radio(
  4302. 'Employee.gender',
  4303. [
  4304. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4305. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4306. ]
  4307. );
  4308. $expected = [
  4309. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4310. ['label' => ['for' => 'employee-gender-male']],
  4311. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4312. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4313. 'Male',
  4314. '/label',
  4315. ['label' => ['for' => 'employee-gender-female']],
  4316. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4317. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4318. 'Female',
  4319. '/label',
  4320. ];
  4321. $this->assertHtml($expected, $result);
  4322. }
  4323. /**
  4324. * Test radio with complex options and empty disabled data.
  4325. *
  4326. * @return void
  4327. */
  4328. public function testRadioComplexDisabled()
  4329. {
  4330. $options = [
  4331. ['value' => 'r', 'text' => 'red'],
  4332. ['value' => 'b', 'text' => 'blue'],
  4333. ];
  4334. $attrs = ['disabled' => []];
  4335. $result = $this->Form->radio('Model.field', $options, $attrs);
  4336. $expected = [
  4337. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4338. ['label' => ['for' => 'model-field-r']],
  4339. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4340. 'red',
  4341. '/label',
  4342. ['label' => ['for' => 'model-field-b']],
  4343. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4344. 'blue',
  4345. '/label',
  4346. ];
  4347. $this->assertHtml($expected, $result);
  4348. $attrs = ['disabled' => ['r']];
  4349. $result = $this->Form->radio('Model.field', $options, $attrs);
  4350. $this->assertContains('disabled="disabled"', $result);
  4351. }
  4352. /**
  4353. * testRadioDefaultValue method
  4354. *
  4355. * Test default value setting on radio() method.
  4356. *
  4357. * @return void
  4358. */
  4359. public function testRadioDefaultValue()
  4360. {
  4361. $Articles = TableRegistry::get('Articles');
  4362. $title = $Articles->getSchema()->getColumn('title');
  4363. $Articles->getSchema()->addColumn(
  4364. 'title',
  4365. ['default' => '1'] + $title
  4366. );
  4367. $this->Form->create($Articles->newEntity());
  4368. $result = $this->Form->radio('title', ['option A', 'option B']);
  4369. $expected = [
  4370. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4371. ['label' => ['for' => 'title-0']],
  4372. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4373. 'option A',
  4374. '/label',
  4375. ['label' => ['for' => 'title-1']],
  4376. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4377. 'option B',
  4378. '/label',
  4379. ];
  4380. $this->assertHtml($expected, $result);
  4381. }
  4382. /**
  4383. * Test setting a hiddenField value on radio buttons.
  4384. *
  4385. * @return void
  4386. */
  4387. public function testRadioHiddenFieldValue()
  4388. {
  4389. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4390. $expected = [
  4391. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4392. 'label' => ['for' => 'title-0'],
  4393. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4394. 'option A',
  4395. '/label',
  4396. ];
  4397. $this->assertHtml($expected, $result);
  4398. }
  4399. /**
  4400. * testControlRadio method
  4401. *
  4402. * Test that input works with radio types.
  4403. *
  4404. * @return void
  4405. */
  4406. public function testControlRadio()
  4407. {
  4408. $result = $this->Form->control('test', [
  4409. 'type' => 'radio',
  4410. 'options' => ['A', 'B'],
  4411. ]);
  4412. $expected = [
  4413. ['div' => ['class' => 'input radio']],
  4414. '<label',
  4415. 'Test',
  4416. '/label',
  4417. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4418. ['label' => ['for' => 'test-0']],
  4419. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4420. 'A',
  4421. '/label',
  4422. ['label' => ['for' => 'test-1']],
  4423. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4424. 'B',
  4425. '/label',
  4426. '/div',
  4427. ];
  4428. $this->assertHtml($expected, $result);
  4429. $result = $this->Form->control('test', [
  4430. 'type' => 'radio',
  4431. 'options' => ['A', 'B'],
  4432. 'value' => '0'
  4433. ]);
  4434. $expected = [
  4435. ['div' => ['class' => 'input radio']],
  4436. '<label',
  4437. 'Test',
  4438. '/label',
  4439. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4440. ['label' => ['for' => 'test-0']],
  4441. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4442. 'A',
  4443. '/label',
  4444. ['label' => ['for' => 'test-1']],
  4445. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4446. 'B',
  4447. '/label',
  4448. '/div',
  4449. ];
  4450. $this->assertHtml($expected, $result);
  4451. $result = $this->Form->control('test', [
  4452. 'type' => 'radio',
  4453. 'options' => ['A', 'B'],
  4454. 'label' => false
  4455. ]);
  4456. $expected = [
  4457. ['div' => ['class' => 'input radio']],
  4458. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4459. ['label' => ['for' => 'test-0']],
  4460. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4461. 'A',
  4462. '/label',
  4463. ['label' => ['for' => 'test-1']],
  4464. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4465. 'B',
  4466. '/label',
  4467. '/div',
  4468. ];
  4469. $this->assertHtml($expected, $result);
  4470. }
  4471. /**
  4472. * testRadioNoLabel method
  4473. *
  4474. * Test that radio() works with label = false.
  4475. *
  4476. * @return void
  4477. */
  4478. public function testRadioNoLabel()
  4479. {
  4480. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4481. $expected = [
  4482. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4483. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4484. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4485. ];
  4486. $this->assertHtml($expected, $result);
  4487. }
  4488. /**
  4489. * testRadioControlInsideLabel method
  4490. *
  4491. * Test generating radio input inside label ala twitter bootstrap.
  4492. *
  4493. * @return void
  4494. */
  4495. public function testRadioControlInsideLabel()
  4496. {
  4497. $this->Form->setTemplates([
  4498. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4499. 'radioWrapper' => '{{label}}'
  4500. ]);
  4501. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4502. //@codingStandardsIgnoreStart
  4503. $expected = [
  4504. ['input' => [
  4505. 'type' => 'hidden',
  4506. 'name' => 'Model[field]',
  4507. 'value' => ''
  4508. ]],
  4509. ['label' => ['for' => 'model-field-0']],
  4510. ['input' => [
  4511. 'type' => 'radio',
  4512. 'name' => 'Model[field]',
  4513. 'value' => '0',
  4514. 'id' => 'model-field-0'
  4515. ]],
  4516. 'option A',
  4517. '/label',
  4518. ['label' => ['for' => 'model-field-1']],
  4519. ['input' => [
  4520. 'type' => 'radio',
  4521. 'name' => 'Model[field]',
  4522. 'value' => '1',
  4523. 'id' => 'model-field-1'
  4524. ]],
  4525. 'option B',
  4526. '/label',
  4527. ];
  4528. //@codingStandardsIgnoreEnd
  4529. $this->assertHtml($expected, $result);
  4530. }
  4531. /**
  4532. * testRadioHiddenControlDisabling method
  4533. *
  4534. * Test disabling the hidden input for radio buttons.
  4535. *
  4536. * @return void
  4537. */
  4538. public function testRadioHiddenControlDisabling()
  4539. {
  4540. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4541. $expected = [
  4542. 'label' => ['for' => 'model-1-field-0'],
  4543. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4544. 'option A',
  4545. '/label'
  4546. ];
  4547. $this->assertHtml($expected, $result);
  4548. }
  4549. /**
  4550. * testRadioOutOfRange method
  4551. *
  4552. * Test radio element set generation.
  4553. *
  4554. * @return void
  4555. */
  4556. public function testRadioOutOfRange()
  4557. {
  4558. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4559. $expected = [
  4560. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4561. 'label' => ['for' => 'model-field-v'],
  4562. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4563. 'value',
  4564. '/label'
  4565. ];
  4566. $this->assertHtml($expected, $result);
  4567. }
  4568. /**
  4569. * testSelect method
  4570. *
  4571. * Test select element generation.
  4572. *
  4573. * @return void
  4574. */
  4575. public function testSelect()
  4576. {
  4577. $result = $this->Form->select('Model.field', []);
  4578. $expected = [
  4579. 'select' => ['name' => 'Model[field]'],
  4580. '/select'
  4581. ];
  4582. $this->assertHtml($expected, $result);
  4583. $this->Form->request = $this->Form->request->withData('Model', ['field' => 'value']);
  4584. $this->Form->create();
  4585. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4586. $expected = [
  4587. 'select' => ['name' => 'Model[field]'],
  4588. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4589. 'good',
  4590. '/option',
  4591. ['option' => ['value' => 'other']],
  4592. 'bad',
  4593. '/option',
  4594. '/select'
  4595. ];
  4596. $this->assertHtml($expected, $result);
  4597. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4598. $this->assertHtml($expected, $result);
  4599. $this->Form->request = $this->Form->request->withParsedBody([]);
  4600. $this->Form->create();
  4601. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4602. $expected = [
  4603. 'select' => ['name' => 'Model[field]'],
  4604. ['option' => ['value' => 'value']],
  4605. 'good',
  4606. '/option',
  4607. ['option' => ['value' => 'other']],
  4608. 'bad',
  4609. '/option',
  4610. '/select'
  4611. ];
  4612. $this->assertHtml($expected, $result);
  4613. $options = [
  4614. ['value' => 'first', 'text' => 'First'],
  4615. ['value' => 'first', 'text' => 'Another First'],
  4616. ];
  4617. $result = $this->Form->select(
  4618. 'Model.field',
  4619. $options,
  4620. ['escape' => false, 'empty' => false]
  4621. );
  4622. $expected = [
  4623. 'select' => ['name' => 'Model[field]'],
  4624. ['option' => ['value' => 'first']],
  4625. 'First',
  4626. '/option',
  4627. ['option' => ['value' => 'first']],
  4628. 'Another First',
  4629. '/option',
  4630. '/select'
  4631. ];
  4632. $this->assertHtml($expected, $result);
  4633. $this->Form->request = $this->Form->request->withParsedBody(['Model' => ['contact_id' => 228]]);
  4634. $this->Form->create();
  4635. $result = $this->Form->select(
  4636. 'Model.contact_id',
  4637. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4638. ['escape' => false, 'empty' => 'pick something']
  4639. );
  4640. $expected = [
  4641. 'select' => ['name' => 'Model[contact_id]'],
  4642. ['option' => ['value' => '']], 'pick something', '/option',
  4643. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4644. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4645. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4646. '/select'
  4647. ];
  4648. $this->assertHtml($expected, $result);
  4649. $this->Form->request = $this->Form->request->withData('Model.field', 0);
  4650. $this->Form->create();
  4651. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4652. $expected = [
  4653. 'select' => ['name' => 'Model[field]'],
  4654. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4655. ['option' => ['value' => '1']], 'Yes', '/option',
  4656. '/select'
  4657. ];
  4658. $this->assertHtml($expected, $result);
  4659. }
  4660. /**
  4661. * testSelectEscapeHtml method
  4662. *
  4663. * Test that select() escapes HTML.
  4664. *
  4665. * @return void
  4666. */
  4667. public function testSelectEscapeHtml()
  4668. {
  4669. $result = $this->Form->select(
  4670. 'Model.field',
  4671. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4672. ['empty' => false]
  4673. );
  4674. $expected = [
  4675. 'select' => ['name' => 'Model[field]'],
  4676. ['option' => ['value' => 'first']],
  4677. 'first &quot;html&quot; &lt;chars&gt;',
  4678. '/option',
  4679. ['option' => ['value' => 'second']],
  4680. 'value',
  4681. '/option',
  4682. '/select'
  4683. ];
  4684. $this->assertHtml($expected, $result);
  4685. $result = $this->Form->select(
  4686. 'Model.field',
  4687. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4688. ['escape' => false, 'empty' => false]
  4689. );
  4690. $expected = [
  4691. 'select' => ['name' => 'Model[field]'],
  4692. ['option' => ['value' => 'first']],
  4693. 'first "html" <chars>',
  4694. '/option',
  4695. ['option' => ['value' => 'second']],
  4696. 'value',
  4697. '/option',
  4698. '/select'
  4699. ];
  4700. $this->assertHtml($expected, $result);
  4701. }
  4702. /**
  4703. * testSelectRequired method
  4704. *
  4705. * Test select() with required and disabled attributes.
  4706. *
  4707. * @return void
  4708. */
  4709. public function testSelectRequired()
  4710. {
  4711. $this->article['required'] = [
  4712. 'user_id' => true
  4713. ];
  4714. $this->Form->create($this->article);
  4715. $result = $this->Form->select('user_id', ['option A']);
  4716. $expected = [
  4717. 'select' => [
  4718. 'name' => 'user_id',
  4719. 'required' => 'required'
  4720. ],
  4721. ['option' => ['value' => '0']], 'option A', '/option',
  4722. '/select'
  4723. ];
  4724. $this->assertHtml($expected, $result);
  4725. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4726. $expected = [
  4727. 'select' => [
  4728. 'name' => 'user_id',
  4729. 'disabled' => 'disabled'
  4730. ],
  4731. ['option' => ['value' => '0']], 'option A', '/option',
  4732. '/select'
  4733. ];
  4734. $this->assertHtml($expected, $result);
  4735. }
  4736. /**
  4737. * testNestedSelect method
  4738. *
  4739. * Test select element generation with optgroups.
  4740. *
  4741. * @return void
  4742. */
  4743. public function testNestedSelect()
  4744. {
  4745. $result = $this->Form->select(
  4746. 'Model.field',
  4747. [1 => 'One', 2 => 'Two', 'Three' => [
  4748. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4749. ]],
  4750. ['empty' => false]
  4751. );
  4752. $expected = [
  4753. 'select' => ['name' => 'Model[field]'],
  4754. ['option' => ['value' => 1]],
  4755. 'One',
  4756. '/option',
  4757. ['option' => ['value' => 2]],
  4758. 'Two',
  4759. '/option',
  4760. ['optgroup' => ['label' => 'Three']],
  4761. ['option' => ['value' => 3]],
  4762. 'Three',
  4763. '/option',
  4764. ['option' => ['value' => 4]],
  4765. 'Four',
  4766. '/option',
  4767. ['option' => ['value' => 5]],
  4768. 'Five',
  4769. '/option',
  4770. '/optgroup',
  4771. '/select'
  4772. ];
  4773. $this->assertHtml($expected, $result);
  4774. }
  4775. /**
  4776. * testSelectMultiple method
  4777. *
  4778. * Test generation of multiple select elements.
  4779. *
  4780. * @return void
  4781. */
  4782. public function testSelectMultiple()
  4783. {
  4784. $options = ['first', 'second', 'third'];
  4785. $result = $this->Form->select(
  4786. 'Model.multi_field',
  4787. $options,
  4788. ['form' => 'my-form', 'multiple' => true]
  4789. );
  4790. $expected = [
  4791. 'input' => [
  4792. 'type' => 'hidden',
  4793. 'name' => 'Model[multi_field]',
  4794. 'value' => '',
  4795. 'form' => 'my-form',
  4796. ],
  4797. 'select' => [
  4798. 'name' => 'Model[multi_field][]',
  4799. 'multiple' => 'multiple',
  4800. 'form' => 'my-form',
  4801. ],
  4802. ['option' => ['value' => '0']],
  4803. 'first',
  4804. '/option',
  4805. ['option' => ['value' => '1']],
  4806. 'second',
  4807. '/option',
  4808. ['option' => ['value' => '2']],
  4809. 'third',
  4810. '/option',
  4811. '/select'
  4812. ];
  4813. $this->assertHtml($expected, $result);
  4814. $result = $this->Form->select(
  4815. 'Model.multi_field',
  4816. $options,
  4817. ['multiple' => 'multiple', 'form' => 'my-form']
  4818. );
  4819. $this->assertHtml($expected, $result);
  4820. }
  4821. /**
  4822. * testCheckboxZeroValue method
  4823. *
  4824. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4825. *
  4826. * @return void
  4827. */
  4828. public function testCheckboxZeroValue()
  4829. {
  4830. $result = $this->Form->control('User.get_spam', [
  4831. 'type' => 'checkbox',
  4832. 'value' => '0',
  4833. 'hiddenField' => '1',
  4834. ]);
  4835. $expected = [
  4836. 'div' => ['class' => 'input checkbox'],
  4837. 'label' => ['for' => 'user-get-spam'],
  4838. ['input' => [
  4839. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4840. 'value' => '1'
  4841. ]],
  4842. ['input' => [
  4843. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4844. 'value' => '0', 'id' => 'user-get-spam'
  4845. ]],
  4846. 'Get Spam',
  4847. '/label',
  4848. '/div'
  4849. ];
  4850. $this->assertHtml($expected, $result);
  4851. }
  4852. /**
  4853. * testHabtmSelectBox method
  4854. *
  4855. * Test generation of habtm select boxes.
  4856. *
  4857. * @return void
  4858. */
  4859. public function testHabtmSelectBox()
  4860. {
  4861. $this->loadFixtures('Articles');
  4862. $options = [
  4863. 1 => 'blue',
  4864. 2 => 'red',
  4865. 3 => 'green'
  4866. ];
  4867. $tags = [
  4868. new Entity(['id' => 1, 'name' => 'blue']),
  4869. new Entity(['id' => 3, 'name' => 'green'])
  4870. ];
  4871. $article = new Article(['tags' => $tags]);
  4872. $this->Form->create($article);
  4873. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4874. $expected = [
  4875. 'div' => ['class' => 'input select'],
  4876. 'label' => ['for' => 'tags-ids'],
  4877. 'Tags',
  4878. '/label',
  4879. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4880. 'select' => [
  4881. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4882. 'multiple' => 'multiple'
  4883. ],
  4884. ['option' => ['value' => '1', 'selected' => 'selected']],
  4885. 'blue',
  4886. '/option',
  4887. ['option' => ['value' => '2']],
  4888. 'red',
  4889. '/option',
  4890. ['option' => ['value' => '3', 'selected' => 'selected']],
  4891. 'green',
  4892. '/option',
  4893. '/select',
  4894. '/div'
  4895. ];
  4896. $this->assertHtml($expected, $result);
  4897. // make sure only 50 is selected, and not 50f5c0cf
  4898. $options = [
  4899. '1' => 'blue',
  4900. '50f5c0cf' => 'red',
  4901. '50' => 'green'
  4902. ];
  4903. $tags = [
  4904. new Entity(['id' => 1, 'name' => 'blue']),
  4905. new Entity(['id' => 50, 'name' => 'green'])
  4906. ];
  4907. $article = new Article(['tags' => $tags]);
  4908. $this->Form->create($article);
  4909. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4910. $expected = [
  4911. 'div' => ['class' => 'input select'],
  4912. 'label' => ['for' => 'tags-ids'],
  4913. 'Tags',
  4914. '/label',
  4915. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4916. 'select' => [
  4917. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4918. 'multiple' => 'multiple'
  4919. ],
  4920. ['option' => ['value' => '1', 'selected' => 'selected']],
  4921. 'blue',
  4922. '/option',
  4923. ['option' => ['value' => '50f5c0cf']],
  4924. 'red',
  4925. '/option',
  4926. ['option' => ['value' => '50', 'selected' => 'selected']],
  4927. 'green',
  4928. '/option',
  4929. '/select',
  4930. '/div'
  4931. ];
  4932. $this->assertHtml($expected, $result);
  4933. $spacecraft = [
  4934. 1 => 'Orion',
  4935. 2 => 'Helios'
  4936. ];
  4937. $this->View->viewVars['spacecraft'] = $spacecraft;
  4938. $this->Form->create();
  4939. $result = $this->Form->control('spacecraft._ids');
  4940. $expected = [
  4941. 'div' => ['class' => 'input select'],
  4942. 'label' => ['for' => 'spacecraft-ids'],
  4943. 'Spacecraft',
  4944. '/label',
  4945. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4946. 'select' => [
  4947. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4948. 'multiple' => 'multiple'
  4949. ],
  4950. ['option' => ['value' => '1']],
  4951. 'Orion',
  4952. '/option',
  4953. ['option' => ['value' => '2']],
  4954. 'Helios',
  4955. '/option',
  4956. '/select',
  4957. '/div'
  4958. ];
  4959. $this->assertHtml($expected, $result);
  4960. }
  4961. /**
  4962. * testErrorsForBelongsToManySelect method
  4963. *
  4964. * Tests that errors for belongsToMany select fields are being
  4965. * picked up properly.
  4966. *
  4967. * @return void
  4968. */
  4969. public function testErrorsForBelongsToManySelect()
  4970. {
  4971. $spacecraft = [
  4972. 1 => 'Orion',
  4973. 2 => 'Helios'
  4974. ];
  4975. $this->View->viewVars['spacecraft'] = $spacecraft;
  4976. $article = new Article();
  4977. $article->setError('spacecraft', ['Invalid']);
  4978. $this->Form->create($article);
  4979. $result = $this->Form->control('spacecraft._ids');
  4980. $expected = [
  4981. ['div' => ['class' => 'input select error']],
  4982. 'label' => ['for' => 'spacecraft-ids'],
  4983. 'Spacecraft',
  4984. '/label',
  4985. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4986. 'select' => [
  4987. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4988. 'multiple' => 'multiple'
  4989. ],
  4990. ['option' => ['value' => '1']],
  4991. 'Orion',
  4992. '/option',
  4993. ['option' => ['value' => '2']],
  4994. 'Helios',
  4995. '/option',
  4996. '/select',
  4997. ['div' => ['class' => 'error-message']],
  4998. 'Invalid',
  4999. '/div',
  5000. '/div'
  5001. ];
  5002. $this->assertHtml($expected, $result);
  5003. }
  5004. /**
  5005. * testSelectMultipleCheckboxes method
  5006. *
  5007. * Test generation of multi select elements in checkbox format.
  5008. *
  5009. * @return void
  5010. */
  5011. public function testSelectMultipleCheckboxes()
  5012. {
  5013. $result = $this->Form->select(
  5014. 'Model.multi_field',
  5015. ['first', 'second', 'third'],
  5016. ['multiple' => 'checkbox']
  5017. );
  5018. $expected = [
  5019. 'input' => [
  5020. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5021. ],
  5022. ['div' => ['class' => 'checkbox']],
  5023. ['label' => ['for' => 'model-multi-field-0']],
  5024. ['input' => [
  5025. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5026. 'value' => '0', 'id' => 'model-multi-field-0'
  5027. ]],
  5028. 'first',
  5029. '/label',
  5030. '/div',
  5031. ['div' => ['class' => 'checkbox']],
  5032. ['label' => ['for' => 'model-multi-field-1']],
  5033. ['input' => [
  5034. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5035. 'value' => '1', 'id' => 'model-multi-field-1'
  5036. ]],
  5037. 'second',
  5038. '/label',
  5039. '/div',
  5040. ['div' => ['class' => 'checkbox']],
  5041. ['label' => ['for' => 'model-multi-field-2']],
  5042. ['input' => [
  5043. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5044. 'value' => '2', 'id' => 'model-multi-field-2'
  5045. ]],
  5046. 'third',
  5047. '/label',
  5048. '/div'
  5049. ];
  5050. $this->assertHtml($expected, $result);
  5051. $result = $this->Form->select(
  5052. 'Model.multi_field',
  5053. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5054. ['multiple' => 'checkbox']
  5055. );
  5056. $expected = [
  5057. 'input' => [
  5058. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5059. ],
  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. 'first',
  5067. '/label',
  5068. '/div',
  5069. ['div' => ['class' => 'checkbox']],
  5070. ['label' => ['for' => 'model-multi-field-a++']],
  5071. ['input' => [
  5072. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5073. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5074. ]],
  5075. 'second',
  5076. '/label',
  5077. '/div',
  5078. ['div' => ['class' => 'checkbox']],
  5079. ['label' => ['for' => 'model-multi-field-a+++']],
  5080. ['input' => [
  5081. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5082. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5083. ]],
  5084. 'third',
  5085. '/label',
  5086. '/div'
  5087. ];
  5088. $this->assertHtml($expected, $result);
  5089. $result = $this->Form->select(
  5090. 'Model.multi_field',
  5091. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5092. ['multiple' => 'checkbox']
  5093. );
  5094. $expected = [
  5095. 'input' => [
  5096. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5097. ],
  5098. ['div' => ['class' => 'checkbox']],
  5099. ['label' => ['for' => 'model-multi-field-a-b']],
  5100. ['input' => [
  5101. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5102. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5103. ]],
  5104. 'first',
  5105. '/label',
  5106. '/div',
  5107. ['div' => ['class' => 'checkbox']],
  5108. ['label' => ['for' => 'model-multi-field-a-b1']],
  5109. ['input' => [
  5110. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5111. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5112. ]],
  5113. 'second',
  5114. '/label',
  5115. '/div',
  5116. ['div' => ['class' => 'checkbox']],
  5117. ['label' => ['for' => 'model-multi-field-a-b2']],
  5118. ['input' => [
  5119. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5120. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5121. ]],
  5122. 'third',
  5123. '/label',
  5124. '/div'
  5125. ];
  5126. $this->assertHtml($expected, $result);
  5127. }
  5128. /**
  5129. * testSelectMultipleCheckboxRequestData method
  5130. *
  5131. * Ensure that multiCheckbox reads from the request data.
  5132. *
  5133. * @return void
  5134. */
  5135. public function testSelectMultipleCheckboxRequestData()
  5136. {
  5137. $this->Form->request = $this->Form->request->withData('Model', ['tags' => [1]]);
  5138. $result = $this->Form->select(
  5139. 'Model.tags',
  5140. ['1' => 'first', 'Array' => 'Array'],
  5141. ['multiple' => 'checkbox']
  5142. );
  5143. $expected = [
  5144. 'input' => [
  5145. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5146. ],
  5147. ['div' => ['class' => 'checkbox']],
  5148. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5149. ['input' => [
  5150. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5151. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5152. ]],
  5153. 'first',
  5154. '/label',
  5155. '/div',
  5156. ['div' => ['class' => 'checkbox']],
  5157. ['label' => ['for' => 'model-tags-array']],
  5158. ['input' => [
  5159. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5160. 'value' => 'Array', 'id' => 'model-tags-array'
  5161. ]],
  5162. 'Array',
  5163. '/label',
  5164. '/div'
  5165. ];
  5166. $this->assertHtml($expected, $result);
  5167. }
  5168. /**
  5169. * testSelectMultipleCheckboxSecurity method
  5170. *
  5171. * Checks the security hash array generated for multiple-input checkbox elements.
  5172. *
  5173. * @return void
  5174. */
  5175. public function testSelectMultipleCheckboxSecurity()
  5176. {
  5177. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  5178. $this->assertEquals([], $this->Form->fields);
  5179. $this->Form->select(
  5180. 'Model.multi_field',
  5181. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5182. ['multiple' => 'checkbox']
  5183. );
  5184. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5185. $result = $this->Form->secure($this->Form->fields);
  5186. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5187. $this->assertContains('"' . $key . '"', $result);
  5188. }
  5189. /**
  5190. * testSelectMultipleSecureWithNoOptions method
  5191. *
  5192. * Multiple select elements should always be secured as they always participate
  5193. * in the POST data.
  5194. *
  5195. * @return void
  5196. */
  5197. public function testSelectMultipleSecureWithNoOptions()
  5198. {
  5199. $this->assertEquals([], $this->Form->fields);
  5200. $this->Form->select(
  5201. 'Model.select',
  5202. [],
  5203. ['multiple' => true]
  5204. );
  5205. $this->assertEquals(['Model.select'], $this->Form->fields);
  5206. }
  5207. /**
  5208. * testSelectNoSecureWithNoOptions method
  5209. *
  5210. * When a select box has no options it should not be added to the fields list
  5211. * as it always fail post validation.
  5212. *
  5213. * @return void
  5214. */
  5215. public function testSelectNoSecureWithNoOptions()
  5216. {
  5217. $this->Form->request = $this->Form->request->withParam('_Token', 'testkey');
  5218. $this->assertEquals([], $this->Form->fields);
  5219. $this->Form->select(
  5220. 'Model.select',
  5221. []
  5222. );
  5223. $this->assertEquals([], $this->Form->fields);
  5224. $this->Form->select(
  5225. 'Model.user_id',
  5226. [],
  5227. ['empty' => true]
  5228. );
  5229. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5230. }
  5231. /**
  5232. * testControlMultipleCheckboxes method
  5233. *
  5234. * Test control() resulting in multi select elements being generated.
  5235. *
  5236. * @return void
  5237. */
  5238. public function testControlMultipleCheckboxes()
  5239. {
  5240. $result = $this->Form->control('Model.multi_field', [
  5241. 'options' => ['first', 'second', 'third'],
  5242. 'multiple' => 'checkbox'
  5243. ]);
  5244. $expected = [
  5245. ['div' => ['class' => 'input select']],
  5246. ['label' => ['for' => 'model-multi-field']],
  5247. 'Multi Field',
  5248. '/label',
  5249. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5250. ['div' => ['class' => 'checkbox']],
  5251. ['label' => ['for' => 'model-multi-field-0']],
  5252. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5253. 'first',
  5254. '/label',
  5255. '/div',
  5256. ['div' => ['class' => 'checkbox']],
  5257. ['label' => ['for' => 'model-multi-field-1']],
  5258. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5259. 'second',
  5260. '/label',
  5261. '/div',
  5262. ['div' => ['class' => 'checkbox']],
  5263. ['label' => ['for' => 'model-multi-field-2']],
  5264. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5265. 'third',
  5266. '/label',
  5267. '/div',
  5268. '/div'
  5269. ];
  5270. $this->assertHtml($expected, $result);
  5271. $result = $this->Form->control('Model.multi_field', [
  5272. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5273. 'multiple' => 'checkbox'
  5274. ]);
  5275. $expected = [
  5276. ['div' => ['class' => 'input select']],
  5277. ['label' => ['for' => 'model-multi-field']],
  5278. 'Multi Field',
  5279. '/label',
  5280. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5281. ['div' => ['class' => 'checkbox']],
  5282. ['label' => ['for' => 'model-multi-field-a']],
  5283. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5284. 'first',
  5285. '/label',
  5286. '/div',
  5287. ['div' => ['class' => 'checkbox']],
  5288. ['label' => ['for' => 'model-multi-field-b']],
  5289. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5290. 'second',
  5291. '/label',
  5292. '/div',
  5293. ['div' => ['class' => 'checkbox']],
  5294. ['label' => ['for' => 'model-multi-field-c']],
  5295. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5296. 'third',
  5297. '/label',
  5298. '/div',
  5299. '/div'
  5300. ];
  5301. $this->assertHtml($expected, $result);
  5302. }
  5303. /**
  5304. * testSelectHiddenFieldOmission method
  5305. *
  5306. * Test that select() with 'hiddenField' => false omits the hidden field.
  5307. *
  5308. * @return void
  5309. */
  5310. public function testSelectHiddenFieldOmission()
  5311. {
  5312. $result = $this->Form->select(
  5313. 'Model.multi_field',
  5314. ['first', 'second'],
  5315. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5316. );
  5317. $this->assertNotContains('type="hidden"', $result);
  5318. }
  5319. /**
  5320. * testSelectCheckboxMultipleOverrideName method
  5321. *
  5322. * Test that select() with multiple = checkbox works with overriding name attribute.
  5323. *
  5324. * @return void
  5325. */
  5326. public function testSelectCheckboxMultipleOverrideName()
  5327. {
  5328. $result = $this->Form->select('category', ['1', '2'], [
  5329. 'multiple' => 'checkbox',
  5330. 'name' => 'fish',
  5331. ]);
  5332. $expected = [
  5333. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5334. ['div' => ['class' => 'checkbox']],
  5335. ['label' => ['for' => 'fish-0']],
  5336. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5337. '1',
  5338. '/label',
  5339. '/div',
  5340. ['div' => ['class' => 'checkbox']],
  5341. ['label' => ['for' => 'fish-1']],
  5342. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5343. '2',
  5344. '/label',
  5345. '/div'
  5346. ];
  5347. $this->assertHtml($expected, $result);
  5348. $result = $this->Form->multiCheckbox(
  5349. 'category',
  5350. new Collection(['1', '2']),
  5351. ['name' => 'fish']
  5352. );
  5353. $this->assertHtml($expected, $result);
  5354. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5355. 'name' => 'fish',
  5356. ]);
  5357. $this->assertHtml($expected, $result);
  5358. }
  5359. /**
  5360. * testControlMultiCheckbox method
  5361. *
  5362. * Test that control() works with multicheckbox.
  5363. *
  5364. * @return void
  5365. */
  5366. public function testControlMultiCheckbox()
  5367. {
  5368. $result = $this->Form->control('category', [
  5369. 'type' => 'multicheckbox',
  5370. 'options' => ['1', '2'],
  5371. ]);
  5372. $expected = [
  5373. ['div' => ['class' => 'input multicheckbox']],
  5374. '<label',
  5375. 'Category',
  5376. '/label',
  5377. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5378. ['div' => ['class' => 'checkbox']],
  5379. ['label' => ['for' => 'category-0']],
  5380. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5381. '1',
  5382. '/label',
  5383. '/div',
  5384. ['div' => ['class' => 'checkbox']],
  5385. ['label' => ['for' => 'category-1']],
  5386. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5387. '2',
  5388. '/label',
  5389. '/div',
  5390. '/div',
  5391. ];
  5392. $this->assertHtml($expected, $result);
  5393. }
  5394. /**
  5395. * testCheckbox method
  5396. *
  5397. * Test generation of checkboxes.
  5398. *
  5399. * @return void
  5400. */
  5401. public function testCheckbox()
  5402. {
  5403. $result = $this->Form->checkbox('Model.field');
  5404. $expected = [
  5405. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5406. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5407. ];
  5408. $this->assertHtml($expected, $result);
  5409. $result = $this->Form->checkbox('Model.field', [
  5410. 'id' => 'theID',
  5411. 'value' => 'myvalue',
  5412. 'form' => 'my-form',
  5413. ]);
  5414. $expected = [
  5415. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5416. ['input' => [
  5417. 'type' => 'checkbox', 'name' => 'Model[field]',
  5418. 'value' => 'myvalue', 'id' => 'theID',
  5419. 'form' => 'my-form',
  5420. ]]
  5421. ];
  5422. $this->assertHtml($expected, $result);
  5423. }
  5424. /**
  5425. * testCheckboxDefaultValue method
  5426. *
  5427. * Test default value setting on checkbox() method.
  5428. *
  5429. * @return void
  5430. */
  5431. public function testCheckboxDefaultValue()
  5432. {
  5433. $this->Form->request = $this->Form->request->withData('Model.field', false);
  5434. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5435. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5436. $this->assertHtml($expected, $result);
  5437. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5438. $this->Form->create();
  5439. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5440. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5441. $this->assertHtml($expected, $result);
  5442. $this->Form->request = $this->Form->request->withData('Model.field', true);
  5443. $this->Form->create();
  5444. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5445. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5446. $this->assertHtml($expected, $result);
  5447. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5448. $this->Form->create();
  5449. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5450. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5451. $this->assertHtml($expected, $result);
  5452. $Articles = TableRegistry::get('Articles');
  5453. $Articles->getSchema()->addColumn(
  5454. 'published',
  5455. ['type' => 'boolean', 'null' => false, 'default' => true]
  5456. );
  5457. $this->Form->create($Articles->newEntity());
  5458. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5459. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5460. $this->assertHtml($expected, $result);
  5461. }
  5462. /**
  5463. * testCheckboxCheckedAndError method
  5464. *
  5465. * Test checkbox being checked or having errors.
  5466. *
  5467. * @return void
  5468. */
  5469. public function testCheckboxCheckedAndError()
  5470. {
  5471. $this->article['errors'] = [
  5472. 'published' => true
  5473. ];
  5474. $this->Form->request = $this->Form->request->withData('published', 'myvalue');
  5475. $this->Form->create($this->article);
  5476. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5477. $expected = [
  5478. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5479. ['input' => [
  5480. 'type' => 'checkbox',
  5481. 'name' => 'published',
  5482. 'value' => 'myvalue',
  5483. 'id' => 'theID',
  5484. 'checked' => 'checked',
  5485. 'class' => 'form-error'
  5486. ]]
  5487. ];
  5488. $this->assertHtml($expected, $result);
  5489. $this->Form->request = $this->Form->request->withData('published', '');
  5490. $this->Form->create($this->article);
  5491. $result = $this->Form->checkbox('published');
  5492. $expected = [
  5493. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5494. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5495. ];
  5496. $this->assertHtml($expected, $result);
  5497. }
  5498. /**
  5499. * testCheckboxCustomNameAttribute method
  5500. *
  5501. * Test checkbox() with a custom name attribute.
  5502. *
  5503. * @return void
  5504. */
  5505. public function testCheckboxCustomNameAttribute()
  5506. {
  5507. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5508. $expected = [
  5509. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5510. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5511. ];
  5512. $this->assertHtml($expected, $result);
  5513. }
  5514. /**
  5515. * testCheckboxHiddenField method
  5516. *
  5517. * Test that the hidden input for checkboxes can be omitted or set to a
  5518. * specific value.
  5519. *
  5520. * @return void
  5521. */
  5522. public function testCheckboxHiddenField()
  5523. {
  5524. $result = $this->Form->checkbox('UserForm.something', [
  5525. 'hiddenField' => false
  5526. ]);
  5527. $expected = [
  5528. 'input' => [
  5529. 'type' => 'checkbox',
  5530. 'name' => 'UserForm[something]',
  5531. 'value' => '1'
  5532. ],
  5533. ];
  5534. $this->assertHtml($expected, $result);
  5535. $result = $this->Form->checkbox('UserForm.something', [
  5536. 'value' => 'Y',
  5537. 'hiddenField' => 'N',
  5538. ]);
  5539. $expected = [
  5540. ['input' => [
  5541. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5542. 'value' => 'N'
  5543. ]],
  5544. ['input' => [
  5545. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5546. 'value' => 'Y'
  5547. ]],
  5548. ];
  5549. $this->assertHtml($expected, $result);
  5550. }
  5551. /**
  5552. * testTime method
  5553. *
  5554. * Test the time type.
  5555. *
  5556. * @return void
  5557. */
  5558. public function testTime()
  5559. {
  5560. $result = $this->Form->time('start_time', [
  5561. 'timeFormat' => 12,
  5562. 'interval' => 5,
  5563. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5564. ]);
  5565. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5566. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5567. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5568. $this->assertNotContains('year', $result);
  5569. $this->assertNotContains('month', $result);
  5570. $this->assertNotContains('day', $result);
  5571. $result = $this->Form->time('start_time', [
  5572. 'timeFormat' => 12,
  5573. 'interval' => 5,
  5574. 'value' => '2014-03-08 16:30:00'
  5575. ]);
  5576. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5577. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5578. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5579. $this->assertNotContains('year', $result);
  5580. $this->assertNotContains('month', $result);
  5581. $this->assertNotContains('day', $result);
  5582. }
  5583. /**
  5584. * testTimeFormat24NoMeridian method
  5585. *
  5586. * Ensure that timeFormat=24 has no merdian.
  5587. *
  5588. * @return void.
  5589. */
  5590. public function testTimeFormat24NoMeridian()
  5591. {
  5592. $result = $this->Form->time('start_time', [
  5593. 'timeFormat' => 24,
  5594. 'interval' => 5,
  5595. 'value' => '2014-03-08 16:30:00'
  5596. ]);
  5597. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5598. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5599. $this->assertNotContains('meridian', $result);
  5600. $this->assertNotContains('pm', $result);
  5601. $this->assertNotContains('year', $result);
  5602. $this->assertNotContains('month', $result);
  5603. $this->assertNotContains('day', $result);
  5604. }
  5605. /**
  5606. * testDate method
  5607. *
  5608. * Test the date type.
  5609. *
  5610. * @return void
  5611. */
  5612. public function testDate()
  5613. {
  5614. $result = $this->Form->date('start_day', [
  5615. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5616. ]);
  5617. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5618. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5619. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5620. $this->assertNotContains('hour', $result);
  5621. $this->assertNotContains('minute', $result);
  5622. $this->assertNotContains('second', $result);
  5623. $this->assertNotContains('meridian', $result);
  5624. }
  5625. /**
  5626. * testDateTime method
  5627. *
  5628. * Test generation of date/time select elements.
  5629. *
  5630. * @return void
  5631. */
  5632. public function testDateTime()
  5633. {
  5634. extract($this->dateRegex);
  5635. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5636. $now = strtotime('now');
  5637. $expected = [
  5638. ['select' => ['name' => 'Contact[date][year]']],
  5639. ['option' => ['value' => '']],
  5640. '/option',
  5641. $yearsRegex,
  5642. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5643. date('Y', $now),
  5644. '/option',
  5645. '*/select',
  5646. ['select' => ['name' => 'Contact[date][month]']],
  5647. ['option' => ['value' => '']],
  5648. '/option',
  5649. $monthsRegex,
  5650. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5651. date('F', $now),
  5652. '/option',
  5653. '*/select',
  5654. ['select' => ['name' => 'Contact[date][day]']],
  5655. ['option' => ['value' => '']],
  5656. '/option',
  5657. $daysRegex,
  5658. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5659. date('j', $now),
  5660. '/option',
  5661. '*/select',
  5662. ['select' => ['name' => 'Contact[date][hour]']],
  5663. ['option' => ['value' => '']],
  5664. '/option',
  5665. $hoursRegex,
  5666. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5667. date('G', $now),
  5668. '/option',
  5669. '*/select',
  5670. ['select' => ['name' => 'Contact[date][minute]']],
  5671. ['option' => ['value' => '']],
  5672. '/option',
  5673. $minutesRegex,
  5674. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5675. date('i', $now),
  5676. '/option',
  5677. '*/select',
  5678. ];
  5679. $this->assertHtml($expected, $result);
  5680. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5681. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5682. $now = strtotime('now');
  5683. $expected = [
  5684. ['select' => ['name' => 'Contact[date][year]']],
  5685. $yearsRegex,
  5686. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5687. date('Y', $now),
  5688. '/option',
  5689. '*/select',
  5690. ['select' => ['name' => 'Contact[date][month]']],
  5691. $monthsRegex,
  5692. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5693. date('F', $now),
  5694. '/option',
  5695. '*/select',
  5696. ['select' => ['name' => 'Contact[date][day]']],
  5697. $daysRegex,
  5698. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5699. date('j', $now),
  5700. '/option',
  5701. '*/select',
  5702. ['select' => ['name' => 'Contact[date][hour]']],
  5703. $hoursRegex,
  5704. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5705. date('G', $now),
  5706. '/option',
  5707. '*/select',
  5708. ['select' => ['name' => 'Contact[date][minute]']],
  5709. $minutesRegex,
  5710. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5711. date('i', $now),
  5712. '/option',
  5713. '*/select',
  5714. ];
  5715. $this->assertHtml($expected, $result);
  5716. }
  5717. /**
  5718. * testDateTimeSecured method
  5719. *
  5720. * Test that datetime fields are added to protected fields list.
  5721. *
  5722. * @return void
  5723. */
  5724. public function testDateTimeSecured()
  5725. {
  5726. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5727. $this->Form->dateTime('Contact.date');
  5728. $expected = [
  5729. 'Contact.date.year',
  5730. 'Contact.date.month',
  5731. 'Contact.date.day',
  5732. 'Contact.date.hour',
  5733. 'Contact.date.minute',
  5734. ];
  5735. $this->assertEquals($expected, $this->Form->fields);
  5736. $this->Form->fields = [];
  5737. $this->Form->date('Contact.published');
  5738. $expected = [
  5739. 'Contact.published.year',
  5740. 'Contact.published.month',
  5741. 'Contact.published.day',
  5742. ];
  5743. $this->assertEquals($expected, $this->Form->fields);
  5744. }
  5745. /**
  5746. * testDateTimeSecuredDisabled method
  5747. *
  5748. * Test that datetime fields are added to protected fields list.
  5749. *
  5750. * @return void
  5751. */
  5752. public function testDateTimeSecuredDisabled()
  5753. {
  5754. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5755. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5756. $expected = [];
  5757. $this->assertEquals($expected, $this->Form->fields);
  5758. $this->Form->fields = [];
  5759. $this->Form->date('Contact.published', ['secure' => false]);
  5760. $expected = [];
  5761. $this->assertEquals($expected, $this->Form->fields);
  5762. }
  5763. /**
  5764. * testDatetimeEmpty method
  5765. *
  5766. * Test empty defaulting to true for datetime.
  5767. *
  5768. * @return void
  5769. */
  5770. public function testDatetimeEmpty()
  5771. {
  5772. extract($this->dateRegex);
  5773. $result = $this->Form->dateTime('Contact.date', [
  5774. 'timeFormat' => 12,
  5775. 'empty' => true,
  5776. 'default' => true
  5777. ]);
  5778. $expected = [
  5779. ['select' => ['name' => 'Contact[date][year]']],
  5780. $yearsRegex,
  5781. ['option' => ['value' => '']],
  5782. '/option',
  5783. '*/select',
  5784. ['select' => ['name' => 'Contact[date][month]']],
  5785. $monthsRegex,
  5786. ['option' => ['value' => '']],
  5787. '/option',
  5788. '*/select',
  5789. ['select' => ['name' => 'Contact[date][day]']],
  5790. $daysRegex,
  5791. ['option' => ['value' => '']],
  5792. '/option',
  5793. '*/select',
  5794. ['select' => ['name' => 'Contact[date][hour]']],
  5795. $hoursRegex,
  5796. ['option' => ['value' => '']],
  5797. '/option',
  5798. '*/select',
  5799. ['select' => ['name' => 'Contact[date][minute]']],
  5800. $minutesRegex,
  5801. ['option' => ['value' => '']],
  5802. '/option',
  5803. '*/select',
  5804. ['select' => ['name' => 'Contact[date][meridian]']],
  5805. $meridianRegex,
  5806. ['option' => ['value' => '']],
  5807. '/option',
  5808. '*/select'
  5809. ];
  5810. $this->assertHtml($expected, $result);
  5811. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5812. }
  5813. /**
  5814. * testDatetimeMinuteInterval method
  5815. *
  5816. * Test datetime with interval option.
  5817. *
  5818. * @return void
  5819. */
  5820. public function testDatetimeMinuteInterval()
  5821. {
  5822. extract($this->dateRegex);
  5823. $result = $this->Form->dateTime('Contact.date', [
  5824. 'interval' => 5,
  5825. 'value' => ''
  5826. ]);
  5827. $expected = [
  5828. ['select' => ['name' => 'Contact[date][year]']],
  5829. $yearsRegex,
  5830. ['option' => ['selected' => 'selected', 'value' => '']],
  5831. '/option',
  5832. '*/select',
  5833. ['select' => ['name' => 'Contact[date][month]']],
  5834. $monthsRegex,
  5835. ['option' => ['selected' => 'selected', 'value' => '']],
  5836. '/option',
  5837. '*/select',
  5838. ['select' => ['name' => 'Contact[date][day]']],
  5839. $daysRegex,
  5840. ['option' => ['selected' => 'selected', 'value' => '']],
  5841. '/option',
  5842. '*/select',
  5843. ['select' => ['name' => 'Contact[date][hour]']],
  5844. $hoursRegex,
  5845. ['option' => ['selected' => 'selected', 'value' => '']],
  5846. '/option',
  5847. '*/select',
  5848. ['select' => ['name' => 'Contact[date][minute]']],
  5849. $minutesRegex,
  5850. ['option' => ['selected' => 'selected', 'value' => '']],
  5851. '/option',
  5852. ['option' => ['value' => '00']],
  5853. '00',
  5854. '/option',
  5855. ['option' => ['value' => '05']],
  5856. '05',
  5857. '/option',
  5858. ['option' => ['value' => '10']],
  5859. '10',
  5860. '/option',
  5861. '*/select',
  5862. ];
  5863. $this->assertHtml($expected, $result);
  5864. }
  5865. /**
  5866. * testDateTimeRounding method
  5867. *
  5868. * Test dateTime with rounding.
  5869. *
  5870. * @return void
  5871. */
  5872. public function testDateTimeRounding()
  5873. {
  5874. $this->Form->request = $this->Form->request->withData('Contact', [
  5875. 'date' => [
  5876. 'day' => '13',
  5877. 'month' => '12',
  5878. 'year' => '2010',
  5879. 'hour' => '04',
  5880. 'minute' => '19',
  5881. 'meridian' => 'AM'
  5882. ]
  5883. ]);
  5884. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5885. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5886. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5887. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5888. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5889. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5890. }
  5891. /**
  5892. * testDatetimeWithDefault method
  5893. *
  5894. * Test that datetime() and default values work.
  5895. *
  5896. * @return void
  5897. */
  5898. public function testDatetimeWithDefault()
  5899. {
  5900. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5901. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5902. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5903. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5904. $result = $this->Form->dateTime('Contact.updated', [
  5905. 'default' => '2009-06-01 11:15:30'
  5906. ]);
  5907. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5908. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5909. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5910. }
  5911. /**
  5912. * testDateTimeAllZeros method
  5913. *
  5914. * Test datetime() with all zeros.
  5915. *
  5916. * @return void
  5917. */
  5918. public function testDateTimeAllZeros()
  5919. {
  5920. $result = $this->Form->dateTime('Contact.date', [
  5921. 'timeFormat' => false,
  5922. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5923. 'value' => '0000-00-00'
  5924. ]);
  5925. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5926. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5927. }
  5928. /**
  5929. * testDateTimeEmptyAsArray method
  5930. *
  5931. * @return void
  5932. */
  5933. public function testDateTimeEmptyAsArray()
  5934. {
  5935. $result = $this->Form->dateTime('Contact.date', [
  5936. 'empty' => [
  5937. 'day' => 'DAY',
  5938. 'month' => 'MONTH',
  5939. 'year' => 'YEAR',
  5940. 'hour' => 'HOUR',
  5941. 'minute' => 'MINUTE',
  5942. 'meridian' => false
  5943. ],
  5944. 'default' => true
  5945. ]);
  5946. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5947. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5948. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5949. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5950. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5951. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5952. $result = $this->Form->dateTime('Contact.date', [
  5953. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5954. 'default' => true
  5955. ]);
  5956. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5957. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5958. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5959. }
  5960. /**
  5961. * testFormDateTimeMulti method
  5962. *
  5963. * Test multiple datetime element generation.
  5964. *
  5965. * @return void
  5966. */
  5967. public function testFormDateTimeMulti()
  5968. {
  5969. extract($this->dateRegex);
  5970. $result = $this->Form->dateTime('Contact.1.updated');
  5971. $this->assertContains('Contact[1][updated][month]', $result);
  5972. $this->assertContains('Contact[1][updated][day]', $result);
  5973. $this->assertContains('Contact[1][updated][year]', $result);
  5974. $this->assertContains('Contact[1][updated][hour]', $result);
  5975. $this->assertContains('Contact[1][updated][minute]', $result);
  5976. }
  5977. /**
  5978. * testDateTimeLabelIdMatchesFirstControl method
  5979. *
  5980. * When changing the date format, the label should always focus the first select box when
  5981. * clicked.
  5982. *
  5983. * @return void
  5984. */
  5985. public function testDateTimeLabelIdMatchesFirstControl()
  5986. {
  5987. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5988. $this->assertContains('<label>Date</label>', $result);
  5989. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5990. $this->assertContains('<label>Date</label>', $result);
  5991. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5992. $this->assertContains('<label>Date</label>', $result);
  5993. }
  5994. /**
  5995. * testDateTimeSecondOptions method
  5996. *
  5997. * Test datetime second=true.
  5998. *
  5999. * @return void
  6000. */
  6001. public function testDateTimeSecondOptions()
  6002. {
  6003. $result = $this->Form->dateTime('updated', ['second' => true]);
  6004. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6005. $result = $this->Form->dateTime('updated', ['second' => []]);
  6006. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6007. $result = $this->Form->dateTime('updated', ['second' => null]);
  6008. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6009. $result = $this->Form->dateTime('updated', ['second' => false]);
  6010. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6011. }
  6012. /**
  6013. * testMonth method
  6014. *
  6015. * Test generation of a month input.
  6016. *
  6017. * @return void
  6018. */
  6019. public function testMonth()
  6020. {
  6021. $result = $this->Form->month('Model.field', ['value' => '']);
  6022. $expected = [
  6023. ['select' => ['name' => 'Model[field][month]']],
  6024. ['option' => ['value' => '', 'selected' => 'selected']],
  6025. '/option',
  6026. ['option' => ['value' => '01']],
  6027. date('F', strtotime('2008-01-01 00:00:00')),
  6028. '/option',
  6029. ['option' => ['value' => '02']],
  6030. date('F', strtotime('2008-02-01 00:00:00')),
  6031. '/option',
  6032. '*/select',
  6033. ];
  6034. $this->assertHtml($expected, $result);
  6035. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6036. $expected = [
  6037. ['select' => ['name' => 'Model[field][month]']],
  6038. ['option' => ['selected' => 'selected', 'value' => '']],
  6039. '/option',
  6040. ['option' => ['value' => '01']],
  6041. date('F', strtotime('2008-01-01 00:00:00')),
  6042. '/option',
  6043. ['option' => ['value' => '02']],
  6044. date('F', strtotime('2008-02-01 00:00:00')),
  6045. '/option',
  6046. '*/select',
  6047. ];
  6048. $this->assertHtml($expected, $result);
  6049. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6050. $expected = [
  6051. ['select' => ['name' => 'Model[field][month]']],
  6052. ['option' => ['selected' => 'selected', 'value' => '']],
  6053. '/option',
  6054. ['option' => ['value' => '01']],
  6055. '1',
  6056. '/option',
  6057. ['option' => ['value' => '02']],
  6058. '2',
  6059. '/option',
  6060. '*/select',
  6061. ];
  6062. $this->assertHtml($expected, $result);
  6063. $monthNames = [
  6064. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6065. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6066. ];
  6067. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6068. $expected = [
  6069. ['select' => ['name' => 'Model[field][month]']],
  6070. ['option' => ['value' => '']],
  6071. '/option',
  6072. ['option' => ['value' => '01', 'selected' => 'selected']],
  6073. 'Jan',
  6074. '/option',
  6075. ['option' => ['value' => '02']],
  6076. 'Feb',
  6077. '/option',
  6078. '*/select',
  6079. ];
  6080. $this->assertHtml($expected, $result);
  6081. $this->Form->request = $this->Form->request->withData('Project.release', '2050-02-10');
  6082. $this->Form->create();
  6083. $result = $this->Form->month('Project.release');
  6084. $expected = [
  6085. ['select' => ['name' => 'Project[release][month]']],
  6086. ['option' => ['value' => '']],
  6087. '/option',
  6088. ['option' => ['value' => '01']],
  6089. 'January',
  6090. '/option',
  6091. ['option' => ['value' => '02', 'selected' => 'selected']],
  6092. 'February',
  6093. '/option',
  6094. '*/select',
  6095. ];
  6096. $this->assertHtml($expected, $result);
  6097. $result = $this->Form->month('Contact.published', [
  6098. 'empty' => 'Published on',
  6099. ]);
  6100. $this->assertContains('Published on', $result);
  6101. }
  6102. /**
  6103. * testDay method
  6104. *
  6105. * Test generation of a day input.
  6106. *
  6107. * @return void
  6108. */
  6109. public function testDay()
  6110. {
  6111. extract($this->dateRegex);
  6112. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6113. $expected = [
  6114. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6115. ['option' => ['selected' => 'selected', 'value' => '']],
  6116. '/option',
  6117. ['option' => ['value' => '01']],
  6118. '1',
  6119. '/option',
  6120. ['option' => ['value' => '02']],
  6121. '2',
  6122. '/option',
  6123. $daysRegex,
  6124. '/select',
  6125. ];
  6126. $this->assertHtml($expected, $result);
  6127. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 23:12:32');
  6128. $this->Form->create();
  6129. $result = $this->Form->day('Model.field');
  6130. $expected = [
  6131. ['select' => ['name' => 'Model[field][day]']],
  6132. ['option' => ['value' => '']],
  6133. '/option',
  6134. ['option' => ['value' => '01']],
  6135. '1',
  6136. '/option',
  6137. ['option' => ['value' => '02']],
  6138. '2',
  6139. '/option',
  6140. $daysRegex,
  6141. ['option' => ['value' => '10', 'selected' => 'selected']],
  6142. '10',
  6143. '/option',
  6144. $daysRegex,
  6145. '/select',
  6146. ];
  6147. $this->assertHtml($expected, $result);
  6148. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6149. $this->Form->create();
  6150. $result = $this->Form->day('Model.field', ['value' => '10']);
  6151. $expected = [
  6152. ['select' => ['name' => 'Model[field][day]']],
  6153. ['option' => ['value' => '']],
  6154. '/option',
  6155. ['option' => ['value' => '01']],
  6156. '1',
  6157. '/option',
  6158. ['option' => ['value' => '02']],
  6159. '2',
  6160. '/option',
  6161. $daysRegex,
  6162. ['option' => ['value' => '10', 'selected' => 'selected']],
  6163. '10',
  6164. '/option',
  6165. $daysRegex,
  6166. '/select',
  6167. ];
  6168. $this->assertHtml($expected, $result);
  6169. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6170. $this->Form->create();
  6171. $result = $this->Form->day('Project.release');
  6172. $expected = [
  6173. ['select' => ['name' => 'Project[release][day]']],
  6174. ['option' => ['value' => '']],
  6175. '/option',
  6176. ['option' => ['value' => '01']],
  6177. '1',
  6178. '/option',
  6179. ['option' => ['value' => '02']],
  6180. '2',
  6181. '/option',
  6182. $daysRegex,
  6183. ['option' => ['value' => '10', 'selected' => 'selected']],
  6184. '10',
  6185. '/option',
  6186. $daysRegex,
  6187. '/select',
  6188. ];
  6189. $this->assertHtml($expected, $result);
  6190. $result = $this->Form->day('Contact.published', [
  6191. 'empty' => 'Published on',
  6192. ]);
  6193. $this->assertContains('Published on', $result);
  6194. }
  6195. /**
  6196. * testMinute method
  6197. *
  6198. * Test generation of a minute input.
  6199. *
  6200. * @return void
  6201. */
  6202. public function testMinute()
  6203. {
  6204. extract($this->dateRegex);
  6205. $result = $this->Form->minute('Model.field', ['value' => '']);
  6206. $expected = [
  6207. ['select' => ['name' => 'Model[field][minute]']],
  6208. ['option' => ['selected' => 'selected', 'value' => '']],
  6209. '/option',
  6210. ['option' => ['value' => '00']],
  6211. '00',
  6212. '/option',
  6213. ['option' => ['value' => '01']],
  6214. '01',
  6215. '/option',
  6216. ['option' => ['value' => '02']],
  6217. '02',
  6218. '/option',
  6219. $minutesRegex,
  6220. '/select',
  6221. ];
  6222. $this->assertHtml($expected, $result);
  6223. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6224. $this->Form->create();
  6225. $result = $this->Form->minute('Model.field');
  6226. $expected = [
  6227. ['select' => ['name' => 'Model[field][minute]']],
  6228. ['option' => ['value' => '']],
  6229. '/option',
  6230. ['option' => ['value' => '00']],
  6231. '00',
  6232. '/option',
  6233. ['option' => ['value' => '01']],
  6234. '01',
  6235. '/option',
  6236. ['option' => ['value' => '02']],
  6237. '02',
  6238. '/option',
  6239. $minutesRegex,
  6240. ['option' => ['value' => '12', 'selected' => 'selected']],
  6241. '12',
  6242. '/option',
  6243. $minutesRegex,
  6244. '/select',
  6245. ];
  6246. $this->assertHtml($expected, $result);
  6247. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6248. $this->Form->create();
  6249. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6250. $expected = [
  6251. ['select' => ['name' => 'Model[field][minute]']],
  6252. ['option' => ['selected' => 'selected', 'value' => '']],
  6253. '/option',
  6254. ['option' => ['value' => '00']],
  6255. '00',
  6256. '/option',
  6257. ['option' => ['value' => '05']],
  6258. '05',
  6259. '/option',
  6260. ['option' => ['value' => '10']],
  6261. '10',
  6262. '/option',
  6263. $minutesRegex,
  6264. '/select',
  6265. ];
  6266. $this->assertHtml($expected, $result);
  6267. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:10:32');
  6268. $this->Form->create();
  6269. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6270. $expected = [
  6271. ['select' => ['name' => 'Model[field][minute]']],
  6272. ['option' => ['value' => '']],
  6273. '/option',
  6274. ['option' => ['value' => '00']],
  6275. '00',
  6276. '/option',
  6277. ['option' => ['value' => '05']],
  6278. '05',
  6279. '/option',
  6280. ['option' => ['value' => '10', 'selected' => 'selected']],
  6281. '10',
  6282. '/option',
  6283. $minutesRegex,
  6284. '/select',
  6285. ];
  6286. $this->assertHtml($expected, $result);
  6287. }
  6288. /**
  6289. * testMeridian method
  6290. *
  6291. * Test generating an input for the meridian.
  6292. *
  6293. * @return void
  6294. */
  6295. public function testMeridian()
  6296. {
  6297. extract($this->dateRegex);
  6298. $now = new \DateTime();
  6299. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6300. $expected = [
  6301. ['select' => ['name' => 'Model[field][meridian]']],
  6302. ['option' => ['value' => '']],
  6303. '/option',
  6304. $meridianRegex,
  6305. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6306. $now->format('a'),
  6307. '/option',
  6308. '*/select'
  6309. ];
  6310. $this->assertHtml($expected, $result);
  6311. }
  6312. /**
  6313. * testHour method
  6314. *
  6315. * Test generation of an hour input.
  6316. *
  6317. * @return void
  6318. */
  6319. public function testHour()
  6320. {
  6321. extract($this->dateRegex);
  6322. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6323. $expected = [
  6324. ['select' => ['name' => 'Model[field][hour]']],
  6325. ['option' => ['selected' => 'selected', 'value' => '']],
  6326. '/option',
  6327. ['option' => ['value' => '01']],
  6328. '1',
  6329. '/option',
  6330. ['option' => ['value' => '02']],
  6331. '2',
  6332. '/option',
  6333. $hoursRegex,
  6334. '/select',
  6335. ];
  6336. $this->assertHtml($expected, $result);
  6337. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6338. $this->Form->create();
  6339. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6340. $expected = [
  6341. ['select' => ['name' => 'Model[field][hour]']],
  6342. ['option' => ['value' => '']],
  6343. '/option',
  6344. ['option' => ['value' => '01']],
  6345. '1',
  6346. '/option',
  6347. ['option' => ['value' => '02']],
  6348. '2',
  6349. '/option',
  6350. $hoursRegex,
  6351. ['option' => ['value' => '12', 'selected' => 'selected']],
  6352. '12',
  6353. '/option',
  6354. '/select',
  6355. ];
  6356. $this->assertHtml($expected, $result);
  6357. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6358. $this->Form->create();
  6359. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6360. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6361. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6362. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6363. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6364. $this->Form->create();
  6365. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6366. $expected = [
  6367. ['select' => ['name' => 'Model[field][hour]']],
  6368. ['option' => ['value' => '']],
  6369. '/option',
  6370. ['option' => ['value' => '00', 'selected' => 'selected']],
  6371. '0',
  6372. '/option',
  6373. ['option' => ['value' => '01']],
  6374. '1',
  6375. '/option',
  6376. ['option' => ['value' => '02']],
  6377. '2',
  6378. '/option',
  6379. $hoursRegex,
  6380. '/select',
  6381. ];
  6382. $this->assertHtml($expected, $result);
  6383. $this->Form->request = $this->Form->request->withData('Model.field', null);
  6384. $this->Form->create();
  6385. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6386. $thisHour = date('H');
  6387. $optValue = date('G');
  6388. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6389. $this->Form->request = $this->Form->request->withData('Model.field', '2050-10-10 01:12:32');
  6390. $this->Form->create();
  6391. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6392. $expected = [
  6393. ['select' => ['name' => 'Model[field][hour]']],
  6394. ['option' => ['value' => '']],
  6395. '/option',
  6396. ['option' => ['value' => '00']],
  6397. '0',
  6398. '/option',
  6399. ['option' => ['value' => '01', 'selected' => 'selected']],
  6400. '1',
  6401. '/option',
  6402. ['option' => ['value' => '02']],
  6403. '2',
  6404. '/option',
  6405. $hoursRegex,
  6406. '/select',
  6407. ];
  6408. $this->assertHtml($expected, $result);
  6409. }
  6410. /**
  6411. * testYear method
  6412. *
  6413. * Test generation of a year input.
  6414. *
  6415. * @return void
  6416. */
  6417. public function testYear()
  6418. {
  6419. $this->Form->request = $this->Form->request->withData('Contact.published', '2006-10-10');
  6420. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6421. $expected = [
  6422. ['select' => ['name' => 'Model[field][year]']],
  6423. ['option' => ['selected' => 'selected', 'value' => '']],
  6424. '/option',
  6425. ['option' => ['value' => '2007']],
  6426. '2007',
  6427. '/option',
  6428. ['option' => ['value' => '2006']],
  6429. '2006',
  6430. '/option',
  6431. '/select',
  6432. ];
  6433. $this->assertHtml($expected, $result);
  6434. $result = $this->Form->year('Model.field', [
  6435. 'value' => '',
  6436. 'minYear' => 2006,
  6437. 'maxYear' => 2007,
  6438. 'orderYear' => 'asc'
  6439. ]);
  6440. $expected = [
  6441. ['select' => ['name' => 'Model[field][year]']],
  6442. ['option' => ['selected' => 'selected', 'value' => '']],
  6443. '/option',
  6444. ['option' => ['value' => '2006']],
  6445. '2006',
  6446. '/option',
  6447. ['option' => ['value' => '2007']],
  6448. '2007',
  6449. '/option',
  6450. '/select',
  6451. ];
  6452. $this->assertHtml($expected, $result);
  6453. $result = $this->Form->year('Contact.published', [
  6454. 'empty' => false,
  6455. 'minYear' => 2006,
  6456. 'maxYear' => 2007,
  6457. ]);
  6458. $expected = [
  6459. ['select' => ['name' => 'Contact[published][year]']],
  6460. ['option' => ['value' => '2007']],
  6461. '2007',
  6462. '/option',
  6463. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6464. '2006',
  6465. '/option',
  6466. '/select',
  6467. ];
  6468. $this->assertHtml($expected, $result);
  6469. $result = $this->Form->year('Contact.published', [
  6470. 'empty' => 'Published on',
  6471. ]);
  6472. $this->assertContains('Published on', $result);
  6473. }
  6474. /**
  6475. * testControlDatetimePreEpoch method
  6476. *
  6477. * Test minYear being prior to the unix epoch.
  6478. *
  6479. * @return void
  6480. */
  6481. public function testControlDatetimePreEpoch()
  6482. {
  6483. $start = date('Y') - 80;
  6484. $end = date('Y') - 18;
  6485. $result = $this->Form->control('birth_year', [
  6486. 'type' => 'date',
  6487. 'label' => 'Birth Year',
  6488. 'minYear' => $start,
  6489. 'maxYear' => $end,
  6490. 'month' => false,
  6491. 'day' => false,
  6492. ]);
  6493. $this->assertContains('value="' . $start . '">' . $start, $result);
  6494. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6495. $this->assertNotContains('value="00">00', $result);
  6496. }
  6497. /**
  6498. * testYearAutoExpandRange method
  6499. *
  6500. * @return void
  6501. */
  6502. public function testYearAutoExpandRange()
  6503. {
  6504. $this->Form->request = $this->Form->request->withData('User.birthday', '1930-10-10');
  6505. $result = $this->Form->year('User.birthday');
  6506. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6507. $result = $matches[1];
  6508. $expected = range(date('Y') + 5, 1930);
  6509. $this->assertEquals($expected, $result);
  6510. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6511. $this->Form->create();
  6512. $result = $this->Form->year('Project.release');
  6513. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6514. $result = $matches[1];
  6515. $expected = range(2050, date('Y') - 5);
  6516. $this->assertEquals($expected, $result);
  6517. $this->Form->request = $this->Form->request->withData('Project.release', '1881-10-10');
  6518. $this->Form->create();
  6519. $result = $this->Form->year('Project.release', [
  6520. 'minYear' => 1890,
  6521. 'maxYear' => 1900
  6522. ]);
  6523. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6524. $result = $matches[1];
  6525. $expected = range(1900, 1881);
  6526. $this->assertEquals($expected, $result);
  6527. }
  6528. /**
  6529. * testControlDate method
  6530. *
  6531. * Test that control() accepts the type of date and passes options in.
  6532. *
  6533. * @return void
  6534. */
  6535. public function testControlDate()
  6536. {
  6537. $this->Form->request = $this->Form->request->withParsedBody([
  6538. 'month_year' => ['month' => date('m')],
  6539. ]);
  6540. $this->Form->create($this->article);
  6541. $result = $this->Form->control('month_year', [
  6542. 'label' => false,
  6543. 'type' => 'date',
  6544. 'minYear' => 2006,
  6545. 'maxYear' => 2008
  6546. ]);
  6547. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6548. $this->assertNotContains('value="2008" selected="selected"', $result);
  6549. }
  6550. /**
  6551. * testControlLabelFalse method
  6552. *
  6553. * Test the label option being set to false.
  6554. *
  6555. * @return void
  6556. */
  6557. public function testControlLabelFalse()
  6558. {
  6559. $this->Form->create($this->article);
  6560. $result = $this->Form->control('title', ['label' => false]);
  6561. $expected = [
  6562. 'div' => ['class' => 'input text required'],
  6563. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6564. '/div'
  6565. ];
  6566. $this->assertHtml($expected, $result);
  6567. }
  6568. /**
  6569. * testControlDateMaxYear method
  6570. *
  6571. * Let's say we want to only allow users born from 2006 to 2008 to register
  6572. * This being the first signup page, we still don't have any data.
  6573. *
  6574. * @return void
  6575. */
  6576. public function testControlDateMaxYear()
  6577. {
  6578. $this->Form->create($this->article);
  6579. $result = $this->Form->control('birthday', [
  6580. 'label' => false,
  6581. 'type' => 'date',
  6582. 'minYear' => 2006,
  6583. 'maxYear' => 2008,
  6584. 'default' => true
  6585. ]);
  6586. $this->assertContains('value="2008" selected="selected"', $result);
  6587. $this->assertContains('value="2006"', $result);
  6588. $this->assertNotContains('value="2005"', $result);
  6589. $this->assertNotContains('value="2009"', $result);
  6590. }
  6591. /**
  6592. * testTextArea method
  6593. *
  6594. * Test generation of a textarea input.
  6595. *
  6596. * @return void
  6597. */
  6598. public function testTextArea()
  6599. {
  6600. $this->Form->request = $this->Form->request->withData('field', 'some test data');
  6601. $result = $this->Form->textarea('field');
  6602. $expected = [
  6603. 'textarea' => ['name' => 'field', 'rows' => 5],
  6604. 'some test data',
  6605. '/textarea',
  6606. ];
  6607. $this->assertHtml($expected, $result);
  6608. $result = $this->Form->textarea('user.bio');
  6609. $expected = [
  6610. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6611. '/textarea',
  6612. ];
  6613. $this->assertHtml($expected, $result);
  6614. $this->Form->request = $this->Form->request
  6615. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  6616. $this->Form->create();
  6617. $result = $this->Form->textarea('field');
  6618. $expected = [
  6619. 'textarea' => ['name' => 'field', 'rows' => 5],
  6620. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6621. '/textarea',
  6622. ];
  6623. $this->assertHtml($expected, $result);
  6624. $this->Form->request = $this->Form->request->withData(
  6625. 'Model.field',
  6626. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6627. );
  6628. $this->Form->create();
  6629. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6630. $expected = [
  6631. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6632. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6633. '/textarea',
  6634. ];
  6635. $this->assertHtml($expected, $result);
  6636. $result = $this->Form->textarea('0.OtherModel.field');
  6637. $expected = [
  6638. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6639. '/textarea'
  6640. ];
  6641. $this->assertHtml($expected, $result);
  6642. }
  6643. /**
  6644. * testTextAreaWithStupidCharacters method
  6645. *
  6646. * Test text area with non-ascii characters.
  6647. *
  6648. * @return void
  6649. */
  6650. public function testTextAreaWithStupidCharacters()
  6651. {
  6652. $result = $this->Form->textarea('Post.content', [
  6653. 'value' => 'GREAT®',
  6654. 'rows' => '15',
  6655. 'cols' => '75'
  6656. ]);
  6657. $expected = [
  6658. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6659. 'GREAT®',
  6660. '/textarea',
  6661. ];
  6662. $this->assertHtml($expected, $result);
  6663. }
  6664. /**
  6665. * testTextAreaMaxLength method
  6666. *
  6667. * Test textareas maxlength read from schema.
  6668. *
  6669. * @return void
  6670. */
  6671. public function testTextAreaMaxLength()
  6672. {
  6673. $this->Form->create([
  6674. 'schema' => [
  6675. 'stuff' => ['type' => 'string', 'length' => 10],
  6676. ]
  6677. ]);
  6678. $result = $this->Form->control('other', ['type' => 'textarea']);
  6679. $expected = [
  6680. 'div' => ['class' => 'input textarea'],
  6681. 'label' => ['for' => 'other'],
  6682. 'Other',
  6683. '/label',
  6684. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6685. '/textarea',
  6686. '/div'
  6687. ];
  6688. $this->assertHtml($expected, $result);
  6689. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6690. $expected = [
  6691. 'div' => ['class' => 'input textarea'],
  6692. 'label' => ['for' => 'stuff'],
  6693. 'Stuff',
  6694. '/label',
  6695. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6696. '/textarea',
  6697. '/div'
  6698. ];
  6699. $this->assertHtml($expected, $result);
  6700. }
  6701. /**
  6702. * testHiddenField method
  6703. *
  6704. * Test generation of a hidden input.
  6705. *
  6706. * @return void
  6707. */
  6708. public function testHiddenField()
  6709. {
  6710. $this->article['errors'] = [
  6711. 'field' => true
  6712. ];
  6713. $this->Form->request = $this->Form->request->withData('field', 'test');
  6714. $this->Form->create($this->article);
  6715. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6716. $expected = [
  6717. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6718. $this->assertHtml($expected, $result);
  6719. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6720. $expected = [
  6721. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6722. ];
  6723. $this->assertHtml($expected, $result);
  6724. }
  6725. /**
  6726. * testFileUploadField method
  6727. *
  6728. * Test generation of a file upload input.
  6729. *
  6730. * @return void
  6731. */
  6732. public function testFileUploadField()
  6733. {
  6734. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6735. $result = $this->Form->file('Model.upload');
  6736. $this->assertHtml($expected, $result);
  6737. $this->Form->request = $this->Form->request->withData('Model.upload', [
  6738. 'name' => '', 'type' => '', 'tmp_name' => '',
  6739. 'error' => 4, 'size' => 0
  6740. ]);
  6741. $result = $this->Form->file('Model.upload');
  6742. $this->assertHtml($expected, $result);
  6743. $this->Form->request = $this->Form->request->withData('Model.upload', 'no data should be set in value');
  6744. $result = $this->Form->file('Model.upload');
  6745. $this->assertHtml($expected, $result);
  6746. }
  6747. /**
  6748. * testFileUploadOnOtherModel method
  6749. *
  6750. * Test File upload input on a model not used in create().
  6751. *
  6752. * @return void
  6753. */
  6754. public function testFileUploadOnOtherModel()
  6755. {
  6756. $this->Form->create($this->article, ['type' => 'file']);
  6757. $result = $this->Form->file('ValidateProfile.city');
  6758. $expected = [
  6759. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6760. ];
  6761. $this->assertHtml($expected, $result);
  6762. }
  6763. /**
  6764. * testButton method
  6765. *
  6766. * Test generation of a form button.
  6767. *
  6768. * @return void
  6769. */
  6770. public function testButton()
  6771. {
  6772. $result = $this->Form->button('Hi');
  6773. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6774. $this->assertHtml($expected, $result);
  6775. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6776. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6777. $this->assertHtml($expected, $result);
  6778. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6779. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6780. $this->assertHtml($expected, $result);
  6781. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6782. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6783. $this->assertHtml($expected, $result);
  6784. $result = $this->Form->button('No type', ['type' => false]);
  6785. $expected = ['button' => [], 'No type', '/button'];
  6786. $this->assertHtml($expected, $result);
  6787. $result = $this->Form->button('Upload Text', [
  6788. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6789. 'escape' => false
  6790. ]);
  6791. $this->assertNotRegExp('/\&039/', $result);
  6792. }
  6793. /**
  6794. * testButtonUnlockedByDefault method
  6795. *
  6796. * Test that button() makes unlocked fields by default.
  6797. *
  6798. * @return void
  6799. */
  6800. public function testButtonUnlockedByDefault()
  6801. {
  6802. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'secured');
  6803. $this->Form->button('Save', ['name' => 'save']);
  6804. $this->Form->button('Clear');
  6805. $result = $this->Form->unlockField();
  6806. $this->assertEquals(['save'], $result);
  6807. }
  6808. /**
  6809. * Test generation of a form button with confirm message.
  6810. *
  6811. * @return void
  6812. */
  6813. public function testButtonWithConfirm()
  6814. {
  6815. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6816. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6817. $this->assertHtml($expected, $result);
  6818. }
  6819. /**
  6820. * testPostButton method
  6821. *
  6822. * @return void
  6823. */
  6824. public function testPostButton()
  6825. {
  6826. $result = $this->Form->postButton('Hi', '/controller/action');
  6827. $expected = [
  6828. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6829. 'div' => ['style' => 'display:none;'],
  6830. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6831. '/div',
  6832. 'button' => ['type' => 'submit'],
  6833. 'Hi',
  6834. '/button',
  6835. '/form'
  6836. ];
  6837. $this->assertHtml($expected, $result);
  6838. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6839. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6840. }
  6841. /**
  6842. * testPostButtonMethodType method
  6843. *
  6844. * @return void
  6845. */
  6846. public function testPostButtonMethodType()
  6847. {
  6848. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6849. $expected = [
  6850. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6851. 'div' => ['style' => 'display:none;'],
  6852. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6853. '/div',
  6854. 'button' => ['type' => 'submit'],
  6855. 'Hi',
  6856. '/button',
  6857. '/form'
  6858. ];
  6859. $this->assertHtml($expected, $result);
  6860. }
  6861. /**
  6862. * testPostButtonFormOptions method
  6863. *
  6864. * @return void
  6865. */
  6866. public function testPostButtonFormOptions()
  6867. {
  6868. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6869. $expected = [
  6870. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6871. 'div' => ['style' => 'display:none;'],
  6872. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6873. '/div',
  6874. 'button' => ['type' => 'submit'],
  6875. 'Hi',
  6876. '/button',
  6877. '/form'
  6878. ];
  6879. $this->assertHtml($expected, $result);
  6880. }
  6881. /**
  6882. * testPostButtonNestedData method
  6883. *
  6884. * Test using postButton with N dimensional data.
  6885. *
  6886. * @return void
  6887. */
  6888. public function testPostButtonNestedData()
  6889. {
  6890. $data = [
  6891. 'one' => [
  6892. 'two' => [
  6893. 3, 4, 5
  6894. ]
  6895. ]
  6896. ];
  6897. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6898. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6899. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6900. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6901. }
  6902. /**
  6903. * testSecurePostButton method
  6904. *
  6905. * Test that postButton adds _Token fields.
  6906. *
  6907. * @return void
  6908. */
  6909. public function testSecurePostButton()
  6910. {
  6911. $this->Form->request = $this->Form->request
  6912. ->withParam('_csrfToken', 'testkey')
  6913. ->withParam('_Token.unlockedFields', []);
  6914. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6915. $tokenDebug = urlencode(json_encode([
  6916. '/posts/delete/1',
  6917. [],
  6918. []
  6919. ]));
  6920. $expected = [
  6921. 'form' => [
  6922. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6923. ],
  6924. ['div' => ['style' => 'display:none;']],
  6925. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6926. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6927. '/div',
  6928. 'button' => ['type' => 'submit'],
  6929. 'Delete',
  6930. '/button',
  6931. ['div' => ['style' => 'display:none;']],
  6932. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6933. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6934. ['input' => [
  6935. 'type' => 'hidden',
  6936. 'name' => '_Token[debug]',
  6937. 'value' => $tokenDebug,
  6938. 'autocomplete' => 'off',
  6939. ]],
  6940. '/div',
  6941. '/form',
  6942. ];
  6943. $this->assertHtml($expected, $result);
  6944. }
  6945. /**
  6946. * testPostLink method
  6947. *
  6948. * @return void
  6949. */
  6950. public function testPostLink()
  6951. {
  6952. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6953. $expected = [
  6954. 'form' => [
  6955. 'method' => 'post', 'action' => '/posts/delete/1',
  6956. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6957. ],
  6958. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6959. '/form',
  6960. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6961. 'Delete',
  6962. '/a'
  6963. ];
  6964. $this->assertHtml($expected, $result);
  6965. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6966. $expected = [
  6967. 'form' => [
  6968. 'method' => 'post', 'action' => '/posts/delete/1',
  6969. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6970. ],
  6971. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6972. '/form',
  6973. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6974. 'Delete',
  6975. '/a'
  6976. ];
  6977. $this->assertHtml($expected, $result);
  6978. $result = $this->Form->postLink(
  6979. 'Delete',
  6980. '/posts/delete/1',
  6981. ['target' => '_blank', 'class' => 'btn btn-danger']
  6982. );
  6983. $expected = [
  6984. 'form' => [
  6985. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6986. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6987. ],
  6988. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6989. '/form',
  6990. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6991. 'Delete',
  6992. '/a'
  6993. ];
  6994. $this->assertHtml($expected, $result);
  6995. }
  6996. /**
  6997. * testPostLinkWithConfirm method
  6998. *
  6999. * Test the confirm option for postLink().
  7000. *
  7001. * @return void
  7002. */
  7003. public function testPostLinkWithConfirm()
  7004. {
  7005. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7006. $expected = [
  7007. 'form' => [
  7008. 'method' => 'post', 'action' => '/posts/delete/1',
  7009. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7010. ],
  7011. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7012. '/form',
  7013. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7014. 'Delete',
  7015. '/a'
  7016. ];
  7017. $this->assertHtml($expected, $result);
  7018. $result = $this->Form->postLink(
  7019. 'Delete',
  7020. '/posts/delete/1',
  7021. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7022. );
  7023. $expected = [
  7024. 'form' => [
  7025. 'method' => 'post', 'action' => '/posts/delete/1',
  7026. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7027. ],
  7028. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7029. '/form',
  7030. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7031. 'Delete',
  7032. '/a'
  7033. ];
  7034. $this->assertHtml($expected, $result);
  7035. }
  7036. /**
  7037. * testPostLinkWithQuery method
  7038. *
  7039. * Test postLink() with query string args.
  7040. *
  7041. * @return void
  7042. */
  7043. public function testPostLinkWithQuery()
  7044. {
  7045. $result = $this->Form->postLink(
  7046. 'Delete',
  7047. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7048. );
  7049. $expected = [
  7050. 'form' => [
  7051. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7052. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7053. ],
  7054. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7055. '/form',
  7056. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7057. 'Delete',
  7058. '/a'
  7059. ];
  7060. $this->assertHtml($expected, $result);
  7061. }
  7062. /**
  7063. * testPostLinkWithData method
  7064. *
  7065. * Test postLink with additional data.
  7066. *
  7067. * @return void
  7068. */
  7069. public function testPostLinkWithData()
  7070. {
  7071. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7072. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7073. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7074. $this->Form->create($entity);
  7075. $this->Form->end();
  7076. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7077. $this->assertContains(
  7078. '<input type="hidden" name="name" value="show"',
  7079. $result,
  7080. 'should not contain entity data.'
  7081. );
  7082. }
  7083. /**
  7084. * testPostLinkSecurityHash method
  7085. *
  7086. * Test that security hashes for postLink include the url.
  7087. *
  7088. * @return void
  7089. */
  7090. public function testPostLinkSecurityHash()
  7091. {
  7092. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7093. $hash .= '%3Aid';
  7094. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7095. $result = $this->Form->postLink(
  7096. 'Delete',
  7097. '/posts/delete/1',
  7098. ['data' => ['id' => 1]]
  7099. );
  7100. $tokenDebug = urlencode(json_encode([
  7101. '/posts/delete/1',
  7102. [
  7103. 'id' => 1
  7104. ],
  7105. []
  7106. ]));
  7107. $expected = [
  7108. 'form' => [
  7109. 'method' => 'post', 'action' => '/posts/delete/1',
  7110. 'name', 'style' => 'display:none;'
  7111. ],
  7112. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7113. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7114. 'div' => ['style' => 'display:none;'],
  7115. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7116. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7117. ['input' => [
  7118. 'type' => 'hidden',
  7119. 'name' => '_Token[debug]',
  7120. 'value' => $tokenDebug,
  7121. 'autocomplete' => 'off',
  7122. ]],
  7123. '/div',
  7124. '/form',
  7125. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7126. 'Delete',
  7127. '/a'
  7128. ];
  7129. $this->assertHtml($expected, $result);
  7130. }
  7131. /**
  7132. * testPostLinkSecurityHashBlockMode method
  7133. *
  7134. * Test that postLink doesn't modify the fields in the containing form.
  7135. *
  7136. * postLink() calls inside open forms should not modify the field list
  7137. * for the form.
  7138. *
  7139. * @return void
  7140. */
  7141. public function testPostLinkSecurityHashBlockMode()
  7142. {
  7143. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7144. $hash .= '%3A';
  7145. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7146. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7147. $this->Form->control('title');
  7148. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7149. $result = $this->View->fetch('postLink');
  7150. $this->assertEquals(['title'], $this->Form->fields);
  7151. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7152. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7153. }
  7154. /**
  7155. * testPostLinkSecurityHashNoDebugMode method
  7156. *
  7157. * Test that security does not include debug token if debug is false.
  7158. *
  7159. * @return void
  7160. */
  7161. public function testPostLinkSecurityHashNoDebugMode()
  7162. {
  7163. Configure::write('debug', false);
  7164. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7165. $hash .= '%3Aid';
  7166. $this->Form->request = $this->Form->request
  7167. ->withParam('_Token.key', 'test');
  7168. $result = $this->Form->postLink(
  7169. 'Delete',
  7170. '/posts/delete/1',
  7171. ['data' => ['id' => 1]]
  7172. );
  7173. $expected = [
  7174. 'form' => [
  7175. 'method' => 'post', 'action' => '/posts/delete/1',
  7176. 'name', 'style' => 'display:none;'
  7177. ],
  7178. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7179. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7180. 'div' => ['style' => 'display:none;'],
  7181. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7182. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7183. '/div',
  7184. '/form',
  7185. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7186. 'Delete',
  7187. '/a'
  7188. ];
  7189. $this->assertHtml($expected, $result);
  7190. }
  7191. /**
  7192. * testPostLinkNestedData method
  7193. *
  7194. * Test using postLink with N dimensional data.
  7195. *
  7196. * @return void
  7197. */
  7198. public function testPostLinkNestedData()
  7199. {
  7200. $data = [
  7201. 'one' => [
  7202. 'two' => [
  7203. 3, 4, 5
  7204. ]
  7205. ]
  7206. ];
  7207. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7208. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7209. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7210. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7211. }
  7212. /**
  7213. * testPostLinkAfterGetForm method
  7214. *
  7215. * Test creating postLinks after a GET form.
  7216. *
  7217. * @return void
  7218. */
  7219. public function testPostLinkAfterGetForm()
  7220. {
  7221. $this->Form->request = $this->Form->request
  7222. ->withParam('_csrfToken', 'testkey')
  7223. ->withParam('_Token', 'val');
  7224. $this->Form->create($this->article, ['type' => 'get']);
  7225. $this->Form->end();
  7226. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7227. $tokenDebug = urlencode(json_encode([
  7228. '/posts/delete/1',
  7229. [],
  7230. []
  7231. ]));
  7232. $expected = [
  7233. 'form' => [
  7234. 'method' => 'post', 'action' => '/posts/delete/1',
  7235. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7236. ],
  7237. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7238. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7239. 'div' => ['style' => 'display:none;'],
  7240. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7241. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7242. ['input' => [
  7243. 'type' => 'hidden', 'name' => '_Token[debug]',
  7244. 'value' => $tokenDebug,
  7245. 'autocomplete' => 'off'
  7246. ]],
  7247. '/div',
  7248. '/form',
  7249. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7250. 'Delete',
  7251. '/a'
  7252. ];
  7253. $this->assertHtml($expected, $result);
  7254. }
  7255. /**
  7256. * testPostLinkFormBuffer method
  7257. *
  7258. * Test that postLink adds form tags to view block.
  7259. *
  7260. * @return void
  7261. */
  7262. public function testPostLinkFormBuffer()
  7263. {
  7264. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7265. $expected = [
  7266. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7267. 'Delete',
  7268. '/a'
  7269. ];
  7270. $this->assertHtml($expected, $result);
  7271. $result = $this->View->fetch('postLink');
  7272. $expected = [
  7273. 'form' => [
  7274. 'method' => 'post', 'action' => '/posts/delete/1',
  7275. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7276. ],
  7277. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7278. '/form'
  7279. ];
  7280. $this->assertHtml($expected, $result);
  7281. $result = $this->Form->postLink(
  7282. 'Delete',
  7283. '/posts/delete/2',
  7284. ['block' => true, 'method' => 'DELETE']
  7285. );
  7286. $expected = [
  7287. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7288. 'Delete',
  7289. '/a'
  7290. ];
  7291. $this->assertHtml($expected, $result);
  7292. $result = $this->View->fetch('postLink');
  7293. $expected = [
  7294. 'form' => [
  7295. 'method' => 'post', 'action' => '/posts/delete/1',
  7296. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7297. ],
  7298. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7299. '/form',
  7300. [
  7301. 'form' => [
  7302. 'method' => 'post', 'action' => '/posts/delete/2',
  7303. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7304. ],
  7305. ],
  7306. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7307. '/form'
  7308. ];
  7309. $this->assertHtml($expected, $result);
  7310. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7311. $expected = [
  7312. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7313. 'Delete',
  7314. '/a'
  7315. ];
  7316. $this->assertHtml($expected, $result);
  7317. $result = $this->View->fetch('foobar');
  7318. $expected = [
  7319. 'form' => [
  7320. 'method' => 'post', 'action' => '/posts/delete/1',
  7321. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7322. ],
  7323. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7324. '/form'
  7325. ];
  7326. $this->assertHtml($expected, $result);
  7327. }
  7328. /**
  7329. * testSubmitButton method
  7330. *
  7331. * @return void
  7332. */
  7333. public function testSubmitButton()
  7334. {
  7335. $result = $this->Form->submit('');
  7336. $expected = [
  7337. 'div' => ['class' => 'submit'],
  7338. 'input' => ['type' => 'submit', 'value' => ''],
  7339. '/div'
  7340. ];
  7341. $this->assertHtml($expected, $result);
  7342. $result = $this->Form->submit('Test Submit');
  7343. $expected = [
  7344. 'div' => ['class' => 'submit'],
  7345. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7346. '/div'
  7347. ];
  7348. $this->assertHtml($expected, $result);
  7349. $result = $this->Form->submit('Next >');
  7350. $expected = [
  7351. 'div' => ['class' => 'submit'],
  7352. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7353. '/div'
  7354. ];
  7355. $this->assertHtml($expected, $result);
  7356. $result = $this->Form->submit('Next >', ['escape' => false]);
  7357. $expected = [
  7358. 'div' => ['class' => 'submit'],
  7359. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7360. '/div'
  7361. ];
  7362. $this->assertHtml($expected, $result);
  7363. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7364. $expected = [
  7365. 'div' => ['class' => 'submit'],
  7366. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7367. '/div'
  7368. ];
  7369. $this->assertHtml($expected, $result);
  7370. }
  7371. /**
  7372. * testSubmitImage method
  7373. *
  7374. * Test image submit types.
  7375. *
  7376. * @return void
  7377. */
  7378. public function testSubmitImage()
  7379. {
  7380. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7381. $expected = [
  7382. 'div' => ['class' => 'submit'],
  7383. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7384. '/div'
  7385. ];
  7386. $this->assertHtml($expected, $result);
  7387. $result = $this->Form->submit('/relative/cake.power.gif');
  7388. $expected = [
  7389. 'div' => ['class' => 'submit'],
  7390. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7391. '/div'
  7392. ];
  7393. $this->assertHtml($expected, $result);
  7394. $result = $this->Form->submit('cake.power.gif');
  7395. $expected = [
  7396. 'div' => ['class' => 'submit'],
  7397. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7398. '/div'
  7399. ];
  7400. $this->assertHtml($expected, $result);
  7401. $result = $this->Form->submit('Not.an.image');
  7402. $expected = [
  7403. 'div' => ['class' => 'submit'],
  7404. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7405. '/div'
  7406. ];
  7407. $this->assertHtml($expected, $result);
  7408. }
  7409. /**
  7410. * testSubmitUnlockedByDefault method
  7411. *
  7412. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7413. * be submitted at a time.
  7414. *
  7415. * @return void
  7416. */
  7417. public function testSubmitUnlockedByDefault()
  7418. {
  7419. $this->Form->request = $this->Form->request->withParam('_Token', 'secured');
  7420. $this->Form->submit('Go go');
  7421. $this->Form->submit('Save', ['name' => 'save']);
  7422. $result = $this->Form->unlockField();
  7423. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7424. }
  7425. /**
  7426. * testSubmitImageTimestamp method
  7427. *
  7428. * Test submit image with timestamps.
  7429. *
  7430. * @return void
  7431. */
  7432. public function testSubmitImageTimestamp()
  7433. {
  7434. Configure::write('Asset.timestamp', 'force');
  7435. $result = $this->Form->submit('cake.power.gif');
  7436. $expected = [
  7437. 'div' => ['class' => 'submit'],
  7438. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7439. '/div'
  7440. ];
  7441. $this->assertHtml($expected, $result);
  7442. }
  7443. /**
  7444. * testDateTimeWithGetForms method
  7445. *
  7446. * Test that datetime() works with GET style forms.
  7447. *
  7448. * @return void
  7449. */
  7450. public function testDateTimeWithGetForms()
  7451. {
  7452. extract($this->dateRegex);
  7453. $this->Form->create($this->article, ['type' => 'get']);
  7454. $result = $this->Form->datetime('created');
  7455. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7456. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7457. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7458. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7459. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7460. }
  7461. /**
  7462. * testForMagicControlNonExistingNorValidated method
  7463. *
  7464. * @return void
  7465. */
  7466. public function testForMagicControlNonExistingNorValidated()
  7467. {
  7468. $this->Form->create($this->article);
  7469. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7470. $result = $this->Form->control('non_existing_nor_validated');
  7471. $expected = [
  7472. 'label' => ['for' => 'non-existing-nor-validated'],
  7473. 'Non Existing Nor Validated',
  7474. '/label',
  7475. 'input' => [
  7476. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7477. 'id' => 'non-existing-nor-validated'
  7478. ]
  7479. ];
  7480. $this->assertHtml($expected, $result);
  7481. $result = $this->Form->control('non_existing_nor_validated', [
  7482. 'val' => 'my value'
  7483. ]);
  7484. $expected = [
  7485. 'label' => ['for' => 'non-existing-nor-validated'],
  7486. 'Non Existing Nor Validated',
  7487. '/label',
  7488. 'input' => [
  7489. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7490. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7491. ]
  7492. ];
  7493. $this->assertHtml($expected, $result);
  7494. $this->Form->request = $this->Form->request->withData('non_existing_nor_validated', 'CakePHP magic');
  7495. $this->Form->create($this->article);
  7496. $result = $this->Form->control('non_existing_nor_validated');
  7497. $expected = [
  7498. 'label' => ['for' => 'non-existing-nor-validated'],
  7499. 'Non Existing Nor Validated',
  7500. '/label',
  7501. 'input' => [
  7502. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7503. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7504. ]
  7505. ];
  7506. $this->assertHtml($expected, $result);
  7507. }
  7508. /**
  7509. * testFormMagicControlLabel method
  7510. *
  7511. * @return void
  7512. */
  7513. public function testFormMagicControlLabel()
  7514. {
  7515. TableRegistry::get('Contacts', [
  7516. 'className' => __NAMESPACE__ . '\ContactsTable'
  7517. ]);
  7518. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7519. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7520. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7521. $expected = [
  7522. 'label' => ['for' => 'contacts-name'],
  7523. 'My label',
  7524. '/label',
  7525. 'input' => [
  7526. 'type' => 'text', 'name' => 'Contacts[name]',
  7527. 'id' => 'contacts-name', 'maxlength' => '255'
  7528. ]
  7529. ];
  7530. $this->assertHtml($expected, $result);
  7531. $result = $this->Form->control('name', [
  7532. 'label' => ['class' => 'mandatory']
  7533. ]);
  7534. $expected = [
  7535. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7536. 'Name',
  7537. '/label',
  7538. 'input' => [
  7539. 'type' => 'text', 'name' => 'name',
  7540. 'id' => 'name', 'maxlength' => '255'
  7541. ]
  7542. ];
  7543. $this->assertHtml($expected, $result);
  7544. $result = $this->Form->control('name', [
  7545. 'div' => false,
  7546. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7547. ]);
  7548. $expected = [
  7549. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7550. 'My label',
  7551. '/label',
  7552. 'input' => [
  7553. 'type' => 'text', 'name' => 'name',
  7554. 'id' => 'name', 'maxlength' => '255'
  7555. ]
  7556. ];
  7557. $this->assertHtml($expected, $result);
  7558. $result = $this->Form->control('Contact.name', [
  7559. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7560. ]);
  7561. $expected = [
  7562. 'label' => ['for' => 'my_id'],
  7563. 'Name',
  7564. '/label',
  7565. 'input' => [
  7566. 'type' => 'text', 'name' => 'Contact[name]',
  7567. 'id' => 'my_id', 'maxlength' => '255'
  7568. ]
  7569. ];
  7570. $this->assertHtml($expected, $result);
  7571. $result = $this->Form->control('1.id');
  7572. $expected = ['input' => [
  7573. 'type' => 'hidden', 'name' => '1[id]',
  7574. 'id' => '1-id'
  7575. ]];
  7576. $this->assertHtml($expected, $result);
  7577. $result = $this->Form->control('1.name');
  7578. $expected = [
  7579. 'label' => ['for' => '1-name'],
  7580. 'Name',
  7581. '/label',
  7582. 'input' => [
  7583. 'type' => 'text', 'name' => '1[name]',
  7584. 'id' => '1-name', 'maxlength' => '255'
  7585. ]
  7586. ];
  7587. $this->assertHtml($expected, $result);
  7588. }
  7589. /**
  7590. * testFormEnd method
  7591. *
  7592. * @return void
  7593. */
  7594. public function testFormEnd()
  7595. {
  7596. $this->assertEquals('</form>', $this->Form->end());
  7597. }
  7598. /**
  7599. * testMultiRecordForm method
  7600. *
  7601. * Test the generation of fields for a multi record form.
  7602. *
  7603. * @return void
  7604. */
  7605. public function testMultiRecordForm()
  7606. {
  7607. $this->loadFixtures('Articles', 'Comments');
  7608. $articles = TableRegistry::get('Articles');
  7609. $articles->hasMany('Comments');
  7610. $comment = new Entity(['comment' => 'Value']);
  7611. $article = new Article(['comments' => [$comment]]);
  7612. $this->Form->create([$article]);
  7613. $result = $this->Form->control('0.comments.1.comment');
  7614. //@codingStandardsIgnoreStart
  7615. $expected = [
  7616. 'div' => ['class' => 'input textarea'],
  7617. 'label' => ['for' => '0-comments-1-comment'],
  7618. 'Comment',
  7619. '/label',
  7620. 'textarea' => [
  7621. 'name',
  7622. 'id' => '0-comments-1-comment',
  7623. 'rows' => 5
  7624. ],
  7625. '/textarea',
  7626. '/div'
  7627. ];
  7628. //@codingStandardsIgnoreEnd
  7629. $this->assertHtml($expected, $result);
  7630. $result = $this->Form->control('0.comments.0.comment');
  7631. //@codingStandardsIgnoreStart
  7632. $expected = [
  7633. 'div' => ['class' => 'input textarea'],
  7634. 'label' => ['for' => '0-comments-0-comment'],
  7635. 'Comment',
  7636. '/label',
  7637. 'textarea' => [
  7638. 'name',
  7639. 'id' => '0-comments-0-comment',
  7640. 'rows' => 5
  7641. ],
  7642. 'Value',
  7643. '/textarea',
  7644. '/div'
  7645. ];
  7646. //@codingStandardsIgnoreEnd
  7647. $this->assertHtml($expected, $result);
  7648. $comment->setError('comment', ['Not valid']);
  7649. $result = $this->Form->control('0.comments.0.comment');
  7650. //@codingStandardsIgnoreStart
  7651. $expected = [
  7652. 'div' => ['class' => 'input textarea error'],
  7653. 'label' => ['for' => '0-comments-0-comment'],
  7654. 'Comment',
  7655. '/label',
  7656. 'textarea' => [
  7657. 'name',
  7658. 'class' => 'form-error',
  7659. 'id' => '0-comments-0-comment',
  7660. 'rows' => 5
  7661. ],
  7662. 'Value',
  7663. '/textarea',
  7664. ['div' => ['class' => 'error-message']],
  7665. 'Not valid',
  7666. '/div',
  7667. '/div'
  7668. ];
  7669. //@codingStandardsIgnoreEnd
  7670. $this->assertHtml($expected, $result);
  7671. TableRegistry::get('Comments')
  7672. ->getValidator('default')
  7673. ->allowEmpty('comment', false);
  7674. $result = $this->Form->control('0.comments.1.comment');
  7675. //@codingStandardsIgnoreStart
  7676. $expected = [
  7677. 'div' => ['class' => 'input textarea required'],
  7678. 'label' => ['for' => '0-comments-1-comment'],
  7679. 'Comment',
  7680. '/label',
  7681. 'textarea' => [
  7682. 'name',
  7683. 'required' => 'required',
  7684. 'id' => '0-comments-1-comment',
  7685. 'rows' => 5
  7686. ],
  7687. '/textarea',
  7688. '/div'
  7689. ];
  7690. //@codingStandardsIgnoreEnd
  7691. $this->assertHtml($expected, $result);
  7692. }
  7693. /**
  7694. * testHtml5Controls method
  7695. *
  7696. * Test that some html5 inputs + FormHelper::__call() work.
  7697. *
  7698. * @return void
  7699. */
  7700. public function testHtml5Controls()
  7701. {
  7702. $result = $this->Form->email('User.email');
  7703. $expected = [
  7704. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7705. ];
  7706. $this->assertHtml($expected, $result);
  7707. $result = $this->Form->search('User.query');
  7708. $expected = [
  7709. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7710. ];
  7711. $this->assertHtml($expected, $result);
  7712. $result = $this->Form->search('User.query', ['value' => 'test']);
  7713. $expected = [
  7714. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7715. ];
  7716. $this->assertHtml($expected, $result);
  7717. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7718. $expected = [
  7719. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7720. ];
  7721. $this->assertHtml($expected, $result);
  7722. }
  7723. /**
  7724. * testHtml5ControlWithControl method
  7725. *
  7726. * Test accessing html5 inputs through control().
  7727. *
  7728. * @return void
  7729. */
  7730. public function testHtml5ControlWithControl()
  7731. {
  7732. $this->Form->create();
  7733. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7734. $result = $this->Form->control('website', [
  7735. 'type' => 'url',
  7736. 'val' => 'http://domain.tld',
  7737. 'label' => false
  7738. ]);
  7739. $expected = [
  7740. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7741. ];
  7742. $this->assertHtml($expected, $result);
  7743. }
  7744. /**
  7745. * testHtml5ControlException method
  7746. *
  7747. * Test errors when field name is missing.
  7748. *
  7749. * @return void
  7750. */
  7751. public function testHtml5ControlException()
  7752. {
  7753. $this->expectException(\Cake\Core\Exception\Exception::class);
  7754. $this->Form->email();
  7755. }
  7756. /**
  7757. * testRequiredAttribute method
  7758. *
  7759. * Tests that formhelper sets required attributes.
  7760. *
  7761. * @return void
  7762. */
  7763. public function testRequiredAttribute()
  7764. {
  7765. $this->article['required'] = [
  7766. 'title' => true,
  7767. 'body' => false,
  7768. ];
  7769. $this->Form->create($this->article);
  7770. $result = $this->Form->control('title');
  7771. $expected = [
  7772. 'div' => ['class' => 'input text required'],
  7773. 'label' => ['for' => 'title'],
  7774. 'Title',
  7775. '/label',
  7776. 'input' => [
  7777. 'type' => 'text',
  7778. 'name' => 'title',
  7779. 'id' => 'title',
  7780. 'required' => 'required',
  7781. ],
  7782. '/div'
  7783. ];
  7784. $this->assertHtml($expected, $result);
  7785. $result = $this->Form->control('title', ['required' => false]);
  7786. $this->assertNotContains('required', $result);
  7787. $result = $this->Form->control('body');
  7788. $expected = [
  7789. 'div' => ['class' => 'input text'],
  7790. 'label' => ['for' => 'body'],
  7791. 'Body',
  7792. '/label',
  7793. 'input' => [
  7794. 'type' => 'text',
  7795. 'name' => 'body',
  7796. 'id' => 'body',
  7797. ],
  7798. '/div'
  7799. ];
  7800. $this->assertHtml($expected, $result);
  7801. $result = $this->Form->control('body', ['required' => true]);
  7802. $this->assertContains('required', $result);
  7803. }
  7804. /**
  7805. * testControlsNotNested method
  7806. *
  7807. * Tests that it is possible to put inputs outside of the label.
  7808. *
  7809. * @return void
  7810. */
  7811. public function testControlsNotNested()
  7812. {
  7813. $this->Form->setTemplates([
  7814. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7815. 'formGroup' => '{{input}}{{label}}',
  7816. ]);
  7817. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7818. $expected = [
  7819. 'div' => ['class' => 'input checkbox'],
  7820. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7821. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7822. 'label' => ['for' => 'foo'],
  7823. 'Foo',
  7824. '/label',
  7825. '/div'
  7826. ];
  7827. $this->assertHtml($expected, $result);
  7828. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7829. $expected = [
  7830. 'div' => ['class' => 'input checkbox'],
  7831. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7832. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7833. '/div'
  7834. ];
  7835. $this->assertHtml($expected, $result);
  7836. $result = $this->Form->control('confirm', [
  7837. 'type' => 'radio',
  7838. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7839. ]);
  7840. $expected = [
  7841. 'div' => ['class' => 'input radio'],
  7842. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7843. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7844. ['label' => ['for' => 'confirm-y']],
  7845. 'Yes',
  7846. '/label',
  7847. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7848. ['label' => ['for' => 'confirm-n']],
  7849. 'No',
  7850. '/label',
  7851. '<label',
  7852. 'Confirm',
  7853. '/label',
  7854. '/div',
  7855. ];
  7856. $this->assertHtml($expected, $result);
  7857. $result = $this->Form->select('category', ['1', '2'], [
  7858. 'multiple' => 'checkbox',
  7859. 'name' => 'fish',
  7860. ]);
  7861. $expected = [
  7862. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7863. ['div' => ['class' => 'checkbox']],
  7864. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7865. ['label' => ['for' => 'fish-0']],
  7866. '1',
  7867. '/label',
  7868. '/div',
  7869. ['div' => ['class' => 'checkbox']],
  7870. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7871. ['label' => ['for' => 'fish-1']],
  7872. '2',
  7873. '/label',
  7874. '/div'
  7875. ];
  7876. $this->assertHtml($expected, $result);
  7877. }
  7878. /**
  7879. * testControlContainerTemplates method
  7880. *
  7881. * Test that *Container templates are used by input.
  7882. *
  7883. * @return void
  7884. */
  7885. public function testControlContainerTemplates()
  7886. {
  7887. $this->Form->setTemplates([
  7888. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7889. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7890. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7891. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7892. ]);
  7893. $this->article['errors'] = [
  7894. 'Article' => ['published' => 'error message']
  7895. ];
  7896. $this->Form->create($this->article);
  7897. $result = $this->Form->control('accept', [
  7898. 'type' => 'checkbox'
  7899. ]);
  7900. $expected = [
  7901. 'div' => ['class' => 'check'],
  7902. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7903. 'label' => ['for' => 'accept'],
  7904. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7905. 'Accept',
  7906. '/label',
  7907. '/div'
  7908. ];
  7909. $this->assertHtml($expected, $result);
  7910. $result = $this->Form->control('accept', [
  7911. 'type' => 'radio',
  7912. 'options' => ['Y', 'N']
  7913. ]);
  7914. $this->assertContains('<div class="rad">', $result);
  7915. $result = $this->Form->control('Article.published', [
  7916. 'type' => 'radio',
  7917. 'options' => ['Y', 'N']
  7918. ]);
  7919. $this->assertContains('<div class="rad err">', $result);
  7920. $result = $this->Form->control('Article.created', [
  7921. 'type' => 'datetime',
  7922. ]);
  7923. $this->assertContains('<div class="dt">', $result);
  7924. }
  7925. /**
  7926. * testFormGroupTemplates method
  7927. *
  7928. * Test that *Container templates are used by input.
  7929. *
  7930. * @return void
  7931. */
  7932. public function testFormGroupTemplates()
  7933. {
  7934. $this->Form->setTemplates([
  7935. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7936. ]);
  7937. $this->Form->create($this->article);
  7938. $result = $this->Form->control('accept', [
  7939. 'type' => 'radio',
  7940. 'options' => ['Y', 'N']
  7941. ]);
  7942. $this->assertContains('<div class="radio">', $result);
  7943. }
  7944. /**
  7945. * testResetTemplates method
  7946. *
  7947. * Test resetting templates.
  7948. *
  7949. * @return void
  7950. */
  7951. public function testResetTemplates()
  7952. {
  7953. $this->Form->setTemplates(['input' => '<input/>']);
  7954. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7955. $this->Form->resetTemplates();
  7956. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7957. }
  7958. /**
  7959. * testContext method
  7960. *
  7961. * Test the context method.
  7962. *
  7963. * @return void
  7964. */
  7965. public function testContext()
  7966. {
  7967. $result = $this->Form->context();
  7968. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7969. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7970. $this->assertSame($mock, $this->Form->context($mock));
  7971. $this->assertSame($mock, $this->Form->context());
  7972. }
  7973. /**
  7974. * testAutoDomId method
  7975. *
  7976. * @return void
  7977. */
  7978. public function testAutoDomId()
  7979. {
  7980. $result = $this->Form->text('field', ['id' => true]);
  7981. $expected = [
  7982. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7983. ];
  7984. $this->assertHtml($expected, $result);
  7985. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7986. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7987. $expected = [
  7988. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7989. ['label' => ['for' => 'field-0']],
  7990. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7991. 'option A',
  7992. '/label',
  7993. ['label' => ['for' => 'field-1']],
  7994. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7995. 'option B',
  7996. '/label',
  7997. ];
  7998. $this->assertHtml($expected, $result);
  7999. $result = $this->Form->select(
  8000. 'multi_field',
  8001. ['first', 'second'],
  8002. ['multiple' => 'checkbox', 'id' => true]
  8003. );
  8004. $expected = [
  8005. 'input' => [
  8006. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8007. ],
  8008. ['div' => ['class' => 'checkbox']],
  8009. ['label' => ['for' => 'multi-field-0']],
  8010. ['input' => [
  8011. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8012. 'value' => '0', 'id' => 'multi-field-0'
  8013. ]],
  8014. 'first',
  8015. '/label',
  8016. '/div',
  8017. ['div' => ['class' => 'checkbox']],
  8018. ['label' => ['for' => 'multi-field-1']],
  8019. ['input' => [
  8020. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8021. 'value' => '1', 'id' => 'multi-field-1'
  8022. ]],
  8023. 'second',
  8024. '/label',
  8025. '/div',
  8026. ];
  8027. $this->assertHtml($expected, $result);
  8028. }
  8029. /**
  8030. * Test the basic setters and getters for value sources
  8031. *
  8032. * @return void
  8033. */
  8034. public function testFormValueSourcesSettersGetters()
  8035. {
  8036. $this->Form->request = $this->Form->request
  8037. ->withData('id', '1')
  8038. ->withQueryParams(['id' => '2']);
  8039. $expected = ['context'];
  8040. $result = $this->Form->getValueSources();
  8041. $this->assertEquals($expected, $result);
  8042. $expected = ['query', 'data', 'context'];
  8043. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8044. $result = $this->Form->getValueSources();
  8045. $this->assertEquals($expected, $result);
  8046. $this->Form->setValueSources(['context']);
  8047. $expected = '1';
  8048. $result = $this->Form->getSourceValue('id');
  8049. $this->assertEquals($expected, $result);
  8050. $this->Form->setValueSources('query');
  8051. $expected = ['query'];
  8052. $result = $this->Form->getValueSources();
  8053. $this->assertEquals($expected, $result);
  8054. $expected = '2';
  8055. $result = $this->Form->getSourceValue('id');
  8056. $this->assertEquals($expected, $result);
  8057. $this->Form->setValueSources(['data']);
  8058. $expected = '1';
  8059. $result = $this->Form->getSourceValue('id');
  8060. $this->assertEquals($expected, $result);
  8061. $this->Form->setValueSources(['query', 'data']);
  8062. $expected = '2';
  8063. $result = $this->Form->getSourceValue('id');
  8064. $this->assertEquals($expected, $result);
  8065. }
  8066. /**
  8067. * Tests the different input rendering values based on sources values switching
  8068. *
  8069. * @return void
  8070. */
  8071. public function testFormValueSourcesSingleSwitchRendering()
  8072. {
  8073. $this->loadFixtures('Articles');
  8074. $articles = TableRegistry::get('Articles');
  8075. $article = new Article();
  8076. $articles->patchEntity($article, ['id' => '3']);
  8077. $this->Form->create($article);
  8078. $this->Form->setValueSources(['context']);
  8079. $result = $this->Form->control('id');
  8080. $expected = [
  8081. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8082. ];
  8083. $this->assertHtml($expected, $result);
  8084. $this->Form->request = $this->Form->request->withQueryParams(['id' => 5]);
  8085. $this->Form->setValueSources(['query']);
  8086. $result = $this->Form->control('id');
  8087. $expected = [
  8088. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8089. ];
  8090. $this->assertHtml($expected, $result);
  8091. $this->Form->request = $this->Form->request
  8092. ->withQueryParams(['id' => '5a'])
  8093. ->withData('id', '5b');
  8094. $this->Form->setValueSources(['context']);
  8095. $this->Form->create($article);
  8096. $result = $this->Form->control('id');
  8097. $expected = [
  8098. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8099. ];
  8100. $this->assertHtml($expected, $result);
  8101. $this->Form->setValueSources(['data']);
  8102. $this->Form->create($article);
  8103. $result = $this->Form->control('id');
  8104. $expected = [
  8105. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8106. ];
  8107. $this->assertHtml($expected, $result);
  8108. $this->Form->setValueSources(['query']);
  8109. $result = $this->Form->control('id');
  8110. $expected = [
  8111. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8112. ];
  8113. $this->assertHtml($expected, $result);
  8114. }
  8115. /**
  8116. * Tests the different input rendering values based on sources values switching while supplying
  8117. * an entity (base context) and multiple sources (such as data, query)
  8118. *
  8119. * @return void
  8120. */
  8121. public function testFormValueSourcesListSwitchRendering()
  8122. {
  8123. $this->loadFixtures('Articles');
  8124. $articles = TableRegistry::get('Articles');
  8125. $article = new Article();
  8126. $articles->patchEntity($article, ['id' => '3']);
  8127. $this->Form->request = $this->Form->request->withQueryParams(['id' => '9']);
  8128. $this->Form->create($article);
  8129. $this->Form->setValueSources(['context', 'query']);
  8130. $result = $this->Form->control('id');
  8131. $expected = [
  8132. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8133. ];
  8134. $this->assertHtml($expected, $result);
  8135. $this->Form->setValueSources(['query', 'context']);
  8136. $result = $this->Form->control('id');
  8137. $expected = [
  8138. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8139. ];
  8140. $this->assertHtml($expected, $result);
  8141. $this->Form->setValueSources(['data', 'query', 'context']);
  8142. $result = $this->Form->control('id');
  8143. $expected = [
  8144. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8145. ];
  8146. $this->assertHtml($expected, $result);
  8147. $this->Form->request = $this->Form->request
  8148. ->withData('id', '8')
  8149. ->withQueryParams(['id' => '9']);
  8150. $this->Form->setValueSources(['data', 'query', 'context']);
  8151. $result = $this->Form->control('id');
  8152. $expected = [
  8153. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8154. ];
  8155. $this->assertHtml($expected, $result);
  8156. }
  8157. /**
  8158. * Test the different form input renderings based on values sources switchings through form options
  8159. *
  8160. * @return void
  8161. */
  8162. public function testFormValueSourcesSwitchViaOptionsRendering()
  8163. {
  8164. $this->loadFixtures('Articles');
  8165. $articles = TableRegistry::get('Articles');
  8166. $article = new Article();
  8167. $articles->patchEntity($article, ['id' => '3']);
  8168. $this->Form->request = $this->Form->request->withData('id', 4)->withQueryParams(['id' => 5]);
  8169. $this->Form->create($article, ['valueSources' => 'query']);
  8170. $result = $this->Form->control('id');
  8171. $expected = [
  8172. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8173. ];
  8174. $this->assertHtml($expected, $result);
  8175. $result = $this->Form->getSourceValue('id');
  8176. $this->assertEquals('5', $result);
  8177. $this->Form->setValueSources(['context']);
  8178. $this->Form->create($article, ['valueSources' => 'query']);
  8179. $result = $this->Form->control('id');
  8180. $expected = [
  8181. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8182. ];
  8183. $this->assertHtml($expected, $result);
  8184. $result = $this->Form->getSourceValue('id');
  8185. $this->assertEquals('5', $result);
  8186. $this->Form->setValueSources(['query']);
  8187. $this->Form->create($article, ['valueSources' => 'data']);
  8188. $result = $this->Form->control('id');
  8189. $expected = [
  8190. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8191. ];
  8192. $this->assertHtml($expected, $result);
  8193. $result = $this->Form->getSourceValue('id');
  8194. $this->assertEquals('4', $result);
  8195. $this->Form->setValueSources(['query']);
  8196. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8197. $result = $this->Form->control('id');
  8198. $expected = [
  8199. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8200. ];
  8201. $this->assertHtml($expected, $result);
  8202. $result = $this->Form->getSourceValue('id');
  8203. $this->assertEquals('4', $result);
  8204. }
  8205. /**
  8206. * Test the different form input renderings based on values sources switchings through form options
  8207. *
  8208. * @return void
  8209. */
  8210. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8211. {
  8212. $this->loadFixtures('Articles');
  8213. $articles = TableRegistry::get('Articles');
  8214. $article = new Article();
  8215. $articles->patchEntity($article, ['id' => '3']);
  8216. $this->Form->request = $this->Form->request->withData('id', 10)->withQueryParams(['id' => 11]);
  8217. $this->Form->setValueSources(['context'])
  8218. ->create($article, ['valueSources' => ['query', 'data']]);
  8219. $result = $this->Form->control('id');
  8220. $expected = [
  8221. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8222. ];
  8223. $this->assertHtml($expected, $result);
  8224. $result = $this->Form->getSourceValue('id');
  8225. $this->assertEquals('11', $result);
  8226. $this->Form->request = $this->Form->request->withQueryParams([]);
  8227. $this->Form->setValueSources(['context'])
  8228. ->create($article, ['valueSources' => ['query', 'data']]);
  8229. $result = $this->Form->control('id');
  8230. $expected = [
  8231. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8232. ];
  8233. $this->assertHtml($expected, $result);
  8234. $result = $this->Form->getSourceValue('id');
  8235. $this->assertEquals('10', $result);
  8236. }
  8237. /**
  8238. * Test the different form values sources resetting through From::end();
  8239. *
  8240. * @return void
  8241. */
  8242. public function testFormValueSourcesResetViaEnd()
  8243. {
  8244. $expected = ['context'];
  8245. $result = $this->Form->getValueSources();
  8246. $this->assertEquals($expected, $result);
  8247. $expected = ['query', 'context', 'data'];
  8248. $this->Form->setValueSources(['query', 'context', 'data']);
  8249. $result = $this->Form->getValueSources();
  8250. $this->assertEquals($expected, $result);
  8251. $this->Form->create();
  8252. $result = $this->Form->getValueSources();
  8253. $this->assertEquals($expected, $result);
  8254. $this->Form->end();
  8255. $result = $this->Form->getValueSources();
  8256. $this->assertEquals(['context'], $result);
  8257. }
  8258. /**
  8259. * Test sources values defaults handling
  8260. *
  8261. * @return void
  8262. */
  8263. public function testFormValueSourcesDefaults()
  8264. {
  8265. $this->Form->request = $this->Form->request->withQueryParams(['password' => 'open Sesame']);
  8266. $this->Form->create();
  8267. $result = $this->Form->password('password');
  8268. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8269. $this->assertHtml($expected, $result);
  8270. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8271. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8272. $this->assertHtml($expected, $result);
  8273. $this->Form->setValueSources('query');
  8274. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8275. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8276. $this->assertHtml($expected, $result);
  8277. $this->Form->setValueSources('data');
  8278. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8279. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8280. $this->assertHtml($expected, $result);
  8281. }
  8282. /**
  8283. * Test sources values schema defaults handling
  8284. *
  8285. * @return void
  8286. */
  8287. public function testSourcesValueDoesntExistPassThrough()
  8288. {
  8289. $this->Form->request = $this->Form->request->withQueryParams(['category' => 'sesame-cookies']);
  8290. $articles = TableRegistry::get('Articles');
  8291. $entity = $articles->newEntity();
  8292. $this->Form->create($entity);
  8293. $this->Form->setValueSources(['query', 'context']);
  8294. $result = $this->Form->getSourceValue('category');
  8295. $this->assertEquals('sesame-cookies', $result);
  8296. $this->Form->setValueSources(['context', 'query']);
  8297. $result = $this->Form->getSourceValue('category');
  8298. $this->assertEquals('sesame-cookies', $result);
  8299. }
  8300. /**
  8301. * testNestedLabelInput method
  8302. *
  8303. * Test the `nestedInput` parameter
  8304. *
  8305. * @group deprecated
  8306. * @return void
  8307. */
  8308. public function testNestedLabelInput()
  8309. {
  8310. $this->deprecated(function () {
  8311. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8312. $expected = [
  8313. 'div' => ['class' => 'input text'],
  8314. 'label' => ['for' => 'foo'],
  8315. ['input' => [
  8316. 'type' => 'text',
  8317. 'name' => 'foo',
  8318. 'id' => 'foo'
  8319. ]],
  8320. 'Foo',
  8321. '/label',
  8322. '/div'
  8323. ];
  8324. $this->assertHtml($expected, $result);
  8325. });
  8326. }
  8327. /**
  8328. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8329. *
  8330. * This test makes sure `false` excludes the label from the render
  8331. *
  8332. * @return void
  8333. */
  8334. public function testControlLabelManipulationDisableLabels()
  8335. {
  8336. $result = $this->Form->control('test', [
  8337. 'type' => 'radio',
  8338. 'options' => ['A', 'B'],
  8339. 'labelOptions' => false
  8340. ]);
  8341. $expected = [
  8342. ['div' => ['class' => 'input radio']],
  8343. '<label',
  8344. 'Test',
  8345. '/label',
  8346. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8347. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8348. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8349. '/div',
  8350. ];
  8351. $this->assertHtml($expected, $result);
  8352. $result = $this->Form->control('checkbox1', [
  8353. 'label' => 'My checkboxes',
  8354. 'multiple' => 'checkbox',
  8355. 'type' => 'select',
  8356. 'options' => [
  8357. ['text' => 'First Checkbox', 'value' => 1],
  8358. ['text' => 'Second Checkbox', 'value' => 2]
  8359. ],
  8360. 'labelOptions' => false
  8361. ]);
  8362. $expected = [
  8363. ['div' => ['class' => 'input select']],
  8364. ['label' => ['for' => 'checkbox1']],
  8365. 'My checkboxes',
  8366. '/label',
  8367. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8368. ['div' => ['class' => 'checkbox']],
  8369. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8370. '/div',
  8371. ['div' => ['class' => 'checkbox']],
  8372. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8373. '/div',
  8374. '/div'
  8375. ];
  8376. $this->assertHtml($expected, $result);
  8377. }
  8378. /**
  8379. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8380. *
  8381. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8382. * added to the class if checked.
  8383. *
  8384. * Also checks to make sure any custom attributes are rendered correctly
  8385. *
  8386. * @return void
  8387. */
  8388. public function testControlLabelManipulationRadios()
  8389. {
  8390. $result = $this->Form->control('test', [
  8391. 'type' => 'radio',
  8392. 'options' => ['A', 'B'],
  8393. 'labelOptions' => ['class' => 'custom-class']
  8394. ]);
  8395. $expected = [
  8396. ['div' => ['class' => 'input radio']],
  8397. '<label',
  8398. 'Test',
  8399. '/label',
  8400. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8401. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8402. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8403. 'A',
  8404. '/label',
  8405. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8406. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8407. 'B',
  8408. '/label',
  8409. '/div',
  8410. ];
  8411. $this->assertHtml($expected, $result);
  8412. $result = $this->Form->control('test', [
  8413. 'type' => 'radio',
  8414. 'options' => ['A', 'B'],
  8415. 'value' => 1,
  8416. 'labelOptions' => ['class' => 'custom-class']
  8417. ]);
  8418. $expected = [
  8419. ['div' => ['class' => 'input radio']],
  8420. '<label',
  8421. 'Test',
  8422. '/label',
  8423. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8424. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8425. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8426. 'A',
  8427. '/label',
  8428. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8429. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8430. 'B',
  8431. '/label',
  8432. '/div',
  8433. ];
  8434. $this->assertHtml($expected, $result);
  8435. $result = $this->Form->control('test', [
  8436. 'type' => 'radio',
  8437. 'options' => ['A', 'B'],
  8438. 'value' => 1,
  8439. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8440. ]);
  8441. $expected = [
  8442. ['div' => ['class' => 'input radio']],
  8443. '<label',
  8444. 'Test',
  8445. '/label',
  8446. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8447. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8448. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8449. 'A',
  8450. '/label',
  8451. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8452. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8453. 'B',
  8454. '/label',
  8455. '/div',
  8456. ];
  8457. $this->assertHtml($expected, $result);
  8458. $result = $this->Form->radio('test', ['A', 'B'], [
  8459. 'label' => [
  8460. 'class' => ['custom-class', 'another-class'],
  8461. 'data-name' => 'bob'
  8462. ],
  8463. 'value' => 1
  8464. ]);
  8465. $expected = [
  8466. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8467. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8468. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8469. 'A',
  8470. '/label',
  8471. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8472. ['input' => [
  8473. 'type' => 'radio',
  8474. 'name' => 'test',
  8475. 'value' => '1',
  8476. 'id' => 'test-1',
  8477. 'checked' => 'checked'
  8478. ]],
  8479. 'B',
  8480. '/label',
  8481. ];
  8482. $this->assertHtml($expected, $result);
  8483. }
  8484. /**
  8485. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8486. *
  8487. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8488. * added to the class if checked.
  8489. *
  8490. * Also checks to make sure any custom attributes are rendered correctly
  8491. *
  8492. * @return void
  8493. */
  8494. public function testControlLabelManipulationCheckboxes()
  8495. {
  8496. $result = $this->Form->control('checkbox1', [
  8497. 'label' => 'My checkboxes',
  8498. 'multiple' => 'checkbox',
  8499. 'type' => 'select',
  8500. 'options' => [
  8501. ['text' => 'First Checkbox', 'value' => 1],
  8502. ['text' => 'Second Checkbox', 'value' => 2]
  8503. ],
  8504. 'labelOptions' => ['class' => 'custom-class'],
  8505. 'value' => ['1']
  8506. ]);
  8507. $expected = [
  8508. ['div' => ['class' => 'input select']],
  8509. ['label' => ['for' => 'checkbox1']],
  8510. 'My checkboxes',
  8511. '/label',
  8512. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8513. ['div' => ['class' => 'checkbox']],
  8514. ['label' => [
  8515. 'class' => 'custom-class selected',
  8516. 'for' => 'checkbox1-1'
  8517. ]],
  8518. ['input' => [
  8519. 'type' => 'checkbox',
  8520. 'name' => 'checkbox1[]',
  8521. 'value' => '1',
  8522. 'id' => 'checkbox1-1',
  8523. 'checked' => 'checked'
  8524. ]],
  8525. 'First Checkbox',
  8526. '/label',
  8527. '/div',
  8528. ['div' => ['class' => 'checkbox']],
  8529. ['label' => [
  8530. 'class' => 'custom-class',
  8531. 'for' => 'checkbox1-2'
  8532. ]],
  8533. ['input' => [
  8534. 'type' => 'checkbox',
  8535. 'name' => 'checkbox1[]',
  8536. 'value' => '2',
  8537. 'id' => 'checkbox1-2'
  8538. ]],
  8539. 'Second Checkbox',
  8540. '/label',
  8541. '/div',
  8542. '/div'
  8543. ];
  8544. $this->assertHtml($expected, $result);
  8545. $result = $this->Form->control('checkbox1', [
  8546. 'label' => 'My checkboxes',
  8547. 'multiple' => 'checkbox',
  8548. 'type' => 'select',
  8549. 'options' => [
  8550. ['text' => 'First Checkbox', 'value' => 1],
  8551. ['text' => 'Second Checkbox', 'value' => 2]
  8552. ],
  8553. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8554. 'value' => ['1']
  8555. ]);
  8556. $expected = [
  8557. ['div' => ['class' => 'input select']],
  8558. ['label' => ['for' => 'checkbox1']],
  8559. 'My checkboxes',
  8560. '/label',
  8561. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8562. ['div' => ['class' => 'checkbox']],
  8563. ['label' => [
  8564. 'class' => 'custom-class another-class selected',
  8565. 'data-name' => 'bob',
  8566. 'for' => 'checkbox1-1'
  8567. ]],
  8568. ['input' => [
  8569. 'type' => 'checkbox',
  8570. 'name' => 'checkbox1[]',
  8571. 'value' => '1',
  8572. 'id' => 'checkbox1-1',
  8573. 'checked' => 'checked'
  8574. ]],
  8575. 'First Checkbox',
  8576. '/label',
  8577. '/div',
  8578. ['div' => ['class' => 'checkbox']],
  8579. ['label' => [
  8580. 'class' => 'custom-class another-class',
  8581. 'data-name' => 'bob',
  8582. 'for' => 'checkbox1-2'
  8583. ]],
  8584. ['input' => [
  8585. 'type' => 'checkbox',
  8586. 'name' => 'checkbox1[]',
  8587. 'value' => '2',
  8588. 'id' => 'checkbox1-2'
  8589. ]],
  8590. 'Second Checkbox',
  8591. '/label',
  8592. '/div',
  8593. '/div'
  8594. ];
  8595. $this->assertHtml($expected, $result);
  8596. }
  8597. }