using System.Web.Routing;
using System.Xml;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.ObjectResolution;
using Umbraco.Tests.Stubs;
using Umbraco.Web;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.cache;
using umbraco.cms.businesslogic.template;
namespace Umbraco.Tests.TestHelpers
{
[TestFixture]
public abstract class BaseWebTest
{
[SetUp]
public virtual void Initialize()
{
TestHelper.SetupLog4NetForTests();
TestHelper.InitializeDatabase();
Resolution.Freeze();
ApplicationContext = new ApplicationContext() { IsReady = true };
}
[TearDown]
public virtual void TearDown()
{
//reset the context on global settings
Umbraco.Core.Configuration.GlobalSettings.HttpContext = null;
Resolution.IsFrozen = false;
TestHelper.ClearDatabase();
Cache.ClearAllCache();
}
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; }
protected UmbracoContext GetUmbracoContext(string url, Template template, RouteData routeData = null)
{
var ctx = new UmbracoContext(
GetHttpContextFactory(url, routeData).HttpContext,
ApplicationContext,
new FakeRoutesCache());
SetupUmbracoContextForTest(ctx, template);
return ctx;
}
protected virtual string GetXmlContent(Template template)
{
return @"
]>
1
This is some content]]>
";
}
///
/// Initlializes the UmbracoContext with specific XML
///
///
///
protected void SetupUmbracoContextForTest(UmbracoContext umbracoContext, Template template)
{
umbracoContext.GetXmlDelegate = () =>
{
var xDoc = new XmlDocument();
//create a custom xml structure to return
xDoc.LoadXml(GetXmlContent(template));
//return the custom x doc
return xDoc;
};
}
}
}