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.Configuration; using Umbraco.Core.IO; using Umbraco.Core.ObjectResolution; using Umbraco.Core.Persistence; using Umbraco.Core.Services; using Umbraco.Tests.Stubs; using Umbraco.Web; using Umbraco.Web.Routing; 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(); TestHelper.InitializeContentDirectories(); string path = TestHelper.CurrentAssemblyDirectory; AppDomain.CurrentDomain.SetData("DataDirectory", path); UmbracoSettings.UseLegacyXmlSchema = false; //Delete database file before continueing string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf"); if (File.Exists(filePath)) { File.Delete(filePath); } //Get the connectionstring settings from config var settings = ConfigurationManager.ConnectionStrings["umbracoDbDsn"]; ConfigurationManager.AppSettings.Set("umbracoDbDSN", @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\UmbracoPetaPocoTests.sdf"); //Create the Sql CE database var engine = new SqlCeEngine(settings.ConnectionString); engine.CreateDatabase(); Resolution.Freeze(); ApplicationContext = new ApplicationContext() { IsReady = true }; DatabaseContext = DatabaseContext.Current; ServiceContext = ServiceContext.Current; //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.CreateDatabaseSchema(); } [TearDown] public virtual void TearDown() { TestHelper.CleanContentDirectories(); //reset the app context DatabaseContext = null; ApplicationContext.Current = null; ServiceContext = null; Resolution.IsFrozen = false; string path = TestHelper.CurrentAssemblyDirectory; AppDomain.CurrentDomain.SetData("DataDirectory", null); string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf"); if (File.Exists(filePath)) { //File.Delete(filePath); } } protected ApplicationContext ApplicationContext { get; set; } protected ServiceContext ServiceContext { get; set; } protected DatabaseContext DatabaseContext { get; set; } 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 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]]> "; } } }