FormHelperTest.php 265 KB

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