FormHelperTest.php 270 KB

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