FormHelperTest.php 308 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316
  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. use Cake\View\Widget\WidgetLocator;
  29. /**
  30. * Test stub.
  31. */
  32. class Article extends Entity
  33. {
  34. }
  35. /**
  36. * Contact class
  37. */
  38. class ContactsTable extends Table
  39. {
  40. /**
  41. * Default schema
  42. *
  43. * @var array
  44. */
  45. protected $_schema = [
  46. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  47. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  51. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  52. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  53. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  54. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  55. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  56. ];
  57. /**
  58. * Initializes the schema
  59. *
  60. * @return void
  61. */
  62. public function initialize(array $config)
  63. {
  64. $this->setSchema($this->_schema);
  65. }
  66. }
  67. /**
  68. * ValidateUser class
  69. */
  70. class ValidateUsersTable extends Table
  71. {
  72. /**
  73. * schema method
  74. *
  75. * @var array
  76. */
  77. protected $_schema = [
  78. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  79. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  81. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  82. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  83. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  84. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  85. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  86. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  87. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  88. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  89. ];
  90. /**
  91. * Initializes the schema
  92. *
  93. * @return void
  94. */
  95. public function initialize(array $config)
  96. {
  97. $this->setSchema($this->_schema);
  98. }
  99. }
  100. /**
  101. * FormHelperTest class
  102. *
  103. * @property \Cake\View\Helper\FormHelper $Form
  104. * @property \Cake\View\View $View
  105. */
  106. class FormHelperTest extends TestCase
  107. {
  108. /**
  109. * Fixtures to be used
  110. *
  111. * @var array
  112. */
  113. public $fixtures = ['core.articles', 'core.comments'];
  114. /**
  115. * Do not load the fixtures by default
  116. *
  117. * @var bool
  118. */
  119. public $autoFixtures = false;
  120. /**
  121. * @var array
  122. */
  123. protected $article = [];
  124. /**
  125. * setUp method
  126. *
  127. * @return void
  128. */
  129. public function setUp()
  130. {
  131. parent::setUp();
  132. Configure::write('Config.language', 'eng');
  133. Configure::write('App.base', '');
  134. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  135. $this->View = new View();
  136. $this->Form = new FormHelper($this->View);
  137. $request = new ServerRequest([
  138. 'webroot' => '',
  139. 'base' => '',
  140. 'url' => '/articles/add',
  141. 'params' => [
  142. 'controller' => 'articles',
  143. 'action' => 'add',
  144. ]
  145. ]);
  146. $this->Form->Url->request = $this->Form->request = $request;
  147. $this->dateRegex = [
  148. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  149. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  150. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  152. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  153. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  154. ];
  155. $this->article = [
  156. 'schema' => [
  157. 'id' => ['type' => 'integer'],
  158. 'author_id' => ['type' => 'integer', 'null' => true],
  159. 'title' => ['type' => 'string', 'null' => true],
  160. 'body' => 'text',
  161. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  162. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  163. ],
  164. 'required' => [
  165. 'author_id' => true,
  166. 'title' => true,
  167. ]
  168. ];
  169. Security::setSalt('foo!');
  170. Router::connect('/:controller', ['action' => 'index']);
  171. Router::connect('/:controller/:action/*');
  172. }
  173. /**
  174. * tearDown method
  175. *
  176. * @return void
  177. */
  178. public function tearDown()
  179. {
  180. parent::tearDown();
  181. unset($this->Form, $this->Controller, $this->View);
  182. TableRegistry::clear();
  183. }
  184. /**
  185. * Test construct() with the templates option.
  186. *
  187. * @return void
  188. */
  189. public function testConstructTemplatesFile()
  190. {
  191. $helper = new FormHelper($this->View, [
  192. 'templates' => 'htmlhelper_tags'
  193. ]);
  194. $result = $helper->control('name');
  195. $this->assertContains('<input', $result);
  196. }
  197. /**
  198. * Test that when specifying custom widgets the config array for that widget
  199. * is overwritten instead of merged.
  200. *
  201. * @return void
  202. */
  203. public function testConstructWithWidgets()
  204. {
  205. $config = [
  206. 'widgets' => [
  207. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  208. ]
  209. ];
  210. $helper = new FormHelper($this->View, $config);
  211. $locator = $helper->getWidgetLocator();
  212. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  213. }
  214. /**
  215. * Test that when specifying custom widgets config file and it should be
  216. * added to widgets array. WidgetLocator will load widgets in constructor.
  217. *
  218. * @return void
  219. */
  220. public function testConstructWithWidgetsConfig()
  221. {
  222. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  223. $locator = $helper->getWidgetLocator();
  224. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  225. }
  226. /**
  227. * Test setting the widget locator
  228. *
  229. * @return void
  230. */
  231. public function testSetAndGetWidgetLocator()
  232. {
  233. $helper = new FormHelper($this->View);
  234. $locator = new WidgetLocator($helper->templater(), $this->View);
  235. $helper->setWidgetLocator($locator);
  236. $this->assertSame($locator, $helper->getWidgetLocator());
  237. }
  238. /**
  239. * Test registering a new widget class and rendering it.
  240. *
  241. * @return void
  242. */
  243. public function testAddWidgetAndRenderWidget()
  244. {
  245. $data = [
  246. 'val' => 1
  247. ];
  248. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  249. $this->Form->addWidget('test', $mock);
  250. $mock->expects($this->once())
  251. ->method('render')
  252. ->with($data)
  253. ->will($this->returnValue('HTML'));
  254. $result = $this->Form->widget('test', $data);
  255. $this->assertEquals('HTML', $result);
  256. }
  257. /**
  258. * Test that secureFields() of widget is called after calling render(),
  259. * not before.
  260. *
  261. * @return void
  262. */
  263. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  264. {
  265. $data = [
  266. 'val' => 1,
  267. 'name' => 'test'
  268. ];
  269. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  270. $this->Form->addWidget('test', $mock);
  271. $mock->expects($this->at(0))
  272. ->method('render')
  273. ->with($data)
  274. ->will($this->returnValue('HTML'));
  275. $mock->expects($this->at(1))
  276. ->method('secureFields')
  277. ->with($data)
  278. ->will($this->returnValue(['test']));
  279. $result = $this->Form->widget('test', $data + ['secure' => true]);
  280. $this->assertEquals('HTML', $result);
  281. }
  282. /**
  283. * Test that empty string is not added to secure fields list when
  284. * rendering input widget without name.
  285. *
  286. * @return void
  287. */
  288. public function testRenderingWidgetWithEmptyName()
  289. {
  290. $this->assertEquals([], $this->Form->fields);
  291. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  292. $this->assertEquals('<select name=""></select>', $result);
  293. $this->assertEquals([], $this->Form->fields);
  294. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  295. $this->assertEquals('<select name="0"></select>', $result);
  296. $this->assertEquals(['0'], $this->Form->fields);
  297. }
  298. /**
  299. * Test registering an invalid widget class.
  300. *
  301. * @return void
  302. */
  303. public function testAddWidgetInvalid()
  304. {
  305. $this->expectException(\RuntimeException::class);
  306. $mock = new \StdClass();
  307. $this->Form->addWidget('test', $mock);
  308. $this->Form->widget('test');
  309. }
  310. /**
  311. * Test adding a new context class.
  312. *
  313. * @return void
  314. */
  315. public function testAddContextProvider()
  316. {
  317. $context = 'My data';
  318. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  319. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  320. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  321. $this->assertEquals($context, $data['entity']);
  322. return $stub;
  323. });
  324. $this->Form->create($context);
  325. $result = $this->Form->context();
  326. $this->assertSame($stub, $result);
  327. }
  328. /**
  329. * Test replacing a context class.
  330. *
  331. * @return void
  332. */
  333. public function testAddContextProviderReplace()
  334. {
  335. $entity = new Article();
  336. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  337. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  338. return $stub;
  339. });
  340. $this->Form->create($entity);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test overriding a context class.
  346. *
  347. * @return void
  348. */
  349. public function testAddContextProviderAdd()
  350. {
  351. $entity = new Article();
  352. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  353. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  354. if ($data['entity'] instanceof Entity) {
  355. return $stub;
  356. }
  357. });
  358. $this->Form->create($entity);
  359. $result = $this->Form->context();
  360. $this->assertSame($stub, $result);
  361. }
  362. /**
  363. * Test adding an invalid context class.
  364. *
  365. * @return void
  366. */
  367. public function testAddContextProviderInvalid()
  368. {
  369. $this->expectException(\RuntimeException::class);
  370. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  371. $context = 'My data';
  372. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  373. return new \StdClass();
  374. });
  375. $this->Form->create($context);
  376. }
  377. /**
  378. * Provides context options for create().
  379. *
  380. * @return array
  381. */
  382. public function contextSelectionProvider()
  383. {
  384. $entity = new Article();
  385. $collection = new Collection([$entity]);
  386. $emptyCollection = new Collection([]);
  387. $arrayObject = new \ArrayObject([]);
  388. $data = [
  389. 'schema' => [
  390. 'title' => ['type' => 'string']
  391. ]
  392. ];
  393. $form = new Form();
  394. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  395. return [
  396. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  397. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  398. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  399. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  400. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  401. 'form' => [$form, 'Cake\View\Form\FormContext'],
  402. 'none' => [null, 'Cake\View\Form\NullContext'],
  403. 'false' => [false, 'Cake\View\Form\NullContext'],
  404. 'custom' => [$custom, get_class($custom)]
  405. ];
  406. }
  407. /**
  408. * Test default context selection in create()
  409. *
  410. * @dataProvider contextSelectionProvider
  411. * @return void
  412. */
  413. public function testCreateContextSelectionBuiltIn($data, $class)
  414. {
  415. $this->loadFixtures('Articles');
  416. $this->Form->create($data);
  417. $this->assertInstanceOf($class, $this->Form->context());
  418. }
  419. /**
  420. * Data provider for type option.
  421. *
  422. * @return array
  423. */
  424. public static function requestTypeProvider()
  425. {
  426. return [
  427. // type, method, override
  428. ['post', 'post', 'POST'],
  429. ['put', 'post', 'PUT'],
  430. ['patch', 'post', 'PATCH'],
  431. ['delete', 'post', 'DELETE'],
  432. ];
  433. }
  434. /**
  435. * Test creating file forms.
  436. *
  437. * @return void
  438. */
  439. public function testCreateFile()
  440. {
  441. $encoding = strtolower(Configure::read('App.encoding'));
  442. $result = $this->Form->create(false, ['type' => 'file']);
  443. $expected = [
  444. 'form' => [
  445. 'method' => 'post', 'action' => '/articles/add',
  446. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  447. ],
  448. 'div' => ['style' => 'display:none;'],
  449. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  450. '/div'
  451. ];
  452. $this->assertHtml($expected, $result);
  453. }
  454. /**
  455. * Test creating GET forms.
  456. *
  457. * @return void
  458. */
  459. public function testCreateGet()
  460. {
  461. $encoding = strtolower(Configure::read('App.encoding'));
  462. $result = $this->Form->create(false, ['type' => 'get']);
  463. $expected = ['form' => [
  464. 'method' => 'get', 'action' => '/articles/add',
  465. 'accept-charset' => $encoding
  466. ]];
  467. $this->assertHtml($expected, $result);
  468. }
  469. /**
  470. * Test explicit method/enctype options.
  471. *
  472. * Explicit method overwrites inferred method from 'type'
  473. *
  474. * @return void
  475. */
  476. public function testCreateExplicitMethodEnctype()
  477. {
  478. $encoding = strtolower(Configure::read('App.encoding'));
  479. $result = $this->Form->create(false, [
  480. 'type' => 'get',
  481. 'method' => 'put',
  482. 'enctype' => 'multipart/form-data'
  483. ]);
  484. $expected = ['form' => [
  485. 'method' => 'put',
  486. 'action' => '/articles/add',
  487. 'enctype' => 'multipart/form-data',
  488. 'accept-charset' => $encoding
  489. ]];
  490. $this->assertHtml($expected, $result);
  491. }
  492. /**
  493. * Test create() with the templates option.
  494. *
  495. * @return void
  496. */
  497. public function testCreateTemplatesArray()
  498. {
  499. $result = $this->Form->create($this->article, [
  500. 'templates' => [
  501. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  502. ]
  503. ]);
  504. $expected = [
  505. 'form' => [
  506. 'class' => 'form-horizontal',
  507. 'method' => 'post',
  508. 'action' => '/articles/add',
  509. 'accept-charset' => 'utf-8'
  510. ]
  511. ];
  512. $this->assertHtml($expected, $result);
  513. }
  514. /**
  515. * Test create() with the templates option.
  516. *
  517. * @return void
  518. */
  519. public function testCreateTemplatesFile()
  520. {
  521. $result = $this->Form->create($this->article, [
  522. 'templates' => 'htmlhelper_tags',
  523. ]);
  524. $expected = [
  525. 'start form',
  526. 'div' => ['class' => 'hidden'],
  527. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  528. '/div'
  529. ];
  530. $this->assertHtml($expected, $result);
  531. }
  532. /**
  533. * Test that create() and end() restore templates.
  534. *
  535. * @return void
  536. */
  537. public function testCreateEndRestoreTemplates()
  538. {
  539. $this->Form->create($this->article, [
  540. 'templates' => ['input' => 'custom input element']
  541. ]);
  542. $this->Form->end();
  543. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  544. }
  545. /**
  546. * Test using template vars in various templates used by control() method.
  547. *
  548. * @return void
  549. */
  550. public function testControlTemplateVars()
  551. {
  552. $result = $this->Form->control('text', [
  553. 'templates' => [
  554. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  555. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  556. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  557. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  558. ],
  559. 'templateVars' => [
  560. 'forinput' => 'in-input',
  561. 'forlabel' => 'in-label',
  562. 'forgroup' => 'in-group',
  563. 'forcontainer' => 'in-container'
  564. ]
  565. ]);
  566. $expected = [
  567. 'div' => ['class'],
  568. 'label' => ['for'],
  569. 'Text in-label',
  570. '/label',
  571. 'in-group',
  572. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  573. 'in-container',
  574. '/div',
  575. ];
  576. $this->assertHtml($expected, $result);
  577. }
  578. /**
  579. * Test ensuring template variables work in template files loaded
  580. * during control().
  581. *
  582. * @return void
  583. */
  584. public function testControlTemplatesFromFile()
  585. {
  586. $result = $this->Form->control('title', [
  587. 'templates' => 'test_templates',
  588. 'templateVars' => [
  589. 'forcontainer' => 'container-data'
  590. ]
  591. ]);
  592. $expected = [
  593. 'div' => ['class'],
  594. 'label' => ['for'],
  595. 'Title',
  596. '/label',
  597. 'input' => ['name', 'type' => 'text', 'id'],
  598. 'container-data',
  599. '/div',
  600. ];
  601. $this->assertHtml($expected, $result);
  602. }
  603. /**
  604. * Test using template vars in inputSubmit and submitContainer template.
  605. *
  606. * @return void
  607. */
  608. public function testSubmitTemplateVars()
  609. {
  610. $this->Form->setTemplates([
  611. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  612. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  613. ]);
  614. $result = $this->Form->submit('Submit', [
  615. 'templateVars' => [
  616. 'forinput' => 'in-input',
  617. 'forcontainer' => 'in-container'
  618. ]
  619. ]);
  620. $expected = [
  621. 'div' => ['class'],
  622. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  623. 'in-container',
  624. '/div',
  625. ];
  626. $this->assertHtml($expected, $result);
  627. }
  628. /**
  629. * test the create() method
  630. *
  631. * @dataProvider requestTypeProvider
  632. * @return void
  633. */
  634. public function testCreateTypeOptions($type, $method, $override)
  635. {
  636. $encoding = strtolower(Configure::read('App.encoding'));
  637. $result = $this->Form->create(false, ['type' => $type]);
  638. $expected = [
  639. 'form' => [
  640. 'method' => $method, 'action' => '/articles/add',
  641. 'accept-charset' => $encoding
  642. ],
  643. 'div' => ['style' => 'display:none;'],
  644. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  645. '/div'
  646. ];
  647. $this->assertHtml($expected, $result);
  648. }
  649. /**
  650. * Test using template vars in Create (formStart template)
  651. *
  652. * @return void
  653. */
  654. public function testCreateTemplateVars()
  655. {
  656. $result = $this->Form->create($this->article, [
  657. 'templates' => [
  658. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  659. ],
  660. 'templateVars' => ['header' => 'headertext']
  661. ]);
  662. $expected = [
  663. 'h4' => ['class'],
  664. 'headertext',
  665. '/h4',
  666. 'form' => [
  667. 'method' => 'post',
  668. 'action' => '/articles/add',
  669. 'accept-charset' => 'utf-8'
  670. ]
  671. ];
  672. $this->assertHtml($expected, $result);
  673. }
  674. /**
  675. * Test opening a form for an update operation.
  676. *
  677. * @return void
  678. */
  679. public function testCreateUpdateForm()
  680. {
  681. $encoding = strtolower(Configure::read('App.encoding'));
  682. $this->Form->request = $this->Form->request
  683. ->withRequestTarget('/articles/edit/1')
  684. ->withParam('action', 'edit');
  685. $this->article['defaults']['id'] = 1;
  686. $result = $this->Form->create($this->article);
  687. $expected = [
  688. 'form' => [
  689. 'method' => 'post',
  690. 'action' => '/articles/edit/1',
  691. 'accept-charset' => $encoding
  692. ],
  693. 'div' => ['style' => 'display:none;'],
  694. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  695. '/div'
  696. ];
  697. $this->assertHtml($expected, $result);
  698. }
  699. /**
  700. * test create() with automatic url generation
  701. *
  702. * @return void
  703. */
  704. public function testCreateAutoUrl()
  705. {
  706. $encoding = strtolower(Configure::read('App.encoding'));
  707. $this->Form->request = $this->Form->request
  708. ->withRequestTarget('/articles/delete/10')
  709. ->withParam('action', 'delete');
  710. $result = $this->Form->create($this->article);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post', 'action' => '/articles/delete/10',
  714. 'accept-charset' => $encoding
  715. ],
  716. 'div' => ['style' => 'display:none;'],
  717. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  718. '/div'
  719. ];
  720. $this->assertHtml($expected, $result);
  721. $this->article['defaults'] = ['id' => 1];
  722. $this->Form->request = $this->Form->request
  723. ->withRequestTarget('/articles/edit/1')
  724. ->withParam('action', 'delete');
  725. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  726. $expected = [
  727. 'form' => [
  728. 'method' => 'post',
  729. 'action' => '/articles/edit/1',
  730. 'accept-charset' => $encoding
  731. ],
  732. 'div' => ['style' => 'display:none;'],
  733. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  734. '/div'
  735. ];
  736. $this->assertHtml($expected, $result);
  737. $this->Form->request = $this->Form->request
  738. ->withParam('action', 'add');
  739. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  740. $expected = [
  741. 'form' => [
  742. 'method' => 'post',
  743. 'action' => '/articles/publish/1',
  744. 'accept-charset' => $encoding
  745. ],
  746. 'div' => ['style' => 'display:none;'],
  747. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  748. '/div'
  749. ];
  750. $this->assertHtml($expected, $result);
  751. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  752. $expected = [
  753. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  754. 'div' => ['style' => 'display:none;'],
  755. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  756. '/div'
  757. ];
  758. $this->assertHtml($expected, $result);
  759. $this->Form->request = $this->Form->request
  760. ->withParam('controller', 'Pages');
  761. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  762. $expected = [
  763. 'form' => [
  764. 'method' => 'post', 'action' => '/Pages/signup/1',
  765. 'accept-charset' => $encoding
  766. ],
  767. 'div' => ['style' => 'display:none;'],
  768. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  769. '/div'
  770. ];
  771. $this->assertHtml($expected, $result);
  772. }
  773. /**
  774. * Test create() with no URL (no "action" attribute for <form> tag)
  775. *
  776. * @return void
  777. */
  778. public function testCreateNoUrl()
  779. {
  780. $result = $this->Form->create(false, ['url' => false]);
  781. $expected = [
  782. 'form' => [
  783. 'method' => 'post',
  784. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  785. ],
  786. 'div' => ['style' => 'display:none;'],
  787. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  788. '/div'
  789. ];
  790. $this->assertHtml($expected, $result);
  791. }
  792. /**
  793. * test create() with a custom route
  794. *
  795. * @return void
  796. */
  797. public function testCreateCustomRoute()
  798. {
  799. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  800. $encoding = strtolower(Configure::read('App.encoding'));
  801. $this->Form->request = $this->Form->request
  802. ->withParam('controller', 'users');
  803. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  804. $expected = [
  805. 'form' => [
  806. 'method' => 'post', 'action' => '/login',
  807. 'accept-charset' => $encoding
  808. ],
  809. 'div' => ['style' => 'display:none;'],
  810. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  811. '/div'
  812. ];
  813. $this->assertHtml($expected, $result);
  814. Router::connect(
  815. '/new-article',
  816. ['controller' => 'articles', 'action' => 'myaction'],
  817. ['_name' => 'my-route']
  818. );
  819. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  820. $expected = [
  821. 'form' => [
  822. 'method' => 'post', 'action' => '/new-article',
  823. 'accept-charset' => $encoding,
  824. ],
  825. 'div' => ['style' => 'display:none;'],
  826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  827. '/div'
  828. ];
  829. $this->assertHtml($expected, $result);
  830. }
  831. /**
  832. * test automatic accept-charset overriding
  833. *
  834. * @return void
  835. */
  836. public function testCreateWithAcceptCharset()
  837. {
  838. $result = $this->Form->create(
  839. $this->article,
  840. [
  841. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  842. ]
  843. );
  844. $expected = [
  845. 'form' => [
  846. 'method' => 'post', 'action' => '/articles',
  847. 'accept-charset' => 'iso-8859-1'
  848. ],
  849. 'div' => ['style' => 'display:none;'],
  850. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  851. '/div'
  852. ];
  853. $this->assertHtml($expected, $result);
  854. }
  855. /**
  856. * Test base form URL when url param is passed with multiple parameters (&)
  857. *
  858. */
  859. public function testCreateQueryStringRequest()
  860. {
  861. $encoding = strtolower(Configure::read('App.encoding'));
  862. $result = $this->Form->create($this->article, [
  863. 'type' => 'post',
  864. 'escape' => false,
  865. 'url' => [
  866. 'controller' => 'controller',
  867. 'action' => 'action',
  868. '?' => ['param1' => 'value1', 'param2' => 'value2']
  869. ]
  870. ]);
  871. $expected = [
  872. 'form' => [
  873. 'method' => 'post',
  874. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  875. 'accept-charset' => $encoding
  876. ],
  877. 'div' => ['style' => 'display:none;'],
  878. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  879. '/div'
  880. ];
  881. $this->assertHtml($expected, $result);
  882. $result = $this->Form->create($this->article, [
  883. 'type' => 'post',
  884. 'url' => [
  885. 'controller' => 'controller',
  886. 'action' => 'action',
  887. '?' => ['param1' => 'value1', 'param2' => 'value2']
  888. ]
  889. ]);
  890. $this->assertHtml($expected, $result);
  891. }
  892. /**
  893. * test that create() doesn't cause errors by multiple id's being in the primary key
  894. * as could happen with multiple select or checkboxes.
  895. *
  896. * @return void
  897. */
  898. public function testCreateWithMultipleIdInData()
  899. {
  900. $encoding = strtolower(Configure::read('App.encoding'));
  901. $this->Form->request = $this->Form->request->withData('Article.id', [1, 2]);
  902. $result = $this->Form->create($this->article);
  903. $expected = [
  904. 'form' => [
  905. 'method' => 'post',
  906. 'action' => '/articles/add',
  907. 'accept-charset' => $encoding
  908. ],
  909. 'div' => ['style' => 'display:none;'],
  910. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  911. '/div'
  912. ];
  913. $this->assertHtml($expected, $result);
  914. }
  915. /**
  916. * test that create() doesn't add in extra passed params.
  917. *
  918. * @return void
  919. */
  920. public function testCreatePassedArgs()
  921. {
  922. $encoding = strtolower(Configure::read('App.encoding'));
  923. $this->Form->request = $this->Form->request->withData('Article.id', 1);
  924. $result = $this->Form->create($this->article, [
  925. 'type' => 'post',
  926. 'escape' => false,
  927. 'url' => [
  928. 'action' => 'edit',
  929. 'myparam'
  930. ]
  931. ]);
  932. $expected = [
  933. 'form' => [
  934. 'method' => 'post',
  935. 'action' => '/articles/edit/myparam',
  936. 'accept-charset' => $encoding
  937. ],
  938. 'div' => ['style' => 'display:none;'],
  939. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  940. '/div'
  941. ];
  942. $this->assertHtml($expected, $result);
  943. }
  944. /**
  945. * test creating a get form, and get form inputs.
  946. *
  947. * @return void
  948. */
  949. public function testGetFormCreate()
  950. {
  951. $encoding = strtolower(Configure::read('App.encoding'));
  952. $result = $this->Form->create($this->article, ['type' => 'get']);
  953. $expected = ['form' => [
  954. 'method' => 'get', 'action' => '/articles/add',
  955. 'accept-charset' => $encoding
  956. ]];
  957. $this->assertHtml($expected, $result);
  958. $result = $this->Form->text('title');
  959. $expected = ['input' => [
  960. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  961. ]];
  962. $this->assertHtml($expected, $result);
  963. $result = $this->Form->password('password');
  964. $expected = ['input' => [
  965. 'name' => 'password', 'type' => 'password'
  966. ]];
  967. $this->assertHtml($expected, $result);
  968. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  969. $result = $this->Form->text('user_form');
  970. $expected = ['input' => [
  971. 'name' => 'user_form', 'type' => 'text'
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. }
  975. /**
  976. * test get form, and inputs when the model param is false
  977. *
  978. * @return void
  979. */
  980. public function testGetFormWithFalseModel()
  981. {
  982. $encoding = strtolower(Configure::read('App.encoding'));
  983. $this->Form->request = $this->Form->request->withParam('controller', 'contact_test');
  984. $result = $this->Form->create(false, [
  985. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  986. ]);
  987. $expected = ['form' => [
  988. 'method' => 'get', 'action' => '/contact_test/add',
  989. 'accept-charset' => $encoding
  990. ]];
  991. $this->assertHtml($expected, $result);
  992. $result = $this->Form->text('reason');
  993. $expected = [
  994. 'input' => ['type' => 'text', 'name' => 'reason']
  995. ];
  996. $this->assertHtml($expected, $result);
  997. }
  998. /**
  999. * testFormCreateWithSecurity method
  1000. *
  1001. * Test form->create() with security key.
  1002. *
  1003. * @return void
  1004. */
  1005. public function testCreateWithSecurity()
  1006. {
  1007. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1008. $encoding = strtolower(Configure::read('App.encoding'));
  1009. $result = $this->Form->create($this->article, [
  1010. 'url' => '/articles/publish',
  1011. ]);
  1012. $expected = [
  1013. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1014. 'div' => ['style' => 'display:none;'],
  1015. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1016. ['input' => [
  1017. 'type' => 'hidden',
  1018. 'name' => '_csrfToken',
  1019. 'value' => 'testKey',
  1020. 'autocomplete' => 'off',
  1021. ]],
  1022. '/div'
  1023. ];
  1024. $this->assertHtml($expected, $result);
  1025. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1026. $expected['form']['id'] = 'MyForm';
  1027. $this->assertHtml($expected, $result);
  1028. }
  1029. /**
  1030. * testFormCreateGetNoSecurity method
  1031. *
  1032. * Test form->create() with no security key as its a get form
  1033. *
  1034. * @return void
  1035. */
  1036. public function testCreateEndGetNoSecurity()
  1037. {
  1038. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1039. $article = new Article();
  1040. $result = $this->Form->create($article, [
  1041. 'type' => 'get',
  1042. 'url' => '/contacts/add'
  1043. ]);
  1044. $this->assertNotContains('testKey', $result);
  1045. $result = $this->Form->end();
  1046. $this->assertNotContains('testKey', $result);
  1047. }
  1048. /**
  1049. * test that create() clears the fields property so it starts fresh
  1050. *
  1051. * @return void
  1052. */
  1053. public function testCreateClearingFields()
  1054. {
  1055. $this->Form->fields = ['model_id'];
  1056. $this->Form->create($this->article);
  1057. $this->assertEquals([], $this->Form->fields);
  1058. }
  1059. /**
  1060. * Tests form hash generation with model-less data
  1061. *
  1062. * @return void
  1063. */
  1064. public function testValidateHashNoModel()
  1065. {
  1066. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1067. $fields = ['anything'];
  1068. $result = $this->Form->secure($fields);
  1069. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1070. $this->assertContains($hash, $result);
  1071. }
  1072. /**
  1073. * Tests that hidden fields generated for checkboxes don't get locked
  1074. *
  1075. * @return void
  1076. */
  1077. public function testNoCheckboxLocking()
  1078. {
  1079. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1080. $this->assertSame([], $this->Form->fields);
  1081. $this->Form->checkbox('check', ['value' => '1']);
  1082. $this->assertSame($this->Form->fields, ['check']);
  1083. }
  1084. /**
  1085. * testFormSecurityFields method
  1086. *
  1087. * Test generation of secure form hash generation.
  1088. *
  1089. * @return void
  1090. */
  1091. public function testFormSecurityFields()
  1092. {
  1093. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1094. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1095. $result = $this->Form->secure($fields);
  1096. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1097. $hash .= ':' . 'Model.valid';
  1098. $hash = urlencode($hash);
  1099. $tokenDebug = urlencode(json_encode([
  1100. '',
  1101. $fields,
  1102. []
  1103. ]));
  1104. $expected = [
  1105. 'div' => ['style' => 'display:none;'],
  1106. ['input' => [
  1107. 'type' => 'hidden',
  1108. 'name' => '_Token[fields]',
  1109. 'value' => $hash,
  1110. 'autocomplete' => 'off',
  1111. ]],
  1112. ['input' => [
  1113. 'type' => 'hidden',
  1114. 'name' => '_Token[unlocked]',
  1115. 'value' => '',
  1116. 'autocomplete' => 'off',
  1117. ]],
  1118. ['input' => [
  1119. 'type' => 'hidden',
  1120. 'name' => '_Token[debug]',
  1121. 'value' => $tokenDebug,
  1122. 'autocomplete' => 'off',
  1123. ]],
  1124. '/div'
  1125. ];
  1126. $this->assertHtml($expected, $result);
  1127. }
  1128. /**
  1129. * testFormSecurityFields method
  1130. *
  1131. * Test debug token is not generated if debug is false
  1132. *
  1133. * @return void
  1134. */
  1135. public function testFormSecurityFieldsNoDebugMode()
  1136. {
  1137. Configure::write('debug', false);
  1138. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1139. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1140. $result = $this->Form->secure($fields);
  1141. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1142. $hash .= ':' . 'Model.valid';
  1143. $hash = urlencode($hash);
  1144. $expected = [
  1145. 'div' => ['style' => 'display:none;'],
  1146. ['input' => [
  1147. 'type' => 'hidden',
  1148. 'name' => '_Token[fields]',
  1149. 'autocomplete' => 'off',
  1150. 'value' => $hash
  1151. ]],
  1152. ['input' => [
  1153. 'type' => 'hidden',
  1154. 'name' => '_Token[unlocked]',
  1155. 'autocomplete' => 'off',
  1156. 'value' => '',
  1157. ]],
  1158. '/div'
  1159. ];
  1160. $this->assertHtml($expected, $result);
  1161. }
  1162. /**
  1163. * Tests correct generation of number fields for smallint
  1164. *
  1165. * @return void
  1166. */
  1167. public function testTextFieldGenerationForSmallint()
  1168. {
  1169. $this->article['schema'] = [
  1170. 'foo' => [
  1171. 'type' => 'smallinteger',
  1172. 'null' => false,
  1173. 'default' => null,
  1174. 'length' => 10
  1175. ]
  1176. ];
  1177. $this->Form->create($this->article);
  1178. $result = $this->Form->control('foo');
  1179. $this->assertContains('class="input number"', $result);
  1180. $this->assertContains('type="number"', $result);
  1181. }
  1182. /**
  1183. * Tests correct generation of number fields for tinyint
  1184. *
  1185. * @return void
  1186. */
  1187. public function testTextFieldGenerationForTinyint()
  1188. {
  1189. $this->article['schema'] = [
  1190. 'foo' => [
  1191. 'type' => 'tinyinteger',
  1192. 'null' => false,
  1193. 'default' => null,
  1194. 'length' => 10
  1195. ]
  1196. ];
  1197. $this->Form->create($this->article);
  1198. $result = $this->Form->control('foo');
  1199. $this->assertContains('class="input number"', $result);
  1200. $this->assertContains('type="number"', $result);
  1201. }
  1202. /**
  1203. * Tests correct generation of number fields for double and float fields
  1204. *
  1205. * @return void
  1206. */
  1207. public function testTextFieldGenerationForFloats()
  1208. {
  1209. $this->article['schema'] = [
  1210. 'foo' => [
  1211. 'type' => 'float',
  1212. 'null' => false,
  1213. 'default' => null,
  1214. 'length' => 10
  1215. ]
  1216. ];
  1217. $this->Form->create($this->article);
  1218. $result = $this->Form->control('foo');
  1219. $expected = [
  1220. 'div' => ['class' => 'input number'],
  1221. 'label' => ['for' => 'foo'],
  1222. 'Foo',
  1223. '/label',
  1224. ['input' => [
  1225. 'type' => 'number',
  1226. 'name' => 'foo',
  1227. 'id' => 'foo',
  1228. 'step' => 'any'
  1229. ]],
  1230. '/div'
  1231. ];
  1232. $this->assertHtml($expected, $result);
  1233. $result = $this->Form->control('foo', ['step' => 0.5]);
  1234. $expected = [
  1235. 'div' => ['class' => 'input number'],
  1236. 'label' => ['for' => 'foo'],
  1237. 'Foo',
  1238. '/label',
  1239. ['input' => [
  1240. 'type' => 'number',
  1241. 'name' => 'foo',
  1242. 'id' => 'foo',
  1243. 'step' => '0.5'
  1244. ]],
  1245. '/div'
  1246. ];
  1247. $this->assertHtml($expected, $result);
  1248. }
  1249. /**
  1250. * Tests correct generation of number fields for integer fields
  1251. *
  1252. * @return void
  1253. */
  1254. public function testTextFieldTypeNumberGenerationForIntegers()
  1255. {
  1256. TableRegistry::get('Contacts', [
  1257. 'className' => __NAMESPACE__ . '\ContactsTable'
  1258. ]);
  1259. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1260. $result = $this->Form->control('age');
  1261. $expected = [
  1262. 'div' => ['class' => 'input number'],
  1263. 'label' => ['for' => 'age'],
  1264. 'Age',
  1265. '/label',
  1266. ['input' => [
  1267. 'type' => 'number', 'name' => 'age',
  1268. 'id' => 'age'
  1269. ]],
  1270. '/div'
  1271. ];
  1272. $this->assertHtml($expected, $result);
  1273. }
  1274. /**
  1275. * Tests correct generation of file upload fields for binary fields
  1276. *
  1277. * @return void
  1278. */
  1279. public function testFileUploadFieldTypeGenerationForBinaries()
  1280. {
  1281. $table = TableRegistry::get('Contacts', [
  1282. 'className' => __NAMESPACE__ . '\ContactsTable'
  1283. ]);
  1284. $table->setSchema(['foo' => [
  1285. 'type' => 'binary',
  1286. 'null' => false,
  1287. 'default' => null,
  1288. 'length' => 1024
  1289. ]]);
  1290. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1291. $result = $this->Form->control('foo');
  1292. $expected = [
  1293. 'div' => ['class' => 'input file'],
  1294. 'label' => ['for' => 'foo'],
  1295. 'Foo',
  1296. '/label',
  1297. ['input' => [
  1298. 'type' => 'file', 'name' => 'foo',
  1299. 'id' => 'foo'
  1300. ]],
  1301. '/div'
  1302. ];
  1303. $this->assertHtml($expected, $result);
  1304. }
  1305. /**
  1306. * testFormSecurityMultipleFields method
  1307. *
  1308. * Test secure() with multiple row form. Ensure hash is correct.
  1309. *
  1310. * @return void
  1311. */
  1312. public function testFormSecurityMultipleFields()
  1313. {
  1314. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1315. $fields = [
  1316. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1317. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1318. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1319. ];
  1320. $result = $this->Form->secure($fields);
  1321. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1322. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1323. $tokenDebug = urlencode(json_encode([
  1324. '',
  1325. $fields,
  1326. []
  1327. ]));
  1328. $expected = [
  1329. 'div' => ['style' => 'display:none;'],
  1330. ['input' => [
  1331. 'type' => 'hidden',
  1332. 'name' => '_Token[fields]',
  1333. 'value' => $hash,
  1334. 'autocomplete' => 'off',
  1335. ]],
  1336. ['input' => [
  1337. 'type' => 'hidden',
  1338. 'name' => '_Token[unlocked]',
  1339. 'autocomplete' => 'off',
  1340. 'value' => ''
  1341. ]],
  1342. ['input' => [
  1343. 'type' => 'hidden',
  1344. 'name' => '_Token[debug]',
  1345. 'value' => $tokenDebug,
  1346. 'autocomplete' => 'off',
  1347. ]],
  1348. '/div'
  1349. ];
  1350. $this->assertHtml($expected, $result);
  1351. }
  1352. /**
  1353. * testFormSecurityMultipleSubmitButtons
  1354. *
  1355. * test form submit generation and ensure that _Token is only created on end()
  1356. *
  1357. * @return void
  1358. */
  1359. public function testFormSecurityMultipleSubmitButtons()
  1360. {
  1361. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1362. $this->Form->create($this->article);
  1363. $this->Form->text('Address.title');
  1364. $this->Form->text('Address.first_name');
  1365. $result = $this->Form->submit('Save', ['name' => 'save']);
  1366. $expected = [
  1367. 'div' => ['class' => 'submit'],
  1368. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1369. '/div',
  1370. ];
  1371. $this->assertHtml($expected, $result);
  1372. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1373. $expected = [
  1374. 'div' => ['class' => 'submit'],
  1375. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1376. '/div',
  1377. ];
  1378. $this->assertHtml($expected, $result);
  1379. $result = $this->Form->end();
  1380. $tokenDebug = urlencode(json_encode([
  1381. '/articles/add',
  1382. [
  1383. 'Address.title',
  1384. 'Address.first_name',
  1385. ],
  1386. ['save', 'cancel']
  1387. ]));
  1388. $expected = [
  1389. 'div' => ['style' => 'display:none;'],
  1390. ['input' => [
  1391. 'type' => 'hidden',
  1392. 'name' => '_Token[fields]',
  1393. 'autocomplete',
  1394. 'value'
  1395. ]],
  1396. ['input' => [
  1397. 'type' => 'hidden',
  1398. 'name' => '_Token[unlocked]',
  1399. 'value' => 'cancel%7Csave',
  1400. 'autocomplete' => 'off',
  1401. ]],
  1402. ['input' => [
  1403. 'type' => 'hidden',
  1404. 'name' => '_Token[debug]',
  1405. 'value' => $tokenDebug,
  1406. 'autocomplete' => 'off',
  1407. ]],
  1408. '/div'
  1409. ];
  1410. $this->assertHtml($expected, $result);
  1411. }
  1412. /**
  1413. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1414. *
  1415. * @return void
  1416. */
  1417. public function testSecurityButtonNestedNamed()
  1418. {
  1419. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1420. $this->Form->create('Addresses');
  1421. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1422. $result = $this->Form->unlockField();
  1423. $this->assertEquals(['Address.button'], $result);
  1424. }
  1425. /**
  1426. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testSecuritySubmitNestedNamed()
  1431. {
  1432. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1433. $this->Form->create($this->article);
  1434. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1435. $result = $this->Form->unlockField();
  1436. $this->assertEquals(['Address.button'], $result);
  1437. }
  1438. /**
  1439. * Test that the correct fields are unlocked for image submits with no names.
  1440. *
  1441. * @return void
  1442. */
  1443. public function testSecuritySubmitImageNoName()
  1444. {
  1445. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1446. $this->Form->create(false);
  1447. $result = $this->Form->submit('save.png');
  1448. $expected = [
  1449. 'div' => ['class' => 'submit'],
  1450. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1451. '/div'
  1452. ];
  1453. $this->assertHtml($expected, $result);
  1454. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1455. }
  1456. /**
  1457. * Test that the correct fields are unlocked for image submits with names.
  1458. *
  1459. * @return void
  1460. */
  1461. public function testSecuritySubmitImageName()
  1462. {
  1463. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1464. $this->Form->create(null);
  1465. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1466. $expected = [
  1467. 'div' => ['class' => 'submit'],
  1468. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1469. '/div'
  1470. ];
  1471. $this->assertHtml($expected, $result);
  1472. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1473. }
  1474. /**
  1475. * testFormSecurityMultipleControlFields method
  1476. *
  1477. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1478. *
  1479. * @return void
  1480. */
  1481. public function testFormSecurityMultipleControlFields()
  1482. {
  1483. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1484. $this->Form->create();
  1485. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1486. $this->Form->control('Addresses.0.title');
  1487. $this->Form->control('Addresses.0.first_name');
  1488. $this->Form->control('Addresses.0.last_name');
  1489. $this->Form->control('Addresses.0.address');
  1490. $this->Form->control('Addresses.0.city');
  1491. $this->Form->control('Addresses.0.phone');
  1492. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1493. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1494. $this->Form->control('Addresses.1.title');
  1495. $this->Form->control('Addresses.1.first_name');
  1496. $this->Form->control('Addresses.1.last_name');
  1497. $this->Form->control('Addresses.1.address');
  1498. $this->Form->control('Addresses.1.city');
  1499. $this->Form->control('Addresses.1.phone');
  1500. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1501. $result = $this->Form->secure($this->Form->fields);
  1502. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1503. $tokenDebug = urlencode(json_encode([
  1504. '/articles/add',
  1505. [
  1506. 'Addresses.0.id' => '123456',
  1507. 'Addresses.0.title',
  1508. 'Addresses.0.first_name',
  1509. 'Addresses.0.last_name',
  1510. 'Addresses.0.address',
  1511. 'Addresses.0.city',
  1512. 'Addresses.0.phone',
  1513. 'Addresses.0.primary',
  1514. 'Addresses.1.id' => '654321',
  1515. 'Addresses.1.title',
  1516. 'Addresses.1.first_name',
  1517. 'Addresses.1.last_name',
  1518. 'Addresses.1.address',
  1519. 'Addresses.1.city',
  1520. 'Addresses.1.phone',
  1521. 'Addresses.1.primary',
  1522. ],
  1523. []
  1524. ]));
  1525. $expected = [
  1526. 'div' => ['style' => 'display:none;'],
  1527. ['input' => [
  1528. 'type' => 'hidden',
  1529. 'name' => '_Token[fields]',
  1530. 'value' => $hash,
  1531. 'autocomplete' => 'off',
  1532. ]],
  1533. ['input' => [
  1534. 'type' => 'hidden',
  1535. 'name' => '_Token[unlocked]',
  1536. 'autocomplete' => 'off',
  1537. 'value' => ''
  1538. ]],
  1539. ['input' => [
  1540. 'type' => 'hidden',
  1541. 'name' => '_Token[debug]',
  1542. 'value' => $tokenDebug,
  1543. 'autocomplete' => 'off',
  1544. ]],
  1545. '/div'
  1546. ];
  1547. $this->assertHtml($expected, $result);
  1548. }
  1549. /**
  1550. * testFormSecurityArrayFields method
  1551. *
  1552. * Test form security with Model.field.0 style inputs.
  1553. *
  1554. * @return void
  1555. */
  1556. public function testFormSecurityArrayFields()
  1557. {
  1558. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1559. $this->Form->create();
  1560. $this->Form->text('Address.primary.1');
  1561. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1562. $this->Form->text('Address.secondary.1.0');
  1563. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1564. }
  1565. /**
  1566. * testFormSecurityMultipleControlDisabledFields method
  1567. *
  1568. * Test secure form generation with multiple records and disabled fields.
  1569. *
  1570. * @return void
  1571. */
  1572. public function testFormSecurityMultipleControlDisabledFields()
  1573. {
  1574. $this->Form->request = $this->Form->request->withParam('_Token', [
  1575. 'unlockedFields' => ['first_name', 'address']
  1576. ]);
  1577. $this->Form->create();
  1578. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1579. $this->Form->text('Addresses.0.title');
  1580. $this->Form->text('Addresses.0.first_name');
  1581. $this->Form->text('Addresses.0.last_name');
  1582. $this->Form->text('Addresses.0.address');
  1583. $this->Form->text('Addresses.0.city');
  1584. $this->Form->text('Addresses.0.phone');
  1585. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1586. $this->Form->text('Addresses.1.title');
  1587. $this->Form->text('Addresses.1.first_name');
  1588. $this->Form->text('Addresses.1.last_name');
  1589. $this->Form->text('Addresses.1.address');
  1590. $this->Form->text('Addresses.1.city');
  1591. $this->Form->text('Addresses.1.phone');
  1592. $result = $this->Form->secure($this->Form->fields);
  1593. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1594. $tokenDebug = urlencode(json_encode([
  1595. '/articles/add',
  1596. [
  1597. 'Addresses.0.id' => '123456',
  1598. 'Addresses.0.title',
  1599. 'Addresses.0.last_name',
  1600. 'Addresses.0.city',
  1601. 'Addresses.0.phone',
  1602. 'Addresses.1.id' => '654321',
  1603. 'Addresses.1.title',
  1604. 'Addresses.1.last_name',
  1605. 'Addresses.1.city',
  1606. 'Addresses.1.phone'
  1607. ],
  1608. [
  1609. 'first_name',
  1610. 'address'
  1611. ]
  1612. ]));
  1613. $expected = [
  1614. 'div' => ['style' => 'display:none;'],
  1615. ['input' => [
  1616. 'type' => 'hidden',
  1617. 'name' => '_Token[fields]',
  1618. 'autocomplete' => 'off',
  1619. 'value' => $hash
  1620. ]],
  1621. ['input' => [
  1622. 'type' => 'hidden',
  1623. 'name' => '_Token[unlocked]',
  1624. 'autocomplete' => 'off',
  1625. 'value' => 'address%7Cfirst_name',
  1626. ]],
  1627. ['input' => [
  1628. 'type' => 'hidden',
  1629. 'name' => '_Token[debug]',
  1630. 'autocomplete' => 'off',
  1631. 'value' => $tokenDebug,
  1632. ]],
  1633. '/div'
  1634. ];
  1635. $this->assertHtml($expected, $result);
  1636. }
  1637. /**
  1638. * testFormSecurityControlDisabledFields method
  1639. *
  1640. * Test single record form with disabled fields.
  1641. *
  1642. * @return void
  1643. */
  1644. public function testFormSecurityControlUnlockedFields()
  1645. {
  1646. $this->Form->request = $this->Form->request->withParam('_Token', [
  1647. 'unlockedFields' => ['first_name', 'address']
  1648. ]);
  1649. $this->Form->create();
  1650. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1651. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1652. $this->Form->text('Addresses.title');
  1653. $this->Form->text('Addresses.first_name');
  1654. $this->Form->text('Addresses.last_name');
  1655. $this->Form->text('Addresses.address');
  1656. $this->Form->text('Addresses.city');
  1657. $this->Form->text('Addresses.phone');
  1658. $result = $this->Form->fields;
  1659. $expected = [
  1660. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1661. 'Addresses.city', 'Addresses.phone'
  1662. ];
  1663. $this->assertEquals($expected, $result);
  1664. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1665. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1666. $tokenDebug = urlencode(json_encode([
  1667. '/articles/add',
  1668. [
  1669. 'Addresses.id' => '123456',
  1670. 'Addresses.title',
  1671. 'Addresses.last_name',
  1672. 'Addresses.city',
  1673. 'Addresses.phone'
  1674. ],
  1675. [
  1676. 'first_name',
  1677. 'address'
  1678. ]
  1679. ]));
  1680. $expected = [
  1681. 'div' => ['style' => 'display:none;'],
  1682. ['input' => [
  1683. 'type' => 'hidden',
  1684. 'name' => '_Token[fields]',
  1685. 'value' => $hash,
  1686. 'autocomplete' => 'off',
  1687. 'data-foo' => 'bar',
  1688. ]],
  1689. ['input' => [
  1690. 'type' => 'hidden',
  1691. 'name' => '_Token[unlocked]',
  1692. 'value' => 'address%7Cfirst_name',
  1693. 'autocomplete' => 'off',
  1694. 'data-foo' => 'bar',
  1695. ]],
  1696. ['input' => [
  1697. 'type' => 'hidden', 'name' => '_Token[debug]',
  1698. 'value' => $tokenDebug,
  1699. 'autocomplete' => 'off',
  1700. 'data-foo' => 'bar'
  1701. ]],
  1702. '/div'
  1703. ];
  1704. $this->assertHtml($expected, $result);
  1705. }
  1706. /**
  1707. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1708. *
  1709. * Test single record form with debugSecurity param.
  1710. *
  1711. * @return void
  1712. */
  1713. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1714. {
  1715. $this->Form->request = $this->Form->request->withParam('_Token', [
  1716. 'unlockedFields' => ['first_name', 'address']
  1717. ]);
  1718. $this->Form->create();
  1719. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1720. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1721. $this->Form->text('Addresses.title');
  1722. $this->Form->text('Addresses.first_name');
  1723. $this->Form->text('Addresses.last_name');
  1724. $this->Form->text('Addresses.address');
  1725. $this->Form->text('Addresses.city');
  1726. $this->Form->text('Addresses.phone');
  1727. $result = $this->Form->fields;
  1728. $expected = [
  1729. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1730. 'Addresses.city', 'Addresses.phone'
  1731. ];
  1732. $this->assertEquals($expected, $result);
  1733. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1734. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1735. $tokenDebug = urlencode(json_encode([
  1736. '/articles/add',
  1737. [
  1738. 'Addresses.id' => '123456',
  1739. 'Addresses.title',
  1740. 'Addresses.last_name',
  1741. 'Addresses.city',
  1742. 'Addresses.phone'
  1743. ],
  1744. [
  1745. 'first_name',
  1746. 'address'
  1747. ]
  1748. ]));
  1749. $expected = [
  1750. 'div' => ['style' => 'display:none;'],
  1751. ['input' => [
  1752. 'type' => 'hidden',
  1753. 'name' => '_Token[fields]',
  1754. 'value' => $hash,
  1755. 'autocomplete' => 'off',
  1756. 'data-foo' => 'bar',
  1757. ]],
  1758. ['input' => [
  1759. 'type' => 'hidden',
  1760. 'name' => '_Token[unlocked]',
  1761. 'value' => 'address%7Cfirst_name',
  1762. 'autocomplete' => 'off',
  1763. 'data-foo' => 'bar',
  1764. ]],
  1765. ['input' => [
  1766. 'type' => 'hidden', 'name' => '_Token[debug]',
  1767. 'value' => $tokenDebug,
  1768. 'autocomplete' => 'off',
  1769. 'data-foo' => 'bar'
  1770. ]],
  1771. '/div'
  1772. ];
  1773. $this->assertHtml($expected, $result);
  1774. }
  1775. /**
  1776. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1777. *
  1778. * Debug is false, debugSecurity is true -> no debug
  1779. *
  1780. * @return void
  1781. */
  1782. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1783. {
  1784. $this->Form->request = $this->Form->request->withParam('_Token', [
  1785. 'unlockedFields' => ['first_name', 'address']
  1786. ]);
  1787. $this->Form->create();
  1788. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1789. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1790. $this->Form->text('Addresses.title');
  1791. $this->Form->text('Addresses.first_name');
  1792. $this->Form->text('Addresses.last_name');
  1793. $this->Form->text('Addresses.address');
  1794. $this->Form->text('Addresses.city');
  1795. $this->Form->text('Addresses.phone');
  1796. $result = $this->Form->fields;
  1797. $expected = [
  1798. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1799. 'Addresses.city', 'Addresses.phone'
  1800. ];
  1801. $this->assertEquals($expected, $result);
  1802. Configure::write('debug', false);
  1803. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1804. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1805. $expected = [
  1806. 'div' => ['style' => 'display:none;'],
  1807. ['input' => [
  1808. 'type' => 'hidden',
  1809. 'name' => '_Token[fields]',
  1810. 'value' => $hash,
  1811. 'autocomplete' => 'off',
  1812. 'data-foo' => 'bar',
  1813. ]],
  1814. ['input' => [
  1815. 'type' => 'hidden',
  1816. 'name' => '_Token[unlocked]',
  1817. 'value' => 'address%7Cfirst_name',
  1818. 'autocomplete' => 'off',
  1819. 'data-foo' => 'bar',
  1820. ]],
  1821. '/div'
  1822. ];
  1823. $this->assertHtml($expected, $result);
  1824. }
  1825. /**
  1826. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1827. *
  1828. * Test single record form with debugSecurity param.
  1829. *
  1830. * @return void
  1831. */
  1832. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1833. {
  1834. $this->Form->request = $this->Form->request->withParam('_Token', [
  1835. 'unlockedFields' => ['first_name', 'address']
  1836. ]);
  1837. $this->Form->create();
  1838. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1839. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1840. $this->Form->text('Addresses.title');
  1841. $this->Form->text('Addresses.first_name');
  1842. $this->Form->text('Addresses.last_name');
  1843. $this->Form->text('Addresses.address');
  1844. $this->Form->text('Addresses.city');
  1845. $this->Form->text('Addresses.phone');
  1846. $result = $this->Form->fields;
  1847. $expected = [
  1848. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1849. 'Addresses.city', 'Addresses.phone'
  1850. ];
  1851. $this->assertEquals($expected, $result);
  1852. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1853. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1854. $expected = [
  1855. 'div' => ['style' => 'display:none;'],
  1856. ['input' => [
  1857. 'type' => 'hidden',
  1858. 'name' => '_Token[fields]',
  1859. 'value' => $hash,
  1860. 'autocomplete' => 'off',
  1861. 'data-foo' => 'bar',
  1862. ]],
  1863. ['input' => [
  1864. 'type' => 'hidden',
  1865. 'name' => '_Token[unlocked]',
  1866. 'value' => 'address%7Cfirst_name',
  1867. 'autocomplete' => 'off',
  1868. 'data-foo' => 'bar',
  1869. ]],
  1870. '/div'
  1871. ];
  1872. $this->assertHtml($expected, $result);
  1873. }
  1874. /**
  1875. * testFormSecureWithCustomNameAttribute method
  1876. *
  1877. * Test securing inputs with custom name attributes.
  1878. *
  1879. * @return void
  1880. */
  1881. public function testFormSecureWithCustomNameAttribute()
  1882. {
  1883. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1884. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1885. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1886. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1887. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1888. }
  1889. /**
  1890. * testFormSecuredControl method
  1891. *
  1892. * Test generation of entire secure form, assertions made on control() output.
  1893. *
  1894. * @return void
  1895. */
  1896. public function testFormSecuredControl()
  1897. {
  1898. $this->Form->request = $this->Form->request
  1899. ->withParam('_Token', 'stuff')
  1900. ->withParam('_csrfToken', 'testKey');
  1901. $this->article['schema'] = [
  1902. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1903. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1904. ];
  1905. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1906. $encoding = strtolower(Configure::read('App.encoding'));
  1907. $expected = [
  1908. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1909. 'div' => ['style' => 'display:none;'],
  1910. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1911. ['input' => [
  1912. 'type' => 'hidden',
  1913. 'name' => '_csrfToken',
  1914. 'value' => 'testKey',
  1915. 'autocomplete' => 'off'
  1916. ]],
  1917. '/div'
  1918. ];
  1919. $this->assertHtml($expected, $result);
  1920. $result = $this->Form->control('ratio');
  1921. $expected = [
  1922. 'div' => ['class'],
  1923. 'label' => ['for'],
  1924. 'Ratio',
  1925. '/label',
  1926. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1927. '/div',
  1928. ];
  1929. $this->assertHtml($expected, $result);
  1930. $result = $this->Form->control('population');
  1931. $expected = [
  1932. 'div' => ['class'],
  1933. 'label' => ['for'],
  1934. 'Population',
  1935. '/label',
  1936. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1937. '/div',
  1938. ];
  1939. $this->assertHtml($expected, $result);
  1940. $result = $this->Form->control('published', ['type' => 'text']);
  1941. $expected = [
  1942. 'div' => ['class' => 'input text'],
  1943. 'label' => ['for' => 'published'],
  1944. 'Published',
  1945. '/label',
  1946. ['input' => [
  1947. 'type' => 'text',
  1948. 'name' => 'published',
  1949. 'id' => 'published'
  1950. ]],
  1951. '/div'
  1952. ];
  1953. $this->assertHtml($expected, $result);
  1954. $result = $this->Form->control('other', ['type' => 'text']);
  1955. $expected = [
  1956. 'div' => ['class' => 'input text'],
  1957. 'label' => ['for' => 'other'],
  1958. 'Other',
  1959. '/label',
  1960. ['input' => [
  1961. 'type' => 'text',
  1962. 'name' => 'other',
  1963. 'id',
  1964. ]],
  1965. '/div'
  1966. ];
  1967. $this->assertHtml($expected, $result);
  1968. $result = $this->Form->hidden('stuff');
  1969. $expected = [
  1970. 'input' => [
  1971. 'type' => 'hidden',
  1972. 'name' => 'stuff'
  1973. ]
  1974. ];
  1975. $this->assertHtml($expected, $result);
  1976. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1977. $expected = ['input' => [
  1978. 'type' => 'hidden',
  1979. 'name' => 'hidden',
  1980. 'value' => '0'
  1981. ]];
  1982. $this->assertHtml($expected, $result);
  1983. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1984. $expected = [
  1985. 'div' => ['class' => 'input checkbox'],
  1986. ['input' => [
  1987. 'type' => 'hidden',
  1988. 'name' => 'something',
  1989. 'value' => '0'
  1990. ]],
  1991. 'label' => ['for' => 'something'],
  1992. ['input' => [
  1993. 'type' => 'checkbox',
  1994. 'name' => 'something',
  1995. 'value' => '1',
  1996. 'id' => 'something'
  1997. ]],
  1998. 'Something',
  1999. '/label',
  2000. '/div'
  2001. ];
  2002. $this->assertHtml($expected, $result);
  2003. $result = $this->Form->fields;
  2004. $expectedFields = [
  2005. 'ratio',
  2006. 'population',
  2007. 'published',
  2008. 'other',
  2009. 'stuff' => '',
  2010. 'hidden' => '0',
  2011. 'something'
  2012. ];
  2013. $this->assertEquals($expectedFields, $result);
  2014. $result = $this->Form->secure($this->Form->fields);
  2015. $tokenDebug = urlencode(json_encode([
  2016. '/articles/add',
  2017. $expectedFields,
  2018. []
  2019. ]));
  2020. $expected = [
  2021. 'div' => ['style' => 'display:none;'],
  2022. ['input' => [
  2023. 'type' => 'hidden',
  2024. 'name' => '_Token[fields]',
  2025. 'value',
  2026. 'autocomplete'
  2027. ]],
  2028. ['input' => [
  2029. 'type' => 'hidden',
  2030. 'name' => '_Token[unlocked]',
  2031. 'value' => '',
  2032. 'autocomplete' => 'off'
  2033. ]],
  2034. ['input' => [
  2035. 'type' => 'hidden', 'name' => '_Token[debug]',
  2036. 'value' => $tokenDebug,
  2037. 'autocomplete' => 'off'
  2038. ]],
  2039. '/div'
  2040. ];
  2041. $this->assertHtml($expected, $result);
  2042. }
  2043. /**
  2044. * testSecuredControlCustomName method
  2045. *
  2046. * Test secured inputs with custom names.
  2047. *
  2048. * @return void
  2049. */
  2050. public function testSecuredControlCustomName()
  2051. {
  2052. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2053. $this->assertEquals([], $this->Form->fields);
  2054. $this->Form->text('text_input', [
  2055. 'name' => 'Option[General.default_role]',
  2056. ]);
  2057. $expected = ['Option.General.default_role'];
  2058. $this->assertEquals($expected, $this->Form->fields);
  2059. $this->Form->select('select_box', [1, 2], [
  2060. 'name' => 'Option[General.select_role]',
  2061. ]);
  2062. $expected[] = 'Option.General.select_role';
  2063. $this->assertEquals($expected, $this->Form->fields);
  2064. $this->Form->text('other.things[]');
  2065. $expected[] = 'other.things';
  2066. $this->assertEquals($expected, $this->Form->fields);
  2067. }
  2068. /**
  2069. * testSecuredControlDuplicate method
  2070. *
  2071. * Test that a hidden field followed by a visible field
  2072. * undoes the hidden field locking.
  2073. *
  2074. * @return void
  2075. */
  2076. public function testSecuredControlDuplicate()
  2077. {
  2078. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2079. $this->assertEquals([], $this->Form->fields);
  2080. $this->Form->control('text_val', [
  2081. 'type' => 'hidden',
  2082. 'value' => 'some text',
  2083. ]);
  2084. $expected = ['text_val' => 'some text'];
  2085. $this->assertEquals($expected, $this->Form->fields);
  2086. $this->Form->control('text_val', [
  2087. 'type' => 'text',
  2088. ]);
  2089. $expected = ['text_val'];
  2090. $this->assertEquals($expected, $this->Form->fields);
  2091. }
  2092. /**
  2093. * testFormSecuredFileControl method
  2094. *
  2095. * Tests that the correct keys are added to the field hash index.
  2096. *
  2097. * @return void
  2098. */
  2099. public function testFormSecuredFileControl()
  2100. {
  2101. $this->assertEquals([], $this->Form->fields);
  2102. $this->Form->file('Attachment.file');
  2103. $expected = [
  2104. 'Attachment.file.name', 'Attachment.file.type',
  2105. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2106. 'Attachment.file.size'
  2107. ];
  2108. $this->assertEquals($expected, $this->Form->fields);
  2109. }
  2110. /**
  2111. * testFormSecuredMultipleSelect method
  2112. *
  2113. * Test that multiple selects keys are added to field hash.
  2114. *
  2115. * @return void
  2116. */
  2117. public function testFormSecuredMultipleSelect()
  2118. {
  2119. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  2120. $this->assertEquals([], $this->Form->fields);
  2121. $options = ['1' => 'one', '2' => 'two'];
  2122. $this->Form->select('Model.select', $options);
  2123. $expected = ['Model.select'];
  2124. $this->assertEquals($expected, $this->Form->fields);
  2125. $this->Form->fields = [];
  2126. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2127. $this->assertEquals($expected, $this->Form->fields);
  2128. }
  2129. /**
  2130. * testFormSecuredRadio method
  2131. *
  2132. * @return void
  2133. */
  2134. public function testFormSecuredRadio()
  2135. {
  2136. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2137. $this->assertEquals([], $this->Form->fields);
  2138. $options = ['1' => 'option1', '2' => 'option2'];
  2139. $this->Form->radio('Test.test', $options);
  2140. $expected = ['Test.test'];
  2141. $this->assertEquals($expected, $this->Form->fields);
  2142. $this->Form->radio('Test.all', $options, [
  2143. 'disabled' => ['option1', 'option2']
  2144. ]);
  2145. $expected = ['Test.test', 'Test.all' => ''];
  2146. $this->assertEquals($expected, $this->Form->fields);
  2147. $this->Form->radio('Test.some', $options, [
  2148. 'disabled' => ['option1']
  2149. ]);
  2150. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2151. $this->assertEquals($expected, $this->Form->fields);
  2152. }
  2153. /**
  2154. * testFormSecuredAndDisabledNotAssoc method
  2155. *
  2156. * Test that when disabled is in a list based attribute array it works.
  2157. *
  2158. * @return void
  2159. */
  2160. public function testFormSecuredAndDisabledNotAssoc()
  2161. {
  2162. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2163. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2164. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2165. $this->Form->text('Model.text', ['disabled']);
  2166. $this->Form->textarea('Model.textarea', ['disabled']);
  2167. $this->Form->password('Model.password', ['disabled']);
  2168. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2169. $expected = [
  2170. 'Model.radio' => ''
  2171. ];
  2172. $this->assertEquals($expected, $this->Form->fields);
  2173. }
  2174. /**
  2175. * testFormSecuredAndDisabled method
  2176. *
  2177. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2178. * hashing.
  2179. *
  2180. * @return void
  2181. */
  2182. public function testFormSecuredAndDisabled()
  2183. {
  2184. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2185. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2186. $this->Form->text('Model.text', ['disabled' => true]);
  2187. $this->Form->password('Model.text', ['disabled' => true]);
  2188. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2189. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2190. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2191. $this->Form->year('Model.year', ['disabled' => true]);
  2192. $this->Form->month('Model.month', ['disabled' => true]);
  2193. $this->Form->day('Model.day', ['disabled' => true]);
  2194. $this->Form->hour('Model.hour', ['disabled' => true]);
  2195. $this->Form->minute('Model.minute', ['disabled' => true]);
  2196. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2197. $expected = [
  2198. 'Model.radio' => ''
  2199. ];
  2200. $this->assertEquals($expected, $this->Form->fields);
  2201. }
  2202. /**
  2203. * testDisableSecurityUsingForm method
  2204. *
  2205. * @return void
  2206. */
  2207. public function testDisableSecurityUsingForm()
  2208. {
  2209. $this->Form->request = $this->Form->request->withParam('_Token', [
  2210. 'disabledFields' => []
  2211. ]);
  2212. $this->Form->create();
  2213. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2214. $this->Form->text('Addresses.title');
  2215. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2216. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2217. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2218. $result = $this->Form->fields;
  2219. $expected = [
  2220. 'Addresses.id' => '123456', 'Addresses.title',
  2221. ];
  2222. $this->assertEquals($expected, $result);
  2223. }
  2224. /**
  2225. * testUnlockFieldAddsToList method
  2226. *
  2227. * Test disableField.
  2228. *
  2229. * @return void
  2230. */
  2231. public function testUnlockFieldAddsToList()
  2232. {
  2233. $this->Form->request = $this->Form->request->withParam('_Token', [
  2234. 'unlockedFields' => []
  2235. ]);
  2236. $this->Form->unlockField('Contact.name');
  2237. $this->Form->text('Contact.name');
  2238. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2239. $this->assertEquals([], $this->Form->fields);
  2240. }
  2241. /**
  2242. * testUnlockFieldRemovingFromFields method
  2243. *
  2244. * Test unlockField removing from fields array.
  2245. *
  2246. * @return void
  2247. */
  2248. public function testUnlockFieldRemovingFromFields()
  2249. {
  2250. $this->Form->request = $this->Form->request->withParam('_Token', [
  2251. 'unlockedFields' => []
  2252. ]);
  2253. $this->Form->create($this->article);
  2254. $this->Form->hidden('Article.id', ['value' => 1]);
  2255. $this->Form->text('Article.title');
  2256. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2257. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2258. $this->Form->unlockField('Article.title');
  2259. $this->Form->unlockField('Article.id');
  2260. $this->assertEquals([], $this->Form->fields);
  2261. }
  2262. /**
  2263. * testResetUnlockFields method
  2264. *
  2265. * Test reset unlockFields, when create new form.
  2266. *
  2267. * @return void
  2268. */
  2269. public function testResetUnlockFields()
  2270. {
  2271. $this->Form->request = $this->Form->request->withParam('_Token', [
  2272. 'key' => 'testKey',
  2273. 'unlockedFields' => []
  2274. ]);
  2275. $this->Form->unlockField('Contact.id');
  2276. $this->Form->create('Contact');
  2277. $this->Form->hidden('Contact.id', ['value' => 1]);
  2278. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2279. $this->Form->end();
  2280. $this->Form->create('Contact');
  2281. $this->Form->hidden('Contact.id', ['value' => 1]);
  2282. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2283. }
  2284. /**
  2285. * testSecuredFormUrlIgnoresHost method
  2286. *
  2287. * Test that only the path + query elements of a form's URL show up in their hash.
  2288. *
  2289. * @return void
  2290. */
  2291. public function testSecuredFormUrlIgnoresHost()
  2292. {
  2293. $this->Form->request = $this->Form->request->withParam('_Token', ['key' => 'testKey']);
  2294. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2295. $this->Form->create($this->article, [
  2296. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2297. ]);
  2298. $result = $this->Form->secure();
  2299. $this->assertContains($expected, $result);
  2300. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2301. $result = $this->Form->secure();
  2302. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2303. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2304. $result = $this->Form->secure();
  2305. $this->assertContains($expected, $result, 'URL path + query should work.');
  2306. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2307. $result = $this->Form->secure();
  2308. $this->assertNotContains($expected, $result, 'URL is different');
  2309. }
  2310. /**
  2311. * testSecuredFormUrlHasHtmlAndIdentifier method
  2312. *
  2313. * Test that URL, HTML and identifier show up in their hashes.
  2314. *
  2315. * @return void
  2316. */
  2317. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2318. {
  2319. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2320. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2321. $this->Form->create($this->article, [
  2322. 'url' => [
  2323. 'controller' => 'articles',
  2324. 'action' => 'view',
  2325. '?' => [
  2326. 'page' => 1,
  2327. 'limit' => 10,
  2328. 'html' => '<>"',
  2329. ],
  2330. '#' => 'result',
  2331. ],
  2332. ]);
  2333. $result = $this->Form->secure();
  2334. $this->assertContains($expected, $result);
  2335. $this->Form->create($this->article, [
  2336. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2337. ]);
  2338. $result = $this->Form->secure();
  2339. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2340. $this->Form->create($this->article, [
  2341. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2342. ]);
  2343. $result = $this->Form->secure();
  2344. $this->assertContains($expected, $result, 'URL path + query should work.');
  2345. }
  2346. /**
  2347. * testErrorMessageDisplay method
  2348. *
  2349. * Test error message display.
  2350. *
  2351. * @return void
  2352. */
  2353. public function testErrorMessageDisplay()
  2354. {
  2355. $this->article['errors'] = [
  2356. 'Article' => [
  2357. 'title' => 'error message',
  2358. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2359. ]
  2360. ];
  2361. $this->Form->create($this->article);
  2362. $result = $this->Form->control('Article.title');
  2363. $expected = [
  2364. 'div' => ['class' => 'input text error'],
  2365. 'label' => ['for' => 'article-title'],
  2366. 'Title',
  2367. '/label',
  2368. 'input' => [
  2369. 'type' => 'text', 'name' => 'Article[title]',
  2370. 'id' => 'article-title', 'class' => 'form-error'
  2371. ],
  2372. ['div' => ['class' => 'error-message']],
  2373. 'error message',
  2374. '/div',
  2375. '/div'
  2376. ];
  2377. $this->assertHtml($expected, $result);
  2378. $result = $this->Form->control('Article.title', [
  2379. 'templates' => [
  2380. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2381. ]
  2382. ]);
  2383. $expected = [
  2384. 'div' => ['class' => 'input text error'],
  2385. 'label' => ['for' => 'article-title'],
  2386. 'Title',
  2387. '/label',
  2388. 'input' => [
  2389. 'type' => 'text', 'name' => 'Article[title]',
  2390. 'id' => 'article-title', 'class' => 'form-error'
  2391. ],
  2392. '/div'
  2393. ];
  2394. $this->assertHtml($expected, $result);
  2395. $result = $this->Form->control('Article.content');
  2396. $expected = [
  2397. 'div' => ['class' => 'input text error'],
  2398. 'label' => ['for' => 'article-content'],
  2399. 'Content',
  2400. '/label',
  2401. 'input' => [
  2402. 'type' => 'text', 'name' => 'Article[content]',
  2403. 'id' => 'article-content', 'class' => 'form-error'
  2404. ],
  2405. ['div' => ['class' => 'error-message']],
  2406. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2407. '/div',
  2408. '/div'
  2409. ];
  2410. $this->assertHtml($expected, $result);
  2411. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2412. $expected = [
  2413. 'div' => ['class' => 'input text error'],
  2414. 'label' => ['for' => 'article-content'],
  2415. 'Content',
  2416. '/label',
  2417. 'input' => [
  2418. 'type' => 'text', 'name' => 'Article[content]',
  2419. 'id' => 'article-content', 'class' => 'form-error'
  2420. ],
  2421. ['div' => ['class' => 'error-message']],
  2422. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2423. '/div',
  2424. '/div'
  2425. ];
  2426. $this->assertHtml($expected, $result);
  2427. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2428. $expected = [
  2429. 'div' => ['class' => 'input text error'],
  2430. 'label' => ['for' => 'article-content'],
  2431. 'Content',
  2432. '/label',
  2433. 'input' => [
  2434. 'type' => 'text', 'name' => 'Article[content]',
  2435. 'id' => 'article-content', 'class' => 'form-error'
  2436. ],
  2437. ['div' => ['class' => 'error-message']],
  2438. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2439. '/div',
  2440. '/div'
  2441. ];
  2442. $this->assertHtml($expected, $result);
  2443. }
  2444. /**
  2445. * testEmptyErrorValidation method
  2446. *
  2447. * Test validation errors, when validation message is an empty string.
  2448. *
  2449. * @return void
  2450. */
  2451. public function testEmptyErrorValidation()
  2452. {
  2453. $this->article['errors'] = [
  2454. 'Article' => ['title' => '']
  2455. ];
  2456. $this->Form->create($this->article);
  2457. $result = $this->Form->control('Article.title');
  2458. $expected = [
  2459. 'div' => ['class' => 'input text error'],
  2460. 'label' => ['for' => 'article-title'],
  2461. 'Title',
  2462. '/label',
  2463. 'input' => [
  2464. 'type' => 'text', 'name' => 'Article[title]',
  2465. 'id' => 'article-title', 'class' => 'form-error'
  2466. ],
  2467. ['div' => ['class' => 'error-message']],
  2468. [],
  2469. '/div',
  2470. '/div'
  2471. ];
  2472. $this->assertHtml($expected, $result);
  2473. }
  2474. /**
  2475. * testEmptyControlErrorValidation method
  2476. *
  2477. * Test validation errors, when calling control() overriding validation message by an empty string.
  2478. *
  2479. * @return void
  2480. */
  2481. public function testEmptyControlErrorValidation()
  2482. {
  2483. $this->article['errors'] = [
  2484. 'Article' => ['title' => 'error message']
  2485. ];
  2486. $this->Form->create($this->article);
  2487. $result = $this->Form->control('Article.title', ['error' => '']);
  2488. $expected = [
  2489. 'div' => ['class' => 'input text error'],
  2490. 'label' => ['for' => 'article-title'],
  2491. 'Title',
  2492. '/label',
  2493. 'input' => [
  2494. 'type' => 'text', 'name' => 'Article[title]',
  2495. 'id' => 'article-title', 'class' => 'form-error'
  2496. ],
  2497. ['div' => ['class' => 'error-message']],
  2498. [],
  2499. '/div',
  2500. '/div'
  2501. ];
  2502. $this->assertHtml($expected, $result);
  2503. }
  2504. /**
  2505. * testControlErrorMessage method
  2506. *
  2507. * Test validation errors, when calling control() overriding validation messages.
  2508. *
  2509. * @return void
  2510. */
  2511. public function testControlErrorMessage()
  2512. {
  2513. $this->article['errors'] = [
  2514. 'title' => ['error message']
  2515. ];
  2516. $this->Form->create($this->article);
  2517. $result = $this->Form->control('title', [
  2518. 'error' => 'Custom error!'
  2519. ]);
  2520. $expected = [
  2521. 'div' => ['class' => 'input text required error'],
  2522. 'label' => ['for' => 'title'],
  2523. 'Title',
  2524. '/label',
  2525. 'input' => [
  2526. 'type' => 'text', 'name' => 'title',
  2527. 'id' => 'title', 'class' => 'form-error',
  2528. 'required' => 'required',
  2529. ],
  2530. ['div' => ['class' => 'error-message']],
  2531. 'Custom error!',
  2532. '/div',
  2533. '/div'
  2534. ];
  2535. $this->assertHtml($expected, $result);
  2536. $result = $this->Form->control('title', [
  2537. 'error' => ['error message' => 'Custom error!']
  2538. ]);
  2539. $expected = [
  2540. 'div' => ['class' => 'input text required error'],
  2541. 'label' => ['for' => 'title'],
  2542. 'Title',
  2543. '/label',
  2544. 'input' => [
  2545. 'type' => 'text',
  2546. 'name' => 'title',
  2547. 'id' => 'title',
  2548. 'class' => 'form-error',
  2549. 'required' => 'required'
  2550. ],
  2551. ['div' => ['class' => 'error-message']],
  2552. 'Custom error!',
  2553. '/div',
  2554. '/div'
  2555. ];
  2556. $this->assertHtml($expected, $result);
  2557. }
  2558. /**
  2559. * testFormValidationAssociated method
  2560. *
  2561. * Tests displaying errors for nested entities.
  2562. *
  2563. * @return void
  2564. */
  2565. public function testFormValidationAssociated()
  2566. {
  2567. $nested = new Entity(['foo' => 'bar']);
  2568. $nested->setError('foo', ['not a valid bar']);
  2569. $entity = new Entity(['nested' => $nested]);
  2570. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2571. $result = $this->Form->error('nested.foo');
  2572. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2573. }
  2574. /**
  2575. * testFormValidationAssociatedSecondLevel method
  2576. *
  2577. * Test form error display with associated model.
  2578. *
  2579. * @return void
  2580. */
  2581. public function testFormValidationAssociatedSecondLevel()
  2582. {
  2583. $inner = new Entity(['bar' => 'baz']);
  2584. $nested = new Entity(['foo' => $inner]);
  2585. $entity = new Entity(['nested' => $nested]);
  2586. $inner->setError('bar', ['not a valid one']);
  2587. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2588. $result = $this->Form->error('nested.foo.bar');
  2589. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2590. }
  2591. /**
  2592. * testFormValidationMultiRecord method
  2593. *
  2594. * Test form error display with multiple records.
  2595. *
  2596. * @return void
  2597. */
  2598. public function testFormValidationMultiRecord()
  2599. {
  2600. $one = new Entity();
  2601. $two = new Entity();
  2602. TableRegistry::get('Contacts', [
  2603. 'className' => __NAMESPACE__ . '\ContactsTable'
  2604. ]);
  2605. $one->set('email', '');
  2606. $one->setError('email', ['invalid email']);
  2607. $two->set('name', '');
  2608. $two->setError('name', ['This is wrong']);
  2609. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2610. $result = $this->Form->control('0.email');
  2611. $expected = [
  2612. 'div' => ['class' => 'input email error'],
  2613. 'label' => ['for' => '0-email'],
  2614. 'Email',
  2615. '/label',
  2616. 'input' => [
  2617. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2618. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2619. ],
  2620. ['div' => ['class' => 'error-message']],
  2621. 'invalid email',
  2622. '/div',
  2623. '/div'
  2624. ];
  2625. $this->assertHtml($expected, $result);
  2626. $result = $this->Form->control('1.name');
  2627. $expected = [
  2628. 'div' => ['class' => 'input text error'],
  2629. 'label' => ['for' => '1-name'],
  2630. 'Name',
  2631. '/label',
  2632. 'input' => [
  2633. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2634. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2635. ],
  2636. ['div' => ['class' => 'error-message']],
  2637. 'This is wrong',
  2638. '/div',
  2639. '/div'
  2640. ];
  2641. $this->assertHtml($expected, $result);
  2642. }
  2643. /**
  2644. * testControl method
  2645. *
  2646. * Test various incarnations of control().
  2647. *
  2648. * @return void
  2649. */
  2650. public function testControl()
  2651. {
  2652. TableRegistry::get('ValidateUsers', [
  2653. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2654. ]);
  2655. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2656. $result = $this->Form->control('ValidateUsers.balance');
  2657. $expected = [
  2658. 'div' => ['class'],
  2659. 'label' => ['for'],
  2660. 'Balance',
  2661. '/label',
  2662. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2663. '/div',
  2664. ];
  2665. $this->assertHtml($expected, $result);
  2666. $result = $this->Form->control('ValidateUser.cost_decimal');
  2667. $expected = [
  2668. 'div' => ['class'],
  2669. 'label' => ['for'],
  2670. 'Cost Decimal',
  2671. '/label',
  2672. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2673. '/div',
  2674. ];
  2675. $this->assertHtml($expected, $result);
  2676. $result = $this->Form->control('ValidateUser.null_decimal');
  2677. $expected = [
  2678. 'div' => ['class'],
  2679. 'label' => ['for'],
  2680. 'Null Decimal',
  2681. '/label',
  2682. 'input' => ['name', 'type' => 'number', 'id'],
  2683. '/div',
  2684. ];
  2685. $this->assertHtml($expected, $result);
  2686. }
  2687. /**
  2688. * testControlCustomization method
  2689. *
  2690. * Tests the input method and passing custom options.
  2691. *
  2692. * @return void
  2693. */
  2694. public function testControlCustomization()
  2695. {
  2696. TableRegistry::get('Contacts', [
  2697. 'className' => __NAMESPACE__ . '\ContactsTable'
  2698. ]);
  2699. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2700. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2701. $expected = [
  2702. 'div' => ['class' => 'input email'],
  2703. 'label' => ['for' => 'custom'],
  2704. 'Email',
  2705. '/label',
  2706. ['input' => [
  2707. 'type' => 'email', 'name' => 'Contact[email]',
  2708. 'id' => 'custom', 'maxlength' => 255
  2709. ]],
  2710. '/div'
  2711. ];
  2712. $this->assertHtml($expected, $result);
  2713. $result = $this->Form->control('Contact.email', [
  2714. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2715. ]);
  2716. $expected = [
  2717. '<div',
  2718. 'label' => ['for' => 'contact-email'],
  2719. 'Email',
  2720. '/label',
  2721. ['input' => [
  2722. 'type' => 'email', 'name' => 'Contact[email]',
  2723. 'id' => 'contact-email', 'maxlength' => 255
  2724. ]],
  2725. '/div'
  2726. ];
  2727. $this->assertHtml($expected, $result);
  2728. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2729. $expected = [
  2730. 'div' => ['class' => 'input text'],
  2731. 'label' => ['for' => 'contact-email'],
  2732. 'Email',
  2733. '/label',
  2734. ['input' => [
  2735. 'type' => 'text', 'name' => 'Contact[email]',
  2736. 'id' => 'contact-email', 'maxlength' => '255'
  2737. ]],
  2738. '/div'
  2739. ];
  2740. $this->assertHtml($expected, $result);
  2741. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2742. $expected = [
  2743. 'div' => ['class' => 'input text'],
  2744. 'label' => ['for' => 'contact-5-email'],
  2745. 'Email',
  2746. '/label',
  2747. ['input' => [
  2748. 'type' => 'text', 'name' => 'Contact[5][email]',
  2749. 'id' => 'contact-5-email', 'maxlength' => '255'
  2750. ]],
  2751. '/div'
  2752. ];
  2753. $this->assertHtml($expected, $result);
  2754. $result = $this->Form->control('Contact.password');
  2755. $expected = [
  2756. 'div' => ['class' => 'input password'],
  2757. 'label' => ['for' => 'contact-password'],
  2758. 'Password',
  2759. '/label',
  2760. ['input' => [
  2761. 'type' => 'password', 'name' => 'Contact[password]',
  2762. 'id' => 'contact-password'
  2763. ]],
  2764. '/div'
  2765. ];
  2766. $this->assertHtml($expected, $result);
  2767. $result = $this->Form->control('Contact.email', [
  2768. 'type' => 'file', 'class' => 'textbox'
  2769. ]);
  2770. $expected = [
  2771. 'div' => ['class' => 'input file'],
  2772. 'label' => ['for' => 'contact-email'],
  2773. 'Email',
  2774. '/label',
  2775. ['input' => [
  2776. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2777. 'id' => 'contact-email'
  2778. ]],
  2779. '/div'
  2780. ];
  2781. $this->assertHtml($expected, $result);
  2782. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2783. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2784. $result = $this->Form->control('phone');
  2785. $expected = [
  2786. 'div' => ['class' => 'input tel'],
  2787. 'label' => ['for' => 'phone'],
  2788. 'Phone',
  2789. '/label',
  2790. ['input' => [
  2791. 'type' => 'tel', 'name' => 'phone',
  2792. 'value' => 'Hello &amp; World &gt; weird chars',
  2793. 'id' => 'phone', 'maxlength' => 255
  2794. ]],
  2795. '/div'
  2796. ];
  2797. $this->assertHtml($expected, $result);
  2798. $this->Form->request = $this->Form->request->withData('Model.0.OtherModel.field', 'My value');
  2799. $this->Form->create();
  2800. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2801. $expected = [
  2802. 'div' => ['class' => 'input text'],
  2803. 'label' => ['for' => 'myId'],
  2804. 'Field',
  2805. '/label',
  2806. 'input' => [
  2807. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2808. 'value' => 'My value', 'id' => 'myId'
  2809. ],
  2810. '/div'
  2811. ];
  2812. $this->assertHtml($expected, $result);
  2813. $this->Form->request = $this->Form->request->withParsedBody([]);
  2814. $this->Form->create();
  2815. $entity->setError('field', 'Badness!');
  2816. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2817. $result = $this->Form->control('field');
  2818. $expected = [
  2819. 'div' => ['class' => 'input text error'],
  2820. 'label' => ['for' => 'field'],
  2821. 'Field',
  2822. '/label',
  2823. 'input' => [
  2824. 'type' => 'text', 'name' => 'field',
  2825. 'id' => 'field', 'class' => 'form-error'
  2826. ],
  2827. ['div' => ['class' => 'error-message']],
  2828. 'Badness!',
  2829. '/div',
  2830. '/div'
  2831. ];
  2832. $this->assertHtml($expected, $result);
  2833. $result = $this->Form->control('field', [
  2834. 'templates' => [
  2835. 'inputContainerError' => '{{content}}{{error}}',
  2836. 'error' => '<span class="error-message">{{content}}</span>'
  2837. ]
  2838. ]);
  2839. $expected = [
  2840. 'label' => ['for' => 'field'],
  2841. 'Field',
  2842. '/label',
  2843. 'input' => [
  2844. 'type' => 'text', 'name' => 'field',
  2845. 'id' => 'field', 'class' => 'form-error'
  2846. ],
  2847. ['span' => ['class' => 'error-message']],
  2848. 'Badness!',
  2849. '/span'
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. $entity->setError('field', ['minLength'], true);
  2853. $result = $this->Form->control('field', [
  2854. 'error' => [
  2855. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2856. 'maxLength' => 'login too large'
  2857. ]
  2858. ]);
  2859. $expected = [
  2860. 'div' => ['class' => 'input text error'],
  2861. 'label' => ['for' => 'field'],
  2862. 'Field',
  2863. '/label',
  2864. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2865. ['div' => ['class' => 'error-message']],
  2866. 'Le login doit contenir au moins 2 caractères',
  2867. '/div',
  2868. '/div'
  2869. ];
  2870. $this->assertHtml($expected, $result);
  2871. $entity->setError('field', ['maxLength'], true);
  2872. $result = $this->Form->control('field', [
  2873. 'error' => [
  2874. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2875. 'maxLength' => 'login too large',
  2876. ]
  2877. ]);
  2878. $expected = [
  2879. 'div' => ['class' => 'input text error'],
  2880. 'label' => ['for' => 'field'],
  2881. 'Field',
  2882. '/label',
  2883. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2884. ['div' => ['class' => 'error-message']],
  2885. 'login too large',
  2886. '/div',
  2887. '/div'
  2888. ];
  2889. $this->assertHtml($expected, $result);
  2890. }
  2891. /**
  2892. * testControlWithTemplateFile method
  2893. *
  2894. * Test that control() accepts a template file.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testControlWithTemplateFile()
  2899. {
  2900. $result = $this->Form->control('field', [
  2901. 'templates' => 'htmlhelper_tags'
  2902. ]);
  2903. $expected = [
  2904. 'label' => ['for' => 'field'],
  2905. 'Field',
  2906. '/label',
  2907. 'input' => [
  2908. 'type' => 'text', 'name' => 'field',
  2909. 'id' => 'field'
  2910. ],
  2911. ];
  2912. $this->assertHtml($expected, $result);
  2913. }
  2914. /**
  2915. * testNestedControlsEndWithBrackets method
  2916. *
  2917. * Test that nested inputs end with brackets.
  2918. *
  2919. * @return void
  2920. */
  2921. public function testNestedControlsEndWithBrackets()
  2922. {
  2923. $result = $this->Form->text('nested.text[]');
  2924. $expected = [
  2925. 'input' => [
  2926. 'type' => 'text', 'name' => 'nested[text][]'
  2927. ],
  2928. ];
  2929. $this->assertHtml($expected, $result);
  2930. $result = $this->Form->file('nested.file[]');
  2931. $expected = [
  2932. 'input' => [
  2933. 'type' => 'file', 'name' => 'nested[file][]'
  2934. ],
  2935. ];
  2936. $this->assertHtml($expected, $result);
  2937. }
  2938. /**
  2939. * testCreateIdPrefix method
  2940. *
  2941. * Test id prefix.
  2942. *
  2943. * @return void
  2944. */
  2945. public function testCreateIdPrefix()
  2946. {
  2947. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2948. $result = $this->Form->control('field');
  2949. $expected = [
  2950. 'div' => ['class' => 'input text'],
  2951. 'label' => ['for' => 'prefix-field'],
  2952. 'Field',
  2953. '/label',
  2954. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2955. '/div'
  2956. ];
  2957. $this->assertHtml($expected, $result);
  2958. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2959. $expected = [
  2960. 'div' => ['class' => 'input text'],
  2961. 'label' => ['for' => 'custom-id'],
  2962. 'Field',
  2963. '/label',
  2964. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2965. '/div'
  2966. ];
  2967. $this->assertHtml($expected, $result);
  2968. $result = $this->Form->radio('Model.field', ['option A']);
  2969. $expected = [
  2970. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2971. 'label' => ['for' => 'prefix-model-field-0'],
  2972. ['input' => [
  2973. 'type' => 'radio',
  2974. 'name' => 'Model[field]',
  2975. 'value' => '0',
  2976. 'id' => 'prefix-model-field-0'
  2977. ]],
  2978. 'option A',
  2979. '/label'
  2980. ];
  2981. $this->assertHtml($expected, $result);
  2982. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2983. $expected = [
  2984. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2985. 'label' => ['for' => 'prefix-model-field-0'],
  2986. ['input' => [
  2987. 'type' => 'radio',
  2988. 'name' => 'Model[field]',
  2989. 'value' => '0',
  2990. 'id' => 'prefix-model-field-0'
  2991. ]],
  2992. 'option A',
  2993. '/label'
  2994. ];
  2995. $this->assertHtml($expected, $result);
  2996. $result = $this->Form->select(
  2997. 'Model.multi_field',
  2998. ['first'],
  2999. ['multiple' => 'checkbox']
  3000. );
  3001. $expected = [
  3002. 'input' => [
  3003. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3004. ],
  3005. ['div' => ['class' => 'checkbox']],
  3006. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3007. ['input' => [
  3008. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3009. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  3010. ]],
  3011. 'first',
  3012. '/label',
  3013. '/div',
  3014. ];
  3015. $this->assertHtml($expected, $result);
  3016. $this->Form->end();
  3017. $result = $this->Form->control('field');
  3018. $expected = [
  3019. 'div' => ['class' => 'input text'],
  3020. 'label' => ['for' => 'field'],
  3021. 'Field',
  3022. '/label',
  3023. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3024. '/div'
  3025. ];
  3026. $this->assertHtml($expected, $result);
  3027. }
  3028. /**
  3029. * testControlZero method
  3030. *
  3031. * Test that inputs with 0 can be created.
  3032. *
  3033. * @return void
  3034. */
  3035. public function testControlZero()
  3036. {
  3037. TableRegistry::get('Contacts', [
  3038. 'className' => __NAMESPACE__ . '\ContactsTable'
  3039. ]);
  3040. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3041. $result = $this->Form->control('0');
  3042. $expected = [
  3043. 'div' => ['class' => 'input text'],
  3044. 'label' => ['for' => '0'], '/label',
  3045. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3046. '/div'
  3047. ];
  3048. $this->assertHtml($expected, $result);
  3049. }
  3050. /**
  3051. * testControlCheckbox method
  3052. *
  3053. * Test control() with checkbox creation.
  3054. *
  3055. * @return void
  3056. */
  3057. public function testControlCheckbox()
  3058. {
  3059. $articles = TableRegistry::get('Articles');
  3060. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3061. $article = $articles->newEntity();
  3062. $this->Form->create($article);
  3063. $result = $this->Form->control('Articles.active');
  3064. $expected = [
  3065. 'div' => ['class' => 'input checkbox'],
  3066. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3067. 'label' => ['for' => 'articles-active'],
  3068. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3069. 'Active',
  3070. '/label',
  3071. '/div'
  3072. ];
  3073. $this->assertHtml($expected, $result);
  3074. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3075. $expected = [
  3076. 'div' => ['class' => 'input checkbox'],
  3077. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3078. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3079. '/div'
  3080. ];
  3081. $this->assertHtml($expected, $result);
  3082. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3083. $expected = [
  3084. 'div' => ['class' => 'input checkbox'],
  3085. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3086. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3087. '/div'
  3088. ];
  3089. $this->assertHtml($expected, $result);
  3090. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3091. $expected = [
  3092. 'div' => ['class' => 'input checkbox'],
  3093. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3094. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3095. '/div'
  3096. ];
  3097. $this->assertHtml($expected, $result);
  3098. $result = $this->Form->control('Articles.disabled', [
  3099. 'label' => 'Disabled',
  3100. 'type' => 'checkbox',
  3101. 'data-foo' => 'disabled'
  3102. ]);
  3103. $expected = [
  3104. 'div' => ['class' => 'input checkbox'],
  3105. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3106. 'label' => ['for' => 'articles-disabled'],
  3107. ['input' => [
  3108. 'type' => 'checkbox',
  3109. 'name' => 'Articles[disabled]',
  3110. 'value' => '1',
  3111. 'id' => 'articles-disabled',
  3112. 'data-foo' => 'disabled'
  3113. ]],
  3114. 'Disabled',
  3115. '/label',
  3116. '/div'
  3117. ];
  3118. $this->assertHtml($expected, $result);
  3119. $result = $this->Form->control('Articles.confirm', [
  3120. 'label' => 'Confirm <b>me</b>!',
  3121. 'type' => 'checkbox',
  3122. 'escape' => false
  3123. ]);
  3124. $expected = [
  3125. 'div' => ['class' => 'input checkbox'],
  3126. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3127. 'label' => ['for' => 'articles-confirm'],
  3128. ['input' => [
  3129. 'type' => 'checkbox',
  3130. 'name' => 'Articles[confirm]',
  3131. 'value' => '1',
  3132. 'id' => 'articles-confirm',
  3133. ]],
  3134. 'Confirm <b>me</b>!',
  3135. '/label',
  3136. '/div'
  3137. ];
  3138. $this->assertHtml($expected, $result);
  3139. }
  3140. /**
  3141. * testControlHidden method
  3142. *
  3143. * Test that control() does not create wrapping div and label tag for hidden fields.
  3144. *
  3145. * @return void
  3146. */
  3147. public function testControlHidden()
  3148. {
  3149. TableRegistry::get('ValidateUsers', [
  3150. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3151. ]);
  3152. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3153. $result = $this->Form->control('ValidateUser.id');
  3154. $expected = [
  3155. 'input' => ['name', 'type' => 'hidden', 'id']
  3156. ];
  3157. $this->assertHtml($expected, $result);
  3158. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3159. $expected = [
  3160. 'input' => ['name', 'type' => 'hidden', 'id']
  3161. ];
  3162. $this->assertHtml($expected, $result);
  3163. }
  3164. /**
  3165. * testControlDatetime method
  3166. *
  3167. * Test form->control() with datetime.
  3168. *
  3169. * @return void
  3170. */
  3171. public function testControlDatetime()
  3172. {
  3173. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3174. ->setMethods(['datetime'])
  3175. ->setConstructorArgs([new View()])
  3176. ->getMock();
  3177. $this->Form->expects($this->once())->method('datetime')
  3178. ->with('prueba', [
  3179. 'type' => 'datetime',
  3180. 'timeFormat' => 24,
  3181. 'minYear' => 2008,
  3182. 'maxYear' => 2011,
  3183. 'interval' => 15,
  3184. 'options' => null,
  3185. 'empty' => false,
  3186. 'id' => 'prueba',
  3187. 'required' => false,
  3188. 'templateVars' => []
  3189. ])
  3190. ->will($this->returnValue('This is it!'));
  3191. $result = $this->Form->control('prueba', [
  3192. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3193. 'maxYear' => 2011, 'interval' => 15
  3194. ]);
  3195. $expected = [
  3196. 'div' => ['class' => 'input datetime'],
  3197. '<label',
  3198. 'Prueba',
  3199. '/label',
  3200. 'This is it!',
  3201. '/div'
  3202. ];
  3203. $this->assertHtml($expected, $result);
  3204. }
  3205. /**
  3206. * testControlDatetimeIdPrefix method
  3207. *
  3208. * Test form->control() with datetime with id prefix.
  3209. *
  3210. * @return void
  3211. */
  3212. public function testControlDatetimeIdPrefix()
  3213. {
  3214. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3215. ->setMethods(['datetime'])
  3216. ->setConstructorArgs([new View()])
  3217. ->getMock();
  3218. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3219. $this->Form->expects($this->once())->method('datetime')
  3220. ->with('prueba', [
  3221. 'type' => 'datetime',
  3222. 'timeFormat' => 24,
  3223. 'minYear' => 2008,
  3224. 'maxYear' => 2011,
  3225. 'interval' => 15,
  3226. 'options' => null,
  3227. 'empty' => false,
  3228. 'id' => 'prefix-prueba',
  3229. 'required' => false,
  3230. 'templateVars' => []
  3231. ])
  3232. ->will($this->returnValue('This is it!'));
  3233. $result = $this->Form->control('prueba', [
  3234. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3235. 'maxYear' => 2011, 'interval' => 15
  3236. ]);
  3237. $expected = [
  3238. 'div' => ['class' => 'input datetime'],
  3239. '<label',
  3240. 'Prueba',
  3241. '/label',
  3242. 'This is it!',
  3243. '/div'
  3244. ];
  3245. $this->assertHtml($expected, $result);
  3246. }
  3247. /**
  3248. * testControlCheckboxWithDisabledElements method
  3249. *
  3250. * Test generating checkboxes with disabled elements.
  3251. *
  3252. * @return void
  3253. */
  3254. public function testControlCheckboxWithDisabledElements()
  3255. {
  3256. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3257. $result = $this->Form->control('Contact.multiple', [
  3258. 'multiple' => 'checkbox',
  3259. 'disabled' => 'disabled',
  3260. 'options' => $options
  3261. ]);
  3262. $expected = [
  3263. ['div' => ['class' => 'input select']],
  3264. ['label' => ['for' => 'contact-multiple']],
  3265. 'Multiple',
  3266. '/label',
  3267. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3268. ['div' => ['class' => 'checkbox']],
  3269. ['label' => ['for' => 'contact-multiple-1']],
  3270. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3271. 'One',
  3272. '/label',
  3273. '/div',
  3274. ['div' => ['class' => 'checkbox']],
  3275. ['label' => ['for' => 'contact-multiple-2']],
  3276. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3277. 'Two',
  3278. '/label',
  3279. '/div',
  3280. ['div' => ['class' => 'checkbox']],
  3281. ['label' => ['for' => 'contact-multiple-3']],
  3282. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3283. 'Three',
  3284. '/label',
  3285. '/div',
  3286. '/div'
  3287. ];
  3288. $this->assertHtml($expected, $result);
  3289. // make sure 50 does only disable 50, and not 50f5c0cf
  3290. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3291. $disabled = [50];
  3292. $expected = [
  3293. ['div' => ['class' => 'input select']],
  3294. ['label' => ['for' => 'contact-multiple']],
  3295. 'Multiple',
  3296. '/label',
  3297. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3298. ['div' => ['class' => 'checkbox']],
  3299. ['label' => ['for' => 'contact-multiple-50']],
  3300. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3301. 'Fifty',
  3302. '/label',
  3303. '/div',
  3304. ['div' => ['class' => 'checkbox']],
  3305. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3306. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3307. 'Stringy',
  3308. '/label',
  3309. '/div',
  3310. '/div'
  3311. ];
  3312. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3313. $this->assertHtml($expected, $result);
  3314. }
  3315. /**
  3316. * testControlWithLeadingInteger method
  3317. *
  3318. * Test input name with leading integer, ensure attributes are generated correctly.
  3319. *
  3320. * @return void
  3321. */
  3322. public function testControlWithLeadingInteger()
  3323. {
  3324. $result = $this->Form->text('0.Node.title');
  3325. $expected = [
  3326. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3327. ];
  3328. $this->assertHtml($expected, $result);
  3329. }
  3330. /**
  3331. * testControlSelectType method
  3332. *
  3333. * Test form->control() with select type inputs.
  3334. *
  3335. * @return void
  3336. */
  3337. public function testControlSelectType()
  3338. {
  3339. $result = $this->Form->control(
  3340. 'email',
  3341. [
  3342. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3343. );
  3344. $expected = [
  3345. 'div' => ['class' => 'input select'],
  3346. 'label' => ['for' => 'email'],
  3347. 'Email',
  3348. '/label',
  3349. ['select' => ['name' => 'email', 'id' => 'email']],
  3350. ['option' => ['value' => '']],
  3351. '/option',
  3352. ['option' => ['value' => 'è']],
  3353. 'Firést',
  3354. '/option',
  3355. ['option' => ['value' => 'é']],
  3356. 'Secoènd',
  3357. '/option',
  3358. '/select',
  3359. '/div'
  3360. ];
  3361. $this->assertHtml($expected, $result);
  3362. $result = $this->Form->control(
  3363. 'email',
  3364. [
  3365. 'options' => ['First', 'Second'], 'empty' => true]
  3366. );
  3367. $expected = [
  3368. 'div' => ['class' => 'input select'],
  3369. 'label' => ['for' => 'email'],
  3370. 'Email',
  3371. '/label',
  3372. ['select' => ['name' => 'email', 'id' => 'email']],
  3373. ['option' => ['value' => '']],
  3374. '/option',
  3375. ['option' => ['value' => '0']],
  3376. 'First',
  3377. '/option',
  3378. ['option' => ['value' => '1']],
  3379. 'Second',
  3380. '/option',
  3381. '/select',
  3382. '/div'
  3383. ];
  3384. $this->assertHtml($expected, $result);
  3385. $result = $this->Form->control('email', [
  3386. 'type' => 'select',
  3387. 'options' => new \ArrayObject(['First', 'Second']),
  3388. 'empty' => true
  3389. ]);
  3390. $this->assertHtml($expected, $result);
  3391. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3392. $this->Form->request = $this->Form->request->withData('Model', ['user_id' => 'value']);
  3393. $this->Form->create();
  3394. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3395. $expected = [
  3396. 'div' => ['class' => 'input select'],
  3397. 'label' => ['for' => 'model-user-id'],
  3398. 'User',
  3399. '/label',
  3400. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3401. ['option' => ['value' => '']],
  3402. '/option',
  3403. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3404. 'good',
  3405. '/option',
  3406. ['option' => ['value' => 'other']],
  3407. 'bad',
  3408. '/option',
  3409. '/select',
  3410. '/div'
  3411. ];
  3412. $this->assertHtml($expected, $result);
  3413. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3414. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => null]);
  3415. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3416. $expected = [
  3417. 'div' => ['class' => 'input select'],
  3418. 'label' => ['for' => 'thing-user-id'],
  3419. 'User',
  3420. '/label',
  3421. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3422. ['option' => ['value' => '']],
  3423. 'Some Empty',
  3424. '/option',
  3425. ['option' => ['value' => 'value']],
  3426. 'good',
  3427. '/option',
  3428. ['option' => ['value' => 'other']],
  3429. 'bad',
  3430. '/option',
  3431. '/select',
  3432. '/div'
  3433. ];
  3434. $this->assertHtml($expected, $result);
  3435. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3436. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => 'value']);
  3437. $this->Form->create();
  3438. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3439. $expected = [
  3440. 'div' => ['class' => 'input select'],
  3441. 'label' => ['for' => 'thing-user-id'],
  3442. 'User',
  3443. '/label',
  3444. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3445. ['option' => ['value' => '']],
  3446. 'Some Empty',
  3447. '/option',
  3448. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3449. 'good',
  3450. '/option',
  3451. ['option' => ['value' => 'other']],
  3452. 'bad',
  3453. '/option',
  3454. '/select',
  3455. '/div'
  3456. ];
  3457. $this->assertHtml($expected, $result);
  3458. $result = $this->Form->control('Publisher.id', [
  3459. 'label' => 'Publisher',
  3460. 'type' => 'select',
  3461. 'multiple' => 'checkbox',
  3462. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3463. ]);
  3464. $expected = [
  3465. ['div' => ['class' => 'input select']],
  3466. ['label' => ['for' => 'publisher-id']],
  3467. 'Publisher',
  3468. '/label',
  3469. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3470. ['div' => ['class' => 'checkbox']],
  3471. ['label' => ['for' => 'publisher-id-value-1']],
  3472. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3473. 'Label 1',
  3474. '/label',
  3475. '/div',
  3476. ['div' => ['class' => 'checkbox']],
  3477. ['label' => ['for' => 'publisher-id-value-2']],
  3478. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3479. 'Label 2',
  3480. '/label',
  3481. '/div',
  3482. '/div'
  3483. ];
  3484. $this->assertHtml($expected, $result);
  3485. }
  3486. /**
  3487. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3488. *
  3489. * Test that control() and a non standard primary key makes a hidden input by default.
  3490. *
  3491. * @return void
  3492. */
  3493. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3494. {
  3495. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3496. $this->Form->create($this->article);
  3497. $result = $this->Form->control('title');
  3498. $expected = [
  3499. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3500. ];
  3501. $this->assertHtml($expected, $result);
  3502. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3503. $this->Form->create($this->article);
  3504. $result = $this->Form->control('title');
  3505. $expected = [
  3506. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3507. ];
  3508. $this->assertHtml($expected, $result);
  3509. $result = $this->Form->control('body');
  3510. $expected = [
  3511. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3512. ];
  3513. $this->assertHtml($expected, $result);
  3514. }
  3515. /**
  3516. * testControlOverridingMagicSelectType method
  3517. *
  3518. * Test that overriding the magic select type widget is possible.
  3519. *
  3520. * @return void
  3521. */
  3522. public function testControlOverridingMagicSelectType()
  3523. {
  3524. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3525. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3526. $expected = [
  3527. 'div' => ['class' => 'input text'],
  3528. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3529. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3530. '/div'
  3531. ];
  3532. $this->assertHtml($expected, $result);
  3533. //Check that magic types still work for plural/singular vars
  3534. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3535. $result = $this->Form->control('Model.type');
  3536. $expected = [
  3537. 'div' => ['class' => 'input select'],
  3538. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3539. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3540. ['option' => ['value' => 'value']], 'good', '/option',
  3541. ['option' => ['value' => 'other']], 'bad', '/option',
  3542. '/select',
  3543. '/div'
  3544. ];
  3545. $this->assertHtml($expected, $result);
  3546. }
  3547. /**
  3548. * testControlMagicTypeDoesNotOverride method
  3549. *
  3550. * Test that inferred types do not override developer input.
  3551. *
  3552. * @return void
  3553. */
  3554. public function testControlMagicTypeDoesNotOverride()
  3555. {
  3556. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3557. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3558. $expected = [
  3559. 'div' => ['class' => 'input checkbox'],
  3560. ['input' => [
  3561. 'type' => 'hidden',
  3562. 'name' => 'Model[user]',
  3563. 'value' => 0,
  3564. ]],
  3565. 'label' => ['for' => 'model-user'],
  3566. ['input' => [
  3567. 'name' => 'Model[user]',
  3568. 'type' => 'checkbox',
  3569. 'id' => 'model-user',
  3570. 'value' => 1
  3571. ]],
  3572. 'User',
  3573. '/label',
  3574. '/div'
  3575. ];
  3576. $this->assertHtml($expected, $result);
  3577. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3578. $options = [
  3579. 1 => 'blue',
  3580. 2 => 'red'
  3581. ];
  3582. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3583. $expected = [
  3584. 'div' => ['class' => 'input select'],
  3585. 'label' => ['for' => 'tags-ids'],
  3586. 'Tags',
  3587. '/label',
  3588. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3589. ['div' => ['class' => 'checkbox']],
  3590. ['label' => ['for' => 'tags-ids-1']],
  3591. ['input' => [
  3592. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3593. 'value' => '1', 'name' => 'tags[_ids][]'
  3594. ]],
  3595. 'blue',
  3596. '/label',
  3597. '/div',
  3598. ['div' => ['class' => 'checkbox']],
  3599. ['label' => ['for' => 'tags-ids-2']],
  3600. ['input' => [
  3601. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3602. 'value' => '2', 'name' => 'tags[_ids][]'
  3603. ]],
  3604. 'red',
  3605. '/label',
  3606. '/div',
  3607. '/div'
  3608. ];
  3609. $this->assertHtml($expected, $result);
  3610. }
  3611. /**
  3612. * testControlMagicSelectForTypeNumber method
  3613. *
  3614. * Test that magic control() selects are created for type=number.
  3615. *
  3616. * @return void
  3617. */
  3618. public function testControlMagicSelectForTypeNumber()
  3619. {
  3620. TableRegistry::get('ValidateUsers', [
  3621. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3622. ]);
  3623. $entity = new Entity(['balance' => 1]);
  3624. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3625. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3626. $result = $this->Form->control('balance');
  3627. $expected = [
  3628. 'div' => ['class' => 'input select'],
  3629. 'label' => ['for' => 'balance'],
  3630. 'Balance',
  3631. '/label',
  3632. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3633. ['option' => ['value' => '0']],
  3634. 'nothing',
  3635. '/option',
  3636. ['option' => ['value' => '1', 'selected' => 'selected']],
  3637. 'some',
  3638. '/option',
  3639. ['option' => ['value' => '100']],
  3640. 'a lot',
  3641. '/option',
  3642. '/select',
  3643. '/div'
  3644. ];
  3645. $this->assertHtml($expected, $result);
  3646. }
  3647. /**
  3648. * testInvalidControlTypeOption method
  3649. *
  3650. * Test invalid 'input' type option to control() function.
  3651. *
  3652. * @return void
  3653. */
  3654. public function testInvalidControlTypeOption()
  3655. {
  3656. $this->expectException(\RuntimeException::class);
  3657. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3658. $this->Form->control('text', ['type' => 'input']);
  3659. }
  3660. /**
  3661. * testControlMagicSelectChangeToRadio method
  3662. *
  3663. * Test that magic control() selects can easily be converted into radio types without error.
  3664. *
  3665. * @return void
  3666. */
  3667. public function testControlMagicSelectChangeToRadio()
  3668. {
  3669. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3670. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3671. $this->assertContains('input type="radio"', $result);
  3672. }
  3673. /**
  3674. * testFormControlSubmit method
  3675. *
  3676. * Test correct results for form::control() and type submit.
  3677. *
  3678. * @return void
  3679. */
  3680. public function testFormControlSubmit()
  3681. {
  3682. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3683. $expected = [
  3684. 'div' => ['class' => 'submit'],
  3685. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3686. '/div'
  3687. ];
  3688. $this->assertHtml($expected, $result);
  3689. }
  3690. /**
  3691. * testFormControls method
  3692. *
  3693. * Test correct results from Form::controls().
  3694. *
  3695. * @return void
  3696. */
  3697. public function testFormControlsLegendFieldset()
  3698. {
  3699. $this->Form->create($this->article);
  3700. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3701. $expected = [
  3702. '<fieldset',
  3703. '<legend',
  3704. 'The Legend',
  3705. '/legend',
  3706. '*/fieldset',
  3707. ];
  3708. $this->assertHtml($expected, $result);
  3709. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3710. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3711. $this->assertContains('<fieldset>', $result);
  3712. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3713. $this->assertNotContains('<legend>', $result);
  3714. $this->assertNotContains('<fieldset>', $result);
  3715. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3716. $this->assertNotContains('<legend>', $result);
  3717. $this->assertNotContains('<fieldset>', $result);
  3718. $this->Form->create($this->article);
  3719. $this->Form->request = $this->Form->request
  3720. ->withParam('prefix', 'admin')
  3721. ->withParam('action', 'admin_edit')
  3722. ->withParam('controller', 'articles');
  3723. $result = $this->Form->allControls();
  3724. $expected = [
  3725. '<fieldset',
  3726. '<legend',
  3727. 'New Article',
  3728. '/legend',
  3729. '*/fieldset',
  3730. ];
  3731. $this->assertHtml($expected, $result);
  3732. $this->Form->create($this->article);
  3733. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3734. $expected = [
  3735. '<fieldset',
  3736. '<legend',
  3737. 'The Legend',
  3738. '/legend',
  3739. '*/fieldset',
  3740. ];
  3741. $this->assertHtml($expected, $result);
  3742. $this->Form->create($this->article);
  3743. $result = $this->Form->allControls([], [
  3744. 'fieldset' => [
  3745. 'class' => 'some-class some-other-class',
  3746. 'disabled' => true,
  3747. 'data-param' => 'a-param'
  3748. ],
  3749. 'legend' => 'The Legend'
  3750. ]);
  3751. $expected = [
  3752. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3753. '<legend',
  3754. 'The Legend',
  3755. '/legend',
  3756. '*/fieldset',
  3757. ];
  3758. $this->assertHtml($expected, $result);
  3759. }
  3760. /**
  3761. * testFormControls method
  3762. *
  3763. * Test the controls() method.
  3764. *
  3765. * @return void
  3766. */
  3767. public function testFormControls()
  3768. {
  3769. $this->Form->create($this->article);
  3770. $result = $this->Form->allControls();
  3771. $expected = [
  3772. '<fieldset',
  3773. '<legend', 'New Article', '/legend',
  3774. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3775. ['div' => ['class' => 'input select required']],
  3776. '*/div',
  3777. ['div' => ['class' => 'input text required']],
  3778. '*/div',
  3779. ['div' => ['class' => 'input text']],
  3780. '*/div',
  3781. ['div' => ['class' => 'input text']],
  3782. '*/div',
  3783. '/fieldset',
  3784. ];
  3785. $this->assertHtml($expected, $result);
  3786. $result = $this->Form->allControls([
  3787. 'published' => ['type' => 'boolean']
  3788. ]);
  3789. $expected = [
  3790. '<fieldset',
  3791. '<legend', 'New Article', '/legend',
  3792. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3793. ['div' => ['class' => 'input select required']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text required']],
  3796. '*/div',
  3797. ['div' => ['class' => 'input text']],
  3798. '*/div',
  3799. ['div' => ['class' => 'input boolean']],
  3800. '*/div',
  3801. '/fieldset',
  3802. ];
  3803. $this->assertHtml($expected, $result);
  3804. $this->Form->create($this->article);
  3805. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3806. $expected = [
  3807. 'fieldset' => [],
  3808. 'legend' => [],
  3809. 'Hello',
  3810. '/legend',
  3811. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3812. ['div' => ['class' => 'input select required']],
  3813. '*/div',
  3814. ['div' => ['class' => 'input text required']],
  3815. '*/div',
  3816. ['div' => ['class' => 'input text']],
  3817. '*/div',
  3818. ['div' => ['class' => 'input text']],
  3819. '*/div',
  3820. '/fieldset'
  3821. ];
  3822. $this->assertHtml($expected, $result);
  3823. $this->Form->create(false);
  3824. $expected = [
  3825. 'fieldset' => [],
  3826. ['div' => ['class' => 'input text']],
  3827. 'label' => ['for' => 'foo'],
  3828. 'Foo',
  3829. '/label',
  3830. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3831. '*/div',
  3832. '/fieldset'
  3833. ];
  3834. $result = $this->Form->allControls(
  3835. ['foo' => ['type' => 'text']],
  3836. ['legend' => false]
  3837. );
  3838. $this->assertHtml($expected, $result);
  3839. }
  3840. /**
  3841. * testFormControlsBlacklist method
  3842. *
  3843. * @return void
  3844. */
  3845. public function testFormControlsBlacklist()
  3846. {
  3847. $this->Form->create($this->article);
  3848. $result = $this->Form->allControls([
  3849. 'id' => false
  3850. ]);
  3851. $expected = [
  3852. '<fieldset',
  3853. '<legend', 'New Article', '/legend',
  3854. ['div' => ['class' => 'input select required']],
  3855. '*/div',
  3856. ['div' => ['class' => 'input text required']],
  3857. '*/div',
  3858. ['div' => ['class' => 'input text']],
  3859. '*/div',
  3860. ['div' => ['class' => 'input text']],
  3861. '*/div',
  3862. '/fieldset',
  3863. ];
  3864. $this->assertHtml($expected, $result);
  3865. $this->Form->create($this->article);
  3866. $result = $this->Form->allControls([
  3867. 'id' => []
  3868. ]);
  3869. $expected = [
  3870. '<fieldset',
  3871. '<legend', 'New Article', '/legend',
  3872. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3873. ['div' => ['class' => 'input select required']],
  3874. '*/div',
  3875. ['div' => ['class' => 'input text required']],
  3876. '*/div',
  3877. ['div' => ['class' => 'input text']],
  3878. '*/div',
  3879. ['div' => ['class' => 'input text']],
  3880. '*/div',
  3881. '/fieldset',
  3882. ];
  3883. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3884. }
  3885. /**
  3886. * testSelectAsCheckbox method
  3887. *
  3888. * Test multi-select widget with checkbox formatting.
  3889. *
  3890. * @return void
  3891. */
  3892. public function testSelectAsCheckbox()
  3893. {
  3894. $result = $this->Form->select(
  3895. 'Model.multi_field',
  3896. ['first', 'second', 'third'],
  3897. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3898. );
  3899. $expected = [
  3900. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3901. ['div' => ['class' => 'checkbox']],
  3902. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3903. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3904. 'first',
  3905. '/label',
  3906. '/div',
  3907. ['div' => ['class' => 'checkbox']],
  3908. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3909. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3910. 'second',
  3911. '/label',
  3912. '/div',
  3913. ['div' => ['class' => 'checkbox']],
  3914. ['label' => ['for' => 'model-multi-field-2']],
  3915. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3916. 'third',
  3917. '/label',
  3918. '/div',
  3919. ];
  3920. $this->assertHtml($expected, $result);
  3921. $result = $this->Form->select(
  3922. 'Model.multi_field',
  3923. ['1/2' => 'half'],
  3924. ['multiple' => 'checkbox']
  3925. );
  3926. $expected = [
  3927. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3928. ['div' => ['class' => 'checkbox']],
  3929. ['label' => ['for' => 'model-multi-field-1-2']],
  3930. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3931. 'half',
  3932. '/label',
  3933. '/div',
  3934. ];
  3935. $this->assertHtml($expected, $result);
  3936. }
  3937. /**
  3938. * testLabel method
  3939. *
  3940. * Test label generation.
  3941. *
  3942. * @return void
  3943. */
  3944. public function testLabel()
  3945. {
  3946. $result = $this->Form->label('Person.name');
  3947. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3948. $this->assertHtml($expected, $result);
  3949. $result = $this->Form->label('Person.name');
  3950. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3951. $this->assertHtml($expected, $result);
  3952. $result = $this->Form->label('Person.first_name');
  3953. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3954. $this->assertHtml($expected, $result);
  3955. $result = $this->Form->label('Person.first_name', 'Your first name');
  3956. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3957. $this->assertHtml($expected, $result);
  3958. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3959. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3960. $this->assertHtml($expected, $result);
  3961. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3962. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3963. $this->assertHtml($expected, $result);
  3964. $result = $this->Form->label('Person.first_name', '');
  3965. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3966. $this->assertHtml($expected, $result);
  3967. $result = $this->Form->label('Person.2.name', '');
  3968. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3969. $this->assertHtml($expected, $result);
  3970. }
  3971. /**
  3972. * testLabelContainControl method
  3973. *
  3974. * Test that label() can accept an input with the correct template vars.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testLabelContainControl()
  3979. {
  3980. $this->Form->setTemplates([
  3981. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3982. ]);
  3983. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3984. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3985. ]);
  3986. $expected = [
  3987. 'label' => ['for' => 'person-accept-terms'],
  3988. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3989. 'Accept',
  3990. '/label',
  3991. ];
  3992. $this->assertHtml($expected, $result);
  3993. }
  3994. /**
  3995. * testTextbox method
  3996. *
  3997. * Test textbox element generation.
  3998. *
  3999. * @return void
  4000. */
  4001. public function testTextbox()
  4002. {
  4003. $result = $this->Form->text('Model.field');
  4004. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4005. $this->assertHtml($expected, $result);
  4006. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4007. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4008. $this->assertHtml($expected, $result);
  4009. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4010. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4011. $this->assertHtml($expected, $result);
  4012. }
  4013. /**
  4014. * testTextBoxDataAndError method
  4015. *
  4016. * Test that text() hooks up with request data and error fields.
  4017. *
  4018. * @return void
  4019. */
  4020. public function testTextBoxDataAndError()
  4021. {
  4022. $this->article['errors'] = [
  4023. 'Contact' => ['text' => 'wrong']
  4024. ];
  4025. $this->Form->request = $this->Form->request
  4026. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4027. ->withData('Contact.text', 'test');
  4028. $this->Form->create($this->article);
  4029. $result = $this->Form->text('Model.text');
  4030. $expected = [
  4031. 'input' => [
  4032. 'type' => 'text',
  4033. 'name' => 'Model[text]',
  4034. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4035. ]
  4036. ];
  4037. $this->assertHtml($expected, $result);
  4038. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4039. $expected = [
  4040. 'input' => [
  4041. 'type' => 'text',
  4042. 'name' => 'Contact[text]',
  4043. 'value' => 'test',
  4044. 'id' => 'theID',
  4045. 'class' => 'form-error'
  4046. ]
  4047. ];
  4048. $this->assertHtml($expected, $result);
  4049. }
  4050. /**
  4051. * testDefaultValue method
  4052. *
  4053. * Test default value setting.
  4054. *
  4055. * @return void
  4056. */
  4057. public function testTextDefaultValue()
  4058. {
  4059. $this->Form->request = $this->Form->request->withData('Model.field', 'test');
  4060. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4061. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4062. $this->assertHtml($expected, $result);
  4063. $this->Form->request = $this->Form->request->withParsedBody([]);
  4064. $this->Form->create();
  4065. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4066. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4067. $this->assertHtml($expected, $result);
  4068. $Articles = TableRegistry::get('Articles');
  4069. $title = $Articles->getSchema()->getColumn('title');
  4070. $Articles->getSchema()->addColumn(
  4071. 'title',
  4072. ['default' => 'default title'] + $title
  4073. );
  4074. $entity = $Articles->newEntity();
  4075. $this->Form->create($entity);
  4076. // Get default value from schema
  4077. $result = $this->Form->text('title');
  4078. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4079. $this->assertHtml($expected, $result);
  4080. // Don't get value from schema
  4081. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4082. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4083. $this->assertHtml($expected, $result);
  4084. // Custom default value overrides default value from schema
  4085. $result = $this->Form->text('title', ['default' => 'override default']);
  4086. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4087. $this->assertHtml($expected, $result);
  4088. // Default value from schema is used only for new entities.
  4089. $entity->isNew(false);
  4090. $result = $this->Form->text('title');
  4091. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4092. $this->assertHtml($expected, $result);
  4093. }
  4094. /**
  4095. * testError method
  4096. *
  4097. * Test field error generation.
  4098. *
  4099. * @return void
  4100. */
  4101. public function testError()
  4102. {
  4103. $this->article['errors'] = [
  4104. 'Article' => ['field' => 'email']
  4105. ];
  4106. $this->Form->create($this->article);
  4107. $result = $this->Form->error('Article.field');
  4108. $expected = [
  4109. ['div' => ['class' => 'error-message']],
  4110. 'email',
  4111. '/div',
  4112. ];
  4113. $this->assertHtml($expected, $result);
  4114. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4115. $expected = [
  4116. ['div' => ['class' => 'error-message']],
  4117. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4118. '/div',
  4119. ];
  4120. $this->assertHtml($expected, $result);
  4121. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4122. $expected = [
  4123. ['div' => ['class' => 'error-message']],
  4124. '<strong', 'Badness!', '/strong',
  4125. '/div',
  4126. ];
  4127. $this->assertHtml($expected, $result);
  4128. }
  4129. /**
  4130. * testErrorRuleName method
  4131. *
  4132. * Test error translation can use rule names for translating.
  4133. *
  4134. * @return void
  4135. */
  4136. public function testErrorRuleName()
  4137. {
  4138. $this->article['errors'] = [
  4139. 'Article' => [
  4140. 'field' => ['email' => 'Your email was not good']
  4141. ]
  4142. ];
  4143. $this->Form->create($this->article);
  4144. $result = $this->Form->error('Article.field');
  4145. $expected = [
  4146. ['div' => ['class' => 'error-message']],
  4147. 'Your email was not good',
  4148. '/div',
  4149. ];
  4150. $this->assertHtml($expected, $result);
  4151. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4152. $expected = [
  4153. ['div' => ['class' => 'error-message']],
  4154. 'Email in use',
  4155. '/div',
  4156. ];
  4157. $this->assertHtml($expected, $result);
  4158. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4159. $expected = [
  4160. ['div' => ['class' => 'error-message']],
  4161. 'Email in use',
  4162. '/div',
  4163. ];
  4164. $this->assertHtml($expected, $result);
  4165. $result = $this->Form->error('Article.field', [
  4166. 'email' => 'Key is preferred',
  4167. 'Your email was not good' => 'Email in use'
  4168. ]);
  4169. $expected = [
  4170. ['div' => ['class' => 'error-message']],
  4171. 'Key is preferred',
  4172. '/div',
  4173. ];
  4174. $this->assertHtml($expected, $result);
  4175. }
  4176. /**
  4177. * testErrorMessages method
  4178. *
  4179. * Test error with nested lists.
  4180. *
  4181. * @return void
  4182. */
  4183. public function testErrorMessages()
  4184. {
  4185. $this->article['errors'] = [
  4186. 'Article' => ['field' => 'email']
  4187. ];
  4188. $this->Form->create($this->article);
  4189. $result = $this->Form->error('Article.field', [
  4190. 'email' => 'No good!'
  4191. ]);
  4192. $expected = [
  4193. 'div' => ['class' => 'error-message'],
  4194. 'No good!',
  4195. '/div'
  4196. ];
  4197. $this->assertHtml($expected, $result);
  4198. }
  4199. /**
  4200. * testErrorMultipleMessages method
  4201. *
  4202. * Test error() with multiple messages.
  4203. *
  4204. * @return void
  4205. */
  4206. public function testErrorMultipleMessages()
  4207. {
  4208. $this->article['errors'] = [
  4209. 'field' => ['notBlank', 'email', 'Something else']
  4210. ];
  4211. $this->Form->create($this->article);
  4212. $result = $this->Form->error('field', [
  4213. 'notBlank' => 'Cannot be empty',
  4214. 'email' => 'No good!'
  4215. ]);
  4216. $expected = [
  4217. 'div' => ['class' => 'error-message'],
  4218. 'ul' => [],
  4219. '<li', 'Cannot be empty', '/li',
  4220. '<li', 'No good!', '/li',
  4221. '<li', 'Something else', '/li',
  4222. '/ul',
  4223. '/div'
  4224. ];
  4225. $this->assertHtml($expected, $result);
  4226. }
  4227. /**
  4228. * testPassword method
  4229. *
  4230. * Test password element generation.
  4231. *
  4232. * @return void
  4233. */
  4234. public function testPassword()
  4235. {
  4236. $this->article['errors'] = [
  4237. 'Contact' => [
  4238. 'passwd' => 1
  4239. ]
  4240. ];
  4241. $this->Form->create($this->article);
  4242. $result = $this->Form->password('Contact.field');
  4243. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4244. $this->assertHtml($expected, $result);
  4245. $this->Form->request = $this->Form->request->withData('Contact.passwd', 'test');
  4246. $this->Form->create($this->article);
  4247. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4248. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4249. $this->assertHtml($expected, $result);
  4250. }
  4251. /**
  4252. * testRadio method
  4253. *
  4254. * Test radio element set generation.
  4255. *
  4256. * @return void
  4257. */
  4258. public function testRadio()
  4259. {
  4260. $result = $this->Form->radio('Model.field', ['option A']);
  4261. $expected = [
  4262. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4263. 'label' => ['for' => 'model-field-0'],
  4264. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4265. 'option A',
  4266. '/label'
  4267. ];
  4268. $this->assertHtml($expected, $result);
  4269. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4270. $this->assertHtml($expected, $result);
  4271. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4272. $expected = [
  4273. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4274. ['label' => ['for' => 'model-field-0']],
  4275. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4276. 'option A',
  4277. '/label',
  4278. ['label' => ['for' => 'model-field-1']],
  4279. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4280. 'option B',
  4281. '/label',
  4282. ];
  4283. $this->assertHtml($expected, $result);
  4284. $result = $this->Form->radio(
  4285. 'Employee.gender',
  4286. ['male' => 'Male', 'female' => 'Female'],
  4287. ['form' => 'my-form']
  4288. );
  4289. $expected = [
  4290. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4291. ['label' => ['for' => 'employee-gender-male']],
  4292. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4293. 'Male',
  4294. '/label',
  4295. ['label' => ['for' => 'employee-gender-female']],
  4296. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4297. 'Female',
  4298. '/label',
  4299. ];
  4300. $this->assertHtml($expected, $result);
  4301. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4302. $expected = [
  4303. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4304. ['label' => ['for' => 'model-custom-0']],
  4305. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4306. 'option A',
  4307. '/label',
  4308. ['label' => ['for' => 'model-custom-1']],
  4309. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4310. 'option B',
  4311. '/label',
  4312. ];
  4313. $this->assertHtml($expected, $result);
  4314. $result = $this->Form->radio(
  4315. 'Employee.gender',
  4316. [
  4317. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4318. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4319. ]
  4320. );
  4321. $expected = [
  4322. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4323. ['label' => ['for' => 'employee-gender-male']],
  4324. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4325. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4326. 'Male',
  4327. '/label',
  4328. ['label' => ['for' => 'employee-gender-female']],
  4329. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4330. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4331. 'Female',
  4332. '/label',
  4333. ];
  4334. $this->assertHtml($expected, $result);
  4335. }
  4336. /**
  4337. * Test radio with complex options and empty disabled data.
  4338. *
  4339. * @return void
  4340. */
  4341. public function testRadioComplexDisabled()
  4342. {
  4343. $options = [
  4344. ['value' => 'r', 'text' => 'red'],
  4345. ['value' => 'b', 'text' => 'blue'],
  4346. ];
  4347. $attrs = ['disabled' => []];
  4348. $result = $this->Form->radio('Model.field', $options, $attrs);
  4349. $expected = [
  4350. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4351. ['label' => ['for' => 'model-field-r']],
  4352. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4353. 'red',
  4354. '/label',
  4355. ['label' => ['for' => 'model-field-b']],
  4356. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4357. 'blue',
  4358. '/label',
  4359. ];
  4360. $this->assertHtml($expected, $result);
  4361. $attrs = ['disabled' => ['r']];
  4362. $result = $this->Form->radio('Model.field', $options, $attrs);
  4363. $this->assertContains('disabled="disabled"', $result);
  4364. }
  4365. /**
  4366. * testRadioDefaultValue method
  4367. *
  4368. * Test default value setting on radio() method.
  4369. *
  4370. * @return void
  4371. */
  4372. public function testRadioDefaultValue()
  4373. {
  4374. $Articles = TableRegistry::get('Articles');
  4375. $title = $Articles->getSchema()->getColumn('title');
  4376. $Articles->getSchema()->addColumn(
  4377. 'title',
  4378. ['default' => '1'] + $title
  4379. );
  4380. $this->Form->create($Articles->newEntity());
  4381. $result = $this->Form->radio('title', ['option A', 'option B']);
  4382. $expected = [
  4383. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4384. ['label' => ['for' => 'title-0']],
  4385. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4386. 'option A',
  4387. '/label',
  4388. ['label' => ['for' => 'title-1']],
  4389. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4390. 'option B',
  4391. '/label',
  4392. ];
  4393. $this->assertHtml($expected, $result);
  4394. }
  4395. /**
  4396. * Test setting a hiddenField value on radio buttons.
  4397. *
  4398. * @return void
  4399. */
  4400. public function testRadioHiddenFieldValue()
  4401. {
  4402. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4403. $expected = [
  4404. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4405. 'label' => ['for' => 'title-0'],
  4406. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4407. 'option A',
  4408. '/label',
  4409. ];
  4410. $this->assertHtml($expected, $result);
  4411. }
  4412. /**
  4413. * testControlRadio method
  4414. *
  4415. * Test that input works with radio types.
  4416. *
  4417. * @return void
  4418. */
  4419. public function testControlRadio()
  4420. {
  4421. $result = $this->Form->control('test', [
  4422. 'type' => 'radio',
  4423. 'options' => ['A', 'B'],
  4424. ]);
  4425. $expected = [
  4426. ['div' => ['class' => 'input radio']],
  4427. '<label',
  4428. 'Test',
  4429. '/label',
  4430. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4431. ['label' => ['for' => 'test-0']],
  4432. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4433. 'A',
  4434. '/label',
  4435. ['label' => ['for' => 'test-1']],
  4436. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4437. 'B',
  4438. '/label',
  4439. '/div',
  4440. ];
  4441. $this->assertHtml($expected, $result);
  4442. $result = $this->Form->control('test', [
  4443. 'type' => 'radio',
  4444. 'options' => ['A', 'B'],
  4445. 'value' => '0'
  4446. ]);
  4447. $expected = [
  4448. ['div' => ['class' => 'input radio']],
  4449. '<label',
  4450. 'Test',
  4451. '/label',
  4452. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4453. ['label' => ['for' => 'test-0']],
  4454. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4455. 'A',
  4456. '/label',
  4457. ['label' => ['for' => 'test-1']],
  4458. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4459. 'B',
  4460. '/label',
  4461. '/div',
  4462. ];
  4463. $this->assertHtml($expected, $result);
  4464. $result = $this->Form->control('test', [
  4465. 'type' => 'radio',
  4466. 'options' => ['A', 'B'],
  4467. 'label' => false
  4468. ]);
  4469. $expected = [
  4470. ['div' => ['class' => 'input radio']],
  4471. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4472. ['label' => ['for' => 'test-0']],
  4473. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4474. 'A',
  4475. '/label',
  4476. ['label' => ['for' => 'test-1']],
  4477. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4478. 'B',
  4479. '/label',
  4480. '/div',
  4481. ];
  4482. $this->assertHtml($expected, $result);
  4483. }
  4484. /**
  4485. * testRadioNoLabel method
  4486. *
  4487. * Test that radio() works with label = false.
  4488. *
  4489. * @return void
  4490. */
  4491. public function testRadioNoLabel()
  4492. {
  4493. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4494. $expected = [
  4495. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4496. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4497. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4498. ];
  4499. $this->assertHtml($expected, $result);
  4500. }
  4501. /**
  4502. * testRadioControlInsideLabel method
  4503. *
  4504. * Test generating radio input inside label ala twitter bootstrap.
  4505. *
  4506. * @return void
  4507. */
  4508. public function testRadioControlInsideLabel()
  4509. {
  4510. $this->Form->setTemplates([
  4511. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4512. 'radioWrapper' => '{{label}}'
  4513. ]);
  4514. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4515. //@codingStandardsIgnoreStart
  4516. $expected = [
  4517. ['input' => [
  4518. 'type' => 'hidden',
  4519. 'name' => 'Model[field]',
  4520. 'value' => ''
  4521. ]],
  4522. ['label' => ['for' => 'model-field-0']],
  4523. ['input' => [
  4524. 'type' => 'radio',
  4525. 'name' => 'Model[field]',
  4526. 'value' => '0',
  4527. 'id' => 'model-field-0'
  4528. ]],
  4529. 'option A',
  4530. '/label',
  4531. ['label' => ['for' => 'model-field-1']],
  4532. ['input' => [
  4533. 'type' => 'radio',
  4534. 'name' => 'Model[field]',
  4535. 'value' => '1',
  4536. 'id' => 'model-field-1'
  4537. ]],
  4538. 'option B',
  4539. '/label',
  4540. ];
  4541. //@codingStandardsIgnoreEnd
  4542. $this->assertHtml($expected, $result);
  4543. }
  4544. /**
  4545. * testRadioHiddenControlDisabling method
  4546. *
  4547. * Test disabling the hidden input for radio buttons.
  4548. *
  4549. * @return void
  4550. */
  4551. public function testRadioHiddenControlDisabling()
  4552. {
  4553. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4554. $expected = [
  4555. 'label' => ['for' => 'model-1-field-0'],
  4556. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4557. 'option A',
  4558. '/label'
  4559. ];
  4560. $this->assertHtml($expected, $result);
  4561. }
  4562. /**
  4563. * testRadioOutOfRange method
  4564. *
  4565. * Test radio element set generation.
  4566. *
  4567. * @return void
  4568. */
  4569. public function testRadioOutOfRange()
  4570. {
  4571. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4572. $expected = [
  4573. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4574. 'label' => ['for' => 'model-field-v'],
  4575. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4576. 'value',
  4577. '/label'
  4578. ];
  4579. $this->assertHtml($expected, $result);
  4580. }
  4581. /**
  4582. * testSelect method
  4583. *
  4584. * Test select element generation.
  4585. *
  4586. * @return void
  4587. */
  4588. public function testSelect()
  4589. {
  4590. $result = $this->Form->select('Model.field', []);
  4591. $expected = [
  4592. 'select' => ['name' => 'Model[field]'],
  4593. '/select'
  4594. ];
  4595. $this->assertHtml($expected, $result);
  4596. $this->Form->request = $this->Form->request->withData('Model', ['field' => 'value']);
  4597. $this->Form->create();
  4598. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4599. $expected = [
  4600. 'select' => ['name' => 'Model[field]'],
  4601. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4602. 'good',
  4603. '/option',
  4604. ['option' => ['value' => 'other']],
  4605. 'bad',
  4606. '/option',
  4607. '/select'
  4608. ];
  4609. $this->assertHtml($expected, $result);
  4610. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4611. $this->assertHtml($expected, $result);
  4612. $this->Form->request = $this->Form->request->withParsedBody([]);
  4613. $this->Form->create();
  4614. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4615. $expected = [
  4616. 'select' => ['name' => 'Model[field]'],
  4617. ['option' => ['value' => 'value']],
  4618. 'good',
  4619. '/option',
  4620. ['option' => ['value' => 'other']],
  4621. 'bad',
  4622. '/option',
  4623. '/select'
  4624. ];
  4625. $this->assertHtml($expected, $result);
  4626. $options = [
  4627. ['value' => 'first', 'text' => 'First'],
  4628. ['value' => 'first', 'text' => 'Another First'],
  4629. ];
  4630. $result = $this->Form->select(
  4631. 'Model.field',
  4632. $options,
  4633. ['escape' => false, 'empty' => false]
  4634. );
  4635. $expected = [
  4636. 'select' => ['name' => 'Model[field]'],
  4637. ['option' => ['value' => 'first']],
  4638. 'First',
  4639. '/option',
  4640. ['option' => ['value' => 'first']],
  4641. 'Another First',
  4642. '/option',
  4643. '/select'
  4644. ];
  4645. $this->assertHtml($expected, $result);
  4646. $this->Form->request = $this->Form->request->withParsedBody(['Model' => ['contact_id' => 228]]);
  4647. $this->Form->create();
  4648. $result = $this->Form->select(
  4649. 'Model.contact_id',
  4650. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4651. ['escape' => false, 'empty' => 'pick something']
  4652. );
  4653. $expected = [
  4654. 'select' => ['name' => 'Model[contact_id]'],
  4655. ['option' => ['value' => '']], 'pick something', '/option',
  4656. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4657. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4658. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4659. '/select'
  4660. ];
  4661. $this->assertHtml($expected, $result);
  4662. $this->Form->request = $this->Form->request->withData('Model.field', 0);
  4663. $this->Form->create();
  4664. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4665. $expected = [
  4666. 'select' => ['name' => 'Model[field]'],
  4667. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4668. ['option' => ['value' => '1']], 'Yes', '/option',
  4669. '/select'
  4670. ];
  4671. $this->assertHtml($expected, $result);
  4672. }
  4673. /**
  4674. * testSelectEscapeHtml method
  4675. *
  4676. * Test that select() escapes HTML.
  4677. *
  4678. * @return void
  4679. */
  4680. public function testSelectEscapeHtml()
  4681. {
  4682. $result = $this->Form->select(
  4683. 'Model.field',
  4684. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4685. ['empty' => false]
  4686. );
  4687. $expected = [
  4688. 'select' => ['name' => 'Model[field]'],
  4689. ['option' => ['value' => 'first']],
  4690. 'first &quot;html&quot; &lt;chars&gt;',
  4691. '/option',
  4692. ['option' => ['value' => 'second']],
  4693. 'value',
  4694. '/option',
  4695. '/select'
  4696. ];
  4697. $this->assertHtml($expected, $result);
  4698. $result = $this->Form->select(
  4699. 'Model.field',
  4700. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4701. ['escape' => false, 'empty' => false]
  4702. );
  4703. $expected = [
  4704. 'select' => ['name' => 'Model[field]'],
  4705. ['option' => ['value' => 'first']],
  4706. 'first "html" <chars>',
  4707. '/option',
  4708. ['option' => ['value' => 'second']],
  4709. 'value',
  4710. '/option',
  4711. '/select'
  4712. ];
  4713. $this->assertHtml($expected, $result);
  4714. }
  4715. /**
  4716. * testSelectRequired method
  4717. *
  4718. * Test select() with required and disabled attributes.
  4719. *
  4720. * @return void
  4721. */
  4722. public function testSelectRequired()
  4723. {
  4724. $this->article['required'] = [
  4725. 'user_id' => true
  4726. ];
  4727. $this->Form->create($this->article);
  4728. $result = $this->Form->select('user_id', ['option A']);
  4729. $expected = [
  4730. 'select' => [
  4731. 'name' => 'user_id',
  4732. 'required' => 'required'
  4733. ],
  4734. ['option' => ['value' => '0']], 'option A', '/option',
  4735. '/select'
  4736. ];
  4737. $this->assertHtml($expected, $result);
  4738. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4739. $expected = [
  4740. 'select' => [
  4741. 'name' => 'user_id',
  4742. 'disabled' => 'disabled'
  4743. ],
  4744. ['option' => ['value' => '0']], 'option A', '/option',
  4745. '/select'
  4746. ];
  4747. $this->assertHtml($expected, $result);
  4748. }
  4749. /**
  4750. * testNestedSelect method
  4751. *
  4752. * Test select element generation with optgroups.
  4753. *
  4754. * @return void
  4755. */
  4756. public function testNestedSelect()
  4757. {
  4758. $result = $this->Form->select(
  4759. 'Model.field',
  4760. [1 => 'One', 2 => 'Two', 'Three' => [
  4761. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4762. ]],
  4763. ['empty' => false]
  4764. );
  4765. $expected = [
  4766. 'select' => ['name' => 'Model[field]'],
  4767. ['option' => ['value' => 1]],
  4768. 'One',
  4769. '/option',
  4770. ['option' => ['value' => 2]],
  4771. 'Two',
  4772. '/option',
  4773. ['optgroup' => ['label' => 'Three']],
  4774. ['option' => ['value' => 3]],
  4775. 'Three',
  4776. '/option',
  4777. ['option' => ['value' => 4]],
  4778. 'Four',
  4779. '/option',
  4780. ['option' => ['value' => 5]],
  4781. 'Five',
  4782. '/option',
  4783. '/optgroup',
  4784. '/select'
  4785. ];
  4786. $this->assertHtml($expected, $result);
  4787. }
  4788. /**
  4789. * testSelectMultiple method
  4790. *
  4791. * Test generation of multiple select elements.
  4792. *
  4793. * @return void
  4794. */
  4795. public function testSelectMultiple()
  4796. {
  4797. $options = ['first', 'second', 'third'];
  4798. $result = $this->Form->select(
  4799. 'Model.multi_field',
  4800. $options,
  4801. ['form' => 'my-form', 'multiple' => true]
  4802. );
  4803. $expected = [
  4804. 'input' => [
  4805. 'type' => 'hidden',
  4806. 'name' => 'Model[multi_field]',
  4807. 'value' => '',
  4808. 'form' => 'my-form',
  4809. ],
  4810. 'select' => [
  4811. 'name' => 'Model[multi_field][]',
  4812. 'multiple' => 'multiple',
  4813. 'form' => 'my-form',
  4814. ],
  4815. ['option' => ['value' => '0']],
  4816. 'first',
  4817. '/option',
  4818. ['option' => ['value' => '1']],
  4819. 'second',
  4820. '/option',
  4821. ['option' => ['value' => '2']],
  4822. 'third',
  4823. '/option',
  4824. '/select'
  4825. ];
  4826. $this->assertHtml($expected, $result);
  4827. $result = $this->Form->select(
  4828. 'Model.multi_field',
  4829. $options,
  4830. ['multiple' => 'multiple', 'form' => 'my-form']
  4831. );
  4832. $this->assertHtml($expected, $result);
  4833. }
  4834. /**
  4835. * testCheckboxZeroValue method
  4836. *
  4837. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4838. *
  4839. * @return void
  4840. */
  4841. public function testCheckboxZeroValue()
  4842. {
  4843. $result = $this->Form->control('User.get_spam', [
  4844. 'type' => 'checkbox',
  4845. 'value' => '0',
  4846. 'hiddenField' => '1',
  4847. ]);
  4848. $expected = [
  4849. 'div' => ['class' => 'input checkbox'],
  4850. 'label' => ['for' => 'user-get-spam'],
  4851. ['input' => [
  4852. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4853. 'value' => '1'
  4854. ]],
  4855. ['input' => [
  4856. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4857. 'value' => '0', 'id' => 'user-get-spam'
  4858. ]],
  4859. 'Get Spam',
  4860. '/label',
  4861. '/div'
  4862. ];
  4863. $this->assertHtml($expected, $result);
  4864. }
  4865. /**
  4866. * testHabtmSelectBox method
  4867. *
  4868. * Test generation of habtm select boxes.
  4869. *
  4870. * @return void
  4871. */
  4872. public function testHabtmSelectBox()
  4873. {
  4874. $this->loadFixtures('Articles');
  4875. $options = [
  4876. 1 => 'blue',
  4877. 2 => 'red',
  4878. 3 => 'green'
  4879. ];
  4880. $tags = [
  4881. new Entity(['id' => 1, 'name' => 'blue']),
  4882. new Entity(['id' => 3, 'name' => 'green'])
  4883. ];
  4884. $article = new Article(['tags' => $tags]);
  4885. $this->Form->create($article);
  4886. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4887. $expected = [
  4888. 'div' => ['class' => 'input select'],
  4889. 'label' => ['for' => 'tags-ids'],
  4890. 'Tags',
  4891. '/label',
  4892. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4893. 'select' => [
  4894. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4895. 'multiple' => 'multiple'
  4896. ],
  4897. ['option' => ['value' => '1', 'selected' => 'selected']],
  4898. 'blue',
  4899. '/option',
  4900. ['option' => ['value' => '2']],
  4901. 'red',
  4902. '/option',
  4903. ['option' => ['value' => '3', 'selected' => 'selected']],
  4904. 'green',
  4905. '/option',
  4906. '/select',
  4907. '/div'
  4908. ];
  4909. $this->assertHtml($expected, $result);
  4910. // make sure only 50 is selected, and not 50f5c0cf
  4911. $options = [
  4912. '1' => 'blue',
  4913. '50f5c0cf' => 'red',
  4914. '50' => 'green'
  4915. ];
  4916. $tags = [
  4917. new Entity(['id' => 1, 'name' => 'blue']),
  4918. new Entity(['id' => 50, 'name' => 'green'])
  4919. ];
  4920. $article = new Article(['tags' => $tags]);
  4921. $this->Form->create($article);
  4922. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4923. $expected = [
  4924. 'div' => ['class' => 'input select'],
  4925. 'label' => ['for' => 'tags-ids'],
  4926. 'Tags',
  4927. '/label',
  4928. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4929. 'select' => [
  4930. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4931. 'multiple' => 'multiple'
  4932. ],
  4933. ['option' => ['value' => '1', 'selected' => 'selected']],
  4934. 'blue',
  4935. '/option',
  4936. ['option' => ['value' => '50f5c0cf']],
  4937. 'red',
  4938. '/option',
  4939. ['option' => ['value' => '50', 'selected' => 'selected']],
  4940. 'green',
  4941. '/option',
  4942. '/select',
  4943. '/div'
  4944. ];
  4945. $this->assertHtml($expected, $result);
  4946. $spacecraft = [
  4947. 1 => 'Orion',
  4948. 2 => 'Helios'
  4949. ];
  4950. $this->View->viewVars['spacecraft'] = $spacecraft;
  4951. $this->Form->create();
  4952. $result = $this->Form->control('spacecraft._ids');
  4953. $expected = [
  4954. 'div' => ['class' => 'input select'],
  4955. 'label' => ['for' => 'spacecraft-ids'],
  4956. 'Spacecraft',
  4957. '/label',
  4958. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4959. 'select' => [
  4960. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4961. 'multiple' => 'multiple'
  4962. ],
  4963. ['option' => ['value' => '1']],
  4964. 'Orion',
  4965. '/option',
  4966. ['option' => ['value' => '2']],
  4967. 'Helios',
  4968. '/option',
  4969. '/select',
  4970. '/div'
  4971. ];
  4972. $this->assertHtml($expected, $result);
  4973. }
  4974. /**
  4975. * testErrorsForBelongsToManySelect method
  4976. *
  4977. * Tests that errors for belongsToMany select fields are being
  4978. * picked up properly.
  4979. *
  4980. * @return void
  4981. */
  4982. public function testErrorsForBelongsToManySelect()
  4983. {
  4984. $spacecraft = [
  4985. 1 => 'Orion',
  4986. 2 => 'Helios'
  4987. ];
  4988. $this->View->viewVars['spacecraft'] = $spacecraft;
  4989. $article = new Article();
  4990. $article->setError('spacecraft', ['Invalid']);
  4991. $this->Form->create($article);
  4992. $result = $this->Form->control('spacecraft._ids');
  4993. $expected = [
  4994. ['div' => ['class' => 'input select error']],
  4995. 'label' => ['for' => 'spacecraft-ids'],
  4996. 'Spacecraft',
  4997. '/label',
  4998. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4999. 'select' => [
  5000. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5001. 'multiple' => 'multiple'
  5002. ],
  5003. ['option' => ['value' => '1']],
  5004. 'Orion',
  5005. '/option',
  5006. ['option' => ['value' => '2']],
  5007. 'Helios',
  5008. '/option',
  5009. '/select',
  5010. ['div' => ['class' => 'error-message']],
  5011. 'Invalid',
  5012. '/div',
  5013. '/div'
  5014. ];
  5015. $this->assertHtml($expected, $result);
  5016. }
  5017. /**
  5018. * testSelectMultipleCheckboxes method
  5019. *
  5020. * Test generation of multi select elements in checkbox format.
  5021. *
  5022. * @return void
  5023. */
  5024. public function testSelectMultipleCheckboxes()
  5025. {
  5026. $result = $this->Form->select(
  5027. 'Model.multi_field',
  5028. ['first', 'second', 'third'],
  5029. ['multiple' => 'checkbox']
  5030. );
  5031. $expected = [
  5032. 'input' => [
  5033. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5034. ],
  5035. ['div' => ['class' => 'checkbox']],
  5036. ['label' => ['for' => 'model-multi-field-0']],
  5037. ['input' => [
  5038. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5039. 'value' => '0', 'id' => 'model-multi-field-0'
  5040. ]],
  5041. 'first',
  5042. '/label',
  5043. '/div',
  5044. ['div' => ['class' => 'checkbox']],
  5045. ['label' => ['for' => 'model-multi-field-1']],
  5046. ['input' => [
  5047. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5048. 'value' => '1', 'id' => 'model-multi-field-1'
  5049. ]],
  5050. 'second',
  5051. '/label',
  5052. '/div',
  5053. ['div' => ['class' => 'checkbox']],
  5054. ['label' => ['for' => 'model-multi-field-2']],
  5055. ['input' => [
  5056. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5057. 'value' => '2', 'id' => 'model-multi-field-2'
  5058. ]],
  5059. 'third',
  5060. '/label',
  5061. '/div'
  5062. ];
  5063. $this->assertHtml($expected, $result);
  5064. $result = $this->Form->select(
  5065. 'Model.multi_field',
  5066. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5067. ['multiple' => 'checkbox']
  5068. );
  5069. $expected = [
  5070. 'input' => [
  5071. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5072. ],
  5073. ['div' => ['class' => 'checkbox']],
  5074. ['label' => ['for' => 'model-multi-field-a+']],
  5075. ['input' => [
  5076. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5077. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5078. ]],
  5079. 'first',
  5080. '/label',
  5081. '/div',
  5082. ['div' => ['class' => 'checkbox']],
  5083. ['label' => ['for' => 'model-multi-field-a++']],
  5084. ['input' => [
  5085. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5086. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5087. ]],
  5088. 'second',
  5089. '/label',
  5090. '/div',
  5091. ['div' => ['class' => 'checkbox']],
  5092. ['label' => ['for' => 'model-multi-field-a+++']],
  5093. ['input' => [
  5094. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5095. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5096. ]],
  5097. 'third',
  5098. '/label',
  5099. '/div'
  5100. ];
  5101. $this->assertHtml($expected, $result);
  5102. $result = $this->Form->select(
  5103. 'Model.multi_field',
  5104. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5105. ['multiple' => 'checkbox']
  5106. );
  5107. $expected = [
  5108. 'input' => [
  5109. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5110. ],
  5111. ['div' => ['class' => 'checkbox']],
  5112. ['label' => ['for' => 'model-multi-field-a-b']],
  5113. ['input' => [
  5114. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5115. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5116. ]],
  5117. 'first',
  5118. '/label',
  5119. '/div',
  5120. ['div' => ['class' => 'checkbox']],
  5121. ['label' => ['for' => 'model-multi-field-a-b1']],
  5122. ['input' => [
  5123. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5124. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5125. ]],
  5126. 'second',
  5127. '/label',
  5128. '/div',
  5129. ['div' => ['class' => 'checkbox']],
  5130. ['label' => ['for' => 'model-multi-field-a-b2']],
  5131. ['input' => [
  5132. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5133. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5134. ]],
  5135. 'third',
  5136. '/label',
  5137. '/div'
  5138. ];
  5139. $this->assertHtml($expected, $result);
  5140. }
  5141. /**
  5142. * testSelectMultipleCheckboxRequestData method
  5143. *
  5144. * Ensure that multiCheckbox reads from the request data.
  5145. *
  5146. * @return void
  5147. */
  5148. public function testSelectMultipleCheckboxRequestData()
  5149. {
  5150. $this->Form->request = $this->Form->request->withData('Model', ['tags' => [1]]);
  5151. $result = $this->Form->select(
  5152. 'Model.tags',
  5153. ['1' => 'first', 'Array' => 'Array'],
  5154. ['multiple' => 'checkbox']
  5155. );
  5156. $expected = [
  5157. 'input' => [
  5158. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5159. ],
  5160. ['div' => ['class' => 'checkbox']],
  5161. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5162. ['input' => [
  5163. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5164. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5165. ]],
  5166. 'first',
  5167. '/label',
  5168. '/div',
  5169. ['div' => ['class' => 'checkbox']],
  5170. ['label' => ['for' => 'model-tags-array']],
  5171. ['input' => [
  5172. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5173. 'value' => 'Array', 'id' => 'model-tags-array'
  5174. ]],
  5175. 'Array',
  5176. '/label',
  5177. '/div'
  5178. ];
  5179. $this->assertHtml($expected, $result);
  5180. }
  5181. /**
  5182. * testSelectMultipleCheckboxSecurity method
  5183. *
  5184. * Checks the security hash array generated for multiple-input checkbox elements.
  5185. *
  5186. * @return void
  5187. */
  5188. public function testSelectMultipleCheckboxSecurity()
  5189. {
  5190. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  5191. $this->assertEquals([], $this->Form->fields);
  5192. $this->Form->select(
  5193. 'Model.multi_field',
  5194. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5195. ['multiple' => 'checkbox']
  5196. );
  5197. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5198. $result = $this->Form->secure($this->Form->fields);
  5199. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5200. $this->assertContains('"' . $key . '"', $result);
  5201. }
  5202. /**
  5203. * testSelectMultipleSecureWithNoOptions method
  5204. *
  5205. * Multiple select elements should always be secured as they always participate
  5206. * in the POST data.
  5207. *
  5208. * @return void
  5209. */
  5210. public function testSelectMultipleSecureWithNoOptions()
  5211. {
  5212. $this->assertEquals([], $this->Form->fields);
  5213. $this->Form->select(
  5214. 'Model.select',
  5215. [],
  5216. ['multiple' => true]
  5217. );
  5218. $this->assertEquals(['Model.select'], $this->Form->fields);
  5219. }
  5220. /**
  5221. * testSelectNoSecureWithNoOptions method
  5222. *
  5223. * When a select box has no options it should not be added to the fields list
  5224. * as it always fail post validation.
  5225. *
  5226. * @return void
  5227. */
  5228. public function testSelectNoSecureWithNoOptions()
  5229. {
  5230. $this->Form->request = $this->Form->request->withParam('_Token', 'testkey');
  5231. $this->assertEquals([], $this->Form->fields);
  5232. $this->Form->select(
  5233. 'Model.select',
  5234. []
  5235. );
  5236. $this->assertEquals([], $this->Form->fields);
  5237. $this->Form->select(
  5238. 'Model.user_id',
  5239. [],
  5240. ['empty' => true]
  5241. );
  5242. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5243. }
  5244. /**
  5245. * testControlMultipleCheckboxes method
  5246. *
  5247. * Test control() resulting in multi select elements being generated.
  5248. *
  5249. * @return void
  5250. */
  5251. public function testControlMultipleCheckboxes()
  5252. {
  5253. $result = $this->Form->control('Model.multi_field', [
  5254. 'options' => ['first', 'second', 'third'],
  5255. 'multiple' => 'checkbox'
  5256. ]);
  5257. $expected = [
  5258. ['div' => ['class' => 'input select']],
  5259. ['label' => ['for' => 'model-multi-field']],
  5260. 'Multi Field',
  5261. '/label',
  5262. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5263. ['div' => ['class' => 'checkbox']],
  5264. ['label' => ['for' => 'model-multi-field-0']],
  5265. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5266. 'first',
  5267. '/label',
  5268. '/div',
  5269. ['div' => ['class' => 'checkbox']],
  5270. ['label' => ['for' => 'model-multi-field-1']],
  5271. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5272. 'second',
  5273. '/label',
  5274. '/div',
  5275. ['div' => ['class' => 'checkbox']],
  5276. ['label' => ['for' => 'model-multi-field-2']],
  5277. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5278. 'third',
  5279. '/label',
  5280. '/div',
  5281. '/div'
  5282. ];
  5283. $this->assertHtml($expected, $result);
  5284. $result = $this->Form->control('Model.multi_field', [
  5285. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5286. 'multiple' => 'checkbox'
  5287. ]);
  5288. $expected = [
  5289. ['div' => ['class' => 'input select']],
  5290. ['label' => ['for' => 'model-multi-field']],
  5291. 'Multi Field',
  5292. '/label',
  5293. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5294. ['div' => ['class' => 'checkbox']],
  5295. ['label' => ['for' => 'model-multi-field-a']],
  5296. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5297. 'first',
  5298. '/label',
  5299. '/div',
  5300. ['div' => ['class' => 'checkbox']],
  5301. ['label' => ['for' => 'model-multi-field-b']],
  5302. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5303. 'second',
  5304. '/label',
  5305. '/div',
  5306. ['div' => ['class' => 'checkbox']],
  5307. ['label' => ['for' => 'model-multi-field-c']],
  5308. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5309. 'third',
  5310. '/label',
  5311. '/div',
  5312. '/div'
  5313. ];
  5314. $this->assertHtml($expected, $result);
  5315. }
  5316. /**
  5317. * testSelectHiddenFieldOmission method
  5318. *
  5319. * Test that select() with 'hiddenField' => false omits the hidden field.
  5320. *
  5321. * @return void
  5322. */
  5323. public function testSelectHiddenFieldOmission()
  5324. {
  5325. $result = $this->Form->select(
  5326. 'Model.multi_field',
  5327. ['first', 'second'],
  5328. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5329. );
  5330. $this->assertNotContains('type="hidden"', $result);
  5331. }
  5332. /**
  5333. * testSelectCheckboxMultipleOverrideName method
  5334. *
  5335. * Test that select() with multiple = checkbox works with overriding name attribute.
  5336. *
  5337. * @return void
  5338. */
  5339. public function testSelectCheckboxMultipleOverrideName()
  5340. {
  5341. $result = $this->Form->select('category', ['1', '2'], [
  5342. 'multiple' => 'checkbox',
  5343. 'name' => 'fish',
  5344. ]);
  5345. $expected = [
  5346. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5347. ['div' => ['class' => 'checkbox']],
  5348. ['label' => ['for' => 'fish-0']],
  5349. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5350. '1',
  5351. '/label',
  5352. '/div',
  5353. ['div' => ['class' => 'checkbox']],
  5354. ['label' => ['for' => 'fish-1']],
  5355. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5356. '2',
  5357. '/label',
  5358. '/div'
  5359. ];
  5360. $this->assertHtml($expected, $result);
  5361. $result = $this->Form->multiCheckbox(
  5362. 'category',
  5363. new Collection(['1', '2']),
  5364. ['name' => 'fish']
  5365. );
  5366. $this->assertHtml($expected, $result);
  5367. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5368. 'name' => 'fish',
  5369. ]);
  5370. $this->assertHtml($expected, $result);
  5371. }
  5372. /**
  5373. * testControlMultiCheckbox method
  5374. *
  5375. * Test that control() works with multicheckbox.
  5376. *
  5377. * @return void
  5378. */
  5379. public function testControlMultiCheckbox()
  5380. {
  5381. $result = $this->Form->control('category', [
  5382. 'type' => 'multicheckbox',
  5383. 'options' => ['1', '2'],
  5384. ]);
  5385. $expected = [
  5386. ['div' => ['class' => 'input multicheckbox']],
  5387. '<label',
  5388. 'Category',
  5389. '/label',
  5390. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5391. ['div' => ['class' => 'checkbox']],
  5392. ['label' => ['for' => 'category-0']],
  5393. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5394. '1',
  5395. '/label',
  5396. '/div',
  5397. ['div' => ['class' => 'checkbox']],
  5398. ['label' => ['for' => 'category-1']],
  5399. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5400. '2',
  5401. '/label',
  5402. '/div',
  5403. '/div',
  5404. ];
  5405. $this->assertHtml($expected, $result);
  5406. }
  5407. /**
  5408. * testCheckbox method
  5409. *
  5410. * Test generation of checkboxes.
  5411. *
  5412. * @return void
  5413. */
  5414. public function testCheckbox()
  5415. {
  5416. $result = $this->Form->checkbox('Model.field');
  5417. $expected = [
  5418. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5419. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5420. ];
  5421. $this->assertHtml($expected, $result);
  5422. $result = $this->Form->checkbox('Model.field', [
  5423. 'id' => 'theID',
  5424. 'value' => 'myvalue',
  5425. 'form' => 'my-form',
  5426. ]);
  5427. $expected = [
  5428. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5429. ['input' => [
  5430. 'type' => 'checkbox', 'name' => 'Model[field]',
  5431. 'value' => 'myvalue', 'id' => 'theID',
  5432. 'form' => 'my-form',
  5433. ]]
  5434. ];
  5435. $this->assertHtml($expected, $result);
  5436. }
  5437. /**
  5438. * testCheckboxDefaultValue method
  5439. *
  5440. * Test default value setting on checkbox() method.
  5441. *
  5442. * @return void
  5443. */
  5444. public function testCheckboxDefaultValue()
  5445. {
  5446. $this->Form->request = $this->Form->request->withData('Model.field', false);
  5447. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5448. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5449. $this->assertHtml($expected, $result);
  5450. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5451. $this->Form->create();
  5452. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5453. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5454. $this->assertHtml($expected, $result);
  5455. $this->Form->request = $this->Form->request->withData('Model.field', true);
  5456. $this->Form->create();
  5457. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5458. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5459. $this->assertHtml($expected, $result);
  5460. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5461. $this->Form->create();
  5462. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5463. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5464. $this->assertHtml($expected, $result);
  5465. $Articles = TableRegistry::get('Articles');
  5466. $Articles->getSchema()->addColumn(
  5467. 'published',
  5468. ['type' => 'boolean', 'null' => false, 'default' => true]
  5469. );
  5470. $this->Form->create($Articles->newEntity());
  5471. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5472. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5473. $this->assertHtml($expected, $result);
  5474. }
  5475. /**
  5476. * testCheckboxCheckedAndError method
  5477. *
  5478. * Test checkbox being checked or having errors.
  5479. *
  5480. * @return void
  5481. */
  5482. public function testCheckboxCheckedAndError()
  5483. {
  5484. $this->article['errors'] = [
  5485. 'published' => true
  5486. ];
  5487. $this->Form->request = $this->Form->request->withData('published', 'myvalue');
  5488. $this->Form->create($this->article);
  5489. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5490. $expected = [
  5491. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5492. ['input' => [
  5493. 'type' => 'checkbox',
  5494. 'name' => 'published',
  5495. 'value' => 'myvalue',
  5496. 'id' => 'theID',
  5497. 'checked' => 'checked',
  5498. 'class' => 'form-error'
  5499. ]]
  5500. ];
  5501. $this->assertHtml($expected, $result);
  5502. $this->Form->request = $this->Form->request->withData('published', '');
  5503. $this->Form->create($this->article);
  5504. $result = $this->Form->checkbox('published');
  5505. $expected = [
  5506. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5507. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5508. ];
  5509. $this->assertHtml($expected, $result);
  5510. }
  5511. /**
  5512. * testCheckboxCustomNameAttribute method
  5513. *
  5514. * Test checkbox() with a custom name attribute.
  5515. *
  5516. * @return void
  5517. */
  5518. public function testCheckboxCustomNameAttribute()
  5519. {
  5520. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5521. $expected = [
  5522. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5523. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5524. ];
  5525. $this->assertHtml($expected, $result);
  5526. }
  5527. /**
  5528. * testCheckboxHiddenField method
  5529. *
  5530. * Test that the hidden input for checkboxes can be omitted or set to a
  5531. * specific value.
  5532. *
  5533. * @return void
  5534. */
  5535. public function testCheckboxHiddenField()
  5536. {
  5537. $result = $this->Form->checkbox('UserForm.something', [
  5538. 'hiddenField' => false
  5539. ]);
  5540. $expected = [
  5541. 'input' => [
  5542. 'type' => 'checkbox',
  5543. 'name' => 'UserForm[something]',
  5544. 'value' => '1'
  5545. ],
  5546. ];
  5547. $this->assertHtml($expected, $result);
  5548. $result = $this->Form->checkbox('UserForm.something', [
  5549. 'value' => 'Y',
  5550. 'hiddenField' => 'N',
  5551. ]);
  5552. $expected = [
  5553. ['input' => [
  5554. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5555. 'value' => 'N'
  5556. ]],
  5557. ['input' => [
  5558. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5559. 'value' => 'Y'
  5560. ]],
  5561. ];
  5562. $this->assertHtml($expected, $result);
  5563. }
  5564. /**
  5565. * testTime method
  5566. *
  5567. * Test the time type.
  5568. *
  5569. * @return void
  5570. */
  5571. public function testTime()
  5572. {
  5573. $result = $this->Form->time('start_time', [
  5574. 'timeFormat' => 12,
  5575. 'interval' => 5,
  5576. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5577. ]);
  5578. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5579. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5580. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5581. $this->assertNotContains('year', $result);
  5582. $this->assertNotContains('month', $result);
  5583. $this->assertNotContains('day', $result);
  5584. $result = $this->Form->time('start_time', [
  5585. 'timeFormat' => 12,
  5586. 'interval' => 5,
  5587. 'value' => '2014-03-08 16:30:00'
  5588. ]);
  5589. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5590. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5591. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5592. $this->assertNotContains('year', $result);
  5593. $this->assertNotContains('month', $result);
  5594. $this->assertNotContains('day', $result);
  5595. }
  5596. /**
  5597. * testTimeFormat24NoMeridian method
  5598. *
  5599. * Ensure that timeFormat=24 has no merdian.
  5600. *
  5601. * @return void.
  5602. */
  5603. public function testTimeFormat24NoMeridian()
  5604. {
  5605. $result = $this->Form->time('start_time', [
  5606. 'timeFormat' => 24,
  5607. 'interval' => 5,
  5608. 'value' => '2014-03-08 16:30:00'
  5609. ]);
  5610. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5611. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5612. $this->assertNotContains('meridian', $result);
  5613. $this->assertNotContains('pm', $result);
  5614. $this->assertNotContains('year', $result);
  5615. $this->assertNotContains('month', $result);
  5616. $this->assertNotContains('day', $result);
  5617. }
  5618. /**
  5619. * testDate method
  5620. *
  5621. * Test the date type.
  5622. *
  5623. * @return void
  5624. */
  5625. public function testDate()
  5626. {
  5627. $result = $this->Form->date('start_day', [
  5628. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5629. ]);
  5630. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5631. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5632. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5633. $this->assertNotContains('hour', $result);
  5634. $this->assertNotContains('minute', $result);
  5635. $this->assertNotContains('second', $result);
  5636. $this->assertNotContains('meridian', $result);
  5637. }
  5638. /**
  5639. * testDateTime method
  5640. *
  5641. * Test generation of date/time select elements.
  5642. *
  5643. * @return void
  5644. */
  5645. public function testDateTime()
  5646. {
  5647. extract($this->dateRegex);
  5648. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5649. $now = strtotime('now');
  5650. $expected = [
  5651. ['select' => ['name' => 'Contact[date][year]']],
  5652. ['option' => ['value' => '']],
  5653. '/option',
  5654. $yearsRegex,
  5655. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5656. date('Y', $now),
  5657. '/option',
  5658. '*/select',
  5659. ['select' => ['name' => 'Contact[date][month]']],
  5660. ['option' => ['value' => '']],
  5661. '/option',
  5662. $monthsRegex,
  5663. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5664. date('F', $now),
  5665. '/option',
  5666. '*/select',
  5667. ['select' => ['name' => 'Contact[date][day]']],
  5668. ['option' => ['value' => '']],
  5669. '/option',
  5670. $daysRegex,
  5671. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5672. date('j', $now),
  5673. '/option',
  5674. '*/select',
  5675. ['select' => ['name' => 'Contact[date][hour]']],
  5676. ['option' => ['value' => '']],
  5677. '/option',
  5678. $hoursRegex,
  5679. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5680. date('G', $now),
  5681. '/option',
  5682. '*/select',
  5683. ['select' => ['name' => 'Contact[date][minute]']],
  5684. ['option' => ['value' => '']],
  5685. '/option',
  5686. $minutesRegex,
  5687. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5688. date('i', $now),
  5689. '/option',
  5690. '*/select',
  5691. ];
  5692. $this->assertHtml($expected, $result);
  5693. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5694. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5695. $now = strtotime('now');
  5696. $expected = [
  5697. ['select' => ['name' => 'Contact[date][year]']],
  5698. $yearsRegex,
  5699. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5700. date('Y', $now),
  5701. '/option',
  5702. '*/select',
  5703. ['select' => ['name' => 'Contact[date][month]']],
  5704. $monthsRegex,
  5705. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5706. date('F', $now),
  5707. '/option',
  5708. '*/select',
  5709. ['select' => ['name' => 'Contact[date][day]']],
  5710. $daysRegex,
  5711. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5712. date('j', $now),
  5713. '/option',
  5714. '*/select',
  5715. ['select' => ['name' => 'Contact[date][hour]']],
  5716. $hoursRegex,
  5717. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5718. date('G', $now),
  5719. '/option',
  5720. '*/select',
  5721. ['select' => ['name' => 'Contact[date][minute]']],
  5722. $minutesRegex,
  5723. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5724. date('i', $now),
  5725. '/option',
  5726. '*/select',
  5727. ];
  5728. $this->assertHtml($expected, $result);
  5729. }
  5730. /**
  5731. * testDateTimeSecured method
  5732. *
  5733. * Test that datetime fields are added to protected fields list.
  5734. *
  5735. * @return void
  5736. */
  5737. public function testDateTimeSecured()
  5738. {
  5739. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5740. $this->Form->dateTime('Contact.date');
  5741. $expected = [
  5742. 'Contact.date.year',
  5743. 'Contact.date.month',
  5744. 'Contact.date.day',
  5745. 'Contact.date.hour',
  5746. 'Contact.date.minute',
  5747. ];
  5748. $this->assertEquals($expected, $this->Form->fields);
  5749. $this->Form->fields = [];
  5750. $this->Form->date('Contact.published');
  5751. $expected = [
  5752. 'Contact.published.year',
  5753. 'Contact.published.month',
  5754. 'Contact.published.day',
  5755. ];
  5756. $this->assertEquals($expected, $this->Form->fields);
  5757. }
  5758. /**
  5759. * testDateTimeSecuredDisabled method
  5760. *
  5761. * Test that datetime fields are added to protected fields list.
  5762. *
  5763. * @return void
  5764. */
  5765. public function testDateTimeSecuredDisabled()
  5766. {
  5767. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5768. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5769. $expected = [];
  5770. $this->assertEquals($expected, $this->Form->fields);
  5771. $this->Form->fields = [];
  5772. $this->Form->date('Contact.published', ['secure' => false]);
  5773. $expected = [];
  5774. $this->assertEquals($expected, $this->Form->fields);
  5775. }
  5776. /**
  5777. * testDatetimeEmpty method
  5778. *
  5779. * Test empty defaulting to true for datetime.
  5780. *
  5781. * @return void
  5782. */
  5783. public function testDatetimeEmpty()
  5784. {
  5785. extract($this->dateRegex);
  5786. $result = $this->Form->dateTime('Contact.date', [
  5787. 'timeFormat' => 12,
  5788. 'empty' => true,
  5789. 'default' => true
  5790. ]);
  5791. $expected = [
  5792. ['select' => ['name' => 'Contact[date][year]']],
  5793. $yearsRegex,
  5794. ['option' => ['value' => '']],
  5795. '/option',
  5796. '*/select',
  5797. ['select' => ['name' => 'Contact[date][month]']],
  5798. $monthsRegex,
  5799. ['option' => ['value' => '']],
  5800. '/option',
  5801. '*/select',
  5802. ['select' => ['name' => 'Contact[date][day]']],
  5803. $daysRegex,
  5804. ['option' => ['value' => '']],
  5805. '/option',
  5806. '*/select',
  5807. ['select' => ['name' => 'Contact[date][hour]']],
  5808. $hoursRegex,
  5809. ['option' => ['value' => '']],
  5810. '/option',
  5811. '*/select',
  5812. ['select' => ['name' => 'Contact[date][minute]']],
  5813. $minutesRegex,
  5814. ['option' => ['value' => '']],
  5815. '/option',
  5816. '*/select',
  5817. ['select' => ['name' => 'Contact[date][meridian]']],
  5818. $meridianRegex,
  5819. ['option' => ['value' => '']],
  5820. '/option',
  5821. '*/select'
  5822. ];
  5823. $this->assertHtml($expected, $result);
  5824. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5825. }
  5826. /**
  5827. * testDatetimeMinuteInterval method
  5828. *
  5829. * Test datetime with interval option.
  5830. *
  5831. * @return void
  5832. */
  5833. public function testDatetimeMinuteInterval()
  5834. {
  5835. extract($this->dateRegex);
  5836. $result = $this->Form->dateTime('Contact.date', [
  5837. 'interval' => 5,
  5838. 'value' => ''
  5839. ]);
  5840. $expected = [
  5841. ['select' => ['name' => 'Contact[date][year]']],
  5842. $yearsRegex,
  5843. ['option' => ['selected' => 'selected', 'value' => '']],
  5844. '/option',
  5845. '*/select',
  5846. ['select' => ['name' => 'Contact[date][month]']],
  5847. $monthsRegex,
  5848. ['option' => ['selected' => 'selected', 'value' => '']],
  5849. '/option',
  5850. '*/select',
  5851. ['select' => ['name' => 'Contact[date][day]']],
  5852. $daysRegex,
  5853. ['option' => ['selected' => 'selected', 'value' => '']],
  5854. '/option',
  5855. '*/select',
  5856. ['select' => ['name' => 'Contact[date][hour]']],
  5857. $hoursRegex,
  5858. ['option' => ['selected' => 'selected', 'value' => '']],
  5859. '/option',
  5860. '*/select',
  5861. ['select' => ['name' => 'Contact[date][minute]']],
  5862. $minutesRegex,
  5863. ['option' => ['selected' => 'selected', 'value' => '']],
  5864. '/option',
  5865. ['option' => ['value' => '00']],
  5866. '00',
  5867. '/option',
  5868. ['option' => ['value' => '05']],
  5869. '05',
  5870. '/option',
  5871. ['option' => ['value' => '10']],
  5872. '10',
  5873. '/option',
  5874. '*/select',
  5875. ];
  5876. $this->assertHtml($expected, $result);
  5877. }
  5878. /**
  5879. * testDateTimeRounding method
  5880. *
  5881. * Test dateTime with rounding.
  5882. *
  5883. * @return void
  5884. */
  5885. public function testDateTimeRounding()
  5886. {
  5887. $this->Form->request = $this->Form->request->withData('Contact', [
  5888. 'date' => [
  5889. 'day' => '13',
  5890. 'month' => '12',
  5891. 'year' => '2010',
  5892. 'hour' => '04',
  5893. 'minute' => '19',
  5894. 'meridian' => 'AM'
  5895. ]
  5896. ]);
  5897. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5898. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5899. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5900. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5901. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5902. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5903. }
  5904. /**
  5905. * testDatetimeWithDefault method
  5906. *
  5907. * Test that datetime() and default values work.
  5908. *
  5909. * @return void
  5910. */
  5911. public function testDatetimeWithDefault()
  5912. {
  5913. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5914. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5915. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5916. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5917. $result = $this->Form->dateTime('Contact.updated', [
  5918. 'default' => '2009-06-01 11:15:30'
  5919. ]);
  5920. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5921. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5922. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5923. }
  5924. /**
  5925. * testDateTimeAllZeros method
  5926. *
  5927. * Test datetime() with all zeros.
  5928. *
  5929. * @return void
  5930. */
  5931. public function testDateTimeAllZeros()
  5932. {
  5933. $result = $this->Form->dateTime('Contact.date', [
  5934. 'timeFormat' => false,
  5935. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5936. 'value' => '0000-00-00'
  5937. ]);
  5938. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5939. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5940. }
  5941. /**
  5942. * testDateTimeEmptyAsArray method
  5943. *
  5944. * @return void
  5945. */
  5946. public function testDateTimeEmptyAsArray()
  5947. {
  5948. $result = $this->Form->dateTime('Contact.date', [
  5949. 'empty' => [
  5950. 'day' => 'DAY',
  5951. 'month' => 'MONTH',
  5952. 'year' => 'YEAR',
  5953. 'hour' => 'HOUR',
  5954. 'minute' => 'MINUTE',
  5955. 'meridian' => false
  5956. ],
  5957. 'default' => true
  5958. ]);
  5959. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5960. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5961. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5962. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5963. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5964. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5965. $result = $this->Form->dateTime('Contact.date', [
  5966. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5967. 'default' => true
  5968. ]);
  5969. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5970. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5971. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5972. }
  5973. /**
  5974. * testFormDateTimeMulti method
  5975. *
  5976. * Test multiple datetime element generation.
  5977. *
  5978. * @return void
  5979. */
  5980. public function testFormDateTimeMulti()
  5981. {
  5982. extract($this->dateRegex);
  5983. $result = $this->Form->dateTime('Contact.1.updated');
  5984. $this->assertContains('Contact[1][updated][month]', $result);
  5985. $this->assertContains('Contact[1][updated][day]', $result);
  5986. $this->assertContains('Contact[1][updated][year]', $result);
  5987. $this->assertContains('Contact[1][updated][hour]', $result);
  5988. $this->assertContains('Contact[1][updated][minute]', $result);
  5989. }
  5990. /**
  5991. * testDateTimeLabelIdMatchesFirstControl method
  5992. *
  5993. * When changing the date format, the label should always focus the first select box when
  5994. * clicked.
  5995. *
  5996. * @return void
  5997. */
  5998. public function testDateTimeLabelIdMatchesFirstControl()
  5999. {
  6000. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6001. $this->assertContains('<label>Date</label>', $result);
  6002. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6003. $this->assertContains('<label>Date</label>', $result);
  6004. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6005. $this->assertContains('<label>Date</label>', $result);
  6006. }
  6007. /**
  6008. * testDateTimeSecondOptions method
  6009. *
  6010. * Test datetime second=true.
  6011. *
  6012. * @return void
  6013. */
  6014. public function testDateTimeSecondOptions()
  6015. {
  6016. $result = $this->Form->dateTime('updated', ['second' => true]);
  6017. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6018. $result = $this->Form->dateTime('updated', ['second' => []]);
  6019. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6020. $result = $this->Form->dateTime('updated', ['second' => null]);
  6021. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6022. $result = $this->Form->dateTime('updated', ['second' => false]);
  6023. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6024. }
  6025. /**
  6026. * testMonth method
  6027. *
  6028. * Test generation of a month input.
  6029. *
  6030. * @return void
  6031. */
  6032. public function testMonth()
  6033. {
  6034. $result = $this->Form->month('Model.field', ['value' => '']);
  6035. $expected = [
  6036. ['select' => ['name' => 'Model[field][month]']],
  6037. ['option' => ['value' => '', 'selected' => 'selected']],
  6038. '/option',
  6039. ['option' => ['value' => '01']],
  6040. date('F', strtotime('2008-01-01 00:00:00')),
  6041. '/option',
  6042. ['option' => ['value' => '02']],
  6043. date('F', strtotime('2008-02-01 00:00:00')),
  6044. '/option',
  6045. '*/select',
  6046. ];
  6047. $this->assertHtml($expected, $result);
  6048. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6049. $expected = [
  6050. ['select' => ['name' => 'Model[field][month]']],
  6051. ['option' => ['selected' => 'selected', 'value' => '']],
  6052. '/option',
  6053. ['option' => ['value' => '01']],
  6054. date('F', strtotime('2008-01-01 00:00:00')),
  6055. '/option',
  6056. ['option' => ['value' => '02']],
  6057. date('F', strtotime('2008-02-01 00:00:00')),
  6058. '/option',
  6059. '*/select',
  6060. ];
  6061. $this->assertHtml($expected, $result);
  6062. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6063. $expected = [
  6064. ['select' => ['name' => 'Model[field][month]']],
  6065. ['option' => ['selected' => 'selected', 'value' => '']],
  6066. '/option',
  6067. ['option' => ['value' => '01']],
  6068. '1',
  6069. '/option',
  6070. ['option' => ['value' => '02']],
  6071. '2',
  6072. '/option',
  6073. '*/select',
  6074. ];
  6075. $this->assertHtml($expected, $result);
  6076. $monthNames = [
  6077. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6078. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6079. ];
  6080. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6081. $expected = [
  6082. ['select' => ['name' => 'Model[field][month]']],
  6083. ['option' => ['value' => '']],
  6084. '/option',
  6085. ['option' => ['value' => '01', 'selected' => 'selected']],
  6086. 'Jan',
  6087. '/option',
  6088. ['option' => ['value' => '02']],
  6089. 'Feb',
  6090. '/option',
  6091. '*/select',
  6092. ];
  6093. $this->assertHtml($expected, $result);
  6094. $this->Form->request = $this->Form->request->withData('Project.release', '2050-02-10');
  6095. $this->Form->create();
  6096. $result = $this->Form->month('Project.release');
  6097. $expected = [
  6098. ['select' => ['name' => 'Project[release][month]']],
  6099. ['option' => ['value' => '']],
  6100. '/option',
  6101. ['option' => ['value' => '01']],
  6102. 'January',
  6103. '/option',
  6104. ['option' => ['value' => '02', 'selected' => 'selected']],
  6105. 'February',
  6106. '/option',
  6107. '*/select',
  6108. ];
  6109. $this->assertHtml($expected, $result);
  6110. $result = $this->Form->month('Contact.published', [
  6111. 'empty' => 'Published on',
  6112. ]);
  6113. $this->assertContains('Published on', $result);
  6114. }
  6115. /**
  6116. * testDay method
  6117. *
  6118. * Test generation of a day input.
  6119. *
  6120. * @return void
  6121. */
  6122. public function testDay()
  6123. {
  6124. extract($this->dateRegex);
  6125. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6126. $expected = [
  6127. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6128. ['option' => ['selected' => 'selected', 'value' => '']],
  6129. '/option',
  6130. ['option' => ['value' => '01']],
  6131. '1',
  6132. '/option',
  6133. ['option' => ['value' => '02']],
  6134. '2',
  6135. '/option',
  6136. $daysRegex,
  6137. '/select',
  6138. ];
  6139. $this->assertHtml($expected, $result);
  6140. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 23:12:32');
  6141. $this->Form->create();
  6142. $result = $this->Form->day('Model.field');
  6143. $expected = [
  6144. ['select' => ['name' => 'Model[field][day]']],
  6145. ['option' => ['value' => '']],
  6146. '/option',
  6147. ['option' => ['value' => '01']],
  6148. '1',
  6149. '/option',
  6150. ['option' => ['value' => '02']],
  6151. '2',
  6152. '/option',
  6153. $daysRegex,
  6154. ['option' => ['value' => '10', 'selected' => 'selected']],
  6155. '10',
  6156. '/option',
  6157. $daysRegex,
  6158. '/select',
  6159. ];
  6160. $this->assertHtml($expected, $result);
  6161. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6162. $this->Form->create();
  6163. $result = $this->Form->day('Model.field', ['value' => '10']);
  6164. $expected = [
  6165. ['select' => ['name' => 'Model[field][day]']],
  6166. ['option' => ['value' => '']],
  6167. '/option',
  6168. ['option' => ['value' => '01']],
  6169. '1',
  6170. '/option',
  6171. ['option' => ['value' => '02']],
  6172. '2',
  6173. '/option',
  6174. $daysRegex,
  6175. ['option' => ['value' => '10', 'selected' => 'selected']],
  6176. '10',
  6177. '/option',
  6178. $daysRegex,
  6179. '/select',
  6180. ];
  6181. $this->assertHtml($expected, $result);
  6182. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6183. $this->Form->create();
  6184. $result = $this->Form->day('Project.release');
  6185. $expected = [
  6186. ['select' => ['name' => 'Project[release][day]']],
  6187. ['option' => ['value' => '']],
  6188. '/option',
  6189. ['option' => ['value' => '01']],
  6190. '1',
  6191. '/option',
  6192. ['option' => ['value' => '02']],
  6193. '2',
  6194. '/option',
  6195. $daysRegex,
  6196. ['option' => ['value' => '10', 'selected' => 'selected']],
  6197. '10',
  6198. '/option',
  6199. $daysRegex,
  6200. '/select',
  6201. ];
  6202. $this->assertHtml($expected, $result);
  6203. $result = $this->Form->day('Contact.published', [
  6204. 'empty' => 'Published on',
  6205. ]);
  6206. $this->assertContains('Published on', $result);
  6207. }
  6208. /**
  6209. * testMinute method
  6210. *
  6211. * Test generation of a minute input.
  6212. *
  6213. * @return void
  6214. */
  6215. public function testMinute()
  6216. {
  6217. extract($this->dateRegex);
  6218. $result = $this->Form->minute('Model.field', ['value' => '']);
  6219. $expected = [
  6220. ['select' => ['name' => 'Model[field][minute]']],
  6221. ['option' => ['selected' => 'selected', 'value' => '']],
  6222. '/option',
  6223. ['option' => ['value' => '00']],
  6224. '00',
  6225. '/option',
  6226. ['option' => ['value' => '01']],
  6227. '01',
  6228. '/option',
  6229. ['option' => ['value' => '02']],
  6230. '02',
  6231. '/option',
  6232. $minutesRegex,
  6233. '/select',
  6234. ];
  6235. $this->assertHtml($expected, $result);
  6236. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6237. $this->Form->create();
  6238. $result = $this->Form->minute('Model.field');
  6239. $expected = [
  6240. ['select' => ['name' => 'Model[field][minute]']],
  6241. ['option' => ['value' => '']],
  6242. '/option',
  6243. ['option' => ['value' => '00']],
  6244. '00',
  6245. '/option',
  6246. ['option' => ['value' => '01']],
  6247. '01',
  6248. '/option',
  6249. ['option' => ['value' => '02']],
  6250. '02',
  6251. '/option',
  6252. $minutesRegex,
  6253. ['option' => ['value' => '12', 'selected' => 'selected']],
  6254. '12',
  6255. '/option',
  6256. $minutesRegex,
  6257. '/select',
  6258. ];
  6259. $this->assertHtml($expected, $result);
  6260. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6261. $this->Form->create();
  6262. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6263. $expected = [
  6264. ['select' => ['name' => 'Model[field][minute]']],
  6265. ['option' => ['selected' => 'selected', 'value' => '']],
  6266. '/option',
  6267. ['option' => ['value' => '00']],
  6268. '00',
  6269. '/option',
  6270. ['option' => ['value' => '05']],
  6271. '05',
  6272. '/option',
  6273. ['option' => ['value' => '10']],
  6274. '10',
  6275. '/option',
  6276. $minutesRegex,
  6277. '/select',
  6278. ];
  6279. $this->assertHtml($expected, $result);
  6280. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:10:32');
  6281. $this->Form->create();
  6282. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6283. $expected = [
  6284. ['select' => ['name' => 'Model[field][minute]']],
  6285. ['option' => ['value' => '']],
  6286. '/option',
  6287. ['option' => ['value' => '00']],
  6288. '00',
  6289. '/option',
  6290. ['option' => ['value' => '05']],
  6291. '05',
  6292. '/option',
  6293. ['option' => ['value' => '10', 'selected' => 'selected']],
  6294. '10',
  6295. '/option',
  6296. $minutesRegex,
  6297. '/select',
  6298. ];
  6299. $this->assertHtml($expected, $result);
  6300. }
  6301. /**
  6302. * testMeridian method
  6303. *
  6304. * Test generating an input for the meridian.
  6305. *
  6306. * @return void
  6307. */
  6308. public function testMeridian()
  6309. {
  6310. extract($this->dateRegex);
  6311. $now = new \DateTime();
  6312. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6313. $expected = [
  6314. ['select' => ['name' => 'Model[field][meridian]']],
  6315. ['option' => ['value' => '']],
  6316. '/option',
  6317. $meridianRegex,
  6318. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6319. $now->format('a'),
  6320. '/option',
  6321. '*/select'
  6322. ];
  6323. $this->assertHtml($expected, $result);
  6324. }
  6325. /**
  6326. * testHour method
  6327. *
  6328. * Test generation of an hour input.
  6329. *
  6330. * @return void
  6331. */
  6332. public function testHour()
  6333. {
  6334. extract($this->dateRegex);
  6335. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6336. $expected = [
  6337. ['select' => ['name' => 'Model[field][hour]']],
  6338. ['option' => ['selected' => 'selected', 'value' => '']],
  6339. '/option',
  6340. ['option' => ['value' => '01']],
  6341. '1',
  6342. '/option',
  6343. ['option' => ['value' => '02']],
  6344. '2',
  6345. '/option',
  6346. $hoursRegex,
  6347. '/select',
  6348. ];
  6349. $this->assertHtml($expected, $result);
  6350. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6351. $this->Form->create();
  6352. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6353. $expected = [
  6354. ['select' => ['name' => 'Model[field][hour]']],
  6355. ['option' => ['value' => '']],
  6356. '/option',
  6357. ['option' => ['value' => '01']],
  6358. '1',
  6359. '/option',
  6360. ['option' => ['value' => '02']],
  6361. '2',
  6362. '/option',
  6363. $hoursRegex,
  6364. ['option' => ['value' => '12', 'selected' => 'selected']],
  6365. '12',
  6366. '/option',
  6367. '/select',
  6368. ];
  6369. $this->assertHtml($expected, $result);
  6370. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6371. $this->Form->create();
  6372. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6373. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6374. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6375. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6376. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6377. $this->Form->create();
  6378. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6379. $expected = [
  6380. ['select' => ['name' => 'Model[field][hour]']],
  6381. ['option' => ['value' => '']],
  6382. '/option',
  6383. ['option' => ['value' => '00', 'selected' => 'selected']],
  6384. '0',
  6385. '/option',
  6386. ['option' => ['value' => '01']],
  6387. '1',
  6388. '/option',
  6389. ['option' => ['value' => '02']],
  6390. '2',
  6391. '/option',
  6392. $hoursRegex,
  6393. '/select',
  6394. ];
  6395. $this->assertHtml($expected, $result);
  6396. $this->Form->request = $this->Form->request->withData('Model.field', null);
  6397. $this->Form->create();
  6398. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6399. $thisHour = date('H');
  6400. $optValue = date('G');
  6401. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6402. $this->Form->request = $this->Form->request->withData('Model.field', '2050-10-10 01:12:32');
  6403. $this->Form->create();
  6404. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6405. $expected = [
  6406. ['select' => ['name' => 'Model[field][hour]']],
  6407. ['option' => ['value' => '']],
  6408. '/option',
  6409. ['option' => ['value' => '00']],
  6410. '0',
  6411. '/option',
  6412. ['option' => ['value' => '01', 'selected' => 'selected']],
  6413. '1',
  6414. '/option',
  6415. ['option' => ['value' => '02']],
  6416. '2',
  6417. '/option',
  6418. $hoursRegex,
  6419. '/select',
  6420. ];
  6421. $this->assertHtml($expected, $result);
  6422. }
  6423. /**
  6424. * testYear method
  6425. *
  6426. * Test generation of a year input.
  6427. *
  6428. * @return void
  6429. */
  6430. public function testYear()
  6431. {
  6432. $this->Form->request = $this->Form->request->withData('Contact.published', '2006-10-10');
  6433. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6434. $expected = [
  6435. ['select' => ['name' => 'Model[field][year]']],
  6436. ['option' => ['selected' => 'selected', 'value' => '']],
  6437. '/option',
  6438. ['option' => ['value' => '2007']],
  6439. '2007',
  6440. '/option',
  6441. ['option' => ['value' => '2006']],
  6442. '2006',
  6443. '/option',
  6444. '/select',
  6445. ];
  6446. $this->assertHtml($expected, $result);
  6447. $result = $this->Form->year('Model.field', [
  6448. 'value' => '',
  6449. 'minYear' => 2006,
  6450. 'maxYear' => 2007,
  6451. 'orderYear' => 'asc'
  6452. ]);
  6453. $expected = [
  6454. ['select' => ['name' => 'Model[field][year]']],
  6455. ['option' => ['selected' => 'selected', 'value' => '']],
  6456. '/option',
  6457. ['option' => ['value' => '2006']],
  6458. '2006',
  6459. '/option',
  6460. ['option' => ['value' => '2007']],
  6461. '2007',
  6462. '/option',
  6463. '/select',
  6464. ];
  6465. $this->assertHtml($expected, $result);
  6466. $result = $this->Form->year('Contact.published', [
  6467. 'empty' => false,
  6468. 'minYear' => 2006,
  6469. 'maxYear' => 2007,
  6470. ]);
  6471. $expected = [
  6472. ['select' => ['name' => 'Contact[published][year]']],
  6473. ['option' => ['value' => '2007']],
  6474. '2007',
  6475. '/option',
  6476. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6477. '2006',
  6478. '/option',
  6479. '/select',
  6480. ];
  6481. $this->assertHtml($expected, $result);
  6482. $result = $this->Form->year('Contact.published', [
  6483. 'empty' => 'Published on',
  6484. ]);
  6485. $this->assertContains('Published on', $result);
  6486. }
  6487. /**
  6488. * testControlDatetimePreEpoch method
  6489. *
  6490. * Test minYear being prior to the unix epoch.
  6491. *
  6492. * @return void
  6493. */
  6494. public function testControlDatetimePreEpoch()
  6495. {
  6496. $start = date('Y') - 80;
  6497. $end = date('Y') - 18;
  6498. $result = $this->Form->control('birth_year', [
  6499. 'type' => 'date',
  6500. 'label' => 'Birth Year',
  6501. 'minYear' => $start,
  6502. 'maxYear' => $end,
  6503. 'month' => false,
  6504. 'day' => false,
  6505. ]);
  6506. $this->assertContains('value="' . $start . '">' . $start, $result);
  6507. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6508. $this->assertNotContains('value="00">00', $result);
  6509. }
  6510. /**
  6511. * testYearAutoExpandRange method
  6512. *
  6513. * @return void
  6514. */
  6515. public function testYearAutoExpandRange()
  6516. {
  6517. $this->Form->request = $this->Form->request->withData('User.birthday', '1930-10-10');
  6518. $result = $this->Form->year('User.birthday');
  6519. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6520. $result = $matches[1];
  6521. $expected = range(date('Y') + 5, 1930);
  6522. $this->assertEquals($expected, $result);
  6523. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6524. $this->Form->create();
  6525. $result = $this->Form->year('Project.release');
  6526. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6527. $result = $matches[1];
  6528. $expected = range(2050, date('Y') - 5);
  6529. $this->assertEquals($expected, $result);
  6530. $this->Form->request = $this->Form->request->withData('Project.release', '1881-10-10');
  6531. $this->Form->create();
  6532. $result = $this->Form->year('Project.release', [
  6533. 'minYear' => 1890,
  6534. 'maxYear' => 1900
  6535. ]);
  6536. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6537. $result = $matches[1];
  6538. $expected = range(1900, 1881);
  6539. $this->assertEquals($expected, $result);
  6540. }
  6541. /**
  6542. * testControlDate method
  6543. *
  6544. * Test that control() accepts the type of date and passes options in.
  6545. *
  6546. * @return void
  6547. */
  6548. public function testControlDate()
  6549. {
  6550. $this->Form->request = $this->Form->request->withParsedBody([
  6551. 'month_year' => ['month' => date('m')],
  6552. ]);
  6553. $this->Form->create($this->article);
  6554. $result = $this->Form->control('month_year', [
  6555. 'label' => false,
  6556. 'type' => 'date',
  6557. 'minYear' => 2006,
  6558. 'maxYear' => 2008
  6559. ]);
  6560. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6561. $this->assertNotContains('value="2008" selected="selected"', $result);
  6562. }
  6563. /**
  6564. * testControlLabelFalse method
  6565. *
  6566. * Test the label option being set to false.
  6567. *
  6568. * @return void
  6569. */
  6570. public function testControlLabelFalse()
  6571. {
  6572. $this->Form->create($this->article);
  6573. $result = $this->Form->control('title', ['label' => false]);
  6574. $expected = [
  6575. 'div' => ['class' => 'input text required'],
  6576. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6577. '/div'
  6578. ];
  6579. $this->assertHtml($expected, $result);
  6580. }
  6581. /**
  6582. * testControlDateMaxYear method
  6583. *
  6584. * Let's say we want to only allow users born from 2006 to 2008 to register
  6585. * This being the first signup page, we still don't have any data.
  6586. *
  6587. * @return void
  6588. */
  6589. public function testControlDateMaxYear()
  6590. {
  6591. $this->Form->create($this->article);
  6592. $result = $this->Form->control('birthday', [
  6593. 'label' => false,
  6594. 'type' => 'date',
  6595. 'minYear' => 2006,
  6596. 'maxYear' => 2008,
  6597. 'default' => true
  6598. ]);
  6599. $this->assertContains('value="2008" selected="selected"', $result);
  6600. $this->assertContains('value="2006"', $result);
  6601. $this->assertNotContains('value="2005"', $result);
  6602. $this->assertNotContains('value="2009"', $result);
  6603. }
  6604. /**
  6605. * testTextArea method
  6606. *
  6607. * Test generation of a textarea input.
  6608. *
  6609. * @return void
  6610. */
  6611. public function testTextArea()
  6612. {
  6613. $this->Form->request = $this->Form->request->withData('field', 'some test data');
  6614. $result = $this->Form->textarea('field');
  6615. $expected = [
  6616. 'textarea' => ['name' => 'field', 'rows' => 5],
  6617. 'some test data',
  6618. '/textarea',
  6619. ];
  6620. $this->assertHtml($expected, $result);
  6621. $result = $this->Form->textarea('user.bio');
  6622. $expected = [
  6623. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6624. '/textarea',
  6625. ];
  6626. $this->assertHtml($expected, $result);
  6627. $this->Form->request = $this->Form->request
  6628. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  6629. $this->Form->create();
  6630. $result = $this->Form->textarea('field');
  6631. $expected = [
  6632. 'textarea' => ['name' => 'field', 'rows' => 5],
  6633. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6634. '/textarea',
  6635. ];
  6636. $this->assertHtml($expected, $result);
  6637. $this->Form->request = $this->Form->request->withData(
  6638. 'Model.field',
  6639. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6640. );
  6641. $this->Form->create();
  6642. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6643. $expected = [
  6644. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6645. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6646. '/textarea',
  6647. ];
  6648. $this->assertHtml($expected, $result);
  6649. $result = $this->Form->textarea('0.OtherModel.field');
  6650. $expected = [
  6651. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6652. '/textarea'
  6653. ];
  6654. $this->assertHtml($expected, $result);
  6655. }
  6656. /**
  6657. * testTextAreaWithStupidCharacters method
  6658. *
  6659. * Test text area with non-ascii characters.
  6660. *
  6661. * @return void
  6662. */
  6663. public function testTextAreaWithStupidCharacters()
  6664. {
  6665. $result = $this->Form->textarea('Post.content', [
  6666. 'value' => 'GREAT®',
  6667. 'rows' => '15',
  6668. 'cols' => '75'
  6669. ]);
  6670. $expected = [
  6671. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6672. 'GREAT®',
  6673. '/textarea',
  6674. ];
  6675. $this->assertHtml($expected, $result);
  6676. }
  6677. /**
  6678. * testTextAreaMaxLength method
  6679. *
  6680. * Test textareas maxlength read from schema.
  6681. *
  6682. * @return void
  6683. */
  6684. public function testTextAreaMaxLength()
  6685. {
  6686. $this->Form->create([
  6687. 'schema' => [
  6688. 'stuff' => ['type' => 'string', 'length' => 10],
  6689. ]
  6690. ]);
  6691. $result = $this->Form->control('other', ['type' => 'textarea']);
  6692. $expected = [
  6693. 'div' => ['class' => 'input textarea'],
  6694. 'label' => ['for' => 'other'],
  6695. 'Other',
  6696. '/label',
  6697. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6698. '/textarea',
  6699. '/div'
  6700. ];
  6701. $this->assertHtml($expected, $result);
  6702. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6703. $expected = [
  6704. 'div' => ['class' => 'input textarea'],
  6705. 'label' => ['for' => 'stuff'],
  6706. 'Stuff',
  6707. '/label',
  6708. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6709. '/textarea',
  6710. '/div'
  6711. ];
  6712. $this->assertHtml($expected, $result);
  6713. }
  6714. /**
  6715. * testHiddenField method
  6716. *
  6717. * Test generation of a hidden input.
  6718. *
  6719. * @return void
  6720. */
  6721. public function testHiddenField()
  6722. {
  6723. $this->article['errors'] = [
  6724. 'field' => true
  6725. ];
  6726. $this->Form->request = $this->Form->request->withData('field', 'test');
  6727. $this->Form->create($this->article);
  6728. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6729. $expected = [
  6730. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6731. $this->assertHtml($expected, $result);
  6732. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6733. $expected = [
  6734. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6735. ];
  6736. $this->assertHtml($expected, $result);
  6737. }
  6738. /**
  6739. * testFileUploadField method
  6740. *
  6741. * Test generation of a file upload input.
  6742. *
  6743. * @return void
  6744. */
  6745. public function testFileUploadField()
  6746. {
  6747. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6748. $result = $this->Form->file('Model.upload');
  6749. $this->assertHtml($expected, $result);
  6750. $this->Form->request = $this->Form->request->withData('Model.upload', [
  6751. 'name' => '', 'type' => '', 'tmp_name' => '',
  6752. 'error' => 4, 'size' => 0
  6753. ]);
  6754. $result = $this->Form->file('Model.upload');
  6755. $this->assertHtml($expected, $result);
  6756. $this->Form->request = $this->Form->request->withData('Model.upload', 'no data should be set in value');
  6757. $result = $this->Form->file('Model.upload');
  6758. $this->assertHtml($expected, $result);
  6759. }
  6760. /**
  6761. * testFileUploadOnOtherModel method
  6762. *
  6763. * Test File upload input on a model not used in create().
  6764. *
  6765. * @return void
  6766. */
  6767. public function testFileUploadOnOtherModel()
  6768. {
  6769. $this->Form->create($this->article, ['type' => 'file']);
  6770. $result = $this->Form->file('ValidateProfile.city');
  6771. $expected = [
  6772. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6773. ];
  6774. $this->assertHtml($expected, $result);
  6775. }
  6776. /**
  6777. * testButton method
  6778. *
  6779. * Test generation of a form button.
  6780. *
  6781. * @return void
  6782. */
  6783. public function testButton()
  6784. {
  6785. $result = $this->Form->button('Hi');
  6786. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6787. $this->assertHtml($expected, $result);
  6788. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6789. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6790. $this->assertHtml($expected, $result);
  6791. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6792. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6793. $this->assertHtml($expected, $result);
  6794. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6795. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6796. $this->assertHtml($expected, $result);
  6797. $result = $this->Form->button('No type', ['type' => false]);
  6798. $expected = ['button' => [], 'No type', '/button'];
  6799. $this->assertHtml($expected, $result);
  6800. $result = $this->Form->button('Upload Text', [
  6801. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6802. 'escape' => false
  6803. ]);
  6804. $this->assertNotRegExp('/\&039/', $result);
  6805. }
  6806. /**
  6807. * testButtonUnlockedByDefault method
  6808. *
  6809. * Test that button() makes unlocked fields by default.
  6810. *
  6811. * @return void
  6812. */
  6813. public function testButtonUnlockedByDefault()
  6814. {
  6815. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'secured');
  6816. $this->Form->button('Save', ['name' => 'save']);
  6817. $this->Form->button('Clear');
  6818. $result = $this->Form->unlockField();
  6819. $this->assertEquals(['save'], $result);
  6820. }
  6821. /**
  6822. * Test generation of a form button with confirm message.
  6823. *
  6824. * @return void
  6825. */
  6826. public function testButtonWithConfirm()
  6827. {
  6828. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6829. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6830. $this->assertHtml($expected, $result);
  6831. }
  6832. /**
  6833. * testPostButton method
  6834. *
  6835. * @return void
  6836. */
  6837. public function testPostButton()
  6838. {
  6839. $result = $this->Form->postButton('Hi', '/controller/action');
  6840. $expected = [
  6841. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6842. 'div' => ['style' => 'display:none;'],
  6843. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6844. '/div',
  6845. 'button' => ['type' => 'submit'],
  6846. 'Hi',
  6847. '/button',
  6848. '/form'
  6849. ];
  6850. $this->assertHtml($expected, $result);
  6851. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6852. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6853. }
  6854. /**
  6855. * testPostButtonMethodType method
  6856. *
  6857. * @return void
  6858. */
  6859. public function testPostButtonMethodType()
  6860. {
  6861. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6862. $expected = [
  6863. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6864. 'div' => ['style' => 'display:none;'],
  6865. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6866. '/div',
  6867. 'button' => ['type' => 'submit'],
  6868. 'Hi',
  6869. '/button',
  6870. '/form'
  6871. ];
  6872. $this->assertHtml($expected, $result);
  6873. }
  6874. /**
  6875. * testPostButtonFormOptions method
  6876. *
  6877. * @return void
  6878. */
  6879. public function testPostButtonFormOptions()
  6880. {
  6881. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6882. $expected = [
  6883. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6884. 'div' => ['style' => 'display:none;'],
  6885. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6886. '/div',
  6887. 'button' => ['type' => 'submit'],
  6888. 'Hi',
  6889. '/button',
  6890. '/form'
  6891. ];
  6892. $this->assertHtml($expected, $result);
  6893. }
  6894. /**
  6895. * testPostButtonNestedData method
  6896. *
  6897. * Test using postButton with N dimensional data.
  6898. *
  6899. * @return void
  6900. */
  6901. public function testPostButtonNestedData()
  6902. {
  6903. $data = [
  6904. 'one' => [
  6905. 'two' => [
  6906. 3, 4, 5
  6907. ]
  6908. ]
  6909. ];
  6910. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6911. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6912. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6913. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6914. }
  6915. /**
  6916. * testSecurePostButton method
  6917. *
  6918. * Test that postButton adds _Token fields.
  6919. *
  6920. * @return void
  6921. */
  6922. public function testSecurePostButton()
  6923. {
  6924. $this->Form->request = $this->Form->request
  6925. ->withParam('_csrfToken', 'testkey')
  6926. ->withParam('_Token.unlockedFields', []);
  6927. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6928. $tokenDebug = urlencode(json_encode([
  6929. '/posts/delete/1',
  6930. [],
  6931. []
  6932. ]));
  6933. $expected = [
  6934. 'form' => [
  6935. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6936. ],
  6937. ['div' => ['style' => 'display:none;']],
  6938. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6939. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6940. '/div',
  6941. 'button' => ['type' => 'submit'],
  6942. 'Delete',
  6943. '/button',
  6944. ['div' => ['style' => 'display:none;']],
  6945. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6946. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6947. ['input' => [
  6948. 'type' => 'hidden',
  6949. 'name' => '_Token[debug]',
  6950. 'value' => $tokenDebug,
  6951. 'autocomplete' => 'off',
  6952. ]],
  6953. '/div',
  6954. '/form',
  6955. ];
  6956. $this->assertHtml($expected, $result);
  6957. }
  6958. /**
  6959. * testPostLink method
  6960. *
  6961. * @return void
  6962. */
  6963. public function testPostLink()
  6964. {
  6965. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  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' => 'POST'],
  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('Delete', '/posts/delete/1', ['method' => 'delete']);
  6979. $expected = [
  6980. 'form' => [
  6981. 'method' => 'post', 'action' => '/posts/delete/1',
  6982. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6983. ],
  6984. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6985. '/form',
  6986. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6987. 'Delete',
  6988. '/a'
  6989. ];
  6990. $this->assertHtml($expected, $result);
  6991. $result = $this->Form->postLink(
  6992. 'Delete',
  6993. '/posts/delete/1',
  6994. ['target' => '_blank', 'class' => 'btn btn-danger']
  6995. );
  6996. $expected = [
  6997. 'form' => [
  6998. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6999. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7000. ],
  7001. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7002. '/form',
  7003. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7004. 'Delete',
  7005. '/a'
  7006. ];
  7007. $this->assertHtml($expected, $result);
  7008. }
  7009. /**
  7010. * testPostLinkWithConfirm method
  7011. *
  7012. * Test the confirm option for postLink().
  7013. *
  7014. * @return void
  7015. */
  7016. public function testPostLinkWithConfirm()
  7017. {
  7018. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7019. $expected = [
  7020. 'form' => [
  7021. 'method' => 'post', 'action' => '/posts/delete/1',
  7022. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7023. ],
  7024. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7025. '/form',
  7026. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7027. 'Delete',
  7028. '/a'
  7029. ];
  7030. $this->assertHtml($expected, $result);
  7031. $result = $this->Form->postLink(
  7032. 'Delete',
  7033. '/posts/delete/1',
  7034. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7035. );
  7036. $expected = [
  7037. 'form' => [
  7038. 'method' => 'post', 'action' => '/posts/delete/1',
  7039. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7040. ],
  7041. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7042. '/form',
  7043. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7044. 'Delete',
  7045. '/a'
  7046. ];
  7047. $this->assertHtml($expected, $result);
  7048. }
  7049. /**
  7050. * testPostLinkWithQuery method
  7051. *
  7052. * Test postLink() with query string args.
  7053. *
  7054. * @return void
  7055. */
  7056. public function testPostLinkWithQuery()
  7057. {
  7058. $result = $this->Form->postLink(
  7059. 'Delete',
  7060. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7061. );
  7062. $expected = [
  7063. 'form' => [
  7064. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7065. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7066. ],
  7067. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7068. '/form',
  7069. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7070. 'Delete',
  7071. '/a'
  7072. ];
  7073. $this->assertHtml($expected, $result);
  7074. }
  7075. /**
  7076. * testPostLinkWithData method
  7077. *
  7078. * Test postLink with additional data.
  7079. *
  7080. * @return void
  7081. */
  7082. public function testPostLinkWithData()
  7083. {
  7084. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7085. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7086. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7087. $this->Form->create($entity);
  7088. $this->Form->end();
  7089. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7090. $this->assertContains(
  7091. '<input type="hidden" name="name" value="show"',
  7092. $result,
  7093. 'should not contain entity data.'
  7094. );
  7095. }
  7096. /**
  7097. * testPostLinkSecurityHash method
  7098. *
  7099. * Test that security hashes for postLink include the url.
  7100. *
  7101. * @return void
  7102. */
  7103. public function testPostLinkSecurityHash()
  7104. {
  7105. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7106. $hash .= '%3Aid';
  7107. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7108. $result = $this->Form->postLink(
  7109. 'Delete',
  7110. '/posts/delete/1',
  7111. ['data' => ['id' => 1]]
  7112. );
  7113. $tokenDebug = urlencode(json_encode([
  7114. '/posts/delete/1',
  7115. [
  7116. 'id' => 1
  7117. ],
  7118. []
  7119. ]));
  7120. $expected = [
  7121. 'form' => [
  7122. 'method' => 'post', 'action' => '/posts/delete/1',
  7123. 'name', 'style' => 'display:none;'
  7124. ],
  7125. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7126. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7127. 'div' => ['style' => 'display:none;'],
  7128. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7129. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7130. ['input' => [
  7131. 'type' => 'hidden',
  7132. 'name' => '_Token[debug]',
  7133. 'value' => $tokenDebug,
  7134. 'autocomplete' => 'off',
  7135. ]],
  7136. '/div',
  7137. '/form',
  7138. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7139. 'Delete',
  7140. '/a'
  7141. ];
  7142. $this->assertHtml($expected, $result);
  7143. }
  7144. /**
  7145. * testPostLinkSecurityHashBlockMode method
  7146. *
  7147. * Test that postLink doesn't modify the fields in the containing form.
  7148. *
  7149. * postLink() calls inside open forms should not modify the field list
  7150. * for the form.
  7151. *
  7152. * @return void
  7153. */
  7154. public function testPostLinkSecurityHashBlockMode()
  7155. {
  7156. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7157. $hash .= '%3A';
  7158. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7159. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7160. $this->Form->control('title');
  7161. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7162. $result = $this->View->fetch('postLink');
  7163. $this->assertEquals(['title'], $this->Form->fields);
  7164. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7165. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7166. }
  7167. /**
  7168. * testPostLinkSecurityHashNoDebugMode method
  7169. *
  7170. * Test that security does not include debug token if debug is false.
  7171. *
  7172. * @return void
  7173. */
  7174. public function testPostLinkSecurityHashNoDebugMode()
  7175. {
  7176. Configure::write('debug', false);
  7177. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7178. $hash .= '%3Aid';
  7179. $this->Form->request = $this->Form->request
  7180. ->withParam('_Token.key', 'test');
  7181. $result = $this->Form->postLink(
  7182. 'Delete',
  7183. '/posts/delete/1',
  7184. ['data' => ['id' => 1]]
  7185. );
  7186. $expected = [
  7187. 'form' => [
  7188. 'method' => 'post', 'action' => '/posts/delete/1',
  7189. 'name', 'style' => 'display:none;'
  7190. ],
  7191. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7192. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7193. 'div' => ['style' => 'display:none;'],
  7194. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7195. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7196. '/div',
  7197. '/form',
  7198. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7199. 'Delete',
  7200. '/a'
  7201. ];
  7202. $this->assertHtml($expected, $result);
  7203. }
  7204. /**
  7205. * testPostLinkNestedData method
  7206. *
  7207. * Test using postLink with N dimensional data.
  7208. *
  7209. * @return void
  7210. */
  7211. public function testPostLinkNestedData()
  7212. {
  7213. $data = [
  7214. 'one' => [
  7215. 'two' => [
  7216. 3, 4, 5
  7217. ]
  7218. ]
  7219. ];
  7220. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7221. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7222. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7223. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7224. }
  7225. /**
  7226. * testPostLinkAfterGetForm method
  7227. *
  7228. * Test creating postLinks after a GET form.
  7229. *
  7230. * @return void
  7231. */
  7232. public function testPostLinkAfterGetForm()
  7233. {
  7234. $this->Form->request = $this->Form->request
  7235. ->withParam('_csrfToken', 'testkey')
  7236. ->withParam('_Token', 'val');
  7237. $this->Form->create($this->article, ['type' => 'get']);
  7238. $this->Form->end();
  7239. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7240. $tokenDebug = urlencode(json_encode([
  7241. '/posts/delete/1',
  7242. [],
  7243. []
  7244. ]));
  7245. $expected = [
  7246. 'form' => [
  7247. 'method' => 'post', 'action' => '/posts/delete/1',
  7248. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7249. ],
  7250. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7251. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7252. 'div' => ['style' => 'display:none;'],
  7253. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7254. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7255. ['input' => [
  7256. 'type' => 'hidden', 'name' => '_Token[debug]',
  7257. 'value' => $tokenDebug,
  7258. 'autocomplete' => 'off'
  7259. ]],
  7260. '/div',
  7261. '/form',
  7262. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7263. 'Delete',
  7264. '/a'
  7265. ];
  7266. $this->assertHtml($expected, $result);
  7267. }
  7268. /**
  7269. * testPostLinkFormBuffer method
  7270. *
  7271. * Test that postLink adds form tags to view block.
  7272. *
  7273. * @return void
  7274. */
  7275. public function testPostLinkFormBuffer()
  7276. {
  7277. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7278. $expected = [
  7279. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7280. 'Delete',
  7281. '/a'
  7282. ];
  7283. $this->assertHtml($expected, $result);
  7284. $result = $this->View->fetch('postLink');
  7285. $expected = [
  7286. 'form' => [
  7287. 'method' => 'post', 'action' => '/posts/delete/1',
  7288. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7289. ],
  7290. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7291. '/form'
  7292. ];
  7293. $this->assertHtml($expected, $result);
  7294. $result = $this->Form->postLink(
  7295. 'Delete',
  7296. '/posts/delete/2',
  7297. ['block' => true, 'method' => 'DELETE']
  7298. );
  7299. $expected = [
  7300. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7301. 'Delete',
  7302. '/a'
  7303. ];
  7304. $this->assertHtml($expected, $result);
  7305. $result = $this->View->fetch('postLink');
  7306. $expected = [
  7307. 'form' => [
  7308. 'method' => 'post', 'action' => '/posts/delete/1',
  7309. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7310. ],
  7311. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7312. '/form',
  7313. [
  7314. 'form' => [
  7315. 'method' => 'post', 'action' => '/posts/delete/2',
  7316. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7317. ],
  7318. ],
  7319. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7320. '/form'
  7321. ];
  7322. $this->assertHtml($expected, $result);
  7323. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7324. $expected = [
  7325. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7326. 'Delete',
  7327. '/a'
  7328. ];
  7329. $this->assertHtml($expected, $result);
  7330. $result = $this->View->fetch('foobar');
  7331. $expected = [
  7332. 'form' => [
  7333. 'method' => 'post', 'action' => '/posts/delete/1',
  7334. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7335. ],
  7336. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7337. '/form'
  7338. ];
  7339. $this->assertHtml($expected, $result);
  7340. }
  7341. /**
  7342. * testSubmitButton method
  7343. *
  7344. * @return void
  7345. */
  7346. public function testSubmitButton()
  7347. {
  7348. $result = $this->Form->submit('');
  7349. $expected = [
  7350. 'div' => ['class' => 'submit'],
  7351. 'input' => ['type' => 'submit', 'value' => ''],
  7352. '/div'
  7353. ];
  7354. $this->assertHtml($expected, $result);
  7355. $result = $this->Form->submit('Test Submit');
  7356. $expected = [
  7357. 'div' => ['class' => 'submit'],
  7358. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7359. '/div'
  7360. ];
  7361. $this->assertHtml($expected, $result);
  7362. $result = $this->Form->submit('Next >');
  7363. $expected = [
  7364. 'div' => ['class' => 'submit'],
  7365. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7366. '/div'
  7367. ];
  7368. $this->assertHtml($expected, $result);
  7369. $result = $this->Form->submit('Next >', ['escape' => false]);
  7370. $expected = [
  7371. 'div' => ['class' => 'submit'],
  7372. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7373. '/div'
  7374. ];
  7375. $this->assertHtml($expected, $result);
  7376. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7377. $expected = [
  7378. 'div' => ['class' => 'submit'],
  7379. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7380. '/div'
  7381. ];
  7382. $this->assertHtml($expected, $result);
  7383. }
  7384. /**
  7385. * testSubmitImage method
  7386. *
  7387. * Test image submit types.
  7388. *
  7389. * @return void
  7390. */
  7391. public function testSubmitImage()
  7392. {
  7393. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7394. $expected = [
  7395. 'div' => ['class' => 'submit'],
  7396. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7397. '/div'
  7398. ];
  7399. $this->assertHtml($expected, $result);
  7400. $result = $this->Form->submit('/relative/cake.power.gif');
  7401. $expected = [
  7402. 'div' => ['class' => 'submit'],
  7403. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7404. '/div'
  7405. ];
  7406. $this->assertHtml($expected, $result);
  7407. $result = $this->Form->submit('cake.power.gif');
  7408. $expected = [
  7409. 'div' => ['class' => 'submit'],
  7410. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7411. '/div'
  7412. ];
  7413. $this->assertHtml($expected, $result);
  7414. $result = $this->Form->submit('Not.an.image');
  7415. $expected = [
  7416. 'div' => ['class' => 'submit'],
  7417. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7418. '/div'
  7419. ];
  7420. $this->assertHtml($expected, $result);
  7421. }
  7422. /**
  7423. * testSubmitUnlockedByDefault method
  7424. *
  7425. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7426. * be submitted at a time.
  7427. *
  7428. * @return void
  7429. */
  7430. public function testSubmitUnlockedByDefault()
  7431. {
  7432. $this->Form->request = $this->Form->request->withParam('_Token', 'secured');
  7433. $this->Form->submit('Go go');
  7434. $this->Form->submit('Save', ['name' => 'save']);
  7435. $result = $this->Form->unlockField();
  7436. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7437. }
  7438. /**
  7439. * testSubmitImageTimestamp method
  7440. *
  7441. * Test submit image with timestamps.
  7442. *
  7443. * @return void
  7444. */
  7445. public function testSubmitImageTimestamp()
  7446. {
  7447. Configure::write('Asset.timestamp', 'force');
  7448. $result = $this->Form->submit('cake.power.gif');
  7449. $expected = [
  7450. 'div' => ['class' => 'submit'],
  7451. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7452. '/div'
  7453. ];
  7454. $this->assertHtml($expected, $result);
  7455. }
  7456. /**
  7457. * testDateTimeWithGetForms method
  7458. *
  7459. * Test that datetime() works with GET style forms.
  7460. *
  7461. * @return void
  7462. */
  7463. public function testDateTimeWithGetForms()
  7464. {
  7465. extract($this->dateRegex);
  7466. $this->Form->create($this->article, ['type' => 'get']);
  7467. $result = $this->Form->datetime('created');
  7468. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7469. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7470. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7471. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7472. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7473. }
  7474. /**
  7475. * testForMagicControlNonExistingNorValidated method
  7476. *
  7477. * @return void
  7478. */
  7479. public function testForMagicControlNonExistingNorValidated()
  7480. {
  7481. $this->Form->create($this->article);
  7482. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7483. $result = $this->Form->control('non_existing_nor_validated');
  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. 'id' => 'non-existing-nor-validated'
  7491. ]
  7492. ];
  7493. $this->assertHtml($expected, $result);
  7494. $result = $this->Form->control('non_existing_nor_validated', [
  7495. 'val' => 'my value'
  7496. ]);
  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' => 'my value', 'id' => 'non-existing-nor-validated'
  7504. ]
  7505. ];
  7506. $this->assertHtml($expected, $result);
  7507. $this->Form->request = $this->Form->request->withData('non_existing_nor_validated', 'CakePHP magic');
  7508. $this->Form->create($this->article);
  7509. $result = $this->Form->control('non_existing_nor_validated');
  7510. $expected = [
  7511. 'label' => ['for' => 'non-existing-nor-validated'],
  7512. 'Non Existing Nor Validated',
  7513. '/label',
  7514. 'input' => [
  7515. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7516. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7517. ]
  7518. ];
  7519. $this->assertHtml($expected, $result);
  7520. }
  7521. /**
  7522. * testFormMagicControlLabel method
  7523. *
  7524. * @return void
  7525. */
  7526. public function testFormMagicControlLabel()
  7527. {
  7528. TableRegistry::get('Contacts', [
  7529. 'className' => __NAMESPACE__ . '\ContactsTable'
  7530. ]);
  7531. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7532. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7533. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7534. $expected = [
  7535. 'label' => ['for' => 'contacts-name'],
  7536. 'My label',
  7537. '/label',
  7538. 'input' => [
  7539. 'type' => 'text', 'name' => 'Contacts[name]',
  7540. 'id' => 'contacts-name', 'maxlength' => '255'
  7541. ]
  7542. ];
  7543. $this->assertHtml($expected, $result);
  7544. $result = $this->Form->control('name', [
  7545. 'label' => ['class' => 'mandatory']
  7546. ]);
  7547. $expected = [
  7548. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7549. 'Name',
  7550. '/label',
  7551. 'input' => [
  7552. 'type' => 'text', 'name' => 'name',
  7553. 'id' => 'name', 'maxlength' => '255'
  7554. ]
  7555. ];
  7556. $this->assertHtml($expected, $result);
  7557. $result = $this->Form->control('name', [
  7558. 'div' => false,
  7559. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7560. ]);
  7561. $expected = [
  7562. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7563. 'My label',
  7564. '/label',
  7565. 'input' => [
  7566. 'type' => 'text', 'name' => 'name',
  7567. 'id' => 'name', 'maxlength' => '255'
  7568. ]
  7569. ];
  7570. $this->assertHtml($expected, $result);
  7571. $result = $this->Form->control('Contact.name', [
  7572. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7573. ]);
  7574. $expected = [
  7575. 'label' => ['for' => 'my_id'],
  7576. 'Name',
  7577. '/label',
  7578. 'input' => [
  7579. 'type' => 'text', 'name' => 'Contact[name]',
  7580. 'id' => 'my_id', 'maxlength' => '255'
  7581. ]
  7582. ];
  7583. $this->assertHtml($expected, $result);
  7584. $result = $this->Form->control('1.id');
  7585. $expected = ['input' => [
  7586. 'type' => 'hidden', 'name' => '1[id]',
  7587. 'id' => '1-id'
  7588. ]];
  7589. $this->assertHtml($expected, $result);
  7590. $result = $this->Form->control('1.name');
  7591. $expected = [
  7592. 'label' => ['for' => '1-name'],
  7593. 'Name',
  7594. '/label',
  7595. 'input' => [
  7596. 'type' => 'text', 'name' => '1[name]',
  7597. 'id' => '1-name', 'maxlength' => '255'
  7598. ]
  7599. ];
  7600. $this->assertHtml($expected, $result);
  7601. }
  7602. /**
  7603. * testFormEnd method
  7604. *
  7605. * @return void
  7606. */
  7607. public function testFormEnd()
  7608. {
  7609. $this->assertEquals('</form>', $this->Form->end());
  7610. }
  7611. /**
  7612. * testMultiRecordForm method
  7613. *
  7614. * Test the generation of fields for a multi record form.
  7615. *
  7616. * @return void
  7617. */
  7618. public function testMultiRecordForm()
  7619. {
  7620. $this->loadFixtures('Articles', 'Comments');
  7621. $articles = TableRegistry::get('Articles');
  7622. $articles->hasMany('Comments');
  7623. $comment = new Entity(['comment' => 'Value']);
  7624. $article = new Article(['comments' => [$comment]]);
  7625. $this->Form->create([$article]);
  7626. $result = $this->Form->control('0.comments.1.comment');
  7627. //@codingStandardsIgnoreStart
  7628. $expected = [
  7629. 'div' => ['class' => 'input textarea'],
  7630. 'label' => ['for' => '0-comments-1-comment'],
  7631. 'Comment',
  7632. '/label',
  7633. 'textarea' => [
  7634. 'name',
  7635. 'id' => '0-comments-1-comment',
  7636. 'rows' => 5
  7637. ],
  7638. '/textarea',
  7639. '/div'
  7640. ];
  7641. //@codingStandardsIgnoreEnd
  7642. $this->assertHtml($expected, $result);
  7643. $result = $this->Form->control('0.comments.0.comment');
  7644. //@codingStandardsIgnoreStart
  7645. $expected = [
  7646. 'div' => ['class' => 'input textarea'],
  7647. 'label' => ['for' => '0-comments-0-comment'],
  7648. 'Comment',
  7649. '/label',
  7650. 'textarea' => [
  7651. 'name',
  7652. 'id' => '0-comments-0-comment',
  7653. 'rows' => 5
  7654. ],
  7655. 'Value',
  7656. '/textarea',
  7657. '/div'
  7658. ];
  7659. //@codingStandardsIgnoreEnd
  7660. $this->assertHtml($expected, $result);
  7661. $comment->setError('comment', ['Not valid']);
  7662. $result = $this->Form->control('0.comments.0.comment');
  7663. //@codingStandardsIgnoreStart
  7664. $expected = [
  7665. 'div' => ['class' => 'input textarea error'],
  7666. 'label' => ['for' => '0-comments-0-comment'],
  7667. 'Comment',
  7668. '/label',
  7669. 'textarea' => [
  7670. 'name',
  7671. 'class' => 'form-error',
  7672. 'id' => '0-comments-0-comment',
  7673. 'rows' => 5
  7674. ],
  7675. 'Value',
  7676. '/textarea',
  7677. ['div' => ['class' => 'error-message']],
  7678. 'Not valid',
  7679. '/div',
  7680. '/div'
  7681. ];
  7682. //@codingStandardsIgnoreEnd
  7683. $this->assertHtml($expected, $result);
  7684. TableRegistry::get('Comments')
  7685. ->getValidator('default')
  7686. ->allowEmpty('comment', false);
  7687. $result = $this->Form->control('0.comments.1.comment');
  7688. //@codingStandardsIgnoreStart
  7689. $expected = [
  7690. 'div' => ['class' => 'input textarea required'],
  7691. 'label' => ['for' => '0-comments-1-comment'],
  7692. 'Comment',
  7693. '/label',
  7694. 'textarea' => [
  7695. 'name',
  7696. 'required' => 'required',
  7697. 'id' => '0-comments-1-comment',
  7698. 'rows' => 5
  7699. ],
  7700. '/textarea',
  7701. '/div'
  7702. ];
  7703. //@codingStandardsIgnoreEnd
  7704. $this->assertHtml($expected, $result);
  7705. }
  7706. /**
  7707. * testHtml5Controls method
  7708. *
  7709. * Test that some html5 inputs + FormHelper::__call() work.
  7710. *
  7711. * @return void
  7712. */
  7713. public function testHtml5Controls()
  7714. {
  7715. $result = $this->Form->email('User.email');
  7716. $expected = [
  7717. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7718. ];
  7719. $this->assertHtml($expected, $result);
  7720. $result = $this->Form->search('User.query');
  7721. $expected = [
  7722. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7723. ];
  7724. $this->assertHtml($expected, $result);
  7725. $result = $this->Form->search('User.query', ['value' => 'test']);
  7726. $expected = [
  7727. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7728. ];
  7729. $this->assertHtml($expected, $result);
  7730. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7731. $expected = [
  7732. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7733. ];
  7734. $this->assertHtml($expected, $result);
  7735. }
  7736. /**
  7737. * testHtml5ControlWithControl method
  7738. *
  7739. * Test accessing html5 inputs through control().
  7740. *
  7741. * @return void
  7742. */
  7743. public function testHtml5ControlWithControl()
  7744. {
  7745. $this->Form->create();
  7746. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7747. $result = $this->Form->control('website', [
  7748. 'type' => 'url',
  7749. 'val' => 'http://domain.tld',
  7750. 'label' => false
  7751. ]);
  7752. $expected = [
  7753. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7754. ];
  7755. $this->assertHtml($expected, $result);
  7756. }
  7757. /**
  7758. * testHtml5ControlException method
  7759. *
  7760. * Test errors when field name is missing.
  7761. *
  7762. * @return void
  7763. */
  7764. public function testHtml5ControlException()
  7765. {
  7766. $this->expectException(\Cake\Core\Exception\Exception::class);
  7767. $this->Form->email();
  7768. }
  7769. /**
  7770. * testRequiredAttribute method
  7771. *
  7772. * Tests that formhelper sets required attributes.
  7773. *
  7774. * @return void
  7775. */
  7776. public function testRequiredAttribute()
  7777. {
  7778. $this->article['required'] = [
  7779. 'title' => true,
  7780. 'body' => false,
  7781. ];
  7782. $this->Form->create($this->article);
  7783. $result = $this->Form->control('title');
  7784. $expected = [
  7785. 'div' => ['class' => 'input text required'],
  7786. 'label' => ['for' => 'title'],
  7787. 'Title',
  7788. '/label',
  7789. 'input' => [
  7790. 'type' => 'text',
  7791. 'name' => 'title',
  7792. 'id' => 'title',
  7793. 'required' => 'required',
  7794. ],
  7795. '/div'
  7796. ];
  7797. $this->assertHtml($expected, $result);
  7798. $result = $this->Form->control('title', ['required' => false]);
  7799. $this->assertNotContains('required', $result);
  7800. $result = $this->Form->control('body');
  7801. $expected = [
  7802. 'div' => ['class' => 'input text'],
  7803. 'label' => ['for' => 'body'],
  7804. 'Body',
  7805. '/label',
  7806. 'input' => [
  7807. 'type' => 'text',
  7808. 'name' => 'body',
  7809. 'id' => 'body',
  7810. ],
  7811. '/div'
  7812. ];
  7813. $this->assertHtml($expected, $result);
  7814. $result = $this->Form->control('body', ['required' => true]);
  7815. $this->assertContains('required', $result);
  7816. }
  7817. /**
  7818. * testControlsNotNested method
  7819. *
  7820. * Tests that it is possible to put inputs outside of the label.
  7821. *
  7822. * @return void
  7823. */
  7824. public function testControlsNotNested()
  7825. {
  7826. $this->Form->setTemplates([
  7827. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7828. 'formGroup' => '{{input}}{{label}}',
  7829. ]);
  7830. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7831. $expected = [
  7832. 'div' => ['class' => 'input checkbox'],
  7833. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7834. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7835. 'label' => ['for' => 'foo'],
  7836. 'Foo',
  7837. '/label',
  7838. '/div'
  7839. ];
  7840. $this->assertHtml($expected, $result);
  7841. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7842. $expected = [
  7843. 'div' => ['class' => 'input checkbox'],
  7844. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7845. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7846. '/div'
  7847. ];
  7848. $this->assertHtml($expected, $result);
  7849. $result = $this->Form->control('confirm', [
  7850. 'type' => 'radio',
  7851. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7852. ]);
  7853. $expected = [
  7854. 'div' => ['class' => 'input radio'],
  7855. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7856. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7857. ['label' => ['for' => 'confirm-y']],
  7858. 'Yes',
  7859. '/label',
  7860. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7861. ['label' => ['for' => 'confirm-n']],
  7862. 'No',
  7863. '/label',
  7864. '<label',
  7865. 'Confirm',
  7866. '/label',
  7867. '/div',
  7868. ];
  7869. $this->assertHtml($expected, $result);
  7870. $result = $this->Form->select('category', ['1', '2'], [
  7871. 'multiple' => 'checkbox',
  7872. 'name' => 'fish',
  7873. ]);
  7874. $expected = [
  7875. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7876. ['div' => ['class' => 'checkbox']],
  7877. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7878. ['label' => ['for' => 'fish-0']],
  7879. '1',
  7880. '/label',
  7881. '/div',
  7882. ['div' => ['class' => 'checkbox']],
  7883. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7884. ['label' => ['for' => 'fish-1']],
  7885. '2',
  7886. '/label',
  7887. '/div'
  7888. ];
  7889. $this->assertHtml($expected, $result);
  7890. }
  7891. /**
  7892. * testControlContainerTemplates method
  7893. *
  7894. * Test that *Container templates are used by input.
  7895. *
  7896. * @return void
  7897. */
  7898. public function testControlContainerTemplates()
  7899. {
  7900. $this->Form->setTemplates([
  7901. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7902. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7903. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7904. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7905. ]);
  7906. $this->article['errors'] = [
  7907. 'Article' => ['published' => 'error message']
  7908. ];
  7909. $this->Form->create($this->article);
  7910. $result = $this->Form->control('accept', [
  7911. 'type' => 'checkbox'
  7912. ]);
  7913. $expected = [
  7914. 'div' => ['class' => 'check'],
  7915. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7916. 'label' => ['for' => 'accept'],
  7917. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7918. 'Accept',
  7919. '/label',
  7920. '/div'
  7921. ];
  7922. $this->assertHtml($expected, $result);
  7923. $result = $this->Form->control('accept', [
  7924. 'type' => 'radio',
  7925. 'options' => ['Y', 'N']
  7926. ]);
  7927. $this->assertContains('<div class="rad">', $result);
  7928. $result = $this->Form->control('Article.published', [
  7929. 'type' => 'radio',
  7930. 'options' => ['Y', 'N']
  7931. ]);
  7932. $this->assertContains('<div class="rad err">', $result);
  7933. $result = $this->Form->control('Article.created', [
  7934. 'type' => 'datetime',
  7935. ]);
  7936. $this->assertContains('<div class="dt">', $result);
  7937. }
  7938. /**
  7939. * testFormGroupTemplates method
  7940. *
  7941. * Test that *Container templates are used by input.
  7942. *
  7943. * @return void
  7944. */
  7945. public function testFormGroupTemplates()
  7946. {
  7947. $this->Form->setTemplates([
  7948. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7949. ]);
  7950. $this->Form->create($this->article);
  7951. $result = $this->Form->control('accept', [
  7952. 'type' => 'radio',
  7953. 'options' => ['Y', 'N']
  7954. ]);
  7955. $this->assertContains('<div class="radio">', $result);
  7956. }
  7957. /**
  7958. * testResetTemplates method
  7959. *
  7960. * Test resetting templates.
  7961. *
  7962. * @return void
  7963. */
  7964. public function testResetTemplates()
  7965. {
  7966. $this->Form->setTemplates(['input' => '<input/>']);
  7967. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7968. $this->Form->resetTemplates();
  7969. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7970. }
  7971. /**
  7972. * testContext method
  7973. *
  7974. * Test the context method.
  7975. *
  7976. * @return void
  7977. */
  7978. public function testContext()
  7979. {
  7980. $result = $this->Form->context();
  7981. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7982. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7983. $this->assertSame($mock, $this->Form->context($mock));
  7984. $this->assertSame($mock, $this->Form->context());
  7985. }
  7986. /**
  7987. * testAutoDomId method
  7988. *
  7989. * @return void
  7990. */
  7991. public function testAutoDomId()
  7992. {
  7993. $result = $this->Form->text('field', ['id' => true]);
  7994. $expected = [
  7995. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7996. ];
  7997. $this->assertHtml($expected, $result);
  7998. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7999. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8000. $expected = [
  8001. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8002. ['label' => ['for' => 'field-0']],
  8003. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8004. 'option A',
  8005. '/label',
  8006. ['label' => ['for' => 'field-1']],
  8007. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8008. 'option B',
  8009. '/label',
  8010. ];
  8011. $this->assertHtml($expected, $result);
  8012. $result = $this->Form->select(
  8013. 'multi_field',
  8014. ['first', 'second'],
  8015. ['multiple' => 'checkbox', 'id' => true]
  8016. );
  8017. $expected = [
  8018. 'input' => [
  8019. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8020. ],
  8021. ['div' => ['class' => 'checkbox']],
  8022. ['label' => ['for' => 'multi-field-0']],
  8023. ['input' => [
  8024. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8025. 'value' => '0', 'id' => 'multi-field-0'
  8026. ]],
  8027. 'first',
  8028. '/label',
  8029. '/div',
  8030. ['div' => ['class' => 'checkbox']],
  8031. ['label' => ['for' => 'multi-field-1']],
  8032. ['input' => [
  8033. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8034. 'value' => '1', 'id' => 'multi-field-1'
  8035. ]],
  8036. 'second',
  8037. '/label',
  8038. '/div',
  8039. ];
  8040. $this->assertHtml($expected, $result);
  8041. }
  8042. /**
  8043. * Test the basic setters and getters for value sources
  8044. *
  8045. * @return void
  8046. */
  8047. public function testFormValueSourcesSettersGetters()
  8048. {
  8049. $this->Form->request = $this->Form->request
  8050. ->withData('id', '1')
  8051. ->withQueryParams(['id' => '2']);
  8052. $expected = ['context'];
  8053. $result = $this->Form->getValueSources();
  8054. $this->assertEquals($expected, $result);
  8055. $expected = ['query', 'data', 'context'];
  8056. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8057. $result = $this->Form->getValueSources();
  8058. $this->assertEquals($expected, $result);
  8059. $this->Form->setValueSources(['context']);
  8060. $expected = '1';
  8061. $result = $this->Form->getSourceValue('id');
  8062. $this->assertEquals($expected, $result);
  8063. $this->Form->setValueSources('query');
  8064. $expected = ['query'];
  8065. $result = $this->Form->getValueSources();
  8066. $this->assertEquals($expected, $result);
  8067. $expected = '2';
  8068. $result = $this->Form->getSourceValue('id');
  8069. $this->assertEquals($expected, $result);
  8070. $this->Form->setValueSources(['data']);
  8071. $expected = '1';
  8072. $result = $this->Form->getSourceValue('id');
  8073. $this->assertEquals($expected, $result);
  8074. $this->Form->setValueSources(['query', 'data']);
  8075. $expected = '2';
  8076. $result = $this->Form->getSourceValue('id');
  8077. $this->assertEquals($expected, $result);
  8078. }
  8079. /**
  8080. * Tests the different input rendering values based on sources values switching
  8081. *
  8082. * @return void
  8083. */
  8084. public function testFormValueSourcesSingleSwitchRendering()
  8085. {
  8086. $this->loadFixtures('Articles');
  8087. $articles = TableRegistry::get('Articles');
  8088. $article = new Article();
  8089. $articles->patchEntity($article, ['id' => '3']);
  8090. $this->Form->create($article);
  8091. $this->Form->setValueSources(['context']);
  8092. $result = $this->Form->control('id');
  8093. $expected = [
  8094. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8095. ];
  8096. $this->assertHtml($expected, $result);
  8097. $this->Form->request = $this->Form->request->withQueryParams(['id' => 5]);
  8098. $this->Form->setValueSources(['query']);
  8099. $result = $this->Form->control('id');
  8100. $expected = [
  8101. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8102. ];
  8103. $this->assertHtml($expected, $result);
  8104. $this->Form->request = $this->Form->request
  8105. ->withQueryParams(['id' => '5a'])
  8106. ->withData('id', '5b');
  8107. $this->Form->setValueSources(['context']);
  8108. $this->Form->create($article);
  8109. $result = $this->Form->control('id');
  8110. $expected = [
  8111. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8112. ];
  8113. $this->assertHtml($expected, $result);
  8114. $this->Form->setValueSources(['data']);
  8115. $this->Form->create($article);
  8116. $result = $this->Form->control('id');
  8117. $expected = [
  8118. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8119. ];
  8120. $this->assertHtml($expected, $result);
  8121. $this->Form->setValueSources(['query']);
  8122. $result = $this->Form->control('id');
  8123. $expected = [
  8124. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8125. ];
  8126. $this->assertHtml($expected, $result);
  8127. }
  8128. /**
  8129. * Tests the different input rendering values based on sources values switching while supplying
  8130. * an entity (base context) and multiple sources (such as data, query)
  8131. *
  8132. * @return void
  8133. */
  8134. public function testFormValueSourcesListSwitchRendering()
  8135. {
  8136. $this->loadFixtures('Articles');
  8137. $articles = TableRegistry::get('Articles');
  8138. $article = new Article();
  8139. $articles->patchEntity($article, ['id' => '3']);
  8140. $this->Form->request = $this->Form->request->withQueryParams(['id' => '9']);
  8141. $this->Form->create($article);
  8142. $this->Form->setValueSources(['context', 'query']);
  8143. $result = $this->Form->control('id');
  8144. $expected = [
  8145. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8146. ];
  8147. $this->assertHtml($expected, $result);
  8148. $this->Form->setValueSources(['query', 'context']);
  8149. $result = $this->Form->control('id');
  8150. $expected = [
  8151. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8152. ];
  8153. $this->assertHtml($expected, $result);
  8154. $this->Form->setValueSources(['data', 'query', 'context']);
  8155. $result = $this->Form->control('id');
  8156. $expected = [
  8157. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8158. ];
  8159. $this->assertHtml($expected, $result);
  8160. $this->Form->request = $this->Form->request
  8161. ->withData('id', '8')
  8162. ->withQueryParams(['id' => '9']);
  8163. $this->Form->setValueSources(['data', 'query', 'context']);
  8164. $result = $this->Form->control('id');
  8165. $expected = [
  8166. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8167. ];
  8168. $this->assertHtml($expected, $result);
  8169. }
  8170. /**
  8171. * Test the different form input renderings based on values sources switchings through form options
  8172. *
  8173. * @return void
  8174. */
  8175. public function testFormValueSourcesSwitchViaOptionsRendering()
  8176. {
  8177. $this->loadFixtures('Articles');
  8178. $articles = TableRegistry::get('Articles');
  8179. $article = new Article();
  8180. $articles->patchEntity($article, ['id' => '3']);
  8181. $this->Form->request = $this->Form->request->withData('id', 4)->withQueryParams(['id' => 5]);
  8182. $this->Form->create($article, ['valueSources' => 'query']);
  8183. $result = $this->Form->control('id');
  8184. $expected = [
  8185. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8186. ];
  8187. $this->assertHtml($expected, $result);
  8188. $result = $this->Form->getSourceValue('id');
  8189. $this->assertEquals('5', $result);
  8190. $this->Form->setValueSources(['context']);
  8191. $this->Form->create($article, ['valueSources' => 'query']);
  8192. $result = $this->Form->control('id');
  8193. $expected = [
  8194. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8195. ];
  8196. $this->assertHtml($expected, $result);
  8197. $result = $this->Form->getSourceValue('id');
  8198. $this->assertEquals('5', $result);
  8199. $this->Form->setValueSources(['query']);
  8200. $this->Form->create($article, ['valueSources' => 'data']);
  8201. $result = $this->Form->control('id');
  8202. $expected = [
  8203. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8204. ];
  8205. $this->assertHtml($expected, $result);
  8206. $result = $this->Form->getSourceValue('id');
  8207. $this->assertEquals('4', $result);
  8208. $this->Form->setValueSources(['query']);
  8209. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8210. $result = $this->Form->control('id');
  8211. $expected = [
  8212. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8213. ];
  8214. $this->assertHtml($expected, $result);
  8215. $result = $this->Form->getSourceValue('id');
  8216. $this->assertEquals('4', $result);
  8217. }
  8218. /**
  8219. * Test the different form input renderings based on values sources switchings through form options
  8220. *
  8221. * @return void
  8222. */
  8223. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8224. {
  8225. $this->loadFixtures('Articles');
  8226. $articles = TableRegistry::get('Articles');
  8227. $article = new Article();
  8228. $articles->patchEntity($article, ['id' => '3']);
  8229. $this->Form->request = $this->Form->request->withData('id', 10)->withQueryParams(['id' => 11]);
  8230. $this->Form->setValueSources(['context'])
  8231. ->create($article, ['valueSources' => ['query', 'data']]);
  8232. $result = $this->Form->control('id');
  8233. $expected = [
  8234. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8235. ];
  8236. $this->assertHtml($expected, $result);
  8237. $result = $this->Form->getSourceValue('id');
  8238. $this->assertEquals('11', $result);
  8239. $this->Form->request = $this->Form->request->withQueryParams([]);
  8240. $this->Form->setValueSources(['context'])
  8241. ->create($article, ['valueSources' => ['query', 'data']]);
  8242. $result = $this->Form->control('id');
  8243. $expected = [
  8244. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8245. ];
  8246. $this->assertHtml($expected, $result);
  8247. $result = $this->Form->getSourceValue('id');
  8248. $this->assertEquals('10', $result);
  8249. }
  8250. /**
  8251. * Test the different form values sources resetting through From::end();
  8252. *
  8253. * @return void
  8254. */
  8255. public function testFormValueSourcesResetViaEnd()
  8256. {
  8257. $expected = ['context'];
  8258. $result = $this->Form->getValueSources();
  8259. $this->assertEquals($expected, $result);
  8260. $expected = ['query', 'context', 'data'];
  8261. $this->Form->setValueSources(['query', 'context', 'data']);
  8262. $result = $this->Form->getValueSources();
  8263. $this->assertEquals($expected, $result);
  8264. $this->Form->create();
  8265. $result = $this->Form->getValueSources();
  8266. $this->assertEquals($expected, $result);
  8267. $this->Form->end();
  8268. $result = $this->Form->getValueSources();
  8269. $this->assertEquals(['context'], $result);
  8270. }
  8271. /**
  8272. * Test sources values defaults handling
  8273. *
  8274. * @return void
  8275. */
  8276. public function testFormValueSourcesDefaults()
  8277. {
  8278. $this->Form->request = $this->Form->request->withQueryParams(['password' => 'open Sesame']);
  8279. $this->Form->create();
  8280. $result = $this->Form->password('password');
  8281. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8282. $this->assertHtml($expected, $result);
  8283. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8284. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8285. $this->assertHtml($expected, $result);
  8286. $this->Form->setValueSources('query');
  8287. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8288. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8289. $this->assertHtml($expected, $result);
  8290. $this->Form->setValueSources('data');
  8291. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8292. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8293. $this->assertHtml($expected, $result);
  8294. }
  8295. /**
  8296. * Test sources values schema defaults handling
  8297. *
  8298. * @return void
  8299. */
  8300. public function testSourcesValueDoesntExistPassThrough()
  8301. {
  8302. $this->Form->request = $this->Form->request->withQueryParams(['category' => 'sesame-cookies']);
  8303. $articles = TableRegistry::get('Articles');
  8304. $entity = $articles->newEntity();
  8305. $this->Form->create($entity);
  8306. $this->Form->setValueSources(['query', 'context']);
  8307. $result = $this->Form->getSourceValue('category');
  8308. $this->assertEquals('sesame-cookies', $result);
  8309. $this->Form->setValueSources(['context', 'query']);
  8310. $result = $this->Form->getSourceValue('category');
  8311. $this->assertEquals('sesame-cookies', $result);
  8312. }
  8313. /**
  8314. * testNestedLabelInput method
  8315. *
  8316. * Test the `nestedInput` parameter
  8317. *
  8318. * @group deprecated
  8319. * @return void
  8320. */
  8321. public function testNestedLabelInput()
  8322. {
  8323. $this->deprecated(function () {
  8324. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8325. $expected = [
  8326. 'div' => ['class' => 'input text'],
  8327. 'label' => ['for' => 'foo'],
  8328. ['input' => [
  8329. 'type' => 'text',
  8330. 'name' => 'foo',
  8331. 'id' => 'foo'
  8332. ]],
  8333. 'Foo',
  8334. '/label',
  8335. '/div'
  8336. ];
  8337. $this->assertHtml($expected, $result);
  8338. });
  8339. }
  8340. /**
  8341. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8342. *
  8343. * This test makes sure `false` excludes the label from the render
  8344. *
  8345. * @return void
  8346. */
  8347. public function testControlLabelManipulationDisableLabels()
  8348. {
  8349. $result = $this->Form->control('test', [
  8350. 'type' => 'radio',
  8351. 'options' => ['A', 'B'],
  8352. 'labelOptions' => false
  8353. ]);
  8354. $expected = [
  8355. ['div' => ['class' => 'input radio']],
  8356. '<label',
  8357. 'Test',
  8358. '/label',
  8359. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8360. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8361. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8362. '/div',
  8363. ];
  8364. $this->assertHtml($expected, $result);
  8365. $result = $this->Form->control('checkbox1', [
  8366. 'label' => 'My checkboxes',
  8367. 'multiple' => 'checkbox',
  8368. 'type' => 'select',
  8369. 'options' => [
  8370. ['text' => 'First Checkbox', 'value' => 1],
  8371. ['text' => 'Second Checkbox', 'value' => 2]
  8372. ],
  8373. 'labelOptions' => false
  8374. ]);
  8375. $expected = [
  8376. ['div' => ['class' => 'input select']],
  8377. ['label' => ['for' => 'checkbox1']],
  8378. 'My checkboxes',
  8379. '/label',
  8380. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8381. ['div' => ['class' => 'checkbox']],
  8382. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8383. '/div',
  8384. ['div' => ['class' => 'checkbox']],
  8385. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8386. '/div',
  8387. '/div'
  8388. ];
  8389. $this->assertHtml($expected, $result);
  8390. }
  8391. /**
  8392. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8393. *
  8394. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8395. * added to the class if checked.
  8396. *
  8397. * Also checks to make sure any custom attributes are rendered correctly
  8398. *
  8399. * @return void
  8400. */
  8401. public function testControlLabelManipulationRadios()
  8402. {
  8403. $result = $this->Form->control('test', [
  8404. 'type' => 'radio',
  8405. 'options' => ['A', 'B'],
  8406. 'labelOptions' => ['class' => 'custom-class']
  8407. ]);
  8408. $expected = [
  8409. ['div' => ['class' => 'input radio']],
  8410. '<label',
  8411. 'Test',
  8412. '/label',
  8413. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8414. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8415. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8416. 'A',
  8417. '/label',
  8418. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8419. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8420. 'B',
  8421. '/label',
  8422. '/div',
  8423. ];
  8424. $this->assertHtml($expected, $result);
  8425. $result = $this->Form->control('test', [
  8426. 'type' => 'radio',
  8427. 'options' => ['A', 'B'],
  8428. 'value' => 1,
  8429. 'labelOptions' => ['class' => 'custom-class']
  8430. ]);
  8431. $expected = [
  8432. ['div' => ['class' => 'input radio']],
  8433. '<label',
  8434. 'Test',
  8435. '/label',
  8436. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8437. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8438. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8439. 'A',
  8440. '/label',
  8441. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8442. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8443. 'B',
  8444. '/label',
  8445. '/div',
  8446. ];
  8447. $this->assertHtml($expected, $result);
  8448. $result = $this->Form->control('test', [
  8449. 'type' => 'radio',
  8450. 'options' => ['A', 'B'],
  8451. 'value' => 1,
  8452. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8453. ]);
  8454. $expected = [
  8455. ['div' => ['class' => 'input radio']],
  8456. '<label',
  8457. 'Test',
  8458. '/label',
  8459. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8460. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8461. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8462. 'A',
  8463. '/label',
  8464. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8465. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8466. 'B',
  8467. '/label',
  8468. '/div',
  8469. ];
  8470. $this->assertHtml($expected, $result);
  8471. $result = $this->Form->radio('test', ['A', 'B'], [
  8472. 'label' => [
  8473. 'class' => ['custom-class', 'another-class'],
  8474. 'data-name' => 'bob'
  8475. ],
  8476. 'value' => 1
  8477. ]);
  8478. $expected = [
  8479. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8480. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8481. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8482. 'A',
  8483. '/label',
  8484. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8485. ['input' => [
  8486. 'type' => 'radio',
  8487. 'name' => 'test',
  8488. 'value' => '1',
  8489. 'id' => 'test-1',
  8490. 'checked' => 'checked'
  8491. ]],
  8492. 'B',
  8493. '/label',
  8494. ];
  8495. $this->assertHtml($expected, $result);
  8496. }
  8497. /**
  8498. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8499. *
  8500. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8501. * added to the class if checked.
  8502. *
  8503. * Also checks to make sure any custom attributes are rendered correctly
  8504. *
  8505. * @return void
  8506. */
  8507. public function testControlLabelManipulationCheckboxes()
  8508. {
  8509. $result = $this->Form->control('checkbox1', [
  8510. 'label' => 'My checkboxes',
  8511. 'multiple' => 'checkbox',
  8512. 'type' => 'select',
  8513. 'options' => [
  8514. ['text' => 'First Checkbox', 'value' => 1],
  8515. ['text' => 'Second Checkbox', 'value' => 2]
  8516. ],
  8517. 'labelOptions' => ['class' => 'custom-class'],
  8518. 'value' => ['1']
  8519. ]);
  8520. $expected = [
  8521. ['div' => ['class' => 'input select']],
  8522. ['label' => ['for' => 'checkbox1']],
  8523. 'My checkboxes',
  8524. '/label',
  8525. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8526. ['div' => ['class' => 'checkbox']],
  8527. ['label' => [
  8528. 'class' => 'custom-class selected',
  8529. 'for' => 'checkbox1-1'
  8530. ]],
  8531. ['input' => [
  8532. 'type' => 'checkbox',
  8533. 'name' => 'checkbox1[]',
  8534. 'value' => '1',
  8535. 'id' => 'checkbox1-1',
  8536. 'checked' => 'checked'
  8537. ]],
  8538. 'First Checkbox',
  8539. '/label',
  8540. '/div',
  8541. ['div' => ['class' => 'checkbox']],
  8542. ['label' => [
  8543. 'class' => 'custom-class',
  8544. 'for' => 'checkbox1-2'
  8545. ]],
  8546. ['input' => [
  8547. 'type' => 'checkbox',
  8548. 'name' => 'checkbox1[]',
  8549. 'value' => '2',
  8550. 'id' => 'checkbox1-2'
  8551. ]],
  8552. 'Second Checkbox',
  8553. '/label',
  8554. '/div',
  8555. '/div'
  8556. ];
  8557. $this->assertHtml($expected, $result);
  8558. $result = $this->Form->control('checkbox1', [
  8559. 'label' => 'My checkboxes',
  8560. 'multiple' => 'checkbox',
  8561. 'type' => 'select',
  8562. 'options' => [
  8563. ['text' => 'First Checkbox', 'value' => 1],
  8564. ['text' => 'Second Checkbox', 'value' => 2]
  8565. ],
  8566. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8567. 'value' => ['1']
  8568. ]);
  8569. $expected = [
  8570. ['div' => ['class' => 'input select']],
  8571. ['label' => ['for' => 'checkbox1']],
  8572. 'My checkboxes',
  8573. '/label',
  8574. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8575. ['div' => ['class' => 'checkbox']],
  8576. ['label' => [
  8577. 'class' => 'custom-class another-class selected',
  8578. 'data-name' => 'bob',
  8579. 'for' => 'checkbox1-1'
  8580. ]],
  8581. ['input' => [
  8582. 'type' => 'checkbox',
  8583. 'name' => 'checkbox1[]',
  8584. 'value' => '1',
  8585. 'id' => 'checkbox1-1',
  8586. 'checked' => 'checked'
  8587. ]],
  8588. 'First Checkbox',
  8589. '/label',
  8590. '/div',
  8591. ['div' => ['class' => 'checkbox']],
  8592. ['label' => [
  8593. 'class' => 'custom-class another-class',
  8594. 'data-name' => 'bob',
  8595. 'for' => 'checkbox1-2'
  8596. ]],
  8597. ['input' => [
  8598. 'type' => 'checkbox',
  8599. 'name' => 'checkbox1[]',
  8600. 'value' => '2',
  8601. 'id' => 'checkbox1-2'
  8602. ]],
  8603. 'Second Checkbox',
  8604. '/label',
  8605. '/div',
  8606. '/div'
  8607. ];
  8608. $this->assertHtml($expected, $result);
  8609. }
  8610. }