Files
Umbraco-CMS/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs

178 lines
6.3 KiB
C#
Raw Normal View History

using System.Web.Mvc;
2012-08-14 23:35:34 +06:00
using System.Web.Routing;
2015-01-09 11:07:38 +11:00
using Moq;
2012-08-14 23:35:34 +06:00
using NUnit.Framework;
using Umbraco.Core;
2015-01-09 11:07:38 +11:00
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.TestHelpers.Stubs;
2012-08-14 23:35:34 +06:00
using Umbraco.Web;
using Umbraco.Web.Models;
2012-08-14 23:35:34 +06:00
using Umbraco.Web.Mvc;
using Umbraco.Web.Routing;
2013-02-27 23:34:55 +06:00
using Umbraco.Web.WebApi;
2012-08-14 23:35:34 +06:00
using umbraco.BusinessLogic;
using Umbraco.Core.Profiling;
2013-02-19 14:00:17 -01:00
using Umbraco.Core.Strings;
2012-08-14 23:35:34 +06:00
namespace Umbraco.Tests.Routing
2012-08-14 23:35:34 +06:00
{
[DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)]
[TestFixture]
2012-08-14 23:35:34 +06:00
public class RenderRouteHandlerTests : BaseRoutingTest
{
2012-08-14 23:35:34 +06:00
public override void Initialize()
{
2012-11-29 13:05:51 -01:00
base.Initialize();
SettingsForTests.UmbracoPath = "~/umbraco";
2012-11-29 13:05:51 -01:00
var webBoot = new WebBootManager(new UmbracoApplication(), new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()), true);
//webBoot.Initialize();
//webBoot.Startup(null); -> don't call startup, we don't want any other application event handlers to bind for this test.
//webBoot.Complete(null);
2012-08-14 23:35:34 +06:00
webBoot.CreateRoutes();
}
protected override void FreezeResolution()
{
DefaultRenderMvcControllerResolver.Current = new DefaultRenderMvcControllerResolver(typeof(RenderMvcController));
SurfaceControllerResolver.Current = new SurfaceControllerResolver(
new ActivatorServiceProvider(), Logger,
PluginManager.Current.ResolveSurfaceControllers());
UmbracoApiControllerResolver.Current = new UmbracoApiControllerResolver(
new ActivatorServiceProvider(), Logger,
PluginManager.Current.ResolveUmbracoApiControllers());
ShortStringHelperResolver.Current = new ShortStringHelperResolver(new LegacyShortStringHelper());
base.FreezeResolution();
}
2012-08-14 23:35:34 +06:00
public override void TearDown()
{
base.TearDown();
UmbracoContext.Current = null;
RouteTable.Routes.Clear();
2012-08-14 23:35:34 +06:00
}
Template CreateTemplate(string alias)
{
var path = "template";
var name = "Template";
var template = new Template(path, name, alias);
template.Content = ""; // else saving throws with a dirty internal error
ApplicationContext.Services.FileService.SaveTemplate(template);
return template;
}
/// <summary>
/// Will route to the default controller and action since no custom controller is defined for this node route
/// </summary>
[Test]
public void Umbraco_Route_Umbraco_Defined_Controller_Action()
{
var template = CreateTemplate("homePage");
var route = RouteTable.Routes["Umbraco_default"];
var routeData = new RouteData() { Route = route };
var routingContext = GetRoutingContext("~/dummy-page", template.Id, routeData);
var docRequest = new PublishedContentRequest(routingContext.UmbracoContext.CleanedUmbracoUrl, routingContext)
{
PublishedContent = routingContext.UmbracoContext.ContentCache.GetById(1174),
TemplateModel = template,
RenderingEngine = RenderingEngine.Mvc
};
2015-01-09 11:07:38 +11:00
var handler = new RenderRouteHandler(
new TestControllerFactory(routingContext.UmbracoContext, Mock.Of<ILogger>()),
routingContext.UmbracoContext);
handler.GetHandlerForRoute(routingContext.UmbracoContext.HttpContext.Request.RequestContext, docRequest);
Assert.AreEqual("RenderMvc", routeData.Values["controller"].ToString());
2013-02-20 06:30:06 +06:00
//the route action will still be the one we've asked for because our RenderActionInvoker is the thing that decides
// if the action matches.
Assert.AreEqual("homePage", routeData.Values["action"].ToString());
}
2012-08-14 23:35:34 +06:00
//test all template name styles to match the ActionName
//[TestCase("home-\\234^^*32page")] //TODO: This fails!
2013-02-01 06:22:28 +06:00
[TestCase("home-page")]
[TestCase("home-page")]
[TestCase("home-page")]
[TestCase("Home-Page")]
2012-08-14 23:35:34 +06:00
[TestCase("HomePage")]
[TestCase("homePage")]
[TestCase("site1/template2")]
[TestCase("site1\\template2")]
public void Umbraco_Route_User_Defined_Controller_Action(string templateName)
2012-08-14 23:35:34 +06:00
{
// NOTE - here we create templates with crazy aliases... assuming that these
// could exist in the database... yet creating templates should sanitize
// aliases one way or another...
var template = CreateTemplate(templateName);
var route = RouteTable.Routes["Umbraco_default"];
2012-08-14 23:35:34 +06:00
var routeData = new RouteData() {Route = route};
var routingContext = GetRoutingContext("~/dummy-page", template.Id, routeData, true);
var docRequest = new PublishedContentRequest(routingContext.UmbracoContext.CleanedUmbracoUrl, routingContext)
2012-08-14 23:35:34 +06:00
{
PublishedContent = routingContext.UmbracoContext.ContentCache.GetById(1172),
TemplateModel = template
2012-08-14 23:35:34 +06:00
};
2015-01-09 11:07:38 +11:00
var handler = new RenderRouteHandler(
new TestControllerFactory(routingContext.UmbracoContext, Mock.Of<ILogger>()),
routingContext.UmbracoContext);
2012-08-14 23:35:34 +06:00
handler.GetHandlerForRoute(routingContext.UmbracoContext.HttpContext.Request.RequestContext, docRequest);
Assert.AreEqual("CustomDocument", routeData.Values["controller"].ToString());
Assert.AreEqual(
//global::umbraco.cms.helpers.Casing.SafeAlias(template.Alias),
template.Alias.ToSafeAlias(),
routeData.Values["action"].ToString());
2012-08-14 23:35:34 +06:00
}
#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 CustomDocumentController(UmbracoContext umbracoContext) : base(umbracoContext)
2015-01-09 11:07:38 +11:00
{
}
2012-08-14 23:35:34 +06:00
2015-01-09 11:07:38 +11:00
public ActionResult HomePage(RenderModel model)
2012-08-14 23:35:34 +06:00
{
return View();
}
}
#endregion
}
}