diff --git a/src/Umbraco.Tests/DocumentLookups/BaseRoutingTest.cs b/src/Umbraco.Tests/Routing/BaseRoutingTest.cs similarity index 80% rename from src/Umbraco.Tests/DocumentLookups/BaseRoutingTest.cs rename to src/Umbraco.Tests/Routing/BaseRoutingTest.cs index c0b4bd17b1..ea99d55bad 100644 --- a/src/Umbraco.Tests/DocumentLookups/BaseRoutingTest.cs +++ b/src/Umbraco.Tests/Routing/BaseRoutingTest.cs @@ -1,29 +1,27 @@ using System.Configuration; using System.Linq; using System.Web.Routing; -using System.Xml; using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.ObjectResolution; using Umbraco.Tests.Stubs; using Umbraco.Tests.TestHelpers; using Umbraco.Web; using Umbraco.Web.Routing; -using umbraco.BusinessLogic; -using umbraco.cms.businesslogic.cache; using umbraco.cms.businesslogic.template; -namespace Umbraco.Tests.DocumentLookups +namespace Umbraco.Tests.Routing { [TestFixture, RequiresSTA] public abstract class BaseRoutingTest : BaseWebTest { public override void Initialize() - { + { + base.Initialize(); } public override void TearDown() { + base.TearDown(); + ConfigurationManager.AppSettings.Set("umbracoHideTopLevelNodeFromPath", ""); } diff --git a/src/Umbraco.Tests/DocumentLookups/LookupByAliasTests.cs b/src/Umbraco.Tests/Routing/LookupByAliasTests.cs similarity index 91% rename from src/Umbraco.Tests/DocumentLookups/LookupByAliasTests.cs rename to src/Umbraco.Tests/Routing/LookupByAliasTests.cs index 1f26a0c50f..0f71362b78 100644 --- a/src/Umbraco.Tests/DocumentLookups/LookupByAliasTests.cs +++ b/src/Umbraco.Tests/Routing/LookupByAliasTests.cs @@ -1,10 +1,9 @@ -using System.Configuration; using NUnit.Framework; using Umbraco.Web.Routing; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.template; -namespace Umbraco.Tests.DocumentLookups +namespace Umbraco.Tests.Routing { [TestFixture] public class LookupByAliasTests : BaseRoutingTest diff --git a/src/Umbraco.Tests/DocumentLookups/LookupByIdTests.cs b/src/Umbraco.Tests/Routing/LookupByIdTests.cs similarity index 93% rename from src/Umbraco.Tests/DocumentLookups/LookupByIdTests.cs rename to src/Umbraco.Tests/Routing/LookupByIdTests.cs index 04d028ceb0..cee11afc1e 100644 --- a/src/Umbraco.Tests/DocumentLookups/LookupByIdTests.cs +++ b/src/Umbraco.Tests/Routing/LookupByIdTests.cs @@ -3,7 +3,7 @@ using Umbraco.Web.Routing; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.template; -namespace Umbraco.Tests.DocumentLookups +namespace Umbraco.Tests.Routing { [TestFixture] public class LookupByIdTests : BaseRoutingTest diff --git a/src/Umbraco.Tests/DocumentLookups/LookupByNiceUrlAndTemplateTests.cs b/src/Umbraco.Tests/Routing/LookupByNiceUrlAndTemplateTests.cs similarity index 94% rename from src/Umbraco.Tests/DocumentLookups/LookupByNiceUrlAndTemplateTests.cs rename to src/Umbraco.Tests/Routing/LookupByNiceUrlAndTemplateTests.cs index 60bf7962ac..562dc31999 100644 --- a/src/Umbraco.Tests/DocumentLookups/LookupByNiceUrlAndTemplateTests.cs +++ b/src/Umbraco.Tests/Routing/LookupByNiceUrlAndTemplateTests.cs @@ -3,7 +3,7 @@ using Umbraco.Web.Routing; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.template; -namespace Umbraco.Tests.DocumentLookups +namespace Umbraco.Tests.Routing { [TestFixture] public class LookupByNiceUrlAndTemplateTests : BaseRoutingTest diff --git a/src/Umbraco.Tests/DocumentLookups/LookupByNiceUrlTests.cs b/src/Umbraco.Tests/Routing/LookupByNiceUrlTests.cs similarity index 94% rename from src/Umbraco.Tests/DocumentLookups/LookupByNiceUrlTests.cs rename to src/Umbraco.Tests/Routing/LookupByNiceUrlTests.cs index d41ae6a3fa..e3c05e14b1 100644 --- a/src/Umbraco.Tests/DocumentLookups/LookupByNiceUrlTests.cs +++ b/src/Umbraco.Tests/Routing/LookupByNiceUrlTests.cs @@ -1,11 +1,10 @@ using System.Configuration; -using System.Web; using NUnit.Framework; using Umbraco.Web.Routing; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.template; -namespace Umbraco.Tests.DocumentLookups +namespace Umbraco.Tests.Routing { [TestFixture] public class LookupByNiceUrlTests : BaseRoutingTest diff --git a/src/Umbraco.Tests/DocumentLookups/RenderRouteHandlerTests.cs b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs similarity index 92% rename from src/Umbraco.Tests/DocumentLookups/RenderRouteHandlerTests.cs rename to src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs index a1c2bf51cb..05d4965bdf 100644 --- a/src/Umbraco.Tests/DocumentLookups/RenderRouteHandlerTests.cs +++ b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs @@ -1,18 +1,14 @@ -using System.Linq; -using System.Web.Mvc; +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 +namespace Umbraco.Tests.Routing { [TestFixture] public class RenderRouteHandlerTests : BaseRoutingTest diff --git a/src/Umbraco.Tests/DocumentLookups/RouteTestExtensions.cs b/src/Umbraco.Tests/Routing/RouteTestExtensions.cs similarity index 94% rename from src/Umbraco.Tests/DocumentLookups/RouteTestExtensions.cs rename to src/Umbraco.Tests/Routing/RouteTestExtensions.cs index 404e9ea4b2..09c1635f63 100644 --- a/src/Umbraco.Tests/DocumentLookups/RouteTestExtensions.cs +++ b/src/Umbraco.Tests/Routing/RouteTestExtensions.cs @@ -3,7 +3,7 @@ using System.Web.Routing; using Rhino.Mocks; using Umbraco.Tests.TestHelpers; -namespace Umbraco.Tests.DocumentLookups +namespace Umbraco.Tests.Routing { public static class RouteTestExtensions { diff --git a/src/Umbraco.Tests/UmbracoModuleTests.cs b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs similarity index 56% rename from src/Umbraco.Tests/UmbracoModuleTests.cs rename to src/Umbraco.Tests/Routing/UmbracoModuleTests.cs index d3f8c2f3ca..8c95076684 100644 --- a/src/Umbraco.Tests/UmbracoModuleTests.cs +++ b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs @@ -1,50 +1,38 @@ using System; -using System.Collections.Generic; using System.Configuration; using System.IO; -using System.Linq; -using System.Threading; using System.Xml; using NUnit.Framework; -using SqlCE4Umbraco; using Umbraco.Core; using Umbraco.Tests.TestHelpers; using Umbraco.Web; -using Umbraco.Web.Media.ThumbnailProviders; -using Umbraco.Web.Routing; -using umbraco.BusinessLogic; -using umbraco.DataLayer; using umbraco.IO; using umbraco.cms.businesslogic.cache; -using umbraco.cms.businesslogic.language; using umbraco.cms.businesslogic.template; -using umbraco.cms.businesslogic.web; -using GlobalSettings = umbraco.GlobalSettings; -namespace Umbraco.Tests +namespace Umbraco.Tests.Routing { [TestFixture, RequiresSTA] - public class UmbracoModuleTests + public class UmbracoModuleTests : BaseRoutingTest { private UmbracoModule _module; - [SetUp] - public void Initialize() + public override void Initialize() { - TestHelper.SetupLog4NetForTests(); - ApplicationContext.Current = new ApplicationContext() - { - IsReady = true - }; + base.Initialize(); + + //the module requires that the singleton is setup + ApplicationContext.Current = ApplicationContext; + + //create the module _module = new UmbracoModule(); + ConfigurationManager.AppSettings.Set("umbracoConfigurationStatus", Umbraco.Core.Configuration.GlobalSettings.CurrentVersion); ConfigurationManager.AppSettings.Set("umbracoReservedPaths", "~/umbraco,~/install/"); ConfigurationManager.AppSettings.Set("umbracoReservedUrls", "~/config/splashes/booting.aspx,~/install/default.aspx,~/config/splashes/noNodes.aspx,~/VSEnterpriseHelper.axd"); - Cache.ClearAllCache(); - TestHelper.InitializeDatabase(); //create the not found handlers config - using(var sw = File.CreateText(IOHelper.MapPath(SystemFiles.NotFoundhandlersConfig, false))) + using (var sw = File.CreateText(Umbraco.Core.IO.IOHelper.MapPath(Umbraco.Core.IO.SystemFiles.NotFoundhandlersConfig, false))) { sw.Write(@" @@ -55,56 +43,16 @@ namespace Umbraco.Tests } } - [TearDown] - public void TearDown() + public override void TearDown() { + base.TearDown(); + _module.DisposeIfDisposable(); - //reset the context on global settings - Umbraco.Core.Configuration.GlobalSettings.HttpContext = null; - //reset the app context - ApplicationContext.Current = null; + //reset the app config ConfigurationManager.AppSettings.Set("umbracoConfigurationStatus", ""); ConfigurationManager.AppSettings.Set("umbracoReservedPaths", ""); - ConfigurationManager.AppSettings.Set("umbracoReservedUrls", ""); - TestHelper.ClearDatabase(); - Cache.ClearAllCache(); - } - - /// - /// Initlializes the UmbracoContext with specific XML - /// - /// - /// - private void SetupUmbracoContextForTest(UmbracoContext umbracoContext, Template template) - { - umbracoContext.GetXmlDelegate = () => - { - var xDoc = new XmlDocument(); - - //create a custom xml structure to return - - xDoc.LoadXml(@" - - -]> - - - - - - - - - - - - -"); - //return the custom x doc - return xDoc; - }; + ConfigurationManager.AppSettings.Set("umbracoReservedUrls", ""); } [TestCase("/umbraco_client/Tree/treeIcons.css", false)] @@ -145,6 +93,9 @@ namespace Umbraco.Tests Assert.AreEqual(assert, result); } + + + //NOTE: This test shows how we can test most of the HttpModule, it however is testing a method that no longer exists and is testing too much, // we need to write unit tests for each of the components: NiceUrlProvider, all of the Lookup classes, etc... // to ensure that each one is individually tested. diff --git a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs index 96e4da64f6..7211e81441 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs @@ -27,6 +27,8 @@ namespace Umbraco.Tests.TestHelpers [TearDown] public virtual void TearDown() { + //reset the app context + ApplicationContext.Current = null; //reset the context on global settings Umbraco.Core.Configuration.GlobalSettings.HttpContext = null; Resolution.IsFrozen = false; diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index a2eeb8ce18..321598df4e 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -64,13 +64,13 @@ - - - - - - - + + + + + + + @@ -99,7 +99,7 @@ - + diff --git a/src/Umbraco.Web/umbraco.presentation/default.aspx.cs b/src/Umbraco.Web/umbraco.presentation/default.aspx.cs index 8971584368..0bc077cc83 100644 --- a/src/Umbraco.Web/umbraco.presentation/default.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/default.aspx.cs @@ -25,8 +25,8 @@ namespace umbraco /// public class UmbracoDefault : Page { - private page _upage = null; - private DocumentRequest _docRequest = null; + private page _upage; + private DocumentRequest _docRequest; bool _validateRequest = true; /// @@ -39,69 +39,53 @@ namespace umbraco set { _validateRequest = value; } } - // fixme - switch over to OnPreInit override - void Page_PreInit(Object sender, EventArgs e) + protected override void OnPreInit(EventArgs e) { + base.OnPreInit(e); // get the document request and the page _docRequest = UmbracoContext.Current.DocumentRequest; - _upage = _docRequest.GetUmbracoPage(); + _upage = _docRequest.GetUmbracoPage(); + + var args = new RequestInitEventArgs() + { + Page = _upage, + PageId = _upage.PageID, + Context = Context + }; + FireBeforeRequestInit(args); + + //if we are cancelling then return and don't proceed + if (args.Cancel) return; + var templatePath = SystemDirectories.Masterpages + "/" + _docRequest.Template.Alias.Replace(" ", "") + ".master"; // fixme - should be in .Template! this.MasterPageFile = templatePath; // set the template - + // reset the friendly path so it's used by forms, etc. Context.RewritePath(UmbracoContext.Current.RequestUrl.PathAndQuery); + + FireAfterRequestInit(args); } - //SD: I'm nearly positive that this is taken care of in our DefaultLastChanceLookup class! + protected override void OnInit(EventArgs e) + { + base.OnInit(e); - //void OnPreInitLegacy() - //{ - // if (_upage.Template == 0) - // { - // string custom404 = umbraco.library.GetCurrentNotFoundPageId(); - // if (!String.IsNullOrEmpty(custom404)) - // { - // XmlNode xmlNodeNotFound = content.Instance.XmlContent.GetElementById(custom404); - // if (xmlNodeNotFound != null) - // { - // _upage = new page(xmlNodeNotFound); - // } - // } - // } + //This is only here for legacy if people arent' using master pages... + //TODO: We need to test that this still works!! Or do we ?? + if (!UmbracoSettings.UseAspNetMasterPages) + { + var pageHolder = new umbraco.layoutControls.umbracoPageHolder + { + ID = "umbPageHolder" + }; + Page.Controls.Add(pageHolder); + _upage.RenderPage(_upage.Template); + var umbPageHolder = (layoutControls.umbracoPageHolder)Page.FindControl("umbPageHolder"); + umbPageHolder.Populate(_upage); + } + } - // if (_upage.Template != 0) - // { - // this.MasterPageFile = template.GetMasterPageName(_upage.Template); - - // string cultureAlias = null; - // for (int i = _upage.SplitPath.Length - 1; i > 0; i--) - // { - // var domains = Domain.GetDomainsById(int.Parse(_upage.SplitPath[i])); - // if (domains.Length > 0) - // { - // cultureAlias = domains[0].Language.CultureAlias; - // break; - // } - // } - - // if (cultureAlias != null) - // { - // LogHelper.Debug("Culture changed to " + cultureAlias, Context.Trace); - // var culture = new System.Globalization.CultureInfo(cultureAlias); - // Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = culture; - // } - // } - // else - // { - // Response.StatusCode = 404; - // RenderNotFound(); - // Response.End(); - // } - //} - - // - protected override void OnLoad(EventArgs e) { base.OnLoad(e); @@ -112,7 +96,6 @@ namespace umbraco Page.Trace.IsEnabled &= GlobalSettings.DebugMode && !String.IsNullOrWhiteSpace(Request["umbDebugShowTrace"]); } - // protected override void Render(HtmlTextWriter writer) { // do the original rendering @@ -148,21 +131,45 @@ namespace umbraco writer.Write(text); } - ////TODO: This should be removed, we should be handling all 404 stuff in the module and executing the - //// DocumentNotFoundHttpHandler instead but we need to fix the above routing concerns so that this all - //// takes place in the Module. - //void RenderNotFound() - //{ - // Context.Response.StatusCode = 404; + /// + /// The preinit event handler + /// + public delegate void RequestInitEventHandler(object sender, RequestInitEventArgs e); + /// + /// occurs before the umbraco page is initialized for rendering. + /// + public static event RequestInitEventHandler BeforeRequestInit; + /// + /// Raises the event. + /// + /// The instance containing the event data. + protected internal virtual void FireBeforeRequestInit(RequestInitEventArgs e) + { + if (BeforeRequestInit != null) + BeforeRequestInit(this, e); + } - // Response.Write("

Page not found

"); - // UmbracoContext.Current.HttpContext.Response.Write("

No umbraco document matches the url '" + HttpUtility.HtmlEncode(UmbracoContext.Current.ClientUrl) + "'.

"); + /// + /// Occurs when [after save]. + /// + public static event RequestInitEventHandler AfterRequestInit; + /// + /// Raises the event. + /// + /// The instance containing the event data. + protected virtual void FireAfterRequestInit(RequestInitEventArgs e) + { + if (AfterRequestInit != null) + AfterRequestInit(this, e); - // // fixme - should try to get infos from the DocumentRequest? + } + } - // Response.Write("

This page can be replaced with a custom 404. Check the documentation for \"custom 404\".

"); - // Response.Write("

This page is intentionally left ugly ;-)

"); - // Response.Write(""); - //} + public class RequestInitEventArgs : System.ComponentModel.CancelEventArgs + { + public page Page { get; internal set; } + public HttpContext Context { get; internal set; } + public string Url { get; internal set; } + public int PageId { get; internal set; } } }