FormHelperTest.php 269 KB

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