From 75f0f1f4ee4fc05f695d184f3051645f6e8f498d Mon Sep 17 00:00:00 2001 From: Stephan Date: Sun, 31 Mar 2013 18:40:54 -0200 Subject: [PATCH] Web.UmbracoContext - allow for InPreviewMode override in tests --- src/Umbraco.Web/UmbracoContext.cs | 34 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 5fd1f249da..deba7751f5 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -114,11 +114,13 @@ namespace Umbraco.Web /// /// The published content cache. /// The published media cache. + /// An optional value overriding detection of preview mode. internal UmbracoContext( HttpContextBase httpContext, ApplicationContext applicationContext, IPublishedContentCache contentCache, - IPublishedMediaCache mediaCache) + IPublishedMediaCache mediaCache, + bool? preview = null) { if (httpContext == null) throw new ArgumentNullException("httpContext"); if (applicationContext == null) throw new ArgumentNullException("applicationContext"); @@ -131,6 +133,7 @@ namespace Umbraco.Web ContentCache = new ContextualPublishedContentCache(contentCache, this); MediaCache = new ContextualPublishedMediaCache(mediaCache, this); + InPreviewMode = preview ?? DetectInPreviewModeFromRequest(); // set the urls... //original request url @@ -319,22 +322,21 @@ namespace Umbraco.Web /// /// Determines whether the current user is in a preview mode and browsing the site (ie. not in the admin UI) /// - public bool InPreviewMode - { - get - { - var request = GetRequestFromContext(); - if (request == null || request.Url == null) - return false; + public bool InPreviewMode { get; private set; } - var currentUrl = request.Url.AbsolutePath; - // zb-00004 #29956 : refactor cookies names & handling - return - StateHelper.Cookies.Preview.HasValue // has preview cookie - && UmbracoUser != null // has user - && !currentUrl.StartsWith(Umbraco.Core.IO.IOHelper.ResolveUrl(Umbraco.Core.IO.SystemDirectories.Umbraco)); // is not in admin UI - } - } + private bool DetectInPreviewModeFromRequest() + { + var request = GetRequestFromContext(); + if (request == null || request.Url == null) + return false; + + var currentUrl = request.Url.AbsolutePath; + // zb-00004 #29956 : refactor cookies names & handling + return + StateHelper.Cookies.Preview.HasValue // has preview cookie + && UmbracoUser != null // has user + && !currentUrl.StartsWith(Umbraco.Core.IO.IOHelper.ResolveUrl(Umbraco.Core.IO.SystemDirectories.Umbraco)); // is not in admin UI + } private HttpRequestBase GetRequestFromContext() {