From 2f5616a21c101735cd0b730d588af5fdf15ed954 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 21 Dec 2015 17:09:31 +0100 Subject: [PATCH] Content is now rendering --- .../PublishedContentCacheTests.cs | 5 +- .../TestHelpers/BaseDatabaseFactoryTest.cs | 16 ++-- .../PublishedCache/PublishedCaches.cs | 2 +- .../PublishedContentCache.cs | 75 +++++++++---------- src/Umbraco.Web/WebBootManager.cs | 4 +- 5 files changed, 48 insertions(+), 54 deletions(-) diff --git a/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs b/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs index 3d408b1222..52e4d99136 100644 --- a/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs +++ b/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs @@ -59,10 +59,7 @@ namespace Umbraco.Tests.Cache.PublishedCache SettingsForTests.ConfigureSettings(settings); _xml = new XmlDocument(); _xml.LoadXml(GetXml()); - var cache = new PublishedContentCache - { - GetXmlDelegate = (context, preview) => _xml - }; + var cache = new PublishedContentCache((context, preview) => _xml); _umbracoContext = new UmbracoContext( _httpContextFactory.HttpContext, diff --git a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs index 23000e4880..92dc963dcf 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs @@ -362,15 +362,13 @@ namespace Umbraco.Tests.TestHelpers protected UmbracoContext GetUmbracoContext(string url, int templateId, RouteData routeData = null, bool setSingleton = false) { - var cache = new PublishedContentCache(); - - cache.GetXmlDelegate = (context, preview) => - { - var doc = new XmlDocument(); - doc.LoadXml(GetXmlContent(templateId)); - return doc; - }; - + var cache = new PublishedContentCache((context, preview) => + { + var doc = new XmlDocument(); + doc.LoadXml(GetXmlContent(templateId)); + return doc; + }); + PublishedContentCache.UnitTesting = true; var httpContext = GetHttpContextFactory(url, routeData).HttpContext; diff --git a/src/Umbraco.Web/PublishedCache/PublishedCaches.cs b/src/Umbraco.Web/PublishedCache/PublishedCaches.cs index 287cd740e6..377a891c4f 100644 --- a/src/Umbraco.Web/PublishedCache/PublishedCaches.cs +++ b/src/Umbraco.Web/PublishedCache/PublishedCaches.cs @@ -4,7 +4,7 @@ /// Provides caches (content and media). /// /// Default implementation for unrelated caches. - class PublishedCaches : IPublishedCaches + internal class PublishedCaches : IPublishedCaches { private readonly IPublishedContentCache _contentCache; private readonly IPublishedMediaCache _mediaCache; diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs index cfaf4fee7e..c0304d5de6 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs @@ -22,8 +22,42 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache { internal class PublishedContentCache : IPublishedContentCache { + /// + /// Constructor + /// + /// + /// + /// Use this ctor for unit tests in order to supply a custom xml result + /// + public PublishedContentCache(Func getXmlDelegate) + { + if (getXmlDelegate == null) throw new ArgumentNullException("getXmlDelegate"); + _xmlDelegate = getXmlDelegate; + } + + /// + /// Constructor + /// + /// + /// Using this ctor will ONLY work in a web/http context + /// public PublishedContentCache() - { + { + _xmlDelegate = ((context, preview) => + { + if (preview) + { + var previewContent = PreviewContentCache.GetOrCreateValue(context); // will use the ctor with no parameters + previewContent.EnsureInitialized(context.UmbracoUser, StateHelper.Cookies.Preview.GetValue(), true, () => + { + if (previewContent.ValidPreviewSet) + previewContent.LoadPreviewset(); + }); + if (previewContent.ValidPreviewSet) + return previewContent.XmlContent; + } + return content.Instance.XmlContent; + }); } #region Routes cache @@ -343,46 +377,11 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache static readonly ConditionalWeakTable PreviewContentCache = new ConditionalWeakTable(); - private Func _xmlDelegate; - - /// - /// Gets/sets the delegate used to retrieve the Xml content, generally the setter is only used for unit tests - /// and by default if it is not set will use the standard delegate which ONLY works when in the context an Http Request - /// - /// - /// If not defined, we will use the standard delegate which ONLY works when in the context an Http Request - /// mostly because the 'content' object heavily relies on HttpContext, SQL connections and a bunch of other stuff - /// that when run inside of a unit test fails. - /// - internal Func GetXmlDelegate - { - get - { - return _xmlDelegate ?? (_xmlDelegate = (context, preview) => - { - if (preview) - { - var previewContent = PreviewContentCache.GetOrCreateValue(context); // will use the ctor with no parameters - previewContent.EnsureInitialized(context.UmbracoUser, StateHelper.Cookies.Preview.GetValue(), true, () => - { - if (previewContent.ValidPreviewSet) - previewContent.LoadPreviewset(); - }); - if (previewContent.ValidPreviewSet) - return previewContent.XmlContent; - } - return content.Instance.XmlContent; - }); - } - set - { - _xmlDelegate = value; - } - } + private readonly Func _xmlDelegate; internal XmlDocument GetXml(UmbracoContext umbracoContext, bool preview) { - return GetXmlDelegate(umbracoContext, preview); + return _xmlDelegate(umbracoContext, preview); } #endregion diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index e996d2e46c..541108e157 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -317,9 +317,9 @@ namespace Umbraco.Web container.EnablePerWebRequestScope(); container.Register(new PerContainerLifetime()); - //TODO: Is this lifespan correct? Need to ask Stephen because we have contextual ones too - container.Register(new PerContainerLifetime()); + container.Register(factory => new PublishedContentCache(), new PerContainerLifetime()); container.Register(new PerContainerLifetime()); + //no need to declare as per request, currently we manage it's lifetime as the singleton container.Register(factory => UmbracoContext.Current);