Fixes: U4-3942 Cannot configure the request if there is not content assigned - exception
Conflicts: src/Umbraco.Tests/Umbraco.Tests.csproj
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
using Umbraco.Web.Routing;
|
||||
|
||||
namespace Umbraco.Tests.PublishedContent
|
||||
{
|
||||
[TestFixture]
|
||||
public class PublishedContentRequestEngineTests : BaseRoutingTest
|
||||
{
|
||||
|
||||
protected override DatabaseBehavior DatabaseTestBehavior
|
||||
{
|
||||
get { return DatabaseBehavior.NoDatabasePerFixture; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ctor_Throws_On_Null_PCR()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new PublishedContentRequestEngine(null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConfigureRequest_Returns_False_Without_HasPublishedContent()
|
||||
{
|
||||
var routeCtx = GetRoutingContext("/test");
|
||||
|
||||
var pcre = new PublishedContentRequestEngine(
|
||||
new PublishedContentRequest(
|
||||
routeCtx.UmbracoContext.CleanedUmbracoUrl, routeCtx));
|
||||
|
||||
var result = pcre.ConfigureRequest();
|
||||
Assert.IsFalse(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConfigureRequest_Returns_False_When_IsRedirect()
|
||||
{
|
||||
var routeCtx = GetRoutingContext("/test");
|
||||
|
||||
var pcr = new PublishedContentRequest(routeCtx.UmbracoContext.CleanedUmbracoUrl, routeCtx);
|
||||
var pc = GetPublishedContentMock();
|
||||
pcr.PublishedContent = pc.Object;
|
||||
pcr.Culture = new CultureInfo("en-AU");
|
||||
pcr.SetRedirect("/hello");
|
||||
var pcre = new PublishedContentRequestEngine(pcr);
|
||||
|
||||
var result = pcre.ConfigureRequest();
|
||||
Assert.IsFalse(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConfigureRequest_Adds_HttpContext_Items_When_Published_Content_Assigned()
|
||||
{
|
||||
var routeCtx = GetRoutingContext("/test");
|
||||
|
||||
var pcr = new PublishedContentRequest(routeCtx.UmbracoContext.CleanedUmbracoUrl, routeCtx);
|
||||
var pc = GetPublishedContentMock();
|
||||
pcr.PublishedContent = pc.Object;
|
||||
pcr.Culture = new CultureInfo("en-AU");
|
||||
var pcre = new PublishedContentRequestEngine(pcr);
|
||||
|
||||
pcre.ConfigureRequest();
|
||||
|
||||
Assert.AreEqual(1, routeCtx.UmbracoContext.HttpContext.Items["pageID"]);
|
||||
Assert.AreEqual(pcr.UmbracoPage.Elements.Count, ((Hashtable)routeCtx.UmbracoContext.HttpContext.Items["pageElements"]).Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConfigureRequest_Sets_UmbracoPage_When_Published_Content_Assigned()
|
||||
{
|
||||
var routeCtx = GetRoutingContext("/test");
|
||||
|
||||
var pcr = new PublishedContentRequest(routeCtx.UmbracoContext.CleanedUmbracoUrl, routeCtx);
|
||||
var pc = GetPublishedContentMock();
|
||||
pcr.Culture = new CultureInfo("en-AU");
|
||||
pcr.PublishedContent = pc.Object;
|
||||
var pcre = new PublishedContentRequestEngine(pcr);
|
||||
|
||||
pcre.ConfigureRequest();
|
||||
|
||||
Assert.IsNotNull(pcr.UmbracoPage);
|
||||
}
|
||||
|
||||
private Mock<IPublishedContent> GetPublishedContentMock()
|
||||
{
|
||||
var pc = new Mock<IPublishedContent>();
|
||||
pc.Setup(content => content.Id).Returns(1);
|
||||
pc.Setup(content => content.Name).Returns("test");
|
||||
pc.Setup(content => content.DocumentTypeId).Returns(2);
|
||||
pc.Setup(content => content.DocumentTypeAlias).Returns("testAlias");
|
||||
pc.Setup(content => content.WriterName).Returns("admin");
|
||||
pc.Setup(content => content.CreatorName).Returns("admin");
|
||||
pc.Setup(content => content.CreateDate).Returns(DateTime.Now);
|
||||
pc.Setup(content => content.UpdateDate).Returns(DateTime.Now);
|
||||
pc.Setup(content => content.Path).Returns("-1,1");
|
||||
pc.Setup(content => content.Version).Returns(Guid.NewGuid);
|
||||
pc.Setup(content => content.Parent).Returns(() => null);
|
||||
pc.Setup(content => content.Version).Returns(Guid.NewGuid);
|
||||
pc.Setup(content => content.Properties).Returns(new Collection<IPublishedProperty>());
|
||||
return pc;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -213,6 +213,7 @@
|
||||
<Compile Include="Persistence\Repositories\UserRepositoryTest.cs" />
|
||||
<Compile Include="Persistence\Repositories\UserTypeRepositoryTest.cs" />
|
||||
<Compile Include="PublishedContent\PublishedContentExtensionTests.cs" />
|
||||
<Compile Include="PublishedContent\PublishedContentRequestEngineTests.cs" />
|
||||
<Compile Include="PublishedContent\StronglyTypedModels\CallingMethodTests.cs" />
|
||||
<Compile Include="PublishedContent\StronglyTypedModels\Subpage.cs" />
|
||||
<Compile Include="PublishedContent\StronglyTypedModels\Textpage.cs" />
|
||||
|
||||
@@ -46,7 +46,10 @@ namespace Umbraco.Web.Routing
|
||||
/// <summary>
|
||||
/// Prepares the request.
|
||||
/// </summary>
|
||||
public void PrepareRequest()
|
||||
/// <returns>
|
||||
/// Returns false if the request was not successfully prepared
|
||||
/// </returns>
|
||||
public bool PrepareRequest()
|
||||
{
|
||||
// note - at that point the original legacy module did something do handle IIS custom 404 errors
|
||||
// ie pages looking like /anything.aspx?404;/path/to/document - I guess the reason was to support
|
||||
@@ -62,10 +65,12 @@ namespace Umbraco.Web.Routing
|
||||
|
||||
// if request has been flagged to redirect then return
|
||||
// whoever called us is in charge of actually redirecting
|
||||
if (_pcr.IsRedirect)
|
||||
return;
|
||||
if (_pcr.IsRedirect)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// set the culture on the thread - once, so it's set when running document lookups
|
||||
// set the culture on the thread - once, so it's set when running document lookups
|
||||
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = _pcr.Culture;
|
||||
|
||||
// find the document & template
|
||||
@@ -85,24 +90,25 @@ namespace Umbraco.Web.Routing
|
||||
// to find out the appropriate template
|
||||
|
||||
//complete the PCR and assign the remaining values
|
||||
ConfigureRequest();
|
||||
return ConfigureRequest();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by PrepareRequest once everything has been discovered, resolved and assigned to the PCR. This method
|
||||
/// finalizes the PCR with the values assigned.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Returns false if the request was not successfully configured
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// This method logic has been put into it's own method in case developers have created a custom PCR or are assigning their own values
|
||||
/// but need to finalize it themselves.
|
||||
///
|
||||
/// This method will throw an exception if no content has been assigned
|
||||
/// </remarks>
|
||||
public void ConfigureRequest()
|
||||
public bool ConfigureRequest()
|
||||
{
|
||||
if (_pcr.HasPublishedContent == false)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot configure the request if there is not content assigned");
|
||||
return false;
|
||||
}
|
||||
|
||||
// set the culture on the thread -- again, 'cos it might have changed in the event handler
|
||||
@@ -110,8 +116,10 @@ namespace Umbraco.Web.Routing
|
||||
|
||||
// if request has been flagged to redirect, or has no content to display,
|
||||
// then return - whoever called us is in charge of actually redirecting
|
||||
if (_pcr.IsRedirect || !_pcr.HasPublishedContent)
|
||||
return;
|
||||
if (_pcr.IsRedirect || _pcr.HasPublishedContent == false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// we may be 404 _and_ have a content
|
||||
|
||||
@@ -132,6 +140,8 @@ namespace Umbraco.Web.Routing
|
||||
// used by many legacy objects
|
||||
_routingContext.UmbracoContext.HttpContext.Items["pageID"] = _pcr.PublishedContent.Id;
|
||||
_routingContext.UmbracoContext.HttpContext.Items["pageElements"] = _pcr.UmbracoPage.Elements;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user