Added RenderRouteHandlerTests
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Web.Routing;
|
||||
using System.Xml;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
@@ -15,7 +16,7 @@ using umbraco.cms.businesslogic.template;
|
||||
namespace Umbraco.Tests.DocumentLookups
|
||||
{
|
||||
[TestFixture, RequiresSTA]
|
||||
public abstract class BaseTest
|
||||
public abstract class BaseRoutingTest
|
||||
{
|
||||
[SetUp]
|
||||
public virtual void Initialize()
|
||||
@@ -23,6 +24,7 @@ namespace Umbraco.Tests.DocumentLookups
|
||||
TestHelper.SetupLog4NetForTests();
|
||||
TestHelper.InitializeDatabase();
|
||||
Resolution.Freeze();
|
||||
ApplicationContext = new ApplicationContext() { IsReady = true };
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
@@ -36,9 +38,12 @@ namespace Umbraco.Tests.DocumentLookups
|
||||
Cache.ClearAllCache();
|
||||
}
|
||||
|
||||
protected FakeHttpContextFactory GetHttpContextFactory(string url)
|
||||
protected FakeHttpContextFactory GetHttpContextFactory(string url, RouteData routeData = null)
|
||||
{
|
||||
var factory = new FakeHttpContextFactory(url);
|
||||
var factory = routeData != null
|
||||
? new FakeHttpContextFactory(url, routeData)
|
||||
: new FakeHttpContextFactory(url);
|
||||
|
||||
|
||||
//set the state helper
|
||||
StateHelper.HttpContext = factory.HttpContext;
|
||||
@@ -46,19 +51,21 @@ namespace Umbraco.Tests.DocumentLookups
|
||||
return factory;
|
||||
}
|
||||
|
||||
private UmbracoContext GetUmbracoContext(string url, Template template)
|
||||
protected ApplicationContext ApplicationContext { get; private set; }
|
||||
|
||||
private UmbracoContext GetUmbracoContext(string url, Template template, RouteData routeData = null)
|
||||
{
|
||||
var ctx = new UmbracoContext(
|
||||
GetHttpContextFactory(url).HttpContext,
|
||||
new ApplicationContext(),
|
||||
GetHttpContextFactory(url, routeData).HttpContext,
|
||||
ApplicationContext,
|
||||
new FakeRoutesCache());
|
||||
SetupUmbracoContextForTest(ctx, template);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
protected RoutingContext GetRoutingContext(string url, Template template)
|
||||
protected RoutingContext GetRoutingContext(string url, Template template, RouteData routeData = null)
|
||||
{
|
||||
var umbracoContext = GetUmbracoContext(url, template);
|
||||
var umbracoContext = GetUmbracoContext(url, template, routeData);
|
||||
var contentStore = new XmlContentStore();
|
||||
var niceUrls = new NiceUrlProvider(contentStore, umbracoContext);
|
||||
var routingRequest = new RoutingContext(
|
||||
@@ -70,23 +77,14 @@ namespace Umbraco.Tests.DocumentLookups
|
||||
return routingRequest;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initlializes the UmbracoContext with specific XML
|
||||
/// </summary>
|
||||
/// <param name="umbracoContext"></param>
|
||||
/// <param name="template"></param>
|
||||
protected void SetupUmbracoContextForTest(UmbracoContext umbracoContext, Template template)
|
||||
protected virtual string GetXmlContent(Template template)
|
||||
{
|
||||
umbracoContext.GetXmlDelegate = () =>
|
||||
{
|
||||
var xDoc = new XmlDocument();
|
||||
|
||||
//create a custom xml structure to return
|
||||
|
||||
xDoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?><!DOCTYPE root[
|
||||
return @"<?xml version=""1.0"" encoding=""utf-8""?>
|
||||
<!DOCTYPE root[
|
||||
<!ELEMENT Home ANY>
|
||||
<!ATTLIST Home id ID #REQUIRED>
|
||||
|
||||
<!ELEMENT CustomDocument ANY>
|
||||
<!ATTLIST CustomDocument id ID #REQUIRED>
|
||||
]>
|
||||
<root id=""-1"">
|
||||
<Home id=""1046"" parentID=""-1"" level=""1"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""" + template.Id + @""" sortOrder=""2"" createDate=""2012-06-12T14:13:17"" updateDate=""2012-07-20T18:50:43"" nodeName=""Home"" urlName=""home"" writerName=""admin"" creatorName=""admin"" path=""-1,1046"" isDoc="""">
|
||||
@@ -106,8 +104,24 @@ namespace Umbraco.Tests.DocumentLookups
|
||||
<Home id=""1175"" parentID=""1046"" level=""2"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""" + template.Id + @""" sortOrder=""2"" createDate=""2012-07-20T18:08:01"" updateDate=""2012-07-20T18:49:32"" nodeName=""Sub 2"" urlName=""sub-2"" writerName=""admin"" creatorName=""admin"" path=""-1,1046,1175"" isDoc=""""><content><![CDATA[]]></content>
|
||||
</Home>
|
||||
</Home>
|
||||
<Home id=""1172"" parentID=""-1"" level=""1"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""" + template.Id + @""" sortOrder=""3"" createDate=""2012-07-16T15:26:59"" updateDate=""2012-07-18T14:23:35"" nodeName=""Test"" urlName=""test"" writerName=""admin"" creatorName=""admin"" path=""-1,1172"" isDoc="""" />
|
||||
</root>");
|
||||
<CustomDocument id=""1172"" parentID=""-1"" level=""1"" writerID=""0"" creatorID=""0"" nodeType=""1044"" template=""" + template.Id + @""" sortOrder=""3"" createDate=""2012-07-16T15:26:59"" updateDate=""2012-07-18T14:23:35"" nodeName=""Test"" urlName=""test-page"" writerName=""admin"" creatorName=""admin"" path=""-1,1172"" isDoc="""" />
|
||||
</root>";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initlializes the UmbracoContext with specific XML
|
||||
/// </summary>
|
||||
/// <param name="umbracoContext"></param>
|
||||
/// <param name="template"></param>
|
||||
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;
|
||||
};
|
||||
@@ -7,7 +7,7 @@ using umbraco.cms.businesslogic.template;
|
||||
namespace Umbraco.Tests.DocumentLookups
|
||||
{
|
||||
[TestFixture]
|
||||
public class LookupByAliasTests : BaseTest
|
||||
public class LookupByAliasTests : BaseRoutingTest
|
||||
{
|
||||
[TestCase("/this/is/my/alias", 1046)]
|
||||
[TestCase("/anotheralias", 1046)]
|
||||
|
||||
@@ -8,7 +8,7 @@ using umbraco.cms.businesslogic.template;
|
||||
namespace Umbraco.Tests.DocumentLookups
|
||||
{
|
||||
[TestFixture]
|
||||
public class LookupByNiceUrlTests : BaseTest
|
||||
public class LookupByNiceUrlTests : BaseRoutingTest
|
||||
{
|
||||
|
||||
[TestCase("/")]
|
||||
|
||||
94
src/Umbraco.Tests/DocumentLookups/RenderRouteHandlerTests.cs
Normal file
94
src/Umbraco.Tests/DocumentLookups/RenderRouteHandlerTests.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Tests.Stubs;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
using Umbraco.Web;
|
||||
using Umbraco.Web.Models;
|
||||
using Umbraco.Web.Mvc;
|
||||
using Umbraco.Web.Routing;
|
||||
using umbraco.BusinessLogic;
|
||||
using umbraco.cms.businesslogic.template;
|
||||
|
||||
namespace Umbraco.Tests.DocumentLookups
|
||||
{
|
||||
[TestFixture]
|
||||
public class RenderRouteHandlerTests : BaseRoutingTest
|
||||
{
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
var webBoot = new WebBootManager(new UmbracoApplication());
|
||||
webBoot.CreateRoutes();
|
||||
}
|
||||
|
||||
public override void TearDown()
|
||||
{
|
||||
base.TearDown();
|
||||
RouteTable.Routes.Clear();
|
||||
}
|
||||
|
||||
//test all template name styles to match the ActionName
|
||||
[TestCase("home-page")]
|
||||
[TestCase("Home-Page")]
|
||||
[TestCase("HomePage")]
|
||||
[TestCase("homePage")]
|
||||
public void Umbraco_Route_User_Defined_Controller_Action(string templateName)
|
||||
{
|
||||
var template = Template.MakeNew(templateName, new User(0));
|
||||
var route = RouteTable.Routes["Umbraco_default"];
|
||||
var routeData = new RouteData() {Route = route};
|
||||
var routingContext = GetRoutingContext("~/dummy-page", template, routeData);
|
||||
var docRequest = new DocumentRequest(routingContext.UmbracoContext.UmbracoUrl, routingContext)
|
||||
{
|
||||
Node = routingContext.ContentStore.GetDocumentById(routingContext.UmbracoContext, 1172),
|
||||
Template = template
|
||||
};
|
||||
|
||||
var handler = new RenderRouteHandler(new TestControllerFactory());
|
||||
|
||||
handler.GetHandlerForRoute(routingContext.UmbracoContext.HttpContext.Request.RequestContext, docRequest);
|
||||
Assert.AreEqual("CustomDocument", routeData.Values["controller"].ToString());
|
||||
Assert.AreEqual("HomePage", routeData.Values["action"].ToString());
|
||||
}
|
||||
|
||||
|
||||
#region Internal classes
|
||||
|
||||
///// <summary>
|
||||
///// Used to test a user route (non-umbraco)
|
||||
///// </summary>
|
||||
//private class CustomUserController : Controller
|
||||
//{
|
||||
|
||||
// public ActionResult Index()
|
||||
// {
|
||||
// return View();
|
||||
// }
|
||||
|
||||
// public ActionResult Test(int id)
|
||||
// {
|
||||
// return View();
|
||||
// }
|
||||
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Used to test a user route umbraco route
|
||||
/// </summary>
|
||||
public class CustomDocumentController : RenderMvcController
|
||||
{
|
||||
|
||||
public ActionResult HomePage(RenderModel model)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
57
src/Umbraco.Tests/DocumentLookups/RouteTestExtensions.cs
Normal file
57
src/Umbraco.Tests/DocumentLookups/RouteTestExtensions.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System.Web;
|
||||
using System.Web.Routing;
|
||||
using Rhino.Mocks;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
|
||||
namespace Umbraco.Tests.DocumentLookups
|
||||
{
|
||||
public static class RouteTestExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Return the route data for the url based on a mocked context
|
||||
/// </summary>
|
||||
/// <param name="routes"></param>
|
||||
/// <param name="requestUrl"></param>
|
||||
/// <returns></returns>
|
||||
public static RouteData GetDataForRoute(this RouteCollection routes, string requestUrl)
|
||||
{
|
||||
var context = new FakeHttpContextFactory(requestUrl);
|
||||
return routes.GetDataForRoute(context.HttpContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the route data based on the url and http context
|
||||
/// </summary>
|
||||
/// <param name="routes"></param>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
public static RouteData GetDataForRoute(this RouteCollection routes, HttpContextBase httpContext)
|
||||
{
|
||||
var data = routes.GetRouteData(httpContext);
|
||||
|
||||
//set the route data on the request context
|
||||
httpContext.Request.RequestContext.Stub(x => x.RouteData).Return(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the URL will be ignored in the RouteTable
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
/// <remarks>
|
||||
/// MVCContrib has a similar one but is faulty:
|
||||
/// http://mvccontrib.codeplex.com/workitem/7173
|
||||
/// </remarks>
|
||||
public static bool ShouldIgnoreRoute(this string url)
|
||||
{
|
||||
var http = new FakeHttpContextFactory(url);
|
||||
var r = RouteTable.Routes.GetRouteData(http.HttpContext);
|
||||
if (r == null) return false;
|
||||
return (r.RouteHandler is StopRoutingHandler);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
39
src/Umbraco.Tests/Stubs/TestControllerFactory.cs
Normal file
39
src/Umbraco.Tests/Stubs/TestControllerFactory.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Umbraco.Core;
|
||||
|
||||
namespace Umbraco.Tests.Stubs
|
||||
{
|
||||
/// <summary>
|
||||
/// Used in place of the UmbracoControllerFactory which relies on BuildManager which throws exceptions in a unit test context
|
||||
/// </summary>
|
||||
internal class TestControllerFactory : IControllerFactory
|
||||
{
|
||||
|
||||
public IController CreateController(RequestContext requestContext, string controllerName)
|
||||
{
|
||||
var types = TypeFinder.FindClassesOfType<ControllerBase>(new[] { Assembly.GetExecutingAssembly() });
|
||||
|
||||
var controllerTypes = types.Where(x => x.Name.Equals(controllerName + "Controller", StringComparison.InvariantCultureIgnoreCase));
|
||||
var t = controllerTypes.SingleOrDefault();
|
||||
|
||||
if (t == null)
|
||||
return null;
|
||||
|
||||
return Activator.CreateInstance(t) as IController;
|
||||
}
|
||||
|
||||
public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void ReleaseController(IController controller)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,9 +60,12 @@
|
||||
<Compile Include="CacheRefresherFactoryTests.cs" />
|
||||
<Compile Include="ContentStoreTests.cs" />
|
||||
<Compile Include="DataTypeFactoryTests.cs" />
|
||||
<Compile Include="DocumentLookups\BaseTest.cs" />
|
||||
<Compile Include="DocumentLookups\BaseRoutingTest.cs" />
|
||||
<Compile Include="DocumentLookups\LookupByAliasTests.cs" />
|
||||
<Compile Include="DocumentLookups\LookupByNiceUrlTests.cs" />
|
||||
<Compile Include="DocumentLookups\RenderRouteHandlerTests.cs" />
|
||||
<Compile Include="DocumentLookups\RouteTestExtensions.cs" />
|
||||
<Compile Include="Stubs\TestControllerFactory.cs" />
|
||||
<Compile Include="UriUtilityTests.cs" />
|
||||
<Compile Include="MacroFieldEditorsResolverTests.cs" />
|
||||
<Compile Include="MacroEngineFactoryTests.cs" />
|
||||
|
||||
@@ -78,19 +78,7 @@ namespace Umbraco.Web.Routing
|
||||
// to allow for fallbacks when doing dictionnary lookup and such?
|
||||
|
||||
public IDocument Node
|
||||
{
|
||||
//get
|
||||
//{
|
||||
// if (!HasNode)
|
||||
// return null;
|
||||
// if (_node == null)
|
||||
// {
|
||||
// //TODO: See the note below, if we don't allow for a get/set INode then how would someone implement
|
||||
// // their own INode? it would not be possible since we're instantiating a specific Node object here.
|
||||
// _node = new Node(XmlNode);
|
||||
// }
|
||||
// return _node;
|
||||
//}
|
||||
{
|
||||
get { return _node; }
|
||||
set
|
||||
{
|
||||
@@ -100,27 +88,6 @@ namespace Umbraco.Web.Routing
|
||||
}
|
||||
}
|
||||
|
||||
////TODO: Should we remove this somehow in place of an INode getter/setter? we are really bound to the xml structure here
|
||||
///// <summary>
|
||||
///// Gets or sets the document request's document xml node.
|
||||
///// </summary>
|
||||
//internal XmlNode XmlNode
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
// return _xmlNode;
|
||||
// }
|
||||
// set
|
||||
// {
|
||||
// _xmlNode = value;
|
||||
// this.Template = null;
|
||||
// if (_xmlNode != null)
|
||||
// _nodeId = int.Parse(RoutingContext.ContentStore.GetNodeProperty(_xmlNode, "@id"));
|
||||
// else
|
||||
// _nodeId = 0;
|
||||
// }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the document request's template.
|
||||
/// </summary>
|
||||
|
||||
@@ -51,12 +51,7 @@ namespace Umbraco.Web
|
||||
ModelBinders.Binders.Add(new KeyValuePair<Type, IModelBinder>(typeof(RenderModel), new RenderModelBinder()));
|
||||
|
||||
//set routes
|
||||
var route = RouteTable.Routes.MapRoute(
|
||||
"Umbraco_default",
|
||||
"Umbraco/RenderMvc/{action}/{id}",
|
||||
new { controller = "RenderMvc", action = "Index", id = UrlParameter.Optional }
|
||||
);
|
||||
route.RouteHandler = new RenderRouteHandler(ControllerBuilder.Current.GetControllerFactory());
|
||||
CreateRoutes();
|
||||
|
||||
//find and initialize the application startup handlers, we need to initialize this resolver here because
|
||||
//it is a special resolver where they need to be instantiated first before any other resolvers in order to bind to
|
||||
@@ -107,6 +102,20 @@ namespace Umbraco.Web
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the routes
|
||||
/// </summary>
|
||||
protected internal void CreateRoutes()
|
||||
{
|
||||
//set routes
|
||||
var route = RouteTable.Routes.MapRoute(
|
||||
"Umbraco_default",
|
||||
"Umbraco/RenderMvc/{action}/{id}",
|
||||
new { controller = "RenderMvc", action = "Index", id = UrlParameter.Optional }
|
||||
);
|
||||
route.RouteHandler = new RenderRouteHandler(ControllerBuilder.Current.GetControllerFactory());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes all web based and core resolves
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user