using System; using System.Configuration; using System.Data.SqlServerCe; 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.Core.Persistence; using Umbraco.Tests.Stubs; using Umbraco.Web; using Umbraco.Web.Routing; using Umbraco.Web.Services; using umbraco.BusinessLogic; namespace Umbraco.Tests.TestHelpers { /// /// Use this abstract class for tests that requires a Sql Ce database populated with the umbraco db schema. /// The PetaPoco Database class should be used through the singleton. /// [TestFixture, RequiresSTA] public abstract class BaseDatabaseFactoryTest { [SetUp] public virtual void Initialize() { TestHelper.SetupLog4NetForTests(); string path = TestHelper.CurrentAssemblyDirectory; AppDomain.CurrentDomain.SetData("DataDirectory", path); //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()); //Delete database file before continueing string filePath = string.Concat(path, "\\test.sdf"); if (File.Exists(filePath)) { File.Delete(filePath); } //Get the connectionstring settings from config var settings = ConfigurationManager.ConnectionStrings["umbracoDbDsn"]; //Create the Sql CE database var engine = new SqlCeEngine(settings.ConnectionString); engine.CreateDatabase(); Resolution.Freeze(); ApplicationContext = new ApplicationContext() { IsReady = true }; ServiceContext = ServiceContext.Current; DatabaseContext = new DatabaseContext(); //Configure the Database and Sql Syntax based on connection string set in config DatabaseContext.Initialize(); //Create the umbraco database and its base data DatabaseContext.Database.Initialize(); } [TearDown] public virtual void TearDown() { //reset the app context ServiceContext = null; DatabaseContext = null; ApplicationContext.Current = null; Resolution.IsFrozen = false; string path = TestHelper.CurrentAssemblyDirectory; AppDomain.CurrentDomain.SetData("DataDirectory", null); string filePath = string.Concat(path, "\\test.sdf"); if (File.Exists(filePath)) { File.Delete(filePath); } } protected ApplicationContext ApplicationContext { get; private set; } protected ServiceContext ServiceContext { get; private set; } protected DatabaseContext DatabaseContext { get; private set; } protected UmbracoContext GetUmbracoContext(string url, int templateId, RouteData routeData = null) { var ctx = new UmbracoContext( GetHttpContextFactory(url, routeData).HttpContext, ApplicationContext, ServiceContext, DatabaseContext, GetRoutesCache()); SetupUmbracoContextForTest(ctx, templateId); return ctx; } 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; } internal virtual IRoutesCache GetRoutesCache() { return new FakeRoutesCache(); } /// /// 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; }; } protected virtual string GetXmlContent(int templateId) { return @" ]> 1 This is some content]]> "; } } }