FormHelperTest.php 270 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.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\Network\Request;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($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->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property FormHelper $Form
  103. */
  104. class FormHelperTest extends TestCase
  105. {
  106. /**
  107. * Fixtures to be used
  108. *
  109. * @var array
  110. */
  111. public $fixtures = ['core.articles', 'core.comments'];
  112. /**
  113. * Do not load the fixtures by default
  114. *
  115. * @var bool
  116. */
  117. public $autoFixtures = false;
  118. /**
  119. * setUp method
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. Configure::write('Config.language', 'eng');
  127. Configure::write('App.base', '');
  128. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  129. Configure::delete('Asset');
  130. $this->View = new View();
  131. $this->Form = new FormHelper($this->View);
  132. $request = new Request('articles/add');
  133. $request->here = '/articles/add';
  134. $request['controller'] = 'articles';
  135. $request['action'] = 'add';
  136. $request->webroot = '';
  137. $request->base = '';
  138. $this->Form->Url->request = $this->Form->request = $request;
  139. $this->dateRegex = [
  140. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  142. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  144. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  145. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  146. ];
  147. $this->article = [
  148. 'schema' => [
  149. 'id' => ['type' => 'integer'],
  150. 'author_id' => ['type' => 'integer', 'null' => true],
  151. 'title' => ['type' => 'string', 'null' => true],
  152. 'body' => 'text',
  153. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  154. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  155. ],
  156. 'required' => [
  157. 'author_id' => true,
  158. 'title' => true,
  159. ]
  160. ];
  161. Security::salt('foo!');
  162. Router::connect('/:controller', ['action' => 'index']);
  163. Router::connect('/:controller/:action/*');
  164. }
  165. /**
  166. * tearDown method
  167. *
  168. * @return void
  169. */
  170. public function tearDown()
  171. {
  172. parent::tearDown();
  173. unset($this->Form, $this->Controller, $this->View);
  174. TableRegistry::clear();
  175. }
  176. /**
  177. * Test construct() with the templates option.
  178. *
  179. * @return void
  180. */
  181. public function testConstructTemplatesFile()
  182. {
  183. $helper = new FormHelper($this->View, [
  184. 'templates' => 'htmlhelper_tags'
  185. ]);
  186. $result = $helper->input('name');
  187. $this->assertContains('<input', $result);
  188. }
  189. /**
  190. * Test that when specifying custom widgets the config array for that widget
  191. * is overwritten instead of merged.
  192. *
  193. * @return void
  194. */
  195. public function testConstructWithWidgets()
  196. {
  197. $config = [
  198. 'widgets' => [
  199. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  200. ]
  201. ];
  202. $helper = new FormHelper($this->View, $config);
  203. $registry = $helper->widgetRegistry();
  204. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  205. }
  206. /**
  207. * Test that when specifying custom widgets config file and it should be
  208. * added to widgets array. WidgetRegistry will load widgets in constructor.
  209. *
  210. * @return void
  211. */
  212. public function testConstructWithWidgetsConfig()
  213. {
  214. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  215. $registry = $helper->widgetRegistry();
  216. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  217. }
  218. /**
  219. * Test registering a new widget class and rendering it.
  220. *
  221. * @return void
  222. */
  223. public function testAddWidgetAndRenderWidget()
  224. {
  225. $data = [
  226. 'val' => 1
  227. ];
  228. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  229. $this->assertNull($this->Form->addWidget('test', $mock));
  230. $mock->expects($this->once())
  231. ->method('render')
  232. ->with($data)
  233. ->will($this->returnValue('HTML'));
  234. $result = $this->Form->widget('test', $data);
  235. $this->assertEquals('HTML', $result);
  236. }
  237. /**
  238. * Test that secureFields() of widget is called after calling render(),
  239. * not before.
  240. *
  241. * @return void
  242. */
  243. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  244. {
  245. $data = [
  246. 'val' => 1,
  247. 'name' => 'test'
  248. ];
  249. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  250. $this->assertNull($this->Form->addWidget('test', $mock));
  251. $mock->expects($this->at(0))
  252. ->method('render')
  253. ->with($data)
  254. ->will($this->returnValue('HTML'));
  255. $mock->expects($this->at(1))
  256. ->method('secureFields')
  257. ->with($data)
  258. ->will($this->returnValue(['test']));
  259. $result = $this->Form->widget('test', $data + ['secure' => true]);
  260. $this->assertEquals('HTML', $result);
  261. }
  262. /**
  263. * Test that empty string is not added to secure fields list when
  264. * rendering input widget without name.
  265. *
  266. * @return void
  267. */
  268. public function testRenderingWidgetWithEmptyName()
  269. {
  270. $this->assertEquals([], $this->Form->fields);
  271. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  272. $this->assertEquals('<select name=""></select>', $result);
  273. $this->assertEquals([], $this->Form->fields);
  274. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  275. $this->assertEquals('<select name="0"></select>', $result);
  276. $this->assertEquals(['0'], $this->Form->fields);
  277. }
  278. /**
  279. * Test registering an invalid widget class.
  280. *
  281. * @expectedException \RuntimeException
  282. * @return void
  283. */
  284. public function testAddWidgetInvalid()
  285. {
  286. $mock = new \StdClass();
  287. $this->Form->addWidget('test', $mock);
  288. $this->Form->widget('test');
  289. }
  290. /**
  291. * Test adding a new context class.
  292. *
  293. * @return void
  294. */
  295. public function testAddContextProvider()
  296. {
  297. $context = 'My data';
  298. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  299. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  300. $this->assertInstanceOf('Cake\Network\Request', $request);
  301. $this->assertEquals($context, $data['entity']);
  302. return $stub;
  303. });
  304. $this->Form->create($context);
  305. $result = $this->Form->context();
  306. $this->assertSame($stub, $result);
  307. }
  308. /**
  309. * Test replacing a context class.
  310. *
  311. * @return void
  312. */
  313. public function testAddContextProviderReplace()
  314. {
  315. $entity = new Article();
  316. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  317. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  318. return $stub;
  319. });
  320. $this->Form->create($entity);
  321. $result = $this->Form->context();
  322. $this->assertSame($stub, $result);
  323. }
  324. /**
  325. * Test overriding a context class.
  326. *
  327. * @return void
  328. */
  329. public function testAddContextProviderAdd()
  330. {
  331. $entity = new Article();
  332. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  333. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  334. if ($data['entity'] instanceof Entity) {
  335. return $stub;
  336. }
  337. });
  338. $this->Form->create($entity);
  339. $result = $this->Form->context();
  340. $this->assertSame($stub, $result);
  341. }
  342. /**
  343. * Test adding an invalid context class.
  344. *
  345. * @expectedException \RuntimeException
  346. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  347. * @return void
  348. */
  349. public function testAddContextProviderInvalid()
  350. {
  351. $context = 'My data';
  352. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  353. return new \StdClass();
  354. });
  355. $this->Form->create($context);
  356. }
  357. /**
  358. * Provides context options for create().
  359. *
  360. * @return array
  361. */
  362. public function contextSelectionProvider()
  363. {
  364. $entity = new Article();
  365. $collection = $this->getMockBuilder('Cake\Collection\Collection')
  366. ->setMethods(['extract'])
  367. ->setConstructorArgs([[$entity]])
  368. ->getMock();
  369. $emptyCollection = new Collection([]);
  370. $emptyArray = [];
  371. $arrayObject = new \ArrayObject([]);
  372. $data = [
  373. 'schema' => [
  374. 'title' => ['type' => 'string']
  375. ]
  376. ];
  377. $form = new Form();
  378. return [
  379. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  380. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  381. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  382. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  383. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  384. 'form' => [$form, 'Cake\View\Form\FormContext'],
  385. 'none' => [null, 'Cake\View\Form\NullContext'],
  386. 'false' => [false, 'Cake\View\Form\NullContext'],
  387. ];
  388. }
  389. /**
  390. * Test default context selection in create()
  391. *
  392. * @dataProvider contextSelectionProvider
  393. * @return void
  394. */
  395. public function testCreateContextSelectionBuiltIn($data, $class)
  396. {
  397. $this->loadFixtures('Articles');
  398. $this->Form->create($data);
  399. $this->assertInstanceOf($class, $this->Form->context());
  400. }
  401. /**
  402. * Data provider for type option.
  403. *
  404. * @return array
  405. */
  406. public static function requestTypeProvider()
  407. {
  408. return [
  409. // type, method, override
  410. ['post', 'post', 'POST'],
  411. ['put', 'post', 'PUT'],
  412. ['patch', 'post', 'PATCH'],
  413. ['delete', 'post', 'DELETE'],
  414. ];
  415. }
  416. /**
  417. * Test creating file forms.
  418. *
  419. * @return void
  420. */
  421. public function testCreateFile()
  422. {
  423. $encoding = strtolower(Configure::read('App.encoding'));
  424. $result = $this->Form->create(false, ['type' => 'file']);
  425. $expected = [
  426. 'form' => [
  427. 'method' => 'post', 'action' => '/articles/add',
  428. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  429. ],
  430. 'div' => ['style' => 'display:none;'],
  431. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  432. '/div'
  433. ];
  434. $this->assertHtml($expected, $result);
  435. }
  436. /**
  437. * Test creating GET forms.
  438. *
  439. * @return void
  440. */
  441. public function testCreateGet()
  442. {
  443. $encoding = strtolower(Configure::read('App.encoding'));
  444. $result = $this->Form->create(false, ['type' => 'get']);
  445. $expected = ['form' => [
  446. 'method' => 'get', 'action' => '/articles/add',
  447. 'accept-charset' => $encoding
  448. ]];
  449. $this->assertHtml($expected, $result);
  450. }
  451. /**
  452. * Test explicit method/enctype options.
  453. *
  454. * Explicit method overwrites inferred method from 'type'
  455. *
  456. * @return void
  457. */
  458. public function testCreateExplictMethodEnctype()
  459. {
  460. $encoding = strtolower(Configure::read('App.encoding'));
  461. $result = $this->Form->create(false, [
  462. 'type' => 'get',
  463. 'method' => 'put',
  464. 'enctype' => 'multipart/form-data'
  465. ]);
  466. $expected = ['form' => [
  467. 'method' => 'put',
  468. 'action' => '/articles/add',
  469. 'enctype' => 'multipart/form-data',
  470. 'accept-charset' => $encoding
  471. ]];
  472. $this->assertHtml($expected, $result);
  473. }
  474. /**
  475. * Test create() with the templates option.
  476. *
  477. * @return void
  478. */
  479. public function testCreateTemplatesArray()
  480. {
  481. $result = $this->Form->create($this->article, [
  482. 'templates' => [
  483. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  484. ]
  485. ]);
  486. $expected = [
  487. 'form' => [
  488. 'class' => 'form-horizontal',
  489. 'method' => 'post',
  490. 'action' => '/articles/add',
  491. 'accept-charset' => 'utf-8'
  492. ]
  493. ];
  494. $this->assertHtml($expected, $result);
  495. }
  496. /**
  497. * Test create() with the templates option.
  498. *
  499. * @return void
  500. */
  501. public function testCreateTemplatesFile()
  502. {
  503. $result = $this->Form->create($this->article, [
  504. 'templates' => 'htmlhelper_tags',
  505. ]);
  506. $expected = [
  507. 'start form',
  508. 'div' => ['class' => 'hidden'],
  509. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  510. '/div'
  511. ];
  512. $this->assertHtml($expected, $result);
  513. }
  514. /**
  515. * Test that create() and end() restore templates.
  516. *
  517. * @return void
  518. */
  519. public function testCreateEndRestoreTemplates()
  520. {
  521. $this->Form->create($this->article, [
  522. 'templates' => ['input' => 'custom input element']
  523. ]);
  524. $this->Form->end();
  525. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  526. }
  527. /**
  528. * Test using template vars in various templates used by input() method.
  529. *
  530. * @return void
  531. */
  532. public function testInputTemplateVars()
  533. {
  534. $result = $this->Form->input('text', [
  535. 'templates' => [
  536. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  537. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  538. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  539. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  540. ],
  541. 'templateVars' => [
  542. 'forinput' => 'in-input',
  543. 'forlabel' => 'in-label',
  544. 'forgroup' => 'in-group',
  545. 'forcontainer' => 'in-container'
  546. ]
  547. ]);
  548. $expected = [
  549. 'div' => ['class'],
  550. 'label' => ['for'],
  551. 'Text in-label',
  552. '/label',
  553. 'in-group',
  554. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  555. 'in-container',
  556. '/div',
  557. ];
  558. $this->assertHtml($expected, $result);
  559. }
  560. /**
  561. * Test ensuring template variables work in template files loaded
  562. * during input().
  563. *
  564. * @return void
  565. */
  566. public function testInputTemplatesFromFile()
  567. {
  568. $result = $this->Form->input('title', [
  569. 'templates' => 'test_templates',
  570. 'templateVars' => [
  571. 'forcontainer' => 'container-data'
  572. ]
  573. ]);
  574. $expected = [
  575. 'div' => ['class'],
  576. 'label' => ['for'],
  577. 'Title',
  578. '/label',
  579. 'input' => ['name', 'type' => 'text', 'id'],
  580. 'container-data',
  581. '/div',
  582. ];
  583. $this->assertHtml($expected, $result);
  584. }
  585. /**
  586. * Test using template vars in inputSubmit and submitContainer template.
  587. *
  588. * @return void
  589. */
  590. public function testSubmitTemplateVars()
  591. {
  592. $this->Form->templates([
  593. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  594. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  595. ]);
  596. $result = $this->Form->submit('Submit', [
  597. 'templateVars' => [
  598. 'forinput' => 'in-input',
  599. 'forcontainer' => 'in-container'
  600. ]
  601. ]);
  602. $expected = [
  603. 'div' => ['class'],
  604. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  605. 'in-container',
  606. '/div',
  607. ];
  608. $this->assertHtml($expected, $result);
  609. }
  610. /**
  611. * test the create() method
  612. *
  613. * @dataProvider requestTypeProvider
  614. * @return void
  615. */
  616. public function testCreateTypeOptions($type, $method, $override)
  617. {
  618. $encoding = strtolower(Configure::read('App.encoding'));
  619. $result = $this->Form->create(false, ['type' => $type]);
  620. $expected = [
  621. 'form' => [
  622. 'method' => $method, 'action' => '/articles/add',
  623. 'accept-charset' => $encoding
  624. ],
  625. 'div' => ['style' => 'display:none;'],
  626. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  627. '/div'
  628. ];
  629. $this->assertHtml($expected, $result);
  630. }
  631. /**
  632. * Test using template vars in Create (formStart template)
  633. *
  634. * @return void
  635. */
  636. public function testCreateTemplateVars()
  637. {
  638. $result = $this->Form->create($this->article, [
  639. 'templates' => [
  640. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  641. ],
  642. 'templateVars' => ['header' => 'headertext']
  643. ]);
  644. $expected = [
  645. 'h4' => ['class'],
  646. 'headertext',
  647. '/h4',
  648. 'form' => [
  649. 'method' => 'post',
  650. 'action' => '/articles/add',
  651. 'accept-charset' => 'utf-8'
  652. ]
  653. ];
  654. $this->assertHtml($expected, $result);
  655. }
  656. /**
  657. * Test opening a form for an update operation.
  658. *
  659. * @return void
  660. */
  661. public function testCreateUpdateForm()
  662. {
  663. $encoding = strtolower(Configure::read('App.encoding'));
  664. $this->Form->request->here = '/articles/edit/1';
  665. $this->Form->request['action'] = 'edit';
  666. $this->article['defaults']['id'] = 1;
  667. $result = $this->Form->create($this->article);
  668. $expected = [
  669. 'form' => [
  670. 'method' => 'post', 'action' => '/articles/edit/1',
  671. 'accept-charset' => $encoding
  672. ],
  673. 'div' => ['style' => 'display:none;'],
  674. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  675. '/div'
  676. ];
  677. $this->assertHtml($expected, $result);
  678. }
  679. /**
  680. * test create() with automatic url generation
  681. *
  682. * @return void
  683. */
  684. public function testCreateAutoUrl()
  685. {
  686. $encoding = strtolower(Configure::read('App.encoding'));
  687. $this->Form->request['action'] = 'delete';
  688. $this->Form->request->here = '/articles/delete/10';
  689. $this->Form->request->base = '';
  690. $result = $this->Form->create($this->article);
  691. $expected = [
  692. 'form' => [
  693. 'method' => 'post', 'action' => '/articles/delete/10',
  694. 'accept-charset' => $encoding
  695. ],
  696. 'div' => ['style' => 'display:none;'],
  697. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  698. '/div'
  699. ];
  700. $this->assertHtml($expected, $result);
  701. $this->article['defaults'] = ['id' => 1];
  702. $this->Form->request->here = '/articles/edit/1';
  703. $this->Form->request['action'] = 'delete';
  704. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  705. $expected = [
  706. 'form' => [
  707. 'method' => 'post',
  708. 'action' => '/articles/edit/1',
  709. 'accept-charset' => $encoding
  710. ],
  711. 'div' => ['style' => 'display:none;'],
  712. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  713. '/div'
  714. ];
  715. $this->assertHtml($expected, $result);
  716. $this->Form->request['action'] = 'add';
  717. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  718. $expected = [
  719. 'form' => [
  720. 'method' => 'post',
  721. 'action' => '/articles/publish/1',
  722. 'accept-charset' => $encoding
  723. ],
  724. 'div' => ['style' => 'display:none;'],
  725. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  726. '/div'
  727. ];
  728. $this->assertHtml($expected, $result);
  729. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  730. $expected = [
  731. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  732. 'div' => ['style' => 'display:none;'],
  733. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  734. '/div'
  735. ];
  736. $this->assertHtml($expected, $result);
  737. $this->Form->request['controller'] = 'Pages';
  738. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  739. $expected = [
  740. 'form' => [
  741. 'method' => 'post', 'action' => '/Pages/signup/1',
  742. 'accept-charset' => $encoding
  743. ],
  744. 'div' => ['style' => 'display:none;'],
  745. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  746. '/div'
  747. ];
  748. $this->assertHtml($expected, $result);
  749. }
  750. /**
  751. * Test create() with no URL (no "action" attribute for <form> tag)
  752. *
  753. * @return void
  754. */
  755. public function testCreateNoUrl()
  756. {
  757. $result = $this->Form->create(false, ['url' => false]);
  758. $expected = [
  759. 'form' => [
  760. 'method' => 'post',
  761. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  762. ],
  763. 'div' => ['style' => 'display:none;'],
  764. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  765. '/div'
  766. ];
  767. $this->assertHtml($expected, $result);
  768. }
  769. /**
  770. * test create() with a custom route
  771. *
  772. * @return void
  773. */
  774. public function testCreateCustomRoute()
  775. {
  776. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  777. $encoding = strtolower(Configure::read('App.encoding'));
  778. $this->Form->request['controller'] = 'users';
  779. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  780. $expected = [
  781. 'form' => [
  782. 'method' => 'post', 'action' => '/login',
  783. 'accept-charset' => $encoding
  784. ],
  785. 'div' => ['style' => 'display:none;'],
  786. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  787. '/div'
  788. ];
  789. $this->assertHtml($expected, $result);
  790. Router::connect(
  791. '/new-article',
  792. ['controller' => 'articles', 'action' => 'myaction'],
  793. ['_name' => 'my-route']
  794. );
  795. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  796. $expected = [
  797. 'form' => [
  798. 'method' => 'post', 'action' => '/new-article',
  799. 'accept-charset' => $encoding,
  800. ],
  801. 'div' => ['style' => 'display:none;'],
  802. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  803. '/div'
  804. ];
  805. $this->assertHtml($expected, $result);
  806. }
  807. /**
  808. * test automatic accept-charset overriding
  809. *
  810. * @return void
  811. */
  812. public function testCreateWithAcceptCharset()
  813. {
  814. $result = $this->Form->create(
  815. $this->article,
  816. [
  817. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  818. ]
  819. );
  820. $expected = [
  821. 'form' => [
  822. 'method' => 'post', 'action' => '/articles',
  823. 'accept-charset' => 'iso-8859-1'
  824. ],
  825. 'div' => ['style' => 'display:none;'],
  826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  827. '/div'
  828. ];
  829. $this->assertHtml($expected, $result);
  830. }
  831. /**
  832. * Test base form URL when url param is passed with multiple parameters (&)
  833. *
  834. */
  835. public function testCreateQuerystringrequest()
  836. {
  837. $encoding = strtolower(Configure::read('App.encoding'));
  838. $result = $this->Form->create($this->article, [
  839. 'type' => 'post',
  840. 'escape' => false,
  841. 'url' => [
  842. 'controller' => 'controller',
  843. 'action' => 'action',
  844. '?' => ['param1' => 'value1', 'param2' => 'value2']
  845. ]
  846. ]);
  847. $expected = [
  848. 'form' => [
  849. 'method' => 'post',
  850. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  851. 'accept-charset' => $encoding
  852. ],
  853. 'div' => ['style' => 'display:none;'],
  854. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  855. '/div'
  856. ];
  857. $this->assertHtml($expected, $result);
  858. $result = $this->Form->create($this->article, [
  859. 'type' => 'post',
  860. 'url' => [
  861. 'controller' => 'controller',
  862. 'action' => 'action',
  863. '?' => ['param1' => 'value1', 'param2' => 'value2']
  864. ]
  865. ]);
  866. $this->assertHtml($expected, $result);
  867. }
  868. /**
  869. * test that create() doesn't cause errors by multiple id's being in the primary key
  870. * as could happen with multiple select or checkboxes.
  871. *
  872. * @return void
  873. */
  874. public function testCreateWithMultipleIdInData()
  875. {
  876. $encoding = strtolower(Configure::read('App.encoding'));
  877. $this->Form->request->data['Article']['id'] = [1, 2];
  878. $result = $this->Form->create($this->article);
  879. $expected = [
  880. 'form' => [
  881. 'method' => 'post',
  882. 'action' => '/articles/add',
  883. 'accept-charset' => $encoding
  884. ],
  885. 'div' => ['style' => 'display:none;'],
  886. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  887. '/div'
  888. ];
  889. $this->assertHtml($expected, $result);
  890. }
  891. /**
  892. * test that create() doesn't add in extra passed params.
  893. *
  894. * @return void
  895. */
  896. public function testCreatePassedArgs()
  897. {
  898. $encoding = strtolower(Configure::read('App.encoding'));
  899. $this->Form->request->data['Article']['id'] = 1;
  900. $result = $this->Form->create($this->article, [
  901. 'type' => 'post',
  902. 'escape' => false,
  903. 'url' => [
  904. 'action' => 'edit',
  905. 'myparam'
  906. ]
  907. ]);
  908. $expected = [
  909. 'form' => [
  910. 'method' => 'post',
  911. 'action' => '/articles/edit/myparam',
  912. 'accept-charset' => $encoding
  913. ],
  914. 'div' => ['style' => 'display:none;'],
  915. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  916. '/div'
  917. ];
  918. $this->assertHtml($expected, $result);
  919. }
  920. /**
  921. * test creating a get form, and get form inputs.
  922. *
  923. * @return void
  924. */
  925. public function testGetFormCreate()
  926. {
  927. $encoding = strtolower(Configure::read('App.encoding'));
  928. $result = $this->Form->create($this->article, ['type' => 'get']);
  929. $expected = ['form' => [
  930. 'method' => 'get', 'action' => '/articles/add',
  931. 'accept-charset' => $encoding
  932. ]];
  933. $this->assertHtml($expected, $result);
  934. $result = $this->Form->text('title');
  935. $expected = ['input' => [
  936. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  937. ]];
  938. $this->assertHtml($expected, $result);
  939. $result = $this->Form->password('password');
  940. $expected = ['input' => [
  941. 'name' => 'password', 'type' => 'password'
  942. ]];
  943. $this->assertHtml($expected, $result);
  944. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  945. $result = $this->Form->text('user_form');
  946. $expected = ['input' => [
  947. 'name' => 'user_form', 'type' => 'text'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. }
  951. /**
  952. * test get form, and inputs when the model param is false
  953. *
  954. * @return void
  955. */
  956. public function testGetFormWithFalseModel()
  957. {
  958. $encoding = strtolower(Configure::read('App.encoding'));
  959. $this->Form->request['controller'] = 'contact_test';
  960. $result = $this->Form->create(false, [
  961. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  962. ]);
  963. $expected = ['form' => [
  964. 'method' => 'get', 'action' => '/contact_test/add',
  965. 'accept-charset' => $encoding
  966. ]];
  967. $this->assertHtml($expected, $result);
  968. $result = $this->Form->text('reason');
  969. $expected = [
  970. 'input' => ['type' => 'text', 'name' => 'reason']
  971. ];
  972. $this->assertHtml($expected, $result);
  973. }
  974. /**
  975. * testFormCreateWithSecurity method
  976. *
  977. * Test form->create() with security key.
  978. *
  979. * @return void
  980. */
  981. public function testCreateWithSecurity()
  982. {
  983. $this->Form->request->params['_csrfToken'] = 'testKey';
  984. $encoding = strtolower(Configure::read('App.encoding'));
  985. $result = $this->Form->create($this->article, [
  986. 'url' => '/articles/publish',
  987. ]);
  988. $expected = [
  989. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  990. 'div' => ['style' => 'display:none;'],
  991. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  992. ['input' => [
  993. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  994. ]],
  995. '/div'
  996. ];
  997. $this->assertHtml($expected, $result);
  998. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  999. $expected['form']['id'] = 'MyForm';
  1000. $this->assertHtml($expected, $result);
  1001. }
  1002. /**
  1003. * testFormCreateGetNoSecurity method
  1004. *
  1005. * Test form->create() with no security key as its a get form
  1006. *
  1007. * @return void
  1008. */
  1009. public function testCreateEndGetNoSecurity()
  1010. {
  1011. $this->Form->request->params['_csrfToken'] = 'testKey';
  1012. $encoding = strtolower(Configure::read('App.encoding'));
  1013. $article = new Article();
  1014. $result = $this->Form->create($article, [
  1015. 'type' => 'get',
  1016. 'url' => '/contacts/add'
  1017. ]);
  1018. $this->assertNotContains('testKey', $result);
  1019. $result = $this->Form->end();
  1020. $this->assertNotContains('testKey', $result);
  1021. }
  1022. /**
  1023. * test that create() clears the fields property so it starts fresh
  1024. *
  1025. * @return void
  1026. */
  1027. public function testCreateClearingFields()
  1028. {
  1029. $this->Form->fields = ['model_id'];
  1030. $this->Form->create($this->article);
  1031. $this->assertEquals([], $this->Form->fields);
  1032. }
  1033. /**
  1034. * Tests form hash generation with model-less data
  1035. *
  1036. * @return void
  1037. */
  1038. public function testValidateHashNoModel()
  1039. {
  1040. $this->Form->request->params['_Token'] = 'foo';
  1041. $result = $this->Form->secure(['anything']);
  1042. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1043. }
  1044. /**
  1045. * Tests that hidden fields generated for checkboxes don't get locked
  1046. *
  1047. * @return void
  1048. */
  1049. public function testNoCheckboxLocking()
  1050. {
  1051. $this->Form->request->params['_Token'] = 'foo';
  1052. $this->assertSame([], $this->Form->fields);
  1053. $this->Form->checkbox('check', ['value' => '1']);
  1054. $this->assertSame($this->Form->fields, ['check']);
  1055. }
  1056. /**
  1057. * testFormSecurityFields method
  1058. *
  1059. * Test generation of secure form hash generation.
  1060. *
  1061. * @return void
  1062. */
  1063. public function testFormSecurityFields()
  1064. {
  1065. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1066. $this->Form->request->params['_Token'] = 'testKey';
  1067. $result = $this->Form->secure($fields);
  1068. $hash = Security::hash(serialize($fields) . Security::salt());
  1069. $hash .= ':' . 'Model.valid';
  1070. $hash = urlencode($hash);
  1071. $tokenDebug = urlencode(json_encode([
  1072. '',
  1073. $fields,
  1074. []
  1075. ]));
  1076. $expected = [
  1077. 'div' => ['style' => 'display:none;'],
  1078. ['input' => [
  1079. 'type' => 'hidden',
  1080. 'name' => '_Token[fields]',
  1081. 'value' => $hash
  1082. ]],
  1083. ['input' => [
  1084. 'type' => 'hidden',
  1085. 'name' => '_Token[unlocked]',
  1086. 'value' => '',
  1087. ]],
  1088. ['input' => [
  1089. 'type' => 'hidden',
  1090. 'name' => '_Token[debug]',
  1091. 'value' => $tokenDebug
  1092. ]],
  1093. '/div'
  1094. ];
  1095. $this->assertHtml($expected, $result);
  1096. }
  1097. /**
  1098. * testFormSecurityFields method
  1099. *
  1100. * Test debug token is not generated if debug is false
  1101. *
  1102. * @return void
  1103. */
  1104. public function testFormSecurityFieldsNoDebugMode()
  1105. {
  1106. Configure::write('debug', false);
  1107. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1108. $this->Form->request->params['_Token'] = 'testKey';
  1109. $result = $this->Form->secure($fields);
  1110. $hash = Security::hash(serialize($fields) . Security::salt());
  1111. $hash .= ':' . 'Model.valid';
  1112. $hash = urlencode($hash);
  1113. $expected = [
  1114. 'div' => ['style' => 'display:none;'],
  1115. ['input' => [
  1116. 'type' => 'hidden',
  1117. 'name' => '_Token[fields]',
  1118. 'value' => $hash
  1119. ]],
  1120. ['input' => [
  1121. 'type' => 'hidden',
  1122. 'name' => '_Token[unlocked]',
  1123. 'value' => '',
  1124. ]],
  1125. '/div'
  1126. ];
  1127. $this->assertHtml($expected, $result);
  1128. }
  1129. /**
  1130. * Tests correct generation of number fields for double and float fields
  1131. *
  1132. * @return void
  1133. */
  1134. public function testTextFieldGenerationForFloats()
  1135. {
  1136. $this->article['schema'] = [
  1137. 'foo' => [
  1138. 'type' => 'float',
  1139. 'null' => false,
  1140. 'default' => null,
  1141. 'length' => 10
  1142. ]
  1143. ];
  1144. $this->Form->create($this->article);
  1145. $result = $this->Form->input('foo');
  1146. $expected = [
  1147. 'div' => ['class' => 'input number'],
  1148. 'label' => ['for' => 'foo'],
  1149. 'Foo',
  1150. '/label',
  1151. ['input' => [
  1152. 'type' => 'number',
  1153. 'name' => 'foo',
  1154. 'id' => 'foo',
  1155. 'step' => 'any'
  1156. ]],
  1157. '/div'
  1158. ];
  1159. $this->assertHtml($expected, $result);
  1160. $result = $this->Form->input('foo', ['step' => 0.5]);
  1161. $expected = [
  1162. 'div' => ['class' => 'input number'],
  1163. 'label' => ['for' => 'foo'],
  1164. 'Foo',
  1165. '/label',
  1166. ['input' => [
  1167. 'type' => 'number',
  1168. 'name' => 'foo',
  1169. 'id' => 'foo',
  1170. 'step' => '0.5'
  1171. ]],
  1172. '/div'
  1173. ];
  1174. $this->assertHtml($expected, $result);
  1175. }
  1176. /**
  1177. * Tests correct generation of number fields for integer fields
  1178. *
  1179. * @return void
  1180. */
  1181. public function testTextFieldTypeNumberGenerationForIntegers()
  1182. {
  1183. TableRegistry::get('Contacts', [
  1184. 'className' => __NAMESPACE__ . '\ContactsTable'
  1185. ]);
  1186. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1187. $result = $this->Form->input('age');
  1188. $expected = [
  1189. 'div' => ['class' => 'input number'],
  1190. 'label' => ['for' => 'age'],
  1191. 'Age',
  1192. '/label',
  1193. ['input' => [
  1194. 'type' => 'number', 'name' => 'age',
  1195. 'id' => 'age'
  1196. ]],
  1197. '/div'
  1198. ];
  1199. $this->assertHtml($expected, $result);
  1200. }
  1201. /**
  1202. * Tests correct generation of file upload fields for binary fields
  1203. *
  1204. * @return void
  1205. */
  1206. public function testFileUploadFieldTypeGenerationForBinaries()
  1207. {
  1208. $table = TableRegistry::get('Contacts', [
  1209. 'className' => __NAMESPACE__ . '\ContactsTable'
  1210. ]);
  1211. $table->schema(['foo' => [
  1212. 'type' => 'binary',
  1213. 'null' => false,
  1214. 'default' => null,
  1215. 'length' => 1024
  1216. ]]);
  1217. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1218. $result = $this->Form->input('foo');
  1219. $expected = [
  1220. 'div' => ['class' => 'input file'],
  1221. 'label' => ['for' => 'foo'],
  1222. 'Foo',
  1223. '/label',
  1224. ['input' => [
  1225. 'type' => 'file', 'name' => 'foo',
  1226. 'id' => 'foo'
  1227. ]],
  1228. '/div'
  1229. ];
  1230. $this->assertHtml($expected, $result);
  1231. }
  1232. /**
  1233. * testFormSecurityMultipleFields method
  1234. *
  1235. * Test secure() with multiple row form. Ensure hash is correct.
  1236. *
  1237. * @return void
  1238. */
  1239. public function testFormSecurityMultipleFields()
  1240. {
  1241. $this->Form->request->params['_Token'] = 'foo';
  1242. $fields = [
  1243. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1244. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1245. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1246. ];
  1247. $result = $this->Form->secure($fields);
  1248. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1249. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1250. $tokenDebug = urlencode(json_encode([
  1251. '',
  1252. $fields,
  1253. []
  1254. ]));
  1255. $expected = [
  1256. 'div' => ['style' => 'display:none;'],
  1257. ['input' => [
  1258. 'type' => 'hidden', 'name' => '_Token[fields]',
  1259. 'value' => $hash
  1260. ]],
  1261. ['input' => [
  1262. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1263. 'value' => ''
  1264. ]],
  1265. ['input' => [
  1266. 'type' => 'hidden', 'name' => '_Token[debug]',
  1267. 'value' => $tokenDebug,
  1268. ]],
  1269. '/div'
  1270. ];
  1271. $this->assertHtml($expected, $result);
  1272. }
  1273. /**
  1274. * testFormSecurityMultipleSubmitButtons
  1275. *
  1276. * test form submit generation and ensure that _Token is only created on end()
  1277. *
  1278. * @return void
  1279. */
  1280. public function testFormSecurityMultipleSubmitButtons()
  1281. {
  1282. $this->Form->request->params['_Token'] = 'testKey';
  1283. $this->Form->create($this->article);
  1284. $this->Form->text('Address.title');
  1285. $this->Form->text('Address.first_name');
  1286. $result = $this->Form->submit('Save', ['name' => 'save']);
  1287. $expected = [
  1288. 'div' => ['class' => 'submit'],
  1289. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1290. '/div',
  1291. ];
  1292. $this->assertHtml($expected, $result);
  1293. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1294. $expected = [
  1295. 'div' => ['class' => 'submit'],
  1296. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1297. '/div',
  1298. ];
  1299. $this->assertHtml($expected, $result);
  1300. $result = $this->Form->end();
  1301. $tokenDebug = urlencode(json_encode([
  1302. '/articles/add',
  1303. [
  1304. 'Address.title',
  1305. 'Address.first_name',
  1306. ],
  1307. ['save', 'cancel']
  1308. ]));
  1309. $expected = [
  1310. 'div' => ['style' => 'display:none;'],
  1311. ['input' => [
  1312. 'type' => 'hidden',
  1313. 'name' => '_Token[fields]',
  1314. 'value'
  1315. ]],
  1316. ['input' => [
  1317. 'type' => 'hidden',
  1318. 'name' => '_Token[unlocked]',
  1319. 'value' => 'cancel%7Csave'
  1320. ]],
  1321. ['input' => [
  1322. 'type' => 'hidden', 'name' => '_Token[debug]',
  1323. 'value' => $tokenDebug,
  1324. ]],
  1325. '/div'
  1326. ];
  1327. $this->assertHtml($expected, $result);
  1328. }
  1329. /**
  1330. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1331. *
  1332. * @return void
  1333. */
  1334. public function testSecurityButtonNestedNamed()
  1335. {
  1336. $key = 'testKey';
  1337. $this->Form->request->params['_csrfToken'] = $key;
  1338. $this->Form->create('Addresses');
  1339. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1340. $result = $this->Form->unlockField();
  1341. $this->assertEquals(['Address.button'], $result);
  1342. }
  1343. /**
  1344. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1345. *
  1346. * @return void
  1347. */
  1348. public function testSecuritySubmitNestedNamed()
  1349. {
  1350. $this->Form->request->params['_Token'] = 'testKey';
  1351. $this->Form->create($this->article);
  1352. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1353. $result = $this->Form->unlockField();
  1354. $this->assertEquals(['Address.button'], $result);
  1355. }
  1356. /**
  1357. * Test that the correct fields are unlocked for image submits with no names.
  1358. *
  1359. * @return void
  1360. */
  1361. public function testSecuritySubmitImageNoName()
  1362. {
  1363. $key = 'testKey';
  1364. $this->Form->request->params['_Token'] = 'testKey';
  1365. $this->Form->create(false);
  1366. $result = $this->Form->submit('save.png');
  1367. $expected = [
  1368. 'div' => ['class' => 'submit'],
  1369. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1370. '/div'
  1371. ];
  1372. $this->assertHtml($expected, $result);
  1373. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1374. }
  1375. /**
  1376. * Test that the correct fields are unlocked for image submits with names.
  1377. *
  1378. * @return void
  1379. */
  1380. public function testSecuritySubmitImageName()
  1381. {
  1382. $this->Form->request->params['_Token'] = 'testKey';
  1383. $this->Form->create(null);
  1384. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1385. $expected = [
  1386. 'div' => ['class' => 'submit'],
  1387. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1388. '/div'
  1389. ];
  1390. $this->assertHtml($expected, $result);
  1391. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1392. }
  1393. /**
  1394. * testFormSecurityMultipleInputFields method
  1395. *
  1396. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1397. *
  1398. * @return void
  1399. */
  1400. public function testFormSecurityMultipleInputFields()
  1401. {
  1402. $this->Form->request->params['_Token'] = 'testKey';
  1403. $this->Form->create();
  1404. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1405. $this->Form->input('Addresses.0.title');
  1406. $this->Form->input('Addresses.0.first_name');
  1407. $this->Form->input('Addresses.0.last_name');
  1408. $this->Form->input('Addresses.0.address');
  1409. $this->Form->input('Addresses.0.city');
  1410. $this->Form->input('Addresses.0.phone');
  1411. $this->Form->input('Addresses.0.primary', ['type' => 'checkbox']);
  1412. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1413. $this->Form->input('Addresses.1.title');
  1414. $this->Form->input('Addresses.1.first_name');
  1415. $this->Form->input('Addresses.1.last_name');
  1416. $this->Form->input('Addresses.1.address');
  1417. $this->Form->input('Addresses.1.city');
  1418. $this->Form->input('Addresses.1.phone');
  1419. $this->Form->input('Addresses.1.primary', ['type' => 'checkbox']);
  1420. $result = $this->Form->secure($this->Form->fields);
  1421. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1422. $tokenDebug = urlencode(json_encode([
  1423. '/articles/add',
  1424. [
  1425. 'Addresses.0.id' => '123456',
  1426. 'Addresses.0.title',
  1427. 'Addresses.0.first_name',
  1428. 'Addresses.0.last_name',
  1429. 'Addresses.0.address',
  1430. 'Addresses.0.city',
  1431. 'Addresses.0.phone',
  1432. 'Addresses.0.primary',
  1433. 'Addresses.1.id' => '654321',
  1434. 'Addresses.1.title',
  1435. 'Addresses.1.first_name',
  1436. 'Addresses.1.last_name',
  1437. 'Addresses.1.address',
  1438. 'Addresses.1.city',
  1439. 'Addresses.1.phone',
  1440. 'Addresses.1.primary',
  1441. ],
  1442. []
  1443. ]));
  1444. $expected = [
  1445. 'div' => ['style' => 'display:none;'],
  1446. ['input' => [
  1447. 'type' => 'hidden', 'name' => '_Token[fields]',
  1448. 'value' => $hash
  1449. ]],
  1450. ['input' => [
  1451. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1452. 'value' => ''
  1453. ]],
  1454. ['input' => [
  1455. 'type' => 'hidden', 'name' => '_Token[debug]',
  1456. 'value' => $tokenDebug,
  1457. ]],
  1458. '/div'
  1459. ];
  1460. $this->assertHtml($expected, $result);
  1461. }
  1462. /**
  1463. * Test form security with Model.field.0 style inputs
  1464. *
  1465. * @return void
  1466. */
  1467. public function testFormSecurityArrayFields()
  1468. {
  1469. $this->Form->request->params['_Token'] = 'testKey';
  1470. $this->Form->create();
  1471. $this->Form->text('Address.primary.1');
  1472. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1473. $this->Form->text('Address.secondary.1.0');
  1474. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1475. }
  1476. /**
  1477. * testFormSecurityMultipleInputDisabledFields method
  1478. *
  1479. * test secure form generation with multiple records and disabled fields.
  1480. *
  1481. * @return void
  1482. */
  1483. public function testFormSecurityMultipleInputDisabledFields()
  1484. {
  1485. $this->Form->request->params['_Token'] = [
  1486. 'unlockedFields' => ['first_name', 'address']
  1487. ];
  1488. $this->Form->create();
  1489. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1490. $this->Form->text('Addresses.0.title');
  1491. $this->Form->text('Addresses.0.first_name');
  1492. $this->Form->text('Addresses.0.last_name');
  1493. $this->Form->text('Addresses.0.address');
  1494. $this->Form->text('Addresses.0.city');
  1495. $this->Form->text('Addresses.0.phone');
  1496. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1497. $this->Form->text('Addresses.1.title');
  1498. $this->Form->text('Addresses.1.first_name');
  1499. $this->Form->text('Addresses.1.last_name');
  1500. $this->Form->text('Addresses.1.address');
  1501. $this->Form->text('Addresses.1.city');
  1502. $this->Form->text('Addresses.1.phone');
  1503. $result = $this->Form->secure($this->Form->fields);
  1504. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1505. $tokenDebug = urlencode(json_encode([
  1506. '/articles/add',
  1507. [
  1508. 'Addresses.0.id' => '123456',
  1509. 'Addresses.0.title',
  1510. 'Addresses.0.last_name',
  1511. 'Addresses.0.city',
  1512. 'Addresses.0.phone',
  1513. 'Addresses.1.id' => '654321',
  1514. 'Addresses.1.title',
  1515. 'Addresses.1.last_name',
  1516. 'Addresses.1.city',
  1517. 'Addresses.1.phone'
  1518. ],
  1519. [
  1520. 'first_name',
  1521. 'address'
  1522. ]
  1523. ]));
  1524. $expected = [
  1525. 'div' => ['style' => 'display:none;'],
  1526. ['input' => [
  1527. 'type' => 'hidden',
  1528. 'name' => '_Token[fields]',
  1529. 'value' => $hash
  1530. ]],
  1531. ['input' => [
  1532. 'type' => 'hidden',
  1533. 'name' => '_Token[unlocked]',
  1534. 'value' => 'address%7Cfirst_name',
  1535. ]],
  1536. ['input' => [
  1537. 'type' => 'hidden', 'name' => '_Token[debug]',
  1538. 'value' => $tokenDebug,
  1539. ]],
  1540. '/div'
  1541. ];
  1542. $this->assertHtml($expected, $result);
  1543. }
  1544. /**
  1545. * testFormSecurityInputDisabledFields method
  1546. *
  1547. * Test single record form with disabled fields.
  1548. *
  1549. * @return void
  1550. */
  1551. public function testFormSecurityInputUnlockedFields()
  1552. {
  1553. $this->Form->request['_Token'] = [
  1554. 'unlockedFields' => ['first_name', 'address']
  1555. ];
  1556. $this->Form->create();
  1557. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1558. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1559. $this->Form->text('Addresses.title');
  1560. $this->Form->text('Addresses.first_name');
  1561. $this->Form->text('Addresses.last_name');
  1562. $this->Form->text('Addresses.address');
  1563. $this->Form->text('Addresses.city');
  1564. $this->Form->text('Addresses.phone');
  1565. $result = $this->Form->fields;
  1566. $expected = [
  1567. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1568. 'Addresses.city', 'Addresses.phone'
  1569. ];
  1570. $this->assertEquals($expected, $result);
  1571. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1572. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1573. $tokenDebug = urlencode(json_encode([
  1574. '/articles/add',
  1575. [
  1576. 'Addresses.id' => '123456',
  1577. 'Addresses.title',
  1578. 'Addresses.last_name',
  1579. 'Addresses.city',
  1580. 'Addresses.phone'
  1581. ],
  1582. [
  1583. 'first_name',
  1584. 'address'
  1585. ]
  1586. ]));
  1587. $expected = [
  1588. 'div' => ['style' => 'display:none;'],
  1589. ['input' => [
  1590. 'type' => 'hidden',
  1591. 'name' => '_Token[fields]',
  1592. 'value' => $hash,
  1593. 'data-foo' => 'bar',
  1594. ]],
  1595. ['input' => [
  1596. 'type' => 'hidden',
  1597. 'name' => '_Token[unlocked]',
  1598. 'value' => 'address%7Cfirst_name',
  1599. 'data-foo' => 'bar',
  1600. ]],
  1601. ['input' => [
  1602. 'type' => 'hidden', 'name' => '_Token[debug]',
  1603. 'value' => $tokenDebug,
  1604. 'data-foo' => 'bar'
  1605. ]],
  1606. '/div'
  1607. ];
  1608. $this->assertHtml($expected, $result);
  1609. }
  1610. /**
  1611. * testFormSecurityInputUnlockedFieldsDebugSecurityTrue method
  1612. *
  1613. * Test single record form with debugSecurity param.
  1614. *
  1615. * @return void
  1616. */
  1617. public function testFormSecurityInputUnlockedFieldsDebugSecurityTrue()
  1618. {
  1619. $this->Form->request['_Token'] = [
  1620. 'unlockedFields' => ['first_name', 'address']
  1621. ];
  1622. $this->Form->create();
  1623. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1624. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1625. $this->Form->text('Addresses.title');
  1626. $this->Form->text('Addresses.first_name');
  1627. $this->Form->text('Addresses.last_name');
  1628. $this->Form->text('Addresses.address');
  1629. $this->Form->text('Addresses.city');
  1630. $this->Form->text('Addresses.phone');
  1631. $result = $this->Form->fields;
  1632. $expected = [
  1633. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1634. 'Addresses.city', 'Addresses.phone'
  1635. ];
  1636. $this->assertEquals($expected, $result);
  1637. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1638. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1639. $tokenDebug = urlencode(json_encode([
  1640. '/articles/add',
  1641. [
  1642. 'Addresses.id' => '123456',
  1643. 'Addresses.title',
  1644. 'Addresses.last_name',
  1645. 'Addresses.city',
  1646. 'Addresses.phone'
  1647. ],
  1648. [
  1649. 'first_name',
  1650. 'address'
  1651. ]
  1652. ]));
  1653. $expected = [
  1654. 'div' => ['style' => 'display:none;'],
  1655. ['input' => [
  1656. 'type' => 'hidden',
  1657. 'name' => '_Token[fields]',
  1658. 'value' => $hash,
  1659. 'data-foo' => 'bar',
  1660. ]],
  1661. ['input' => [
  1662. 'type' => 'hidden',
  1663. 'name' => '_Token[unlocked]',
  1664. 'value' => 'address%7Cfirst_name',
  1665. 'data-foo' => 'bar',
  1666. ]],
  1667. ['input' => [
  1668. 'type' => 'hidden', 'name' => '_Token[debug]',
  1669. 'value' => $tokenDebug,
  1670. 'data-foo' => 'bar'
  1671. ]],
  1672. '/div'
  1673. ];
  1674. $this->assertHtml($expected, $result);
  1675. }
  1676. /**
  1677. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1678. *
  1679. * Debug is false, debugSecurity is true -> no debug
  1680. *
  1681. * @return void
  1682. */
  1683. public function testFormSecurityInputUnlockedFieldsDebugSecurityDebugFalse()
  1684. {
  1685. $this->Form->request['_Token'] = [
  1686. 'unlockedFields' => ['first_name', 'address']
  1687. ];
  1688. $this->Form->create();
  1689. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1690. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1691. $this->Form->text('Addresses.title');
  1692. $this->Form->text('Addresses.first_name');
  1693. $this->Form->text('Addresses.last_name');
  1694. $this->Form->text('Addresses.address');
  1695. $this->Form->text('Addresses.city');
  1696. $this->Form->text('Addresses.phone');
  1697. $result = $this->Form->fields;
  1698. $expected = [
  1699. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1700. 'Addresses.city', 'Addresses.phone'
  1701. ];
  1702. $this->assertEquals($expected, $result);
  1703. Configure::write('debug', false);
  1704. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1705. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1706. $expected = [
  1707. 'div' => ['style' => 'display:none;'],
  1708. ['input' => [
  1709. 'type' => 'hidden',
  1710. 'name' => '_Token[fields]',
  1711. 'value' => $hash,
  1712. 'data-foo' => 'bar',
  1713. ]],
  1714. ['input' => [
  1715. 'type' => 'hidden',
  1716. 'name' => '_Token[unlocked]',
  1717. 'value' => 'address%7Cfirst_name',
  1718. 'data-foo' => 'bar',
  1719. ]],
  1720. '/div'
  1721. ];
  1722. $this->assertHtml($expected, $result);
  1723. }
  1724. /**
  1725. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1726. *
  1727. * Test single record form with debugSecurity param.
  1728. *
  1729. * @return void
  1730. */
  1731. public function testFormSecurityInputUnlockedFieldsDebugSecurityFalse()
  1732. {
  1733. $this->Form->request['_Token'] = [
  1734. 'unlockedFields' => ['first_name', 'address']
  1735. ];
  1736. $this->Form->create();
  1737. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1738. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1739. $this->Form->text('Addresses.title');
  1740. $this->Form->text('Addresses.first_name');
  1741. $this->Form->text('Addresses.last_name');
  1742. $this->Form->text('Addresses.address');
  1743. $this->Form->text('Addresses.city');
  1744. $this->Form->text('Addresses.phone');
  1745. $result = $this->Form->fields;
  1746. $expected = [
  1747. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1748. 'Addresses.city', 'Addresses.phone'
  1749. ];
  1750. $this->assertEquals($expected, $result);
  1751. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1752. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1753. $expected = [
  1754. 'div' => ['style' => 'display:none;'],
  1755. ['input' => [
  1756. 'type' => 'hidden',
  1757. 'name' => '_Token[fields]',
  1758. 'value' => $hash,
  1759. 'data-foo' => 'bar',
  1760. ]],
  1761. ['input' => [
  1762. 'type' => 'hidden',
  1763. 'name' => '_Token[unlocked]',
  1764. 'value' => 'address%7Cfirst_name',
  1765. 'data-foo' => 'bar',
  1766. ]],
  1767. '/div'
  1768. ];
  1769. $this->assertHtml($expected, $result);
  1770. }
  1771. /**
  1772. * test securing inputs with custom name attributes.
  1773. *
  1774. * @return void
  1775. */
  1776. public function testFormSecureWithCustomNameAttribute()
  1777. {
  1778. $this->Form->request->params['_Token'] = 'testKey';
  1779. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1780. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1781. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1782. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1783. }
  1784. /**
  1785. * testFormSecuredInput method
  1786. *
  1787. * Test generation of entire secure form, assertions made on input() output.
  1788. *
  1789. * @return void
  1790. */
  1791. public function testFormSecuredInput()
  1792. {
  1793. $this->Form->request->params['_csrfToken'] = 'testKey';
  1794. $this->Form->request->params['_Token'] = 'stuff';
  1795. $this->article['schema'] = [
  1796. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1797. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1798. ];
  1799. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1800. $encoding = strtolower(Configure::read('App.encoding'));
  1801. $expected = [
  1802. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1803. 'div' => ['style' => 'display:none;'],
  1804. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1805. ['input' => [
  1806. 'type' => 'hidden',
  1807. 'name' => '_csrfToken',
  1808. 'value' => 'testKey'
  1809. ]],
  1810. '/div'
  1811. ];
  1812. $this->assertHtml($expected, $result);
  1813. $result = $this->Form->input('ratio');
  1814. $expected = [
  1815. 'div' => ['class'],
  1816. 'label' => ['for'],
  1817. 'Ratio',
  1818. '/label',
  1819. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1820. '/div',
  1821. ];
  1822. $this->assertHtml($expected, $result);
  1823. $result = $this->Form->input('population');
  1824. $expected = [
  1825. 'div' => ['class'],
  1826. 'label' => ['for'],
  1827. 'Population',
  1828. '/label',
  1829. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1830. '/div',
  1831. ];
  1832. $this->assertHtml($expected, $result);
  1833. $result = $this->Form->input('published', ['type' => 'text']);
  1834. $expected = [
  1835. 'div' => ['class' => 'input text'],
  1836. 'label' => ['for' => 'published'],
  1837. 'Published',
  1838. '/label',
  1839. ['input' => [
  1840. 'type' => 'text',
  1841. 'name' => 'published',
  1842. 'id' => 'published'
  1843. ]],
  1844. '/div'
  1845. ];
  1846. $this->assertHtml($expected, $result);
  1847. $result = $this->Form->input('other', ['type' => 'text']);
  1848. $expected = [
  1849. 'div' => ['class' => 'input text'],
  1850. 'label' => ['for' => 'other'],
  1851. 'Other',
  1852. '/label',
  1853. ['input' => [
  1854. 'type' => 'text',
  1855. 'name' => 'other',
  1856. 'id',
  1857. ]],
  1858. '/div'
  1859. ];
  1860. $this->assertHtml($expected, $result);
  1861. $result = $this->Form->hidden('stuff');
  1862. $expected = [
  1863. 'input' => [
  1864. 'type' => 'hidden',
  1865. 'name' => 'stuff'
  1866. ]
  1867. ];
  1868. $this->assertHtml($expected, $result);
  1869. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1870. $expected = ['input' => [
  1871. 'type' => 'hidden',
  1872. 'name' => 'hidden',
  1873. 'value' => '0'
  1874. ]];
  1875. $this->assertHtml($expected, $result);
  1876. $result = $this->Form->input('something', ['type' => 'checkbox']);
  1877. $expected = [
  1878. 'div' => ['class' => 'input checkbox'],
  1879. ['input' => [
  1880. 'type' => 'hidden',
  1881. 'name' => 'something',
  1882. 'value' => '0'
  1883. ]],
  1884. 'label' => ['for' => 'something'],
  1885. ['input' => [
  1886. 'type' => 'checkbox',
  1887. 'name' => 'something',
  1888. 'value' => '1',
  1889. 'id' => 'something'
  1890. ]],
  1891. 'Something',
  1892. '/label',
  1893. '/div'
  1894. ];
  1895. $this->assertHtml($expected, $result);
  1896. $result = $this->Form->fields;
  1897. $expectedFields = [
  1898. 'ratio',
  1899. 'population',
  1900. 'published',
  1901. 'other',
  1902. 'stuff' => '',
  1903. 'hidden' => '0',
  1904. 'something'
  1905. ];
  1906. $this->assertEquals($expectedFields, $result);
  1907. $result = $this->Form->secure($this->Form->fields);
  1908. $tokenDebug = urlencode(json_encode([
  1909. '/articles/add',
  1910. $expectedFields,
  1911. []
  1912. ]));
  1913. $expected = [
  1914. 'div' => ['style' => 'display:none;'],
  1915. ['input' => [
  1916. 'type' => 'hidden',
  1917. 'name' => '_Token[fields]',
  1918. 'value'
  1919. ]],
  1920. ['input' => [
  1921. 'type' => 'hidden',
  1922. 'name' => '_Token[unlocked]',
  1923. 'value' => ''
  1924. ]],
  1925. ['input' => [
  1926. 'type' => 'hidden', 'name' => '_Token[debug]',
  1927. 'value' => $tokenDebug,
  1928. ]],
  1929. '/div'
  1930. ];
  1931. $this->assertHtml($expected, $result);
  1932. }
  1933. /**
  1934. * Test secured inputs with custom names.
  1935. *
  1936. * @return void
  1937. */
  1938. public function testSecuredInputCustomName()
  1939. {
  1940. $this->Form->request->params['_Token'] = 'testKey';
  1941. $this->assertEquals([], $this->Form->fields);
  1942. $this->Form->text('text_input', [
  1943. 'name' => 'Option[General.default_role]',
  1944. ]);
  1945. $expected = ['Option.General.default_role'];
  1946. $this->assertEquals($expected, $this->Form->fields);
  1947. $this->Form->select('select_box', [1, 2], [
  1948. 'name' => 'Option[General.select_role]',
  1949. ]);
  1950. $expected[] = 'Option.General.select_role';
  1951. $this->assertEquals($expected, $this->Form->fields);
  1952. $this->Form->text('other.things[]');
  1953. $expected[] = 'other.things';
  1954. $this->assertEquals($expected, $this->Form->fields);
  1955. }
  1956. /**
  1957. * Test that a hidden field followed by a visible field
  1958. * undoes the hidden field locking.
  1959. *
  1960. * @return void
  1961. */
  1962. public function testSecuredInputDuplicate()
  1963. {
  1964. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1965. $this->assertEquals([], $this->Form->fields);
  1966. $this->Form->input('text_val', [
  1967. 'type' => 'hidden',
  1968. 'value' => 'some text',
  1969. ]);
  1970. $expected = ['text_val' => 'some text'];
  1971. $this->assertEquals($expected, $this->Form->fields);
  1972. $this->Form->input('text_val', [
  1973. 'type' => 'text',
  1974. ]);
  1975. $expected = ['text_val'];
  1976. $this->assertEquals($expected, $this->Form->fields);
  1977. }
  1978. /**
  1979. * Tests that the correct keys are added to the field hash index
  1980. *
  1981. * @return void
  1982. */
  1983. public function testFormSecuredFileInput()
  1984. {
  1985. $this->assertEquals([], $this->Form->fields);
  1986. $this->Form->file('Attachment.file');
  1987. $expected = [
  1988. 'Attachment.file.name', 'Attachment.file.type',
  1989. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1990. 'Attachment.file.size'
  1991. ];
  1992. $this->assertEquals($expected, $this->Form->fields);
  1993. }
  1994. /**
  1995. * test that multiple selects keys are added to field hash
  1996. *
  1997. * @return void
  1998. */
  1999. public function testFormSecuredMultipleSelect()
  2000. {
  2001. $this->Form->request->params['_csrfToken'] = 'testKey';
  2002. $this->assertEquals([], $this->Form->fields);
  2003. $options = ['1' => 'one', '2' => 'two'];
  2004. $this->Form->select('Model.select', $options);
  2005. $expected = ['Model.select'];
  2006. $this->assertEquals($expected, $this->Form->fields);
  2007. $this->Form->fields = [];
  2008. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2009. $this->assertEquals($expected, $this->Form->fields);
  2010. }
  2011. /**
  2012. * testFormSecuredRadio method
  2013. *
  2014. * @return void
  2015. */
  2016. public function testFormSecuredRadio()
  2017. {
  2018. $this->Form->request->params['_Token'] = 'testKey';
  2019. $this->assertEquals([], $this->Form->fields);
  2020. $options = ['1' => 'option1', '2' => 'option2'];
  2021. $this->Form->radio('Test.test', $options);
  2022. $expected = ['Test.test'];
  2023. $this->assertEquals($expected, $this->Form->fields);
  2024. $this->Form->radio('Test.all', $options, [
  2025. 'disabled' => ['option1', 'option2']
  2026. ]);
  2027. $expected = ['Test.test', 'Test.all' => ''];
  2028. $this->assertEquals($expected, $this->Form->fields);
  2029. $this->Form->radio('Test.some', $options, [
  2030. 'disabled' => ['option1']
  2031. ]);
  2032. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2033. $this->assertEquals($expected, $this->Form->fields);
  2034. }
  2035. /**
  2036. * Test that when disabled is in a list based attribute array it works.
  2037. *
  2038. * @return void
  2039. */
  2040. public function testFormSecuredAndDisabledNotAssoc()
  2041. {
  2042. $this->Form->request->params['_Token'] = 'testKey';
  2043. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2044. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2045. $this->Form->text('Model.text', ['disabled']);
  2046. $this->Form->textarea('Model.textarea', ['disabled']);
  2047. $this->Form->password('Model.password', ['disabled']);
  2048. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2049. $expected = [
  2050. 'Model.radio' => ''
  2051. ];
  2052. $this->assertEquals($expected, $this->Form->fields);
  2053. }
  2054. /**
  2055. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  2056. * hashing.
  2057. *
  2058. * @return void
  2059. */
  2060. public function testFormSecuredAndDisabled()
  2061. {
  2062. $this->Form->request->params['_Token'] = 'testKey';
  2063. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2064. $this->Form->text('Model.text', ['disabled' => true]);
  2065. $this->Form->password('Model.text', ['disabled' => true]);
  2066. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2067. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2068. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2069. $this->Form->year('Model.year', ['disabled' => true]);
  2070. $this->Form->month('Model.month', ['disabled' => true]);
  2071. $this->Form->day('Model.day', ['disabled' => true]);
  2072. $this->Form->hour('Model.hour', ['disabled' => true]);
  2073. $this->Form->minute('Model.minute', ['disabled' => true]);
  2074. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2075. $expected = [
  2076. 'Model.radio' => ''
  2077. ];
  2078. $this->assertEquals($expected, $this->Form->fields);
  2079. }
  2080. /**
  2081. * testDisableSecurityUsingForm method
  2082. *
  2083. * @return void
  2084. */
  2085. public function testDisableSecurityUsingForm()
  2086. {
  2087. $this->Form->request['_Token'] = [
  2088. 'disabledFields' => []
  2089. ];
  2090. $this->Form->create();
  2091. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2092. $this->Form->text('Addresses.title');
  2093. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2094. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2095. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2096. $result = $this->Form->fields;
  2097. $expected = [
  2098. 'Addresses.id' => '123456', 'Addresses.title',
  2099. ];
  2100. $this->assertEquals($expected, $result);
  2101. }
  2102. /**
  2103. * test disableField
  2104. *
  2105. * @return void
  2106. */
  2107. public function testUnlockFieldAddsToList()
  2108. {
  2109. $this->Form->request['_Token'] = [
  2110. 'unlockedFields' => []
  2111. ];
  2112. $this->Form->unlockField('Contact.name');
  2113. $this->Form->text('Contact.name');
  2114. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2115. $this->assertEquals([], $this->Form->fields);
  2116. }
  2117. /**
  2118. * test unlockField removing from fields array.
  2119. *
  2120. * @return void
  2121. */
  2122. public function testUnlockFieldRemovingFromFields()
  2123. {
  2124. $this->Form->request['_Token'] = [
  2125. 'unlockedFields' => []
  2126. ];
  2127. $this->Form->create($this->article);
  2128. $this->Form->hidden('Article.id', ['value' => 1]);
  2129. $this->Form->text('Article.title');
  2130. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2131. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2132. $this->Form->unlockField('Article.title');
  2133. $this->Form->unlockField('Article.id');
  2134. $this->assertEquals([], $this->Form->fields);
  2135. }
  2136. /**
  2137. * test reset unlockFields, when create new form.
  2138. *
  2139. * @return void
  2140. */
  2141. public function testResetUnlockFields()
  2142. {
  2143. $this->Form->request['_Token'] = [
  2144. 'key' => 'testKey',
  2145. 'unlockedFields' => []
  2146. ];
  2147. $this->Form->unlockField('Contact.id');
  2148. $this->Form->create('Contact');
  2149. $this->Form->hidden('Contact.id', ['value' => 1]);
  2150. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2151. $this->Form->end();
  2152. $this->Form->create('Contact');
  2153. $this->Form->hidden('Contact.id', ['value' => 1]);
  2154. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2155. }
  2156. /**
  2157. * Test that only the path + query elements of a form's URL show up in their hash.
  2158. *
  2159. * @return void
  2160. */
  2161. public function testSecuredFormUrlIgnoresHost()
  2162. {
  2163. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2164. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2165. $this->Form->create($this->article, [
  2166. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2167. ]);
  2168. $result = $this->Form->secure();
  2169. $this->assertContains($expected, $result);
  2170. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2171. $result = $this->Form->secure();
  2172. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2173. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2174. $result = $this->Form->secure();
  2175. $this->assertContains($expected, $result, 'URL path + query should work.');
  2176. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2177. $result = $this->Form->secure();
  2178. $this->assertNotContains($expected, $result, 'URL is different');
  2179. }
  2180. /**
  2181. * Test that URL, HTML and identifer show up in their hashs.
  2182. *
  2183. * @return void
  2184. */
  2185. public function testSecuredFormUrlHasHtmlAndIdentifer()
  2186. {
  2187. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2188. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2189. $res = $this->Form->create($this->article, [
  2190. 'url' => [
  2191. 'controller' => 'articles',
  2192. 'action' => 'view',
  2193. '?' => [
  2194. 'page' => 1,
  2195. 'limit' => 10,
  2196. 'html' => '<>"',
  2197. ],
  2198. '#' => 'result',
  2199. ],
  2200. ]);
  2201. $result = $this->Form->secure();
  2202. $this->assertContains($expected, $result);
  2203. $this->Form->create($this->article, [
  2204. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2205. ]);
  2206. $result = $this->Form->secure();
  2207. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2208. $this->Form->create($this->article, [
  2209. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2210. ]);
  2211. $result = $this->Form->secure();
  2212. $this->assertContains($expected, $result, 'URL path + query should work.');
  2213. }
  2214. /**
  2215. * test error message display
  2216. *
  2217. * @return void
  2218. */
  2219. public function testErrorMessageDisplay()
  2220. {
  2221. $this->article['errors'] = [
  2222. 'Article' => [
  2223. 'title' => 'error message',
  2224. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2225. ]
  2226. ];
  2227. $this->Form->create($this->article);
  2228. $result = $this->Form->input('Article.title');
  2229. $expected = [
  2230. 'div' => ['class' => 'input text error'],
  2231. 'label' => ['for' => 'article-title'],
  2232. 'Title',
  2233. '/label',
  2234. 'input' => [
  2235. 'type' => 'text', 'name' => 'Article[title]',
  2236. 'id' => 'article-title', 'class' => 'form-error'
  2237. ],
  2238. ['div' => ['class' => 'error-message']],
  2239. 'error message',
  2240. '/div',
  2241. '/div'
  2242. ];
  2243. $this->assertHtml($expected, $result);
  2244. $result = $this->Form->input('Article.title', [
  2245. 'templates' => [
  2246. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2247. ]
  2248. ]);
  2249. $expected = [
  2250. 'div' => ['class' => 'input text error'],
  2251. 'label' => ['for' => 'article-title'],
  2252. 'Title',
  2253. '/label',
  2254. 'input' => [
  2255. 'type' => 'text', 'name' => 'Article[title]',
  2256. 'id' => 'article-title', 'class' => 'form-error'
  2257. ],
  2258. '/div'
  2259. ];
  2260. $this->assertHtml($expected, $result);
  2261. $result = $this->Form->input('Article.content');
  2262. $expected = [
  2263. 'div' => ['class' => 'input text error'],
  2264. 'label' => ['for' => 'article-content'],
  2265. 'Content',
  2266. '/label',
  2267. 'input' => [
  2268. 'type' => 'text', 'name' => 'Article[content]',
  2269. 'id' => 'article-content', 'class' => 'form-error'
  2270. ],
  2271. ['div' => ['class' => 'error-message']],
  2272. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2273. '/div',
  2274. '/div'
  2275. ];
  2276. $this->assertHtml($expected, $result);
  2277. $result = $this->Form->input('Article.content', ['error' => ['escape' => true]]);
  2278. $expected = [
  2279. 'div' => ['class' => 'input text error'],
  2280. 'label' => ['for' => 'article-content'],
  2281. 'Content',
  2282. '/label',
  2283. 'input' => [
  2284. 'type' => 'text', 'name' => 'Article[content]',
  2285. 'id' => 'article-content', 'class' => 'form-error'
  2286. ],
  2287. ['div' => ['class' => 'error-message']],
  2288. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2289. '/div',
  2290. '/div'
  2291. ];
  2292. $this->assertHtml($expected, $result);
  2293. $result = $this->Form->input('Article.content', ['error' => ['escape' => false]]);
  2294. $expected = [
  2295. 'div' => ['class' => 'input text error'],
  2296. 'label' => ['for' => 'article-content'],
  2297. 'Content',
  2298. '/label',
  2299. 'input' => [
  2300. 'type' => 'text', 'name' => 'Article[content]',
  2301. 'id' => 'article-content', 'class' => 'form-error'
  2302. ],
  2303. ['div' => ['class' => 'error-message']],
  2304. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2305. '/div',
  2306. '/div'
  2307. ];
  2308. $this->assertHtml($expected, $result);
  2309. }
  2310. /**
  2311. * Test validation errors, when validation message is an empty string.
  2312. *
  2313. * @return void
  2314. */
  2315. public function testEmptyErrorValidation()
  2316. {
  2317. $this->article['errors'] = [
  2318. 'Article' => ['title' => '']
  2319. ];
  2320. $this->Form->create($this->article);
  2321. $result = $this->Form->input('Article.title');
  2322. $expected = [
  2323. 'div' => ['class' => 'input text error'],
  2324. 'label' => ['for' => 'article-title'],
  2325. 'Title',
  2326. '/label',
  2327. 'input' => [
  2328. 'type' => 'text', 'name' => 'Article[title]',
  2329. 'id' => 'article-title', 'class' => 'form-error'
  2330. ],
  2331. ['div' => ['class' => 'error-message']],
  2332. [],
  2333. '/div',
  2334. '/div'
  2335. ];
  2336. $this->assertHtml($expected, $result);
  2337. }
  2338. /**
  2339. * Test validation errors, when calling input() overriding validation message by an empty string.
  2340. *
  2341. * @return void
  2342. */
  2343. public function testEmptyInputErrorValidation()
  2344. {
  2345. $this->article['errors'] = [
  2346. 'Article' => ['title' => 'error message']
  2347. ];
  2348. $this->Form->create($this->article);
  2349. $result = $this->Form->input('Article.title', ['error' => '']);
  2350. $expected = [
  2351. 'div' => ['class' => 'input text error'],
  2352. 'label' => ['for' => 'article-title'],
  2353. 'Title',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text', 'name' => 'Article[title]',
  2357. 'id' => 'article-title', 'class' => 'form-error'
  2358. ],
  2359. ['div' => ['class' => 'error-message']],
  2360. [],
  2361. '/div',
  2362. '/div'
  2363. ];
  2364. $this->assertHtml($expected, $result);
  2365. }
  2366. /**
  2367. * Test validation errors, when calling input() overriding validation messages
  2368. *
  2369. * @return void
  2370. */
  2371. public function testInputErrorMessage()
  2372. {
  2373. $this->article['errors'] = [
  2374. 'title' => ['error message']
  2375. ];
  2376. $this->Form->create($this->article);
  2377. $result = $this->Form->input('title', [
  2378. 'error' => 'Custom error!'
  2379. ]);
  2380. $expected = [
  2381. 'div' => ['class' => 'input text required error'],
  2382. 'label' => ['for' => 'title'],
  2383. 'Title',
  2384. '/label',
  2385. 'input' => [
  2386. 'type' => 'text', 'name' => 'title',
  2387. 'id' => 'title', 'class' => 'form-error',
  2388. 'required' => 'required',
  2389. ],
  2390. ['div' => ['class' => 'error-message']],
  2391. 'Custom error!',
  2392. '/div',
  2393. '/div'
  2394. ];
  2395. $this->assertHtml($expected, $result);
  2396. $result = $this->Form->input('title', [
  2397. 'error' => ['error message' => 'Custom error!']
  2398. ]);
  2399. $expected = [
  2400. 'div' => ['class' => 'input text required error'],
  2401. 'label' => ['for' => 'title'],
  2402. 'Title',
  2403. '/label',
  2404. 'input' => [
  2405. 'type' => 'text',
  2406. 'name' => 'title',
  2407. 'id' => 'title',
  2408. 'class' => 'form-error',
  2409. 'required' => 'required'
  2410. ],
  2411. ['div' => ['class' => 'error-message']],
  2412. 'Custom error!',
  2413. '/div',
  2414. '/div'
  2415. ];
  2416. $this->assertHtml($expected, $result);
  2417. }
  2418. /**
  2419. * Tests displaying errors for nested entities
  2420. *
  2421. * @return void
  2422. */
  2423. public function testFormValidationAssociated()
  2424. {
  2425. $nested = new Entity(['foo' => 'bar']);
  2426. $nested->errors('foo', ['not a valid bar']);
  2427. $entity = new Entity(['nested' => $nested]);
  2428. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2429. $result = $this->Form->error('nested.foo');
  2430. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2431. }
  2432. /**
  2433. * testFormValidationAssociatedSecondLevel method
  2434. *
  2435. * test form error display with associated model.
  2436. *
  2437. * @return void
  2438. */
  2439. public function testFormValidationAssociatedSecondLevel()
  2440. {
  2441. $inner = new Entity(['bar' => 'baz']);
  2442. $nested = new Entity(['foo' => $inner]);
  2443. $entity = new Entity(['nested' => $nested]);
  2444. $inner->errors('bar', ['not a valid one']);
  2445. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2446. $result = $this->Form->error('nested.foo.bar');
  2447. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2448. }
  2449. /**
  2450. * testFormValidationMultiRecord method
  2451. *
  2452. * test form error display with multiple records.
  2453. *
  2454. * @return void
  2455. */
  2456. public function testFormValidationMultiRecord()
  2457. {
  2458. $one = new Entity;
  2459. $two = new Entity;
  2460. TableRegistry::get('Contacts', [
  2461. 'className' => __NAMESPACE__ . '\ContactsTable'
  2462. ]);
  2463. $one->set('email', '');
  2464. $one->errors('email', ['invalid email']);
  2465. $two->set('name', '');
  2466. $two->errors('name', ['This is wrong']);
  2467. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2468. $result = $this->Form->input('0.email');
  2469. $expected = [
  2470. 'div' => ['class' => 'input email error'],
  2471. 'label' => ['for' => '0-email'],
  2472. 'Email',
  2473. '/label',
  2474. 'input' => [
  2475. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2476. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2477. ],
  2478. ['div' => ['class' => 'error-message']],
  2479. 'invalid email',
  2480. '/div',
  2481. '/div'
  2482. ];
  2483. $this->assertHtml($expected, $result);
  2484. $result = $this->Form->input('1.name');
  2485. $expected = [
  2486. 'div' => ['class' => 'input text error'],
  2487. 'label' => ['for' => '1-name'],
  2488. 'Name',
  2489. '/label',
  2490. 'input' => [
  2491. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2492. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2493. ],
  2494. ['div' => ['class' => 'error-message']],
  2495. 'This is wrong',
  2496. '/div',
  2497. '/div'
  2498. ];
  2499. $this->assertHtml($expected, $result);
  2500. }
  2501. /**
  2502. * testInput method
  2503. *
  2504. * Test various incarnations of input().
  2505. *
  2506. * @return void
  2507. */
  2508. public function testInput()
  2509. {
  2510. TableRegistry::get('ValidateUsers', [
  2511. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2512. ]);
  2513. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2514. $result = $this->Form->input('ValidateUsers.balance');
  2515. $expected = [
  2516. 'div' => ['class'],
  2517. 'label' => ['for'],
  2518. 'Balance',
  2519. '/label',
  2520. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2521. '/div',
  2522. ];
  2523. $this->assertHtml($expected, $result);
  2524. $result = $this->Form->input('ValidateUser.cost_decimal');
  2525. $expected = [
  2526. 'div' => ['class'],
  2527. 'label' => ['for'],
  2528. 'Cost Decimal',
  2529. '/label',
  2530. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2531. '/div',
  2532. ];
  2533. $this->assertHtml($expected, $result);
  2534. $result = $this->Form->input('ValidateUser.null_decimal');
  2535. $expected = [
  2536. 'div' => ['class'],
  2537. 'label' => ['for'],
  2538. 'Null Decimal',
  2539. '/label',
  2540. 'input' => ['name', 'type' => 'number', 'id'],
  2541. '/div',
  2542. ];
  2543. $this->assertHtml($expected, $result);
  2544. }
  2545. /**
  2546. * Tests the input method and passing custom options
  2547. *
  2548. * @return void
  2549. */
  2550. public function testInputCustomization()
  2551. {
  2552. TableRegistry::get('Contacts', [
  2553. 'className' => __NAMESPACE__ . '\ContactsTable'
  2554. ]);
  2555. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2556. $result = $this->Form->input('Contact.email', ['id' => 'custom']);
  2557. $expected = [
  2558. 'div' => ['class' => 'input email'],
  2559. 'label' => ['for' => 'custom'],
  2560. 'Email',
  2561. '/label',
  2562. ['input' => [
  2563. 'type' => 'email', 'name' => 'Contact[email]',
  2564. 'id' => 'custom', 'maxlength' => 255
  2565. ]],
  2566. '/div'
  2567. ];
  2568. $this->assertHtml($expected, $result);
  2569. $result = $this->Form->input('Contact.email', [
  2570. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2571. ]);
  2572. $expected = [
  2573. '<div',
  2574. 'label' => ['for' => 'contact-email'],
  2575. 'Email',
  2576. '/label',
  2577. ['input' => [
  2578. 'type' => 'email', 'name' => 'Contact[email]',
  2579. 'id' => 'contact-email', 'maxlength' => 255
  2580. ]],
  2581. '/div'
  2582. ];
  2583. $this->assertHtml($expected, $result);
  2584. $result = $this->Form->input('Contact.email', ['type' => 'text']);
  2585. $expected = [
  2586. 'div' => ['class' => 'input text'],
  2587. 'label' => ['for' => 'contact-email'],
  2588. 'Email',
  2589. '/label',
  2590. ['input' => [
  2591. 'type' => 'text', 'name' => 'Contact[email]',
  2592. 'id' => 'contact-email', 'maxlength' => '255'
  2593. ]],
  2594. '/div'
  2595. ];
  2596. $this->assertHtml($expected, $result);
  2597. $result = $this->Form->input('Contact.5.email', ['type' => 'text']);
  2598. $expected = [
  2599. 'div' => ['class' => 'input text'],
  2600. 'label' => ['for' => 'contact-5-email'],
  2601. 'Email',
  2602. '/label',
  2603. ['input' => [
  2604. 'type' => 'text', 'name' => 'Contact[5][email]',
  2605. 'id' => 'contact-5-email', 'maxlength' => '255'
  2606. ]],
  2607. '/div'
  2608. ];
  2609. $this->assertHtml($expected, $result);
  2610. $result = $this->Form->input('Contact.password');
  2611. $expected = [
  2612. 'div' => ['class' => 'input password'],
  2613. 'label' => ['for' => 'contact-password'],
  2614. 'Password',
  2615. '/label',
  2616. ['input' => [
  2617. 'type' => 'password', 'name' => 'Contact[password]',
  2618. 'id' => 'contact-password'
  2619. ]],
  2620. '/div'
  2621. ];
  2622. $this->assertHtml($expected, $result);
  2623. $result = $this->Form->input('Contact.email', [
  2624. 'type' => 'file', 'class' => 'textbox'
  2625. ]);
  2626. $expected = [
  2627. 'div' => ['class' => 'input file'],
  2628. 'label' => ['for' => 'contact-email'],
  2629. 'Email',
  2630. '/label',
  2631. ['input' => [
  2632. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2633. 'id' => 'contact-email'
  2634. ]],
  2635. '/div'
  2636. ];
  2637. $this->assertHtml($expected, $result);
  2638. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2639. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2640. $result = $this->Form->input('phone');
  2641. $expected = [
  2642. 'div' => ['class' => 'input tel'],
  2643. 'label' => ['for' => 'phone'],
  2644. 'Phone',
  2645. '/label',
  2646. ['input' => [
  2647. 'type' => 'tel', 'name' => 'phone',
  2648. 'value' => 'Hello &amp; World &gt; weird chars',
  2649. 'id' => 'phone', 'maxlength' => 255
  2650. ]],
  2651. '/div'
  2652. ];
  2653. $this->assertHtml($expected, $result);
  2654. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2655. $this->Form->create();
  2656. $result = $this->Form->input('Model.0.OtherModel.field', ['id' => 'myId']);
  2657. $expected = [
  2658. 'div' => ['class' => 'input text'],
  2659. 'label' => ['for' => 'myId'],
  2660. 'Field',
  2661. '/label',
  2662. 'input' => [
  2663. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2664. 'value' => 'My value', 'id' => 'myId'
  2665. ],
  2666. '/div'
  2667. ];
  2668. $this->assertHtml($expected, $result);
  2669. $this->Form->request->data = [];
  2670. $entity->errors('field', 'Badness!');
  2671. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2672. $result = $this->Form->input('field');
  2673. $expected = [
  2674. 'div' => ['class' => 'input text error'],
  2675. 'label' => ['for' => 'field'],
  2676. 'Field',
  2677. '/label',
  2678. 'input' => [
  2679. 'type' => 'text', 'name' => 'field',
  2680. 'id' => 'field', 'class' => 'form-error'
  2681. ],
  2682. ['div' => ['class' => 'error-message']],
  2683. 'Badness!',
  2684. '/div',
  2685. '/div'
  2686. ];
  2687. $this->assertHtml($expected, $result);
  2688. $result = $this->Form->input('field', [
  2689. 'templates' => [
  2690. 'inputContainerError' => '{{content}}{{error}}',
  2691. 'error' => '<span class="error-message">{{content}}</span>'
  2692. ]
  2693. ]);
  2694. $expected = [
  2695. 'label' => ['for' => 'field'],
  2696. 'Field',
  2697. '/label',
  2698. 'input' => [
  2699. 'type' => 'text', 'name' => 'field',
  2700. 'id' => 'field', 'class' => 'form-error'
  2701. ],
  2702. ['span' => ['class' => 'error-message']],
  2703. 'Badness!',
  2704. '/span'
  2705. ];
  2706. $this->assertHtml($expected, $result);
  2707. $entity->errors('field', ['minLength'], true);
  2708. $result = $this->Form->input('field', [
  2709. 'error' => [
  2710. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2711. 'maxLength' => 'login too large'
  2712. ]
  2713. ]);
  2714. $expected = [
  2715. 'div' => ['class' => 'input text error'],
  2716. 'label' => ['for' => 'field'],
  2717. 'Field',
  2718. '/label',
  2719. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2720. ['div' => ['class' => 'error-message']],
  2721. 'Le login doit contenir au moins 2 caractères',
  2722. '/div',
  2723. '/div'
  2724. ];
  2725. $this->assertHtml($expected, $result);
  2726. $entity->errors('field', ['maxLength'], true);
  2727. $result = $this->Form->input('field', [
  2728. 'error' => [
  2729. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2730. 'maxLength' => 'login too large',
  2731. ]
  2732. ]);
  2733. $expected = [
  2734. 'div' => ['class' => 'input text error'],
  2735. 'label' => ['for' => 'field'],
  2736. 'Field',
  2737. '/label',
  2738. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2739. ['div' => ['class' => 'error-message']],
  2740. 'login too large',
  2741. '/div',
  2742. '/div'
  2743. ];
  2744. $this->assertHtml($expected, $result);
  2745. }
  2746. /**
  2747. * Test that input() accepts a template file.
  2748. *
  2749. * @return void
  2750. */
  2751. public function testInputWithTemplateFile()
  2752. {
  2753. $result = $this->Form->input('field', [
  2754. 'templates' => 'htmlhelper_tags'
  2755. ]);
  2756. $expected = [
  2757. 'label' => ['for' => 'field'],
  2758. 'Field',
  2759. '/label',
  2760. 'input' => [
  2761. 'type' => 'text', 'name' => 'field',
  2762. 'id' => 'field'
  2763. ],
  2764. ];
  2765. $this->assertHtml($expected, $result);
  2766. }
  2767. /**
  2768. * Test that nested inputs end with brackets
  2769. *
  2770. * @return void
  2771. */
  2772. public function testNestedInputsEndWithBrackets()
  2773. {
  2774. $result = $this->Form->text('nested.text[]');
  2775. $expected = [
  2776. 'input' => [
  2777. 'type' => 'text', 'name' => 'nested[text][]'
  2778. ],
  2779. ];
  2780. $result = $this->Form->file('nested.file[]');
  2781. $expected = [
  2782. 'input' => [
  2783. 'type' => 'file', 'name' => 'nested[file][]'
  2784. ],
  2785. ];
  2786. $this->assertHtml($expected, $result);
  2787. }
  2788. /**
  2789. * Test id prefix
  2790. *
  2791. * @return void
  2792. */
  2793. public function testCreateIdPrefix()
  2794. {
  2795. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2796. $result = $this->Form->input('field');
  2797. $expected = [
  2798. 'div' => ['class' => 'input text'],
  2799. 'label' => ['for' => 'prefix-field'],
  2800. 'Field',
  2801. '/label',
  2802. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2803. '/div'
  2804. ];
  2805. $this->assertHtml($expected, $result);
  2806. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2807. $expected = [
  2808. 'div' => ['class' => 'input text'],
  2809. 'label' => ['for' => 'custom-id'],
  2810. 'Field',
  2811. '/label',
  2812. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2813. '/div'
  2814. ];
  2815. $this->assertHtml($expected, $result);
  2816. $result = $this->Form->radio('Model.field', ['option A']);
  2817. $expected = [
  2818. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2819. 'label' => ['for' => 'prefix-model-field-0'],
  2820. ['input' => [
  2821. 'type' => 'radio',
  2822. 'name' => 'Model[field]',
  2823. 'value' => '0',
  2824. 'id' => 'prefix-model-field-0'
  2825. ]],
  2826. 'option A',
  2827. '/label'
  2828. ];
  2829. $this->assertHtml($expected, $result);
  2830. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2831. $expected = [
  2832. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2833. 'label' => ['for' => 'prefix-model-field-0'],
  2834. ['input' => [
  2835. 'type' => 'radio',
  2836. 'name' => 'Model[field]',
  2837. 'value' => '0',
  2838. 'id' => 'prefix-model-field-0'
  2839. ]],
  2840. 'option A',
  2841. '/label'
  2842. ];
  2843. $this->assertHtml($expected, $result);
  2844. $result = $this->Form->select(
  2845. 'Model.multi_field',
  2846. ['first'],
  2847. ['multiple' => 'checkbox']
  2848. );
  2849. $expected = [
  2850. 'input' => [
  2851. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2852. ],
  2853. ['div' => ['class' => 'checkbox']],
  2854. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2855. ['input' => [
  2856. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2857. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2858. ]],
  2859. 'first',
  2860. '/label',
  2861. '/div',
  2862. ];
  2863. $this->assertHtml($expected, $result);
  2864. $this->Form->end();
  2865. $result = $this->Form->input('field');
  2866. $expected = [
  2867. 'div' => ['class' => 'input text'],
  2868. 'label' => ['for' => 'field'],
  2869. 'Field',
  2870. '/label',
  2871. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2872. '/div'
  2873. ];
  2874. $this->assertHtml($expected, $result);
  2875. }
  2876. /**
  2877. * Test that inputs with 0 can be created.
  2878. *
  2879. * @return void
  2880. */
  2881. public function testInputZero()
  2882. {
  2883. TableRegistry::get('Contacts', [
  2884. 'className' => __NAMESPACE__ . '\ContactsTable'
  2885. ]);
  2886. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2887. $result = $this->Form->input('0');
  2888. $expected = [
  2889. 'div' => ['class' => 'input text'],
  2890. 'label' => ['for' => '0'], '/label',
  2891. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2892. '/div'
  2893. ];
  2894. $this->assertHtml($expected, $result);
  2895. }
  2896. /**
  2897. * test input() with checkbox creation
  2898. *
  2899. * @return void
  2900. */
  2901. public function testInputCheckbox()
  2902. {
  2903. $result = $this->Form->input('User.active', ['label' => false, 'checked' => true]);
  2904. $expected = [
  2905. 'div' => ['class' => 'input checkbox'],
  2906. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2907. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2908. '/div'
  2909. ];
  2910. $this->assertHtml($expected, $result);
  2911. $result = $this->Form->input('User.active', ['label' => false, 'checked' => 1]);
  2912. $expected = [
  2913. 'div' => ['class' => 'input checkbox'],
  2914. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2915. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2916. '/div'
  2917. ];
  2918. $this->assertHtml($expected, $result);
  2919. $result = $this->Form->input('User.active', ['label' => false, 'checked' => '1']);
  2920. $expected = [
  2921. 'div' => ['class' => 'input checkbox'],
  2922. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2923. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2924. '/div'
  2925. ];
  2926. $this->assertHtml($expected, $result);
  2927. $result = $this->Form->input('User.disabled', [
  2928. 'label' => 'Disabled',
  2929. 'type' => 'checkbox',
  2930. 'data-foo' => 'disabled'
  2931. ]);
  2932. $expected = [
  2933. 'div' => ['class' => 'input checkbox'],
  2934. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2935. 'label' => ['for' => 'user-disabled'],
  2936. ['input' => [
  2937. 'type' => 'checkbox',
  2938. 'name' => 'User[disabled]',
  2939. 'value' => '1',
  2940. 'id' => 'user-disabled',
  2941. 'data-foo' => 'disabled'
  2942. ]],
  2943. 'Disabled',
  2944. '/label',
  2945. '/div'
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. $result = $this->Form->input('User.confirm', [
  2949. 'label' => 'Confirm <b>me</b>!',
  2950. 'type' => 'checkbox',
  2951. 'escape' => false
  2952. ]);
  2953. $expected = [
  2954. 'div' => ['class' => 'input checkbox'],
  2955. 'input' => ['type' => 'hidden', 'name' => 'User[confirm]', 'value' => '0'],
  2956. 'label' => ['for' => 'user-confirm'],
  2957. ['input' => [
  2958. 'type' => 'checkbox',
  2959. 'name' => 'User[confirm]',
  2960. 'value' => '1',
  2961. 'id' => 'user-confirm',
  2962. ]],
  2963. 'Confirm <b>me</b>!',
  2964. '/label',
  2965. '/div'
  2966. ];
  2967. $this->assertHtml($expected, $result);
  2968. }
  2969. /**
  2970. * Test that input() does not create wrapping div and label tag for hidden fields
  2971. *
  2972. * @return void
  2973. */
  2974. public function testInputHidden()
  2975. {
  2976. TableRegistry::get('ValidateUsers', [
  2977. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2978. ]);
  2979. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2980. $result = $this->Form->input('ValidateUser.id');
  2981. $expected = [
  2982. 'input' => ['name', 'type' => 'hidden', 'id']
  2983. ];
  2984. $this->assertHtml($expected, $result);
  2985. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2986. $expected = [
  2987. 'input' => ['name', 'type' => 'hidden', 'id']
  2988. ];
  2989. $this->assertHtml($expected, $result);
  2990. }
  2991. /**
  2992. * test form->input() with datetime
  2993. *
  2994. * @return void
  2995. */
  2996. public function testInputDatetime()
  2997. {
  2998. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  2999. ->setMethods(['datetime'])
  3000. ->setConstructorArgs([new View()])
  3001. ->getMock();
  3002. $this->Form->expects($this->once())->method('datetime')
  3003. ->with('prueba', [
  3004. 'type' => 'datetime',
  3005. 'timeFormat' => 24,
  3006. 'minYear' => 2008,
  3007. 'maxYear' => 2011,
  3008. 'interval' => 15,
  3009. 'options' => null,
  3010. 'empty' => false,
  3011. 'id' => 'prueba',
  3012. 'required' => false,
  3013. 'templateVars' => []
  3014. ])
  3015. ->will($this->returnValue('This is it!'));
  3016. $result = $this->Form->input('prueba', [
  3017. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3018. 'maxYear' => 2011, 'interval' => 15
  3019. ]);
  3020. $expected = [
  3021. 'div' => ['class' => 'input datetime'],
  3022. '<label',
  3023. 'Prueba',
  3024. '/label',
  3025. 'This is it!',
  3026. '/div'
  3027. ];
  3028. $this->assertHtml($expected, $result);
  3029. }
  3030. /**
  3031. * test form->input() with datetime with id prefix
  3032. *
  3033. * @return void
  3034. */
  3035. public function testInputDatetimeIdPrefix()
  3036. {
  3037. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3038. ->setMethods(['datetime'])
  3039. ->setConstructorArgs([new View()])
  3040. ->getMock();
  3041. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3042. $this->Form->expects($this->once())->method('datetime')
  3043. ->with('prueba', [
  3044. 'type' => 'datetime',
  3045. 'timeFormat' => 24,
  3046. 'minYear' => 2008,
  3047. 'maxYear' => 2011,
  3048. 'interval' => 15,
  3049. 'options' => null,
  3050. 'empty' => false,
  3051. 'id' => 'prefix-prueba',
  3052. 'required' => false,
  3053. 'templateVars' => []
  3054. ])
  3055. ->will($this->returnValue('This is it!'));
  3056. $result = $this->Form->input('prueba', [
  3057. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3058. 'maxYear' => 2011, 'interval' => 15
  3059. ]);
  3060. $expected = [
  3061. 'div' => ['class' => 'input datetime'],
  3062. '<label',
  3063. 'Prueba',
  3064. '/label',
  3065. 'This is it!',
  3066. '/div'
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. }
  3070. /**
  3071. * Test generating checkboxes with disabled elements.
  3072. *
  3073. * @return void
  3074. */
  3075. public function testInputCheckboxWithDisabledElements()
  3076. {
  3077. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3078. $result = $this->Form->input('Contact.multiple', [
  3079. 'multiple' => 'checkbox',
  3080. 'disabled' => 'disabled',
  3081. 'options' => $options
  3082. ]);
  3083. $expected = [
  3084. ['div' => ['class' => 'input select']],
  3085. ['label' => ['for' => "contact-multiple"]],
  3086. 'Multiple',
  3087. '/label',
  3088. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3089. ['div' => ['class' => 'checkbox']],
  3090. ['label' => ['for' => "contact-multiple-1"]],
  3091. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3092. 'One',
  3093. '/label',
  3094. '/div',
  3095. ['div' => ['class' => 'checkbox']],
  3096. ['label' => ['for' => "contact-multiple-2"]],
  3097. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3098. 'Two',
  3099. '/label',
  3100. '/div',
  3101. ['div' => ['class' => 'checkbox']],
  3102. ['label' => ['for' => "contact-multiple-3"]],
  3103. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3104. 'Three',
  3105. '/label',
  3106. '/div',
  3107. '/div'
  3108. ];
  3109. $this->assertHtml($expected, $result);
  3110. // make sure 50 does only disable 50, and not 50f5c0cf
  3111. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3112. $disabled = [50];
  3113. $expected = [
  3114. ['div' => ['class' => 'input select']],
  3115. ['label' => ['for' => "contact-multiple"]],
  3116. 'Multiple',
  3117. '/label',
  3118. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3119. ['div' => ['class' => 'checkbox']],
  3120. ['label' => ['for' => "contact-multiple-50"]],
  3121. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3122. 'Fifty',
  3123. '/label',
  3124. '/div',
  3125. ['div' => ['class' => 'checkbox']],
  3126. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3127. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3128. 'Stringy',
  3129. '/label',
  3130. '/div',
  3131. '/div'
  3132. ];
  3133. $result = $this->Form->input('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3134. $this->assertHtml($expected, $result);
  3135. }
  3136. /**
  3137. * test input name with leading integer, ensure attributes are generated correctly.
  3138. *
  3139. * @return void
  3140. */
  3141. public function testInputWithLeadingInteger()
  3142. {
  3143. $result = $this->Form->text('0.Node.title');
  3144. $expected = [
  3145. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3146. ];
  3147. $this->assertHtml($expected, $result);
  3148. }
  3149. /**
  3150. * test form->input() with select type inputs.
  3151. *
  3152. * @return void
  3153. */
  3154. public function testInputSelectType()
  3155. {
  3156. $result = $this->Form->input(
  3157. 'email',
  3158. [
  3159. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3160. );
  3161. $expected = [
  3162. 'div' => ['class' => 'input select'],
  3163. 'label' => ['for' => 'email'],
  3164. 'Email',
  3165. '/label',
  3166. ['select' => ['name' => 'email', 'id' => 'email']],
  3167. ['option' => ['value' => '']],
  3168. '/option',
  3169. ['option' => ['value' => 'è']],
  3170. 'Firést',
  3171. '/option',
  3172. ['option' => ['value' => 'é']],
  3173. 'Secoènd',
  3174. '/option',
  3175. '/select',
  3176. '/div'
  3177. ];
  3178. $this->assertHtml($expected, $result);
  3179. $result = $this->Form->input(
  3180. 'email',
  3181. [
  3182. 'options' => ['First', 'Second'], 'empty' => true]
  3183. );
  3184. $expected = [
  3185. 'div' => ['class' => 'input select'],
  3186. 'label' => ['for' => 'email'],
  3187. 'Email',
  3188. '/label',
  3189. ['select' => ['name' => 'email', 'id' => 'email']],
  3190. ['option' => ['value' => '']],
  3191. '/option',
  3192. ['option' => ['value' => '0']],
  3193. 'First',
  3194. '/option',
  3195. ['option' => ['value' => '1']],
  3196. 'Second',
  3197. '/option',
  3198. '/select',
  3199. '/div'
  3200. ];
  3201. $this->assertHtml($expected, $result);
  3202. $result = $this->Form->input('email', [
  3203. 'type' => 'select',
  3204. 'options' => new \ArrayObject(['First', 'Second']),
  3205. 'empty' => true
  3206. ]);
  3207. $this->assertHtml($expected, $result);
  3208. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3209. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3210. $result = $this->Form->input('Model.user_id', ['empty' => true]);
  3211. $expected = [
  3212. 'div' => ['class' => 'input select'],
  3213. 'label' => ['for' => 'model-user-id'],
  3214. 'User',
  3215. '/label',
  3216. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3217. ['option' => ['value' => '']],
  3218. '/option',
  3219. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3220. 'good',
  3221. '/option',
  3222. ['option' => ['value' => 'other']],
  3223. 'bad',
  3224. '/option',
  3225. '/select',
  3226. '/div'
  3227. ];
  3228. $this->assertHtml($expected, $result);
  3229. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3230. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3231. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3232. $expected = [
  3233. 'div' => ['class' => 'input select'],
  3234. 'label' => ['for' => 'thing-user-id'],
  3235. 'User',
  3236. '/label',
  3237. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3238. ['option' => ['value' => '']],
  3239. 'Some Empty',
  3240. '/option',
  3241. ['option' => ['value' => 'value']],
  3242. 'good',
  3243. '/option',
  3244. ['option' => ['value' => 'other']],
  3245. 'bad',
  3246. '/option',
  3247. '/select',
  3248. '/div'
  3249. ];
  3250. $this->assertHtml($expected, $result);
  3251. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3252. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3253. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3254. $expected = [
  3255. 'div' => ['class' => 'input select'],
  3256. 'label' => ['for' => 'thing-user-id'],
  3257. 'User',
  3258. '/label',
  3259. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3260. ['option' => ['value' => '']],
  3261. 'Some Empty',
  3262. '/option',
  3263. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3264. 'good',
  3265. '/option',
  3266. ['option' => ['value' => 'other']],
  3267. 'bad',
  3268. '/option',
  3269. '/select',
  3270. '/div'
  3271. ];
  3272. $this->assertHtml($expected, $result);
  3273. $this->Form->data = [];
  3274. $result = $this->Form->input('Publisher.id', [
  3275. 'label' => 'Publisher',
  3276. 'type' => 'select',
  3277. 'multiple' => 'checkbox',
  3278. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3279. ]);
  3280. $expected = [
  3281. ['div' => ['class' => 'input select']],
  3282. ['label' => ['for' => 'publisher-id']],
  3283. 'Publisher',
  3284. '/label',
  3285. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3286. ['div' => ['class' => 'checkbox']],
  3287. ['label' => ['for' => 'publisher-id-value-1']],
  3288. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3289. 'Label 1',
  3290. '/label',
  3291. '/div',
  3292. ['div' => ['class' => 'checkbox']],
  3293. ['label' => ['for' => 'publisher-id-value-2']],
  3294. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3295. 'Label 2',
  3296. '/label',
  3297. '/div',
  3298. '/div'
  3299. ];
  3300. $this->assertHtml($expected, $result);
  3301. }
  3302. /**
  3303. * test that input() and a non standard primary key makes a hidden input by default.
  3304. *
  3305. * @return void
  3306. */
  3307. public function testInputWithNonStandardPrimaryKeyMakesHidden()
  3308. {
  3309. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3310. $this->Form->create($this->article);
  3311. $result = $this->Form->input('title');
  3312. $expected = [
  3313. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3314. ];
  3315. $this->assertHtml($expected, $result);
  3316. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3317. $this->Form->create($this->article);
  3318. $result = $this->Form->input('title');
  3319. $expected = [
  3320. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3321. ];
  3322. $this->assertHtml($expected, $result);
  3323. $result = $this->Form->input('body');
  3324. $expected = [
  3325. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3326. ];
  3327. $this->assertHtml($expected, $result);
  3328. }
  3329. /**
  3330. * test that overriding the magic select type widget is possible
  3331. *
  3332. * @return void
  3333. */
  3334. public function testInputOverridingMagicSelectType()
  3335. {
  3336. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3337. $result = $this->Form->input('Model.user_id', ['type' => 'text']);
  3338. $expected = [
  3339. 'div' => ['class' => 'input text'],
  3340. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3341. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3342. '/div'
  3343. ];
  3344. $this->assertHtml($expected, $result);
  3345. //Check that magic types still work for plural/singular vars
  3346. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3347. $result = $this->Form->input('Model.type');
  3348. $expected = [
  3349. 'div' => ['class' => 'input select'],
  3350. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3351. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3352. ['option' => ['value' => 'value']], 'good', '/option',
  3353. ['option' => ['value' => 'other']], 'bad', '/option',
  3354. '/select',
  3355. '/div'
  3356. ];
  3357. $this->assertHtml($expected, $result);
  3358. }
  3359. /**
  3360. * Test that inferred types do not override developer input
  3361. *
  3362. * @return void
  3363. */
  3364. public function testInputMagicTypeDoesNotOverride()
  3365. {
  3366. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3367. $result = $this->Form->input('Model.user', ['type' => 'checkbox']);
  3368. $expected = [
  3369. 'div' => ['class' => 'input checkbox'],
  3370. ['input' => [
  3371. 'type' => 'hidden',
  3372. 'name' => 'Model[user]',
  3373. 'value' => 0,
  3374. ]],
  3375. 'label' => ['for' => 'model-user'],
  3376. ['input' => [
  3377. 'name' => 'Model[user]',
  3378. 'type' => 'checkbox',
  3379. 'id' => 'model-user',
  3380. 'value' => 1
  3381. ]],
  3382. 'User',
  3383. '/label',
  3384. '/div'
  3385. ];
  3386. $this->assertHtml($expected, $result);
  3387. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3388. $options = [
  3389. 1 => 'blue',
  3390. 2 => 'red'
  3391. ];
  3392. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3393. $expected = [
  3394. 'div' => ['class' => 'input select'],
  3395. 'label' => ['for' => 'tags-ids'],
  3396. 'Tags',
  3397. '/label',
  3398. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3399. ['div' => ['class' => 'checkbox']],
  3400. ['label' => ['for' => 'tags-ids-1']],
  3401. ['input' => [
  3402. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3403. 'value' => '1', 'name' => 'tags[_ids][]'
  3404. ]],
  3405. 'blue',
  3406. '/label',
  3407. '/div',
  3408. ['div' => ['class' => 'checkbox']],
  3409. ['label' => ['for' => 'tags-ids-2']],
  3410. ['input' => [
  3411. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3412. 'value' => '2', 'name' => 'tags[_ids][]'
  3413. ]],
  3414. 'red',
  3415. '/label',
  3416. '/div',
  3417. '/div'
  3418. ];
  3419. $this->assertHtml($expected, $result);
  3420. }
  3421. /**
  3422. * Test that magic input() selects are created for type=number
  3423. *
  3424. * @return void
  3425. */
  3426. public function testInputMagicSelectForTypeNumber()
  3427. {
  3428. TableRegistry::get('ValidateUsers', [
  3429. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3430. ]);
  3431. $entity = new Entity(['balance' => 1]);
  3432. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3433. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3434. $result = $this->Form->input('balance');
  3435. $expected = [
  3436. 'div' => ['class' => 'input select'],
  3437. 'label' => ['for' => 'balance'],
  3438. 'Balance',
  3439. '/label',
  3440. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3441. ['option' => ['value' => '0']],
  3442. 'nothing',
  3443. '/option',
  3444. ['option' => ['value' => '1', 'selected' => 'selected']],
  3445. 'some',
  3446. '/option',
  3447. ['option' => ['value' => '100']],
  3448. 'a lot',
  3449. '/option',
  3450. '/select',
  3451. '/div'
  3452. ];
  3453. $this->assertHtml($expected, $result);
  3454. }
  3455. /**
  3456. * Test invalid 'input' type option to input() function.
  3457. *
  3458. * @expectedException \RuntimeException
  3459. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3460. * @return void
  3461. */
  3462. public function testInvalidInputTypeOption()
  3463. {
  3464. $this->Form->input('text', ['type' => 'input']);
  3465. }
  3466. /**
  3467. * Test that magic input() selects can easily be converted into radio types without error.
  3468. *
  3469. * @return void
  3470. */
  3471. public function testInputMagicSelectChangeToRadio()
  3472. {
  3473. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3474. $result = $this->Form->input('Model.user_id', ['type' => 'radio']);
  3475. $this->assertContains('input type="radio"', $result);
  3476. }
  3477. /**
  3478. * testFormInputSubmit method
  3479. *
  3480. * test correct results for form::input() and type submit.
  3481. *
  3482. * @return void
  3483. */
  3484. public function testFormInputSubmit()
  3485. {
  3486. $result = $this->Form->input('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3487. $expected = [
  3488. 'div' => ['class' => 'submit'],
  3489. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3490. '/div'
  3491. ];
  3492. $this->assertHtml($expected, $result);
  3493. }
  3494. /**
  3495. * testFormInputs method
  3496. *
  3497. * test correct results from form::inputs().
  3498. *
  3499. * @return void
  3500. */
  3501. public function testFormInputsLegendFieldset()
  3502. {
  3503. $this->Form->create($this->article);
  3504. $result = $this->Form->allInputs([], ['legend' => 'The Legend']);
  3505. $expected = [
  3506. '<fieldset',
  3507. '<legend',
  3508. 'The Legend',
  3509. '/legend',
  3510. '*/fieldset',
  3511. ];
  3512. $this->assertHtml($expected, $result);
  3513. $result = $this->Form->allInputs([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3514. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3515. $this->assertContains('<fieldset>', $result);
  3516. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => false]);
  3517. $this->assertNotContains('<legend>', $result);
  3518. $this->assertNotContains('<fieldset>', $result);
  3519. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => 'Hello']);
  3520. $this->assertNotContains('<legend>', $result);
  3521. $this->assertNotContains('<fieldset>', $result);
  3522. $this->Form->create($this->article);
  3523. $this->Form->request->params['prefix'] = 'admin';
  3524. $this->Form->request->params['action'] = 'admin_edit';
  3525. $this->Form->request->params['controller'] = 'articles';
  3526. $result = $this->Form->allInputs();
  3527. $expected = [
  3528. '<fieldset',
  3529. '<legend',
  3530. 'New Article',
  3531. '/legend',
  3532. '*/fieldset',
  3533. ];
  3534. $this->assertHtml($expected, $result);
  3535. $this->Form->create($this->article);
  3536. $result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
  3537. $expected = [
  3538. '<fieldset',
  3539. '<legend',
  3540. 'The Legend',
  3541. '/legend',
  3542. '*/fieldset',
  3543. ];
  3544. $this->assertHtml($expected, $result);
  3545. $this->Form->create($this->article);
  3546. $result = $this->Form->allInputs([], [
  3547. 'fieldset' => [
  3548. 'class' => 'some-class some-other-class',
  3549. 'disabled' => true,
  3550. 'data-param' => 'a-param'
  3551. ],
  3552. 'legend' => 'The Legend'
  3553. ]);
  3554. $expected = [
  3555. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3556. '<legend',
  3557. 'The Legend',
  3558. '/legend',
  3559. '*/fieldset',
  3560. ];
  3561. $this->assertHtml($expected, $result);
  3562. }
  3563. /**
  3564. * Test the inputs() method.
  3565. *
  3566. * @return void
  3567. */
  3568. public function testFormInputs()
  3569. {
  3570. $this->Form->create($this->article);
  3571. $result = $this->Form->allInputs();
  3572. $expected = [
  3573. '<fieldset',
  3574. '<legend', 'New Article', '/legend',
  3575. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3576. ['div' => ['class' => 'input select required']],
  3577. '*/div',
  3578. ['div' => ['class' => 'input text required']],
  3579. '*/div',
  3580. ['div' => ['class' => 'input text']],
  3581. '*/div',
  3582. ['div' => ['class' => 'input text']],
  3583. '*/div',
  3584. '/fieldset',
  3585. ];
  3586. $this->assertHtml($expected, $result);
  3587. $result = $this->Form->allInputs([
  3588. 'published' => ['type' => 'boolean']
  3589. ]);
  3590. $expected = [
  3591. '<fieldset',
  3592. '<legend', 'New Article', '/legend',
  3593. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3594. ['div' => ['class' => 'input select required']],
  3595. '*/div',
  3596. ['div' => ['class' => 'input text required']],
  3597. '*/div',
  3598. ['div' => ['class' => 'input text']],
  3599. '*/div',
  3600. ['div' => ['class' => 'input boolean']],
  3601. '*/div',
  3602. '/fieldset',
  3603. ];
  3604. $this->assertHtml($expected, $result);
  3605. $this->Form->create($this->article);
  3606. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  3607. $expected = [
  3608. 'fieldset' => [],
  3609. 'legend' => [],
  3610. 'Hello',
  3611. '/legend',
  3612. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3613. ['div' => ['class' => 'input select required']],
  3614. '*/div',
  3615. ['div' => ['class' => 'input text required']],
  3616. '*/div',
  3617. ['div' => ['class' => 'input text']],
  3618. '*/div',
  3619. ['div' => ['class' => 'input text']],
  3620. '*/div',
  3621. '/fieldset'
  3622. ];
  3623. $this->assertHtml($expected, $result);
  3624. $this->Form->create(false);
  3625. $expected = [
  3626. 'fieldset' => [],
  3627. ['div' => ['class' => 'input text']],
  3628. 'label' => ['for' => 'foo'],
  3629. 'Foo',
  3630. '/label',
  3631. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3632. '*/div',
  3633. '/fieldset'
  3634. ];
  3635. $result = $this->Form->allInputs(
  3636. ['foo' => ['type' => 'text']],
  3637. ['legend' => false]
  3638. );
  3639. $this->assertHtml($expected, $result);
  3640. }
  3641. /**
  3642. * testFormInputsBlacklist
  3643. *
  3644. * @return void
  3645. */
  3646. public function testFormInputsBlacklist()
  3647. {
  3648. $this->Form->create($this->article);
  3649. $result = $this->Form->allInputs([
  3650. 'id' => false
  3651. ]);
  3652. $expected = [
  3653. '<fieldset',
  3654. '<legend', 'New Article', '/legend',
  3655. ['div' => ['class' => 'input select required']],
  3656. '*/div',
  3657. ['div' => ['class' => 'input text required']],
  3658. '*/div',
  3659. ['div' => ['class' => 'input text']],
  3660. '*/div',
  3661. ['div' => ['class' => 'input text']],
  3662. '*/div',
  3663. '/fieldset',
  3664. ];
  3665. $this->assertHtml($expected, $result);
  3666. $this->Form->create($this->article);
  3667. $result = $this->Form->allInputs([
  3668. 'id' => []
  3669. ]);
  3670. $expected = [
  3671. '<fieldset',
  3672. '<legend', 'New Article', '/legend',
  3673. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3674. ['div' => ['class' => 'input select required']],
  3675. '*/div',
  3676. ['div' => ['class' => 'input text required']],
  3677. '*/div',
  3678. ['div' => ['class' => 'input text']],
  3679. '*/div',
  3680. ['div' => ['class' => 'input text']],
  3681. '*/div',
  3682. '/fieldset',
  3683. ];
  3684. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3685. }
  3686. /**
  3687. * testSelectAsCheckbox method
  3688. *
  3689. * test multi-select widget with checkbox formatting.
  3690. *
  3691. * @return void
  3692. */
  3693. public function testSelectAsCheckbox()
  3694. {
  3695. $result = $this->Form->select(
  3696. 'Model.multi_field',
  3697. ['first', 'second', 'third'],
  3698. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3699. );
  3700. $expected = [
  3701. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3702. ['div' => ['class' => 'checkbox']],
  3703. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3704. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3705. 'first',
  3706. '/label',
  3707. '/div',
  3708. ['div' => ['class' => 'checkbox']],
  3709. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3710. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3711. 'second',
  3712. '/label',
  3713. '/div',
  3714. ['div' => ['class' => 'checkbox']],
  3715. ['label' => ['for' => 'model-multi-field-2']],
  3716. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3717. 'third',
  3718. '/label',
  3719. '/div',
  3720. ];
  3721. $this->assertHtml($expected, $result);
  3722. $result = $this->Form->select(
  3723. 'Model.multi_field',
  3724. ['1/2' => 'half'],
  3725. ['multiple' => 'checkbox']
  3726. );
  3727. $expected = [
  3728. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3729. ['div' => ['class' => 'checkbox']],
  3730. ['label' => ['for' => 'model-multi-field-1-2']],
  3731. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3732. 'half',
  3733. '/label',
  3734. '/div',
  3735. ];
  3736. $this->assertHtml($expected, $result);
  3737. }
  3738. /**
  3739. * testLabel method
  3740. *
  3741. * test label generation.
  3742. *
  3743. * @return void
  3744. */
  3745. public function testLabel()
  3746. {
  3747. $result = $this->Form->label('Person.name');
  3748. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3749. $this->assertHtml($expected, $result);
  3750. $result = $this->Form->label('Person.name');
  3751. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3752. $this->assertHtml($expected, $result);
  3753. $result = $this->Form->label('Person.first_name');
  3754. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3755. $this->assertHtml($expected, $result);
  3756. $result = $this->Form->label('Person.first_name', 'Your first name');
  3757. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3758. $this->assertHtml($expected, $result);
  3759. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3760. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3761. $this->assertHtml($expected, $result);
  3762. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3763. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3764. $this->assertHtml($expected, $result);
  3765. $result = $this->Form->label('Person.first_name', '');
  3766. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3767. $this->assertHtml($expected, $result);
  3768. $result = $this->Form->label('Person.2.name', '');
  3769. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3770. $this->assertHtml($expected, $result);
  3771. }
  3772. /**
  3773. * Test that label() can accept an input with the correct template vars.
  3774. *
  3775. * @return void
  3776. */
  3777. public function testLabelContainInput()
  3778. {
  3779. $this->Form->templates([
  3780. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3781. ]);
  3782. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3783. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3784. ]);
  3785. $expected = [
  3786. 'label' => ['for' => 'person-accept-terms'],
  3787. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3788. 'Accept',
  3789. '/label',
  3790. ];
  3791. $this->assertHtml($expected, $result);
  3792. }
  3793. /**
  3794. * testTextbox method
  3795. *
  3796. * test textbox element generation
  3797. *
  3798. * @return void
  3799. */
  3800. public function testTextbox()
  3801. {
  3802. $result = $this->Form->text('Model.field');
  3803. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3804. $this->assertHtml($expected, $result);
  3805. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3806. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3807. $this->assertHtml($expected, $result);
  3808. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3809. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3810. $this->assertHtml($expected, $result);
  3811. }
  3812. /**
  3813. * Test that text() hooks up with request data and error fields.
  3814. *
  3815. * @return void
  3816. */
  3817. public function testTextBoxDataAndError()
  3818. {
  3819. $this->article['errors'] = [
  3820. 'Contact' => ['text' => 'wrong']
  3821. ];
  3822. $this->Form->create($this->article);
  3823. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3824. $result = $this->Form->text('Model.text');
  3825. $expected = [
  3826. 'input' => [
  3827. 'type' => 'text',
  3828. 'name' => 'Model[text]',
  3829. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3830. ]
  3831. ];
  3832. $this->assertHtml($expected, $result);
  3833. $this->Form->request->data['Contact']['text'] = 'test';
  3834. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3835. $expected = [
  3836. 'input' => [
  3837. 'type' => 'text',
  3838. 'name' => 'Contact[text]',
  3839. 'value' => 'test',
  3840. 'id' => 'theID',
  3841. 'class' => 'form-error'
  3842. ]
  3843. ];
  3844. $this->assertHtml($expected, $result);
  3845. }
  3846. /**
  3847. * testDefaultValue method
  3848. *
  3849. * Test default value setting
  3850. *
  3851. * @return void
  3852. */
  3853. public function testTextDefaultValue()
  3854. {
  3855. $this->Form->request->data['Model']['field'] = 'test';
  3856. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3857. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3858. $this->assertHtml($expected, $result);
  3859. unset($this->Form->request->data['Model']['field']);
  3860. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3861. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3862. $this->assertHtml($expected, $result);
  3863. $Articles = TableRegistry::get('Articles');
  3864. $title = $Articles->schema()->column('title');
  3865. $Articles->schema()->addColumn(
  3866. 'title',
  3867. ['default' => 'default title'] + $title
  3868. );
  3869. $entity = $Articles->newEntity();
  3870. $this->Form->create($entity);
  3871. // Get default value from schema
  3872. $result = $this->Form->text('title');
  3873. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3874. $this->assertHtml($expected, $result);
  3875. // Don't get value from schema
  3876. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3877. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3878. $this->assertHtml($expected, $result);
  3879. // Custom default value overrides default value from schema
  3880. $result = $this->Form->text('title', ['default' => 'override default']);
  3881. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3882. $this->assertHtml($expected, $result);
  3883. // Default value from schema is used only for new entities.
  3884. $entity->isNew(false);
  3885. $result = $this->Form->text('title');
  3886. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3887. $this->assertHtml($expected, $result);
  3888. }
  3889. /**
  3890. * testError method
  3891. *
  3892. * Test field error generation
  3893. *
  3894. * @return void
  3895. */
  3896. public function testError()
  3897. {
  3898. $this->article['errors'] = [
  3899. 'Article' => ['field' => 'email']
  3900. ];
  3901. $this->Form->create($this->article);
  3902. $result = $this->Form->error('Article.field');
  3903. $expected = [
  3904. ['div' => ['class' => 'error-message']],
  3905. 'email',
  3906. '/div',
  3907. ];
  3908. $this->assertHtml($expected, $result);
  3909. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3910. $expected = [
  3911. ['div' => ['class' => 'error-message']],
  3912. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3913. '/div',
  3914. ];
  3915. $this->assertHtml($expected, $result);
  3916. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3917. $expected = [
  3918. ['div' => ['class' => 'error-message']],
  3919. '<strong', 'Badness!', '/strong',
  3920. '/div',
  3921. ];
  3922. $this->assertHtml($expected, $result);
  3923. }
  3924. /**
  3925. * test error translation can use rule names for translating.
  3926. *
  3927. * @return void
  3928. */
  3929. public function testErrorRuleName()
  3930. {
  3931. $this->article['errors'] = [
  3932. 'Article' => [
  3933. 'field' => ['email' => 'Your email was not good']
  3934. ]
  3935. ];
  3936. $this->Form->create($this->article);
  3937. $result = $this->Form->error('Article.field');
  3938. $expected = [
  3939. ['div' => ['class' => 'error-message']],
  3940. 'Your email was not good',
  3941. '/div',
  3942. ];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3945. $expected = [
  3946. ['div' => ['class' => 'error-message']],
  3947. 'Email in use',
  3948. '/div',
  3949. ];
  3950. $this->assertHtml($expected, $result);
  3951. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3952. $expected = [
  3953. ['div' => ['class' => 'error-message']],
  3954. 'Email in use',
  3955. '/div',
  3956. ];
  3957. $this->assertHtml($expected, $result);
  3958. $result = $this->Form->error('Article.field', [
  3959. 'email' => 'Key is preferred',
  3960. 'Your email was not good' => 'Email in use'
  3961. ]);
  3962. $expected = [
  3963. ['div' => ['class' => 'error-message']],
  3964. 'Key is preferred',
  3965. '/div',
  3966. ];
  3967. $this->assertHtml($expected, $result);
  3968. }
  3969. /**
  3970. * Test error with nested lists.
  3971. *
  3972. * @return void
  3973. */
  3974. public function testErrorMessages()
  3975. {
  3976. $this->article['errors'] = [
  3977. 'Article' => ['field' => 'email']
  3978. ];
  3979. $this->Form->create($this->article);
  3980. $result = $this->Form->error('Article.field', [
  3981. 'email' => 'No good!'
  3982. ]);
  3983. $expected = [
  3984. 'div' => ['class' => 'error-message'],
  3985. 'No good!',
  3986. '/div'
  3987. ];
  3988. $this->assertHtml($expected, $result);
  3989. }
  3990. /**
  3991. * test error() with multiple messages.
  3992. *
  3993. * @return void
  3994. */
  3995. public function testErrorMultipleMessages()
  3996. {
  3997. $this->article['errors'] = [
  3998. 'field' => ['notBlank', 'email', 'Something else']
  3999. ];
  4000. $this->Form->create($this->article);
  4001. $result = $this->Form->error('field', [
  4002. 'notBlank' => 'Cannot be empty',
  4003. 'email' => 'No good!'
  4004. ]);
  4005. $expected = [
  4006. 'div' => ['class' => 'error-message'],
  4007. 'ul' => [],
  4008. '<li', 'Cannot be empty', '/li',
  4009. '<li', 'No good!', '/li',
  4010. '<li', 'Something else', '/li',
  4011. '/ul',
  4012. '/div'
  4013. ];
  4014. $this->assertHtml($expected, $result);
  4015. }
  4016. /**
  4017. * testPassword method
  4018. *
  4019. * Test password element generation
  4020. *
  4021. * @return void
  4022. */
  4023. public function testPassword()
  4024. {
  4025. $this->article['errors'] = [
  4026. 'Contact' => [
  4027. 'passwd' => 1
  4028. ]
  4029. ];
  4030. $this->Form->create($this->article);
  4031. $result = $this->Form->password('Contact.field');
  4032. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4033. $this->assertHtml($expected, $result);
  4034. $this->Form->request->data['Contact']['passwd'] = 'test';
  4035. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4036. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4037. $this->assertHtml($expected, $result);
  4038. }
  4039. /**
  4040. * testRadio method
  4041. *
  4042. * Test radio element set generation
  4043. *
  4044. * @return void
  4045. */
  4046. public function testRadio()
  4047. {
  4048. $result = $this->Form->radio('Model.field', ['option A']);
  4049. $expected = [
  4050. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4051. 'label' => ['for' => 'model-field-0'],
  4052. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4053. 'option A',
  4054. '/label'
  4055. ];
  4056. $this->assertHtml($expected, $result);
  4057. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4058. $this->assertHtml($expected, $result);
  4059. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4060. $expected = [
  4061. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4062. ['label' => ['for' => 'model-field-0']],
  4063. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4064. 'option A',
  4065. '/label',
  4066. ['label' => ['for' => 'model-field-1']],
  4067. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4068. 'option B',
  4069. '/label',
  4070. ];
  4071. $this->assertHtml($expected, $result);
  4072. $result = $this->Form->radio(
  4073. 'Employee.gender',
  4074. ['male' => 'Male', 'female' => 'Female'],
  4075. ['form' => 'my-form']
  4076. );
  4077. $expected = [
  4078. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4079. ['label' => ['for' => 'employee-gender-male']],
  4080. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4081. 'Male',
  4082. '/label',
  4083. ['label' => ['for' => 'employee-gender-female']],
  4084. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4085. 'Female',
  4086. '/label',
  4087. ];
  4088. $this->assertHtml($expected, $result);
  4089. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4090. $expected = [
  4091. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4092. ['label' => ['for' => 'model-custom-0']],
  4093. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4094. 'option A',
  4095. '/label',
  4096. ['label' => ['for' => 'model-custom-1']],
  4097. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4098. 'option B',
  4099. '/label',
  4100. ];
  4101. $this->assertHtml($expected, $result);
  4102. $result = $this->Form->radio(
  4103. 'Employee.gender',
  4104. [
  4105. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4106. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4107. ]
  4108. );
  4109. $expected = [
  4110. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4111. ['label' => ['for' => 'employee-gender-male']],
  4112. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4113. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4114. 'Male',
  4115. '/label',
  4116. ['label' => ['for' => 'employee-gender-female']],
  4117. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4118. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4119. 'Female',
  4120. '/label',
  4121. ];
  4122. $this->assertHtml($expected, $result);
  4123. }
  4124. /**
  4125. * Test default value setting on radio() method
  4126. *
  4127. * @return void
  4128. */
  4129. public function testRadioDefaultValue()
  4130. {
  4131. $Articles = TableRegistry::get('Articles');
  4132. $title = $Articles->schema()->column('title');
  4133. $Articles->schema()->addColumn(
  4134. 'title',
  4135. ['default' => '1'] + $title
  4136. );
  4137. $this->Form->create($Articles->newEntity());
  4138. $result = $this->Form->radio('title', ['option A', 'option B']);
  4139. $expected = [
  4140. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4141. ['label' => ['for' => 'title-0']],
  4142. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4143. 'option A',
  4144. '/label',
  4145. ['label' => ['for' => 'title-1']],
  4146. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4147. 'option B',
  4148. '/label',
  4149. ];
  4150. $this->assertHtml($expected, $result);
  4151. }
  4152. /**
  4153. * Test that input works with radio types
  4154. *
  4155. * @return void
  4156. */
  4157. public function testInputRadio()
  4158. {
  4159. $result = $this->Form->input('test', [
  4160. 'type' => 'radio',
  4161. 'options' => ['A', 'B'],
  4162. ]);
  4163. $expected = [
  4164. ['div' => ['class' => 'input radio']],
  4165. '<label',
  4166. 'Test',
  4167. '/label',
  4168. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4169. ['label' => ['for' => 'test-0']],
  4170. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4171. 'A',
  4172. '/label',
  4173. ['label' => ['for' => 'test-1']],
  4174. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4175. 'B',
  4176. '/label',
  4177. '/div',
  4178. ];
  4179. $this->assertHtml($expected, $result);
  4180. $result = $this->Form->input('test', [
  4181. 'type' => 'radio',
  4182. 'options' => ['A', 'B'],
  4183. 'value' => '0'
  4184. ]);
  4185. $expected = [
  4186. ['div' => ['class' => 'input radio']],
  4187. '<label',
  4188. 'Test',
  4189. '/label',
  4190. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4191. ['label' => ['for' => 'test-0']],
  4192. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4193. 'A',
  4194. '/label',
  4195. ['label' => ['for' => 'test-1']],
  4196. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4197. 'B',
  4198. '/label',
  4199. '/div',
  4200. ];
  4201. $this->assertHtml($expected, $result);
  4202. $result = $this->Form->input('test', [
  4203. 'type' => 'radio',
  4204. 'options' => ['A', 'B'],
  4205. 'label' => false
  4206. ]);
  4207. $expected = [
  4208. ['div' => ['class' => 'input radio']],
  4209. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4210. ['label' => ['for' => 'test-0']],
  4211. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4212. 'A',
  4213. '/label',
  4214. ['label' => ['for' => 'test-1']],
  4215. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4216. 'B',
  4217. '/label',
  4218. '/div',
  4219. ];
  4220. $this->assertHtml($expected, $result);
  4221. }
  4222. /**
  4223. * Test that radio() works with label = false
  4224. *
  4225. * @return void
  4226. */
  4227. public function testRadioNoLabel()
  4228. {
  4229. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4230. $expected = [
  4231. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4232. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4233. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4234. ];
  4235. $this->assertHtml($expected, $result);
  4236. }
  4237. /**
  4238. * test generating radio input inside label ala twitter bootstrap
  4239. *
  4240. * @return void
  4241. */
  4242. public function testRadioInputInsideLabel()
  4243. {
  4244. $this->Form->templates([
  4245. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4246. 'radioWrapper' => '{{label}}'
  4247. ]);
  4248. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4249. //@codingStandardsIgnoreStart
  4250. $expected = [
  4251. ['input' => [
  4252. 'type' => 'hidden',
  4253. 'name' => 'Model[field]',
  4254. 'value' => ''
  4255. ]],
  4256. ['label' => ['for' => 'model-field-0']],
  4257. ['input' => [
  4258. 'type' => 'radio',
  4259. 'name' => 'Model[field]',
  4260. 'value' => '0',
  4261. 'id' => 'model-field-0'
  4262. ]],
  4263. 'option A',
  4264. '/label',
  4265. ['label' => ['for' => 'model-field-1']],
  4266. ['input' => [
  4267. 'type' => 'radio',
  4268. 'name' => 'Model[field]',
  4269. 'value' => '1',
  4270. 'id' => 'model-field-1'
  4271. ]],
  4272. 'option B',
  4273. '/label',
  4274. ];
  4275. //@codingStandardsIgnoreEnd
  4276. $this->assertHtml($expected, $result);
  4277. }
  4278. /**
  4279. * test disabling the hidden input for radio buttons
  4280. *
  4281. * @return void
  4282. */
  4283. public function testRadioHiddenInputDisabling()
  4284. {
  4285. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4286. $expected = [
  4287. 'label' => ['for' => 'model-1-field-0'],
  4288. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4289. 'option A',
  4290. '/label'
  4291. ];
  4292. $this->assertHtml($expected, $result);
  4293. }
  4294. /**
  4295. * testRadio method
  4296. *
  4297. * Test radio element set generation
  4298. *
  4299. * @return void
  4300. */
  4301. public function testRadioOutOfRange()
  4302. {
  4303. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4304. $expected = [
  4305. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4306. 'label' => ['for' => 'model-field-v'],
  4307. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4308. 'value',
  4309. '/label'
  4310. ];
  4311. $this->assertHtml($expected, $result);
  4312. }
  4313. /**
  4314. * testSelect method
  4315. *
  4316. * Test select element generation.
  4317. *
  4318. * @return void
  4319. */
  4320. public function testSelect()
  4321. {
  4322. $result = $this->Form->select('Model.field', []);
  4323. $expected = [
  4324. 'select' => ['name' => 'Model[field]'],
  4325. '/select'
  4326. ];
  4327. $this->assertHtml($expected, $result);
  4328. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4329. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4330. $expected = [
  4331. 'select' => ['name' => 'Model[field]'],
  4332. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4333. 'good',
  4334. '/option',
  4335. ['option' => ['value' => 'other']],
  4336. 'bad',
  4337. '/option',
  4338. '/select'
  4339. ];
  4340. $this->assertHtml($expected, $result);
  4341. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4342. $this->assertHtml($expected, $result);
  4343. $this->Form->request->data = [];
  4344. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4345. $expected = [
  4346. 'select' => ['name' => 'Model[field]'],
  4347. ['option' => ['value' => 'value']],
  4348. 'good',
  4349. '/option',
  4350. ['option' => ['value' => 'other']],
  4351. 'bad',
  4352. '/option',
  4353. '/select'
  4354. ];
  4355. $this->assertHtml($expected, $result);
  4356. $options = [
  4357. ['value' => 'first', 'text' => 'First'],
  4358. ['value' => 'first', 'text' => 'Another First'],
  4359. ];
  4360. $result = $this->Form->select(
  4361. 'Model.field',
  4362. $options,
  4363. ['escape' => false, 'empty' => false]
  4364. );
  4365. $expected = [
  4366. 'select' => ['name' => 'Model[field]'],
  4367. ['option' => ['value' => 'first']],
  4368. 'First',
  4369. '/option',
  4370. ['option' => ['value' => 'first']],
  4371. 'Another First',
  4372. '/option',
  4373. '/select'
  4374. ];
  4375. $this->assertHtml($expected, $result);
  4376. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4377. $result = $this->Form->select(
  4378. 'Model.contact_id',
  4379. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4380. ['escape' => false, 'empty' => 'pick something']
  4381. );
  4382. $expected = [
  4383. 'select' => ['name' => 'Model[contact_id]'],
  4384. ['option' => ['value' => '']], 'pick something', '/option',
  4385. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4386. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4387. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4388. '/select'
  4389. ];
  4390. $this->assertHtml($expected, $result);
  4391. $this->Form->request->data['Model']['field'] = 0;
  4392. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4393. $expected = [
  4394. 'select' => ['name' => 'Model[field]'],
  4395. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4396. ['option' => ['value' => '1']], 'Yes', '/option',
  4397. '/select'
  4398. ];
  4399. $this->assertHtml($expected, $result);
  4400. }
  4401. /**
  4402. * Test that select() escapes HTML.
  4403. *
  4404. * @return void
  4405. */
  4406. public function testSelectEscapeHtml()
  4407. {
  4408. $result = $this->Form->select(
  4409. 'Model.field',
  4410. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4411. ['empty' => false]
  4412. );
  4413. $expected = [
  4414. 'select' => ['name' => 'Model[field]'],
  4415. ['option' => ['value' => 'first']],
  4416. 'first &quot;html&quot; &lt;chars&gt;',
  4417. '/option',
  4418. ['option' => ['value' => 'second']],
  4419. 'value',
  4420. '/option',
  4421. '/select'
  4422. ];
  4423. $this->assertHtml($expected, $result);
  4424. $result = $this->Form->select(
  4425. 'Model.field',
  4426. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4427. ['escape' => false, 'empty' => false]
  4428. );
  4429. $expected = [
  4430. 'select' => ['name' => 'Model[field]'],
  4431. ['option' => ['value' => 'first']],
  4432. 'first "html" <chars>',
  4433. '/option',
  4434. ['option' => ['value' => 'second']],
  4435. 'value',
  4436. '/option',
  4437. '/select'
  4438. ];
  4439. $this->assertHtml($expected, $result);
  4440. }
  4441. /**
  4442. * test select() with required and disabled attributes.
  4443. *
  4444. * @return void
  4445. */
  4446. public function testSelectRequired()
  4447. {
  4448. $this->article['required'] = [
  4449. 'user_id' => true
  4450. ];
  4451. $this->Form->create($this->article);
  4452. $result = $this->Form->select('user_id', ['option A']);
  4453. $expected = [
  4454. 'select' => [
  4455. 'name' => 'user_id',
  4456. 'required' => 'required'
  4457. ],
  4458. ['option' => ['value' => '0']], 'option A', '/option',
  4459. '/select'
  4460. ];
  4461. $this->assertHtml($expected, $result);
  4462. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4463. $expected = [
  4464. 'select' => [
  4465. 'name' => 'user_id',
  4466. 'disabled' => 'disabled'
  4467. ],
  4468. ['option' => ['value' => '0']], 'option A', '/option',
  4469. '/select'
  4470. ];
  4471. $this->assertHtml($expected, $result);
  4472. }
  4473. /**
  4474. * testNestedSelect method
  4475. *
  4476. * test select element generation with optgroups
  4477. *
  4478. * @return void
  4479. */
  4480. public function testNestedSelect()
  4481. {
  4482. $result = $this->Form->select(
  4483. 'Model.field',
  4484. [1 => 'One', 2 => 'Two', 'Three' => [
  4485. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4486. ]],
  4487. ['empty' => false]
  4488. );
  4489. $expected = [
  4490. 'select' => ['name' => 'Model[field]'],
  4491. ['option' => ['value' => 1]],
  4492. 'One',
  4493. '/option',
  4494. ['option' => ['value' => 2]],
  4495. 'Two',
  4496. '/option',
  4497. ['optgroup' => ['label' => 'Three']],
  4498. ['option' => ['value' => 3]],
  4499. 'Three',
  4500. '/option',
  4501. ['option' => ['value' => 4]],
  4502. 'Four',
  4503. '/option',
  4504. ['option' => ['value' => 5]],
  4505. 'Five',
  4506. '/option',
  4507. '/optgroup',
  4508. '/select'
  4509. ];
  4510. $this->assertHtml($expected, $result);
  4511. }
  4512. /**
  4513. * testSelectMultiple method
  4514. *
  4515. * test generation of multiple select elements
  4516. *
  4517. * @return void
  4518. */
  4519. public function testSelectMultiple()
  4520. {
  4521. $options = ['first', 'second', 'third'];
  4522. $result = $this->Form->select(
  4523. 'Model.multi_field',
  4524. $options,
  4525. ['form' => 'my-form', 'multiple' => true]
  4526. );
  4527. $expected = [
  4528. 'input' => [
  4529. 'type' => 'hidden',
  4530. 'name' => 'Model[multi_field]',
  4531. 'value' => '',
  4532. 'form' => 'my-form',
  4533. ],
  4534. 'select' => [
  4535. 'name' => 'Model[multi_field][]',
  4536. 'multiple' => 'multiple',
  4537. 'form' => 'my-form',
  4538. ],
  4539. ['option' => ['value' => '0']],
  4540. 'first',
  4541. '/option',
  4542. ['option' => ['value' => '1']],
  4543. 'second',
  4544. '/option',
  4545. ['option' => ['value' => '2']],
  4546. 'third',
  4547. '/option',
  4548. '/select'
  4549. ];
  4550. $this->assertHtml($expected, $result);
  4551. $result = $this->Form->select(
  4552. 'Model.multi_field',
  4553. $options,
  4554. ['multiple' => 'multiple', 'form' => 'my-form']
  4555. );
  4556. $this->assertHtml($expected, $result);
  4557. }
  4558. /**
  4559. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4560. *
  4561. * @return void
  4562. */
  4563. public function testCheckboxZeroValue()
  4564. {
  4565. $result = $this->Form->input('User.get_spam', [
  4566. 'type' => 'checkbox',
  4567. 'value' => '0',
  4568. 'hiddenField' => '1',
  4569. ]);
  4570. $expected = [
  4571. 'div' => ['class' => 'input checkbox'],
  4572. 'label' => ['for' => 'user-get-spam'],
  4573. ['input' => [
  4574. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4575. 'value' => '1'
  4576. ]],
  4577. ['input' => [
  4578. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4579. 'value' => '0', 'id' => 'user-get-spam'
  4580. ]],
  4581. 'Get Spam',
  4582. '/label',
  4583. '/div'
  4584. ];
  4585. $this->assertHtml($expected, $result);
  4586. }
  4587. /**
  4588. * test generation of habtm select boxes.
  4589. *
  4590. * @return void
  4591. */
  4592. public function testHabtmSelectBox()
  4593. {
  4594. $this->loadFixtures('Articles');
  4595. $options = [
  4596. 1 => 'blue',
  4597. 2 => 'red',
  4598. 3 => 'green'
  4599. ];
  4600. $tags = [
  4601. new Entity(['id' => 1, 'name' => 'blue']),
  4602. new Entity(['id' => 3, 'name' => 'green'])
  4603. ];
  4604. $article = new Article(['tags' => $tags]);
  4605. $this->Form->create($article);
  4606. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4607. $expected = [
  4608. 'div' => ['class' => 'input select'],
  4609. 'label' => ['for' => 'tags-ids'],
  4610. 'Tags',
  4611. '/label',
  4612. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4613. 'select' => [
  4614. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4615. 'multiple' => 'multiple'
  4616. ],
  4617. ['option' => ['value' => '1', 'selected' => 'selected']],
  4618. 'blue',
  4619. '/option',
  4620. ['option' => ['value' => '2']],
  4621. 'red',
  4622. '/option',
  4623. ['option' => ['value' => '3', 'selected' => 'selected']],
  4624. 'green',
  4625. '/option',
  4626. '/select',
  4627. '/div'
  4628. ];
  4629. $this->assertHtml($expected, $result);
  4630. // make sure only 50 is selected, and not 50f5c0cf
  4631. $options = [
  4632. '1' => 'blue',
  4633. '50f5c0cf' => 'red',
  4634. '50' => 'green'
  4635. ];
  4636. $tags = [
  4637. new Entity(['id' => 1, 'name' => 'blue']),
  4638. new Entity(['id' => 50, 'name' => 'green'])
  4639. ];
  4640. $article = new Article(['tags' => $tags]);
  4641. $this->Form->create($article);
  4642. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4643. $expected = [
  4644. 'div' => ['class' => 'input select'],
  4645. 'label' => ['for' => 'tags-ids'],
  4646. 'Tags',
  4647. '/label',
  4648. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4649. 'select' => [
  4650. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4651. 'multiple' => 'multiple'
  4652. ],
  4653. ['option' => ['value' => '1', 'selected' => 'selected']],
  4654. 'blue',
  4655. '/option',
  4656. ['option' => ['value' => '50f5c0cf']],
  4657. 'red',
  4658. '/option',
  4659. ['option' => ['value' => '50', 'selected' => 'selected']],
  4660. 'green',
  4661. '/option',
  4662. '/select',
  4663. '/div'
  4664. ];
  4665. $this->assertHtml($expected, $result);
  4666. $spacecraft = [
  4667. 1 => 'Orion',
  4668. 2 => 'Helios'
  4669. ];
  4670. $this->View->viewVars['spacecraft'] = $spacecraft;
  4671. $this->Form->create();
  4672. $result = $this->Form->input('spacecraft._ids');
  4673. $expected = [
  4674. 'div' => ['class' => 'input select'],
  4675. 'label' => ['for' => 'spacecraft-ids'],
  4676. 'Spacecraft',
  4677. '/label',
  4678. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4679. 'select' => [
  4680. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4681. 'multiple' => 'multiple'
  4682. ],
  4683. ['option' => ['value' => '1']],
  4684. 'Orion',
  4685. '/option',
  4686. ['option' => ['value' => '2']],
  4687. 'Helios',
  4688. '/option',
  4689. '/select',
  4690. '/div'
  4691. ];
  4692. $this->assertHtml($expected, $result);
  4693. }
  4694. /**
  4695. * Tests that errors for belongsToMany select fields are being
  4696. * picked up properly.
  4697. *
  4698. * @return void
  4699. */
  4700. public function testErrorsForBelongsToManySelect()
  4701. {
  4702. $spacecraft = [
  4703. 1 => 'Orion',
  4704. 2 => 'Helios'
  4705. ];
  4706. $this->View->viewVars['spacecraft'] = $spacecraft;
  4707. $article = new Article();
  4708. $article->errors('spacecraft', ['Invalid']);
  4709. $this->Form->create($article);
  4710. $result = $this->Form->input('spacecraft._ids');
  4711. $expected = [
  4712. ['div' => ['class' => 'input select error']],
  4713. 'label' => ['for' => 'spacecraft-ids'],
  4714. 'Spacecraft',
  4715. '/label',
  4716. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4717. 'select' => [
  4718. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4719. 'multiple' => 'multiple'
  4720. ],
  4721. ['option' => ['value' => '1']],
  4722. 'Orion',
  4723. '/option',
  4724. ['option' => ['value' => '2']],
  4725. 'Helios',
  4726. '/option',
  4727. '/select',
  4728. ['div' => ['class' => 'error-message']],
  4729. 'Invalid',
  4730. '/div',
  4731. '/div'
  4732. ];
  4733. $this->assertHtml($expected, $result);
  4734. }
  4735. /**
  4736. * test generation of multi select elements in checkbox format
  4737. *
  4738. * @return void
  4739. */
  4740. public function testSelectMultipleCheckboxes()
  4741. {
  4742. $result = $this->Form->select(
  4743. 'Model.multi_field',
  4744. ['first', 'second', 'third'],
  4745. ['multiple' => 'checkbox']
  4746. );
  4747. $expected = [
  4748. 'input' => [
  4749. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4750. ],
  4751. ['div' => ['class' => 'checkbox']],
  4752. ['label' => ['for' => 'model-multi-field-0']],
  4753. ['input' => [
  4754. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4755. 'value' => '0', 'id' => 'model-multi-field-0'
  4756. ]],
  4757. 'first',
  4758. '/label',
  4759. '/div',
  4760. ['div' => ['class' => 'checkbox']],
  4761. ['label' => ['for' => 'model-multi-field-1']],
  4762. ['input' => [
  4763. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4764. 'value' => '1', 'id' => 'model-multi-field-1'
  4765. ]],
  4766. 'second',
  4767. '/label',
  4768. '/div',
  4769. ['div' => ['class' => 'checkbox']],
  4770. ['label' => ['for' => 'model-multi-field-2']],
  4771. ['input' => [
  4772. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4773. 'value' => '2', 'id' => 'model-multi-field-2'
  4774. ]],
  4775. 'third',
  4776. '/label',
  4777. '/div'
  4778. ];
  4779. $this->assertHtml($expected, $result);
  4780. $result = $this->Form->select(
  4781. 'Model.multi_field',
  4782. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4783. ['multiple' => 'checkbox']
  4784. );
  4785. $expected = [
  4786. 'input' => [
  4787. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4788. ],
  4789. ['div' => ['class' => 'checkbox']],
  4790. ['label' => ['for' => 'model-multi-field-a+']],
  4791. ['input' => [
  4792. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4793. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4794. ]],
  4795. 'first',
  4796. '/label',
  4797. '/div',
  4798. ['div' => ['class' => 'checkbox']],
  4799. ['label' => ['for' => 'model-multi-field-a++']],
  4800. ['input' => [
  4801. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4802. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4803. ]],
  4804. 'second',
  4805. '/label',
  4806. '/div',
  4807. ['div' => ['class' => 'checkbox']],
  4808. ['label' => ['for' => 'model-multi-field-a+++']],
  4809. ['input' => [
  4810. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4811. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4812. ]],
  4813. 'third',
  4814. '/label',
  4815. '/div'
  4816. ];
  4817. $this->assertHtml($expected, $result);
  4818. $result = $this->Form->select(
  4819. 'Model.multi_field',
  4820. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4821. ['multiple' => 'checkbox']
  4822. );
  4823. $expected = [
  4824. 'input' => [
  4825. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4826. ],
  4827. ['div' => ['class' => 'checkbox']],
  4828. ['label' => ['for' => 'model-multi-field-a-b']],
  4829. ['input' => [
  4830. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4831. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4832. ]],
  4833. 'first',
  4834. '/label',
  4835. '/div',
  4836. ['div' => ['class' => 'checkbox']],
  4837. ['label' => ['for' => 'model-multi-field-a-b1']],
  4838. ['input' => [
  4839. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4840. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4841. ]],
  4842. 'second',
  4843. '/label',
  4844. '/div',
  4845. ['div' => ['class' => 'checkbox']],
  4846. ['label' => ['for' => 'model-multi-field-a-b2']],
  4847. ['input' => [
  4848. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4849. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4850. ]],
  4851. 'third',
  4852. '/label',
  4853. '/div'
  4854. ];
  4855. $this->assertHtml($expected, $result);
  4856. }
  4857. /**
  4858. * Ensure that multiCheckbox reads from the request data.
  4859. *
  4860. * @return void
  4861. */
  4862. public function testSelectMultipleCheckboxRequestData()
  4863. {
  4864. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4865. $result = $this->Form->select(
  4866. 'Model.tags',
  4867. ['1' => 'first', 'Array' => 'Array'],
  4868. ['multiple' => 'checkbox']
  4869. );
  4870. $expected = [
  4871. 'input' => [
  4872. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4873. ],
  4874. ['div' => ['class' => 'checkbox']],
  4875. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4876. ['input' => [
  4877. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4878. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4879. ]],
  4880. 'first',
  4881. '/label',
  4882. '/div',
  4883. ['div' => ['class' => 'checkbox']],
  4884. ['label' => ['for' => 'model-tags-array']],
  4885. ['input' => [
  4886. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4887. 'value' => 'Array', 'id' => 'model-tags-array'
  4888. ]],
  4889. 'Array',
  4890. '/label',
  4891. '/div'
  4892. ];
  4893. $this->assertHtml($expected, $result);
  4894. }
  4895. /**
  4896. * Checks the security hash array generated for multiple-input checkbox elements
  4897. *
  4898. * @return void
  4899. */
  4900. public function testSelectMultipleCheckboxSecurity()
  4901. {
  4902. $this->Form->request->params['_Token'] = 'testKey';
  4903. $this->assertEquals([], $this->Form->fields);
  4904. $result = $this->Form->select(
  4905. 'Model.multi_field',
  4906. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4907. ['multiple' => 'checkbox']
  4908. );
  4909. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  4910. $result = $this->Form->secure($this->Form->fields);
  4911. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4912. $this->assertRegExp('/"' . $key . '"/', $result);
  4913. }
  4914. /**
  4915. * Multiple select elements should always be secured as they always participate
  4916. * in the POST data.
  4917. *
  4918. * @return void
  4919. */
  4920. public function testSelectMultipleSecureWithNoOptions()
  4921. {
  4922. $this->assertEquals([], $this->Form->fields);
  4923. $this->Form->select(
  4924. 'Model.select',
  4925. [],
  4926. ['multiple' => true]
  4927. );
  4928. $this->assertEquals(['Model.select'], $this->Form->fields);
  4929. }
  4930. /**
  4931. * When a select box has no options it should not be added to the fields list
  4932. * as it always fail post validation.
  4933. *
  4934. * @return void
  4935. */
  4936. public function testSelectNoSecureWithNoOptions()
  4937. {
  4938. $this->Form->request->params['_Token'] = 'testkey';
  4939. $this->assertEquals([], $this->Form->fields);
  4940. $this->Form->select(
  4941. 'Model.select',
  4942. []
  4943. );
  4944. $this->assertEquals([], $this->Form->fields);
  4945. $this->Form->select(
  4946. 'Model.user_id',
  4947. [],
  4948. ['empty' => true]
  4949. );
  4950. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  4951. }
  4952. /**
  4953. * testInputMultipleCheckboxes method
  4954. *
  4955. * test input() resulting in multi select elements being generated.
  4956. *
  4957. * @return void
  4958. */
  4959. public function testInputMultipleCheckboxes()
  4960. {
  4961. $result = $this->Form->input('Model.multi_field', [
  4962. 'options' => ['first', 'second', 'third'],
  4963. 'multiple' => 'checkbox'
  4964. ]);
  4965. $expected = [
  4966. ['div' => ['class' => 'input select']],
  4967. ['label' => ['for' => 'model-multi-field']],
  4968. 'Multi Field',
  4969. '/label',
  4970. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4971. ['div' => ['class' => 'checkbox']],
  4972. ['label' => ['for' => 'model-multi-field-0']],
  4973. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  4974. 'first',
  4975. '/label',
  4976. '/div',
  4977. ['div' => ['class' => 'checkbox']],
  4978. ['label' => ['for' => 'model-multi-field-1']],
  4979. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  4980. 'second',
  4981. '/label',
  4982. '/div',
  4983. ['div' => ['class' => 'checkbox']],
  4984. ['label' => ['for' => 'model-multi-field-2']],
  4985. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  4986. 'third',
  4987. '/label',
  4988. '/div',
  4989. '/div'
  4990. ];
  4991. $this->assertHtml($expected, $result);
  4992. $result = $this->Form->input('Model.multi_field', [
  4993. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  4994. 'multiple' => 'checkbox'
  4995. ]);
  4996. $expected = [
  4997. ['div' => ['class' => 'input select']],
  4998. ['label' => ['for' => 'model-multi-field']],
  4999. 'Multi Field',
  5000. '/label',
  5001. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5002. ['div' => ['class' => 'checkbox']],
  5003. ['label' => ['for' => 'model-multi-field-a']],
  5004. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5005. 'first',
  5006. '/label',
  5007. '/div',
  5008. ['div' => ['class' => 'checkbox']],
  5009. ['label' => ['for' => 'model-multi-field-b']],
  5010. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5011. 'second',
  5012. '/label',
  5013. '/div',
  5014. ['div' => ['class' => 'checkbox']],
  5015. ['label' => ['for' => 'model-multi-field-c']],
  5016. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5017. 'third',
  5018. '/label',
  5019. '/div',
  5020. '/div'
  5021. ];
  5022. $this->assertHtml($expected, $result);
  5023. }
  5024. /**
  5025. * testSelectHiddenFieldOmission method
  5026. *
  5027. * test that select() with 'hiddenField' => false omits the hidden field
  5028. *
  5029. * @return void
  5030. */
  5031. public function testSelectHiddenFieldOmission()
  5032. {
  5033. $result = $this->Form->select(
  5034. 'Model.multi_field',
  5035. ['first', 'second'],
  5036. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5037. );
  5038. $this->assertNotContains('type="hidden"', $result);
  5039. }
  5040. /**
  5041. * test that select() with multiple = checkbox works with overriding name attribute.
  5042. *
  5043. * @return void
  5044. */
  5045. public function testSelectCheckboxMultipleOverrideName()
  5046. {
  5047. $result = $this->Form->select('category', ['1', '2'], [
  5048. 'multiple' => 'checkbox',
  5049. 'name' => 'fish',
  5050. ]);
  5051. $expected = [
  5052. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5053. ['div' => ['class' => 'checkbox']],
  5054. ['label' => ['for' => 'fish-0']],
  5055. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5056. '1',
  5057. '/label',
  5058. '/div',
  5059. ['div' => ['class' => 'checkbox']],
  5060. ['label' => ['for' => 'fish-1']],
  5061. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5062. '2',
  5063. '/label',
  5064. '/div'
  5065. ];
  5066. $this->assertHtml($expected, $result);
  5067. $result = $this->Form->multiCheckbox(
  5068. 'category',
  5069. new Collection(['1', '2']),
  5070. ['name' => 'fish']
  5071. );
  5072. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5073. 'name' => 'fish',
  5074. ]);
  5075. $this->assertHtml($expected, $result);
  5076. }
  5077. /**
  5078. * Test that input() works with multicheckbox.
  5079. *
  5080. * @return void
  5081. */
  5082. public function testInputMultiCheckbox()
  5083. {
  5084. $result = $this->Form->input('category', [
  5085. 'type' => 'multicheckbox',
  5086. 'options' => ['1', '2'],
  5087. ]);
  5088. $expected = [
  5089. ['div' => ['class' => 'input multicheckbox']],
  5090. '<label',
  5091. 'Category',
  5092. '/label',
  5093. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5094. ['div' => ['class' => 'checkbox']],
  5095. ['label' => ['for' => 'category-0']],
  5096. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5097. '1',
  5098. '/label',
  5099. '/div',
  5100. ['div' => ['class' => 'checkbox']],
  5101. ['label' => ['for' => 'category-1']],
  5102. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5103. '2',
  5104. '/label',
  5105. '/div',
  5106. '/div',
  5107. ];
  5108. $this->assertHtml($expected, $result);
  5109. }
  5110. /**
  5111. * testCheckbox method
  5112. *
  5113. * Test generation of checkboxes
  5114. *
  5115. * @return void
  5116. */
  5117. public function testCheckbox()
  5118. {
  5119. $result = $this->Form->checkbox('Model.field');
  5120. $expected = [
  5121. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5122. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5123. ];
  5124. $this->assertHtml($expected, $result);
  5125. $result = $this->Form->checkbox('Model.field', [
  5126. 'id' => 'theID',
  5127. 'value' => 'myvalue',
  5128. 'form' => 'my-form',
  5129. ]);
  5130. $expected = [
  5131. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5132. ['input' => [
  5133. 'type' => 'checkbox', 'name' => 'Model[field]',
  5134. 'value' => 'myvalue', 'id' => 'theID',
  5135. 'form' => 'my-form',
  5136. ]]
  5137. ];
  5138. $this->assertHtml($expected, $result);
  5139. }
  5140. /**
  5141. * testCheckboxDefaultValue method
  5142. *
  5143. * Test default value setting on checkbox() method
  5144. *
  5145. * @return void
  5146. */
  5147. public function testCheckboxDefaultValue()
  5148. {
  5149. $this->Form->request->data['Model']['field'] = false;
  5150. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5151. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5152. $this->assertHtml($expected, $result);
  5153. unset($this->Form->request->data['Model']['field']);
  5154. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5155. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5156. $this->assertHtml($expected, $result);
  5157. $this->Form->request->data['Model']['field'] = true;
  5158. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5159. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5160. $this->assertHtml($expected, $result);
  5161. unset($this->Form->request->data['Model']['field']);
  5162. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5163. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5164. $this->assertHtml($expected, $result);
  5165. $Articles = TableRegistry::get('Articles');
  5166. $Articles->schema()->addColumn(
  5167. 'published',
  5168. ['type' => 'boolean', 'null' => false, 'default' => true]
  5169. );
  5170. $this->Form->create($Articles->newEntity());
  5171. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5172. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5173. $this->assertHtml($expected, $result);
  5174. }
  5175. /**
  5176. * Test checkbox being checked or having errors.
  5177. *
  5178. * @return void
  5179. */
  5180. public function testCheckboxCheckedAndError()
  5181. {
  5182. $this->article['errors'] = [
  5183. 'published' => true
  5184. ];
  5185. $this->Form->request->data['published'] = 'myvalue';
  5186. $this->Form->create($this->article);
  5187. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5188. $expected = [
  5189. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5190. ['input' => [
  5191. 'type' => 'checkbox',
  5192. 'name' => 'published',
  5193. 'value' => 'myvalue',
  5194. 'id' => 'theID',
  5195. 'checked' => 'checked',
  5196. 'class' => 'form-error'
  5197. ]]
  5198. ];
  5199. $this->assertHtml($expected, $result);
  5200. $this->Form->request->data['published'] = '';
  5201. $result = $this->Form->checkbox('published');
  5202. $expected = [
  5203. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5204. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5205. ];
  5206. $this->assertHtml($expected, $result);
  5207. }
  5208. /**
  5209. * test checkbox() with a custom name attribute
  5210. *
  5211. * @return void
  5212. */
  5213. public function testCheckboxCustomNameAttribute()
  5214. {
  5215. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5216. $expected = [
  5217. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5218. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5219. ];
  5220. $this->assertHtml($expected, $result);
  5221. }
  5222. /**
  5223. * Test that the hidden input for checkboxes can be omitted or set to a
  5224. * specific value.
  5225. *
  5226. * @return void
  5227. */
  5228. public function testCheckboxHiddenField()
  5229. {
  5230. $result = $this->Form->checkbox('UserForm.something', [
  5231. 'hiddenField' => false
  5232. ]);
  5233. $expected = [
  5234. 'input' => [
  5235. 'type' => 'checkbox',
  5236. 'name' => 'UserForm[something]',
  5237. 'value' => '1'
  5238. ],
  5239. ];
  5240. $this->assertHtml($expected, $result);
  5241. $result = $this->Form->checkbox('UserForm.something', [
  5242. 'value' => 'Y',
  5243. 'hiddenField' => 'N',
  5244. ]);
  5245. $expected = [
  5246. ['input' => [
  5247. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5248. 'value' => 'N'
  5249. ]],
  5250. ['input' => [
  5251. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5252. 'value' => 'Y'
  5253. ]],
  5254. ];
  5255. $this->assertHtml($expected, $result);
  5256. }
  5257. /**
  5258. * Test the time type.
  5259. *
  5260. * @return void
  5261. */
  5262. public function testTime()
  5263. {
  5264. $result = $this->Form->time('start_time', [
  5265. 'timeFormat' => 12,
  5266. 'interval' => 5,
  5267. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5268. ]);
  5269. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5270. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5271. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5272. $this->assertNotContains('year', $result);
  5273. $this->assertNotContains('month', $result);
  5274. $this->assertNotContains('day', $result);
  5275. $result = $this->Form->time('start_time', [
  5276. 'timeFormat' => 12,
  5277. 'interval' => 5,
  5278. 'value' => '2014-03-08 16:30:00'
  5279. ]);
  5280. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5281. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5282. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5283. $this->assertNotContains('year', $result);
  5284. $this->assertNotContains('month', $result);
  5285. $this->assertNotContains('day', $result);
  5286. }
  5287. /**
  5288. * Ensure that timeFormat=24 has no merdian.
  5289. *
  5290. * @return void.
  5291. */
  5292. public function testTimeFormat24NoMeridian()
  5293. {
  5294. $result = $this->Form->time('start_time', [
  5295. 'timeFormat' => 24,
  5296. 'interval' => 5,
  5297. 'value' => '2014-03-08 16:30:00'
  5298. ]);
  5299. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5300. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5301. $this->assertNotContains('meridian', $result);
  5302. $this->assertNotContains('pm', $result);
  5303. $this->assertNotContains('year', $result);
  5304. $this->assertNotContains('month', $result);
  5305. $this->assertNotContains('day', $result);
  5306. }
  5307. /**
  5308. * Test the date type.
  5309. *
  5310. * @return void
  5311. */
  5312. public function testDate()
  5313. {
  5314. $result = $this->Form->date('start_day', [
  5315. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5316. ]);
  5317. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5318. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5319. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5320. $this->assertNotContains('hour', $result);
  5321. $this->assertNotContains('minute', $result);
  5322. $this->assertNotContains('second', $result);
  5323. $this->assertNotContains('meridian', $result);
  5324. }
  5325. /**
  5326. * testDateTime method
  5327. *
  5328. * Test generation of date/time select elements
  5329. *
  5330. * @return void
  5331. */
  5332. public function testDateTime()
  5333. {
  5334. extract($this->dateRegex);
  5335. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5336. $now = strtotime('now');
  5337. $expected = [
  5338. ['select' => ['name' => 'Contact[date][year]']],
  5339. ['option' => ['value' => '']],
  5340. '/option',
  5341. $yearsRegex,
  5342. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5343. date('Y', $now),
  5344. '/option',
  5345. '*/select',
  5346. ['select' => ['name' => 'Contact[date][month]']],
  5347. ['option' => ['value' => '']],
  5348. '/option',
  5349. $monthsRegex,
  5350. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5351. date('F', $now),
  5352. '/option',
  5353. '*/select',
  5354. ['select' => ['name' => 'Contact[date][day]']],
  5355. ['option' => ['value' => '']],
  5356. '/option',
  5357. $daysRegex,
  5358. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5359. date('j', $now),
  5360. '/option',
  5361. '*/select',
  5362. ['select' => ['name' => 'Contact[date][hour]']],
  5363. ['option' => ['value' => '']],
  5364. '/option',
  5365. $hoursRegex,
  5366. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5367. date('G', $now),
  5368. '/option',
  5369. '*/select',
  5370. ['select' => ['name' => 'Contact[date][minute]']],
  5371. ['option' => ['value' => '']],
  5372. '/option',
  5373. $minutesRegex,
  5374. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5375. date('i', $now),
  5376. '/option',
  5377. '*/select',
  5378. ];
  5379. $this->assertHtml($expected, $result);
  5380. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5381. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5382. $now = strtotime('now');
  5383. $expected = [
  5384. ['select' => ['name' => 'Contact[date][year]']],
  5385. $yearsRegex,
  5386. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5387. date('Y', $now),
  5388. '/option',
  5389. '*/select',
  5390. ['select' => ['name' => 'Contact[date][month]']],
  5391. $monthsRegex,
  5392. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5393. date('F', $now),
  5394. '/option',
  5395. '*/select',
  5396. ['select' => ['name' => 'Contact[date][day]']],
  5397. $daysRegex,
  5398. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5399. date('j', $now),
  5400. '/option',
  5401. '*/select',
  5402. ['select' => ['name' => 'Contact[date][hour]']],
  5403. $hoursRegex,
  5404. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5405. date('G', $now),
  5406. '/option',
  5407. '*/select',
  5408. ['select' => ['name' => 'Contact[date][minute]']],
  5409. $minutesRegex,
  5410. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5411. date('i', $now),
  5412. '/option',
  5413. '*/select',
  5414. ];
  5415. $this->assertHtml($expected, $result);
  5416. }
  5417. /**
  5418. * Test that datetime fields are added to protected fields list.
  5419. *
  5420. * @return void
  5421. */
  5422. public function testDateTimeSecured()
  5423. {
  5424. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5425. $this->Form->dateTime('Contact.date');
  5426. $expected = [
  5427. 'Contact.date.year',
  5428. 'Contact.date.month',
  5429. 'Contact.date.day',
  5430. 'Contact.date.hour',
  5431. 'Contact.date.minute',
  5432. ];
  5433. $this->assertEquals($expected, $this->Form->fields);
  5434. $this->Form->fields = [];
  5435. $this->Form->date('Contact.published');
  5436. $expected = [
  5437. 'Contact.published.year',
  5438. 'Contact.published.month',
  5439. 'Contact.published.day',
  5440. ];
  5441. $this->assertEquals($expected, $this->Form->fields);
  5442. }
  5443. /**
  5444. * Test that datetime fields are added to protected fields list.
  5445. *
  5446. * @return void
  5447. */
  5448. public function testDateTimeSecuredDisabled()
  5449. {
  5450. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5451. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5452. $expected = [];
  5453. $this->assertEquals($expected, $this->Form->fields);
  5454. $this->Form->fields = [];
  5455. $this->Form->date('Contact.published', ['secure' => false]);
  5456. $expected = [];
  5457. $this->assertEquals($expected, $this->Form->fields);
  5458. }
  5459. /**
  5460. * Test empty defaulting to true for datetime.
  5461. *
  5462. * @return void
  5463. */
  5464. public function testDatetimeEmpty()
  5465. {
  5466. extract($this->dateRegex);
  5467. $now = strtotime('now');
  5468. $result = $this->Form->dateTime('Contact.date', [
  5469. 'timeFormat' => 12,
  5470. 'empty' => true,
  5471. 'default' => true
  5472. ]);
  5473. $expected = [
  5474. ['select' => ['name' => 'Contact[date][year]']],
  5475. $yearsRegex,
  5476. ['option' => ['value' => '']],
  5477. '/option',
  5478. '*/select',
  5479. ['select' => ['name' => 'Contact[date][month]']],
  5480. $monthsRegex,
  5481. ['option' => ['value' => '']],
  5482. '/option',
  5483. '*/select',
  5484. ['select' => ['name' => 'Contact[date][day]']],
  5485. $daysRegex,
  5486. ['option' => ['value' => '']],
  5487. '/option',
  5488. '*/select',
  5489. ['select' => ['name' => 'Contact[date][hour]']],
  5490. $hoursRegex,
  5491. ['option' => ['value' => '']],
  5492. '/option',
  5493. '*/select',
  5494. ['select' => ['name' => 'Contact[date][minute]']],
  5495. $minutesRegex,
  5496. ['option' => ['value' => '']],
  5497. '/option',
  5498. '*/select',
  5499. ['select' => ['name' => 'Contact[date][meridian]']],
  5500. $meridianRegex,
  5501. ['option' => ['value' => '']],
  5502. '/option',
  5503. '*/select'
  5504. ];
  5505. $this->assertHtml($expected, $result);
  5506. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5507. }
  5508. /**
  5509. * Test datetime with interval option.
  5510. *
  5511. * @return void
  5512. */
  5513. public function testDatetimeMinuteInterval()
  5514. {
  5515. extract($this->dateRegex);
  5516. $now = strtotime('now');
  5517. $result = $this->Form->dateTime('Contact.date', [
  5518. 'interval' => 5,
  5519. 'value' => ''
  5520. ]);
  5521. $expected = [
  5522. ['select' => ['name' => 'Contact[date][year]']],
  5523. $yearsRegex,
  5524. ['option' => ['selected' => 'selected', 'value' => '']],
  5525. '/option',
  5526. '*/select',
  5527. ['select' => ['name' => 'Contact[date][month]']],
  5528. $monthsRegex,
  5529. ['option' => ['selected' => 'selected', 'value' => '']],
  5530. '/option',
  5531. '*/select',
  5532. ['select' => ['name' => 'Contact[date][day]']],
  5533. $daysRegex,
  5534. ['option' => ['selected' => 'selected', 'value' => '']],
  5535. '/option',
  5536. '*/select',
  5537. ['select' => ['name' => 'Contact[date][hour]']],
  5538. $hoursRegex,
  5539. ['option' => ['selected' => 'selected', 'value' => '']],
  5540. '/option',
  5541. '*/select',
  5542. ['select' => ['name' => 'Contact[date][minute]']],
  5543. $minutesRegex,
  5544. ['option' => ['selected' => 'selected', 'value' => '']],
  5545. '/option',
  5546. ['option' => ['value' => '00']],
  5547. '00',
  5548. '/option',
  5549. ['option' => ['value' => '05']],
  5550. '05',
  5551. '/option',
  5552. ['option' => ['value' => '10']],
  5553. '10',
  5554. '/option',
  5555. '*/select',
  5556. ];
  5557. $this->assertHtml($expected, $result);
  5558. }
  5559. /**
  5560. * Test dateTime with rounding
  5561. *
  5562. * @return void
  5563. */
  5564. public function testDateTimeRounding()
  5565. {
  5566. $this->Form->request->data['Contact'] = [
  5567. 'date' => [
  5568. 'day' => '13',
  5569. 'month' => '12',
  5570. 'year' => '2010',
  5571. 'hour' => '04',
  5572. 'minute' => '19',
  5573. 'meridian' => 'AM'
  5574. ]
  5575. ];
  5576. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5577. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5578. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5579. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5580. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5581. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5582. }
  5583. /**
  5584. * test that datetime() and default values work.
  5585. *
  5586. * @return void
  5587. */
  5588. public function testDatetimeWithDefault()
  5589. {
  5590. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5591. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5592. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5593. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5594. $result = $this->Form->dateTime('Contact.updated', [
  5595. 'default' => '2009-06-01 11:15:30'
  5596. ]);
  5597. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5598. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5599. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5600. }
  5601. /**
  5602. * testDateTime all zeros
  5603. *
  5604. * @return void
  5605. */
  5606. public function testDateTimeAllZeros()
  5607. {
  5608. $result = $this->Form->dateTime('Contact.date', [
  5609. 'timeFormat' => false,
  5610. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5611. 'value' => '0000-00-00'
  5612. ]);
  5613. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5614. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5615. }
  5616. /**
  5617. * testDateTimeEmptyAsArray
  5618. *
  5619. * @return void
  5620. */
  5621. public function testDateTimeEmptyAsArray()
  5622. {
  5623. $result = $this->Form->dateTime('Contact.date', [
  5624. 'empty' => [
  5625. 'day' => 'DAY',
  5626. 'month' => 'MONTH',
  5627. 'year' => 'YEAR',
  5628. 'hour' => 'HOUR',
  5629. 'minute' => 'MINUTE',
  5630. 'meridian' => false
  5631. ],
  5632. 'default' => true
  5633. ]);
  5634. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5635. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5636. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5637. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5638. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5639. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5640. $result = $this->Form->dateTime('Contact.date', [
  5641. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5642. 'default' => true
  5643. ]);
  5644. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5645. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5646. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5647. }
  5648. /**
  5649. * testFormDateTimeMulti method
  5650. *
  5651. * test multiple datetime element generation
  5652. *
  5653. * @return void
  5654. */
  5655. public function testFormDateTimeMulti()
  5656. {
  5657. extract($this->dateRegex);
  5658. $result = $this->Form->dateTime('Contact.1.updated');
  5659. $this->assertContains('Contact[1][updated][month]', $result);
  5660. $this->assertContains('Contact[1][updated][day]', $result);
  5661. $this->assertContains('Contact[1][updated][year]', $result);
  5662. $this->assertContains('Contact[1][updated][hour]', $result);
  5663. $this->assertContains('Contact[1][updated][minute]', $result);
  5664. }
  5665. /**
  5666. * When changing the date format, the label should always focus the first select box when
  5667. * clicked.
  5668. *
  5669. * @return void
  5670. */
  5671. public function testDateTimeLabelIdMatchesFirstInput()
  5672. {
  5673. $result = $this->Form->input('Model.date', ['type' => 'date']);
  5674. $this->assertContains('<label>Date</label>', $result);
  5675. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5676. $this->assertContains('<label>Date</label>', $result);
  5677. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5678. $this->assertContains('<label>Date</label>', $result);
  5679. }
  5680. /**
  5681. * test datetime second=true
  5682. *
  5683. * @return void
  5684. */
  5685. public function testDateTimeSecondOptions()
  5686. {
  5687. $result = $this->Form->dateTime('updated', ['second' => true]);
  5688. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5689. $result = $this->Form->dateTime('updated', ['second' => []]);
  5690. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5691. $result = $this->Form->dateTime('updated', ['second' => null]);
  5692. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5693. $result = $this->Form->dateTime('updated', ['second' => false]);
  5694. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5695. }
  5696. /**
  5697. * testMonth method
  5698. *
  5699. * @return void
  5700. */
  5701. public function testMonth()
  5702. {
  5703. $result = $this->Form->month('Model.field', ['value' => '']);
  5704. $expected = [
  5705. ['select' => ['name' => 'Model[field][month]']],
  5706. ['option' => ['value' => '', 'selected' => 'selected']],
  5707. '/option',
  5708. ['option' => ['value' => '01']],
  5709. date('F', strtotime('2008-01-01 00:00:00')),
  5710. '/option',
  5711. ['option' => ['value' => '02']],
  5712. date('F', strtotime('2008-02-01 00:00:00')),
  5713. '/option',
  5714. '*/select',
  5715. ];
  5716. $this->assertHtml($expected, $result);
  5717. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5718. $expected = [
  5719. ['select' => ['name' => 'Model[field][month]']],
  5720. ['option' => ['selected' => 'selected', 'value' => '']],
  5721. '/option',
  5722. ['option' => ['value' => '01']],
  5723. date('F', strtotime('2008-01-01 00:00:00')),
  5724. '/option',
  5725. ['option' => ['value' => '02']],
  5726. date('F', strtotime('2008-02-01 00:00:00')),
  5727. '/option',
  5728. '*/select',
  5729. ];
  5730. $this->assertHtml($expected, $result);
  5731. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5732. $expected = [
  5733. ['select' => ['name' => 'Model[field][month]']],
  5734. ['option' => ['selected' => 'selected', 'value' => '']],
  5735. '/option',
  5736. ['option' => ['value' => '01']],
  5737. '1',
  5738. '/option',
  5739. ['option' => ['value' => '02']],
  5740. '2',
  5741. '/option',
  5742. '*/select',
  5743. ];
  5744. $this->assertHtml($expected, $result);
  5745. $monthNames = [
  5746. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5747. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5748. ];
  5749. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5750. $expected = [
  5751. ['select' => ['name' => 'Model[field][month]']],
  5752. ['option' => ['value' => '']],
  5753. '/option',
  5754. ['option' => ['value' => '01', 'selected' => 'selected']],
  5755. 'Jan',
  5756. '/option',
  5757. ['option' => ['value' => '02']],
  5758. 'Feb',
  5759. '/option',
  5760. '*/select',
  5761. ];
  5762. $this->assertHtml($expected, $result);
  5763. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5764. $result = $this->Form->month('Project.release');
  5765. $expected = [
  5766. ['select' => ['name' => 'Project[release][month]']],
  5767. ['option' => ['value' => '']],
  5768. '/option',
  5769. ['option' => ['value' => '01']],
  5770. 'January',
  5771. '/option',
  5772. ['option' => ['value' => '02', 'selected' => 'selected']],
  5773. 'February',
  5774. '/option',
  5775. '*/select',
  5776. ];
  5777. $this->assertHtml($expected, $result);
  5778. $result = $this->Form->month('Contact.published', [
  5779. 'empty' => 'Published on',
  5780. ]);
  5781. $this->assertContains('Published on', $result);
  5782. }
  5783. /**
  5784. * testDay method
  5785. *
  5786. * @return void
  5787. */
  5788. public function testDay()
  5789. {
  5790. extract($this->dateRegex);
  5791. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5792. $expected = [
  5793. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5794. ['option' => ['selected' => 'selected', 'value' => '']],
  5795. '/option',
  5796. ['option' => ['value' => '01']],
  5797. '1',
  5798. '/option',
  5799. ['option' => ['value' => '02']],
  5800. '2',
  5801. '/option',
  5802. $daysRegex,
  5803. '/select',
  5804. ];
  5805. $this->assertHtml($expected, $result);
  5806. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5807. $result = $this->Form->day('Model.field');
  5808. $expected = [
  5809. ['select' => ['name' => 'Model[field][day]']],
  5810. ['option' => ['value' => '']],
  5811. '/option',
  5812. ['option' => ['value' => '01']],
  5813. '1',
  5814. '/option',
  5815. ['option' => ['value' => '02']],
  5816. '2',
  5817. '/option',
  5818. $daysRegex,
  5819. ['option' => ['value' => '10', 'selected' => 'selected']],
  5820. '10',
  5821. '/option',
  5822. $daysRegex,
  5823. '/select',
  5824. ];
  5825. $this->assertHtml($expected, $result);
  5826. $this->Form->request->data['Model']['field'] = '';
  5827. $result = $this->Form->day('Model.field', ['value' => '10']);
  5828. $expected = [
  5829. ['select' => ['name' => 'Model[field][day]']],
  5830. ['option' => ['value' => '']],
  5831. '/option',
  5832. ['option' => ['value' => '01']],
  5833. '1',
  5834. '/option',
  5835. ['option' => ['value' => '02']],
  5836. '2',
  5837. '/option',
  5838. $daysRegex,
  5839. ['option' => ['value' => '10', 'selected' => 'selected']],
  5840. '10',
  5841. '/option',
  5842. $daysRegex,
  5843. '/select',
  5844. ];
  5845. $this->assertHtml($expected, $result);
  5846. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5847. $result = $this->Form->day('Project.release');
  5848. $expected = [
  5849. ['select' => ['name' => 'Project[release][day]']],
  5850. ['option' => ['value' => '']],
  5851. '/option',
  5852. ['option' => ['value' => '01']],
  5853. '1',
  5854. '/option',
  5855. ['option' => ['value' => '02']],
  5856. '2',
  5857. '/option',
  5858. $daysRegex,
  5859. ['option' => ['value' => '10', 'selected' => 'selected']],
  5860. '10',
  5861. '/option',
  5862. $daysRegex,
  5863. '/select',
  5864. ];
  5865. $this->assertHtml($expected, $result);
  5866. $result = $this->Form->day('Contact.published', [
  5867. 'empty' => 'Published on',
  5868. ]);
  5869. $this->assertContains('Published on', $result);
  5870. }
  5871. /**
  5872. * testMinute method
  5873. *
  5874. * @return void
  5875. */
  5876. public function testMinute()
  5877. {
  5878. extract($this->dateRegex);
  5879. $result = $this->Form->minute('Model.field', ['value' => '']);
  5880. $expected = [
  5881. ['select' => ['name' => 'Model[field][minute]']],
  5882. ['option' => ['selected' => 'selected', 'value' => '']],
  5883. '/option',
  5884. ['option' => ['value' => '00']],
  5885. '00',
  5886. '/option',
  5887. ['option' => ['value' => '01']],
  5888. '01',
  5889. '/option',
  5890. ['option' => ['value' => '02']],
  5891. '02',
  5892. '/option',
  5893. $minutesRegex,
  5894. '/select',
  5895. ];
  5896. $this->assertHtml($expected, $result);
  5897. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5898. $result = $this->Form->minute('Model.field');
  5899. $expected = [
  5900. ['select' => ['name' => 'Model[field][minute]']],
  5901. ['option' => ['value' => '']],
  5902. '/option',
  5903. ['option' => ['value' => '00']],
  5904. '00',
  5905. '/option',
  5906. ['option' => ['value' => '01']],
  5907. '01',
  5908. '/option',
  5909. ['option' => ['value' => '02']],
  5910. '02',
  5911. '/option',
  5912. $minutesRegex,
  5913. ['option' => ['value' => '12', 'selected' => 'selected']],
  5914. '12',
  5915. '/option',
  5916. $minutesRegex,
  5917. '/select',
  5918. ];
  5919. $this->assertHtml($expected, $result);
  5920. $this->Form->request->data['Model']['field'] = '';
  5921. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5922. $expected = [
  5923. ['select' => ['name' => 'Model[field][minute]']],
  5924. ['option' => ['selected' => 'selected', 'value' => '']],
  5925. '/option',
  5926. ['option' => ['value' => '00']],
  5927. '00',
  5928. '/option',
  5929. ['option' => ['value' => '05']],
  5930. '05',
  5931. '/option',
  5932. ['option' => ['value' => '10']],
  5933. '10',
  5934. '/option',
  5935. $minutesRegex,
  5936. '/select',
  5937. ];
  5938. $this->assertHtml($expected, $result);
  5939. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5940. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5941. $expected = [
  5942. ['select' => ['name' => 'Model[field][minute]']],
  5943. ['option' => ['value' => '']],
  5944. '/option',
  5945. ['option' => ['value' => '00']],
  5946. '00',
  5947. '/option',
  5948. ['option' => ['value' => '05']],
  5949. '05',
  5950. '/option',
  5951. ['option' => ['value' => '10', 'selected' => 'selected']],
  5952. '10',
  5953. '/option',
  5954. $minutesRegex,
  5955. '/select',
  5956. ];
  5957. $this->assertHtml($expected, $result);
  5958. }
  5959. /**
  5960. * Test generating an input for the meridian.
  5961. *
  5962. * @return void
  5963. */
  5964. public function testMeridian()
  5965. {
  5966. extract($this->dateRegex);
  5967. $now = new \DateTime();
  5968. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5969. $expected = [
  5970. ['select' => ['name' => 'Model[field][meridian]']],
  5971. ['option' => ['value' => '']],
  5972. '/option',
  5973. $meridianRegex,
  5974. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  5975. $now->format('a'),
  5976. '/option',
  5977. '*/select'
  5978. ];
  5979. $this->assertHtml($expected, $result);
  5980. }
  5981. /**
  5982. * testHour method
  5983. *
  5984. * @return void
  5985. */
  5986. public function testHour()
  5987. {
  5988. extract($this->dateRegex);
  5989. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  5990. $expected = [
  5991. ['select' => ['name' => 'Model[field][hour]']],
  5992. ['option' => ['selected' => 'selected', 'value' => '']],
  5993. '/option',
  5994. ['option' => ['value' => '01']],
  5995. '1',
  5996. '/option',
  5997. ['option' => ['value' => '02']],
  5998. '2',
  5999. '/option',
  6000. $hoursRegex,
  6001. '/select',
  6002. ];
  6003. $this->assertHtml($expected, $result);
  6004. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6005. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6006. $expected = [
  6007. ['select' => ['name' => 'Model[field][hour]']],
  6008. ['option' => ['value' => '']],
  6009. '/option',
  6010. ['option' => ['value' => '01']],
  6011. '1',
  6012. '/option',
  6013. ['option' => ['value' => '02']],
  6014. '2',
  6015. '/option',
  6016. $hoursRegex,
  6017. ['option' => ['value' => '12', 'selected' => 'selected']],
  6018. '12',
  6019. '/option',
  6020. '/select',
  6021. ];
  6022. $this->assertHtml($expected, $result);
  6023. $this->Form->request->data['Model']['field'] = '';
  6024. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6025. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6026. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6027. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6028. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6029. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6030. $expected = [
  6031. ['select' => ['name' => 'Model[field][hour]']],
  6032. ['option' => ['value' => '']],
  6033. '/option',
  6034. ['option' => ['value' => '00', 'selected' => 'selected']],
  6035. '0',
  6036. '/option',
  6037. ['option' => ['value' => '01']],
  6038. '1',
  6039. '/option',
  6040. ['option' => ['value' => '02']],
  6041. '2',
  6042. '/option',
  6043. $hoursRegex,
  6044. '/select',
  6045. ];
  6046. $this->assertHtml($expected, $result);
  6047. unset($this->Form->request->data['Model']['field']);
  6048. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6049. $thisHour = date('H');
  6050. $optValue = date('G');
  6051. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6052. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6053. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6054. $expected = [
  6055. ['select' => ['name' => 'Model[field][hour]']],
  6056. ['option' => ['value' => '']],
  6057. '/option',
  6058. ['option' => ['value' => '00']],
  6059. '0',
  6060. '/option',
  6061. ['option' => ['value' => '01', 'selected' => 'selected']],
  6062. '1',
  6063. '/option',
  6064. ['option' => ['value' => '02']],
  6065. '2',
  6066. '/option',
  6067. $hoursRegex,
  6068. '/select',
  6069. ];
  6070. $this->assertHtml($expected, $result);
  6071. }
  6072. /**
  6073. * testYear method
  6074. *
  6075. * @return void
  6076. */
  6077. public function testYear()
  6078. {
  6079. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6080. $expected = [
  6081. ['select' => ['name' => 'Model[field][year]']],
  6082. ['option' => ['selected' => 'selected', 'value' => '']],
  6083. '/option',
  6084. ['option' => ['value' => '2007']],
  6085. '2007',
  6086. '/option',
  6087. ['option' => ['value' => '2006']],
  6088. '2006',
  6089. '/option',
  6090. '/select',
  6091. ];
  6092. $this->assertHtml($expected, $result);
  6093. $result = $this->Form->year('Model.field', [
  6094. 'value' => '',
  6095. 'minYear' => 2006,
  6096. 'maxYear' => 2007,
  6097. 'orderYear' => 'asc'
  6098. ]);
  6099. $expected = [
  6100. ['select' => ['name' => 'Model[field][year]']],
  6101. ['option' => ['selected' => 'selected', 'value' => '']],
  6102. '/option',
  6103. ['option' => ['value' => '2006']],
  6104. '2006',
  6105. '/option',
  6106. ['option' => ['value' => '2007']],
  6107. '2007',
  6108. '/option',
  6109. '/select',
  6110. ];
  6111. $this->assertHtml($expected, $result);
  6112. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6113. $result = $this->Form->year('Contact.published', [
  6114. 'empty' => false,
  6115. 'minYear' => 2006,
  6116. 'maxYear' => 2007,
  6117. ]);
  6118. $expected = [
  6119. ['select' => ['name' => 'Contact[published][year]']],
  6120. ['option' => ['value' => '2007']],
  6121. '2007',
  6122. '/option',
  6123. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6124. '2006',
  6125. '/option',
  6126. '/select',
  6127. ];
  6128. $this->assertHtml($expected, $result);
  6129. $result = $this->Form->year('Contact.published', [
  6130. 'empty' => 'Published on',
  6131. ]);
  6132. $this->assertContains('Published on', $result);
  6133. }
  6134. /**
  6135. * Test minYear being prior to the unix epoch
  6136. *
  6137. * @return void
  6138. */
  6139. public function testInputDatetimePreEpoch()
  6140. {
  6141. $start = date('Y') - 80;
  6142. $end = date('Y') - 18;
  6143. $result = $this->Form->input('birth_year', [
  6144. 'type' => 'date',
  6145. 'label' => 'Birth Year',
  6146. 'minYear' => $start,
  6147. 'maxYear' => $end,
  6148. 'month' => false,
  6149. 'day' => false,
  6150. ]);
  6151. $this->assertContains('value="' . $start . '">' . $start, $result);
  6152. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6153. $this->assertNotContains('value="00">00', $result);
  6154. }
  6155. /**
  6156. * testYearAutoExpandRange method
  6157. *
  6158. * @return void
  6159. */
  6160. public function testYearAutoExpandRange()
  6161. {
  6162. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6163. $result = $this->Form->year('User.birthday');
  6164. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6165. $result = $matches[1];
  6166. $expected = range(date('Y') + 5, 1930);
  6167. $this->assertEquals($expected, $result);
  6168. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6169. $result = $this->Form->year('Project.release');
  6170. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6171. $result = $matches[1];
  6172. $expected = range(2050, date('Y') - 5);
  6173. $this->assertEquals($expected, $result);
  6174. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6175. $result = $this->Form->year('Project.release', [
  6176. 'minYear' => 1890,
  6177. 'maxYear' => 1900
  6178. ]);
  6179. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6180. $result = $matches[1];
  6181. $expected = range(1900, 1881);
  6182. $this->assertEquals($expected, $result);
  6183. }
  6184. /**
  6185. * Test that input() accepts the type of date and passes options in.
  6186. *
  6187. * @return void
  6188. */
  6189. public function testInputDate()
  6190. {
  6191. $this->Form->request->data = [
  6192. 'month_year' => ['month' => date('m')],
  6193. ];
  6194. $this->Form->create($this->article);
  6195. $result = $this->Form->input('month_year', [
  6196. 'label' => false,
  6197. 'type' => 'date',
  6198. 'minYear' => 2006,
  6199. 'maxYear' => 2008
  6200. ]);
  6201. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6202. $this->assertNotContains('value="2008" selected="selected"', $result);
  6203. }
  6204. /**
  6205. * Test the label option being set to false.
  6206. *
  6207. * @return void
  6208. */
  6209. public function testInputLabelFalse()
  6210. {
  6211. $this->Form->create($this->article);
  6212. $result = $this->Form->input('title', ['label' => false]);
  6213. $expected = [
  6214. 'div' => ['class' => 'input text required'],
  6215. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6216. '/div'
  6217. ];
  6218. $this->assertHtml($expected, $result);
  6219. }
  6220. /**
  6221. * testInputDateMaxYear method
  6222. *
  6223. * Let's say we want to only allow users born from 2006 to 2008 to register
  6224. * This being the first signup page, we still don't have any data
  6225. *
  6226. * @return void
  6227. */
  6228. public function testInputDateMaxYear()
  6229. {
  6230. $this->Form->request->data = [];
  6231. $this->Form->create($this->article);
  6232. $result = $this->Form->input('birthday', [
  6233. 'label' => false,
  6234. 'type' => 'date',
  6235. 'minYear' => 2006,
  6236. 'maxYear' => 2008,
  6237. 'default' => true
  6238. ]);
  6239. $this->assertContains('value="2008" selected="selected"', $result);
  6240. $this->assertContains('value="2006"', $result);
  6241. $this->assertNotContains('value="2005"', $result);
  6242. $this->assertNotContains('value="2009"', $result);
  6243. }
  6244. /**
  6245. * testTextArea method
  6246. *
  6247. * @return void
  6248. */
  6249. public function testTextArea()
  6250. {
  6251. $this->Form->request->data = ['field' => 'some test data'];
  6252. $result = $this->Form->textarea('field');
  6253. $expected = [
  6254. 'textarea' => ['name' => 'field', 'rows' => 5],
  6255. 'some test data',
  6256. '/textarea',
  6257. ];
  6258. $this->assertHtml($expected, $result);
  6259. $result = $this->Form->textarea('user.bio');
  6260. $expected = [
  6261. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6262. '/textarea',
  6263. ];
  6264. $this->assertHtml($expected, $result);
  6265. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6266. $result = $this->Form->textarea('field');
  6267. $expected = [
  6268. 'textarea' => ['name' => 'field', 'rows' => 5],
  6269. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6270. '/textarea',
  6271. ];
  6272. $this->assertHtml($expected, $result);
  6273. $this->Form->request->data = [
  6274. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6275. ];
  6276. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6277. $expected = [
  6278. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6279. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6280. '/textarea',
  6281. ];
  6282. $this->assertHtml($expected, $result);
  6283. $result = $this->Form->textarea('0.OtherModel.field');
  6284. $expected = [
  6285. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6286. '/textarea'
  6287. ];
  6288. $this->assertHtml($expected, $result);
  6289. }
  6290. /**
  6291. * testTextAreaWithStupidCharacters method
  6292. *
  6293. * test text area with non-ascii characters
  6294. *
  6295. * @return void
  6296. */
  6297. public function testTextAreaWithStupidCharacters()
  6298. {
  6299. $result = $this->Form->textarea('Post.content', [
  6300. 'value' => "GREAT®",
  6301. 'rows' => '15',
  6302. 'cols' => '75'
  6303. ]);
  6304. $expected = [
  6305. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6306. 'GREAT®',
  6307. '/textarea',
  6308. ];
  6309. $this->assertHtml($expected, $result);
  6310. }
  6311. /**
  6312. * Test textareas waxlength read from schema.
  6313. *
  6314. * @return void
  6315. */
  6316. public function testTextAreaMaxLength()
  6317. {
  6318. $this->Form->create([
  6319. 'schema' => [
  6320. 'stuff' => ['type' => 'string', 'length' => 10],
  6321. ]
  6322. ]);
  6323. $result = $this->Form->input('other', ['type' => 'textarea']);
  6324. $expected = [
  6325. 'div' => ['class' => 'input textarea'],
  6326. 'label' => ['for' => 'other'],
  6327. 'Other',
  6328. '/label',
  6329. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6330. '/textarea',
  6331. '/div'
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  6335. $expected = [
  6336. 'div' => ['class' => 'input textarea'],
  6337. 'label' => ['for' => 'stuff'],
  6338. 'Stuff',
  6339. '/label',
  6340. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6341. '/textarea',
  6342. '/div'
  6343. ];
  6344. $this->assertHtml($expected, $result);
  6345. }
  6346. /**
  6347. * testHiddenField method
  6348. *
  6349. * @return void
  6350. */
  6351. public function testHiddenField()
  6352. {
  6353. $this->article['errors'] = [
  6354. 'field' => true
  6355. ];
  6356. $this->Form->request->data['field'] = 'test';
  6357. $this->Form->create($this->article);
  6358. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6359. $expected = [
  6360. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6361. $this->assertHtml($expected, $result);
  6362. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6363. $expected = [
  6364. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6365. ];
  6366. $this->assertHtml($expected, $result);
  6367. }
  6368. /**
  6369. * testFileUploadField method
  6370. *
  6371. * @return void
  6372. */
  6373. public function testFileUploadField()
  6374. {
  6375. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6376. $result = $this->Form->file('Model.upload');
  6377. $this->assertHtml($expected, $result);
  6378. $this->Form->request->data['Model']['upload'] = [
  6379. 'name' => '', 'type' => '', 'tmp_name' => '',
  6380. 'error' => 4, 'size' => 0
  6381. ];
  6382. $result = $this->Form->file('Model.upload');
  6383. $this->assertHtml($expected, $result);
  6384. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6385. $result = $this->Form->file('Model.upload');
  6386. $this->assertHtml($expected, $result);
  6387. }
  6388. /**
  6389. * test File upload input on a model not used in create();
  6390. *
  6391. * @return void
  6392. */
  6393. public function testFileUploadOnOtherModel()
  6394. {
  6395. $this->Form->create($this->article, ['type' => 'file']);
  6396. $result = $this->Form->file('ValidateProfile.city');
  6397. $expected = [
  6398. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6399. ];
  6400. $this->assertHtml($expected, $result);
  6401. }
  6402. /**
  6403. * testButton method
  6404. *
  6405. * @return void
  6406. */
  6407. public function testButton()
  6408. {
  6409. $result = $this->Form->button('Hi');
  6410. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6411. $this->assertHtml($expected, $result);
  6412. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6413. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6414. $this->assertHtml($expected, $result);
  6415. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6416. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6417. $this->assertHtml($expected, $result);
  6418. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6419. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6420. $this->assertHtml($expected, $result);
  6421. $result = $this->Form->button('No type', ['type' => false]);
  6422. $expected = ['button' => [], 'No type', '/button'];
  6423. $this->assertHtml($expected, $result);
  6424. $result = $this->Form->button('Upload Text', [
  6425. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6426. 'escape' => false
  6427. ]);
  6428. $this->assertNotRegExp('/\&039/', $result);
  6429. }
  6430. /**
  6431. * Test that button() makes unlocked fields by default.
  6432. *
  6433. * @return void
  6434. */
  6435. public function testButtonUnlockedByDefault()
  6436. {
  6437. $this->Form->request->params['_csrfToken'] = 'secured';
  6438. $this->Form->button('Save', ['name' => 'save']);
  6439. $this->Form->button('Clear');
  6440. $result = $this->Form->unlockField();
  6441. $this->assertEquals(['save'], $result);
  6442. }
  6443. /**
  6444. * testPostButton method
  6445. *
  6446. * @return void
  6447. */
  6448. public function testPostButton()
  6449. {
  6450. $result = $this->Form->postButton('Hi', '/controller/action');
  6451. $expected = [
  6452. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6453. 'div' => ['style' => 'display:none;'],
  6454. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6455. '/div',
  6456. 'button' => ['type' => 'submit'],
  6457. 'Hi',
  6458. '/button',
  6459. '/form'
  6460. ];
  6461. $this->assertHtml($expected, $result);
  6462. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6463. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6464. }
  6465. public function testPostButtonMethodType()
  6466. {
  6467. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6468. $expected = [
  6469. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6470. 'div' => ['style' => 'display:none;'],
  6471. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6472. '/div',
  6473. 'button' => ['type' => 'submit'],
  6474. 'Hi',
  6475. '/button',
  6476. '/form'
  6477. ];
  6478. $this->assertHtml($expected, $result);
  6479. }
  6480. public function testPostButtonFormOptions()
  6481. {
  6482. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6483. $expected = [
  6484. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6485. 'div' => ['style' => 'display:none;'],
  6486. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6487. '/div',
  6488. 'button' => ['type' => 'submit'],
  6489. 'Hi',
  6490. '/button',
  6491. '/form'
  6492. ];
  6493. $this->assertHtml($expected, $result);
  6494. }
  6495. /**
  6496. * Test using postButton with N dimensional data.
  6497. *
  6498. * @return void
  6499. */
  6500. public function testPostButtonNestedData()
  6501. {
  6502. $data = [
  6503. 'one' => [
  6504. 'two' => [
  6505. 3, 4, 5
  6506. ]
  6507. ]
  6508. ];
  6509. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6510. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6511. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6512. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6513. }
  6514. /**
  6515. * Test that postButton adds _Token fields.
  6516. *
  6517. * @return void
  6518. */
  6519. public function testSecurePostButton()
  6520. {
  6521. $this->Form->request->params['_csrfToken'] = 'testkey';
  6522. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6523. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6524. $tokenDebug = urlencode(json_encode([
  6525. '/posts/delete/1',
  6526. [],
  6527. []
  6528. ]));
  6529. $expected = [
  6530. 'form' => [
  6531. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6532. ],
  6533. ['div' => ['style' => 'display:none;']],
  6534. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6535. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6536. '/div',
  6537. 'button' => ['type' => 'submit'],
  6538. 'Delete',
  6539. '/button',
  6540. ['div' => ['style' => 'display:none;']],
  6541. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6542. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6543. ['input' => [
  6544. 'type' => 'hidden', 'name' => '_Token[debug]',
  6545. 'value' => $tokenDebug,
  6546. ]],
  6547. '/div',
  6548. '/form',
  6549. ];
  6550. $this->assertHtml($expected, $result);
  6551. }
  6552. /**
  6553. * testPostLink method
  6554. *
  6555. * @return void
  6556. */
  6557. public function testPostLink()
  6558. {
  6559. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6560. $expected = [
  6561. 'form' => [
  6562. 'method' => 'post', 'action' => '/posts/delete/1',
  6563. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6564. ],
  6565. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6566. '/form',
  6567. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6568. 'Delete',
  6569. '/a'
  6570. ];
  6571. $this->assertHtml($expected, $result);
  6572. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6573. $expected = [
  6574. 'form' => [
  6575. 'method' => 'post', 'action' => '/posts/delete/1',
  6576. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6577. ],
  6578. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6579. '/form',
  6580. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6581. 'Delete',
  6582. '/a'
  6583. ];
  6584. $this->assertHtml($expected, $result);
  6585. $result = $this->Form->postLink(
  6586. 'Delete',
  6587. '/posts/delete/1',
  6588. ['target' => '_blank', 'class' => 'btn btn-danger']
  6589. );
  6590. $expected = [
  6591. 'form' => [
  6592. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6593. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6594. ],
  6595. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6596. '/form',
  6597. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6598. 'Delete',
  6599. '/a'
  6600. ];
  6601. $this->assertHtml($expected, $result);
  6602. }
  6603. /**
  6604. * Test the confirm option for postLink()
  6605. *
  6606. * @return void
  6607. */
  6608. public function testPostLinkWithConfirm()
  6609. {
  6610. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6611. $expected = [
  6612. 'form' => [
  6613. 'method' => 'post', 'action' => '/posts/delete/1',
  6614. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6615. ],
  6616. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6617. '/form',
  6618. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6619. 'Delete',
  6620. '/a'
  6621. ];
  6622. $this->assertHtml($expected, $result);
  6623. $result = $this->Form->postLink(
  6624. 'Delete',
  6625. '/posts/delete/1',
  6626. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6627. );
  6628. $expected = [
  6629. 'form' => [
  6630. 'method' => 'post', 'action' => '/posts/delete/1',
  6631. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6632. ],
  6633. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6634. '/form',
  6635. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\nthis \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6636. 'Delete',
  6637. '/a'
  6638. ];
  6639. $this->assertHtml($expected, $result);
  6640. }
  6641. /**
  6642. * test postLink() with query string args.
  6643. *
  6644. * @return void
  6645. */
  6646. public function testPostLinkWithQuery()
  6647. {
  6648. $result = $this->Form->postLink(
  6649. 'Delete',
  6650. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6651. );
  6652. $expected = [
  6653. 'form' => [
  6654. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6655. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6656. ],
  6657. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6658. '/form',
  6659. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6660. 'Delete',
  6661. '/a'
  6662. ];
  6663. $this->assertHtml($expected, $result);
  6664. }
  6665. /**
  6666. * Test postLink with additional data.
  6667. *
  6668. * @return void
  6669. */
  6670. public function testPostLinkWithData()
  6671. {
  6672. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6673. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6674. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6675. $this->Form->create($entity);
  6676. $this->Form->end();
  6677. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6678. $this->assertContains(
  6679. '<input type="hidden" name="name" value="show"',
  6680. $result,
  6681. 'should not contain entity data.'
  6682. );
  6683. }
  6684. /**
  6685. * Test that security hashes for postLink include the url.
  6686. *
  6687. * @return void
  6688. */
  6689. public function testPostLinkSecurityHash()
  6690. {
  6691. $hash = Security::hash(
  6692. '/posts/delete/1' .
  6693. serialize(['id' => '1']) .
  6694. '' .
  6695. Security::salt()
  6696. );
  6697. $hash .= '%3Aid';
  6698. $this->Form->request->params['_Token']['key'] = 'test';
  6699. $result = $this->Form->postLink(
  6700. 'Delete',
  6701. '/posts/delete/1',
  6702. ['data' => ['id' => 1]]
  6703. );
  6704. $tokenDebug = urlencode(json_encode([
  6705. '/posts/delete/1',
  6706. [
  6707. 'id' => 1
  6708. ],
  6709. []
  6710. ]));
  6711. $expected = [
  6712. 'form' => [
  6713. 'method' => 'post', 'action' => '/posts/delete/1',
  6714. 'name', 'style' => 'display:none;'
  6715. ],
  6716. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6717. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6718. 'div' => ['style' => 'display:none;'],
  6719. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6720. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6721. ['input' => [
  6722. 'type' => 'hidden', 'name' => '_Token[debug]',
  6723. 'value' => $tokenDebug,
  6724. ]],
  6725. '/div',
  6726. '/form',
  6727. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6728. 'Delete',
  6729. '/a'
  6730. ];
  6731. $this->assertHtml($expected, $result);
  6732. }
  6733. /**
  6734. * Test that postLink doesn't modify the fields in the containing form.
  6735. *
  6736. * postLink() calls inside open forms should not modify the field list
  6737. * for the form.
  6738. *
  6739. * @return void
  6740. */
  6741. public function testPostLinkSecurityHashBlockMode()
  6742. {
  6743. $hash = Security::hash(
  6744. '/posts/delete/1' .
  6745. serialize([]) .
  6746. '' .
  6747. Security::salt()
  6748. );
  6749. $hash .= '%3A';
  6750. $this->Form->request->params['_Token']['key'] = 'test';
  6751. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6752. $this->Form->input('title');
  6753. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6754. $result = $this->View->fetch('postLink');
  6755. $this->assertEquals(['title'], $this->Form->fields);
  6756. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6757. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6758. }
  6759. /**
  6760. * Test that security does not include debug token if debug is false.
  6761. *
  6762. * @return void
  6763. */
  6764. public function testPostLinkSecurityHashNoDebugMode()
  6765. {
  6766. Configure::write('debug', false);
  6767. $hash = Security::hash(
  6768. '/posts/delete/1' .
  6769. serialize(['id' => '1']) .
  6770. '' .
  6771. Security::salt()
  6772. );
  6773. $hash .= '%3Aid';
  6774. $this->Form->request->params['_Token']['key'] = 'test';
  6775. $result = $this->Form->postLink(
  6776. 'Delete',
  6777. '/posts/delete/1',
  6778. ['data' => ['id' => 1]]
  6779. );
  6780. $expected = [
  6781. 'form' => [
  6782. 'method' => 'post', 'action' => '/posts/delete/1',
  6783. 'name', 'style' => 'display:none;'
  6784. ],
  6785. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6786. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6787. 'div' => ['style' => 'display:none;'],
  6788. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6789. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6790. '/div',
  6791. '/form',
  6792. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6793. 'Delete',
  6794. '/a'
  6795. ];
  6796. $this->assertHtml($expected, $result);
  6797. }
  6798. /**
  6799. * Test using postLink with N dimensional data.
  6800. *
  6801. * @return void
  6802. */
  6803. public function testPostLinkNestedData()
  6804. {
  6805. $data = [
  6806. 'one' => [
  6807. 'two' => [
  6808. 3, 4, 5
  6809. ]
  6810. ]
  6811. ];
  6812. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6813. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6814. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6815. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6816. }
  6817. /**
  6818. * test creating postLinks after a GET form.
  6819. *
  6820. * @return void
  6821. */
  6822. public function testPostLinkAfterGetForm()
  6823. {
  6824. $this->Form->request->params['_csrfToken'] = 'testkey';
  6825. $this->Form->request->params['_Token'] = 'val';
  6826. $this->Form->create($this->article, ['type' => 'get']);
  6827. $this->Form->end();
  6828. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6829. $tokenDebug = urlencode(json_encode([
  6830. '/posts/delete/1',
  6831. [],
  6832. []
  6833. ]));
  6834. $expected = [
  6835. 'form' => [
  6836. 'method' => 'post', 'action' => '/posts/delete/1',
  6837. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6838. ],
  6839. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6840. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6841. 'div' => ['style' => 'display:none;'],
  6842. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6843. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6844. ['input' => [
  6845. 'type' => 'hidden', 'name' => '_Token[debug]',
  6846. 'value' => $tokenDebug,
  6847. ]],
  6848. '/div',
  6849. '/form',
  6850. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6851. 'Delete',
  6852. '/a'
  6853. ];
  6854. $this->assertHtml($expected, $result);
  6855. }
  6856. /**
  6857. * Test that postLink adds form tags to view block
  6858. *
  6859. * @return void
  6860. */
  6861. public function testPostLinkFormBuffer()
  6862. {
  6863. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6864. $expected = [
  6865. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6866. 'Delete',
  6867. '/a'
  6868. ];
  6869. $this->assertHtml($expected, $result);
  6870. $result = $this->View->fetch('postLink');
  6871. $expected = [
  6872. 'form' => [
  6873. 'method' => 'post', 'action' => '/posts/delete/1',
  6874. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6875. ],
  6876. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6877. '/form'
  6878. ];
  6879. $this->assertHtml($expected, $result);
  6880. $result = $this->Form->postLink(
  6881. 'Delete',
  6882. '/posts/delete/2',
  6883. ['block' => true, 'method' => 'DELETE']
  6884. );
  6885. $expected = [
  6886. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6887. 'Delete',
  6888. '/a'
  6889. ];
  6890. $this->assertHtml($expected, $result);
  6891. $result = $this->View->fetch('postLink');
  6892. $expected = [
  6893. 'form' => [
  6894. 'method' => 'post', 'action' => '/posts/delete/1',
  6895. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6896. ],
  6897. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6898. '/form',
  6899. [
  6900. 'form' => [
  6901. 'method' => 'post', 'action' => '/posts/delete/2',
  6902. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6903. ],
  6904. ],
  6905. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6906. '/form'
  6907. ];
  6908. $this->assertHtml($expected, $result);
  6909. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6910. $expected = [
  6911. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6912. 'Delete',
  6913. '/a'
  6914. ];
  6915. $this->assertHtml($expected, $result);
  6916. $result = $this->View->fetch('foobar');
  6917. $expected = [
  6918. 'form' => [
  6919. 'method' => 'post', 'action' => '/posts/delete/1',
  6920. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6921. ],
  6922. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6923. '/form'
  6924. ];
  6925. $this->assertHtml($expected, $result);
  6926. }
  6927. /**
  6928. * testSubmitButton method
  6929. *
  6930. * @return void
  6931. */
  6932. public function testSubmitButton()
  6933. {
  6934. $result = $this->Form->submit('');
  6935. $expected = [
  6936. 'div' => ['class' => 'submit'],
  6937. 'input' => ['type' => 'submit', 'value' => ''],
  6938. '/div'
  6939. ];
  6940. $this->assertHtml($expected, $result);
  6941. $result = $this->Form->submit('Test Submit');
  6942. $expected = [
  6943. 'div' => ['class' => 'submit'],
  6944. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6945. '/div'
  6946. ];
  6947. $this->assertHtml($expected, $result);
  6948. $result = $this->Form->submit('Next >');
  6949. $expected = [
  6950. 'div' => ['class' => 'submit'],
  6951. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6952. '/div'
  6953. ];
  6954. $this->assertHtml($expected, $result);
  6955. $result = $this->Form->submit('Next >', ['escape' => false]);
  6956. $expected = [
  6957. 'div' => ['class' => 'submit'],
  6958. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6959. '/div'
  6960. ];
  6961. $this->assertHtml($expected, $result);
  6962. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6963. $expected = [
  6964. 'div' => ['class' => 'submit'],
  6965. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6966. '/div'
  6967. ];
  6968. $this->assertHtml($expected, $result);
  6969. }
  6970. /**
  6971. * test image submit types.
  6972. *
  6973. * @return void
  6974. */
  6975. public function testSubmitImage()
  6976. {
  6977. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6978. $expected = [
  6979. 'div' => ['class' => 'submit'],
  6980. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6981. '/div'
  6982. ];
  6983. $this->assertHtml($expected, $result);
  6984. $result = $this->Form->submit('/relative/cake.power.gif');
  6985. $expected = [
  6986. 'div' => ['class' => 'submit'],
  6987. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6988. '/div'
  6989. ];
  6990. $this->assertHtml($expected, $result);
  6991. $result = $this->Form->submit('cake.power.gif');
  6992. $expected = [
  6993. 'div' => ['class' => 'submit'],
  6994. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6995. '/div'
  6996. ];
  6997. $this->assertHtml($expected, $result);
  6998. $result = $this->Form->submit('Not.an.image');
  6999. $expected = [
  7000. 'div' => ['class' => 'submit'],
  7001. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7002. '/div'
  7003. ];
  7004. $this->assertHtml($expected, $result);
  7005. }
  7006. /**
  7007. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7008. * be submitted at a time.
  7009. *
  7010. * @return void
  7011. */
  7012. public function testSubmitUnlockedByDefault()
  7013. {
  7014. $this->Form->request->params['_Token'] = 'secured';
  7015. $this->Form->submit('Go go');
  7016. $this->Form->submit('Save', ['name' => 'save']);
  7017. $result = $this->Form->unlockField();
  7018. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7019. }
  7020. /**
  7021. * Test submit image with timestamps.
  7022. *
  7023. * @return void
  7024. */
  7025. public function testSubmitImageTimestamp()
  7026. {
  7027. Configure::write('Asset.timestamp', 'force');
  7028. $result = $this->Form->submit('cake.power.gif');
  7029. $expected = [
  7030. 'div' => ['class' => 'submit'],
  7031. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7032. '/div'
  7033. ];
  7034. $this->assertHtml($expected, $result);
  7035. }
  7036. /**
  7037. * test that datetime() works with GET style forms.
  7038. *
  7039. * @return void
  7040. */
  7041. public function testDateTimeWithGetForms()
  7042. {
  7043. extract($this->dateRegex);
  7044. $this->Form->create($this->article, ['type' => 'get']);
  7045. $result = $this->Form->datetime('created');
  7046. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7047. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7048. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7049. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7050. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7051. }
  7052. /**
  7053. * testForMagicInputNonExistingNorValidated method
  7054. *
  7055. * @return void
  7056. */
  7057. public function testForMagicInputNonExistingNorValidated()
  7058. {
  7059. $result = $this->Form->create($this->article);
  7060. $this->Form->templates(['inputContainer' => '{{content}}']);
  7061. $result = $this->Form->input('non_existing_nor_validated');
  7062. $expected = [
  7063. 'label' => ['for' => 'non-existing-nor-validated'],
  7064. 'Non Existing Nor Validated',
  7065. '/label',
  7066. 'input' => [
  7067. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7068. 'id' => 'non-existing-nor-validated'
  7069. ]
  7070. ];
  7071. $this->assertHtml($expected, $result);
  7072. $result = $this->Form->input('non_existing_nor_validated', [
  7073. 'val' => 'my value'
  7074. ]);
  7075. $expected = [
  7076. 'label' => ['for' => 'non-existing-nor-validated'],
  7077. 'Non Existing Nor Validated',
  7078. '/label',
  7079. 'input' => [
  7080. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7081. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7082. ]
  7083. ];
  7084. $this->assertHtml($expected, $result);
  7085. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7086. $result = $this->Form->input('non_existing_nor_validated');
  7087. $expected = [
  7088. 'label' => ['for' => 'non-existing-nor-validated'],
  7089. 'Non Existing Nor Validated',
  7090. '/label',
  7091. 'input' => [
  7092. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7093. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7094. ]
  7095. ];
  7096. $this->assertHtml($expected, $result);
  7097. }
  7098. /**
  7099. * testFormMagicInputLabel method
  7100. *
  7101. * @return void
  7102. */
  7103. public function testFormMagicInputLabel()
  7104. {
  7105. TableRegistry::get('Contacts', [
  7106. 'className' => __NAMESPACE__ . '\ContactsTable'
  7107. ]);
  7108. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7109. $this->Form->templates(['inputContainer' => '{{content}}']);
  7110. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  7111. $expected = [
  7112. 'label' => ['for' => 'contacts-name'],
  7113. 'My label',
  7114. '/label',
  7115. 'input' => [
  7116. 'type' => 'text', 'name' => 'Contacts[name]',
  7117. 'id' => 'contacts-name', 'maxlength' => '255'
  7118. ]
  7119. ];
  7120. $this->assertHtml($expected, $result);
  7121. $result = $this->Form->input('name', [
  7122. 'label' => ['class' => 'mandatory']
  7123. ]);
  7124. $expected = [
  7125. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7126. 'Name',
  7127. '/label',
  7128. 'input' => [
  7129. 'type' => 'text', 'name' => 'name',
  7130. 'id' => 'name', 'maxlength' => '255'
  7131. ]
  7132. ];
  7133. $this->assertHtml($expected, $result);
  7134. $result = $this->Form->input('name', [
  7135. 'div' => false,
  7136. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7137. ]);
  7138. $expected = [
  7139. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7140. 'My label',
  7141. '/label',
  7142. 'input' => [
  7143. 'type' => 'text', 'name' => 'name',
  7144. 'id' => 'name', 'maxlength' => '255'
  7145. ]
  7146. ];
  7147. $this->assertHtml($expected, $result);
  7148. $result = $this->Form->input('Contact.name', [
  7149. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7150. ]);
  7151. $expected = [
  7152. 'label' => ['for' => 'my_id'],
  7153. 'Name',
  7154. '/label',
  7155. 'input' => [
  7156. 'type' => 'text', 'name' => 'Contact[name]',
  7157. 'id' => 'my_id', 'maxlength' => '255'
  7158. ]
  7159. ];
  7160. $this->assertHtml($expected, $result);
  7161. $result = $this->Form->input('1.id');
  7162. $expected = ['input' => [
  7163. 'type' => 'hidden', 'name' => '1[id]',
  7164. 'id' => '1-id'
  7165. ]];
  7166. $this->assertHtml($expected, $result);
  7167. $result = $this->Form->input("1.name");
  7168. $expected = [
  7169. 'label' => ['for' => '1-name'],
  7170. 'Name',
  7171. '/label',
  7172. 'input' => [
  7173. 'type' => 'text', 'name' => '1[name]',
  7174. 'id' => '1-name', 'maxlength' => '255'
  7175. ]
  7176. ];
  7177. $this->assertHtml($expected, $result);
  7178. }
  7179. /**
  7180. * testFormEnd method
  7181. *
  7182. * @return void
  7183. */
  7184. public function testFormEnd()
  7185. {
  7186. $this->assertEquals('</form>', $this->Form->end());
  7187. }
  7188. /**
  7189. * Test the generation of fields for a multi record form.
  7190. *
  7191. * @return void
  7192. */
  7193. public function testMultiRecordForm()
  7194. {
  7195. $this->loadFixtures('Articles', 'Comments');
  7196. $articles = TableRegistry::get('Articles');
  7197. $articles->hasMany('Comments');
  7198. $comment = new Entity(['comment' => 'Value']);
  7199. $article = new Article(['comments' => [$comment]]);
  7200. $this->Form->create([$article]);
  7201. $result = $this->Form->input('0.comments.1.comment');
  7202. //@codingStandardsIgnoreStart
  7203. $expected = [
  7204. 'div' => ['class' => 'input textarea'],
  7205. 'label' => ['for' => '0-comments-1-comment'],
  7206. 'Comment',
  7207. '/label',
  7208. 'textarea' => [
  7209. 'name',
  7210. 'id' => '0-comments-1-comment',
  7211. 'rows' => 5
  7212. ],
  7213. '/textarea',
  7214. '/div'
  7215. ];
  7216. //@codingStandardsIgnoreEnd
  7217. $this->assertHtml($expected, $result);
  7218. $result = $this->Form->input('0.comments.0.comment');
  7219. //@codingStandardsIgnoreStart
  7220. $expected = [
  7221. 'div' => ['class' => 'input textarea'],
  7222. 'label' => ['for' => '0-comments-0-comment'],
  7223. 'Comment',
  7224. '/label',
  7225. 'textarea' => [
  7226. 'name',
  7227. 'id' => '0-comments-0-comment',
  7228. 'rows' => 5
  7229. ],
  7230. 'Value',
  7231. '/textarea',
  7232. '/div'
  7233. ];
  7234. //@codingStandardsIgnoreEnd
  7235. $this->assertHtml($expected, $result);
  7236. $comment->errors('comment', ['Not valid']);
  7237. $result = $this->Form->input('0.comments.0.comment');
  7238. //@codingStandardsIgnoreStart
  7239. $expected = [
  7240. 'div' => ['class' => 'input textarea error'],
  7241. 'label' => ['for' => '0-comments-0-comment'],
  7242. 'Comment',
  7243. '/label',
  7244. 'textarea' => [
  7245. 'name',
  7246. 'class' => 'form-error',
  7247. 'id' => '0-comments-0-comment',
  7248. 'rows' => 5
  7249. ],
  7250. 'Value',
  7251. '/textarea',
  7252. ['div' => ['class' => 'error-message']],
  7253. 'Not valid',
  7254. '/div',
  7255. '/div'
  7256. ];
  7257. //@codingStandardsIgnoreEnd
  7258. $this->assertHtml($expected, $result);
  7259. TableRegistry::get('Comments')
  7260. ->validator('default')
  7261. ->allowEmpty('comment', false);
  7262. $result = $this->Form->input('0.comments.1.comment');
  7263. //@codingStandardsIgnoreStart
  7264. $expected = [
  7265. 'div' => ['class' => 'input textarea required'],
  7266. 'label' => ['for' => '0-comments-1-comment'],
  7267. 'Comment',
  7268. '/label',
  7269. 'textarea' => [
  7270. 'name',
  7271. 'required' => 'required',
  7272. 'id' => '0-comments-1-comment',
  7273. 'rows' => 5
  7274. ],
  7275. '/textarea',
  7276. '/div'
  7277. ];
  7278. //@codingStandardsIgnoreEnd
  7279. $this->assertHtml($expected, $result);
  7280. }
  7281. /**
  7282. * test that some html5 inputs + FormHelper::__call() work
  7283. *
  7284. * @return void
  7285. */
  7286. public function testHtml5Inputs()
  7287. {
  7288. $result = $this->Form->email('User.email');
  7289. $expected = [
  7290. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7291. ];
  7292. $this->assertHtml($expected, $result);
  7293. $result = $this->Form->search('User.query');
  7294. $expected = [
  7295. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7296. ];
  7297. $this->assertHtml($expected, $result);
  7298. $result = $this->Form->search('User.query', ['value' => 'test']);
  7299. $expected = [
  7300. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7301. ];
  7302. $this->assertHtml($expected, $result);
  7303. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7304. $expected = [
  7305. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7306. ];
  7307. $this->assertHtml($expected, $result);
  7308. }
  7309. /**
  7310. * Test accessing html5 inputs through input().
  7311. *
  7312. * @return void
  7313. */
  7314. public function testHtml5InputWithInput()
  7315. {
  7316. $this->Form->create();
  7317. $this->Form->templates(['inputContainer' => '{{content}}']);
  7318. $result = $this->Form->input('website', [
  7319. 'type' => 'url',
  7320. 'val' => 'http://domain.tld',
  7321. 'label' => false
  7322. ]);
  7323. $expected = [
  7324. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7325. ];
  7326. $this->assertHtml($expected, $result);
  7327. }
  7328. /**
  7329. * Test errors when field name is missing.
  7330. *
  7331. * @expectedException \Cake\Core\Exception\Exception
  7332. * @return void
  7333. */
  7334. public function testHtml5InputException()
  7335. {
  7336. $this->Form->email();
  7337. }
  7338. /**
  7339. * Tests that formhelper sets required attributes.
  7340. *
  7341. * @return void
  7342. */
  7343. public function testRequiredAttribute()
  7344. {
  7345. $this->article['required'] = [
  7346. 'title' => true,
  7347. 'body' => false,
  7348. ];
  7349. $this->Form->create($this->article);
  7350. $result = $this->Form->input('title');
  7351. $expected = [
  7352. 'div' => ['class' => 'input text required'],
  7353. 'label' => ['for' => 'title'],
  7354. 'Title',
  7355. '/label',
  7356. 'input' => [
  7357. 'type' => 'text',
  7358. 'name' => 'title',
  7359. 'id' => 'title',
  7360. 'required' => 'required',
  7361. ],
  7362. '/div'
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $result = $this->Form->input('title', ['required' => false]);
  7366. $this->assertNotContains('required', $result);
  7367. $result = $this->Form->input('body');
  7368. $expected = [
  7369. 'div' => ['class' => 'input text'],
  7370. 'label' => ['for' => 'body'],
  7371. 'Body',
  7372. '/label',
  7373. 'input' => [
  7374. 'type' => 'text',
  7375. 'name' => 'body',
  7376. 'id' => 'body',
  7377. ],
  7378. '/div'
  7379. ];
  7380. $this->assertHtml($expected, $result);
  7381. $result = $this->Form->input('body', ['required' => true]);
  7382. $this->assertContains('required', $result);
  7383. }
  7384. /**
  7385. * Tests that it is possible to put inputs outside of the label
  7386. *
  7387. * @return void
  7388. */
  7389. public function testInputsNotNested()
  7390. {
  7391. $this->Form->templates([
  7392. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7393. 'formGroup' => '{{input}}{{label}}',
  7394. ]);
  7395. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  7396. $expected = [
  7397. 'div' => ['class' => 'input checkbox'],
  7398. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7399. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7400. 'label' => ['for' => 'foo'],
  7401. 'Foo',
  7402. '/label',
  7403. '/div'
  7404. ];
  7405. $this->assertHtml($expected, $result);
  7406. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  7407. $expected = [
  7408. 'div' => ['class' => 'input checkbox'],
  7409. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7410. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7411. '/div'
  7412. ];
  7413. $this->assertHtml($expected, $result);
  7414. $result = $this->Form->input('confirm', [
  7415. 'type' => 'radio',
  7416. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7417. ]);
  7418. $expected = [
  7419. 'div' => ['class' => 'input radio'],
  7420. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7421. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7422. ['label' => ['for' => 'confirm-y']],
  7423. 'Yes',
  7424. '/label',
  7425. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7426. ['label' => ['for' => 'confirm-n']],
  7427. 'No',
  7428. '/label',
  7429. '<label',
  7430. 'Confirm',
  7431. '/label',
  7432. '/div',
  7433. ];
  7434. $this->assertHtml($expected, $result);
  7435. $result = $this->Form->select('category', ['1', '2'], [
  7436. 'multiple' => 'checkbox',
  7437. 'name' => 'fish',
  7438. ]);
  7439. $expected = [
  7440. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7441. ['div' => ['class' => 'checkbox']],
  7442. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7443. ['label' => ['for' => 'fish-0']],
  7444. '1',
  7445. '/label',
  7446. '/div',
  7447. ['div' => ['class' => 'checkbox']],
  7448. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7449. ['label' => ['for' => 'fish-1']],
  7450. '2',
  7451. '/label',
  7452. '/div'
  7453. ];
  7454. $this->assertHtml($expected, $result);
  7455. }
  7456. /**
  7457. * Test that *Container templates are used by input.
  7458. *
  7459. * @return void
  7460. */
  7461. public function testInputContainerTemplates()
  7462. {
  7463. $this->Form->templates([
  7464. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7465. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7466. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7467. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7468. ]);
  7469. $this->article['errors'] = [
  7470. 'Article' => ['published' => 'error message']
  7471. ];
  7472. $this->Form->create($this->article);
  7473. $result = $this->Form->input('accept', [
  7474. 'type' => 'checkbox'
  7475. ]);
  7476. $expected = [
  7477. 'div' => ['class' => 'check'],
  7478. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7479. 'label' => ['for' => 'accept'],
  7480. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7481. 'Accept',
  7482. '/label',
  7483. '/div'
  7484. ];
  7485. $this->assertHtml($expected, $result);
  7486. $result = $this->Form->input('accept', [
  7487. 'type' => 'radio',
  7488. 'options' => ['Y', 'N']
  7489. ]);
  7490. $this->assertContains('<div class="rad">', $result);
  7491. $result = $this->Form->input('Article.published', [
  7492. 'type' => 'radio',
  7493. 'options' => ['Y', 'N']
  7494. ]);
  7495. $this->assertContains('<div class="rad err">', $result);
  7496. $result = $this->Form->input('Article.created', [
  7497. 'type' => 'datetime',
  7498. ]);
  7499. $this->assertContains('<div class="dt">', $result);
  7500. }
  7501. /**
  7502. * Test that *Container templates are used by input.
  7503. *
  7504. * @return void
  7505. */
  7506. public function testFormGroupTemplates()
  7507. {
  7508. $this->Form->templates([
  7509. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7510. ]);
  7511. $this->Form->create($this->article);
  7512. $result = $this->Form->input('accept', [
  7513. 'type' => 'radio',
  7514. 'options' => ['Y', 'N']
  7515. ]);
  7516. $this->assertContains('<div class="radio">', $result);
  7517. }
  7518. /**
  7519. * Test resetting templates.
  7520. *
  7521. * @return void
  7522. */
  7523. public function testResetTemplates()
  7524. {
  7525. $this->Form->templates(['input' => '<input/>']);
  7526. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7527. $this->assertNull($this->Form->resetTemplates());
  7528. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7529. }
  7530. /**
  7531. * Test the context method.
  7532. *
  7533. * @return void
  7534. */
  7535. public function testContext()
  7536. {
  7537. $result = $this->Form->context();
  7538. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7539. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7540. $this->assertSame($mock, $this->Form->context($mock));
  7541. $this->assertSame($mock, $this->Form->context());
  7542. }
  7543. /**
  7544. * testAutoDomId
  7545. *
  7546. * @return void
  7547. */
  7548. public function testAutoDomId()
  7549. {
  7550. $result = $this->Form->text('field', ['id' => true]);
  7551. $expected = [
  7552. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7553. ];
  7554. $this->assertHtml($expected, $result);
  7555. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7556. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7557. $expected = [
  7558. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7559. ['label' => ['for' => 'field-0']],
  7560. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7561. 'option A',
  7562. '/label',
  7563. ['label' => ['for' => 'field-1']],
  7564. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7565. 'option B',
  7566. '/label',
  7567. ];
  7568. $this->assertHtml($expected, $result);
  7569. $result = $this->Form->select(
  7570. 'multi_field',
  7571. ['first', 'second'],
  7572. ['multiple' => 'checkbox', 'id' => true]
  7573. );
  7574. $expected = [
  7575. 'input' => [
  7576. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7577. ],
  7578. ['div' => ['class' => 'checkbox']],
  7579. ['label' => ['for' => 'multi-field-0']],
  7580. ['input' => [
  7581. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7582. 'value' => '0', 'id' => 'multi-field-0'
  7583. ]],
  7584. 'first',
  7585. '/label',
  7586. '/div',
  7587. ['div' => ['class' => 'checkbox']],
  7588. ['label' => ['for' => 'multi-field-1']],
  7589. ['input' => [
  7590. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7591. 'value' => '1', 'id' => 'multi-field-1'
  7592. ]],
  7593. 'second',
  7594. '/label',
  7595. '/div',
  7596. ];
  7597. $this->assertHtml($expected, $result);
  7598. }
  7599. }