using System; using System.IO; using System.Web.Routing; using System.Xml; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.IO; using Umbraco.Core.ObjectResolution; using Umbraco.Tests.Stubs; using Umbraco.Web; using Umbraco.Web.Routing; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.cache; using umbraco.cms.businesslogic.template; namespace Umbraco.Tests.TestHelpers { [TestFixture, RequiresSTA] public abstract class BaseWebTest { [SetUp] public virtual void Initialize() { TestHelper.SetupLog4NetForTests(); AppDomain.CurrentDomain.SetData("DataDirectory", TestHelper.CurrentAssemblyDirectory); if (RequiresDbSetup) TestHelper.InitializeDatabase(); Resolution.Freeze(); ApplicationContext = new ApplicationContext() { IsReady = true }; //we need to clear out all currently created template files var masterPages = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Masterpages)); masterPages.GetFiles().ForEach(x => x.Delete()); var mvcViews = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.MvcViews)); mvcViews.GetFiles().ForEach(x => x.Delete()); } [TearDown] public virtual void TearDown() { AppDomain.CurrentDomain.SetData("DataDirectory", null); //reset the app context ApplicationContext.Current = null; Resolution.IsFrozen = false; if (RequiresDbSetup) TestHelper.ClearDatabase(); Cache.ClearAllCache(); } /// /// By default this unit test will create and initialize an umbraco database /// protected virtual bool RequiresDbSetup { get { return true; } } protected FakeHttpContextFactory GetHttpContextFactory(string url, RouteData routeData = null) { var factory = routeData != null ? new FakeHttpContextFactory(url, routeData) : new FakeHttpContextFactory(url); //set the state helper StateHelper.HttpContext = factory.HttpContext; return factory; } protected ApplicationContext ApplicationContext { get; private set; } internal virtual IRoutesCache GetRoutesCache() { return new FakeRoutesCache(); } protected UmbracoContext GetUmbracoContext(string url, int templateId, RouteData routeData = null) { var ctx = new UmbracoContext( GetHttpContextFactory(url, routeData).HttpContext, ApplicationContext, GetRoutesCache()); SetupUmbracoContextForTest(ctx, templateId); return ctx; } protected virtual string GetXmlContent(int templateId) { return @" ]> 1 This is some content]]> "; } /// /// Initlializes the UmbracoContext with specific XML /// /// /// protected void SetupUmbracoContextForTest(UmbracoContext umbracoContext, int templateId) { umbracoContext.GetXmlDelegate = () => { var xDoc = new XmlDocument(); //create a custom xml structure to return xDoc.LoadXml(GetXmlContent(templateId)); //return the custom x doc return xDoc; }; } } }