FormHelperTest.php 310 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355
  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\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. use Cake\View\Helper\FormHelper;
  26. use Cake\View\View;
  27. use Cake\View\Widget\WidgetLocator;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->setSchema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->setSchema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::setSalt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. $this->getTableLocator()->clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $locator = $helper->getWidgetLocator();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetLocator will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $locator = $helper->getWidgetLocator();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  224. }
  225. /**
  226. * Test setting the widget locator
  227. *
  228. * @return void
  229. */
  230. public function testSetAndGetWidgetLocator()
  231. {
  232. $helper = new FormHelper($this->View);
  233. $locator = new WidgetLocator($helper->templater(), $this->View);
  234. $helper->setWidgetLocator($locator);
  235. $this->assertSame($locator, $helper->getWidgetLocator());
  236. }
  237. /**
  238. * Test overridding grouped input types which controls generation of "for"
  239. * attribute of labels.
  240. *
  241. * @return void
  242. */
  243. public function testConstructWithGroupedInputTypes()
  244. {
  245. $helper = new FormHelper($this->View, [
  246. 'groupedInputTypes' => ['radio'],
  247. ]);
  248. $result = $helper->control('when', ['type' => 'datetime']);
  249. $this->assertContains('<label for="when">When</label>', $result);
  250. }
  251. /**
  252. * Test registering a new widget class and rendering it.
  253. *
  254. * @return void
  255. */
  256. public function testAddWidgetAndRenderWidget()
  257. {
  258. $data = [
  259. 'val' => 1
  260. ];
  261. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  262. $this->Form->addWidget('test', $mock);
  263. $mock->expects($this->once())
  264. ->method('render')
  265. ->with($data)
  266. ->will($this->returnValue('HTML'));
  267. $result = $this->Form->widget('test', $data);
  268. $this->assertEquals('HTML', $result);
  269. }
  270. /**
  271. * Test that secureFields() of widget is called after calling render(),
  272. * not before.
  273. *
  274. * @return void
  275. */
  276. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  277. {
  278. $data = [
  279. 'val' => 1,
  280. 'name' => 'test'
  281. ];
  282. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  283. $this->Form->addWidget('test', $mock);
  284. $mock->expects($this->at(0))
  285. ->method('render')
  286. ->with($data)
  287. ->will($this->returnValue('HTML'));
  288. $mock->expects($this->at(1))
  289. ->method('secureFields')
  290. ->with($data)
  291. ->will($this->returnValue(['test']));
  292. $result = $this->Form->widget('test', $data + ['secure' => true]);
  293. $this->assertEquals('HTML', $result);
  294. }
  295. /**
  296. * Test that empty string is not added to secure fields list when
  297. * rendering input widget without name.
  298. *
  299. * @return void
  300. */
  301. public function testRenderingWidgetWithEmptyName()
  302. {
  303. $this->assertEquals([], $this->Form->fields);
  304. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  305. $this->assertEquals('<select name=""></select>', $result);
  306. $this->assertEquals([], $this->Form->fields);
  307. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  308. $this->assertEquals('<select name="0"></select>', $result);
  309. $this->assertEquals(['0'], $this->Form->fields);
  310. }
  311. /**
  312. * Test registering an invalid widget class.
  313. *
  314. * @return void
  315. */
  316. public function testAddWidgetInvalid()
  317. {
  318. $this->expectException(\RuntimeException::class);
  319. $mock = new \StdClass();
  320. $this->Form->addWidget('test', $mock);
  321. $this->Form->widget('test');
  322. }
  323. /**
  324. * Test adding a new context class.
  325. *
  326. * @return void
  327. */
  328. public function testAddContextProvider()
  329. {
  330. $context = 'My data';
  331. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  332. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  333. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  334. $this->assertEquals($context, $data['entity']);
  335. return $stub;
  336. });
  337. $this->Form->create($context);
  338. $result = $this->Form->context();
  339. $this->assertSame($stub, $result);
  340. }
  341. /**
  342. * Test replacing a context class.
  343. *
  344. * @return void
  345. */
  346. public function testAddContextProviderReplace()
  347. {
  348. $entity = new Article();
  349. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  350. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  351. return $stub;
  352. });
  353. $this->Form->create($entity);
  354. $result = $this->Form->context();
  355. $this->assertSame($stub, $result);
  356. }
  357. /**
  358. * Test overriding a context class.
  359. *
  360. * @return void
  361. */
  362. public function testAddContextProviderAdd()
  363. {
  364. $entity = new Article();
  365. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  366. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  367. if ($data['entity'] instanceof Entity) {
  368. return $stub;
  369. }
  370. });
  371. $this->Form->create($entity);
  372. $result = $this->Form->context();
  373. $this->assertSame($stub, $result);
  374. }
  375. /**
  376. * Test adding an invalid context class.
  377. *
  378. * @return void
  379. */
  380. public function testAddContextProviderInvalid()
  381. {
  382. $this->expectException(\RuntimeException::class);
  383. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  384. $context = 'My data';
  385. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  386. return new \StdClass();
  387. });
  388. $this->Form->create($context);
  389. }
  390. /**
  391. * Provides context options for create().
  392. *
  393. * @return array
  394. */
  395. public function contextSelectionProvider()
  396. {
  397. $entity = new Article();
  398. $collection = new Collection([$entity]);
  399. $emptyCollection = new Collection([]);
  400. $arrayObject = new \ArrayObject([]);
  401. $data = [
  402. 'schema' => [
  403. 'title' => ['type' => 'string']
  404. ]
  405. ];
  406. $form = new Form();
  407. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  408. return [
  409. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  410. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  411. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  412. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  413. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  414. 'form' => [$form, 'Cake\View\Form\FormContext'],
  415. 'none' => [null, 'Cake\View\Form\NullContext'],
  416. 'false' => [false, 'Cake\View\Form\NullContext'],
  417. 'custom' => [$custom, get_class($custom)]
  418. ];
  419. }
  420. /**
  421. * Test default context selection in create()
  422. *
  423. * @dataProvider contextSelectionProvider
  424. * @return void
  425. */
  426. public function testCreateContextSelectionBuiltIn($data, $class)
  427. {
  428. $this->loadFixtures('Articles');
  429. $this->Form->create($data);
  430. $this->assertInstanceOf($class, $this->Form->context());
  431. }
  432. /**
  433. * Data provider for type option.
  434. *
  435. * @return array
  436. */
  437. public static function requestTypeProvider()
  438. {
  439. return [
  440. // type, method, override
  441. ['post', 'post', 'POST'],
  442. ['put', 'post', 'PUT'],
  443. ['patch', 'post', 'PATCH'],
  444. ['delete', 'post', 'DELETE'],
  445. ];
  446. }
  447. /**
  448. * Test creating file forms.
  449. *
  450. * @return void
  451. */
  452. public function testCreateFile()
  453. {
  454. $encoding = strtolower(Configure::read('App.encoding'));
  455. $result = $this->Form->create(false, ['type' => 'file']);
  456. $expected = [
  457. 'form' => [
  458. 'method' => 'post', 'action' => '/articles/add',
  459. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  460. ],
  461. 'div' => ['style' => 'display:none;'],
  462. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  463. '/div'
  464. ];
  465. $this->assertHtml($expected, $result);
  466. }
  467. /**
  468. * Test creating GET forms.
  469. *
  470. * @return void
  471. */
  472. public function testCreateGet()
  473. {
  474. $encoding = strtolower(Configure::read('App.encoding'));
  475. $result = $this->Form->create(false, ['type' => 'get']);
  476. $expected = ['form' => [
  477. 'method' => 'get', 'action' => '/articles/add',
  478. 'accept-charset' => $encoding
  479. ]];
  480. $this->assertHtml($expected, $result);
  481. }
  482. /**
  483. * Test explicit method/enctype options.
  484. *
  485. * Explicit method overwrites inferred method from 'type'
  486. *
  487. * @return void
  488. */
  489. public function testCreateExplicitMethodEnctype()
  490. {
  491. $encoding = strtolower(Configure::read('App.encoding'));
  492. $result = $this->Form->create(false, [
  493. 'type' => 'get',
  494. 'method' => 'put',
  495. 'enctype' => 'multipart/form-data'
  496. ]);
  497. $expected = ['form' => [
  498. 'method' => 'put',
  499. 'action' => '/articles/add',
  500. 'enctype' => 'multipart/form-data',
  501. 'accept-charset' => $encoding
  502. ]];
  503. $this->assertHtml($expected, $result);
  504. }
  505. /**
  506. * Test create() with the templates option.
  507. *
  508. * @return void
  509. */
  510. public function testCreateTemplatesArray()
  511. {
  512. $result = $this->Form->create($this->article, [
  513. 'templates' => [
  514. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  515. ]
  516. ]);
  517. $expected = [
  518. 'form' => [
  519. 'class' => 'form-horizontal',
  520. 'method' => 'post',
  521. 'action' => '/articles/add',
  522. 'accept-charset' => 'utf-8'
  523. ]
  524. ];
  525. $this->assertHtml($expected, $result);
  526. }
  527. /**
  528. * Test create() with the templates option.
  529. *
  530. * @return void
  531. */
  532. public function testCreateTemplatesFile()
  533. {
  534. $result = $this->Form->create($this->article, [
  535. 'templates' => 'htmlhelper_tags',
  536. ]);
  537. $expected = [
  538. 'start form',
  539. 'div' => ['class' => 'hidden'],
  540. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  541. '/div'
  542. ];
  543. $this->assertHtml($expected, $result);
  544. }
  545. /**
  546. * Test that create() and end() restore templates.
  547. *
  548. * @return void
  549. */
  550. public function testCreateEndRestoreTemplates()
  551. {
  552. $this->Form->create($this->article, [
  553. 'templates' => ['input' => 'custom input element']
  554. ]);
  555. $this->Form->end();
  556. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  557. }
  558. /**
  559. * Test using template vars in various templates used by control() method.
  560. *
  561. * @return void
  562. */
  563. public function testControlTemplateVars()
  564. {
  565. $result = $this->Form->control('text', [
  566. 'templates' => [
  567. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  568. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  569. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  570. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  571. ],
  572. 'templateVars' => [
  573. 'forinput' => 'in-input',
  574. 'forlabel' => 'in-label',
  575. 'forgroup' => 'in-group',
  576. 'forcontainer' => 'in-container'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Text in-label',
  583. '/label',
  584. 'in-group',
  585. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  586. 'in-container',
  587. '/div',
  588. ];
  589. $this->assertHtml($expected, $result);
  590. }
  591. /**
  592. * Test ensuring template variables work in template files loaded
  593. * during control().
  594. *
  595. * @return void
  596. */
  597. public function testControlTemplatesFromFile()
  598. {
  599. $result = $this->Form->control('title', [
  600. 'templates' => 'test_templates',
  601. 'templateVars' => [
  602. 'forcontainer' => 'container-data'
  603. ]
  604. ]);
  605. $expected = [
  606. 'div' => ['class'],
  607. 'label' => ['for'],
  608. 'Title',
  609. '/label',
  610. 'input' => ['name', 'type' => 'text', 'id'],
  611. 'container-data',
  612. '/div',
  613. ];
  614. $this->assertHtml($expected, $result);
  615. }
  616. /**
  617. * Test using template vars in inputSubmit and submitContainer template.
  618. *
  619. * @return void
  620. */
  621. public function testSubmitTemplateVars()
  622. {
  623. $this->Form->setTemplates([
  624. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  625. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  626. ]);
  627. $result = $this->Form->submit('Submit', [
  628. 'templateVars' => [
  629. 'forinput' => 'in-input',
  630. 'forcontainer' => 'in-container'
  631. ]
  632. ]);
  633. $expected = [
  634. 'div' => ['class'],
  635. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  636. 'in-container',
  637. '/div',
  638. ];
  639. $this->assertHtml($expected, $result);
  640. }
  641. /**
  642. * test the create() method
  643. *
  644. * @dataProvider requestTypeProvider
  645. * @return void
  646. */
  647. public function testCreateTypeOptions($type, $method, $override)
  648. {
  649. $encoding = strtolower(Configure::read('App.encoding'));
  650. $result = $this->Form->create(false, ['type' => $type]);
  651. $expected = [
  652. 'form' => [
  653. 'method' => $method, 'action' => '/articles/add',
  654. 'accept-charset' => $encoding
  655. ],
  656. 'div' => ['style' => 'display:none;'],
  657. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  658. '/div'
  659. ];
  660. $this->assertHtml($expected, $result);
  661. }
  662. /**
  663. * Test using template vars in Create (formStart template)
  664. *
  665. * @return void
  666. */
  667. public function testCreateTemplateVars()
  668. {
  669. $result = $this->Form->create($this->article, [
  670. 'templates' => [
  671. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  672. ],
  673. 'templateVars' => ['header' => 'headertext']
  674. ]);
  675. $expected = [
  676. 'h4' => ['class'],
  677. 'headertext',
  678. '/h4',
  679. 'form' => [
  680. 'method' => 'post',
  681. 'action' => '/articles/add',
  682. 'accept-charset' => 'utf-8'
  683. ]
  684. ];
  685. $this->assertHtml($expected, $result);
  686. }
  687. /**
  688. * Test opening a form for an update operation.
  689. *
  690. * @return void
  691. */
  692. public function testCreateUpdateForm()
  693. {
  694. $encoding = strtolower(Configure::read('App.encoding'));
  695. $this->Form->request = $this->Form->request
  696. ->withRequestTarget('/articles/edit/1')
  697. ->withParam('action', 'edit');
  698. $this->article['defaults']['id'] = 1;
  699. $result = $this->Form->create($this->article);
  700. $expected = [
  701. 'form' => [
  702. 'method' => 'post',
  703. 'action' => '/articles/edit/1',
  704. 'accept-charset' => $encoding
  705. ],
  706. 'div' => ['style' => 'display:none;'],
  707. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  708. '/div'
  709. ];
  710. $this->assertHtml($expected, $result);
  711. }
  712. /**
  713. * test create() with automatic url generation
  714. *
  715. * @return void
  716. */
  717. public function testCreateAutoUrl()
  718. {
  719. $encoding = strtolower(Configure::read('App.encoding'));
  720. $this->Form->request = $this->Form->request
  721. ->withRequestTarget('/articles/delete/10')
  722. ->withParam('action', 'delete');
  723. $result = $this->Form->create($this->article);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post', 'action' => '/articles/delete/10',
  727. 'accept-charset' => $encoding
  728. ],
  729. 'div' => ['style' => 'display:none;'],
  730. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  731. '/div'
  732. ];
  733. $this->assertHtml($expected, $result);
  734. $this->article['defaults'] = ['id' => 1];
  735. $this->Form->request = $this->Form->request
  736. ->withRequestTarget('/articles/edit/1')
  737. ->withParam('action', 'delete');
  738. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  739. $expected = [
  740. 'form' => [
  741. 'method' => 'post',
  742. 'action' => '/articles/edit/1',
  743. 'accept-charset' => $encoding
  744. ],
  745. 'div' => ['style' => 'display:none;'],
  746. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  747. '/div'
  748. ];
  749. $this->assertHtml($expected, $result);
  750. $this->Form->request = $this->Form->request
  751. ->withParam('action', 'add');
  752. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  753. $expected = [
  754. 'form' => [
  755. 'method' => 'post',
  756. 'action' => '/articles/publish/1',
  757. 'accept-charset' => $encoding
  758. ],
  759. 'div' => ['style' => 'display:none;'],
  760. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  761. '/div'
  762. ];
  763. $this->assertHtml($expected, $result);
  764. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  765. $expected = [
  766. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  767. 'div' => ['style' => 'display:none;'],
  768. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  769. '/div'
  770. ];
  771. $this->assertHtml($expected, $result);
  772. $this->Form->request = $this->Form->request
  773. ->withParam('controller', 'Pages');
  774. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  775. $expected = [
  776. 'form' => [
  777. 'method' => 'post', 'action' => '/Pages/signup/1',
  778. 'accept-charset' => $encoding
  779. ],
  780. 'div' => ['style' => 'display:none;'],
  781. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  782. '/div'
  783. ];
  784. $this->assertHtml($expected, $result);
  785. }
  786. /**
  787. * Test create() with no URL (no "action" attribute for <form> tag)
  788. *
  789. * @return void
  790. */
  791. public function testCreateNoUrl()
  792. {
  793. $result = $this->Form->create(false, ['url' => false]);
  794. $expected = [
  795. 'form' => [
  796. 'method' => 'post',
  797. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  798. ],
  799. 'div' => ['style' => 'display:none;'],
  800. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  801. '/div'
  802. ];
  803. $this->assertHtml($expected, $result);
  804. }
  805. /**
  806. * test create() with a custom route
  807. *
  808. * @return void
  809. */
  810. public function testCreateCustomRoute()
  811. {
  812. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  813. $encoding = strtolower(Configure::read('App.encoding'));
  814. $this->Form->request = $this->Form->request
  815. ->withParam('controller', 'users');
  816. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  817. $expected = [
  818. 'form' => [
  819. 'method' => 'post', 'action' => '/login',
  820. 'accept-charset' => $encoding
  821. ],
  822. 'div' => ['style' => 'display:none;'],
  823. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  824. '/div'
  825. ];
  826. $this->assertHtml($expected, $result);
  827. Router::connect(
  828. '/new-article',
  829. ['controller' => 'articles', 'action' => 'myaction'],
  830. ['_name' => 'my-route']
  831. );
  832. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  833. $expected = [
  834. 'form' => [
  835. 'method' => 'post', 'action' => '/new-article',
  836. 'accept-charset' => $encoding,
  837. ],
  838. 'div' => ['style' => 'display:none;'],
  839. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  840. '/div'
  841. ];
  842. $this->assertHtml($expected, $result);
  843. }
  844. /**
  845. * test automatic accept-charset overriding
  846. *
  847. * @return void
  848. */
  849. public function testCreateWithAcceptCharset()
  850. {
  851. $result = $this->Form->create(
  852. $this->article,
  853. [
  854. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  855. ]
  856. );
  857. $expected = [
  858. 'form' => [
  859. 'method' => 'post', 'action' => '/articles',
  860. 'accept-charset' => 'iso-8859-1'
  861. ],
  862. 'div' => ['style' => 'display:none;'],
  863. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  864. '/div'
  865. ];
  866. $this->assertHtml($expected, $result);
  867. }
  868. /**
  869. * Test base form URL when url param is passed with multiple parameters (&)
  870. *
  871. */
  872. public function testCreateQueryStringRequest()
  873. {
  874. $encoding = strtolower(Configure::read('App.encoding'));
  875. $result = $this->Form->create($this->article, [
  876. 'type' => 'post',
  877. 'escape' => false,
  878. 'url' => [
  879. 'controller' => 'controller',
  880. 'action' => 'action',
  881. '?' => ['param1' => 'value1', 'param2' => 'value2']
  882. ]
  883. ]);
  884. $expected = [
  885. 'form' => [
  886. 'method' => 'post',
  887. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  888. 'accept-charset' => $encoding
  889. ],
  890. 'div' => ['style' => 'display:none;'],
  891. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  892. '/div'
  893. ];
  894. $this->assertHtml($expected, $result);
  895. $result = $this->Form->create($this->article, [
  896. 'type' => 'post',
  897. 'url' => [
  898. 'controller' => 'controller',
  899. 'action' => 'action',
  900. '?' => ['param1' => 'value1', 'param2' => 'value2']
  901. ]
  902. ]);
  903. $this->assertHtml($expected, $result);
  904. }
  905. /**
  906. * test that create() doesn't cause errors by multiple id's being in the primary key
  907. * as could happen with multiple select or checkboxes.
  908. *
  909. * @return void
  910. */
  911. public function testCreateWithMultipleIdInData()
  912. {
  913. $encoding = strtolower(Configure::read('App.encoding'));
  914. $this->Form->request = $this->Form->request->withData('Article.id', [1, 2]);
  915. $result = $this->Form->create($this->article);
  916. $expected = [
  917. 'form' => [
  918. 'method' => 'post',
  919. 'action' => '/articles/add',
  920. 'accept-charset' => $encoding
  921. ],
  922. 'div' => ['style' => 'display:none;'],
  923. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  924. '/div'
  925. ];
  926. $this->assertHtml($expected, $result);
  927. }
  928. /**
  929. * test that create() doesn't add in extra passed params.
  930. *
  931. * @return void
  932. */
  933. public function testCreatePassedArgs()
  934. {
  935. $encoding = strtolower(Configure::read('App.encoding'));
  936. $this->Form->request = $this->Form->request->withData('Article.id', 1);
  937. $result = $this->Form->create($this->article, [
  938. 'type' => 'post',
  939. 'escape' => false,
  940. 'url' => [
  941. 'action' => 'edit',
  942. 'myparam'
  943. ]
  944. ]);
  945. $expected = [
  946. 'form' => [
  947. 'method' => 'post',
  948. 'action' => '/articles/edit/myparam',
  949. 'accept-charset' => $encoding
  950. ],
  951. 'div' => ['style' => 'display:none;'],
  952. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  953. '/div'
  954. ];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test creating a get form, and get form inputs.
  959. *
  960. * @return void
  961. */
  962. public function testGetFormCreate()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $result = $this->Form->create($this->article, ['type' => 'get']);
  966. $expected = ['form' => [
  967. 'method' => 'get', 'action' => '/articles/add',
  968. 'accept-charset' => $encoding
  969. ]];
  970. $this->assertHtml($expected, $result);
  971. $result = $this->Form->text('title');
  972. $expected = ['input' => [
  973. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  974. ]];
  975. $this->assertHtml($expected, $result);
  976. $result = $this->Form->password('password');
  977. $expected = ['input' => [
  978. 'name' => 'password', 'type' => 'password'
  979. ]];
  980. $this->assertHtml($expected, $result);
  981. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  982. $result = $this->Form->text('user_form');
  983. $expected = ['input' => [
  984. 'name' => 'user_form', 'type' => 'text'
  985. ]];
  986. $this->assertHtml($expected, $result);
  987. }
  988. /**
  989. * test get form, and inputs when the model param is false
  990. *
  991. * @return void
  992. */
  993. public function testGetFormWithFalseModel()
  994. {
  995. $encoding = strtolower(Configure::read('App.encoding'));
  996. $this->Form->request = $this->Form->request->withParam('controller', 'contact_test');
  997. $result = $this->Form->create(false, [
  998. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  999. ]);
  1000. $expected = ['form' => [
  1001. 'method' => 'get', 'action' => '/contact_test/add',
  1002. 'accept-charset' => $encoding
  1003. ]];
  1004. $this->assertHtml($expected, $result);
  1005. $result = $this->Form->text('reason');
  1006. $expected = [
  1007. 'input' => ['type' => 'text', 'name' => 'reason']
  1008. ];
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateWithSecurity method
  1013. *
  1014. * Test form->create() with security key.
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateWithSecurity()
  1019. {
  1020. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1021. $encoding = strtolower(Configure::read('App.encoding'));
  1022. $result = $this->Form->create($this->article, [
  1023. 'url' => '/articles/publish',
  1024. ]);
  1025. $expected = [
  1026. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1027. 'div' => ['style' => 'display:none;'],
  1028. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1029. ['input' => [
  1030. 'type' => 'hidden',
  1031. 'name' => '_csrfToken',
  1032. 'value' => 'testKey',
  1033. 'autocomplete' => 'off',
  1034. ]],
  1035. '/div'
  1036. ];
  1037. $this->assertHtml($expected, $result);
  1038. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1039. $expected['form']['id'] = 'MyForm';
  1040. $this->assertHtml($expected, $result);
  1041. }
  1042. /**
  1043. * testFormCreateGetNoSecurity method
  1044. *
  1045. * Test form->create() with no security key as its a get form
  1046. *
  1047. * @return void
  1048. */
  1049. public function testCreateEndGetNoSecurity()
  1050. {
  1051. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1052. $article = new Article();
  1053. $result = $this->Form->create($article, [
  1054. 'type' => 'get',
  1055. 'url' => '/contacts/add'
  1056. ]);
  1057. $this->assertNotContains('testKey', $result);
  1058. $result = $this->Form->end();
  1059. $this->assertNotContains('testKey', $result);
  1060. }
  1061. /**
  1062. * test that create() clears the fields property so it starts fresh
  1063. *
  1064. * @return void
  1065. */
  1066. public function testCreateClearingFields()
  1067. {
  1068. $this->Form->fields = ['model_id'];
  1069. $this->Form->create($this->article);
  1070. $this->assertEquals([], $this->Form->fields);
  1071. }
  1072. /**
  1073. * Tests form hash generation with model-less data
  1074. *
  1075. * @return void
  1076. */
  1077. public function testValidateHashNoModel()
  1078. {
  1079. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1080. $fields = ['anything'];
  1081. $result = $this->Form->secure($fields);
  1082. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1083. $this->assertContains($hash, $result);
  1084. }
  1085. /**
  1086. * Tests that hidden fields generated for checkboxes don't get locked
  1087. *
  1088. * @return void
  1089. */
  1090. public function testNoCheckboxLocking()
  1091. {
  1092. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1093. $this->assertSame([], $this->Form->fields);
  1094. $this->Form->checkbox('check', ['value' => '1']);
  1095. $this->assertSame($this->Form->fields, ['check']);
  1096. }
  1097. /**
  1098. * testFormSecurityFields method
  1099. *
  1100. * Test generation of secure form hash generation.
  1101. *
  1102. * @return void
  1103. */
  1104. public function testFormSecurityFields()
  1105. {
  1106. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1107. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1108. $result = $this->Form->secure($fields);
  1109. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1110. $hash .= ':' . 'Model.valid';
  1111. $hash = urlencode($hash);
  1112. $tokenDebug = urlencode(json_encode([
  1113. '',
  1114. $fields,
  1115. []
  1116. ]));
  1117. $expected = [
  1118. 'div' => ['style' => 'display:none;'],
  1119. ['input' => [
  1120. 'type' => 'hidden',
  1121. 'name' => '_Token[fields]',
  1122. 'value' => $hash,
  1123. 'autocomplete' => 'off',
  1124. ]],
  1125. ['input' => [
  1126. 'type' => 'hidden',
  1127. 'name' => '_Token[unlocked]',
  1128. 'value' => '',
  1129. 'autocomplete' => 'off',
  1130. ]],
  1131. ['input' => [
  1132. 'type' => 'hidden',
  1133. 'name' => '_Token[debug]',
  1134. 'value' => $tokenDebug,
  1135. 'autocomplete' => 'off',
  1136. ]],
  1137. '/div'
  1138. ];
  1139. $this->assertHtml($expected, $result);
  1140. }
  1141. /**
  1142. * testFormSecurityFields method
  1143. *
  1144. * Test debug token is not generated if debug is false
  1145. *
  1146. * @return void
  1147. */
  1148. public function testFormSecurityFieldsNoDebugMode()
  1149. {
  1150. Configure::write('debug', false);
  1151. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1152. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1153. $result = $this->Form->secure($fields);
  1154. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1155. $hash .= ':' . 'Model.valid';
  1156. $hash = urlencode($hash);
  1157. $expected = [
  1158. 'div' => ['style' => 'display:none;'],
  1159. ['input' => [
  1160. 'type' => 'hidden',
  1161. 'name' => '_Token[fields]',
  1162. 'autocomplete' => 'off',
  1163. 'value' => $hash
  1164. ]],
  1165. ['input' => [
  1166. 'type' => 'hidden',
  1167. 'name' => '_Token[unlocked]',
  1168. 'autocomplete' => 'off',
  1169. 'value' => '',
  1170. ]],
  1171. '/div'
  1172. ];
  1173. $this->assertHtml($expected, $result);
  1174. }
  1175. /**
  1176. * Tests correct generation of number fields for smallint
  1177. *
  1178. * @return void
  1179. */
  1180. public function testTextFieldGenerationForSmallint()
  1181. {
  1182. $this->article['schema'] = [
  1183. 'foo' => [
  1184. 'type' => 'smallinteger',
  1185. 'null' => false,
  1186. 'default' => null,
  1187. 'length' => 10
  1188. ]
  1189. ];
  1190. $this->Form->create($this->article);
  1191. $result = $this->Form->control('foo');
  1192. $this->assertContains('class="input number"', $result);
  1193. $this->assertContains('type="number"', $result);
  1194. }
  1195. /**
  1196. * Tests correct generation of number fields for tinyint
  1197. *
  1198. * @return void
  1199. */
  1200. public function testTextFieldGenerationForTinyint()
  1201. {
  1202. $this->article['schema'] = [
  1203. 'foo' => [
  1204. 'type' => 'tinyinteger',
  1205. 'null' => false,
  1206. 'default' => null,
  1207. 'length' => 10
  1208. ]
  1209. ];
  1210. $this->Form->create($this->article);
  1211. $result = $this->Form->control('foo');
  1212. $this->assertContains('class="input number"', $result);
  1213. $this->assertContains('type="number"', $result);
  1214. }
  1215. /**
  1216. * Tests correct generation of number fields for double and float fields
  1217. *
  1218. * @return void
  1219. */
  1220. public function testTextFieldGenerationForFloats()
  1221. {
  1222. $this->article['schema'] = [
  1223. 'foo' => [
  1224. 'type' => 'float',
  1225. 'null' => false,
  1226. 'default' => null,
  1227. 'length' => 10
  1228. ]
  1229. ];
  1230. $this->Form->create($this->article);
  1231. $result = $this->Form->control('foo');
  1232. $expected = [
  1233. 'div' => ['class' => 'input number'],
  1234. 'label' => ['for' => 'foo'],
  1235. 'Foo',
  1236. '/label',
  1237. ['input' => [
  1238. 'type' => 'number',
  1239. 'name' => 'foo',
  1240. 'id' => 'foo',
  1241. 'step' => 'any'
  1242. ]],
  1243. '/div'
  1244. ];
  1245. $this->assertHtml($expected, $result);
  1246. $result = $this->Form->control('foo', ['step' => 0.5]);
  1247. $expected = [
  1248. 'div' => ['class' => 'input number'],
  1249. 'label' => ['for' => 'foo'],
  1250. 'Foo',
  1251. '/label',
  1252. ['input' => [
  1253. 'type' => 'number',
  1254. 'name' => 'foo',
  1255. 'id' => 'foo',
  1256. 'step' => '0.5'
  1257. ]],
  1258. '/div'
  1259. ];
  1260. $this->assertHtml($expected, $result);
  1261. }
  1262. /**
  1263. * Tests correct generation of number fields for integer fields
  1264. *
  1265. * @return void
  1266. */
  1267. public function testTextFieldTypeNumberGenerationForIntegers()
  1268. {
  1269. $this->getTableLocator()->get('Contacts', [
  1270. 'className' => __NAMESPACE__ . '\ContactsTable'
  1271. ]);
  1272. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1273. $result = $this->Form->control('age');
  1274. $expected = [
  1275. 'div' => ['class' => 'input number'],
  1276. 'label' => ['for' => 'age'],
  1277. 'Age',
  1278. '/label',
  1279. ['input' => [
  1280. 'type' => 'number', 'name' => 'age',
  1281. 'id' => 'age'
  1282. ]],
  1283. '/div'
  1284. ];
  1285. $this->assertHtml($expected, $result);
  1286. }
  1287. /**
  1288. * Tests correct generation of file upload fields for binary fields
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFileUploadFieldTypeGenerationForBinaries()
  1293. {
  1294. $table = $this->getTableLocator()->get('Contacts', [
  1295. 'className' => __NAMESPACE__ . '\ContactsTable'
  1296. ]);
  1297. $table->setSchema(['foo' => [
  1298. 'type' => 'binary',
  1299. 'null' => false,
  1300. 'default' => null,
  1301. 'length' => 1024
  1302. ]]);
  1303. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1304. $result = $this->Form->control('foo');
  1305. $expected = [
  1306. 'div' => ['class' => 'input file'],
  1307. 'label' => ['for' => 'foo'],
  1308. 'Foo',
  1309. '/label',
  1310. ['input' => [
  1311. 'type' => 'file', 'name' => 'foo',
  1312. 'id' => 'foo'
  1313. ]],
  1314. '/div'
  1315. ];
  1316. $this->assertHtml($expected, $result);
  1317. }
  1318. /**
  1319. * testFormSecurityMultipleFields method
  1320. *
  1321. * Test secure() with multiple row form. Ensure hash is correct.
  1322. *
  1323. * @return void
  1324. */
  1325. public function testFormSecurityMultipleFields()
  1326. {
  1327. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1328. $fields = [
  1329. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1330. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1331. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1332. ];
  1333. $result = $this->Form->secure($fields);
  1334. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1335. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1336. $tokenDebug = urlencode(json_encode([
  1337. '',
  1338. $fields,
  1339. []
  1340. ]));
  1341. $expected = [
  1342. 'div' => ['style' => 'display:none;'],
  1343. ['input' => [
  1344. 'type' => 'hidden',
  1345. 'name' => '_Token[fields]',
  1346. 'value' => $hash,
  1347. 'autocomplete' => 'off',
  1348. ]],
  1349. ['input' => [
  1350. 'type' => 'hidden',
  1351. 'name' => '_Token[unlocked]',
  1352. 'autocomplete' => 'off',
  1353. 'value' => ''
  1354. ]],
  1355. ['input' => [
  1356. 'type' => 'hidden',
  1357. 'name' => '_Token[debug]',
  1358. 'value' => $tokenDebug,
  1359. 'autocomplete' => 'off',
  1360. ]],
  1361. '/div'
  1362. ];
  1363. $this->assertHtml($expected, $result);
  1364. }
  1365. /**
  1366. * testFormSecurityMultipleSubmitButtons
  1367. *
  1368. * test form submit generation and ensure that _Token is only created on end()
  1369. *
  1370. * @return void
  1371. */
  1372. public function testFormSecurityMultipleSubmitButtons()
  1373. {
  1374. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1375. $this->Form->create($this->article);
  1376. $this->Form->text('Address.title');
  1377. $this->Form->text('Address.first_name');
  1378. $result = $this->Form->submit('Save', ['name' => 'save']);
  1379. $expected = [
  1380. 'div' => ['class' => 'submit'],
  1381. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1382. '/div',
  1383. ];
  1384. $this->assertHtml($expected, $result);
  1385. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1386. $expected = [
  1387. 'div' => ['class' => 'submit'],
  1388. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1389. '/div',
  1390. ];
  1391. $this->assertHtml($expected, $result);
  1392. $result = $this->Form->end();
  1393. $tokenDebug = urlencode(json_encode([
  1394. '/articles/add',
  1395. [
  1396. 'Address.title',
  1397. 'Address.first_name',
  1398. ],
  1399. ['save', 'cancel']
  1400. ]));
  1401. $expected = [
  1402. 'div' => ['style' => 'display:none;'],
  1403. ['input' => [
  1404. 'type' => 'hidden',
  1405. 'name' => '_Token[fields]',
  1406. 'autocomplete',
  1407. 'value'
  1408. ]],
  1409. ['input' => [
  1410. 'type' => 'hidden',
  1411. 'name' => '_Token[unlocked]',
  1412. 'value' => 'cancel%7Csave',
  1413. 'autocomplete' => 'off',
  1414. ]],
  1415. ['input' => [
  1416. 'type' => 'hidden',
  1417. 'name' => '_Token[debug]',
  1418. 'value' => $tokenDebug,
  1419. 'autocomplete' => 'off',
  1420. ]],
  1421. '/div'
  1422. ];
  1423. $this->assertHtml($expected, $result);
  1424. }
  1425. /**
  1426. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testSecurityButtonNestedNamed()
  1431. {
  1432. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1433. $this->Form->create('Addresses');
  1434. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1435. $result = $this->Form->unlockField();
  1436. $this->assertEquals(['Address.button'], $result);
  1437. }
  1438. /**
  1439. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1440. *
  1441. * @return void
  1442. */
  1443. public function testSecuritySubmitNestedNamed()
  1444. {
  1445. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1446. $this->Form->create($this->article);
  1447. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1448. $result = $this->Form->unlockField();
  1449. $this->assertEquals(['Address.button'], $result);
  1450. }
  1451. /**
  1452. * Test that the correct fields are unlocked for image submits with no names.
  1453. *
  1454. * @return void
  1455. */
  1456. public function testSecuritySubmitImageNoName()
  1457. {
  1458. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1459. $this->Form->create(false);
  1460. $result = $this->Form->submit('save.png');
  1461. $expected = [
  1462. 'div' => ['class' => 'submit'],
  1463. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1464. '/div'
  1465. ];
  1466. $this->assertHtml($expected, $result);
  1467. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1468. }
  1469. /**
  1470. * Test that the correct fields are unlocked for image submits with names.
  1471. *
  1472. * @return void
  1473. */
  1474. public function testSecuritySubmitImageName()
  1475. {
  1476. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1477. $this->Form->create(null);
  1478. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1479. $expected = [
  1480. 'div' => ['class' => 'submit'],
  1481. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1482. '/div'
  1483. ];
  1484. $this->assertHtml($expected, $result);
  1485. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1486. }
  1487. /**
  1488. * testFormSecurityMultipleControlFields method
  1489. *
  1490. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1491. *
  1492. * @return void
  1493. */
  1494. public function testFormSecurityMultipleControlFields()
  1495. {
  1496. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1497. $this->Form->create();
  1498. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1499. $this->Form->control('Addresses.0.title');
  1500. $this->Form->control('Addresses.0.first_name');
  1501. $this->Form->control('Addresses.0.last_name');
  1502. $this->Form->control('Addresses.0.address');
  1503. $this->Form->control('Addresses.0.city');
  1504. $this->Form->control('Addresses.0.phone');
  1505. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1506. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1507. $this->Form->control('Addresses.1.title');
  1508. $this->Form->control('Addresses.1.first_name');
  1509. $this->Form->control('Addresses.1.last_name');
  1510. $this->Form->control('Addresses.1.address');
  1511. $this->Form->control('Addresses.1.city');
  1512. $this->Form->control('Addresses.1.phone');
  1513. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1514. $result = $this->Form->secure($this->Form->fields);
  1515. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1516. $tokenDebug = urlencode(json_encode([
  1517. '/articles/add',
  1518. [
  1519. 'Addresses.0.id' => '123456',
  1520. 'Addresses.0.title',
  1521. 'Addresses.0.first_name',
  1522. 'Addresses.0.last_name',
  1523. 'Addresses.0.address',
  1524. 'Addresses.0.city',
  1525. 'Addresses.0.phone',
  1526. 'Addresses.0.primary',
  1527. 'Addresses.1.id' => '654321',
  1528. 'Addresses.1.title',
  1529. 'Addresses.1.first_name',
  1530. 'Addresses.1.last_name',
  1531. 'Addresses.1.address',
  1532. 'Addresses.1.city',
  1533. 'Addresses.1.phone',
  1534. 'Addresses.1.primary',
  1535. ],
  1536. []
  1537. ]));
  1538. $expected = [
  1539. 'div' => ['style' => 'display:none;'],
  1540. ['input' => [
  1541. 'type' => 'hidden',
  1542. 'name' => '_Token[fields]',
  1543. 'value' => $hash,
  1544. 'autocomplete' => 'off',
  1545. ]],
  1546. ['input' => [
  1547. 'type' => 'hidden',
  1548. 'name' => '_Token[unlocked]',
  1549. 'autocomplete' => 'off',
  1550. 'value' => ''
  1551. ]],
  1552. ['input' => [
  1553. 'type' => 'hidden',
  1554. 'name' => '_Token[debug]',
  1555. 'value' => $tokenDebug,
  1556. 'autocomplete' => 'off',
  1557. ]],
  1558. '/div'
  1559. ];
  1560. $this->assertHtml($expected, $result);
  1561. }
  1562. /**
  1563. * testFormSecurityArrayFields method
  1564. *
  1565. * Test form security with Model.field.0 style inputs.
  1566. *
  1567. * @return void
  1568. */
  1569. public function testFormSecurityArrayFields()
  1570. {
  1571. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1572. $this->Form->create();
  1573. $this->Form->text('Address.primary.1');
  1574. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1575. $this->Form->text('Address.secondary.1.0');
  1576. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1577. }
  1578. /**
  1579. * testFormSecurityMultipleControlDisabledFields method
  1580. *
  1581. * Test secure form generation with multiple records and disabled fields.
  1582. *
  1583. * @return void
  1584. */
  1585. public function testFormSecurityMultipleControlDisabledFields()
  1586. {
  1587. $this->Form->request = $this->Form->request->withParam('_Token', [
  1588. 'unlockedFields' => ['first_name', 'address']
  1589. ]);
  1590. $this->Form->create();
  1591. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1592. $this->Form->text('Addresses.0.title');
  1593. $this->Form->text('Addresses.0.first_name');
  1594. $this->Form->text('Addresses.0.last_name');
  1595. $this->Form->text('Addresses.0.address');
  1596. $this->Form->text('Addresses.0.city');
  1597. $this->Form->text('Addresses.0.phone');
  1598. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1599. $this->Form->text('Addresses.1.title');
  1600. $this->Form->text('Addresses.1.first_name');
  1601. $this->Form->text('Addresses.1.last_name');
  1602. $this->Form->text('Addresses.1.address');
  1603. $this->Form->text('Addresses.1.city');
  1604. $this->Form->text('Addresses.1.phone');
  1605. $result = $this->Form->secure($this->Form->fields);
  1606. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1607. $tokenDebug = urlencode(json_encode([
  1608. '/articles/add',
  1609. [
  1610. 'Addresses.0.id' => '123456',
  1611. 'Addresses.0.title',
  1612. 'Addresses.0.last_name',
  1613. 'Addresses.0.city',
  1614. 'Addresses.0.phone',
  1615. 'Addresses.1.id' => '654321',
  1616. 'Addresses.1.title',
  1617. 'Addresses.1.last_name',
  1618. 'Addresses.1.city',
  1619. 'Addresses.1.phone'
  1620. ],
  1621. [
  1622. 'first_name',
  1623. 'address'
  1624. ]
  1625. ]));
  1626. $expected = [
  1627. 'div' => ['style' => 'display:none;'],
  1628. ['input' => [
  1629. 'type' => 'hidden',
  1630. 'name' => '_Token[fields]',
  1631. 'autocomplete' => 'off',
  1632. 'value' => $hash
  1633. ]],
  1634. ['input' => [
  1635. 'type' => 'hidden',
  1636. 'name' => '_Token[unlocked]',
  1637. 'autocomplete' => 'off',
  1638. 'value' => 'address%7Cfirst_name',
  1639. ]],
  1640. ['input' => [
  1641. 'type' => 'hidden',
  1642. 'name' => '_Token[debug]',
  1643. 'autocomplete' => 'off',
  1644. 'value' => $tokenDebug,
  1645. ]],
  1646. '/div'
  1647. ];
  1648. $this->assertHtml($expected, $result);
  1649. }
  1650. /**
  1651. * testFormSecurityControlDisabledFields method
  1652. *
  1653. * Test single record form with disabled fields.
  1654. *
  1655. * @return void
  1656. */
  1657. public function testFormSecurityControlUnlockedFields()
  1658. {
  1659. $this->Form->request = $this->Form->request->withParam('_Token', [
  1660. 'unlockedFields' => ['first_name', 'address']
  1661. ]);
  1662. $this->Form->create();
  1663. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1664. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1665. $this->Form->text('Addresses.title');
  1666. $this->Form->text('Addresses.first_name');
  1667. $this->Form->text('Addresses.last_name');
  1668. $this->Form->text('Addresses.address');
  1669. $this->Form->text('Addresses.city');
  1670. $this->Form->text('Addresses.phone');
  1671. $result = $this->Form->fields;
  1672. $expected = [
  1673. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1674. 'Addresses.city', 'Addresses.phone'
  1675. ];
  1676. $this->assertEquals($expected, $result);
  1677. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1678. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1679. $tokenDebug = urlencode(json_encode([
  1680. '/articles/add',
  1681. [
  1682. 'Addresses.id' => '123456',
  1683. 'Addresses.title',
  1684. 'Addresses.last_name',
  1685. 'Addresses.city',
  1686. 'Addresses.phone'
  1687. ],
  1688. [
  1689. 'first_name',
  1690. 'address'
  1691. ]
  1692. ]));
  1693. $expected = [
  1694. 'div' => ['style' => 'display:none;'],
  1695. ['input' => [
  1696. 'type' => 'hidden',
  1697. 'name' => '_Token[fields]',
  1698. 'value' => $hash,
  1699. 'autocomplete' => 'off',
  1700. 'data-foo' => 'bar',
  1701. ]],
  1702. ['input' => [
  1703. 'type' => 'hidden',
  1704. 'name' => '_Token[unlocked]',
  1705. 'value' => 'address%7Cfirst_name',
  1706. 'autocomplete' => 'off',
  1707. 'data-foo' => 'bar',
  1708. ]],
  1709. ['input' => [
  1710. 'type' => 'hidden', 'name' => '_Token[debug]',
  1711. 'value' => $tokenDebug,
  1712. 'autocomplete' => 'off',
  1713. 'data-foo' => 'bar'
  1714. ]],
  1715. '/div'
  1716. ];
  1717. $this->assertHtml($expected, $result);
  1718. }
  1719. /**
  1720. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1721. *
  1722. * Test single record form with debugSecurity param.
  1723. *
  1724. * @return void
  1725. */
  1726. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1727. {
  1728. $this->Form->request = $this->Form->request->withParam('_Token', [
  1729. 'unlockedFields' => ['first_name', 'address']
  1730. ]);
  1731. $this->Form->create();
  1732. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1733. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1734. $this->Form->text('Addresses.title');
  1735. $this->Form->text('Addresses.first_name');
  1736. $this->Form->text('Addresses.last_name');
  1737. $this->Form->text('Addresses.address');
  1738. $this->Form->text('Addresses.city');
  1739. $this->Form->text('Addresses.phone');
  1740. $result = $this->Form->fields;
  1741. $expected = [
  1742. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1743. 'Addresses.city', 'Addresses.phone'
  1744. ];
  1745. $this->assertEquals($expected, $result);
  1746. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1747. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1748. $tokenDebug = urlencode(json_encode([
  1749. '/articles/add',
  1750. [
  1751. 'Addresses.id' => '123456',
  1752. 'Addresses.title',
  1753. 'Addresses.last_name',
  1754. 'Addresses.city',
  1755. 'Addresses.phone'
  1756. ],
  1757. [
  1758. 'first_name',
  1759. 'address'
  1760. ]
  1761. ]));
  1762. $expected = [
  1763. 'div' => ['style' => 'display:none;'],
  1764. ['input' => [
  1765. 'type' => 'hidden',
  1766. 'name' => '_Token[fields]',
  1767. 'value' => $hash,
  1768. 'autocomplete' => 'off',
  1769. 'data-foo' => 'bar',
  1770. ]],
  1771. ['input' => [
  1772. 'type' => 'hidden',
  1773. 'name' => '_Token[unlocked]',
  1774. 'value' => 'address%7Cfirst_name',
  1775. 'autocomplete' => 'off',
  1776. 'data-foo' => 'bar',
  1777. ]],
  1778. ['input' => [
  1779. 'type' => 'hidden', 'name' => '_Token[debug]',
  1780. 'value' => $tokenDebug,
  1781. 'autocomplete' => 'off',
  1782. 'data-foo' => 'bar'
  1783. ]],
  1784. '/div'
  1785. ];
  1786. $this->assertHtml($expected, $result);
  1787. }
  1788. /**
  1789. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1790. *
  1791. * Debug is false, debugSecurity is true -> no debug
  1792. *
  1793. * @return void
  1794. */
  1795. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1796. {
  1797. $this->Form->request = $this->Form->request->withParam('_Token', [
  1798. 'unlockedFields' => ['first_name', 'address']
  1799. ]);
  1800. $this->Form->create();
  1801. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1802. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1803. $this->Form->text('Addresses.title');
  1804. $this->Form->text('Addresses.first_name');
  1805. $this->Form->text('Addresses.last_name');
  1806. $this->Form->text('Addresses.address');
  1807. $this->Form->text('Addresses.city');
  1808. $this->Form->text('Addresses.phone');
  1809. $result = $this->Form->fields;
  1810. $expected = [
  1811. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1812. 'Addresses.city', 'Addresses.phone'
  1813. ];
  1814. $this->assertEquals($expected, $result);
  1815. Configure::write('debug', false);
  1816. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1817. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1818. $expected = [
  1819. 'div' => ['style' => 'display:none;'],
  1820. ['input' => [
  1821. 'type' => 'hidden',
  1822. 'name' => '_Token[fields]',
  1823. 'value' => $hash,
  1824. 'autocomplete' => 'off',
  1825. 'data-foo' => 'bar',
  1826. ]],
  1827. ['input' => [
  1828. 'type' => 'hidden',
  1829. 'name' => '_Token[unlocked]',
  1830. 'value' => 'address%7Cfirst_name',
  1831. 'autocomplete' => 'off',
  1832. 'data-foo' => 'bar',
  1833. ]],
  1834. '/div'
  1835. ];
  1836. $this->assertHtml($expected, $result);
  1837. }
  1838. /**
  1839. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1840. *
  1841. * Test single record form with debugSecurity param.
  1842. *
  1843. * @return void
  1844. */
  1845. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1846. {
  1847. $this->Form->request = $this->Form->request->withParam('_Token', [
  1848. 'unlockedFields' => ['first_name', 'address']
  1849. ]);
  1850. $this->Form->create();
  1851. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1852. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1853. $this->Form->text('Addresses.title');
  1854. $this->Form->text('Addresses.first_name');
  1855. $this->Form->text('Addresses.last_name');
  1856. $this->Form->text('Addresses.address');
  1857. $this->Form->text('Addresses.city');
  1858. $this->Form->text('Addresses.phone');
  1859. $result = $this->Form->fields;
  1860. $expected = [
  1861. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1862. 'Addresses.city', 'Addresses.phone'
  1863. ];
  1864. $this->assertEquals($expected, $result);
  1865. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1866. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1867. $expected = [
  1868. 'div' => ['style' => 'display:none;'],
  1869. ['input' => [
  1870. 'type' => 'hidden',
  1871. 'name' => '_Token[fields]',
  1872. 'value' => $hash,
  1873. 'autocomplete' => 'off',
  1874. 'data-foo' => 'bar',
  1875. ]],
  1876. ['input' => [
  1877. 'type' => 'hidden',
  1878. 'name' => '_Token[unlocked]',
  1879. 'value' => 'address%7Cfirst_name',
  1880. 'autocomplete' => 'off',
  1881. 'data-foo' => 'bar',
  1882. ]],
  1883. '/div'
  1884. ];
  1885. $this->assertHtml($expected, $result);
  1886. }
  1887. /**
  1888. * testFormSecureWithCustomNameAttribute method
  1889. *
  1890. * Test securing inputs with custom name attributes.
  1891. *
  1892. * @return void
  1893. */
  1894. public function testFormSecureWithCustomNameAttribute()
  1895. {
  1896. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1897. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1898. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1899. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1900. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1901. }
  1902. /**
  1903. * testFormSecuredControl method
  1904. *
  1905. * Test generation of entire secure form, assertions made on control() output.
  1906. *
  1907. * @return void
  1908. */
  1909. public function testFormSecuredControl()
  1910. {
  1911. $this->Form->request = $this->Form->request
  1912. ->withParam('_Token', 'stuff')
  1913. ->withParam('_csrfToken', 'testKey');
  1914. $this->article['schema'] = [
  1915. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1916. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1917. ];
  1918. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1919. $encoding = strtolower(Configure::read('App.encoding'));
  1920. $expected = [
  1921. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1922. 'div' => ['style' => 'display:none;'],
  1923. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1924. ['input' => [
  1925. 'type' => 'hidden',
  1926. 'name' => '_csrfToken',
  1927. 'value' => 'testKey',
  1928. 'autocomplete' => 'off'
  1929. ]],
  1930. '/div'
  1931. ];
  1932. $this->assertHtml($expected, $result);
  1933. $result = $this->Form->control('ratio');
  1934. $expected = [
  1935. 'div' => ['class'],
  1936. 'label' => ['for'],
  1937. 'Ratio',
  1938. '/label',
  1939. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1940. '/div',
  1941. ];
  1942. $this->assertHtml($expected, $result);
  1943. $result = $this->Form->control('population');
  1944. $expected = [
  1945. 'div' => ['class'],
  1946. 'label' => ['for'],
  1947. 'Population',
  1948. '/label',
  1949. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1950. '/div',
  1951. ];
  1952. $this->assertHtml($expected, $result);
  1953. $result = $this->Form->control('published', ['type' => 'text']);
  1954. $expected = [
  1955. 'div' => ['class' => 'input text'],
  1956. 'label' => ['for' => 'published'],
  1957. 'Published',
  1958. '/label',
  1959. ['input' => [
  1960. 'type' => 'text',
  1961. 'name' => 'published',
  1962. 'id' => 'published'
  1963. ]],
  1964. '/div'
  1965. ];
  1966. $this->assertHtml($expected, $result);
  1967. $result = $this->Form->control('other', ['type' => 'text']);
  1968. $expected = [
  1969. 'div' => ['class' => 'input text'],
  1970. 'label' => ['for' => 'other'],
  1971. 'Other',
  1972. '/label',
  1973. ['input' => [
  1974. 'type' => 'text',
  1975. 'name' => 'other',
  1976. 'id',
  1977. ]],
  1978. '/div'
  1979. ];
  1980. $this->assertHtml($expected, $result);
  1981. $result = $this->Form->hidden('stuff');
  1982. $expected = [
  1983. 'input' => [
  1984. 'type' => 'hidden',
  1985. 'name' => 'stuff'
  1986. ]
  1987. ];
  1988. $this->assertHtml($expected, $result);
  1989. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1990. $expected = ['input' => [
  1991. 'type' => 'hidden',
  1992. 'name' => 'hidden',
  1993. 'value' => '0'
  1994. ]];
  1995. $this->assertHtml($expected, $result);
  1996. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1997. $expected = [
  1998. 'div' => ['class' => 'input checkbox'],
  1999. ['input' => [
  2000. 'type' => 'hidden',
  2001. 'name' => 'something',
  2002. 'value' => '0'
  2003. ]],
  2004. 'label' => ['for' => 'something'],
  2005. ['input' => [
  2006. 'type' => 'checkbox',
  2007. 'name' => 'something',
  2008. 'value' => '1',
  2009. 'id' => 'something'
  2010. ]],
  2011. 'Something',
  2012. '/label',
  2013. '/div'
  2014. ];
  2015. $this->assertHtml($expected, $result);
  2016. $result = $this->Form->fields;
  2017. $expectedFields = [
  2018. 'ratio',
  2019. 'population',
  2020. 'published',
  2021. 'other',
  2022. 'stuff' => '',
  2023. 'hidden' => '0',
  2024. 'something'
  2025. ];
  2026. $this->assertEquals($expectedFields, $result);
  2027. $result = $this->Form->secure($this->Form->fields);
  2028. $tokenDebug = urlencode(json_encode([
  2029. '/articles/add',
  2030. $expectedFields,
  2031. []
  2032. ]));
  2033. $expected = [
  2034. 'div' => ['style' => 'display:none;'],
  2035. ['input' => [
  2036. 'type' => 'hidden',
  2037. 'name' => '_Token[fields]',
  2038. 'value',
  2039. 'autocomplete'
  2040. ]],
  2041. ['input' => [
  2042. 'type' => 'hidden',
  2043. 'name' => '_Token[unlocked]',
  2044. 'value' => '',
  2045. 'autocomplete' => 'off'
  2046. ]],
  2047. ['input' => [
  2048. 'type' => 'hidden', 'name' => '_Token[debug]',
  2049. 'value' => $tokenDebug,
  2050. 'autocomplete' => 'off'
  2051. ]],
  2052. '/div'
  2053. ];
  2054. $this->assertHtml($expected, $result);
  2055. }
  2056. /**
  2057. * testSecuredControlCustomName method
  2058. *
  2059. * Test secured inputs with custom names.
  2060. *
  2061. * @return void
  2062. */
  2063. public function testSecuredControlCustomName()
  2064. {
  2065. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2066. $this->assertEquals([], $this->Form->fields);
  2067. $this->Form->text('text_input', [
  2068. 'name' => 'Option[General.default_role]',
  2069. ]);
  2070. $expected = ['Option.General.default_role'];
  2071. $this->assertEquals($expected, $this->Form->fields);
  2072. $this->Form->select('select_box', [1, 2], [
  2073. 'name' => 'Option[General.select_role]',
  2074. ]);
  2075. $expected[] = 'Option.General.select_role';
  2076. $this->assertEquals($expected, $this->Form->fields);
  2077. $this->Form->text('other.things[]');
  2078. $expected[] = 'other.things';
  2079. $this->assertEquals($expected, $this->Form->fields);
  2080. }
  2081. /**
  2082. * testSecuredControlDuplicate method
  2083. *
  2084. * Test that a hidden field followed by a visible field
  2085. * undoes the hidden field locking.
  2086. *
  2087. * @return void
  2088. */
  2089. public function testSecuredControlDuplicate()
  2090. {
  2091. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2092. $this->assertEquals([], $this->Form->fields);
  2093. $this->Form->control('text_val', [
  2094. 'type' => 'hidden',
  2095. 'value' => 'some text',
  2096. ]);
  2097. $expected = ['text_val' => 'some text'];
  2098. $this->assertEquals($expected, $this->Form->fields);
  2099. $this->Form->control('text_val', [
  2100. 'type' => 'text',
  2101. ]);
  2102. $expected = ['text_val'];
  2103. $this->assertEquals($expected, $this->Form->fields);
  2104. }
  2105. /**
  2106. * testFormSecuredFileControl method
  2107. *
  2108. * Tests that the correct keys are added to the field hash index.
  2109. *
  2110. * @return void
  2111. */
  2112. public function testFormSecuredFileControl()
  2113. {
  2114. $this->assertEquals([], $this->Form->fields);
  2115. $this->Form->file('Attachment.file');
  2116. $expected = [
  2117. 'Attachment.file.name', 'Attachment.file.type',
  2118. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2119. 'Attachment.file.size'
  2120. ];
  2121. $this->assertEquals($expected, $this->Form->fields);
  2122. }
  2123. /**
  2124. * testFormSecuredMultipleSelect method
  2125. *
  2126. * Test that multiple selects keys are added to field hash.
  2127. *
  2128. * @return void
  2129. */
  2130. public function testFormSecuredMultipleSelect()
  2131. {
  2132. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  2133. $this->assertEquals([], $this->Form->fields);
  2134. $options = ['1' => 'one', '2' => 'two'];
  2135. $this->Form->select('Model.select', $options);
  2136. $expected = ['Model.select'];
  2137. $this->assertEquals($expected, $this->Form->fields);
  2138. $this->Form->fields = [];
  2139. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2140. $this->assertEquals($expected, $this->Form->fields);
  2141. }
  2142. /**
  2143. * testFormSecuredRadio method
  2144. *
  2145. * @return void
  2146. */
  2147. public function testFormSecuredRadio()
  2148. {
  2149. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2150. $this->assertEquals([], $this->Form->fields);
  2151. $options = ['1' => 'option1', '2' => 'option2'];
  2152. $this->Form->radio('Test.test', $options);
  2153. $expected = ['Test.test'];
  2154. $this->assertEquals($expected, $this->Form->fields);
  2155. $this->Form->radio('Test.all', $options, [
  2156. 'disabled' => ['option1', 'option2']
  2157. ]);
  2158. $expected = ['Test.test', 'Test.all' => ''];
  2159. $this->assertEquals($expected, $this->Form->fields);
  2160. $this->Form->radio('Test.some', $options, [
  2161. 'disabled' => ['option1']
  2162. ]);
  2163. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2164. $this->assertEquals($expected, $this->Form->fields);
  2165. }
  2166. /**
  2167. * testFormSecuredAndDisabledNotAssoc method
  2168. *
  2169. * Test that when disabled is in a list based attribute array it works.
  2170. *
  2171. * @return void
  2172. */
  2173. public function testFormSecuredAndDisabledNotAssoc()
  2174. {
  2175. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2176. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2177. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2178. $this->Form->text('Model.text', ['disabled']);
  2179. $this->Form->textarea('Model.textarea', ['disabled']);
  2180. $this->Form->password('Model.password', ['disabled']);
  2181. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2182. $expected = [
  2183. 'Model.radio' => ''
  2184. ];
  2185. $this->assertEquals($expected, $this->Form->fields);
  2186. }
  2187. /**
  2188. * testFormSecuredAndDisabled method
  2189. *
  2190. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2191. * hashing.
  2192. *
  2193. * @return void
  2194. */
  2195. public function testFormSecuredAndDisabled()
  2196. {
  2197. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2198. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2199. $this->Form->text('Model.text', ['disabled' => true]);
  2200. $this->Form->password('Model.text', ['disabled' => true]);
  2201. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2202. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2203. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2204. $this->Form->year('Model.year', ['disabled' => true]);
  2205. $this->Form->month('Model.month', ['disabled' => true]);
  2206. $this->Form->day('Model.day', ['disabled' => true]);
  2207. $this->Form->hour('Model.hour', ['disabled' => true]);
  2208. $this->Form->minute('Model.minute', ['disabled' => true]);
  2209. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2210. $expected = [
  2211. 'Model.radio' => ''
  2212. ];
  2213. $this->assertEquals($expected, $this->Form->fields);
  2214. }
  2215. /**
  2216. * testDisableSecurityUsingForm method
  2217. *
  2218. * @return void
  2219. */
  2220. public function testDisableSecurityUsingForm()
  2221. {
  2222. $this->Form->request = $this->Form->request->withParam('_Token', [
  2223. 'disabledFields' => []
  2224. ]);
  2225. $this->Form->create();
  2226. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2227. $this->Form->text('Addresses.title');
  2228. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2229. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2230. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2231. $result = $this->Form->fields;
  2232. $expected = [
  2233. 'Addresses.id' => '123456', 'Addresses.title',
  2234. ];
  2235. $this->assertEquals($expected, $result);
  2236. }
  2237. /**
  2238. * testUnlockFieldAddsToList method
  2239. *
  2240. * Test disableField.
  2241. *
  2242. * @return void
  2243. */
  2244. public function testUnlockFieldAddsToList()
  2245. {
  2246. $this->Form->request = $this->Form->request->withParam('_Token', [
  2247. 'unlockedFields' => []
  2248. ]);
  2249. $this->Form->unlockField('Contact.name');
  2250. $this->Form->text('Contact.name');
  2251. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2252. $this->assertEquals([], $this->Form->fields);
  2253. }
  2254. /**
  2255. * testUnlockFieldRemovingFromFields method
  2256. *
  2257. * Test unlockField removing from fields array.
  2258. *
  2259. * @return void
  2260. */
  2261. public function testUnlockFieldRemovingFromFields()
  2262. {
  2263. $this->Form->request = $this->Form->request->withParam('_Token', [
  2264. 'unlockedFields' => []
  2265. ]);
  2266. $this->Form->create($this->article);
  2267. $this->Form->hidden('Article.id', ['value' => 1]);
  2268. $this->Form->text('Article.title');
  2269. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2270. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2271. $this->Form->unlockField('Article.title');
  2272. $this->Form->unlockField('Article.id');
  2273. $this->assertEquals([], $this->Form->fields);
  2274. }
  2275. /**
  2276. * testResetUnlockFields method
  2277. *
  2278. * Test reset unlockFields, when create new form.
  2279. *
  2280. * @return void
  2281. */
  2282. public function testResetUnlockFields()
  2283. {
  2284. $this->Form->request = $this->Form->request->withParam('_Token', [
  2285. 'key' => 'testKey',
  2286. 'unlockedFields' => []
  2287. ]);
  2288. $this->Form->unlockField('Contact.id');
  2289. $this->Form->create('Contact');
  2290. $this->Form->hidden('Contact.id', ['value' => 1]);
  2291. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2292. $this->Form->end();
  2293. $this->Form->create('Contact');
  2294. $this->Form->hidden('Contact.id', ['value' => 1]);
  2295. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2296. }
  2297. /**
  2298. * testSecuredFormUrlIgnoresHost method
  2299. *
  2300. * Test that only the path + query elements of a form's URL show up in their hash.
  2301. *
  2302. * @return void
  2303. */
  2304. public function testSecuredFormUrlIgnoresHost()
  2305. {
  2306. $this->Form->request = $this->Form->request->withParam('_Token', ['key' => 'testKey']);
  2307. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2308. $this->Form->create($this->article, [
  2309. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2310. ]);
  2311. $result = $this->Form->secure();
  2312. $this->assertContains($expected, $result);
  2313. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2314. $result = $this->Form->secure();
  2315. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2316. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2317. $result = $this->Form->secure();
  2318. $this->assertContains($expected, $result, 'URL path + query should work.');
  2319. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2320. $result = $this->Form->secure();
  2321. $this->assertNotContains($expected, $result, 'URL is different');
  2322. }
  2323. /**
  2324. * testSecuredFormUrlHasHtmlAndIdentifier method
  2325. *
  2326. * Test that URL, HTML and identifier show up in their hashes.
  2327. *
  2328. * @return void
  2329. */
  2330. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2331. {
  2332. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2333. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2334. $this->Form->create($this->article, [
  2335. 'url' => [
  2336. 'controller' => 'articles',
  2337. 'action' => 'view',
  2338. '?' => [
  2339. 'page' => 1,
  2340. 'limit' => 10,
  2341. 'html' => '<>"',
  2342. ],
  2343. '#' => 'result',
  2344. ],
  2345. ]);
  2346. $result = $this->Form->secure();
  2347. $this->assertContains($expected, $result);
  2348. $this->Form->create($this->article, [
  2349. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2350. ]);
  2351. $result = $this->Form->secure();
  2352. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2353. $this->Form->create($this->article, [
  2354. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2355. ]);
  2356. $result = $this->Form->secure();
  2357. $this->assertContains($expected, $result, 'URL path + query should work.');
  2358. }
  2359. /**
  2360. * testErrorMessageDisplay method
  2361. *
  2362. * Test error message display.
  2363. *
  2364. * @return void
  2365. */
  2366. public function testErrorMessageDisplay()
  2367. {
  2368. $this->article['errors'] = [
  2369. 'Article' => [
  2370. 'title' => 'error message',
  2371. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2372. ]
  2373. ];
  2374. $this->Form->create($this->article);
  2375. $result = $this->Form->control('Article.title');
  2376. $expected = [
  2377. 'div' => ['class' => 'input text error'],
  2378. 'label' => ['for' => 'article-title'],
  2379. 'Title',
  2380. '/label',
  2381. 'input' => [
  2382. 'type' => 'text', 'name' => 'Article[title]',
  2383. 'id' => 'article-title', 'class' => 'form-error'
  2384. ],
  2385. ['div' => ['class' => 'error-message']],
  2386. 'error message',
  2387. '/div',
  2388. '/div'
  2389. ];
  2390. $this->assertHtml($expected, $result);
  2391. $result = $this->Form->control('Article.title', [
  2392. 'templates' => [
  2393. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2394. ]
  2395. ]);
  2396. $expected = [
  2397. 'div' => ['class' => 'input text error'],
  2398. 'label' => ['for' => 'article-title'],
  2399. 'Title',
  2400. '/label',
  2401. 'input' => [
  2402. 'type' => 'text', 'name' => 'Article[title]',
  2403. 'id' => 'article-title', 'class' => 'form-error'
  2404. ],
  2405. '/div'
  2406. ];
  2407. $this->assertHtml($expected, $result);
  2408. $result = $this->Form->control('Article.content');
  2409. $expected = [
  2410. 'div' => ['class' => 'input text error'],
  2411. 'label' => ['for' => 'article-content'],
  2412. 'Content',
  2413. '/label',
  2414. 'input' => [
  2415. 'type' => 'text', 'name' => 'Article[content]',
  2416. 'id' => 'article-content', 'class' => 'form-error'
  2417. ],
  2418. ['div' => ['class' => 'error-message']],
  2419. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2420. '/div',
  2421. '/div'
  2422. ];
  2423. $this->assertHtml($expected, $result);
  2424. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2425. $expected = [
  2426. 'div' => ['class' => 'input text error'],
  2427. 'label' => ['for' => 'article-content'],
  2428. 'Content',
  2429. '/label',
  2430. 'input' => [
  2431. 'type' => 'text', 'name' => 'Article[content]',
  2432. 'id' => 'article-content', 'class' => 'form-error'
  2433. ],
  2434. ['div' => ['class' => 'error-message']],
  2435. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2436. '/div',
  2437. '/div'
  2438. ];
  2439. $this->assertHtml($expected, $result);
  2440. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2441. $expected = [
  2442. 'div' => ['class' => 'input text error'],
  2443. 'label' => ['for' => 'article-content'],
  2444. 'Content',
  2445. '/label',
  2446. 'input' => [
  2447. 'type' => 'text', 'name' => 'Article[content]',
  2448. 'id' => 'article-content', 'class' => 'form-error'
  2449. ],
  2450. ['div' => ['class' => 'error-message']],
  2451. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2452. '/div',
  2453. '/div'
  2454. ];
  2455. $this->assertHtml($expected, $result);
  2456. }
  2457. /**
  2458. * testEmptyErrorValidation method
  2459. *
  2460. * Test validation errors, when validation message is an empty string.
  2461. *
  2462. * @return void
  2463. */
  2464. public function testEmptyErrorValidation()
  2465. {
  2466. $this->article['errors'] = [
  2467. 'Article' => ['title' => '']
  2468. ];
  2469. $this->Form->create($this->article);
  2470. $result = $this->Form->control('Article.title');
  2471. $expected = [
  2472. 'div' => ['class' => 'input text error'],
  2473. 'label' => ['for' => 'article-title'],
  2474. 'Title',
  2475. '/label',
  2476. 'input' => [
  2477. 'type' => 'text', 'name' => 'Article[title]',
  2478. 'id' => 'article-title', 'class' => 'form-error'
  2479. ],
  2480. ['div' => ['class' => 'error-message']],
  2481. [],
  2482. '/div',
  2483. '/div'
  2484. ];
  2485. $this->assertHtml($expected, $result);
  2486. }
  2487. /**
  2488. * testEmptyControlErrorValidation method
  2489. *
  2490. * Test validation errors, when calling control() overriding validation message by an empty string.
  2491. *
  2492. * @return void
  2493. */
  2494. public function testEmptyControlErrorValidation()
  2495. {
  2496. $this->article['errors'] = [
  2497. 'Article' => ['title' => 'error message']
  2498. ];
  2499. $this->Form->create($this->article);
  2500. $result = $this->Form->control('Article.title', ['error' => '']);
  2501. $expected = [
  2502. 'div' => ['class' => 'input text error'],
  2503. 'label' => ['for' => 'article-title'],
  2504. 'Title',
  2505. '/label',
  2506. 'input' => [
  2507. 'type' => 'text', 'name' => 'Article[title]',
  2508. 'id' => 'article-title', 'class' => 'form-error'
  2509. ],
  2510. ['div' => ['class' => 'error-message']],
  2511. [],
  2512. '/div',
  2513. '/div'
  2514. ];
  2515. $this->assertHtml($expected, $result);
  2516. }
  2517. /**
  2518. * testControlErrorMessage method
  2519. *
  2520. * Test validation errors, when calling control() overriding validation messages.
  2521. *
  2522. * @return void
  2523. */
  2524. public function testControlErrorMessage()
  2525. {
  2526. $this->article['errors'] = [
  2527. 'title' => ['error message']
  2528. ];
  2529. $this->Form->create($this->article);
  2530. $result = $this->Form->control('title', [
  2531. 'error' => 'Custom error!'
  2532. ]);
  2533. $expected = [
  2534. 'div' => ['class' => 'input text required error'],
  2535. 'label' => ['for' => 'title'],
  2536. 'Title',
  2537. '/label',
  2538. 'input' => [
  2539. 'type' => 'text', 'name' => 'title',
  2540. 'id' => 'title', 'class' => 'form-error',
  2541. 'required' => 'required',
  2542. ],
  2543. ['div' => ['class' => 'error-message']],
  2544. 'Custom error!',
  2545. '/div',
  2546. '/div'
  2547. ];
  2548. $this->assertHtml($expected, $result);
  2549. $result = $this->Form->control('title', [
  2550. 'error' => ['error message' => 'Custom error!']
  2551. ]);
  2552. $expected = [
  2553. 'div' => ['class' => 'input text required error'],
  2554. 'label' => ['for' => 'title'],
  2555. 'Title',
  2556. '/label',
  2557. 'input' => [
  2558. 'type' => 'text',
  2559. 'name' => 'title',
  2560. 'id' => 'title',
  2561. 'class' => 'form-error',
  2562. 'required' => 'required'
  2563. ],
  2564. ['div' => ['class' => 'error-message']],
  2565. 'Custom error!',
  2566. '/div',
  2567. '/div'
  2568. ];
  2569. $this->assertHtml($expected, $result);
  2570. }
  2571. /**
  2572. * testFormValidationAssociated method
  2573. *
  2574. * Tests displaying errors for nested entities.
  2575. *
  2576. * @return void
  2577. */
  2578. public function testFormValidationAssociated()
  2579. {
  2580. $nested = new Entity(['foo' => 'bar']);
  2581. $nested->setError('foo', ['not a valid bar']);
  2582. $entity = new Entity(['nested' => $nested]);
  2583. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2584. $result = $this->Form->error('nested.foo');
  2585. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2586. }
  2587. /**
  2588. * testFormValidationAssociatedSecondLevel method
  2589. *
  2590. * Test form error display with associated model.
  2591. *
  2592. * @return void
  2593. */
  2594. public function testFormValidationAssociatedSecondLevel()
  2595. {
  2596. $inner = new Entity(['bar' => 'baz']);
  2597. $nested = new Entity(['foo' => $inner]);
  2598. $entity = new Entity(['nested' => $nested]);
  2599. $inner->setError('bar', ['not a valid one']);
  2600. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2601. $result = $this->Form->error('nested.foo.bar');
  2602. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2603. }
  2604. /**
  2605. * testFormValidationMultiRecord method
  2606. *
  2607. * Test form error display with multiple records.
  2608. *
  2609. * @return void
  2610. */
  2611. public function testFormValidationMultiRecord()
  2612. {
  2613. $one = new Entity();
  2614. $two = new Entity();
  2615. $this->getTableLocator()->get('Contacts', [
  2616. 'className' => __NAMESPACE__ . '\ContactsTable'
  2617. ]);
  2618. $one->set('email', '');
  2619. $one->setError('email', ['invalid email']);
  2620. $two->set('name', '');
  2621. $two->setError('name', ['This is wrong']);
  2622. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2623. $result = $this->Form->control('0.email');
  2624. $expected = [
  2625. 'div' => ['class' => 'input email error'],
  2626. 'label' => ['for' => '0-email'],
  2627. 'Email',
  2628. '/label',
  2629. 'input' => [
  2630. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2631. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2632. ],
  2633. ['div' => ['class' => 'error-message']],
  2634. 'invalid email',
  2635. '/div',
  2636. '/div'
  2637. ];
  2638. $this->assertHtml($expected, $result);
  2639. $result = $this->Form->control('1.name');
  2640. $expected = [
  2641. 'div' => ['class' => 'input text error'],
  2642. 'label' => ['for' => '1-name'],
  2643. 'Name',
  2644. '/label',
  2645. 'input' => [
  2646. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2647. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2648. ],
  2649. ['div' => ['class' => 'error-message']],
  2650. 'This is wrong',
  2651. '/div',
  2652. '/div'
  2653. ];
  2654. $this->assertHtml($expected, $result);
  2655. }
  2656. /**
  2657. * testControl method
  2658. *
  2659. * Test various incarnations of control().
  2660. *
  2661. * @return void
  2662. */
  2663. public function testControl()
  2664. {
  2665. $this->getTableLocator()->get('ValidateUsers', [
  2666. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2667. ]);
  2668. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2669. $result = $this->Form->control('ValidateUsers.balance');
  2670. $expected = [
  2671. 'div' => ['class'],
  2672. 'label' => ['for'],
  2673. 'Balance',
  2674. '/label',
  2675. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2676. '/div',
  2677. ];
  2678. $this->assertHtml($expected, $result);
  2679. $result = $this->Form->control('ValidateUser.cost_decimal');
  2680. $expected = [
  2681. 'div' => ['class'],
  2682. 'label' => ['for'],
  2683. 'Cost Decimal',
  2684. '/label',
  2685. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2686. '/div',
  2687. ];
  2688. $this->assertHtml($expected, $result);
  2689. $result = $this->Form->control('ValidateUser.null_decimal');
  2690. $expected = [
  2691. 'div' => ['class'],
  2692. 'label' => ['for'],
  2693. 'Null Decimal',
  2694. '/label',
  2695. 'input' => ['name', 'type' => 'number', 'id'],
  2696. '/div',
  2697. ];
  2698. $this->assertHtml($expected, $result);
  2699. }
  2700. /**
  2701. * testControlCustomization method
  2702. *
  2703. * Tests the input method and passing custom options.
  2704. *
  2705. * @return void
  2706. */
  2707. public function testControlCustomization()
  2708. {
  2709. $this->getTableLocator()->get('Contacts', [
  2710. 'className' => __NAMESPACE__ . '\ContactsTable'
  2711. ]);
  2712. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2713. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2714. $expected = [
  2715. 'div' => ['class' => 'input email'],
  2716. 'label' => ['for' => 'custom'],
  2717. 'Email',
  2718. '/label',
  2719. ['input' => [
  2720. 'type' => 'email', 'name' => 'Contact[email]',
  2721. 'id' => 'custom', 'maxlength' => 255
  2722. ]],
  2723. '/div'
  2724. ];
  2725. $this->assertHtml($expected, $result);
  2726. $result = $this->Form->control('Contact.email', [
  2727. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2728. ]);
  2729. $expected = [
  2730. '<div',
  2731. 'label' => ['for' => 'contact-email'],
  2732. 'Email',
  2733. '/label',
  2734. ['input' => [
  2735. 'type' => 'email', 'name' => 'Contact[email]',
  2736. 'id' => 'contact-email', 'maxlength' => 255
  2737. ]],
  2738. '/div'
  2739. ];
  2740. $this->assertHtml($expected, $result);
  2741. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2742. $expected = [
  2743. 'div' => ['class' => 'input text'],
  2744. 'label' => ['for' => 'contact-email'],
  2745. 'Email',
  2746. '/label',
  2747. ['input' => [
  2748. 'type' => 'text', 'name' => 'Contact[email]',
  2749. 'id' => 'contact-email', 'maxlength' => '255'
  2750. ]],
  2751. '/div'
  2752. ];
  2753. $this->assertHtml($expected, $result);
  2754. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2755. $expected = [
  2756. 'div' => ['class' => 'input text'],
  2757. 'label' => ['for' => 'contact-5-email'],
  2758. 'Email',
  2759. '/label',
  2760. ['input' => [
  2761. 'type' => 'text', 'name' => 'Contact[5][email]',
  2762. 'id' => 'contact-5-email', 'maxlength' => '255'
  2763. ]],
  2764. '/div'
  2765. ];
  2766. $this->assertHtml($expected, $result);
  2767. $result = $this->Form->control('Contact.password');
  2768. $expected = [
  2769. 'div' => ['class' => 'input password'],
  2770. 'label' => ['for' => 'contact-password'],
  2771. 'Password',
  2772. '/label',
  2773. ['input' => [
  2774. 'type' => 'password', 'name' => 'Contact[password]',
  2775. 'id' => 'contact-password'
  2776. ]],
  2777. '/div'
  2778. ];
  2779. $this->assertHtml($expected, $result);
  2780. $result = $this->Form->control('Contact.email', [
  2781. 'type' => 'file', 'class' => 'textbox'
  2782. ]);
  2783. $expected = [
  2784. 'div' => ['class' => 'input file'],
  2785. 'label' => ['for' => 'contact-email'],
  2786. 'Email',
  2787. '/label',
  2788. ['input' => [
  2789. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2790. 'id' => 'contact-email'
  2791. ]],
  2792. '/div'
  2793. ];
  2794. $this->assertHtml($expected, $result);
  2795. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2796. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2797. $result = $this->Form->control('phone');
  2798. $expected = [
  2799. 'div' => ['class' => 'input tel'],
  2800. 'label' => ['for' => 'phone'],
  2801. 'Phone',
  2802. '/label',
  2803. ['input' => [
  2804. 'type' => 'tel', 'name' => 'phone',
  2805. 'value' => 'Hello &amp; World &gt; weird chars',
  2806. 'id' => 'phone', 'maxlength' => 255
  2807. ]],
  2808. '/div'
  2809. ];
  2810. $this->assertHtml($expected, $result);
  2811. $this->Form->request = $this->Form->request->withData('Model.0.OtherModel.field', 'My value');
  2812. $this->Form->create();
  2813. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2814. $expected = [
  2815. 'div' => ['class' => 'input text'],
  2816. 'label' => ['for' => 'myId'],
  2817. 'Field',
  2818. '/label',
  2819. 'input' => [
  2820. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2821. 'value' => 'My value', 'id' => 'myId'
  2822. ],
  2823. '/div'
  2824. ];
  2825. $this->assertHtml($expected, $result);
  2826. $this->Form->request = $this->Form->request->withParsedBody([]);
  2827. $this->Form->create();
  2828. $entity->setError('field', 'Badness!');
  2829. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2830. $result = $this->Form->control('field');
  2831. $expected = [
  2832. 'div' => ['class' => 'input text error'],
  2833. 'label' => ['for' => 'field'],
  2834. 'Field',
  2835. '/label',
  2836. 'input' => [
  2837. 'type' => 'text', 'name' => 'field',
  2838. 'id' => 'field', 'class' => 'form-error'
  2839. ],
  2840. ['div' => ['class' => 'error-message']],
  2841. 'Badness!',
  2842. '/div',
  2843. '/div'
  2844. ];
  2845. $this->assertHtml($expected, $result);
  2846. $result = $this->Form->control('field', [
  2847. 'templates' => [
  2848. 'inputContainerError' => '{{content}}{{error}}',
  2849. 'error' => '<span class="error-message">{{content}}</span>'
  2850. ]
  2851. ]);
  2852. $expected = [
  2853. 'label' => ['for' => 'field'],
  2854. 'Field',
  2855. '/label',
  2856. 'input' => [
  2857. 'type' => 'text', 'name' => 'field',
  2858. 'id' => 'field', 'class' => 'form-error'
  2859. ],
  2860. ['span' => ['class' => 'error-message']],
  2861. 'Badness!',
  2862. '/span'
  2863. ];
  2864. $this->assertHtml($expected, $result);
  2865. $entity->setError('field', ['minLength'], true);
  2866. $result = $this->Form->control('field', [
  2867. 'error' => [
  2868. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2869. 'maxLength' => 'login too large'
  2870. ]
  2871. ]);
  2872. $expected = [
  2873. 'div' => ['class' => 'input text error'],
  2874. 'label' => ['for' => 'field'],
  2875. 'Field',
  2876. '/label',
  2877. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2878. ['div' => ['class' => 'error-message']],
  2879. 'Le login doit contenir au moins 2 caractères',
  2880. '/div',
  2881. '/div'
  2882. ];
  2883. $this->assertHtml($expected, $result);
  2884. $entity->setError('field', ['maxLength'], true);
  2885. $result = $this->Form->control('field', [
  2886. 'error' => [
  2887. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2888. 'maxLength' => 'login too large',
  2889. ]
  2890. ]);
  2891. $expected = [
  2892. 'div' => ['class' => 'input text error'],
  2893. 'label' => ['for' => 'field'],
  2894. 'Field',
  2895. '/label',
  2896. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2897. ['div' => ['class' => 'error-message']],
  2898. 'login too large',
  2899. '/div',
  2900. '/div'
  2901. ];
  2902. $this->assertHtml($expected, $result);
  2903. }
  2904. /**
  2905. * testControlWithTemplateFile method
  2906. *
  2907. * Test that control() accepts a template file.
  2908. *
  2909. * @return void
  2910. */
  2911. public function testControlWithTemplateFile()
  2912. {
  2913. $result = $this->Form->control('field', [
  2914. 'templates' => 'htmlhelper_tags'
  2915. ]);
  2916. $expected = [
  2917. 'label' => ['for' => 'field'],
  2918. 'Field',
  2919. '/label',
  2920. 'input' => [
  2921. 'type' => 'text', 'name' => 'field',
  2922. 'id' => 'field'
  2923. ],
  2924. ];
  2925. $this->assertHtml($expected, $result);
  2926. }
  2927. /**
  2928. * testNestedControlsEndWithBrackets method
  2929. *
  2930. * Test that nested inputs end with brackets.
  2931. *
  2932. * @return void
  2933. */
  2934. public function testNestedControlsEndWithBrackets()
  2935. {
  2936. $result = $this->Form->text('nested.text[]');
  2937. $expected = [
  2938. 'input' => [
  2939. 'type' => 'text', 'name' => 'nested[text][]'
  2940. ],
  2941. ];
  2942. $this->assertHtml($expected, $result);
  2943. $result = $this->Form->file('nested.file[]');
  2944. $expected = [
  2945. 'input' => [
  2946. 'type' => 'file', 'name' => 'nested[file][]'
  2947. ],
  2948. ];
  2949. $this->assertHtml($expected, $result);
  2950. }
  2951. /**
  2952. * testCreateIdPrefix method
  2953. *
  2954. * Test id prefix.
  2955. *
  2956. * @return void
  2957. */
  2958. public function testCreateIdPrefix()
  2959. {
  2960. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2961. $result = $this->Form->control('field');
  2962. $expected = [
  2963. 'div' => ['class' => 'input text'],
  2964. 'label' => ['for' => 'prefix-field'],
  2965. 'Field',
  2966. '/label',
  2967. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2968. '/div'
  2969. ];
  2970. $this->assertHtml($expected, $result);
  2971. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2972. $expected = [
  2973. 'div' => ['class' => 'input text'],
  2974. 'label' => ['for' => 'custom-id'],
  2975. 'Field',
  2976. '/label',
  2977. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2978. '/div'
  2979. ];
  2980. $this->assertHtml($expected, $result);
  2981. $result = $this->Form->radio('Model.field', ['option A']);
  2982. $expected = [
  2983. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2984. 'label' => ['for' => 'prefix-model-field-0'],
  2985. ['input' => [
  2986. 'type' => 'radio',
  2987. 'name' => 'Model[field]',
  2988. 'value' => '0',
  2989. 'id' => 'prefix-model-field-0'
  2990. ]],
  2991. 'option A',
  2992. '/label'
  2993. ];
  2994. $this->assertHtml($expected, $result);
  2995. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2996. $expected = [
  2997. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2998. 'label' => ['for' => 'prefix-model-field-0'],
  2999. ['input' => [
  3000. 'type' => 'radio',
  3001. 'name' => 'Model[field]',
  3002. 'value' => '0',
  3003. 'id' => 'prefix-model-field-0'
  3004. ]],
  3005. 'option A',
  3006. '/label'
  3007. ];
  3008. $this->assertHtml($expected, $result);
  3009. $result = $this->Form->select(
  3010. 'Model.multi_field',
  3011. ['first'],
  3012. ['multiple' => 'checkbox']
  3013. );
  3014. $expected = [
  3015. 'input' => [
  3016. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3017. ],
  3018. ['div' => ['class' => 'checkbox']],
  3019. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3020. ['input' => [
  3021. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3022. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  3023. ]],
  3024. 'first',
  3025. '/label',
  3026. '/div',
  3027. ];
  3028. $this->assertHtml($expected, $result);
  3029. $this->Form->end();
  3030. $result = $this->Form->control('field');
  3031. $expected = [
  3032. 'div' => ['class' => 'input text'],
  3033. 'label' => ['for' => 'field'],
  3034. 'Field',
  3035. '/label',
  3036. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3037. '/div'
  3038. ];
  3039. $this->assertHtml($expected, $result);
  3040. }
  3041. /**
  3042. * testControlZero method
  3043. *
  3044. * Test that inputs with 0 can be created.
  3045. *
  3046. * @return void
  3047. */
  3048. public function testControlZero()
  3049. {
  3050. $this->getTableLocator()->get('Contacts', [
  3051. 'className' => __NAMESPACE__ . '\ContactsTable'
  3052. ]);
  3053. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3054. $result = $this->Form->control('0');
  3055. $expected = [
  3056. 'div' => ['class' => 'input text'],
  3057. 'label' => ['for' => '0'], '/label',
  3058. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3059. '/div'
  3060. ];
  3061. $this->assertHtml($expected, $result);
  3062. }
  3063. /**
  3064. * testControlCheckbox method
  3065. *
  3066. * Test control() with checkbox creation.
  3067. *
  3068. * @return void
  3069. */
  3070. public function testControlCheckbox()
  3071. {
  3072. $articles = $this->getTableLocator()->get('Articles');
  3073. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3074. $article = $articles->newEntity();
  3075. $this->Form->create($article);
  3076. $result = $this->Form->control('Articles.active');
  3077. $expected = [
  3078. 'div' => ['class' => 'input checkbox'],
  3079. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3080. 'label' => ['for' => 'articles-active'],
  3081. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3082. 'Active',
  3083. '/label',
  3084. '/div'
  3085. ];
  3086. $this->assertHtml($expected, $result);
  3087. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3088. $expected = [
  3089. 'div' => ['class' => 'input checkbox'],
  3090. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3091. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3092. '/div'
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3096. $expected = [
  3097. 'div' => ['class' => 'input checkbox'],
  3098. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3099. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3100. '/div'
  3101. ];
  3102. $this->assertHtml($expected, $result);
  3103. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3104. $expected = [
  3105. 'div' => ['class' => 'input checkbox'],
  3106. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3107. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3108. '/div'
  3109. ];
  3110. $this->assertHtml($expected, $result);
  3111. $result = $this->Form->control('Articles.disabled', [
  3112. 'label' => 'Disabled',
  3113. 'type' => 'checkbox',
  3114. 'data-foo' => 'disabled'
  3115. ]);
  3116. $expected = [
  3117. 'div' => ['class' => 'input checkbox'],
  3118. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3119. 'label' => ['for' => 'articles-disabled'],
  3120. ['input' => [
  3121. 'type' => 'checkbox',
  3122. 'name' => 'Articles[disabled]',
  3123. 'value' => '1',
  3124. 'id' => 'articles-disabled',
  3125. 'data-foo' => 'disabled'
  3126. ]],
  3127. 'Disabled',
  3128. '/label',
  3129. '/div'
  3130. ];
  3131. $this->assertHtml($expected, $result);
  3132. $result = $this->Form->control('Articles.confirm', [
  3133. 'label' => 'Confirm <b>me</b>!',
  3134. 'type' => 'checkbox',
  3135. 'escape' => false
  3136. ]);
  3137. $expected = [
  3138. 'div' => ['class' => 'input checkbox'],
  3139. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3140. 'label' => ['for' => 'articles-confirm'],
  3141. ['input' => [
  3142. 'type' => 'checkbox',
  3143. 'name' => 'Articles[confirm]',
  3144. 'value' => '1',
  3145. 'id' => 'articles-confirm',
  3146. ]],
  3147. 'Confirm <b>me</b>!',
  3148. '/label',
  3149. '/div'
  3150. ];
  3151. $this->assertHtml($expected, $result);
  3152. }
  3153. /**
  3154. * testControlHidden method
  3155. *
  3156. * Test that control() does not create wrapping div and label tag for hidden fields.
  3157. *
  3158. * @return void
  3159. */
  3160. public function testControlHidden()
  3161. {
  3162. $this->getTableLocator()->get('ValidateUsers', [
  3163. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3164. ]);
  3165. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3166. $result = $this->Form->control('ValidateUser.id');
  3167. $expected = [
  3168. 'input' => ['name', 'type' => 'hidden', 'id']
  3169. ];
  3170. $this->assertHtml($expected, $result);
  3171. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3172. $expected = [
  3173. 'input' => ['name', 'type' => 'hidden', 'id']
  3174. ];
  3175. $this->assertHtml($expected, $result);
  3176. }
  3177. /**
  3178. * testControlDatetime method
  3179. *
  3180. * Test form->control() with datetime.
  3181. *
  3182. * @return void
  3183. */
  3184. public function testControlDatetime()
  3185. {
  3186. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3187. ->setMethods(['datetime'])
  3188. ->setConstructorArgs([new View()])
  3189. ->getMock();
  3190. $this->Form->expects($this->once())->method('datetime')
  3191. ->with('prueba', [
  3192. 'type' => 'datetime',
  3193. 'timeFormat' => 24,
  3194. 'minYear' => 2008,
  3195. 'maxYear' => 2011,
  3196. 'interval' => 15,
  3197. 'options' => null,
  3198. 'empty' => false,
  3199. 'id' => 'prueba',
  3200. 'required' => false,
  3201. 'templateVars' => []
  3202. ])
  3203. ->will($this->returnValue('This is it!'));
  3204. $result = $this->Form->control('prueba', [
  3205. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3206. 'maxYear' => 2011, 'interval' => 15
  3207. ]);
  3208. $expected = [
  3209. 'div' => ['class' => 'input datetime'],
  3210. '<label',
  3211. 'Prueba',
  3212. '/label',
  3213. 'This is it!',
  3214. '/div'
  3215. ];
  3216. $this->assertHtml($expected, $result);
  3217. }
  3218. /**
  3219. * testControlDatetimeIdPrefix method
  3220. *
  3221. * Test form->control() with datetime with id prefix.
  3222. *
  3223. * @return void
  3224. */
  3225. public function testControlDatetimeIdPrefix()
  3226. {
  3227. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3228. ->setMethods(['datetime'])
  3229. ->setConstructorArgs([new View()])
  3230. ->getMock();
  3231. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3232. $this->Form->expects($this->once())->method('datetime')
  3233. ->with('prueba', [
  3234. 'type' => 'datetime',
  3235. 'timeFormat' => 24,
  3236. 'minYear' => 2008,
  3237. 'maxYear' => 2011,
  3238. 'interval' => 15,
  3239. 'options' => null,
  3240. 'empty' => false,
  3241. 'id' => 'prefix-prueba',
  3242. 'required' => false,
  3243. 'templateVars' => []
  3244. ])
  3245. ->will($this->returnValue('This is it!'));
  3246. $result = $this->Form->control('prueba', [
  3247. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3248. 'maxYear' => 2011, 'interval' => 15
  3249. ]);
  3250. $expected = [
  3251. 'div' => ['class' => 'input datetime'],
  3252. '<label',
  3253. 'Prueba',
  3254. '/label',
  3255. 'This is it!',
  3256. '/div'
  3257. ];
  3258. $this->assertHtml($expected, $result);
  3259. }
  3260. /**
  3261. * testControlCheckboxWithDisabledElements method
  3262. *
  3263. * Test generating checkboxes with disabled elements.
  3264. *
  3265. * @return void
  3266. */
  3267. public function testControlCheckboxWithDisabledElements()
  3268. {
  3269. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3270. $result = $this->Form->control('Contact.multiple', [
  3271. 'multiple' => 'checkbox',
  3272. 'disabled' => 'disabled',
  3273. 'options' => $options
  3274. ]);
  3275. $expected = [
  3276. ['div' => ['class' => 'input select']],
  3277. ['label' => ['for' => 'contact-multiple']],
  3278. 'Multiple',
  3279. '/label',
  3280. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3281. ['div' => ['class' => 'checkbox']],
  3282. ['label' => ['for' => 'contact-multiple-1']],
  3283. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3284. 'One',
  3285. '/label',
  3286. '/div',
  3287. ['div' => ['class' => 'checkbox']],
  3288. ['label' => ['for' => 'contact-multiple-2']],
  3289. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3290. 'Two',
  3291. '/label',
  3292. '/div',
  3293. ['div' => ['class' => 'checkbox']],
  3294. ['label' => ['for' => 'contact-multiple-3']],
  3295. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3296. 'Three',
  3297. '/label',
  3298. '/div',
  3299. '/div'
  3300. ];
  3301. $this->assertHtml($expected, $result);
  3302. // make sure 50 does only disable 50, and not 50f5c0cf
  3303. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3304. $disabled = [50];
  3305. $expected = [
  3306. ['div' => ['class' => 'input select']],
  3307. ['label' => ['for' => 'contact-multiple']],
  3308. 'Multiple',
  3309. '/label',
  3310. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3311. ['div' => ['class' => 'checkbox']],
  3312. ['label' => ['for' => 'contact-multiple-50']],
  3313. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3314. 'Fifty',
  3315. '/label',
  3316. '/div',
  3317. ['div' => ['class' => 'checkbox']],
  3318. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3319. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3320. 'Stringy',
  3321. '/label',
  3322. '/div',
  3323. '/div'
  3324. ];
  3325. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3326. $this->assertHtml($expected, $result);
  3327. }
  3328. /**
  3329. * testControlWithLeadingInteger method
  3330. *
  3331. * Test input name with leading integer, ensure attributes are generated correctly.
  3332. *
  3333. * @return void
  3334. */
  3335. public function testControlWithLeadingInteger()
  3336. {
  3337. $result = $this->Form->text('0.Node.title');
  3338. $expected = [
  3339. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3340. ];
  3341. $this->assertHtml($expected, $result);
  3342. }
  3343. /**
  3344. * testControlSelectType method
  3345. *
  3346. * Test form->control() with select type inputs.
  3347. *
  3348. * @return void
  3349. */
  3350. public function testControlSelectType()
  3351. {
  3352. $result = $this->Form->control(
  3353. 'email',
  3354. [
  3355. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3356. );
  3357. $expected = [
  3358. 'div' => ['class' => 'input select'],
  3359. 'label' => ['for' => 'email'],
  3360. 'Email',
  3361. '/label',
  3362. ['select' => ['name' => 'email', 'id' => 'email']],
  3363. ['option' => ['value' => '']],
  3364. '/option',
  3365. ['option' => ['value' => 'è']],
  3366. 'Firést',
  3367. '/option',
  3368. ['option' => ['value' => 'é']],
  3369. 'Secoènd',
  3370. '/option',
  3371. '/select',
  3372. '/div'
  3373. ];
  3374. $this->assertHtml($expected, $result);
  3375. $result = $this->Form->control(
  3376. 'email',
  3377. [
  3378. 'options' => ['First', 'Second'], 'empty' => true]
  3379. );
  3380. $expected = [
  3381. 'div' => ['class' => 'input select'],
  3382. 'label' => ['for' => 'email'],
  3383. 'Email',
  3384. '/label',
  3385. ['select' => ['name' => 'email', 'id' => 'email']],
  3386. ['option' => ['value' => '']],
  3387. '/option',
  3388. ['option' => ['value' => '0']],
  3389. 'First',
  3390. '/option',
  3391. ['option' => ['value' => '1']],
  3392. 'Second',
  3393. '/option',
  3394. '/select',
  3395. '/div'
  3396. ];
  3397. $this->assertHtml($expected, $result);
  3398. $result = $this->Form->control('email', [
  3399. 'type' => 'select',
  3400. 'options' => new \ArrayObject(['First', 'Second']),
  3401. 'empty' => true
  3402. ]);
  3403. $this->assertHtml($expected, $result);
  3404. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3405. $this->Form->request = $this->Form->request->withData('Model', ['user_id' => 'value']);
  3406. $this->Form->create();
  3407. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3408. $expected = [
  3409. 'div' => ['class' => 'input select'],
  3410. 'label' => ['for' => 'model-user-id'],
  3411. 'User',
  3412. '/label',
  3413. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3414. ['option' => ['value' => '']],
  3415. '/option',
  3416. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3417. 'good',
  3418. '/option',
  3419. ['option' => ['value' => 'other']],
  3420. 'bad',
  3421. '/option',
  3422. '/select',
  3423. '/div'
  3424. ];
  3425. $this->assertHtml($expected, $result);
  3426. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3427. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => null]);
  3428. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3429. $expected = [
  3430. 'div' => ['class' => 'input select'],
  3431. 'label' => ['for' => 'thing-user-id'],
  3432. 'User',
  3433. '/label',
  3434. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3435. ['option' => ['value' => '']],
  3436. 'Some Empty',
  3437. '/option',
  3438. ['option' => ['value' => 'value']],
  3439. 'good',
  3440. '/option',
  3441. ['option' => ['value' => 'other']],
  3442. 'bad',
  3443. '/option',
  3444. '/select',
  3445. '/div'
  3446. ];
  3447. $this->assertHtml($expected, $result);
  3448. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3449. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => 'value']);
  3450. $this->Form->create();
  3451. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3452. $expected = [
  3453. 'div' => ['class' => 'input select'],
  3454. 'label' => ['for' => 'thing-user-id'],
  3455. 'User',
  3456. '/label',
  3457. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3458. ['option' => ['value' => '']],
  3459. 'Some Empty',
  3460. '/option',
  3461. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3462. 'good',
  3463. '/option',
  3464. ['option' => ['value' => 'other']],
  3465. 'bad',
  3466. '/option',
  3467. '/select',
  3468. '/div'
  3469. ];
  3470. $this->assertHtml($expected, $result);
  3471. $result = $this->Form->control('Publisher.id', [
  3472. 'label' => 'Publisher',
  3473. 'type' => 'select',
  3474. 'multiple' => 'checkbox',
  3475. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3476. ]);
  3477. $expected = [
  3478. ['div' => ['class' => 'input select']],
  3479. ['label' => ['for' => 'publisher-id']],
  3480. 'Publisher',
  3481. '/label',
  3482. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3483. ['div' => ['class' => 'checkbox']],
  3484. ['label' => ['for' => 'publisher-id-value-1']],
  3485. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3486. 'Label 1',
  3487. '/label',
  3488. '/div',
  3489. ['div' => ['class' => 'checkbox']],
  3490. ['label' => ['for' => 'publisher-id-value-2']],
  3491. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3492. 'Label 2',
  3493. '/label',
  3494. '/div',
  3495. '/div'
  3496. ];
  3497. $this->assertHtml($expected, $result);
  3498. }
  3499. /**
  3500. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3501. *
  3502. * Test that control() and a non standard primary key makes a hidden input by default.
  3503. *
  3504. * @return void
  3505. */
  3506. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3507. {
  3508. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3509. $this->Form->create($this->article);
  3510. $result = $this->Form->control('title');
  3511. $expected = [
  3512. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3513. ];
  3514. $this->assertHtml($expected, $result);
  3515. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3516. $this->Form->create($this->article);
  3517. $result = $this->Form->control('title');
  3518. $expected = [
  3519. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3520. ];
  3521. $this->assertHtml($expected, $result);
  3522. $result = $this->Form->control('body');
  3523. $expected = [
  3524. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3525. ];
  3526. $this->assertHtml($expected, $result);
  3527. }
  3528. /**
  3529. * testControlOverridingMagicSelectType method
  3530. *
  3531. * Test that overriding the magic select type widget is possible.
  3532. *
  3533. * @return void
  3534. */
  3535. public function testControlOverridingMagicSelectType()
  3536. {
  3537. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3538. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3539. $expected = [
  3540. 'div' => ['class' => 'input text'],
  3541. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3542. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3543. '/div'
  3544. ];
  3545. $this->assertHtml($expected, $result);
  3546. //Check that magic types still work for plural/singular vars
  3547. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3548. $result = $this->Form->control('Model.type');
  3549. $expected = [
  3550. 'div' => ['class' => 'input select'],
  3551. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3552. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3553. ['option' => ['value' => 'value']], 'good', '/option',
  3554. ['option' => ['value' => 'other']], 'bad', '/option',
  3555. '/select',
  3556. '/div'
  3557. ];
  3558. $this->assertHtml($expected, $result);
  3559. }
  3560. /**
  3561. * testControlMagicTypeDoesNotOverride method
  3562. *
  3563. * Test that inferred types do not override developer input.
  3564. *
  3565. * @return void
  3566. */
  3567. public function testControlMagicTypeDoesNotOverride()
  3568. {
  3569. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3570. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3571. $expected = [
  3572. 'div' => ['class' => 'input checkbox'],
  3573. ['input' => [
  3574. 'type' => 'hidden',
  3575. 'name' => 'Model[user]',
  3576. 'value' => 0,
  3577. ]],
  3578. 'label' => ['for' => 'model-user'],
  3579. ['input' => [
  3580. 'name' => 'Model[user]',
  3581. 'type' => 'checkbox',
  3582. 'id' => 'model-user',
  3583. 'value' => 1
  3584. ]],
  3585. 'User',
  3586. '/label',
  3587. '/div'
  3588. ];
  3589. $this->assertHtml($expected, $result);
  3590. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3591. $options = [
  3592. 1 => 'blue',
  3593. 2 => 'red'
  3594. ];
  3595. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3596. $expected = [
  3597. 'div' => ['class' => 'input select'],
  3598. 'label' => ['for' => 'tags-ids'],
  3599. 'Tags',
  3600. '/label',
  3601. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3602. ['div' => ['class' => 'checkbox']],
  3603. ['label' => ['for' => 'tags-ids-1']],
  3604. ['input' => [
  3605. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3606. 'value' => '1', 'name' => 'tags[_ids][]'
  3607. ]],
  3608. 'blue',
  3609. '/label',
  3610. '/div',
  3611. ['div' => ['class' => 'checkbox']],
  3612. ['label' => ['for' => 'tags-ids-2']],
  3613. ['input' => [
  3614. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3615. 'value' => '2', 'name' => 'tags[_ids][]'
  3616. ]],
  3617. 'red',
  3618. '/label',
  3619. '/div',
  3620. '/div'
  3621. ];
  3622. $this->assertHtml($expected, $result);
  3623. }
  3624. /**
  3625. * testControlMagicSelectForTypeNumber method
  3626. *
  3627. * Test that magic control() selects are created for type=number.
  3628. *
  3629. * @return void
  3630. */
  3631. public function testControlMagicSelectForTypeNumber()
  3632. {
  3633. $this->getTableLocator()->get('ValidateUsers', [
  3634. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3635. ]);
  3636. $entity = new Entity(['balance' => 1]);
  3637. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3638. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3639. $result = $this->Form->control('balance');
  3640. $expected = [
  3641. 'div' => ['class' => 'input select'],
  3642. 'label' => ['for' => 'balance'],
  3643. 'Balance',
  3644. '/label',
  3645. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3646. ['option' => ['value' => '0']],
  3647. 'nothing',
  3648. '/option',
  3649. ['option' => ['value' => '1', 'selected' => 'selected']],
  3650. 'some',
  3651. '/option',
  3652. ['option' => ['value' => '100']],
  3653. 'a lot',
  3654. '/option',
  3655. '/select',
  3656. '/div'
  3657. ];
  3658. $this->assertHtml($expected, $result);
  3659. }
  3660. /**
  3661. * testInvalidControlTypeOption method
  3662. *
  3663. * Test invalid 'input' type option to control() function.
  3664. *
  3665. * @return void
  3666. */
  3667. public function testInvalidControlTypeOption()
  3668. {
  3669. $this->expectException(\RuntimeException::class);
  3670. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3671. $this->Form->control('text', ['type' => 'input']);
  3672. }
  3673. /**
  3674. * testControlMagicSelectChangeToRadio method
  3675. *
  3676. * Test that magic control() selects can easily be converted into radio types without error.
  3677. *
  3678. * @return void
  3679. */
  3680. public function testControlMagicSelectChangeToRadio()
  3681. {
  3682. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3683. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3684. $this->assertContains('input type="radio"', $result);
  3685. }
  3686. /**
  3687. * testFormControlSubmit method
  3688. *
  3689. * Test correct results for form::control() and type submit.
  3690. *
  3691. * @return void
  3692. */
  3693. public function testFormControlSubmit()
  3694. {
  3695. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3696. $expected = [
  3697. 'div' => ['class' => 'submit'],
  3698. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3699. '/div'
  3700. ];
  3701. $this->assertHtml($expected, $result);
  3702. }
  3703. /**
  3704. * testFormControls method
  3705. *
  3706. * Test correct results from Form::controls().
  3707. *
  3708. * @return void
  3709. */
  3710. public function testFormControlsLegendFieldset()
  3711. {
  3712. $this->Form->create($this->article);
  3713. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3714. $expected = [
  3715. '<fieldset',
  3716. '<legend',
  3717. 'The Legend',
  3718. '/legend',
  3719. '*/fieldset',
  3720. ];
  3721. $this->assertHtml($expected, $result);
  3722. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3723. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3724. $this->assertContains('<fieldset>', $result);
  3725. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3726. $this->assertNotContains('<legend>', $result);
  3727. $this->assertNotContains('<fieldset>', $result);
  3728. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3729. $this->assertNotContains('<legend>', $result);
  3730. $this->assertNotContains('<fieldset>', $result);
  3731. $this->Form->create($this->article);
  3732. $this->Form->request = $this->Form->request
  3733. ->withParam('prefix', 'admin')
  3734. ->withParam('action', 'admin_edit')
  3735. ->withParam('controller', 'articles');
  3736. $result = $this->Form->allControls();
  3737. $expected = [
  3738. '<fieldset',
  3739. '<legend',
  3740. 'New Article',
  3741. '/legend',
  3742. '*/fieldset',
  3743. ];
  3744. $this->assertHtml($expected, $result);
  3745. $this->Form->create($this->article);
  3746. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3747. $expected = [
  3748. '<fieldset',
  3749. '<legend',
  3750. 'The Legend',
  3751. '/legend',
  3752. '*/fieldset',
  3753. ];
  3754. $this->assertHtml($expected, $result);
  3755. $this->Form->create($this->article);
  3756. $result = $this->Form->allControls([], [
  3757. 'fieldset' => [
  3758. 'class' => 'some-class some-other-class',
  3759. 'disabled' => true,
  3760. 'data-param' => 'a-param'
  3761. ],
  3762. 'legend' => 'The Legend'
  3763. ]);
  3764. $expected = [
  3765. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3766. '<legend',
  3767. 'The Legend',
  3768. '/legend',
  3769. '*/fieldset',
  3770. ];
  3771. $this->assertHtml($expected, $result);
  3772. }
  3773. /**
  3774. * testFormControls method
  3775. *
  3776. * Test the controls() method.
  3777. *
  3778. * @return void
  3779. */
  3780. public function testFormControls()
  3781. {
  3782. $this->Form->create($this->article);
  3783. $result = $this->Form->allControls();
  3784. $expected = [
  3785. '<fieldset',
  3786. '<legend', 'New Article', '/legend',
  3787. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3788. ['div' => ['class' => 'input select required']],
  3789. '*/div',
  3790. ['div' => ['class' => 'input text required']],
  3791. '*/div',
  3792. ['div' => ['class' => 'input text']],
  3793. '*/div',
  3794. ['div' => ['class' => 'input text']],
  3795. '*/div',
  3796. '/fieldset',
  3797. ];
  3798. $this->assertHtml($expected, $result);
  3799. $result = $this->Form->allControls([
  3800. 'published' => ['type' => 'boolean']
  3801. ]);
  3802. $expected = [
  3803. '<fieldset',
  3804. '<legend', 'New Article', '/legend',
  3805. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3806. ['div' => ['class' => 'input select required']],
  3807. '*/div',
  3808. ['div' => ['class' => 'input text required']],
  3809. '*/div',
  3810. ['div' => ['class' => 'input text']],
  3811. '*/div',
  3812. ['div' => ['class' => 'input boolean']],
  3813. '*/div',
  3814. '/fieldset',
  3815. ];
  3816. $this->assertHtml($expected, $result);
  3817. $this->Form->create($this->article);
  3818. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3819. $expected = [
  3820. 'fieldset' => [],
  3821. 'legend' => [],
  3822. 'Hello',
  3823. '/legend',
  3824. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3825. ['div' => ['class' => 'input select required']],
  3826. '*/div',
  3827. ['div' => ['class' => 'input text required']],
  3828. '*/div',
  3829. ['div' => ['class' => 'input text']],
  3830. '*/div',
  3831. ['div' => ['class' => 'input text']],
  3832. '*/div',
  3833. '/fieldset'
  3834. ];
  3835. $this->assertHtml($expected, $result);
  3836. $this->Form->create(false);
  3837. $expected = [
  3838. 'fieldset' => [],
  3839. ['div' => ['class' => 'input text']],
  3840. 'label' => ['for' => 'foo'],
  3841. 'Foo',
  3842. '/label',
  3843. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3844. '*/div',
  3845. '/fieldset'
  3846. ];
  3847. $result = $this->Form->allControls(
  3848. ['foo' => ['type' => 'text']],
  3849. ['legend' => false]
  3850. );
  3851. $this->assertHtml($expected, $result);
  3852. }
  3853. /**
  3854. * testFormControlsBlacklist method
  3855. *
  3856. * @return void
  3857. */
  3858. public function testFormControlsBlacklist()
  3859. {
  3860. $this->Form->create($this->article);
  3861. $result = $this->Form->allControls([
  3862. 'id' => false
  3863. ]);
  3864. $expected = [
  3865. '<fieldset',
  3866. '<legend', 'New Article', '/legend',
  3867. ['div' => ['class' => 'input select required']],
  3868. '*/div',
  3869. ['div' => ['class' => 'input text required']],
  3870. '*/div',
  3871. ['div' => ['class' => 'input text']],
  3872. '*/div',
  3873. ['div' => ['class' => 'input text']],
  3874. '*/div',
  3875. '/fieldset',
  3876. ];
  3877. $this->assertHtml($expected, $result);
  3878. $this->Form->create($this->article);
  3879. $result = $this->Form->allControls([
  3880. 'id' => []
  3881. ]);
  3882. $expected = [
  3883. '<fieldset',
  3884. '<legend', 'New Article', '/legend',
  3885. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3886. ['div' => ['class' => 'input select required']],
  3887. '*/div',
  3888. ['div' => ['class' => 'input text required']],
  3889. '*/div',
  3890. ['div' => ['class' => 'input text']],
  3891. '*/div',
  3892. ['div' => ['class' => 'input text']],
  3893. '*/div',
  3894. '/fieldset',
  3895. ];
  3896. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3897. }
  3898. /**
  3899. * testSelectAsCheckbox method
  3900. *
  3901. * Test multi-select widget with checkbox formatting.
  3902. *
  3903. * @return void
  3904. */
  3905. public function testSelectAsCheckbox()
  3906. {
  3907. $result = $this->Form->select(
  3908. 'Model.multi_field',
  3909. ['first', 'second', 'third'],
  3910. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3911. );
  3912. $expected = [
  3913. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3914. ['div' => ['class' => 'checkbox']],
  3915. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3916. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3917. 'first',
  3918. '/label',
  3919. '/div',
  3920. ['div' => ['class' => 'checkbox']],
  3921. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3922. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3923. 'second',
  3924. '/label',
  3925. '/div',
  3926. ['div' => ['class' => 'checkbox']],
  3927. ['label' => ['for' => 'model-multi-field-2']],
  3928. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3929. 'third',
  3930. '/label',
  3931. '/div',
  3932. ];
  3933. $this->assertHtml($expected, $result);
  3934. $result = $this->Form->select(
  3935. 'Model.multi_field',
  3936. ['1/2' => 'half'],
  3937. ['multiple' => 'checkbox']
  3938. );
  3939. $expected = [
  3940. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3941. ['div' => ['class' => 'checkbox']],
  3942. ['label' => ['for' => 'model-multi-field-1-2']],
  3943. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3944. 'half',
  3945. '/label',
  3946. '/div',
  3947. ];
  3948. $this->assertHtml($expected, $result);
  3949. }
  3950. /**
  3951. * testLabel method
  3952. *
  3953. * Test label generation.
  3954. *
  3955. * @return void
  3956. */
  3957. public function testLabel()
  3958. {
  3959. $result = $this->Form->label('Person.name');
  3960. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3961. $this->assertHtml($expected, $result);
  3962. $result = $this->Form->label('Person.name');
  3963. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3964. $this->assertHtml($expected, $result);
  3965. $result = $this->Form->label('Person.first_name');
  3966. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3967. $this->assertHtml($expected, $result);
  3968. $result = $this->Form->label('Person.first_name', 'Your first name');
  3969. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3970. $this->assertHtml($expected, $result);
  3971. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3972. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3973. $this->assertHtml($expected, $result);
  3974. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3975. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3976. $this->assertHtml($expected, $result);
  3977. $result = $this->Form->label('Person.first_name', '');
  3978. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3979. $this->assertHtml($expected, $result);
  3980. $result = $this->Form->label('Person.2.name', '');
  3981. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3982. $this->assertHtml($expected, $result);
  3983. }
  3984. /**
  3985. * testLabelContainControl method
  3986. *
  3987. * Test that label() can accept an input with the correct template vars.
  3988. *
  3989. * @return void
  3990. */
  3991. public function testLabelContainControl()
  3992. {
  3993. $this->Form->setTemplates([
  3994. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3995. ]);
  3996. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3997. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3998. ]);
  3999. $expected = [
  4000. 'label' => ['for' => 'person-accept-terms'],
  4001. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4002. 'Accept',
  4003. '/label',
  4004. ];
  4005. $this->assertHtml($expected, $result);
  4006. }
  4007. /**
  4008. * testTextbox method
  4009. *
  4010. * Test textbox element generation.
  4011. *
  4012. * @return void
  4013. */
  4014. public function testTextbox()
  4015. {
  4016. $result = $this->Form->text('Model.field');
  4017. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4018. $this->assertHtml($expected, $result);
  4019. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4020. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4021. $this->assertHtml($expected, $result);
  4022. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4023. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4024. $this->assertHtml($expected, $result);
  4025. }
  4026. /**
  4027. * testTextBoxDataAndError method
  4028. *
  4029. * Test that text() hooks up with request data and error fields.
  4030. *
  4031. * @return void
  4032. */
  4033. public function testTextBoxDataAndError()
  4034. {
  4035. $this->article['errors'] = [
  4036. 'Contact' => ['text' => 'wrong']
  4037. ];
  4038. $this->Form->request = $this->Form->request
  4039. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4040. ->withData('Contact.text', 'test');
  4041. $this->Form->create($this->article);
  4042. $result = $this->Form->text('Model.text');
  4043. $expected = [
  4044. 'input' => [
  4045. 'type' => 'text',
  4046. 'name' => 'Model[text]',
  4047. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4048. ]
  4049. ];
  4050. $this->assertHtml($expected, $result);
  4051. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4052. $expected = [
  4053. 'input' => [
  4054. 'type' => 'text',
  4055. 'name' => 'Contact[text]',
  4056. 'value' => 'test',
  4057. 'id' => 'theID',
  4058. 'class' => 'form-error'
  4059. ]
  4060. ];
  4061. $this->assertHtml($expected, $result);
  4062. }
  4063. /**
  4064. * testDefaultValue method
  4065. *
  4066. * Test default value setting.
  4067. *
  4068. * @return void
  4069. */
  4070. public function testTextDefaultValue()
  4071. {
  4072. $this->Form->request = $this->Form->request->withData('Model.field', 'test');
  4073. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4074. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4075. $this->assertHtml($expected, $result);
  4076. $this->Form->request = $this->Form->request->withParsedBody([]);
  4077. $this->Form->create();
  4078. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4079. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4080. $this->assertHtml($expected, $result);
  4081. $Articles = $this->getTableLocator()->get('Articles');
  4082. $title = $Articles->getSchema()->getColumn('title');
  4083. $Articles->getSchema()->addColumn(
  4084. 'title',
  4085. ['default' => 'default title'] + $title
  4086. );
  4087. $entity = $Articles->newEntity();
  4088. $this->Form->create($entity);
  4089. // Get default value from schema
  4090. $result = $this->Form->text('title');
  4091. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4092. $this->assertHtml($expected, $result);
  4093. // Don't get value from schema
  4094. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4095. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4096. $this->assertHtml($expected, $result);
  4097. // Custom default value overrides default value from schema
  4098. $result = $this->Form->text('title', ['default' => 'override default']);
  4099. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4100. $this->assertHtml($expected, $result);
  4101. // Default value from schema is used only for new entities.
  4102. $entity->isNew(false);
  4103. $result = $this->Form->text('title');
  4104. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4105. $this->assertHtml($expected, $result);
  4106. }
  4107. /**
  4108. * testError method
  4109. *
  4110. * Test field error generation.
  4111. *
  4112. * @return void
  4113. */
  4114. public function testError()
  4115. {
  4116. $this->article['errors'] = [
  4117. 'Article' => ['field' => 'email']
  4118. ];
  4119. $this->Form->create($this->article);
  4120. $result = $this->Form->error('Article.field');
  4121. $expected = [
  4122. ['div' => ['class' => 'error-message']],
  4123. 'email',
  4124. '/div',
  4125. ];
  4126. $this->assertHtml($expected, $result);
  4127. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4128. $expected = [
  4129. ['div' => ['class' => 'error-message']],
  4130. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4131. '/div',
  4132. ];
  4133. $this->assertHtml($expected, $result);
  4134. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4135. $expected = [
  4136. ['div' => ['class' => 'error-message']],
  4137. '<strong', 'Badness!', '/strong',
  4138. '/div',
  4139. ];
  4140. $this->assertHtml($expected, $result);
  4141. }
  4142. /**
  4143. * testErrorRuleName method
  4144. *
  4145. * Test error translation can use rule names for translating.
  4146. *
  4147. * @return void
  4148. */
  4149. public function testErrorRuleName()
  4150. {
  4151. $this->article['errors'] = [
  4152. 'Article' => [
  4153. 'field' => ['email' => 'Your email was not good']
  4154. ]
  4155. ];
  4156. $this->Form->create($this->article);
  4157. $result = $this->Form->error('Article.field');
  4158. $expected = [
  4159. ['div' => ['class' => 'error-message']],
  4160. 'Your email was not good',
  4161. '/div',
  4162. ];
  4163. $this->assertHtml($expected, $result);
  4164. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4165. $expected = [
  4166. ['div' => ['class' => 'error-message']],
  4167. 'Email in use',
  4168. '/div',
  4169. ];
  4170. $this->assertHtml($expected, $result);
  4171. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4172. $expected = [
  4173. ['div' => ['class' => 'error-message']],
  4174. 'Email in use',
  4175. '/div',
  4176. ];
  4177. $this->assertHtml($expected, $result);
  4178. $result = $this->Form->error('Article.field', [
  4179. 'email' => 'Key is preferred',
  4180. 'Your email was not good' => 'Email in use'
  4181. ]);
  4182. $expected = [
  4183. ['div' => ['class' => 'error-message']],
  4184. 'Key is preferred',
  4185. '/div',
  4186. ];
  4187. $this->assertHtml($expected, $result);
  4188. }
  4189. /**
  4190. * testErrorMessages method
  4191. *
  4192. * Test error with nested lists.
  4193. *
  4194. * @return void
  4195. */
  4196. public function testErrorMessages()
  4197. {
  4198. $this->article['errors'] = [
  4199. 'Article' => ['field' => 'email']
  4200. ];
  4201. $this->Form->create($this->article);
  4202. $result = $this->Form->error('Article.field', [
  4203. 'email' => 'No good!'
  4204. ]);
  4205. $expected = [
  4206. 'div' => ['class' => 'error-message'],
  4207. 'No good!',
  4208. '/div'
  4209. ];
  4210. $this->assertHtml($expected, $result);
  4211. }
  4212. /**
  4213. * testErrorMultipleMessages method
  4214. *
  4215. * Test error() with multiple messages.
  4216. *
  4217. * @return void
  4218. */
  4219. public function testErrorMultipleMessages()
  4220. {
  4221. $this->article['errors'] = [
  4222. 'field' => ['notBlank', 'email', 'Something else']
  4223. ];
  4224. $this->Form->create($this->article);
  4225. $result = $this->Form->error('field', [
  4226. 'notBlank' => 'Cannot be empty',
  4227. 'email' => 'No good!'
  4228. ]);
  4229. $expected = [
  4230. 'div' => ['class' => 'error-message'],
  4231. 'ul' => [],
  4232. '<li', 'Cannot be empty', '/li',
  4233. '<li', 'No good!', '/li',
  4234. '<li', 'Something else', '/li',
  4235. '/ul',
  4236. '/div'
  4237. ];
  4238. $this->assertHtml($expected, $result);
  4239. }
  4240. /**
  4241. * testPassword method
  4242. *
  4243. * Test password element generation.
  4244. *
  4245. * @return void
  4246. */
  4247. public function testPassword()
  4248. {
  4249. $this->article['errors'] = [
  4250. 'Contact' => [
  4251. 'passwd' => 1
  4252. ]
  4253. ];
  4254. $this->Form->create($this->article);
  4255. $result = $this->Form->password('Contact.field');
  4256. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4257. $this->assertHtml($expected, $result);
  4258. $this->Form->request = $this->Form->request->withData('Contact.passwd', 'test');
  4259. $this->Form->create($this->article);
  4260. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4261. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4262. $this->assertHtml($expected, $result);
  4263. }
  4264. /**
  4265. * testRadio method
  4266. *
  4267. * Test radio element set generation.
  4268. *
  4269. * @return void
  4270. */
  4271. public function testRadio()
  4272. {
  4273. $result = $this->Form->radio('Model.field', ['option A']);
  4274. $expected = [
  4275. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4276. 'label' => ['for' => 'model-field-0'],
  4277. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4278. 'option A',
  4279. '/label'
  4280. ];
  4281. $this->assertHtml($expected, $result);
  4282. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4283. $this->assertHtml($expected, $result);
  4284. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4285. $expected = [
  4286. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4287. ['label' => ['for' => 'model-field-0']],
  4288. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4289. 'option A',
  4290. '/label',
  4291. ['label' => ['for' => 'model-field-1']],
  4292. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4293. 'option B',
  4294. '/label',
  4295. ];
  4296. $this->assertHtml($expected, $result);
  4297. $result = $this->Form->radio(
  4298. 'Employee.gender',
  4299. ['male' => 'Male', 'female' => 'Female'],
  4300. ['form' => 'my-form']
  4301. );
  4302. $expected = [
  4303. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4304. ['label' => ['for' => 'employee-gender-male']],
  4305. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4306. 'Male',
  4307. '/label',
  4308. ['label' => ['for' => 'employee-gender-female']],
  4309. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4310. 'Female',
  4311. '/label',
  4312. ];
  4313. $this->assertHtml($expected, $result);
  4314. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4315. $expected = [
  4316. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4317. ['label' => ['for' => 'model-custom-0']],
  4318. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4319. 'option A',
  4320. '/label',
  4321. ['label' => ['for' => 'model-custom-1']],
  4322. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4323. 'option B',
  4324. '/label',
  4325. ];
  4326. $this->assertHtml($expected, $result);
  4327. $result = $this->Form->radio(
  4328. 'Employee.gender',
  4329. [
  4330. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4331. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4332. ]
  4333. );
  4334. $expected = [
  4335. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4336. ['label' => ['for' => 'employee-gender-male']],
  4337. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4338. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4339. 'Male',
  4340. '/label',
  4341. ['label' => ['for' => 'employee-gender-female']],
  4342. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4343. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4344. 'Female',
  4345. '/label',
  4346. ];
  4347. $this->assertHtml($expected, $result);
  4348. }
  4349. /**
  4350. * Test radio with complex options and empty disabled data.
  4351. *
  4352. * @return void
  4353. */
  4354. public function testRadioComplexDisabled()
  4355. {
  4356. $options = [
  4357. ['value' => 'r', 'text' => 'red'],
  4358. ['value' => 'b', 'text' => 'blue'],
  4359. ];
  4360. $attrs = ['disabled' => []];
  4361. $result = $this->Form->radio('Model.field', $options, $attrs);
  4362. $expected = [
  4363. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4364. ['label' => ['for' => 'model-field-r']],
  4365. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4366. 'red',
  4367. '/label',
  4368. ['label' => ['for' => 'model-field-b']],
  4369. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4370. 'blue',
  4371. '/label',
  4372. ];
  4373. $this->assertHtml($expected, $result);
  4374. $attrs = ['disabled' => ['r']];
  4375. $result = $this->Form->radio('Model.field', $options, $attrs);
  4376. $this->assertContains('disabled="disabled"', $result);
  4377. }
  4378. /**
  4379. * testRadioDefaultValue method
  4380. *
  4381. * Test default value setting on radio() method.
  4382. *
  4383. * @return void
  4384. */
  4385. public function testRadioDefaultValue()
  4386. {
  4387. $Articles = $this->getTableLocator()->get('Articles');
  4388. $title = $Articles->getSchema()->getColumn('title');
  4389. $Articles->getSchema()->addColumn(
  4390. 'title',
  4391. ['default' => '1'] + $title
  4392. );
  4393. $this->Form->create($Articles->newEntity());
  4394. $result = $this->Form->radio('title', ['option A', 'option B']);
  4395. $expected = [
  4396. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4397. ['label' => ['for' => 'title-0']],
  4398. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4399. 'option A',
  4400. '/label',
  4401. ['label' => ['for' => 'title-1']],
  4402. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4403. 'option B',
  4404. '/label',
  4405. ];
  4406. $this->assertHtml($expected, $result);
  4407. }
  4408. /**
  4409. * Test setting a hiddenField value on radio buttons.
  4410. *
  4411. * @return void
  4412. */
  4413. public function testRadioHiddenFieldValue()
  4414. {
  4415. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4416. $expected = [
  4417. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4418. 'label' => ['for' => 'title-0'],
  4419. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4420. 'option A',
  4421. '/label',
  4422. ];
  4423. $this->assertHtml($expected, $result);
  4424. }
  4425. /**
  4426. * testControlRadio method
  4427. *
  4428. * Test that input works with radio types.
  4429. *
  4430. * @return void
  4431. */
  4432. public function testControlRadio()
  4433. {
  4434. $result = $this->Form->control('test', [
  4435. 'type' => 'radio',
  4436. 'options' => ['A', 'B'],
  4437. ]);
  4438. $expected = [
  4439. ['div' => ['class' => 'input radio']],
  4440. '<label',
  4441. 'Test',
  4442. '/label',
  4443. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4444. ['label' => ['for' => 'test-0']],
  4445. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4446. 'A',
  4447. '/label',
  4448. ['label' => ['for' => 'test-1']],
  4449. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4450. 'B',
  4451. '/label',
  4452. '/div',
  4453. ];
  4454. $this->assertHtml($expected, $result);
  4455. $result = $this->Form->control('test', [
  4456. 'type' => 'radio',
  4457. 'options' => ['A', 'B'],
  4458. 'value' => '0'
  4459. ]);
  4460. $expected = [
  4461. ['div' => ['class' => 'input radio']],
  4462. '<label',
  4463. 'Test',
  4464. '/label',
  4465. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4466. ['label' => ['for' => 'test-0']],
  4467. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4468. 'A',
  4469. '/label',
  4470. ['label' => ['for' => 'test-1']],
  4471. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4472. 'B',
  4473. '/label',
  4474. '/div',
  4475. ];
  4476. $this->assertHtml($expected, $result);
  4477. $result = $this->Form->control('test', [
  4478. 'type' => 'radio',
  4479. 'options' => ['A', 'B'],
  4480. 'label' => false
  4481. ]);
  4482. $expected = [
  4483. ['div' => ['class' => 'input radio']],
  4484. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4485. ['label' => ['for' => 'test-0']],
  4486. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4487. 'A',
  4488. '/label',
  4489. ['label' => ['for' => 'test-1']],
  4490. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4491. 'B',
  4492. '/label',
  4493. '/div',
  4494. ];
  4495. $this->assertHtml($expected, $result);
  4496. }
  4497. /**
  4498. * testRadioNoLabel method
  4499. *
  4500. * Test that radio() works with label = false.
  4501. *
  4502. * @return void
  4503. */
  4504. public function testRadioNoLabel()
  4505. {
  4506. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4507. $expected = [
  4508. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4509. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4510. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4511. ];
  4512. $this->assertHtml($expected, $result);
  4513. }
  4514. /**
  4515. * testRadioControlInsideLabel method
  4516. *
  4517. * Test generating radio input inside label ala twitter bootstrap.
  4518. *
  4519. * @return void
  4520. */
  4521. public function testRadioControlInsideLabel()
  4522. {
  4523. $this->Form->setTemplates([
  4524. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4525. 'radioWrapper' => '{{label}}'
  4526. ]);
  4527. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4528. //@codingStandardsIgnoreStart
  4529. $expected = [
  4530. ['input' => [
  4531. 'type' => 'hidden',
  4532. 'name' => 'Model[field]',
  4533. 'value' => ''
  4534. ]],
  4535. ['label' => ['for' => 'model-field-0']],
  4536. ['input' => [
  4537. 'type' => 'radio',
  4538. 'name' => 'Model[field]',
  4539. 'value' => '0',
  4540. 'id' => 'model-field-0'
  4541. ]],
  4542. 'option A',
  4543. '/label',
  4544. ['label' => ['for' => 'model-field-1']],
  4545. ['input' => [
  4546. 'type' => 'radio',
  4547. 'name' => 'Model[field]',
  4548. 'value' => '1',
  4549. 'id' => 'model-field-1'
  4550. ]],
  4551. 'option B',
  4552. '/label',
  4553. ];
  4554. //@codingStandardsIgnoreEnd
  4555. $this->assertHtml($expected, $result);
  4556. }
  4557. /**
  4558. * testRadioHiddenControlDisabling method
  4559. *
  4560. * Test disabling the hidden input for radio buttons.
  4561. *
  4562. * @return void
  4563. */
  4564. public function testRadioHiddenControlDisabling()
  4565. {
  4566. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4567. $expected = [
  4568. 'label' => ['for' => 'model-1-field-0'],
  4569. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4570. 'option A',
  4571. '/label'
  4572. ];
  4573. $this->assertHtml($expected, $result);
  4574. }
  4575. /**
  4576. * testRadioOutOfRange method
  4577. *
  4578. * Test radio element set generation.
  4579. *
  4580. * @return void
  4581. */
  4582. public function testRadioOutOfRange()
  4583. {
  4584. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4585. $expected = [
  4586. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4587. 'label' => ['for' => 'model-field-v'],
  4588. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4589. 'value',
  4590. '/label'
  4591. ];
  4592. $this->assertHtml($expected, $result);
  4593. }
  4594. /**
  4595. * testSelect method
  4596. *
  4597. * Test select element generation.
  4598. *
  4599. * @return void
  4600. */
  4601. public function testSelect()
  4602. {
  4603. $result = $this->Form->select('Model.field', []);
  4604. $expected = [
  4605. 'select' => ['name' => 'Model[field]'],
  4606. '/select'
  4607. ];
  4608. $this->assertHtml($expected, $result);
  4609. $this->Form->request = $this->Form->request->withData('Model', ['field' => 'value']);
  4610. $this->Form->create();
  4611. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4612. $expected = [
  4613. 'select' => ['name' => 'Model[field]'],
  4614. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4615. 'good',
  4616. '/option',
  4617. ['option' => ['value' => 'other']],
  4618. 'bad',
  4619. '/option',
  4620. '/select'
  4621. ];
  4622. $this->assertHtml($expected, $result);
  4623. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4624. $this->assertHtml($expected, $result);
  4625. $this->Form->request = $this->Form->request->withParsedBody([]);
  4626. $this->Form->create();
  4627. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4628. $expected = [
  4629. 'select' => ['name' => 'Model[field]'],
  4630. ['option' => ['value' => 'value']],
  4631. 'good',
  4632. '/option',
  4633. ['option' => ['value' => 'other']],
  4634. 'bad',
  4635. '/option',
  4636. '/select'
  4637. ];
  4638. $this->assertHtml($expected, $result);
  4639. $options = [
  4640. ['value' => 'first', 'text' => 'First'],
  4641. ['value' => 'first', 'text' => 'Another First'],
  4642. ];
  4643. $result = $this->Form->select(
  4644. 'Model.field',
  4645. $options,
  4646. ['escape' => false, 'empty' => false]
  4647. );
  4648. $expected = [
  4649. 'select' => ['name' => 'Model[field]'],
  4650. ['option' => ['value' => 'first']],
  4651. 'First',
  4652. '/option',
  4653. ['option' => ['value' => 'first']],
  4654. 'Another First',
  4655. '/option',
  4656. '/select'
  4657. ];
  4658. $this->assertHtml($expected, $result);
  4659. $this->Form->request = $this->Form->request->withParsedBody(['Model' => ['contact_id' => 228]]);
  4660. $this->Form->create();
  4661. $result = $this->Form->select(
  4662. 'Model.contact_id',
  4663. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4664. ['escape' => false, 'empty' => 'pick something']
  4665. );
  4666. $expected = [
  4667. 'select' => ['name' => 'Model[contact_id]'],
  4668. ['option' => ['value' => '']], 'pick something', '/option',
  4669. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4670. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4671. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4672. '/select'
  4673. ];
  4674. $this->assertHtml($expected, $result);
  4675. $this->Form->request = $this->Form->request->withData('Model.field', 0);
  4676. $this->Form->create();
  4677. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4678. $expected = [
  4679. 'select' => ['name' => 'Model[field]'],
  4680. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4681. ['option' => ['value' => '1']], 'Yes', '/option',
  4682. '/select'
  4683. ];
  4684. $this->assertHtml($expected, $result);
  4685. }
  4686. /**
  4687. * testSelectEscapeHtml method
  4688. *
  4689. * Test that select() escapes HTML.
  4690. *
  4691. * @return void
  4692. */
  4693. public function testSelectEscapeHtml()
  4694. {
  4695. $result = $this->Form->select(
  4696. 'Model.field',
  4697. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4698. ['empty' => false]
  4699. );
  4700. $expected = [
  4701. 'select' => ['name' => 'Model[field]'],
  4702. ['option' => ['value' => 'first']],
  4703. 'first &quot;html&quot; &lt;chars&gt;',
  4704. '/option',
  4705. ['option' => ['value' => 'second']],
  4706. 'value',
  4707. '/option',
  4708. '/select'
  4709. ];
  4710. $this->assertHtml($expected, $result);
  4711. $result = $this->Form->select(
  4712. 'Model.field',
  4713. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4714. ['escape' => false, 'empty' => false]
  4715. );
  4716. $expected = [
  4717. 'select' => ['name' => 'Model[field]'],
  4718. ['option' => ['value' => 'first']],
  4719. 'first "html" <chars>',
  4720. '/option',
  4721. ['option' => ['value' => 'second']],
  4722. 'value',
  4723. '/option',
  4724. '/select'
  4725. ];
  4726. $this->assertHtml($expected, $result);
  4727. }
  4728. /**
  4729. * testSelectRequired method
  4730. *
  4731. * Test select() with required and disabled attributes.
  4732. *
  4733. * @return void
  4734. */
  4735. public function testSelectRequired()
  4736. {
  4737. $this->article['required'] = [
  4738. 'user_id' => true
  4739. ];
  4740. $this->Form->create($this->article);
  4741. $result = $this->Form->select('user_id', ['option A']);
  4742. $expected = [
  4743. 'select' => [
  4744. 'name' => 'user_id',
  4745. 'required' => 'required'
  4746. ],
  4747. ['option' => ['value' => '0']], 'option A', '/option',
  4748. '/select'
  4749. ];
  4750. $this->assertHtml($expected, $result);
  4751. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4752. $expected = [
  4753. 'select' => [
  4754. 'name' => 'user_id',
  4755. 'disabled' => 'disabled'
  4756. ],
  4757. ['option' => ['value' => '0']], 'option A', '/option',
  4758. '/select'
  4759. ];
  4760. $this->assertHtml($expected, $result);
  4761. }
  4762. /**
  4763. * testNestedSelect method
  4764. *
  4765. * Test select element generation with optgroups.
  4766. *
  4767. * @return void
  4768. */
  4769. public function testNestedSelect()
  4770. {
  4771. $result = $this->Form->select(
  4772. 'Model.field',
  4773. [1 => 'One', 2 => 'Two', 'Three' => [
  4774. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4775. ]],
  4776. ['empty' => false]
  4777. );
  4778. $expected = [
  4779. 'select' => ['name' => 'Model[field]'],
  4780. ['option' => ['value' => 1]],
  4781. 'One',
  4782. '/option',
  4783. ['option' => ['value' => 2]],
  4784. 'Two',
  4785. '/option',
  4786. ['optgroup' => ['label' => 'Three']],
  4787. ['option' => ['value' => 3]],
  4788. 'Three',
  4789. '/option',
  4790. ['option' => ['value' => 4]],
  4791. 'Four',
  4792. '/option',
  4793. ['option' => ['value' => 5]],
  4794. 'Five',
  4795. '/option',
  4796. '/optgroup',
  4797. '/select'
  4798. ];
  4799. $this->assertHtml($expected, $result);
  4800. }
  4801. /**
  4802. * testSelectMultiple method
  4803. *
  4804. * Test generation of multiple select elements.
  4805. *
  4806. * @return void
  4807. */
  4808. public function testSelectMultiple()
  4809. {
  4810. $options = ['first', 'second', 'third'];
  4811. $result = $this->Form->select(
  4812. 'Model.multi_field',
  4813. $options,
  4814. ['form' => 'my-form', 'multiple' => true]
  4815. );
  4816. $expected = [
  4817. 'input' => [
  4818. 'type' => 'hidden',
  4819. 'name' => 'Model[multi_field]',
  4820. 'value' => '',
  4821. 'form' => 'my-form',
  4822. ],
  4823. 'select' => [
  4824. 'name' => 'Model[multi_field][]',
  4825. 'multiple' => 'multiple',
  4826. 'form' => 'my-form',
  4827. ],
  4828. ['option' => ['value' => '0']],
  4829. 'first',
  4830. '/option',
  4831. ['option' => ['value' => '1']],
  4832. 'second',
  4833. '/option',
  4834. ['option' => ['value' => '2']],
  4835. 'third',
  4836. '/option',
  4837. '/select'
  4838. ];
  4839. $this->assertHtml($expected, $result);
  4840. $result = $this->Form->select(
  4841. 'Model.multi_field',
  4842. $options,
  4843. ['multiple' => 'multiple', 'form' => 'my-form']
  4844. );
  4845. $this->assertHtml($expected, $result);
  4846. $result = $this->Form->select(
  4847. 'Model.multi_field',
  4848. $options,
  4849. ['form' => 'my-form', 'multiple' => false]
  4850. );
  4851. $this->assertNotContains('multiple', $result);
  4852. }
  4853. /**
  4854. * testCheckboxZeroValue method
  4855. *
  4856. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4857. *
  4858. * @return void
  4859. */
  4860. public function testCheckboxZeroValue()
  4861. {
  4862. $result = $this->Form->control('User.get_spam', [
  4863. 'type' => 'checkbox',
  4864. 'value' => '0',
  4865. 'hiddenField' => '1',
  4866. ]);
  4867. $expected = [
  4868. 'div' => ['class' => 'input checkbox'],
  4869. 'label' => ['for' => 'user-get-spam'],
  4870. ['input' => [
  4871. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4872. 'value' => '1'
  4873. ]],
  4874. ['input' => [
  4875. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4876. 'value' => '0', 'id' => 'user-get-spam'
  4877. ]],
  4878. 'Get Spam',
  4879. '/label',
  4880. '/div'
  4881. ];
  4882. $this->assertHtml($expected, $result);
  4883. }
  4884. /**
  4885. * testHabtmSelectBox method
  4886. *
  4887. * Test generation of habtm select boxes.
  4888. *
  4889. * @return void
  4890. */
  4891. public function testHabtmSelectBox()
  4892. {
  4893. $this->loadFixtures('Articles');
  4894. $options = [
  4895. 1 => 'blue',
  4896. 2 => 'red',
  4897. 3 => 'green'
  4898. ];
  4899. $tags = [
  4900. new Entity(['id' => 1, 'name' => 'blue']),
  4901. new Entity(['id' => 3, 'name' => 'green'])
  4902. ];
  4903. $article = new Article(['tags' => $tags]);
  4904. $this->Form->create($article);
  4905. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4906. $expected = [
  4907. 'div' => ['class' => 'input select'],
  4908. 'label' => ['for' => 'tags-ids'],
  4909. 'Tags',
  4910. '/label',
  4911. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4912. 'select' => [
  4913. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4914. 'multiple' => 'multiple'
  4915. ],
  4916. ['option' => ['value' => '1', 'selected' => 'selected']],
  4917. 'blue',
  4918. '/option',
  4919. ['option' => ['value' => '2']],
  4920. 'red',
  4921. '/option',
  4922. ['option' => ['value' => '3', 'selected' => 'selected']],
  4923. 'green',
  4924. '/option',
  4925. '/select',
  4926. '/div'
  4927. ];
  4928. $this->assertHtml($expected, $result);
  4929. // make sure only 50 is selected, and not 50f5c0cf
  4930. $options = [
  4931. '1' => 'blue',
  4932. '50f5c0cf' => 'red',
  4933. '50' => 'green'
  4934. ];
  4935. $tags = [
  4936. new Entity(['id' => 1, 'name' => 'blue']),
  4937. new Entity(['id' => 50, 'name' => 'green'])
  4938. ];
  4939. $article = new Article(['tags' => $tags]);
  4940. $this->Form->create($article);
  4941. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4942. $expected = [
  4943. 'div' => ['class' => 'input select'],
  4944. 'label' => ['for' => 'tags-ids'],
  4945. 'Tags',
  4946. '/label',
  4947. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4948. 'select' => [
  4949. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4950. 'multiple' => 'multiple'
  4951. ],
  4952. ['option' => ['value' => '1', 'selected' => 'selected']],
  4953. 'blue',
  4954. '/option',
  4955. ['option' => ['value' => '50f5c0cf']],
  4956. 'red',
  4957. '/option',
  4958. ['option' => ['value' => '50', 'selected' => 'selected']],
  4959. 'green',
  4960. '/option',
  4961. '/select',
  4962. '/div'
  4963. ];
  4964. $this->assertHtml($expected, $result);
  4965. $spacecraft = [
  4966. 1 => 'Orion',
  4967. 2 => 'Helios'
  4968. ];
  4969. $this->View->viewVars['spacecraft'] = $spacecraft;
  4970. $this->Form->create();
  4971. $result = $this->Form->control('spacecraft._ids');
  4972. $expected = [
  4973. 'div' => ['class' => 'input select'],
  4974. 'label' => ['for' => 'spacecraft-ids'],
  4975. 'Spacecraft',
  4976. '/label',
  4977. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4978. 'select' => [
  4979. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4980. 'multiple' => 'multiple'
  4981. ],
  4982. ['option' => ['value' => '1']],
  4983. 'Orion',
  4984. '/option',
  4985. ['option' => ['value' => '2']],
  4986. 'Helios',
  4987. '/option',
  4988. '/select',
  4989. '/div'
  4990. ];
  4991. $this->assertHtml($expected, $result);
  4992. }
  4993. /**
  4994. * testErrorsForBelongsToManySelect method
  4995. *
  4996. * Tests that errors for belongsToMany select fields are being
  4997. * picked up properly.
  4998. *
  4999. * @return void
  5000. */
  5001. public function testErrorsForBelongsToManySelect()
  5002. {
  5003. $spacecraft = [
  5004. 1 => 'Orion',
  5005. 2 => 'Helios'
  5006. ];
  5007. $this->View->viewVars['spacecraft'] = $spacecraft;
  5008. $article = new Article();
  5009. $article->setError('spacecraft', ['Invalid']);
  5010. $this->Form->create($article);
  5011. $result = $this->Form->control('spacecraft._ids');
  5012. $expected = [
  5013. ['div' => ['class' => 'input select error']],
  5014. 'label' => ['for' => 'spacecraft-ids'],
  5015. 'Spacecraft',
  5016. '/label',
  5017. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5018. 'select' => [
  5019. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5020. 'multiple' => 'multiple'
  5021. ],
  5022. ['option' => ['value' => '1']],
  5023. 'Orion',
  5024. '/option',
  5025. ['option' => ['value' => '2']],
  5026. 'Helios',
  5027. '/option',
  5028. '/select',
  5029. ['div' => ['class' => 'error-message']],
  5030. 'Invalid',
  5031. '/div',
  5032. '/div'
  5033. ];
  5034. $this->assertHtml($expected, $result);
  5035. }
  5036. /**
  5037. * testSelectMultipleCheckboxes method
  5038. *
  5039. * Test generation of multi select elements in checkbox format.
  5040. *
  5041. * @return void
  5042. */
  5043. public function testSelectMultipleCheckboxes()
  5044. {
  5045. $result = $this->Form->select(
  5046. 'Model.multi_field',
  5047. ['first', 'second', 'third'],
  5048. ['multiple' => 'checkbox']
  5049. );
  5050. $expected = [
  5051. 'input' => [
  5052. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5053. ],
  5054. ['div' => ['class' => 'checkbox']],
  5055. ['label' => ['for' => 'model-multi-field-0']],
  5056. ['input' => [
  5057. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5058. 'value' => '0', 'id' => 'model-multi-field-0'
  5059. ]],
  5060. 'first',
  5061. '/label',
  5062. '/div',
  5063. ['div' => ['class' => 'checkbox']],
  5064. ['label' => ['for' => 'model-multi-field-1']],
  5065. ['input' => [
  5066. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5067. 'value' => '1', 'id' => 'model-multi-field-1'
  5068. ]],
  5069. 'second',
  5070. '/label',
  5071. '/div',
  5072. ['div' => ['class' => 'checkbox']],
  5073. ['label' => ['for' => 'model-multi-field-2']],
  5074. ['input' => [
  5075. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5076. 'value' => '2', 'id' => 'model-multi-field-2'
  5077. ]],
  5078. 'third',
  5079. '/label',
  5080. '/div'
  5081. ];
  5082. $this->assertHtml($expected, $result);
  5083. $result = $this->Form->select(
  5084. 'Model.multi_field',
  5085. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5086. ['multiple' => 'checkbox']
  5087. );
  5088. $expected = [
  5089. 'input' => [
  5090. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5091. ],
  5092. ['div' => ['class' => 'checkbox']],
  5093. ['label' => ['for' => 'model-multi-field-a+']],
  5094. ['input' => [
  5095. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5096. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5097. ]],
  5098. 'first',
  5099. '/label',
  5100. '/div',
  5101. ['div' => ['class' => 'checkbox']],
  5102. ['label' => ['for' => 'model-multi-field-a++']],
  5103. ['input' => [
  5104. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5105. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5106. ]],
  5107. 'second',
  5108. '/label',
  5109. '/div',
  5110. ['div' => ['class' => 'checkbox']],
  5111. ['label' => ['for' => 'model-multi-field-a+++']],
  5112. ['input' => [
  5113. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5114. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5115. ]],
  5116. 'third',
  5117. '/label',
  5118. '/div'
  5119. ];
  5120. $this->assertHtml($expected, $result);
  5121. $result = $this->Form->select(
  5122. 'Model.multi_field',
  5123. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5124. ['multiple' => 'checkbox']
  5125. );
  5126. $expected = [
  5127. 'input' => [
  5128. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5129. ],
  5130. ['div' => ['class' => 'checkbox']],
  5131. ['label' => ['for' => 'model-multi-field-a-b']],
  5132. ['input' => [
  5133. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5134. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5135. ]],
  5136. 'first',
  5137. '/label',
  5138. '/div',
  5139. ['div' => ['class' => 'checkbox']],
  5140. ['label' => ['for' => 'model-multi-field-a-b1']],
  5141. ['input' => [
  5142. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5143. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5144. ]],
  5145. 'second',
  5146. '/label',
  5147. '/div',
  5148. ['div' => ['class' => 'checkbox']],
  5149. ['label' => ['for' => 'model-multi-field-a-b2']],
  5150. ['input' => [
  5151. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5152. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5153. ]],
  5154. 'third',
  5155. '/label',
  5156. '/div'
  5157. ];
  5158. $this->assertHtml($expected, $result);
  5159. }
  5160. /**
  5161. * testSelectMultipleCheckboxRequestData method
  5162. *
  5163. * Ensure that multiCheckbox reads from the request data.
  5164. *
  5165. * @return void
  5166. */
  5167. public function testSelectMultipleCheckboxRequestData()
  5168. {
  5169. $this->Form->request = $this->Form->request->withData('Model', ['tags' => [1]]);
  5170. $result = $this->Form->select(
  5171. 'Model.tags',
  5172. ['1' => 'first', 'Array' => 'Array'],
  5173. ['multiple' => 'checkbox']
  5174. );
  5175. $expected = [
  5176. 'input' => [
  5177. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5178. ],
  5179. ['div' => ['class' => 'checkbox']],
  5180. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5181. ['input' => [
  5182. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5183. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5184. ]],
  5185. 'first',
  5186. '/label',
  5187. '/div',
  5188. ['div' => ['class' => 'checkbox']],
  5189. ['label' => ['for' => 'model-tags-array']],
  5190. ['input' => [
  5191. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5192. 'value' => 'Array', 'id' => 'model-tags-array'
  5193. ]],
  5194. 'Array',
  5195. '/label',
  5196. '/div'
  5197. ];
  5198. $this->assertHtml($expected, $result);
  5199. }
  5200. /**
  5201. * testSelectMultipleCheckboxSecurity method
  5202. *
  5203. * Checks the security hash array generated for multiple-input checkbox elements.
  5204. *
  5205. * @return void
  5206. */
  5207. public function testSelectMultipleCheckboxSecurity()
  5208. {
  5209. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  5210. $this->assertEquals([], $this->Form->fields);
  5211. $this->Form->select(
  5212. 'Model.multi_field',
  5213. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5214. ['multiple' => 'checkbox']
  5215. );
  5216. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5217. $result = $this->Form->secure($this->Form->fields);
  5218. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5219. $this->assertContains('"' . $key . '"', $result);
  5220. }
  5221. /**
  5222. * testSelectMultipleSecureWithNoOptions method
  5223. *
  5224. * Multiple select elements should always be secured as they always participate
  5225. * in the POST data.
  5226. *
  5227. * @return void
  5228. */
  5229. public function testSelectMultipleSecureWithNoOptions()
  5230. {
  5231. $this->assertEquals([], $this->Form->fields);
  5232. $this->Form->select(
  5233. 'Model.select',
  5234. [],
  5235. ['multiple' => true]
  5236. );
  5237. $this->assertEquals(['Model.select'], $this->Form->fields);
  5238. }
  5239. /**
  5240. * testSelectNoSecureWithNoOptions method
  5241. *
  5242. * When a select box has no options it should not be added to the fields list
  5243. * as it always fail post validation.
  5244. *
  5245. * @return void
  5246. */
  5247. public function testSelectNoSecureWithNoOptions()
  5248. {
  5249. $this->Form->request = $this->Form->request->withParam('_Token', 'testkey');
  5250. $this->assertEquals([], $this->Form->fields);
  5251. $this->Form->select(
  5252. 'Model.select',
  5253. []
  5254. );
  5255. $this->assertEquals([], $this->Form->fields);
  5256. $this->Form->select(
  5257. 'Model.user_id',
  5258. [],
  5259. ['empty' => true]
  5260. );
  5261. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5262. }
  5263. /**
  5264. * testControlMultipleCheckboxes method
  5265. *
  5266. * Test control() resulting in multi select elements being generated.
  5267. *
  5268. * @return void
  5269. */
  5270. public function testControlMultipleCheckboxes()
  5271. {
  5272. $result = $this->Form->control('Model.multi_field', [
  5273. 'options' => ['first', 'second', 'third'],
  5274. 'multiple' => 'checkbox'
  5275. ]);
  5276. $expected = [
  5277. ['div' => ['class' => 'input select']],
  5278. ['label' => ['for' => 'model-multi-field']],
  5279. 'Multi Field',
  5280. '/label',
  5281. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5282. ['div' => ['class' => 'checkbox']],
  5283. ['label' => ['for' => 'model-multi-field-0']],
  5284. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5285. 'first',
  5286. '/label',
  5287. '/div',
  5288. ['div' => ['class' => 'checkbox']],
  5289. ['label' => ['for' => 'model-multi-field-1']],
  5290. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5291. 'second',
  5292. '/label',
  5293. '/div',
  5294. ['div' => ['class' => 'checkbox']],
  5295. ['label' => ['for' => 'model-multi-field-2']],
  5296. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5297. 'third',
  5298. '/label',
  5299. '/div',
  5300. '/div'
  5301. ];
  5302. $this->assertHtml($expected, $result);
  5303. $result = $this->Form->control('Model.multi_field', [
  5304. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5305. 'multiple' => 'checkbox'
  5306. ]);
  5307. $expected = [
  5308. ['div' => ['class' => 'input select']],
  5309. ['label' => ['for' => 'model-multi-field']],
  5310. 'Multi Field',
  5311. '/label',
  5312. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5313. ['div' => ['class' => 'checkbox']],
  5314. ['label' => ['for' => 'model-multi-field-a']],
  5315. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5316. 'first',
  5317. '/label',
  5318. '/div',
  5319. ['div' => ['class' => 'checkbox']],
  5320. ['label' => ['for' => 'model-multi-field-b']],
  5321. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5322. 'second',
  5323. '/label',
  5324. '/div',
  5325. ['div' => ['class' => 'checkbox']],
  5326. ['label' => ['for' => 'model-multi-field-c']],
  5327. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5328. 'third',
  5329. '/label',
  5330. '/div',
  5331. '/div'
  5332. ];
  5333. $this->assertHtml($expected, $result);
  5334. }
  5335. /**
  5336. * testSelectHiddenFieldOmission method
  5337. *
  5338. * Test that select() with 'hiddenField' => false omits the hidden field.
  5339. *
  5340. * @return void
  5341. */
  5342. public function testSelectHiddenFieldOmission()
  5343. {
  5344. $result = $this->Form->select(
  5345. 'Model.multi_field',
  5346. ['first', 'second'],
  5347. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5348. );
  5349. $this->assertNotContains('type="hidden"', $result);
  5350. }
  5351. /**
  5352. * testSelectCheckboxMultipleOverrideName method
  5353. *
  5354. * Test that select() with multiple = checkbox works with overriding name attribute.
  5355. *
  5356. * @return void
  5357. */
  5358. public function testSelectCheckboxMultipleOverrideName()
  5359. {
  5360. $result = $this->Form->select('category', ['1', '2'], [
  5361. 'multiple' => 'checkbox',
  5362. 'name' => 'fish',
  5363. ]);
  5364. $expected = [
  5365. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5366. ['div' => ['class' => 'checkbox']],
  5367. ['label' => ['for' => 'fish-0']],
  5368. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5369. '1',
  5370. '/label',
  5371. '/div',
  5372. ['div' => ['class' => 'checkbox']],
  5373. ['label' => ['for' => 'fish-1']],
  5374. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5375. '2',
  5376. '/label',
  5377. '/div'
  5378. ];
  5379. $this->assertHtml($expected, $result);
  5380. $result = $this->Form->multiCheckbox(
  5381. 'category',
  5382. new Collection(['1', '2']),
  5383. ['name' => 'fish']
  5384. );
  5385. $this->assertHtml($expected, $result);
  5386. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5387. 'name' => 'fish',
  5388. ]);
  5389. $this->assertHtml($expected, $result);
  5390. }
  5391. /**
  5392. * testControlMultiCheckbox method
  5393. *
  5394. * Test that control() works with multicheckbox.
  5395. *
  5396. * @return void
  5397. */
  5398. public function testControlMultiCheckbox()
  5399. {
  5400. $result = $this->Form->control('category', [
  5401. 'type' => 'multicheckbox',
  5402. 'options' => ['1', '2'],
  5403. ]);
  5404. $expected = [
  5405. ['div' => ['class' => 'input multicheckbox']],
  5406. '<label',
  5407. 'Category',
  5408. '/label',
  5409. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5410. ['div' => ['class' => 'checkbox']],
  5411. ['label' => ['for' => 'category-0']],
  5412. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5413. '1',
  5414. '/label',
  5415. '/div',
  5416. ['div' => ['class' => 'checkbox']],
  5417. ['label' => ['for' => 'category-1']],
  5418. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5419. '2',
  5420. '/label',
  5421. '/div',
  5422. '/div',
  5423. ];
  5424. $this->assertHtml($expected, $result);
  5425. }
  5426. /**
  5427. * testCheckbox method
  5428. *
  5429. * Test generation of checkboxes.
  5430. *
  5431. * @return void
  5432. */
  5433. public function testCheckbox()
  5434. {
  5435. $result = $this->Form->checkbox('Model.field');
  5436. $expected = [
  5437. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5438. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5439. ];
  5440. $this->assertHtml($expected, $result);
  5441. $result = $this->Form->checkbox('Model.field', [
  5442. 'id' => 'theID',
  5443. 'value' => 'myvalue',
  5444. 'form' => 'my-form',
  5445. ]);
  5446. $expected = [
  5447. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5448. ['input' => [
  5449. 'type' => 'checkbox', 'name' => 'Model[field]',
  5450. 'value' => 'myvalue', 'id' => 'theID',
  5451. 'form' => 'my-form',
  5452. ]]
  5453. ];
  5454. $this->assertHtml($expected, $result);
  5455. }
  5456. /**
  5457. * testCheckboxDefaultValue method
  5458. *
  5459. * Test default value setting on checkbox() method.
  5460. *
  5461. * @return void
  5462. */
  5463. public function testCheckboxDefaultValue()
  5464. {
  5465. $this->Form->request = $this->Form->request->withData('Model.field', false);
  5466. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5467. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5468. $this->assertHtml($expected, $result);
  5469. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5470. $this->Form->create();
  5471. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5472. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5473. $this->assertHtml($expected, $result);
  5474. $this->Form->request = $this->Form->request->withData('Model.field', true);
  5475. $this->Form->create();
  5476. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5477. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5478. $this->assertHtml($expected, $result);
  5479. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5480. $this->Form->create();
  5481. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5482. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5483. $this->assertHtml($expected, $result);
  5484. $Articles = $this->getTableLocator()->get('Articles');
  5485. $Articles->getSchema()->addColumn(
  5486. 'published',
  5487. ['type' => 'boolean', 'null' => false, 'default' => true]
  5488. );
  5489. $this->Form->create($Articles->newEntity());
  5490. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5491. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5492. $this->assertHtml($expected, $result);
  5493. }
  5494. /**
  5495. * testCheckboxCheckedAndError method
  5496. *
  5497. * Test checkbox being checked or having errors.
  5498. *
  5499. * @return void
  5500. */
  5501. public function testCheckboxCheckedAndError()
  5502. {
  5503. $this->article['errors'] = [
  5504. 'published' => true
  5505. ];
  5506. $this->Form->request = $this->Form->request->withData('published', 'myvalue');
  5507. $this->Form->create($this->article);
  5508. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5509. $expected = [
  5510. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5511. ['input' => [
  5512. 'type' => 'checkbox',
  5513. 'name' => 'published',
  5514. 'value' => 'myvalue',
  5515. 'id' => 'theID',
  5516. 'checked' => 'checked',
  5517. 'class' => 'form-error'
  5518. ]]
  5519. ];
  5520. $this->assertHtml($expected, $result);
  5521. $this->Form->request = $this->Form->request->withData('published', '');
  5522. $this->Form->create($this->article);
  5523. $result = $this->Form->checkbox('published');
  5524. $expected = [
  5525. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5526. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5527. ];
  5528. $this->assertHtml($expected, $result);
  5529. }
  5530. /**
  5531. * testCheckboxCustomNameAttribute method
  5532. *
  5533. * Test checkbox() with a custom name attribute.
  5534. *
  5535. * @return void
  5536. */
  5537. public function testCheckboxCustomNameAttribute()
  5538. {
  5539. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5540. $expected = [
  5541. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5542. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5543. ];
  5544. $this->assertHtml($expected, $result);
  5545. }
  5546. /**
  5547. * testCheckboxHiddenField method
  5548. *
  5549. * Test that the hidden input for checkboxes can be omitted or set to a
  5550. * specific value.
  5551. *
  5552. * @return void
  5553. */
  5554. public function testCheckboxHiddenField()
  5555. {
  5556. $result = $this->Form->checkbox('UserForm.something', [
  5557. 'hiddenField' => false
  5558. ]);
  5559. $expected = [
  5560. 'input' => [
  5561. 'type' => 'checkbox',
  5562. 'name' => 'UserForm[something]',
  5563. 'value' => '1'
  5564. ],
  5565. ];
  5566. $this->assertHtml($expected, $result);
  5567. $result = $this->Form->checkbox('UserForm.something', [
  5568. 'value' => 'Y',
  5569. 'hiddenField' => 'N',
  5570. ]);
  5571. $expected = [
  5572. ['input' => [
  5573. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5574. 'value' => 'N'
  5575. ]],
  5576. ['input' => [
  5577. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5578. 'value' => 'Y'
  5579. ]],
  5580. ];
  5581. $this->assertHtml($expected, $result);
  5582. }
  5583. /**
  5584. * testTime method
  5585. *
  5586. * Test the time type.
  5587. *
  5588. * @return void
  5589. */
  5590. public function testTime()
  5591. {
  5592. $result = $this->Form->time('start_time', [
  5593. 'timeFormat' => 12,
  5594. 'interval' => 5,
  5595. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5596. ]);
  5597. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5598. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5599. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5600. $this->assertNotContains('year', $result);
  5601. $this->assertNotContains('month', $result);
  5602. $this->assertNotContains('day', $result);
  5603. $result = $this->Form->time('start_time', [
  5604. 'timeFormat' => 12,
  5605. 'interval' => 5,
  5606. 'value' => '2014-03-08 16:30:00'
  5607. ]);
  5608. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5609. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5610. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5611. $this->assertNotContains('year', $result);
  5612. $this->assertNotContains('month', $result);
  5613. $this->assertNotContains('day', $result);
  5614. }
  5615. /**
  5616. * testTimeFormat24NoMeridian method
  5617. *
  5618. * Ensure that timeFormat=24 has no merdian.
  5619. *
  5620. * @return void.
  5621. */
  5622. public function testTimeFormat24NoMeridian()
  5623. {
  5624. $result = $this->Form->time('start_time', [
  5625. 'timeFormat' => 24,
  5626. 'interval' => 5,
  5627. 'value' => '2014-03-08 16:30:00'
  5628. ]);
  5629. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5630. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5631. $this->assertNotContains('meridian', $result);
  5632. $this->assertNotContains('pm', $result);
  5633. $this->assertNotContains('year', $result);
  5634. $this->assertNotContains('month', $result);
  5635. $this->assertNotContains('day', $result);
  5636. }
  5637. /**
  5638. * testDate method
  5639. *
  5640. * Test the date type.
  5641. *
  5642. * @return void
  5643. */
  5644. public function testDate()
  5645. {
  5646. $result = $this->Form->date('start_day', [
  5647. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5648. ]);
  5649. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5650. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5651. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5652. $this->assertNotContains('hour', $result);
  5653. $this->assertNotContains('minute', $result);
  5654. $this->assertNotContains('second', $result);
  5655. $this->assertNotContains('meridian', $result);
  5656. }
  5657. /**
  5658. * testDateTime method
  5659. *
  5660. * Test generation of date/time select elements.
  5661. *
  5662. * @return void
  5663. */
  5664. public function testDateTime()
  5665. {
  5666. extract($this->dateRegex);
  5667. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5668. $now = strtotime('now');
  5669. $expected = [
  5670. ['select' => ['name' => 'Contact[date][year]']],
  5671. ['option' => ['value' => '']],
  5672. '/option',
  5673. $yearsRegex,
  5674. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5675. date('Y', $now),
  5676. '/option',
  5677. '*/select',
  5678. ['select' => ['name' => 'Contact[date][month]']],
  5679. ['option' => ['value' => '']],
  5680. '/option',
  5681. $monthsRegex,
  5682. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5683. date('F', $now),
  5684. '/option',
  5685. '*/select',
  5686. ['select' => ['name' => 'Contact[date][day]']],
  5687. ['option' => ['value' => '']],
  5688. '/option',
  5689. $daysRegex,
  5690. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5691. date('j', $now),
  5692. '/option',
  5693. '*/select',
  5694. ['select' => ['name' => 'Contact[date][hour]']],
  5695. ['option' => ['value' => '']],
  5696. '/option',
  5697. $hoursRegex,
  5698. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5699. date('G', $now),
  5700. '/option',
  5701. '*/select',
  5702. ['select' => ['name' => 'Contact[date][minute]']],
  5703. ['option' => ['value' => '']],
  5704. '/option',
  5705. $minutesRegex,
  5706. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5707. date('i', $now),
  5708. '/option',
  5709. '*/select',
  5710. ];
  5711. $this->assertHtml($expected, $result);
  5712. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5713. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5714. $now = strtotime('now');
  5715. $expected = [
  5716. ['select' => ['name' => 'Contact[date][year]']],
  5717. $yearsRegex,
  5718. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5719. date('Y', $now),
  5720. '/option',
  5721. '*/select',
  5722. ['select' => ['name' => 'Contact[date][month]']],
  5723. $monthsRegex,
  5724. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5725. date('F', $now),
  5726. '/option',
  5727. '*/select',
  5728. ['select' => ['name' => 'Contact[date][day]']],
  5729. $daysRegex,
  5730. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5731. date('j', $now),
  5732. '/option',
  5733. '*/select',
  5734. ['select' => ['name' => 'Contact[date][hour]']],
  5735. $hoursRegex,
  5736. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5737. date('G', $now),
  5738. '/option',
  5739. '*/select',
  5740. ['select' => ['name' => 'Contact[date][minute]']],
  5741. $minutesRegex,
  5742. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5743. date('i', $now),
  5744. '/option',
  5745. '*/select',
  5746. ];
  5747. $this->assertHtml($expected, $result);
  5748. }
  5749. /**
  5750. * testDateTimeSecured method
  5751. *
  5752. * Test that datetime fields are added to protected fields list.
  5753. *
  5754. * @return void
  5755. */
  5756. public function testDateTimeSecured()
  5757. {
  5758. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5759. $this->Form->dateTime('Contact.date');
  5760. $expected = [
  5761. 'Contact.date.year',
  5762. 'Contact.date.month',
  5763. 'Contact.date.day',
  5764. 'Contact.date.hour',
  5765. 'Contact.date.minute',
  5766. ];
  5767. $this->assertEquals($expected, $this->Form->fields);
  5768. $this->Form->fields = [];
  5769. $this->Form->date('Contact.published');
  5770. $expected = [
  5771. 'Contact.published.year',
  5772. 'Contact.published.month',
  5773. 'Contact.published.day',
  5774. ];
  5775. $this->assertEquals($expected, $this->Form->fields);
  5776. }
  5777. /**
  5778. * testDateTimeSecuredDisabled method
  5779. *
  5780. * Test that datetime fields are added to protected fields list.
  5781. *
  5782. * @return void
  5783. */
  5784. public function testDateTimeSecuredDisabled()
  5785. {
  5786. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5787. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5788. $expected = [];
  5789. $this->assertEquals($expected, $this->Form->fields);
  5790. $this->Form->fields = [];
  5791. $this->Form->date('Contact.published', ['secure' => false]);
  5792. $expected = [];
  5793. $this->assertEquals($expected, $this->Form->fields);
  5794. }
  5795. /**
  5796. * testDatetimeEmpty method
  5797. *
  5798. * Test empty defaulting to true for datetime.
  5799. *
  5800. * @return void
  5801. */
  5802. public function testDatetimeEmpty()
  5803. {
  5804. extract($this->dateRegex);
  5805. $result = $this->Form->dateTime('Contact.date', [
  5806. 'timeFormat' => 12,
  5807. 'empty' => true,
  5808. 'default' => true
  5809. ]);
  5810. $expected = [
  5811. ['select' => ['name' => 'Contact[date][year]']],
  5812. $yearsRegex,
  5813. ['option' => ['value' => '']],
  5814. '/option',
  5815. '*/select',
  5816. ['select' => ['name' => 'Contact[date][month]']],
  5817. $monthsRegex,
  5818. ['option' => ['value' => '']],
  5819. '/option',
  5820. '*/select',
  5821. ['select' => ['name' => 'Contact[date][day]']],
  5822. $daysRegex,
  5823. ['option' => ['value' => '']],
  5824. '/option',
  5825. '*/select',
  5826. ['select' => ['name' => 'Contact[date][hour]']],
  5827. $hoursRegex,
  5828. ['option' => ['value' => '']],
  5829. '/option',
  5830. '*/select',
  5831. ['select' => ['name' => 'Contact[date][minute]']],
  5832. $minutesRegex,
  5833. ['option' => ['value' => '']],
  5834. '/option',
  5835. '*/select',
  5836. ['select' => ['name' => 'Contact[date][meridian]']],
  5837. $meridianRegex,
  5838. ['option' => ['value' => '']],
  5839. '/option',
  5840. '*/select'
  5841. ];
  5842. $this->assertHtml($expected, $result);
  5843. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5844. }
  5845. /**
  5846. * testDatetimeMinuteInterval method
  5847. *
  5848. * Test datetime with interval option.
  5849. *
  5850. * @return void
  5851. */
  5852. public function testDatetimeMinuteInterval()
  5853. {
  5854. extract($this->dateRegex);
  5855. $result = $this->Form->dateTime('Contact.date', [
  5856. 'interval' => 5,
  5857. 'value' => ''
  5858. ]);
  5859. $expected = [
  5860. ['select' => ['name' => 'Contact[date][year]']],
  5861. $yearsRegex,
  5862. ['option' => ['selected' => 'selected', 'value' => '']],
  5863. '/option',
  5864. '*/select',
  5865. ['select' => ['name' => 'Contact[date][month]']],
  5866. $monthsRegex,
  5867. ['option' => ['selected' => 'selected', 'value' => '']],
  5868. '/option',
  5869. '*/select',
  5870. ['select' => ['name' => 'Contact[date][day]']],
  5871. $daysRegex,
  5872. ['option' => ['selected' => 'selected', 'value' => '']],
  5873. '/option',
  5874. '*/select',
  5875. ['select' => ['name' => 'Contact[date][hour]']],
  5876. $hoursRegex,
  5877. ['option' => ['selected' => 'selected', 'value' => '']],
  5878. '/option',
  5879. '*/select',
  5880. ['select' => ['name' => 'Contact[date][minute]']],
  5881. $minutesRegex,
  5882. ['option' => ['selected' => 'selected', 'value' => '']],
  5883. '/option',
  5884. ['option' => ['value' => '00']],
  5885. '00',
  5886. '/option',
  5887. ['option' => ['value' => '05']],
  5888. '05',
  5889. '/option',
  5890. ['option' => ['value' => '10']],
  5891. '10',
  5892. '/option',
  5893. '*/select',
  5894. ];
  5895. $this->assertHtml($expected, $result);
  5896. }
  5897. /**
  5898. * testDateTimeRounding method
  5899. *
  5900. * Test dateTime with rounding.
  5901. *
  5902. * @return void
  5903. */
  5904. public function testDateTimeRounding()
  5905. {
  5906. $this->Form->request = $this->Form->request->withData('Contact', [
  5907. 'date' => [
  5908. 'day' => '13',
  5909. 'month' => '12',
  5910. 'year' => '2010',
  5911. 'hour' => '04',
  5912. 'minute' => '19',
  5913. 'meridian' => 'AM'
  5914. ]
  5915. ]);
  5916. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5917. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5918. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5919. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5920. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5921. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5922. }
  5923. /**
  5924. * testDatetimeWithDefault method
  5925. *
  5926. * Test that datetime() and default values work.
  5927. *
  5928. * @return void
  5929. */
  5930. public function testDatetimeWithDefault()
  5931. {
  5932. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5933. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5934. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5935. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5936. $result = $this->Form->dateTime('Contact.updated', [
  5937. 'default' => '2009-06-01 11:15:30'
  5938. ]);
  5939. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5940. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5941. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5942. }
  5943. /**
  5944. * testDateTimeAllZeros method
  5945. *
  5946. * Test datetime() with all zeros.
  5947. *
  5948. * @return void
  5949. */
  5950. public function testDateTimeAllZeros()
  5951. {
  5952. $result = $this->Form->dateTime('Contact.date', [
  5953. 'timeFormat' => false,
  5954. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5955. 'value' => '0000-00-00'
  5956. ]);
  5957. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5958. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5959. }
  5960. /**
  5961. * testDateTimeEmptyAsArray method
  5962. *
  5963. * @return void
  5964. */
  5965. public function testDateTimeEmptyAsArray()
  5966. {
  5967. $result = $this->Form->dateTime('Contact.date', [
  5968. 'empty' => [
  5969. 'day' => 'DAY',
  5970. 'month' => 'MONTH',
  5971. 'year' => 'YEAR',
  5972. 'hour' => 'HOUR',
  5973. 'minute' => 'MINUTE',
  5974. 'meridian' => false
  5975. ],
  5976. 'default' => true
  5977. ]);
  5978. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5979. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5980. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5981. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5982. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5983. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5984. $result = $this->Form->dateTime('Contact.date', [
  5985. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5986. 'default' => true
  5987. ]);
  5988. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5989. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5990. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5991. }
  5992. /**
  5993. * testFormDateTimeMulti method
  5994. *
  5995. * Test multiple datetime element generation.
  5996. *
  5997. * @return void
  5998. */
  5999. public function testFormDateTimeMulti()
  6000. {
  6001. extract($this->dateRegex);
  6002. $result = $this->Form->dateTime('Contact.1.updated');
  6003. $this->assertContains('Contact[1][updated][month]', $result);
  6004. $this->assertContains('Contact[1][updated][day]', $result);
  6005. $this->assertContains('Contact[1][updated][year]', $result);
  6006. $this->assertContains('Contact[1][updated][hour]', $result);
  6007. $this->assertContains('Contact[1][updated][minute]', $result);
  6008. }
  6009. /**
  6010. * testDateTimeLabelIdMatchesFirstControl method
  6011. *
  6012. * When changing the date format, the label should always focus the first select box when
  6013. * clicked.
  6014. *
  6015. * @return void
  6016. */
  6017. public function testDateTimeLabelIdMatchesFirstControl()
  6018. {
  6019. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6020. $this->assertContains('<label>Date</label>', $result);
  6021. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6022. $this->assertContains('<label>Date</label>', $result);
  6023. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6024. $this->assertContains('<label>Date</label>', $result);
  6025. }
  6026. /**
  6027. * testDateTimeSecondOptions method
  6028. *
  6029. * Test datetime second=true.
  6030. *
  6031. * @return void
  6032. */
  6033. public function testDateTimeSecondOptions()
  6034. {
  6035. $result = $this->Form->dateTime('updated', ['second' => true]);
  6036. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6037. $result = $this->Form->dateTime('updated', ['second' => []]);
  6038. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6039. $result = $this->Form->dateTime('updated', ['second' => null]);
  6040. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6041. $result = $this->Form->dateTime('updated', ['second' => false]);
  6042. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6043. }
  6044. /**
  6045. * testMonth method
  6046. *
  6047. * Test generation of a month input.
  6048. *
  6049. * @return void
  6050. */
  6051. public function testMonth()
  6052. {
  6053. $result = $this->Form->month('Model.field', ['value' => '']);
  6054. $expected = [
  6055. ['select' => ['name' => 'Model[field][month]']],
  6056. ['option' => ['value' => '', 'selected' => 'selected']],
  6057. '/option',
  6058. ['option' => ['value' => '01']],
  6059. date('F', strtotime('2008-01-01 00:00:00')),
  6060. '/option',
  6061. ['option' => ['value' => '02']],
  6062. date('F', strtotime('2008-02-01 00:00:00')),
  6063. '/option',
  6064. '*/select',
  6065. ];
  6066. $this->assertHtml($expected, $result);
  6067. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6068. $expected = [
  6069. ['select' => ['name' => 'Model[field][month]']],
  6070. ['option' => ['selected' => 'selected', 'value' => '']],
  6071. '/option',
  6072. ['option' => ['value' => '01']],
  6073. date('F', strtotime('2008-01-01 00:00:00')),
  6074. '/option',
  6075. ['option' => ['value' => '02']],
  6076. date('F', strtotime('2008-02-01 00:00:00')),
  6077. '/option',
  6078. '*/select',
  6079. ];
  6080. $this->assertHtml($expected, $result);
  6081. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6082. $expected = [
  6083. ['select' => ['name' => 'Model[field][month]']],
  6084. ['option' => ['selected' => 'selected', 'value' => '']],
  6085. '/option',
  6086. ['option' => ['value' => '01']],
  6087. '1',
  6088. '/option',
  6089. ['option' => ['value' => '02']],
  6090. '2',
  6091. '/option',
  6092. '*/select',
  6093. ];
  6094. $this->assertHtml($expected, $result);
  6095. $monthNames = [
  6096. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6097. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6098. ];
  6099. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6100. $expected = [
  6101. ['select' => ['name' => 'Model[field][month]']],
  6102. ['option' => ['value' => '']],
  6103. '/option',
  6104. ['option' => ['value' => '01', 'selected' => 'selected']],
  6105. 'Jan',
  6106. '/option',
  6107. ['option' => ['value' => '02']],
  6108. 'Feb',
  6109. '/option',
  6110. '*/select',
  6111. ];
  6112. $this->assertHtml($expected, $result);
  6113. $this->Form->request = $this->Form->request->withData('Project.release', '2050-02-10');
  6114. $this->Form->create();
  6115. $result = $this->Form->month('Project.release');
  6116. $expected = [
  6117. ['select' => ['name' => 'Project[release][month]']],
  6118. ['option' => ['value' => '']],
  6119. '/option',
  6120. ['option' => ['value' => '01']],
  6121. 'January',
  6122. '/option',
  6123. ['option' => ['value' => '02', 'selected' => 'selected']],
  6124. 'February',
  6125. '/option',
  6126. '*/select',
  6127. ];
  6128. $this->assertHtml($expected, $result);
  6129. $result = $this->Form->month('Contact.published', [
  6130. 'empty' => 'Published on',
  6131. ]);
  6132. $this->assertContains('Published on', $result);
  6133. }
  6134. /**
  6135. * testDay method
  6136. *
  6137. * Test generation of a day input.
  6138. *
  6139. * @return void
  6140. */
  6141. public function testDay()
  6142. {
  6143. extract($this->dateRegex);
  6144. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6145. $expected = [
  6146. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6147. ['option' => ['selected' => 'selected', 'value' => '']],
  6148. '/option',
  6149. ['option' => ['value' => '01']],
  6150. '1',
  6151. '/option',
  6152. ['option' => ['value' => '02']],
  6153. '2',
  6154. '/option',
  6155. $daysRegex,
  6156. '/select',
  6157. ];
  6158. $this->assertHtml($expected, $result);
  6159. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 23:12:32');
  6160. $this->Form->create();
  6161. $result = $this->Form->day('Model.field');
  6162. $expected = [
  6163. ['select' => ['name' => 'Model[field][day]']],
  6164. ['option' => ['value' => '']],
  6165. '/option',
  6166. ['option' => ['value' => '01']],
  6167. '1',
  6168. '/option',
  6169. ['option' => ['value' => '02']],
  6170. '2',
  6171. '/option',
  6172. $daysRegex,
  6173. ['option' => ['value' => '10', 'selected' => 'selected']],
  6174. '10',
  6175. '/option',
  6176. $daysRegex,
  6177. '/select',
  6178. ];
  6179. $this->assertHtml($expected, $result);
  6180. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6181. $this->Form->create();
  6182. $result = $this->Form->day('Model.field', ['value' => '10']);
  6183. $expected = [
  6184. ['select' => ['name' => 'Model[field][day]']],
  6185. ['option' => ['value' => '']],
  6186. '/option',
  6187. ['option' => ['value' => '01']],
  6188. '1',
  6189. '/option',
  6190. ['option' => ['value' => '02']],
  6191. '2',
  6192. '/option',
  6193. $daysRegex,
  6194. ['option' => ['value' => '10', 'selected' => 'selected']],
  6195. '10',
  6196. '/option',
  6197. $daysRegex,
  6198. '/select',
  6199. ];
  6200. $this->assertHtml($expected, $result);
  6201. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6202. $this->Form->create();
  6203. $result = $this->Form->day('Project.release');
  6204. $expected = [
  6205. ['select' => ['name' => 'Project[release][day]']],
  6206. ['option' => ['value' => '']],
  6207. '/option',
  6208. ['option' => ['value' => '01']],
  6209. '1',
  6210. '/option',
  6211. ['option' => ['value' => '02']],
  6212. '2',
  6213. '/option',
  6214. $daysRegex,
  6215. ['option' => ['value' => '10', 'selected' => 'selected']],
  6216. '10',
  6217. '/option',
  6218. $daysRegex,
  6219. '/select',
  6220. ];
  6221. $this->assertHtml($expected, $result);
  6222. $result = $this->Form->day('Contact.published', [
  6223. 'empty' => 'Published on',
  6224. ]);
  6225. $this->assertContains('Published on', $result);
  6226. }
  6227. /**
  6228. * testMinute method
  6229. *
  6230. * Test generation of a minute input.
  6231. *
  6232. * @return void
  6233. */
  6234. public function testMinute()
  6235. {
  6236. extract($this->dateRegex);
  6237. $result = $this->Form->minute('Model.field', ['value' => '']);
  6238. $expected = [
  6239. ['select' => ['name' => 'Model[field][minute]']],
  6240. ['option' => ['selected' => 'selected', 'value' => '']],
  6241. '/option',
  6242. ['option' => ['value' => '00']],
  6243. '00',
  6244. '/option',
  6245. ['option' => ['value' => '01']],
  6246. '01',
  6247. '/option',
  6248. ['option' => ['value' => '02']],
  6249. '02',
  6250. '/option',
  6251. $minutesRegex,
  6252. '/select',
  6253. ];
  6254. $this->assertHtml($expected, $result);
  6255. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6256. $this->Form->create();
  6257. $result = $this->Form->minute('Model.field');
  6258. $expected = [
  6259. ['select' => ['name' => 'Model[field][minute]']],
  6260. ['option' => ['value' => '']],
  6261. '/option',
  6262. ['option' => ['value' => '00']],
  6263. '00',
  6264. '/option',
  6265. ['option' => ['value' => '01']],
  6266. '01',
  6267. '/option',
  6268. ['option' => ['value' => '02']],
  6269. '02',
  6270. '/option',
  6271. $minutesRegex,
  6272. ['option' => ['value' => '12', 'selected' => 'selected']],
  6273. '12',
  6274. '/option',
  6275. $minutesRegex,
  6276. '/select',
  6277. ];
  6278. $this->assertHtml($expected, $result);
  6279. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6280. $this->Form->create();
  6281. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6282. $expected = [
  6283. ['select' => ['name' => 'Model[field][minute]']],
  6284. ['option' => ['selected' => 'selected', 'value' => '']],
  6285. '/option',
  6286. ['option' => ['value' => '00']],
  6287. '00',
  6288. '/option',
  6289. ['option' => ['value' => '05']],
  6290. '05',
  6291. '/option',
  6292. ['option' => ['value' => '10']],
  6293. '10',
  6294. '/option',
  6295. $minutesRegex,
  6296. '/select',
  6297. ];
  6298. $this->assertHtml($expected, $result);
  6299. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:10:32');
  6300. $this->Form->create();
  6301. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6302. $expected = [
  6303. ['select' => ['name' => 'Model[field][minute]']],
  6304. ['option' => ['value' => '']],
  6305. '/option',
  6306. ['option' => ['value' => '00']],
  6307. '00',
  6308. '/option',
  6309. ['option' => ['value' => '05']],
  6310. '05',
  6311. '/option',
  6312. ['option' => ['value' => '10', 'selected' => 'selected']],
  6313. '10',
  6314. '/option',
  6315. $minutesRegex,
  6316. '/select',
  6317. ];
  6318. $this->assertHtml($expected, $result);
  6319. }
  6320. /**
  6321. * testMeridian method
  6322. *
  6323. * Test generating an input for the meridian.
  6324. *
  6325. * @return void
  6326. */
  6327. public function testMeridian()
  6328. {
  6329. extract($this->dateRegex);
  6330. $now = new \DateTime();
  6331. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6332. $expected = [
  6333. ['select' => ['name' => 'Model[field][meridian]']],
  6334. ['option' => ['value' => '']],
  6335. '/option',
  6336. $meridianRegex,
  6337. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6338. $now->format('a'),
  6339. '/option',
  6340. '*/select'
  6341. ];
  6342. $this->assertHtml($expected, $result);
  6343. }
  6344. /**
  6345. * testHour method
  6346. *
  6347. * Test generation of an hour input.
  6348. *
  6349. * @return void
  6350. */
  6351. public function testHour()
  6352. {
  6353. extract($this->dateRegex);
  6354. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6355. $expected = [
  6356. ['select' => ['name' => 'Model[field][hour]']],
  6357. ['option' => ['selected' => 'selected', 'value' => '']],
  6358. '/option',
  6359. ['option' => ['value' => '01']],
  6360. '1',
  6361. '/option',
  6362. ['option' => ['value' => '02']],
  6363. '2',
  6364. '/option',
  6365. $hoursRegex,
  6366. '/select',
  6367. ];
  6368. $this->assertHtml($expected, $result);
  6369. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6370. $this->Form->create();
  6371. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6372. $expected = [
  6373. ['select' => ['name' => 'Model[field][hour]']],
  6374. ['option' => ['value' => '']],
  6375. '/option',
  6376. ['option' => ['value' => '01']],
  6377. '1',
  6378. '/option',
  6379. ['option' => ['value' => '02']],
  6380. '2',
  6381. '/option',
  6382. $hoursRegex,
  6383. ['option' => ['value' => '12', 'selected' => 'selected']],
  6384. '12',
  6385. '/option',
  6386. '/select',
  6387. ];
  6388. $this->assertHtml($expected, $result);
  6389. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6390. $this->Form->create();
  6391. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6392. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6393. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6394. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6395. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6396. $this->Form->create();
  6397. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6398. $expected = [
  6399. ['select' => ['name' => 'Model[field][hour]']],
  6400. ['option' => ['value' => '']],
  6401. '/option',
  6402. ['option' => ['value' => '00', 'selected' => 'selected']],
  6403. '0',
  6404. '/option',
  6405. ['option' => ['value' => '01']],
  6406. '1',
  6407. '/option',
  6408. ['option' => ['value' => '02']],
  6409. '2',
  6410. '/option',
  6411. $hoursRegex,
  6412. '/select',
  6413. ];
  6414. $this->assertHtml($expected, $result);
  6415. $this->Form->request = $this->Form->request->withData('Model.field', null);
  6416. $this->Form->create();
  6417. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6418. $thisHour = date('H');
  6419. $optValue = date('G');
  6420. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6421. $this->Form->request = $this->Form->request->withData('Model.field', '2050-10-10 01:12:32');
  6422. $this->Form->create();
  6423. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6424. $expected = [
  6425. ['select' => ['name' => 'Model[field][hour]']],
  6426. ['option' => ['value' => '']],
  6427. '/option',
  6428. ['option' => ['value' => '00']],
  6429. '0',
  6430. '/option',
  6431. ['option' => ['value' => '01', 'selected' => 'selected']],
  6432. '1',
  6433. '/option',
  6434. ['option' => ['value' => '02']],
  6435. '2',
  6436. '/option',
  6437. $hoursRegex,
  6438. '/select',
  6439. ];
  6440. $this->assertHtml($expected, $result);
  6441. }
  6442. /**
  6443. * testYear method
  6444. *
  6445. * Test generation of a year input.
  6446. *
  6447. * @return void
  6448. */
  6449. public function testYear()
  6450. {
  6451. $this->Form->request = $this->Form->request->withData('Contact.published', '2006-10-10');
  6452. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6453. $expected = [
  6454. ['select' => ['name' => 'Model[field][year]']],
  6455. ['option' => ['selected' => 'selected', 'value' => '']],
  6456. '/option',
  6457. ['option' => ['value' => '2007']],
  6458. '2007',
  6459. '/option',
  6460. ['option' => ['value' => '2006']],
  6461. '2006',
  6462. '/option',
  6463. '/select',
  6464. ];
  6465. $this->assertHtml($expected, $result);
  6466. $result = $this->Form->year('Model.field', [
  6467. 'value' => '',
  6468. 'minYear' => 2006,
  6469. 'maxYear' => 2007,
  6470. 'orderYear' => 'asc'
  6471. ]);
  6472. $expected = [
  6473. ['select' => ['name' => 'Model[field][year]']],
  6474. ['option' => ['selected' => 'selected', 'value' => '']],
  6475. '/option',
  6476. ['option' => ['value' => '2006']],
  6477. '2006',
  6478. '/option',
  6479. ['option' => ['value' => '2007']],
  6480. '2007',
  6481. '/option',
  6482. '/select',
  6483. ];
  6484. $this->assertHtml($expected, $result);
  6485. $result = $this->Form->year('Contact.published', [
  6486. 'empty' => false,
  6487. 'minYear' => 2006,
  6488. 'maxYear' => 2007,
  6489. ]);
  6490. $expected = [
  6491. ['select' => ['name' => 'Contact[published][year]']],
  6492. ['option' => ['value' => '2007']],
  6493. '2007',
  6494. '/option',
  6495. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6496. '2006',
  6497. '/option',
  6498. '/select',
  6499. ];
  6500. $this->assertHtml($expected, $result);
  6501. $result = $this->Form->year('Contact.published', [
  6502. 'empty' => 'Published on',
  6503. ]);
  6504. $this->assertContains('Published on', $result);
  6505. }
  6506. /**
  6507. * testControlDatetimePreEpoch method
  6508. *
  6509. * Test minYear being prior to the unix epoch.
  6510. *
  6511. * @return void
  6512. */
  6513. public function testControlDatetimePreEpoch()
  6514. {
  6515. $start = date('Y') - 80;
  6516. $end = date('Y') - 18;
  6517. $result = $this->Form->control('birth_year', [
  6518. 'type' => 'date',
  6519. 'label' => 'Birth Year',
  6520. 'minYear' => $start,
  6521. 'maxYear' => $end,
  6522. 'month' => false,
  6523. 'day' => false,
  6524. ]);
  6525. $this->assertContains('value="' . $start . '">' . $start, $result);
  6526. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6527. $this->assertNotContains('value="00">00', $result);
  6528. }
  6529. /**
  6530. * test control() datetime & required attributes
  6531. *
  6532. * @return void
  6533. */
  6534. public function testControlDatetimeRequired()
  6535. {
  6536. $result = $this->Form->control('birthday', [
  6537. 'type' => 'date',
  6538. 'required' => true
  6539. ]);
  6540. $this->assertContains(
  6541. '<select name="birthday[year]" required="required"',
  6542. $result
  6543. );
  6544. }
  6545. /**
  6546. * testYearAutoExpandRange method
  6547. *
  6548. * @return void
  6549. */
  6550. public function testYearAutoExpandRange()
  6551. {
  6552. $this->Form->request = $this->Form->request->withData('User.birthday', '1930-10-10');
  6553. $result = $this->Form->year('User.birthday');
  6554. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6555. $result = $matches[1];
  6556. $expected = range(date('Y') + 5, 1930);
  6557. $this->assertEquals($expected, $result);
  6558. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6559. $this->Form->create();
  6560. $result = $this->Form->year('Project.release');
  6561. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6562. $result = $matches[1];
  6563. $expected = range(2050, date('Y') - 5);
  6564. $this->assertEquals($expected, $result);
  6565. $this->Form->request = $this->Form->request->withData('Project.release', '1881-10-10');
  6566. $this->Form->create();
  6567. $result = $this->Form->year('Project.release', [
  6568. 'minYear' => 1890,
  6569. 'maxYear' => 1900
  6570. ]);
  6571. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6572. $result = $matches[1];
  6573. $expected = range(1900, 1881);
  6574. $this->assertEquals($expected, $result);
  6575. }
  6576. /**
  6577. * testControlDate method
  6578. *
  6579. * Test that control() accepts the type of date and passes options in.
  6580. *
  6581. * @return void
  6582. */
  6583. public function testControlDate()
  6584. {
  6585. $this->Form->request = $this->Form->request->withParsedBody([
  6586. 'month_year' => ['month' => date('m')],
  6587. ]);
  6588. $this->Form->create($this->article);
  6589. $result = $this->Form->control('month_year', [
  6590. 'label' => false,
  6591. 'type' => 'date',
  6592. 'minYear' => 2006,
  6593. 'maxYear' => 2008
  6594. ]);
  6595. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6596. $this->assertNotContains('value="2008" selected="selected"', $result);
  6597. }
  6598. /**
  6599. * testControlLabelFalse method
  6600. *
  6601. * Test the label option being set to false.
  6602. *
  6603. * @return void
  6604. */
  6605. public function testControlLabelFalse()
  6606. {
  6607. $this->Form->create($this->article);
  6608. $result = $this->Form->control('title', ['label' => false]);
  6609. $expected = [
  6610. 'div' => ['class' => 'input text required'],
  6611. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6612. '/div'
  6613. ];
  6614. $this->assertHtml($expected, $result);
  6615. }
  6616. /**
  6617. * testControlDateMaxYear method
  6618. *
  6619. * Let's say we want to only allow users born from 2006 to 2008 to register
  6620. * This being the first signup page, we still don't have any data.
  6621. *
  6622. * @return void
  6623. */
  6624. public function testControlDateMaxYear()
  6625. {
  6626. $this->Form->create($this->article);
  6627. $result = $this->Form->control('birthday', [
  6628. 'label' => false,
  6629. 'type' => 'date',
  6630. 'minYear' => 2006,
  6631. 'maxYear' => 2008,
  6632. 'default' => true
  6633. ]);
  6634. $this->assertContains('value="2008" selected="selected"', $result);
  6635. $this->assertContains('value="2006"', $result);
  6636. $this->assertNotContains('value="2005"', $result);
  6637. $this->assertNotContains('value="2009"', $result);
  6638. }
  6639. /**
  6640. * testTextArea method
  6641. *
  6642. * Test generation of a textarea input.
  6643. *
  6644. * @return void
  6645. */
  6646. public function testTextArea()
  6647. {
  6648. $this->Form->request = $this->Form->request->withData('field', 'some test data');
  6649. $result = $this->Form->textarea('field');
  6650. $expected = [
  6651. 'textarea' => ['name' => 'field', 'rows' => 5],
  6652. 'some test data',
  6653. '/textarea',
  6654. ];
  6655. $this->assertHtml($expected, $result);
  6656. $result = $this->Form->textarea('user.bio');
  6657. $expected = [
  6658. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6659. '/textarea',
  6660. ];
  6661. $this->assertHtml($expected, $result);
  6662. $this->Form->request = $this->Form->request
  6663. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  6664. $this->Form->create();
  6665. $result = $this->Form->textarea('field');
  6666. $expected = [
  6667. 'textarea' => ['name' => 'field', 'rows' => 5],
  6668. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6669. '/textarea',
  6670. ];
  6671. $this->assertHtml($expected, $result);
  6672. $this->Form->request = $this->Form->request->withData(
  6673. 'Model.field',
  6674. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6675. );
  6676. $this->Form->create();
  6677. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6678. $expected = [
  6679. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6680. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6681. '/textarea',
  6682. ];
  6683. $this->assertHtml($expected, $result);
  6684. $result = $this->Form->textarea('0.OtherModel.field');
  6685. $expected = [
  6686. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6687. '/textarea'
  6688. ];
  6689. $this->assertHtml($expected, $result);
  6690. }
  6691. /**
  6692. * testTextAreaWithStupidCharacters method
  6693. *
  6694. * Test text area with non-ascii characters.
  6695. *
  6696. * @return void
  6697. */
  6698. public function testTextAreaWithStupidCharacters()
  6699. {
  6700. $result = $this->Form->textarea('Post.content', [
  6701. 'value' => 'GREAT®',
  6702. 'rows' => '15',
  6703. 'cols' => '75'
  6704. ]);
  6705. $expected = [
  6706. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6707. 'GREAT®',
  6708. '/textarea',
  6709. ];
  6710. $this->assertHtml($expected, $result);
  6711. }
  6712. /**
  6713. * testTextAreaMaxLength method
  6714. *
  6715. * Test textareas maxlength read from schema.
  6716. *
  6717. * @return void
  6718. */
  6719. public function testTextAreaMaxLength()
  6720. {
  6721. $this->Form->create([
  6722. 'schema' => [
  6723. 'stuff' => ['type' => 'string', 'length' => 10],
  6724. ]
  6725. ]);
  6726. $result = $this->Form->control('other', ['type' => 'textarea']);
  6727. $expected = [
  6728. 'div' => ['class' => 'input textarea'],
  6729. 'label' => ['for' => 'other'],
  6730. 'Other',
  6731. '/label',
  6732. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6733. '/textarea',
  6734. '/div'
  6735. ];
  6736. $this->assertHtml($expected, $result);
  6737. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6738. $expected = [
  6739. 'div' => ['class' => 'input textarea'],
  6740. 'label' => ['for' => 'stuff'],
  6741. 'Stuff',
  6742. '/label',
  6743. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6744. '/textarea',
  6745. '/div'
  6746. ];
  6747. $this->assertHtml($expected, $result);
  6748. }
  6749. /**
  6750. * testHiddenField method
  6751. *
  6752. * Test generation of a hidden input.
  6753. *
  6754. * @return void
  6755. */
  6756. public function testHiddenField()
  6757. {
  6758. $this->article['errors'] = [
  6759. 'field' => true
  6760. ];
  6761. $this->Form->request = $this->Form->request->withData('field', 'test');
  6762. $this->Form->create($this->article);
  6763. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6764. $expected = [
  6765. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6766. $this->assertHtml($expected, $result);
  6767. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6768. $expected = [
  6769. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6770. ];
  6771. $this->assertHtml($expected, $result);
  6772. }
  6773. /**
  6774. * testFileUploadField method
  6775. *
  6776. * Test generation of a file upload input.
  6777. *
  6778. * @return void
  6779. */
  6780. public function testFileUploadField()
  6781. {
  6782. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6783. $result = $this->Form->file('Model.upload');
  6784. $this->assertHtml($expected, $result);
  6785. $this->Form->request = $this->Form->request->withData('Model.upload', [
  6786. 'name' => '', 'type' => '', 'tmp_name' => '',
  6787. 'error' => 4, 'size' => 0
  6788. ]);
  6789. $result = $this->Form->file('Model.upload');
  6790. $this->assertHtml($expected, $result);
  6791. $this->Form->request = $this->Form->request->withData('Model.upload', 'no data should be set in value');
  6792. $result = $this->Form->file('Model.upload');
  6793. $this->assertHtml($expected, $result);
  6794. }
  6795. /**
  6796. * testFileUploadOnOtherModel method
  6797. *
  6798. * Test File upload input on a model not used in create().
  6799. *
  6800. * @return void
  6801. */
  6802. public function testFileUploadOnOtherModel()
  6803. {
  6804. $this->Form->create($this->article, ['type' => 'file']);
  6805. $result = $this->Form->file('ValidateProfile.city');
  6806. $expected = [
  6807. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6808. ];
  6809. $this->assertHtml($expected, $result);
  6810. }
  6811. /**
  6812. * testButton method
  6813. *
  6814. * Test generation of a form button.
  6815. *
  6816. * @return void
  6817. */
  6818. public function testButton()
  6819. {
  6820. $result = $this->Form->button('Hi');
  6821. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6822. $this->assertHtml($expected, $result);
  6823. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6824. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6825. $this->assertHtml($expected, $result);
  6826. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6827. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6828. $this->assertHtml($expected, $result);
  6829. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6830. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6831. $this->assertHtml($expected, $result);
  6832. $result = $this->Form->button('No type', ['type' => false]);
  6833. $expected = ['button' => [], 'No type', '/button'];
  6834. $this->assertHtml($expected, $result);
  6835. $result = $this->Form->button('Upload Text', [
  6836. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6837. 'escape' => false
  6838. ]);
  6839. $this->assertNotRegExp('/\&039/', $result);
  6840. }
  6841. /**
  6842. * testButtonUnlockedByDefault method
  6843. *
  6844. * Test that button() makes unlocked fields by default.
  6845. *
  6846. * @return void
  6847. */
  6848. public function testButtonUnlockedByDefault()
  6849. {
  6850. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'secured');
  6851. $this->Form->button('Save', ['name' => 'save']);
  6852. $this->Form->button('Clear');
  6853. $result = $this->Form->unlockField();
  6854. $this->assertEquals(['save'], $result);
  6855. }
  6856. /**
  6857. * Test generation of a form button with confirm message.
  6858. *
  6859. * @return void
  6860. */
  6861. public function testButtonWithConfirm()
  6862. {
  6863. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6864. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6865. $this->assertHtml($expected, $result);
  6866. }
  6867. /**
  6868. * testPostButton method
  6869. *
  6870. * @return void
  6871. */
  6872. public function testPostButton()
  6873. {
  6874. $result = $this->Form->postButton('Hi', '/controller/action');
  6875. $expected = [
  6876. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6877. 'div' => ['style' => 'display:none;'],
  6878. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6879. '/div',
  6880. 'button' => ['type' => 'submit'],
  6881. 'Hi',
  6882. '/button',
  6883. '/form'
  6884. ];
  6885. $this->assertHtml($expected, $result);
  6886. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6887. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6888. }
  6889. /**
  6890. * testPostButtonMethodType method
  6891. *
  6892. * @return void
  6893. */
  6894. public function testPostButtonMethodType()
  6895. {
  6896. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6897. $expected = [
  6898. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6899. 'div' => ['style' => 'display:none;'],
  6900. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6901. '/div',
  6902. 'button' => ['type' => 'submit'],
  6903. 'Hi',
  6904. '/button',
  6905. '/form'
  6906. ];
  6907. $this->assertHtml($expected, $result);
  6908. }
  6909. /**
  6910. * testPostButtonFormOptions method
  6911. *
  6912. * @return void
  6913. */
  6914. public function testPostButtonFormOptions()
  6915. {
  6916. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6917. $expected = [
  6918. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6919. 'div' => ['style' => 'display:none;'],
  6920. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6921. '/div',
  6922. 'button' => ['type' => 'submit'],
  6923. 'Hi',
  6924. '/button',
  6925. '/form'
  6926. ];
  6927. $this->assertHtml($expected, $result);
  6928. }
  6929. /**
  6930. * testPostButtonNestedData method
  6931. *
  6932. * Test using postButton with N dimensional data.
  6933. *
  6934. * @return void
  6935. */
  6936. public function testPostButtonNestedData()
  6937. {
  6938. $data = [
  6939. 'one' => [
  6940. 'two' => [
  6941. 3, 4, 5
  6942. ]
  6943. ]
  6944. ];
  6945. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6946. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6947. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6948. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6949. }
  6950. /**
  6951. * testSecurePostButton method
  6952. *
  6953. * Test that postButton adds _Token fields.
  6954. *
  6955. * @return void
  6956. */
  6957. public function testSecurePostButton()
  6958. {
  6959. $this->Form->request = $this->Form->request
  6960. ->withParam('_csrfToken', 'testkey')
  6961. ->withParam('_Token.unlockedFields', []);
  6962. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6963. $tokenDebug = urlencode(json_encode([
  6964. '/posts/delete/1',
  6965. [],
  6966. []
  6967. ]));
  6968. $expected = [
  6969. 'form' => [
  6970. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6971. ],
  6972. ['div' => ['style' => 'display:none;']],
  6973. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6974. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6975. '/div',
  6976. 'button' => ['type' => 'submit'],
  6977. 'Delete',
  6978. '/button',
  6979. ['div' => ['style' => 'display:none;']],
  6980. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6981. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6982. ['input' => [
  6983. 'type' => 'hidden',
  6984. 'name' => '_Token[debug]',
  6985. 'value' => $tokenDebug,
  6986. 'autocomplete' => 'off',
  6987. ]],
  6988. '/div',
  6989. '/form',
  6990. ];
  6991. $this->assertHtml($expected, $result);
  6992. }
  6993. /**
  6994. * testPostLink method
  6995. *
  6996. * @return void
  6997. */
  6998. public function testPostLink()
  6999. {
  7000. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7001. $expected = [
  7002. 'form' => [
  7003. 'method' => 'post', 'action' => '/posts/delete/1',
  7004. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7005. ],
  7006. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7007. '/form',
  7008. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7009. 'Delete',
  7010. '/a'
  7011. ];
  7012. $this->assertHtml($expected, $result);
  7013. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7014. $expected = [
  7015. 'form' => [
  7016. 'method' => 'post', 'action' => '/posts/delete/1',
  7017. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7018. ],
  7019. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7020. '/form',
  7021. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7022. 'Delete',
  7023. '/a'
  7024. ];
  7025. $this->assertHtml($expected, $result);
  7026. $result = $this->Form->postLink(
  7027. 'Delete',
  7028. '/posts/delete/1',
  7029. ['target' => '_blank', 'class' => 'btn btn-danger']
  7030. );
  7031. $expected = [
  7032. 'form' => [
  7033. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7034. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7035. ],
  7036. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7037. '/form',
  7038. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7039. 'Delete',
  7040. '/a'
  7041. ];
  7042. $this->assertHtml($expected, $result);
  7043. }
  7044. /**
  7045. * testPostLinkWithConfirm method
  7046. *
  7047. * Test the confirm option for postLink().
  7048. *
  7049. * @return void
  7050. */
  7051. public function testPostLinkWithConfirm()
  7052. {
  7053. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7054. $expected = [
  7055. 'form' => [
  7056. 'method' => 'post', 'action' => '/posts/delete/1',
  7057. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7058. ],
  7059. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7060. '/form',
  7061. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7062. 'Delete',
  7063. '/a'
  7064. ];
  7065. $this->assertHtml($expected, $result);
  7066. $result = $this->Form->postLink(
  7067. 'Delete',
  7068. '/posts/delete/1',
  7069. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7070. );
  7071. $expected = [
  7072. 'form' => [
  7073. 'method' => 'post', 'action' => '/posts/delete/1',
  7074. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7075. ],
  7076. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7077. '/form',
  7078. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7079. 'Delete',
  7080. '/a'
  7081. ];
  7082. $this->assertHtml($expected, $result);
  7083. }
  7084. /**
  7085. * testPostLinkWithQuery method
  7086. *
  7087. * Test postLink() with query string args.
  7088. *
  7089. * @return void
  7090. */
  7091. public function testPostLinkWithQuery()
  7092. {
  7093. $result = $this->Form->postLink(
  7094. 'Delete',
  7095. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7096. );
  7097. $expected = [
  7098. 'form' => [
  7099. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7100. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7101. ],
  7102. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7103. '/form',
  7104. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7105. 'Delete',
  7106. '/a'
  7107. ];
  7108. $this->assertHtml($expected, $result);
  7109. }
  7110. /**
  7111. * testPostLinkWithData method
  7112. *
  7113. * Test postLink with additional data.
  7114. *
  7115. * @return void
  7116. */
  7117. public function testPostLinkWithData()
  7118. {
  7119. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7120. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7121. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7122. $this->Form->create($entity);
  7123. $this->Form->end();
  7124. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7125. $this->assertContains(
  7126. '<input type="hidden" name="name" value="show"',
  7127. $result,
  7128. 'should not contain entity data.'
  7129. );
  7130. }
  7131. /**
  7132. * testPostLinkSecurityHash method
  7133. *
  7134. * Test that security hashes for postLink include the url.
  7135. *
  7136. * @return void
  7137. */
  7138. public function testPostLinkSecurityHash()
  7139. {
  7140. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7141. $hash .= '%3Aid';
  7142. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7143. $result = $this->Form->postLink(
  7144. 'Delete',
  7145. '/posts/delete/1',
  7146. ['data' => ['id' => 1]]
  7147. );
  7148. $tokenDebug = urlencode(json_encode([
  7149. '/posts/delete/1',
  7150. [
  7151. 'id' => 1
  7152. ],
  7153. []
  7154. ]));
  7155. $expected = [
  7156. 'form' => [
  7157. 'method' => 'post', 'action' => '/posts/delete/1',
  7158. 'name', 'style' => 'display:none;'
  7159. ],
  7160. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7161. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7162. 'div' => ['style' => 'display:none;'],
  7163. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7164. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7165. ['input' => [
  7166. 'type' => 'hidden',
  7167. 'name' => '_Token[debug]',
  7168. 'value' => $tokenDebug,
  7169. 'autocomplete' => 'off',
  7170. ]],
  7171. '/div',
  7172. '/form',
  7173. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7174. 'Delete',
  7175. '/a'
  7176. ];
  7177. $this->assertHtml($expected, $result);
  7178. }
  7179. /**
  7180. * testPostLinkSecurityHashBlockMode method
  7181. *
  7182. * Test that postLink doesn't modify the fields in the containing form.
  7183. *
  7184. * postLink() calls inside open forms should not modify the field list
  7185. * for the form.
  7186. *
  7187. * @return void
  7188. */
  7189. public function testPostLinkSecurityHashBlockMode()
  7190. {
  7191. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7192. $hash .= '%3A';
  7193. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7194. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7195. $this->Form->control('title');
  7196. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7197. $result = $this->View->fetch('postLink');
  7198. $this->assertEquals(['title'], $this->Form->fields);
  7199. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7200. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7201. }
  7202. /**
  7203. * testPostLinkSecurityHashNoDebugMode method
  7204. *
  7205. * Test that security does not include debug token if debug is false.
  7206. *
  7207. * @return void
  7208. */
  7209. public function testPostLinkSecurityHashNoDebugMode()
  7210. {
  7211. Configure::write('debug', false);
  7212. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7213. $hash .= '%3Aid';
  7214. $this->Form->request = $this->Form->request
  7215. ->withParam('_Token.key', 'test');
  7216. $result = $this->Form->postLink(
  7217. 'Delete',
  7218. '/posts/delete/1',
  7219. ['data' => ['id' => 1]]
  7220. );
  7221. $expected = [
  7222. 'form' => [
  7223. 'method' => 'post', 'action' => '/posts/delete/1',
  7224. 'name', 'style' => 'display:none;'
  7225. ],
  7226. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7227. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7228. 'div' => ['style' => 'display:none;'],
  7229. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7230. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7231. '/div',
  7232. '/form',
  7233. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7234. 'Delete',
  7235. '/a'
  7236. ];
  7237. $this->assertHtml($expected, $result);
  7238. }
  7239. /**
  7240. * testPostLinkNestedData method
  7241. *
  7242. * Test using postLink with N dimensional data.
  7243. *
  7244. * @return void
  7245. */
  7246. public function testPostLinkNestedData()
  7247. {
  7248. $data = [
  7249. 'one' => [
  7250. 'two' => [
  7251. 3, 4, 5
  7252. ]
  7253. ]
  7254. ];
  7255. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7256. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7257. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7258. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7259. }
  7260. /**
  7261. * testPostLinkAfterGetForm method
  7262. *
  7263. * Test creating postLinks after a GET form.
  7264. *
  7265. * @return void
  7266. */
  7267. public function testPostLinkAfterGetForm()
  7268. {
  7269. $this->Form->request = $this->Form->request
  7270. ->withParam('_csrfToken', 'testkey')
  7271. ->withParam('_Token', 'val');
  7272. $this->Form->create($this->article, ['type' => 'get']);
  7273. $this->Form->end();
  7274. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7275. $tokenDebug = urlencode(json_encode([
  7276. '/posts/delete/1',
  7277. [],
  7278. []
  7279. ]));
  7280. $expected = [
  7281. 'form' => [
  7282. 'method' => 'post', 'action' => '/posts/delete/1',
  7283. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7284. ],
  7285. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7286. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7287. 'div' => ['style' => 'display:none;'],
  7288. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7289. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7290. ['input' => [
  7291. 'type' => 'hidden', 'name' => '_Token[debug]',
  7292. 'value' => $tokenDebug,
  7293. 'autocomplete' => 'off'
  7294. ]],
  7295. '/div',
  7296. '/form',
  7297. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7298. 'Delete',
  7299. '/a'
  7300. ];
  7301. $this->assertHtml($expected, $result);
  7302. }
  7303. /**
  7304. * testPostLinkFormBuffer method
  7305. *
  7306. * Test that postLink adds form tags to view block.
  7307. *
  7308. * @return void
  7309. */
  7310. public function testPostLinkFormBuffer()
  7311. {
  7312. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7313. $expected = [
  7314. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7315. 'Delete',
  7316. '/a'
  7317. ];
  7318. $this->assertHtml($expected, $result);
  7319. $result = $this->View->fetch('postLink');
  7320. $expected = [
  7321. 'form' => [
  7322. 'method' => 'post', 'action' => '/posts/delete/1',
  7323. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7324. ],
  7325. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7326. '/form'
  7327. ];
  7328. $this->assertHtml($expected, $result);
  7329. $result = $this->Form->postLink(
  7330. 'Delete',
  7331. '/posts/delete/2',
  7332. ['block' => true, 'method' => 'DELETE']
  7333. );
  7334. $expected = [
  7335. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7336. 'Delete',
  7337. '/a'
  7338. ];
  7339. $this->assertHtml($expected, $result);
  7340. $result = $this->View->fetch('postLink');
  7341. $expected = [
  7342. 'form' => [
  7343. 'method' => 'post', 'action' => '/posts/delete/1',
  7344. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7345. ],
  7346. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7347. '/form',
  7348. [
  7349. 'form' => [
  7350. 'method' => 'post', 'action' => '/posts/delete/2',
  7351. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7352. ],
  7353. ],
  7354. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7355. '/form'
  7356. ];
  7357. $this->assertHtml($expected, $result);
  7358. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7359. $expected = [
  7360. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7361. 'Delete',
  7362. '/a'
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $result = $this->View->fetch('foobar');
  7366. $expected = [
  7367. 'form' => [
  7368. 'method' => 'post', 'action' => '/posts/delete/1',
  7369. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7370. ],
  7371. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7372. '/form'
  7373. ];
  7374. $this->assertHtml($expected, $result);
  7375. }
  7376. /**
  7377. * testSubmitButton method
  7378. *
  7379. * @return void
  7380. */
  7381. public function testSubmitButton()
  7382. {
  7383. $result = $this->Form->submit('');
  7384. $expected = [
  7385. 'div' => ['class' => 'submit'],
  7386. 'input' => ['type' => 'submit', 'value' => ''],
  7387. '/div'
  7388. ];
  7389. $this->assertHtml($expected, $result);
  7390. $result = $this->Form->submit('Test Submit');
  7391. $expected = [
  7392. 'div' => ['class' => 'submit'],
  7393. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7394. '/div'
  7395. ];
  7396. $this->assertHtml($expected, $result);
  7397. $result = $this->Form->submit('Next >');
  7398. $expected = [
  7399. 'div' => ['class' => 'submit'],
  7400. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7401. '/div'
  7402. ];
  7403. $this->assertHtml($expected, $result);
  7404. $result = $this->Form->submit('Next >', ['escape' => false]);
  7405. $expected = [
  7406. 'div' => ['class' => 'submit'],
  7407. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7408. '/div'
  7409. ];
  7410. $this->assertHtml($expected, $result);
  7411. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7412. $expected = [
  7413. 'div' => ['class' => 'submit'],
  7414. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7415. '/div'
  7416. ];
  7417. $this->assertHtml($expected, $result);
  7418. }
  7419. /**
  7420. * testSubmitImage method
  7421. *
  7422. * Test image submit types.
  7423. *
  7424. * @return void
  7425. */
  7426. public function testSubmitImage()
  7427. {
  7428. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7429. $expected = [
  7430. 'div' => ['class' => 'submit'],
  7431. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7432. '/div'
  7433. ];
  7434. $this->assertHtml($expected, $result);
  7435. $result = $this->Form->submit('/relative/cake.power.gif');
  7436. $expected = [
  7437. 'div' => ['class' => 'submit'],
  7438. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7439. '/div'
  7440. ];
  7441. $this->assertHtml($expected, $result);
  7442. $result = $this->Form->submit('cake.power.gif');
  7443. $expected = [
  7444. 'div' => ['class' => 'submit'],
  7445. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7446. '/div'
  7447. ];
  7448. $this->assertHtml($expected, $result);
  7449. $result = $this->Form->submit('Not.an.image');
  7450. $expected = [
  7451. 'div' => ['class' => 'submit'],
  7452. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7453. '/div'
  7454. ];
  7455. $this->assertHtml($expected, $result);
  7456. }
  7457. /**
  7458. * testSubmitUnlockedByDefault method
  7459. *
  7460. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7461. * be submitted at a time.
  7462. *
  7463. * @return void
  7464. */
  7465. public function testSubmitUnlockedByDefault()
  7466. {
  7467. $this->Form->request = $this->Form->request->withParam('_Token', 'secured');
  7468. $this->Form->submit('Go go');
  7469. $this->Form->submit('Save', ['name' => 'save']);
  7470. $result = $this->Form->unlockField();
  7471. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7472. }
  7473. /**
  7474. * testSubmitImageTimestamp method
  7475. *
  7476. * Test submit image with timestamps.
  7477. *
  7478. * @return void
  7479. */
  7480. public function testSubmitImageTimestamp()
  7481. {
  7482. Configure::write('Asset.timestamp', 'force');
  7483. $result = $this->Form->submit('cake.power.gif');
  7484. $expected = [
  7485. 'div' => ['class' => 'submit'],
  7486. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7487. '/div'
  7488. ];
  7489. $this->assertHtml($expected, $result);
  7490. }
  7491. /**
  7492. * testDateTimeWithGetForms method
  7493. *
  7494. * Test that datetime() works with GET style forms.
  7495. *
  7496. * @return void
  7497. */
  7498. public function testDateTimeWithGetForms()
  7499. {
  7500. extract($this->dateRegex);
  7501. $this->Form->create($this->article, ['type' => 'get']);
  7502. $result = $this->Form->datetime('created');
  7503. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7504. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7505. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7506. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7507. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7508. }
  7509. /**
  7510. * testForMagicControlNonExistingNorValidated method
  7511. *
  7512. * @return void
  7513. */
  7514. public function testForMagicControlNonExistingNorValidated()
  7515. {
  7516. $this->Form->create($this->article);
  7517. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7518. $result = $this->Form->control('non_existing_nor_validated');
  7519. $expected = [
  7520. 'label' => ['for' => 'non-existing-nor-validated'],
  7521. 'Non Existing Nor Validated',
  7522. '/label',
  7523. 'input' => [
  7524. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7525. 'id' => 'non-existing-nor-validated'
  7526. ]
  7527. ];
  7528. $this->assertHtml($expected, $result);
  7529. $result = $this->Form->control('non_existing_nor_validated', [
  7530. 'val' => 'my value'
  7531. ]);
  7532. $expected = [
  7533. 'label' => ['for' => 'non-existing-nor-validated'],
  7534. 'Non Existing Nor Validated',
  7535. '/label',
  7536. 'input' => [
  7537. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7538. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7539. ]
  7540. ];
  7541. $this->assertHtml($expected, $result);
  7542. $this->Form->request = $this->Form->request->withData('non_existing_nor_validated', 'CakePHP magic');
  7543. $this->Form->create($this->article);
  7544. $result = $this->Form->control('non_existing_nor_validated');
  7545. $expected = [
  7546. 'label' => ['for' => 'non-existing-nor-validated'],
  7547. 'Non Existing Nor Validated',
  7548. '/label',
  7549. 'input' => [
  7550. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7551. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7552. ]
  7553. ];
  7554. $this->assertHtml($expected, $result);
  7555. }
  7556. /**
  7557. * testFormMagicControlLabel method
  7558. *
  7559. * @return void
  7560. */
  7561. public function testFormMagicControlLabel()
  7562. {
  7563. $this->getTableLocator()->get('Contacts', [
  7564. 'className' => __NAMESPACE__ . '\ContactsTable'
  7565. ]);
  7566. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7567. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7568. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7569. $expected = [
  7570. 'label' => ['for' => 'contacts-name'],
  7571. 'My label',
  7572. '/label',
  7573. 'input' => [
  7574. 'type' => 'text', 'name' => 'Contacts[name]',
  7575. 'id' => 'contacts-name', 'maxlength' => '255'
  7576. ]
  7577. ];
  7578. $this->assertHtml($expected, $result);
  7579. $result = $this->Form->control('name', [
  7580. 'label' => ['class' => 'mandatory']
  7581. ]);
  7582. $expected = [
  7583. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7584. 'Name',
  7585. '/label',
  7586. 'input' => [
  7587. 'type' => 'text', 'name' => 'name',
  7588. 'id' => 'name', 'maxlength' => '255'
  7589. ]
  7590. ];
  7591. $this->assertHtml($expected, $result);
  7592. $result = $this->Form->control('name', [
  7593. 'div' => false,
  7594. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7595. ]);
  7596. $expected = [
  7597. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7598. 'My label',
  7599. '/label',
  7600. 'input' => [
  7601. 'type' => 'text', 'name' => 'name',
  7602. 'id' => 'name', 'maxlength' => '255'
  7603. ]
  7604. ];
  7605. $this->assertHtml($expected, $result);
  7606. $result = $this->Form->control('Contact.name', [
  7607. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7608. ]);
  7609. $expected = [
  7610. 'label' => ['for' => 'my_id'],
  7611. 'Name',
  7612. '/label',
  7613. 'input' => [
  7614. 'type' => 'text', 'name' => 'Contact[name]',
  7615. 'id' => 'my_id', 'maxlength' => '255'
  7616. ]
  7617. ];
  7618. $this->assertHtml($expected, $result);
  7619. $result = $this->Form->control('1.id');
  7620. $expected = ['input' => [
  7621. 'type' => 'hidden', 'name' => '1[id]',
  7622. 'id' => '1-id'
  7623. ]];
  7624. $this->assertHtml($expected, $result);
  7625. $result = $this->Form->control('1.name');
  7626. $expected = [
  7627. 'label' => ['for' => '1-name'],
  7628. 'Name',
  7629. '/label',
  7630. 'input' => [
  7631. 'type' => 'text', 'name' => '1[name]',
  7632. 'id' => '1-name', 'maxlength' => '255'
  7633. ]
  7634. ];
  7635. $this->assertHtml($expected, $result);
  7636. }
  7637. /**
  7638. * testFormEnd method
  7639. *
  7640. * @return void
  7641. */
  7642. public function testFormEnd()
  7643. {
  7644. $this->assertEquals('</form>', $this->Form->end());
  7645. }
  7646. /**
  7647. * testMultiRecordForm method
  7648. *
  7649. * Test the generation of fields for a multi record form.
  7650. *
  7651. * @return void
  7652. */
  7653. public function testMultiRecordForm()
  7654. {
  7655. $this->loadFixtures('Articles', 'Comments');
  7656. $articles = $this->getTableLocator()->get('Articles');
  7657. $articles->hasMany('Comments');
  7658. $comment = new Entity(['comment' => 'Value']);
  7659. $article = new Article(['comments' => [$comment]]);
  7660. $this->Form->create([$article]);
  7661. $result = $this->Form->control('0.comments.1.comment');
  7662. //@codingStandardsIgnoreStart
  7663. $expected = [
  7664. 'div' => ['class' => 'input textarea'],
  7665. 'label' => ['for' => '0-comments-1-comment'],
  7666. 'Comment',
  7667. '/label',
  7668. 'textarea' => [
  7669. 'name',
  7670. 'id' => '0-comments-1-comment',
  7671. 'rows' => 5
  7672. ],
  7673. '/textarea',
  7674. '/div'
  7675. ];
  7676. //@codingStandardsIgnoreEnd
  7677. $this->assertHtml($expected, $result);
  7678. $result = $this->Form->control('0.comments.0.comment');
  7679. //@codingStandardsIgnoreStart
  7680. $expected = [
  7681. 'div' => ['class' => 'input textarea'],
  7682. 'label' => ['for' => '0-comments-0-comment'],
  7683. 'Comment',
  7684. '/label',
  7685. 'textarea' => [
  7686. 'name',
  7687. 'id' => '0-comments-0-comment',
  7688. 'rows' => 5
  7689. ],
  7690. 'Value',
  7691. '/textarea',
  7692. '/div'
  7693. ];
  7694. //@codingStandardsIgnoreEnd
  7695. $this->assertHtml($expected, $result);
  7696. $comment->setError('comment', ['Not valid']);
  7697. $result = $this->Form->control('0.comments.0.comment');
  7698. //@codingStandardsIgnoreStart
  7699. $expected = [
  7700. 'div' => ['class' => 'input textarea error'],
  7701. 'label' => ['for' => '0-comments-0-comment'],
  7702. 'Comment',
  7703. '/label',
  7704. 'textarea' => [
  7705. 'name',
  7706. 'class' => 'form-error',
  7707. 'id' => '0-comments-0-comment',
  7708. 'rows' => 5
  7709. ],
  7710. 'Value',
  7711. '/textarea',
  7712. ['div' => ['class' => 'error-message']],
  7713. 'Not valid',
  7714. '/div',
  7715. '/div'
  7716. ];
  7717. //@codingStandardsIgnoreEnd
  7718. $this->assertHtml($expected, $result);
  7719. $this->getTableLocator()->get('Comments')
  7720. ->getValidator('default')
  7721. ->allowEmpty('comment', false);
  7722. $result = $this->Form->control('0.comments.1.comment');
  7723. //@codingStandardsIgnoreStart
  7724. $expected = [
  7725. 'div' => ['class' => 'input textarea required'],
  7726. 'label' => ['for' => '0-comments-1-comment'],
  7727. 'Comment',
  7728. '/label',
  7729. 'textarea' => [
  7730. 'name',
  7731. 'required' => 'required',
  7732. 'id' => '0-comments-1-comment',
  7733. 'rows' => 5
  7734. ],
  7735. '/textarea',
  7736. '/div'
  7737. ];
  7738. //@codingStandardsIgnoreEnd
  7739. $this->assertHtml($expected, $result);
  7740. }
  7741. /**
  7742. * testHtml5Controls method
  7743. *
  7744. * Test that some html5 inputs + FormHelper::__call() work.
  7745. *
  7746. * @return void
  7747. */
  7748. public function testHtml5Controls()
  7749. {
  7750. $result = $this->Form->email('User.email');
  7751. $expected = [
  7752. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7753. ];
  7754. $this->assertHtml($expected, $result);
  7755. $result = $this->Form->search('User.query');
  7756. $expected = [
  7757. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7758. ];
  7759. $this->assertHtml($expected, $result);
  7760. $result = $this->Form->search('User.query', ['value' => 'test']);
  7761. $expected = [
  7762. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7763. ];
  7764. $this->assertHtml($expected, $result);
  7765. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7766. $expected = [
  7767. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7768. ];
  7769. $this->assertHtml($expected, $result);
  7770. }
  7771. /**
  7772. * testHtml5ControlWithControl method
  7773. *
  7774. * Test accessing html5 inputs through control().
  7775. *
  7776. * @return void
  7777. */
  7778. public function testHtml5ControlWithControl()
  7779. {
  7780. $this->Form->create();
  7781. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7782. $result = $this->Form->control('website', [
  7783. 'type' => 'url',
  7784. 'val' => 'http://domain.tld',
  7785. 'label' => false
  7786. ]);
  7787. $expected = [
  7788. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7789. ];
  7790. $this->assertHtml($expected, $result);
  7791. }
  7792. /**
  7793. * testHtml5ControlException method
  7794. *
  7795. * Test errors when field name is missing.
  7796. *
  7797. * @return void
  7798. */
  7799. public function testHtml5ControlException()
  7800. {
  7801. $this->expectException(\Cake\Core\Exception\Exception::class);
  7802. $this->Form->email();
  7803. }
  7804. /**
  7805. * testRequiredAttribute method
  7806. *
  7807. * Tests that formhelper sets required attributes.
  7808. *
  7809. * @return void
  7810. */
  7811. public function testRequiredAttribute()
  7812. {
  7813. $this->article['required'] = [
  7814. 'title' => true,
  7815. 'body' => false,
  7816. ];
  7817. $this->Form->create($this->article);
  7818. $result = $this->Form->control('title');
  7819. $expected = [
  7820. 'div' => ['class' => 'input text required'],
  7821. 'label' => ['for' => 'title'],
  7822. 'Title',
  7823. '/label',
  7824. 'input' => [
  7825. 'type' => 'text',
  7826. 'name' => 'title',
  7827. 'id' => 'title',
  7828. 'required' => 'required',
  7829. ],
  7830. '/div'
  7831. ];
  7832. $this->assertHtml($expected, $result);
  7833. $result = $this->Form->control('title', ['required' => false]);
  7834. $this->assertNotContains('required', $result);
  7835. $result = $this->Form->control('body');
  7836. $expected = [
  7837. 'div' => ['class' => 'input text'],
  7838. 'label' => ['for' => 'body'],
  7839. 'Body',
  7840. '/label',
  7841. 'input' => [
  7842. 'type' => 'text',
  7843. 'name' => 'body',
  7844. 'id' => 'body',
  7845. ],
  7846. '/div'
  7847. ];
  7848. $this->assertHtml($expected, $result);
  7849. $result = $this->Form->control('body', ['required' => true]);
  7850. $this->assertContains('required', $result);
  7851. }
  7852. /**
  7853. * testControlsNotNested method
  7854. *
  7855. * Tests that it is possible to put inputs outside of the label.
  7856. *
  7857. * @return void
  7858. */
  7859. public function testControlsNotNested()
  7860. {
  7861. $this->Form->setTemplates([
  7862. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7863. 'formGroup' => '{{input}}{{label}}',
  7864. ]);
  7865. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7866. $expected = [
  7867. 'div' => ['class' => 'input checkbox'],
  7868. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7869. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7870. 'label' => ['for' => 'foo'],
  7871. 'Foo',
  7872. '/label',
  7873. '/div'
  7874. ];
  7875. $this->assertHtml($expected, $result);
  7876. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7877. $expected = [
  7878. 'div' => ['class' => 'input checkbox'],
  7879. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7880. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7881. '/div'
  7882. ];
  7883. $this->assertHtml($expected, $result);
  7884. $result = $this->Form->control('confirm', [
  7885. 'type' => 'radio',
  7886. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7887. ]);
  7888. $expected = [
  7889. 'div' => ['class' => 'input radio'],
  7890. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7891. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7892. ['label' => ['for' => 'confirm-y']],
  7893. 'Yes',
  7894. '/label',
  7895. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7896. ['label' => ['for' => 'confirm-n']],
  7897. 'No',
  7898. '/label',
  7899. '<label',
  7900. 'Confirm',
  7901. '/label',
  7902. '/div',
  7903. ];
  7904. $this->assertHtml($expected, $result);
  7905. $result = $this->Form->select('category', ['1', '2'], [
  7906. 'multiple' => 'checkbox',
  7907. 'name' => 'fish',
  7908. ]);
  7909. $expected = [
  7910. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7911. ['div' => ['class' => 'checkbox']],
  7912. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7913. ['label' => ['for' => 'fish-0']],
  7914. '1',
  7915. '/label',
  7916. '/div',
  7917. ['div' => ['class' => 'checkbox']],
  7918. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7919. ['label' => ['for' => 'fish-1']],
  7920. '2',
  7921. '/label',
  7922. '/div'
  7923. ];
  7924. $this->assertHtml($expected, $result);
  7925. }
  7926. /**
  7927. * testControlContainerTemplates method
  7928. *
  7929. * Test that *Container templates are used by input.
  7930. *
  7931. * @return void
  7932. */
  7933. public function testControlContainerTemplates()
  7934. {
  7935. $this->Form->setTemplates([
  7936. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7937. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7938. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7939. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7940. ]);
  7941. $this->article['errors'] = [
  7942. 'Article' => ['published' => 'error message']
  7943. ];
  7944. $this->Form->create($this->article);
  7945. $result = $this->Form->control('accept', [
  7946. 'type' => 'checkbox'
  7947. ]);
  7948. $expected = [
  7949. 'div' => ['class' => 'check'],
  7950. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7951. 'label' => ['for' => 'accept'],
  7952. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7953. 'Accept',
  7954. '/label',
  7955. '/div'
  7956. ];
  7957. $this->assertHtml($expected, $result);
  7958. $result = $this->Form->control('accept', [
  7959. 'type' => 'radio',
  7960. 'options' => ['Y', 'N']
  7961. ]);
  7962. $this->assertContains('<div class="rad">', $result);
  7963. $result = $this->Form->control('Article.published', [
  7964. 'type' => 'radio',
  7965. 'options' => ['Y', 'N']
  7966. ]);
  7967. $this->assertContains('<div class="rad err">', $result);
  7968. $result = $this->Form->control('Article.created', [
  7969. 'type' => 'datetime',
  7970. ]);
  7971. $this->assertContains('<div class="dt">', $result);
  7972. }
  7973. /**
  7974. * testFormGroupTemplates method
  7975. *
  7976. * Test that *Container templates are used by input.
  7977. *
  7978. * @return void
  7979. */
  7980. public function testFormGroupTemplates()
  7981. {
  7982. $this->Form->setTemplates([
  7983. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7984. ]);
  7985. $this->Form->create($this->article);
  7986. $result = $this->Form->control('accept', [
  7987. 'type' => 'radio',
  7988. 'options' => ['Y', 'N']
  7989. ]);
  7990. $this->assertContains('<div class="radio">', $result);
  7991. }
  7992. /**
  7993. * testResetTemplates method
  7994. *
  7995. * Test resetting templates.
  7996. *
  7997. * @return void
  7998. */
  7999. public function testResetTemplates()
  8000. {
  8001. $this->Form->setTemplates(['input' => '<input/>']);
  8002. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8003. $this->Form->resetTemplates();
  8004. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8005. }
  8006. /**
  8007. * testContext method
  8008. *
  8009. * Test the context method.
  8010. *
  8011. * @return void
  8012. */
  8013. public function testContext()
  8014. {
  8015. $result = $this->Form->context();
  8016. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8017. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8018. $this->assertSame($mock, $this->Form->context($mock));
  8019. $this->assertSame($mock, $this->Form->context());
  8020. }
  8021. /**
  8022. * testAutoDomId method
  8023. *
  8024. * @return void
  8025. */
  8026. public function testAutoDomId()
  8027. {
  8028. $result = $this->Form->text('field', ['id' => true]);
  8029. $expected = [
  8030. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8031. ];
  8032. $this->assertHtml($expected, $result);
  8033. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8034. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8035. $expected = [
  8036. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8037. ['label' => ['for' => 'field-0']],
  8038. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8039. 'option A',
  8040. '/label',
  8041. ['label' => ['for' => 'field-1']],
  8042. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8043. 'option B',
  8044. '/label',
  8045. ];
  8046. $this->assertHtml($expected, $result);
  8047. $result = $this->Form->select(
  8048. 'multi_field',
  8049. ['first', 'second'],
  8050. ['multiple' => 'checkbox', 'id' => true]
  8051. );
  8052. $expected = [
  8053. 'input' => [
  8054. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8055. ],
  8056. ['div' => ['class' => 'checkbox']],
  8057. ['label' => ['for' => 'multi-field-0']],
  8058. ['input' => [
  8059. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8060. 'value' => '0', 'id' => 'multi-field-0'
  8061. ]],
  8062. 'first',
  8063. '/label',
  8064. '/div',
  8065. ['div' => ['class' => 'checkbox']],
  8066. ['label' => ['for' => 'multi-field-1']],
  8067. ['input' => [
  8068. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8069. 'value' => '1', 'id' => 'multi-field-1'
  8070. ]],
  8071. 'second',
  8072. '/label',
  8073. '/div',
  8074. ];
  8075. $this->assertHtml($expected, $result);
  8076. }
  8077. /**
  8078. * Test the basic setters and getters for value sources
  8079. *
  8080. * @return void
  8081. */
  8082. public function testFormValueSourcesSettersGetters()
  8083. {
  8084. $this->Form->request = $this->Form->request
  8085. ->withData('id', '1')
  8086. ->withQueryParams(['id' => '2']);
  8087. $expected = ['context'];
  8088. $result = $this->Form->getValueSources();
  8089. $this->assertEquals($expected, $result);
  8090. $expected = ['query', 'data', 'context'];
  8091. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8092. $result = $this->Form->getValueSources();
  8093. $this->assertEquals($expected, $result);
  8094. $this->Form->setValueSources(['context']);
  8095. $expected = '1';
  8096. $result = $this->Form->getSourceValue('id');
  8097. $this->assertEquals($expected, $result);
  8098. $this->Form->setValueSources('query');
  8099. $expected = ['query'];
  8100. $result = $this->Form->getValueSources();
  8101. $this->assertEquals($expected, $result);
  8102. $expected = '2';
  8103. $result = $this->Form->getSourceValue('id');
  8104. $this->assertEquals($expected, $result);
  8105. $this->Form->setValueSources(['data']);
  8106. $expected = '1';
  8107. $result = $this->Form->getSourceValue('id');
  8108. $this->assertEquals($expected, $result);
  8109. $this->Form->setValueSources(['query', 'data']);
  8110. $expected = '2';
  8111. $result = $this->Form->getSourceValue('id');
  8112. $this->assertEquals($expected, $result);
  8113. }
  8114. /**
  8115. * Tests the different input rendering values based on sources values switching
  8116. *
  8117. * @return void
  8118. */
  8119. public function testFormValueSourcesSingleSwitchRendering()
  8120. {
  8121. $this->loadFixtures('Articles');
  8122. $articles = $this->getTableLocator()->get('Articles');
  8123. $article = new Article();
  8124. $articles->patchEntity($article, ['id' => '3']);
  8125. $this->Form->create($article);
  8126. $this->Form->setValueSources(['context']);
  8127. $result = $this->Form->control('id');
  8128. $expected = [
  8129. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8130. ];
  8131. $this->assertHtml($expected, $result);
  8132. $this->Form->request = $this->Form->request->withQueryParams(['id' => 5]);
  8133. $this->Form->setValueSources(['query']);
  8134. $result = $this->Form->control('id');
  8135. $expected = [
  8136. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8137. ];
  8138. $this->assertHtml($expected, $result);
  8139. $this->Form->request = $this->Form->request
  8140. ->withQueryParams(['id' => '5a'])
  8141. ->withData('id', '5b');
  8142. $this->Form->setValueSources(['context']);
  8143. $this->Form->create($article);
  8144. $result = $this->Form->control('id');
  8145. $expected = [
  8146. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8147. ];
  8148. $this->assertHtml($expected, $result);
  8149. $this->Form->setValueSources(['data']);
  8150. $this->Form->create($article);
  8151. $result = $this->Form->control('id');
  8152. $expected = [
  8153. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8154. ];
  8155. $this->assertHtml($expected, $result);
  8156. $this->Form->setValueSources(['query']);
  8157. $result = $this->Form->control('id');
  8158. $expected = [
  8159. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8160. ];
  8161. $this->assertHtml($expected, $result);
  8162. }
  8163. /**
  8164. * Tests the different input rendering values based on sources values switching while supplying
  8165. * an entity (base context) and multiple sources (such as data, query)
  8166. *
  8167. * @return void
  8168. */
  8169. public function testFormValueSourcesListSwitchRendering()
  8170. {
  8171. $this->loadFixtures('Articles');
  8172. $articles = $this->getTableLocator()->get('Articles');
  8173. $article = new Article();
  8174. $articles->patchEntity($article, ['id' => '3']);
  8175. $this->Form->request = $this->Form->request->withQueryParams(['id' => '9']);
  8176. $this->Form->create($article);
  8177. $this->Form->setValueSources(['context', 'query']);
  8178. $result = $this->Form->control('id');
  8179. $expected = [
  8180. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8181. ];
  8182. $this->assertHtml($expected, $result);
  8183. $this->Form->setValueSources(['query', 'context']);
  8184. $result = $this->Form->control('id');
  8185. $expected = [
  8186. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8187. ];
  8188. $this->assertHtml($expected, $result);
  8189. $this->Form->setValueSources(['data', 'query', 'context']);
  8190. $result = $this->Form->control('id');
  8191. $expected = [
  8192. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8193. ];
  8194. $this->assertHtml($expected, $result);
  8195. $this->Form->request = $this->Form->request
  8196. ->withData('id', '8')
  8197. ->withQueryParams(['id' => '9']);
  8198. $this->Form->setValueSources(['data', 'query', 'context']);
  8199. $result = $this->Form->control('id');
  8200. $expected = [
  8201. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8202. ];
  8203. $this->assertHtml($expected, $result);
  8204. }
  8205. /**
  8206. * Test the different form input renderings based on values sources switchings through form options
  8207. *
  8208. * @return void
  8209. */
  8210. public function testFormValueSourcesSwitchViaOptionsRendering()
  8211. {
  8212. $this->loadFixtures('Articles');
  8213. $articles = $this->getTableLocator()->get('Articles');
  8214. $article = new Article();
  8215. $articles->patchEntity($article, ['id' => '3']);
  8216. $this->Form->request = $this->Form->request->withData('id', 4)->withQueryParams(['id' => 5]);
  8217. $this->Form->create($article, ['valueSources' => 'query']);
  8218. $result = $this->Form->control('id');
  8219. $expected = [
  8220. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8221. ];
  8222. $this->assertHtml($expected, $result);
  8223. $result = $this->Form->getSourceValue('id');
  8224. $this->assertEquals('5', $result);
  8225. $this->Form->setValueSources(['context']);
  8226. $this->Form->create($article, ['valueSources' => 'query']);
  8227. $result = $this->Form->control('id');
  8228. $expected = [
  8229. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8230. ];
  8231. $this->assertHtml($expected, $result);
  8232. $result = $this->Form->getSourceValue('id');
  8233. $this->assertEquals('5', $result);
  8234. $this->Form->setValueSources(['query']);
  8235. $this->Form->create($article, ['valueSources' => 'data']);
  8236. $result = $this->Form->control('id');
  8237. $expected = [
  8238. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8239. ];
  8240. $this->assertHtml($expected, $result);
  8241. $result = $this->Form->getSourceValue('id');
  8242. $this->assertEquals('4', $result);
  8243. $this->Form->setValueSources(['query']);
  8244. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8245. $result = $this->Form->control('id');
  8246. $expected = [
  8247. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8248. ];
  8249. $this->assertHtml($expected, $result);
  8250. $result = $this->Form->getSourceValue('id');
  8251. $this->assertEquals('4', $result);
  8252. }
  8253. /**
  8254. * Test the different form input renderings based on values sources switchings through form options
  8255. *
  8256. * @return void
  8257. */
  8258. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8259. {
  8260. $this->loadFixtures('Articles');
  8261. $articles = $this->getTableLocator()->get('Articles');
  8262. $article = new Article();
  8263. $articles->patchEntity($article, ['id' => '3']);
  8264. $this->Form->request = $this->Form->request->withData('id', 10)->withQueryParams(['id' => 11]);
  8265. $this->Form->setValueSources(['context'])
  8266. ->create($article, ['valueSources' => ['query', 'data']]);
  8267. $result = $this->Form->control('id');
  8268. $expected = [
  8269. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8270. ];
  8271. $this->assertHtml($expected, $result);
  8272. $result = $this->Form->getSourceValue('id');
  8273. $this->assertEquals('11', $result);
  8274. $this->Form->request = $this->Form->request->withQueryParams([]);
  8275. $this->Form->setValueSources(['context'])
  8276. ->create($article, ['valueSources' => ['query', 'data']]);
  8277. $result = $this->Form->control('id');
  8278. $expected = [
  8279. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8280. ];
  8281. $this->assertHtml($expected, $result);
  8282. $result = $this->Form->getSourceValue('id');
  8283. $this->assertEquals('10', $result);
  8284. }
  8285. /**
  8286. * Test the different form values sources resetting through From::end();
  8287. *
  8288. * @return void
  8289. */
  8290. public function testFormValueSourcesResetViaEnd()
  8291. {
  8292. $expected = ['context'];
  8293. $result = $this->Form->getValueSources();
  8294. $this->assertEquals($expected, $result);
  8295. $expected = ['query', 'context', 'data'];
  8296. $this->Form->setValueSources(['query', 'context', 'data']);
  8297. $result = $this->Form->getValueSources();
  8298. $this->assertEquals($expected, $result);
  8299. $this->Form->create();
  8300. $result = $this->Form->getValueSources();
  8301. $this->assertEquals($expected, $result);
  8302. $this->Form->end();
  8303. $result = $this->Form->getValueSources();
  8304. $this->assertEquals(['context'], $result);
  8305. }
  8306. /**
  8307. * Test sources values defaults handling
  8308. *
  8309. * @return void
  8310. */
  8311. public function testFormValueSourcesDefaults()
  8312. {
  8313. $this->Form->request = $this->Form->request->withQueryParams(['password' => 'open Sesame']);
  8314. $this->Form->create();
  8315. $result = $this->Form->password('password');
  8316. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8317. $this->assertHtml($expected, $result);
  8318. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8319. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8320. $this->assertHtml($expected, $result);
  8321. $this->Form->setValueSources('query');
  8322. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8323. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8324. $this->assertHtml($expected, $result);
  8325. $this->Form->setValueSources('data');
  8326. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8327. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8328. $this->assertHtml($expected, $result);
  8329. }
  8330. /**
  8331. * Test sources values schema defaults handling
  8332. *
  8333. * @return void
  8334. */
  8335. public function testSourcesValueDoesntExistPassThrough()
  8336. {
  8337. $this->Form->request = $this->Form->request->withQueryParams(['category' => 'sesame-cookies']);
  8338. $articles = $this->getTableLocator()->get('Articles');
  8339. $entity = $articles->newEntity();
  8340. $this->Form->create($entity);
  8341. $this->Form->setValueSources(['query', 'context']);
  8342. $result = $this->Form->getSourceValue('category');
  8343. $this->assertEquals('sesame-cookies', $result);
  8344. $this->Form->setValueSources(['context', 'query']);
  8345. $result = $this->Form->getSourceValue('category');
  8346. $this->assertEquals('sesame-cookies', $result);
  8347. }
  8348. /**
  8349. * testNestedLabelInput method
  8350. *
  8351. * Test the `nestedInput` parameter
  8352. *
  8353. * @group deprecated
  8354. * @return void
  8355. */
  8356. public function testNestedLabelInput()
  8357. {
  8358. $this->deprecated(function () {
  8359. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8360. $expected = [
  8361. 'div' => ['class' => 'input text'],
  8362. 'label' => ['for' => 'foo'],
  8363. ['input' => [
  8364. 'type' => 'text',
  8365. 'name' => 'foo',
  8366. 'id' => 'foo'
  8367. ]],
  8368. 'Foo',
  8369. '/label',
  8370. '/div'
  8371. ];
  8372. $this->assertHtml($expected, $result);
  8373. });
  8374. }
  8375. /**
  8376. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8377. *
  8378. * This test makes sure `false` excludes the label from the render
  8379. *
  8380. * @return void
  8381. */
  8382. public function testControlLabelManipulationDisableLabels()
  8383. {
  8384. $result = $this->Form->control('test', [
  8385. 'type' => 'radio',
  8386. 'options' => ['A', 'B'],
  8387. 'labelOptions' => false
  8388. ]);
  8389. $expected = [
  8390. ['div' => ['class' => 'input radio']],
  8391. '<label',
  8392. 'Test',
  8393. '/label',
  8394. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8395. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8396. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8397. '/div',
  8398. ];
  8399. $this->assertHtml($expected, $result);
  8400. $result = $this->Form->control('checkbox1', [
  8401. 'label' => 'My checkboxes',
  8402. 'multiple' => 'checkbox',
  8403. 'type' => 'select',
  8404. 'options' => [
  8405. ['text' => 'First Checkbox', 'value' => 1],
  8406. ['text' => 'Second Checkbox', 'value' => 2]
  8407. ],
  8408. 'labelOptions' => false
  8409. ]);
  8410. $expected = [
  8411. ['div' => ['class' => 'input select']],
  8412. ['label' => ['for' => 'checkbox1']],
  8413. 'My checkboxes',
  8414. '/label',
  8415. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8416. ['div' => ['class' => 'checkbox']],
  8417. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8418. '/div',
  8419. ['div' => ['class' => 'checkbox']],
  8420. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8421. '/div',
  8422. '/div'
  8423. ];
  8424. $this->assertHtml($expected, $result);
  8425. }
  8426. /**
  8427. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8428. *
  8429. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8430. * added to the class if checked.
  8431. *
  8432. * Also checks to make sure any custom attributes are rendered correctly
  8433. *
  8434. * @return void
  8435. */
  8436. public function testControlLabelManipulationRadios()
  8437. {
  8438. $result = $this->Form->control('test', [
  8439. 'type' => 'radio',
  8440. 'options' => ['A', 'B'],
  8441. 'labelOptions' => ['class' => 'custom-class']
  8442. ]);
  8443. $expected = [
  8444. ['div' => ['class' => 'input radio']],
  8445. '<label',
  8446. 'Test',
  8447. '/label',
  8448. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8449. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8450. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8451. 'A',
  8452. '/label',
  8453. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8454. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8455. 'B',
  8456. '/label',
  8457. '/div',
  8458. ];
  8459. $this->assertHtml($expected, $result);
  8460. $result = $this->Form->control('test', [
  8461. 'type' => 'radio',
  8462. 'options' => ['A', 'B'],
  8463. 'value' => 1,
  8464. 'labelOptions' => ['class' => 'custom-class']
  8465. ]);
  8466. $expected = [
  8467. ['div' => ['class' => 'input radio']],
  8468. '<label',
  8469. 'Test',
  8470. '/label',
  8471. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8472. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8473. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8474. 'A',
  8475. '/label',
  8476. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8477. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8478. 'B',
  8479. '/label',
  8480. '/div',
  8481. ];
  8482. $this->assertHtml($expected, $result);
  8483. $result = $this->Form->control('test', [
  8484. 'type' => 'radio',
  8485. 'options' => ['A', 'B'],
  8486. 'value' => 1,
  8487. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8488. ]);
  8489. $expected = [
  8490. ['div' => ['class' => 'input radio']],
  8491. '<label',
  8492. 'Test',
  8493. '/label',
  8494. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8495. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8496. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8497. 'A',
  8498. '/label',
  8499. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8500. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8501. 'B',
  8502. '/label',
  8503. '/div',
  8504. ];
  8505. $this->assertHtml($expected, $result);
  8506. $result = $this->Form->radio('test', ['A', 'B'], [
  8507. 'label' => [
  8508. 'class' => ['custom-class', 'another-class'],
  8509. 'data-name' => 'bob'
  8510. ],
  8511. 'value' => 1
  8512. ]);
  8513. $expected = [
  8514. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8515. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8516. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8517. 'A',
  8518. '/label',
  8519. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8520. ['input' => [
  8521. 'type' => 'radio',
  8522. 'name' => 'test',
  8523. 'value' => '1',
  8524. 'id' => 'test-1',
  8525. 'checked' => 'checked'
  8526. ]],
  8527. 'B',
  8528. '/label',
  8529. ];
  8530. $this->assertHtml($expected, $result);
  8531. }
  8532. /**
  8533. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8534. *
  8535. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8536. * added to the class if checked.
  8537. *
  8538. * Also checks to make sure any custom attributes are rendered correctly
  8539. *
  8540. * @return void
  8541. */
  8542. public function testControlLabelManipulationCheckboxes()
  8543. {
  8544. $result = $this->Form->control('checkbox1', [
  8545. 'label' => 'My checkboxes',
  8546. 'multiple' => 'checkbox',
  8547. 'type' => 'select',
  8548. 'options' => [
  8549. ['text' => 'First Checkbox', 'value' => 1],
  8550. ['text' => 'Second Checkbox', 'value' => 2]
  8551. ],
  8552. 'labelOptions' => ['class' => 'custom-class'],
  8553. 'value' => ['1']
  8554. ]);
  8555. $expected = [
  8556. ['div' => ['class' => 'input select']],
  8557. ['label' => ['for' => 'checkbox1']],
  8558. 'My checkboxes',
  8559. '/label',
  8560. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8561. ['div' => ['class' => 'checkbox']],
  8562. ['label' => [
  8563. 'class' => 'custom-class selected',
  8564. 'for' => 'checkbox1-1'
  8565. ]],
  8566. ['input' => [
  8567. 'type' => 'checkbox',
  8568. 'name' => 'checkbox1[]',
  8569. 'value' => '1',
  8570. 'id' => 'checkbox1-1',
  8571. 'checked' => 'checked'
  8572. ]],
  8573. 'First Checkbox',
  8574. '/label',
  8575. '/div',
  8576. ['div' => ['class' => 'checkbox']],
  8577. ['label' => [
  8578. 'class' => 'custom-class',
  8579. 'for' => 'checkbox1-2'
  8580. ]],
  8581. ['input' => [
  8582. 'type' => 'checkbox',
  8583. 'name' => 'checkbox1[]',
  8584. 'value' => '2',
  8585. 'id' => 'checkbox1-2'
  8586. ]],
  8587. 'Second Checkbox',
  8588. '/label',
  8589. '/div',
  8590. '/div'
  8591. ];
  8592. $this->assertHtml($expected, $result);
  8593. $result = $this->Form->control('checkbox1', [
  8594. 'label' => 'My checkboxes',
  8595. 'multiple' => 'checkbox',
  8596. 'type' => 'select',
  8597. 'options' => [
  8598. ['text' => 'First Checkbox', 'value' => 1],
  8599. ['text' => 'Second Checkbox', 'value' => 2]
  8600. ],
  8601. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8602. 'value' => ['1']
  8603. ]);
  8604. $expected = [
  8605. ['div' => ['class' => 'input select']],
  8606. ['label' => ['for' => 'checkbox1']],
  8607. 'My checkboxes',
  8608. '/label',
  8609. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8610. ['div' => ['class' => 'checkbox']],
  8611. ['label' => [
  8612. 'class' => 'custom-class another-class selected',
  8613. 'data-name' => 'bob',
  8614. 'for' => 'checkbox1-1'
  8615. ]],
  8616. ['input' => [
  8617. 'type' => 'checkbox',
  8618. 'name' => 'checkbox1[]',
  8619. 'value' => '1',
  8620. 'id' => 'checkbox1-1',
  8621. 'checked' => 'checked'
  8622. ]],
  8623. 'First Checkbox',
  8624. '/label',
  8625. '/div',
  8626. ['div' => ['class' => 'checkbox']],
  8627. ['label' => [
  8628. 'class' => 'custom-class another-class',
  8629. 'data-name' => 'bob',
  8630. 'for' => 'checkbox1-2'
  8631. ]],
  8632. ['input' => [
  8633. 'type' => 'checkbox',
  8634. 'name' => 'checkbox1[]',
  8635. 'value' => '2',
  8636. 'id' => 'checkbox1-2'
  8637. ]],
  8638. 'Second Checkbox',
  8639. '/label',
  8640. '/div',
  8641. '/div'
  8642. ];
  8643. $this->assertHtml($expected, $result);
  8644. }
  8645. }