diff --git a/src/Umbraco.Core/ApplicationContext.cs b/src/Umbraco.Core/ApplicationContext.cs index 0aceac24bf..f42e4723fc 100644 --- a/src/Umbraco.Core/ApplicationContext.cs +++ b/src/Umbraco.Core/ApplicationContext.cs @@ -338,7 +338,7 @@ namespace Umbraco.Core this.ApplicationCache = null; if (_databaseContext != null) //need to check the internal field here { - if (DatabaseContext.IsDatabaseConfigured) + if (DatabaseContext.IsDatabaseConfigured && DatabaseContext.Database != null) { DatabaseContext.Database.Dispose(); } diff --git a/src/Umbraco.Tests/MockTests.cs b/src/Umbraco.Tests/MockTests.cs index 6a91efcb7b..e7fdfea54d 100644 --- a/src/Umbraco.Tests/MockTests.cs +++ b/src/Umbraco.Tests/MockTests.cs @@ -13,6 +13,7 @@ using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Core.Profiling; using Umbraco.Core.Services; using Moq; +using Umbraco.Tests.TestHelpers; using Umbraco.Web; namespace Umbraco.Tests @@ -31,42 +32,7 @@ namespace Umbraco.Tests [Test] public void Can_Create_Service_Context() { - var svcCtx = new ServiceContext( - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new PackagingService( - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), Mock.Of(), Mock.Of(), Mock.Of()), - new Mock().Object), - new Mock().Object, - new RelationService( - new Mock().Object, - new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), Mock.Of(), Mock.Of(), Mock.Of()), - Mock.Of(), - new Mock().Object), - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - Mock.Of(), - Mock.Of(), - Mock.Of()); + var svcCtx = MockHelper.GetMockedServiceContext(); Assert.Pass(); } @@ -82,42 +48,7 @@ namespace Umbraco.Tests { var appCtx = new ApplicationContext( new DatabaseContext(new Mock().Object, Mock.Of(), Mock.Of(), "test"), - new ServiceContext( - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new PackagingService( - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), Mock.Of(), Mock.Of(), Mock.Of()), - new Mock().Object), - new Mock().Object, - new RelationService( - new Mock().Object, - new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), Mock.Of(), Mock.Of(), Mock.Of()), - Mock.Of(), - new Mock().Object), - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - new Mock().Object, - Mock.Of(), - Mock.Of(), - Mock.Of()), + MockHelper.GetMockedServiceContext(), CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(Mock.Of(), Mock.Of())); @@ -150,6 +81,7 @@ namespace Umbraco.Tests var umbCtx = UmbracoContext.EnsureContext( new Mock().Object, appCtx, + Mock.Of(), true); Assert.AreEqual(umbCtx, UmbracoContext.Current); diff --git a/src/Umbraco.Tests/Mvc/UmbracoViewPageTests.cs b/src/Umbraco.Tests/Mvc/UmbracoViewPageTests.cs index f5ffee1ce3..9c76cbefe0 100644 --- a/src/Umbraco.Tests/Mvc/UmbracoViewPageTests.cs +++ b/src/Umbraco.Tests/Mvc/UmbracoViewPageTests.cs @@ -6,7 +6,15 @@ using System.Text; using System.Web.Mvc; using System.Web.Routing; using System.Xml; +using Moq; using NUnit.Framework; +using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.SqlSyntax; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Profiling; +using Umbraco.Core.Services; using Umbraco.Web.Security; using umbraco.BusinessLogic; using Umbraco.Core; @@ -366,11 +374,56 @@ namespace Umbraco.Tests.Mvc #region Test helpers + ServiceContext GetServiceContext(IUmbracoSettingsSection umbracoSettings, ILogger logger) + { + var svcCtx = new ServiceContext( + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new PackagingService( + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), logger, Mock.Of(), umbracoSettings), + new Mock().Object), + new Mock().Object, + new RelationService( + new Mock().Object, + new RepositoryFactory(CacheHelper.CreateDisabledCacheHelper(), logger, Mock.Of(), umbracoSettings), + logger, + new Mock().Object), + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + Mock.Of(), + Mock.Of(), + Mock.Of()); + return svcCtx; + } + ViewContext GetViewContext() { - var umbracoContext = GetUmbracoContext("/dang", 0); + var settings = SettingsForTests.GetDefault(); + var logger = Mock.Of(); + var umbracoContext = GetUmbracoContext( + logger, settings, + "/dang", 0); - var urlProvider = new UrlProvider(umbracoContext, new IUrlProvider[] { new DefaultUrlProvider() }); + var urlProvider = new UrlProvider(umbracoContext, settings.WebRouting, new IUrlProvider[] { new DefaultUrlProvider() }); var routingContext = new RoutingContext( umbracoContext, Enumerable.Empty(), @@ -389,7 +442,7 @@ namespace Umbraco.Tests.Mvc return context; } - protected UmbracoContext GetUmbracoContext(string url, int templateId, RouteData routeData = null, bool setSingleton = false) + protected UmbracoContext GetUmbracoContext(ILogger logger, IUmbracoSettingsSection umbracoSettings, string url, int templateId, RouteData routeData = null, bool setSingleton = false) { var cache = new PublishedContentCache(); @@ -403,7 +456,14 @@ namespace Umbraco.Tests.Mvc //PublishedContentCache.UnitTesting = true; // ApplicationContext.Current = new ApplicationContext(false) { IsReady = true }; - var appCtx = new ApplicationContext(CacheHelper.CreateDisabledCacheHelper()) { IsReady = true }; + var svcCtx = GetServiceContext(umbracoSettings, logger); + + var appCtx = new ApplicationContext( + new DatabaseContext(Mock.Of(), logger, Mock.Of(), "test"), + svcCtx, + CacheHelper.CreateDisabledCacheHelper(), + new ProfilingLogger(logger, Mock.Of())) { IsReady = true }; + var http = GetHttpContextFactory(url, routeData).HttpContext; var ctx = new UmbracoContext( GetHttpContextFactory(url, routeData).HttpContext, diff --git a/src/Umbraco.Tests/Routing/ContentFinderByAliasTests.cs b/src/Umbraco.Tests/Routing/ContentFinderByAliasTests.cs index 60fee894d3..32df304872 100644 --- a/src/Umbraco.Tests/Routing/ContentFinderByAliasTests.cs +++ b/src/Umbraco.Tests/Routing/ContentFinderByAliasTests.cs @@ -1,12 +1,12 @@ using NUnit.Framework; -using Umbraco.Tests.TestHelpers; using Umbraco.Web.Routing; namespace Umbraco.Tests.Routing { + //TODO: We should be able to decouple this from the base db tests since we're just mocking the services now [TestFixture] - public class ContentFinderByAliasTests : BaseRoutingTest + public class ContentFinderByAliasTests : UrlRoutingTestBase { [TestCase("/this/is/my/alias", 1001)] [TestCase("/anotheralias", 1001)] diff --git a/src/Umbraco.Tests/Routing/ContentFinderByAliasWithDomainsTests.cs b/src/Umbraco.Tests/Routing/ContentFinderByAliasWithDomainsTests.cs index b83f0bfd2b..1767a1336e 100644 --- a/src/Umbraco.Tests/Routing/ContentFinderByAliasWithDomainsTests.cs +++ b/src/Umbraco.Tests/Routing/ContentFinderByAliasWithDomainsTests.cs @@ -1,51 +1,13 @@ -using System.Linq; using NUnit.Framework; -using Umbraco.Tests.TestHelpers; using Umbraco.Web.Routing; -using umbraco.cms.businesslogic.language; -using umbraco.cms.businesslogic.web; namespace Umbraco.Tests.Routing { - [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)] + [TestFixture] public class ContentFinderByAliasWithDomainsTests : ContentFinderByAliasTests { - public override void Initialize() - { - base.Initialize(); - // ensure we can create them although the content is not in the database - TestHelper.DropForeignKeys("umbracoDomains"); - - InitializeLanguagesAndDomains(); - } - - void InitializeLanguagesAndDomains() - { - var domains = Domain.GetDomains(); - foreach (var d in domains) - d.Delete(); - - var langs = Language.GetAllAsList(); - foreach (var l in langs.Skip(1)) - l.Delete(); - - // en-US is there by default - Language.MakeNew("fr-FR"); - Language.MakeNew("de-DE"); - } - - void SetDomains1() - { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); - var langDe = Language.GetByCultureCode("de-DE"); - - Domain.MakeNew("domain1.com/", 1001, langDe.id); - Domain.MakeNew("domain1.com/en", 10011, langEn.id); - Domain.MakeNew("domain1.com/fr", 10012, langFr.id); - } [TestCase("http://domain1.com/this/is/my/alias", "de-DE", -1001)] // alias to domain's page fails - no alias on domain's home @@ -60,7 +22,7 @@ namespace Umbraco.Tests.Routing [TestCase("http://domain1.com/en/bar/nil", "en-US", 100111)] // ok, alias includes "en/" public void Lookup_By_Url_Alias_And_Domain(string inputUrl, string expectedCulture, int expectedNode) { - SetDomains1(); + //SetDomains1(); var routingContext = GetRoutingContext(inputUrl); var url = routingContext.UmbracoContext.CleanedUmbracoUrl; //very important to use the cleaned up umbraco url diff --git a/src/Umbraco.Tests/Routing/ContentFinderByNiceUrlWithDomainsTests.cs b/src/Umbraco.Tests/Routing/ContentFinderByNiceUrlWithDomainsTests.cs index 15912a8c5d..1a1c116eca 100644 --- a/src/Umbraco.Tests/Routing/ContentFinderByNiceUrlWithDomainsTests.cs +++ b/src/Umbraco.Tests/Routing/ContentFinderByNiceUrlWithDomainsTests.cs @@ -2,62 +2,78 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Moq; using NUnit.Framework; +using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Web.Routing; using umbraco.cms.businesslogic.web; -using umbraco.cms.businesslogic.language; using System.Configuration; namespace Umbraco.Tests.Routing { - [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)] + [TestFixture] - public class ContentFinderByNiceUrlWithDomainsTests : BaseRoutingTest + public class ContentFinderByNiceUrlWithDomainsTests : UrlRoutingTestBase { - public override void Initialize() - { - base.Initialize(); + //public override void Initialize() + //{ + // base.Initialize(); - // ensure we can create them although the content is not in the database - TestHelper.DropForeignKeys("umbracoDomains"); + // // ensure we can create them although the content is not in the database + // TestHelper.DropForeignKeys("umbracoDomains"); - InitializeLanguagesAndDomains(); - } + // InitializeLanguagesAndDomains(); + //} - void InitializeLanguagesAndDomains() - { - var domains = Domain.GetDomains(); - foreach (var d in domains) - d.Delete(); + //void InitializeLanguagesAndDomains() + //{ + // var domains = Domain.GetDomains(); + // foreach (var d in domains) + // d.Delete(); - var langs = Language.GetAllAsList(); - foreach (var l in langs.Skip(1)) - l.Delete(); + // var langs = Language.GetAllAsList(); + // foreach (var l in langs.Skip(1)) + // l.Delete(); - Language.MakeNew("fr-FR"); - } + // Language.MakeNew("fr-FR"); + //} void SetDomains3() { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "domain1.com/", Language = new Language("de-DE"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}} + }); - Domain.MakeNew("domain1.com/", 1001, langEn.id); + //var langEn = Language.GetByCultureCode("en-US"); + //var langFr = Language.GetByCultureCode("fr-FR"); + + //Domain.MakeNew("domain1.com/", 1001, langEn.id); } void SetDomains4() { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "domain1.com/", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}}, + new UmbracoDomain {Id = 1, DomainName = "domain1.com/en", Language = new Language("en-US"), RootContent = new Content("test2", -1, new ContentType(-1)) {Id = 10011}}, + new UmbracoDomain {Id = 1, DomainName = "domain1.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test3", -1, new ContentType(-1)) {Id = 10012}}, + new UmbracoDomain {Id = 1, DomainName = "domain3.com/", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1003}}, + new UmbracoDomain {Id = 1, DomainName = "domain3.com/en", Language = new Language("en-US"), RootContent = new Content("test2", -1, new ContentType(-1)) {Id = 10031}}, + new UmbracoDomain {Id = 1, DomainName = "domain3.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test3", -1, new ContentType(-1)) {Id = 10032}} + }); - Domain.MakeNew("domain1.com/", 1001, langEn.id); - Domain.MakeNew("domain1.com/en", 10011, langEn.id); - Domain.MakeNew("domain1.com/fr", 10012, langFr.id); + //var langEn = Language.GetByCultureCode("en-US"); + //var langFr = Language.GetByCultureCode("fr-FR"); - Domain.MakeNew("http://domain3.com/", 1003, langEn.id); - Domain.MakeNew("http://domain3.com/en", 10031, langEn.id); - Domain.MakeNew("http://domain3.com/fr", 10032, langFr.id); + //Domain.MakeNew("domain1.com/", 1001, langEn.id); + //Domain.MakeNew("domain1.com/en", 10011, langEn.id); + //Domain.MakeNew("domain1.com/fr", 10012, langFr.id); + + //Domain.MakeNew("http://domain3.com/", 1003, langEn.id); + //Domain.MakeNew("http://domain3.com/en", 10031, langEn.id); + //Domain.MakeNew("http://domain3.com/fr", 10032, langFr.id); } protected override string GetXmlContent(int templateId) diff --git a/src/Umbraco.Tests/Routing/DomainsAndCulturesTests.cs b/src/Umbraco.Tests/Routing/DomainsAndCulturesTests.cs index 27b00cf676..2acadbb830 100644 --- a/src/Umbraco.Tests/Routing/DomainsAndCulturesTests.cs +++ b/src/Umbraco.Tests/Routing/DomainsAndCulturesTests.cs @@ -2,72 +2,43 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Moq; using NUnit.Framework; +using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Web.Routing; using umbraco.cms.businesslogic.web; -using umbraco.cms.businesslogic.language; +using System.Configuration; namespace Umbraco.Tests.Routing { - [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)] [TestFixture] - class DomainsAndCulturesTests : BaseRoutingTest + class DomainsAndCulturesTests : UrlRoutingTestBase { - public override void Initialize() - { - base.Initialize(); - - // ensure we can create them although the content is not in the database - TestHelper.DropForeignKeys("umbracoDomains"); - - InitializeLanguagesAndDomains(); - } - - void InitializeLanguagesAndDomains() - { - var domains = Domain.GetDomains(true); // we want wildcards too here - foreach (var d in domains) - d.Delete(); - - var langs = Language.GetAllAsList(); - foreach (var l in langs.Skip(1)) - l.Delete(); - - // en-US is there by default - Language.MakeNew("fr-FR"); - Language.MakeNew("de-DE"); - - Language.MakeNew("da-DK"); - Language.MakeNew("cs-CZ"); - Language.MakeNew("nl-NL"); - } - void SetDomains1() { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); - var langDe = Language.GetByCultureCode("de-DE"); - - Domain.MakeNew("domain1.com/", 1001, langDe.id); - Domain.MakeNew("domain1.com/en", 10011, langEn.id); - Domain.MakeNew("domain1.com/fr", 10012, langFr.id); + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "domain1.com/", Language = new Language("de-DE"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}}, + new UmbracoDomain {Id = 1, DomainName = "domain1.com/en", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}}, + new UmbracoDomain {Id = 1, DomainName = "domain1.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10012}} + }); } void SetDomains2() { - SetDomains1(); - - var langDk = Language.GetByCultureCode("da-DK"); - var langCz = Language.GetByCultureCode("cs-CZ"); - var langNl = Language.GetByCultureCode("nl-NL"); - - Domain.MakeNew("*1001", 1001, langDk.id); - Domain.MakeNew("*10011", 10011, langCz.id); - Domain.MakeNew("*100112", 100112, langNl.id); - Domain.MakeNew("*1001122", 1001122, langDk.id); - Domain.MakeNew("*10012", 10012, langNl.id); - Domain.MakeNew("*10031", 10031, langNl.id); + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "domain1.com/", Language = new Language("de-DE"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}}, + new UmbracoDomain {Id = 1, DomainName = "domain1.com/en", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}}, + new UmbracoDomain {Id = 1, DomainName = "domain1.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10012}}, + new UmbracoDomain {Id = 1, DomainName = "*1001", Language = new Language("de-DE"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}}, + new UmbracoDomain {Id = 1, DomainName = "*10011", Language = new Language("cs-CZ"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}}, + new UmbracoDomain {Id = 1, DomainName = "*100112", Language = new Language("nl-NL"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 100112}}, + new UmbracoDomain {Id = 1, DomainName = "*1001122", Language = new Language("da-DK"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001122}}, + new UmbracoDomain {Id = 1, DomainName = "*10012", Language = new Language("nl-NL"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10012}}, + new UmbracoDomain {Id = 1, DomainName = "*10031", Language = new Language("nl-NL"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10031}} + }); } protected override string GetXmlContent(int templateId) diff --git a/src/Umbraco.Tests/Routing/NiceUrlsProviderWithDomainsTests.cs b/src/Umbraco.Tests/Routing/NiceUrlsProviderWithDomainsTests.cs index 670de640ff..48d67deb28 100644 --- a/src/Umbraco.Tests/Routing/NiceUrlsProviderWithDomainsTests.cs +++ b/src/Umbraco.Tests/Routing/NiceUrlsProviderWithDomainsTests.cs @@ -1,107 +1,80 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using Moq; using NUnit.Framework; using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Web.PublishedCache.XmlPublishedCache; using Umbraco.Web.Routing; using umbraco.cms.businesslogic.web; -using umbraco.cms.businesslogic.language; - +using System.Configuration; namespace Umbraco.Tests.Routing { - [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)] [TestFixture] - public class NiceUrlsProviderWithDomainsTests : BaseRoutingTest + public class NiceUrlsProviderWithDomainsTests : UrlRoutingTestBase { - public override void Initialize() - { - base.Initialize(); - - //generate new mock settings and assign so we can configure in individual tests - _umbracoSettings = SettingsForTests.GenerateMockSettings(); - SettingsForTests.ConfigureSettings(_umbracoSettings); - - // ensure we can create them although the content is not in the database - TestHelper.DropForeignKeys("umbracoDomains"); - } - - private IUmbracoSettingsSection _umbracoSettings; - protected override void FreezeResolution() { SiteDomainHelperResolver.Current = new SiteDomainHelperResolver(new SiteDomainHelper()); base.FreezeResolution(); } - void InitializeLanguagesAndDomains() - { - var domains = Domain.GetDomains(); - foreach (var d in domains) - d.Delete(); - - var langs = Language.GetAllAsList(); - foreach (var l in langs.Skip(1)) - l.Delete(); - - Language.MakeNew("fr-FR"); - } void SetDomains1() { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); - Domain.MakeNew("domain1.com", 1001, langFr.id); + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "domain1.com", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}} + }); } - void SetDomains2() - { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); + void SetDomains2() + { + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "http://domain1.com/foo", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}} + }); + } - Domain.MakeNew("http://domain1.com/foo", 1001, langFr.id); - } + void SetDomains3() + { + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "http://domain1.com/", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}} + }); + } - void SetDomains3() - { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); + void SetDomains4() + { + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "http://domain1.com/", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain1.com/en", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain1.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10012}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain3.com/", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1003}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain3.com/en", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10031}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain3.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10032}} + }); + } - Domain.MakeNew("http://domain1.com/", 10011, langFr.id); - } - - void SetDomains4() - { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); - - Domain.MakeNew("http://domain1.com/", 1001, langEn.id); - Domain.MakeNew("http://domain1.com/en", 10011, langEn.id); - Domain.MakeNew("http://domain1.com/fr", 10012, langFr.id); - - Domain.MakeNew("http://domain3.com/", 1003, langEn.id); - Domain.MakeNew("http://domain3.com/en", 10031, langEn.id); - Domain.MakeNew("http://domain3.com/fr", 10032, langFr.id); - } - - void SetDomains5() - { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); - - Domain.MakeNew("http://domain1.com/en", 10011, langEn.id); - Domain.MakeNew("http://domain1a.com/en", 10011, langEn.id); - Domain.MakeNew("http://domain1b.com/en", 10011, langEn.id); - Domain.MakeNew("http://domain1.com/fr", 10012, langFr.id); - Domain.MakeNew("http://domain1a.com/fr", 10012, langFr.id); - Domain.MakeNew("http://domain1b.com/fr", 10012, langFr.id); - - Domain.MakeNew("http://domain3.com/en", 10031, langEn.id); - Domain.MakeNew("http://domain3.com/fr", 10032, langFr.id); - } + void SetDomains5() + { + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "http://domain1.com/en", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain1a.com/en", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain1b.com/en", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain1.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10012}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain1a.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10012}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain1b.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10012}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain3.com/en", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10031}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain3.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10032}} + }); + } protected override string GetXmlContent(int templateId) { @@ -201,14 +174,15 @@ namespace Umbraco.Tests.Routing [TestCase(10011, "https://domain1.com", false, "/1001-1/")] public void Get_Nice_Url_SimpleDomain(int nodeId, string currentUrl, bool absolute, string expected) { - var routingContext = GetRoutingContext("/test", 1111); + var settings = SettingsForTests.GenerateMockSettings(); + var request = Mock.Get(settings.RequestHandler); + request.Setup(x => x.UseDomainPrefixes).Returns(false); + + var routingContext = GetRoutingContext("/test", 1111, umbracoSettings: settings); SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; // ignored w/domains - var requestMock = Mock.Get(_umbracoSettings.RequestHandler); - requestMock.Setup(x => x.UseDomainPrefixes).Returns(false); - InitializeLanguagesAndDomains(); SetDomains1(); var currentUri = new Uri(currentUrl); @@ -231,14 +205,15 @@ namespace Umbraco.Tests.Routing [TestCase(10011, "https://domain1.com", false, "http://domain1.com/foo/1001-1/")] public void Get_Nice_Url_SimpleWithSchemeAndPath(int nodeId, string currentUrl, bool absolute, string expected) { - var routingContext = GetRoutingContext("/test", 1111); + var settings = SettingsForTests.GenerateMockSettings(); + var request = Mock.Get(settings.RequestHandler); + request.Setup(x => x.UseDomainPrefixes).Returns(false); + + var routingContext = GetRoutingContext("/test", 1111, umbracoSettings: settings); SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; // ignored w/domains - var requestMock = Mock.Get(_umbracoSettings.RequestHandler); - requestMock.Setup(x => x.UseDomainPrefixes).Returns(false); - InitializeLanguagesAndDomains(); SetDomains2(); var currentUri = new Uri(currentUrl); @@ -253,15 +228,16 @@ namespace Umbraco.Tests.Routing [TestCase(1002, "http://domain1.com", false, "/1002/")] public void Get_Nice_Url_DeepDomain(int nodeId, string currentUrl, bool absolute, string expected) { - var routingContext = GetRoutingContext("/test", 1111); + var settings = SettingsForTests.GenerateMockSettings(); + var request = Mock.Get(settings.RequestHandler); + request.Setup(x => x.UseDomainPrefixes).Returns(false); + + var routingContext = GetRoutingContext("/test", 1111, umbracoSettings: settings); SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; // ignored w/domains - var requestMock = Mock.Get(_umbracoSettings.RequestHandler); - requestMock.Setup(x => x.UseDomainPrefixes).Returns(false); - InitializeLanguagesAndDomains(); - SetDomains3(); + SetDomains3(); var currentUri = new Uri(currentUrl); var result = routingContext.UrlProvider.GetUrl(nodeId, currentUri, absolute); @@ -281,14 +257,15 @@ namespace Umbraco.Tests.Routing [TestCase(100321, "http://domain3.com", false, "/fr/1003-2-1/")] public void Get_Nice_Url_NestedDomains(int nodeId, string currentUrl, bool absolute, string expected) { - var routingContext = GetRoutingContext("/test", 1111); + var settings = SettingsForTests.GenerateMockSettings(); + var request = Mock.Get(settings.RequestHandler); + request.Setup(x => x.UseDomainPrefixes).Returns(false); + + var routingContext = GetRoutingContext("/test", 1111, umbracoSettings: settings); SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; // ignored w/domains - var requestMock = Mock.Get(_umbracoSettings.RequestHandler); - requestMock.Setup(x => x.UseDomainPrefixes).Returns(false); - InitializeLanguagesAndDomains(); SetDomains4(); var currentUri = new Uri(currentUrl); @@ -299,14 +276,15 @@ namespace Umbraco.Tests.Routing [Test] public void Get_Nice_Url_DomainsAndCache() { - var routingContext = GetRoutingContext("/test", 1111); + var settings = SettingsForTests.GenerateMockSettings(); + var request = Mock.Get(settings.RequestHandler); + request.Setup(x => x.UseDomainPrefixes).Returns(false); + + var routingContext = GetRoutingContext("/test", 1111, umbracoSettings: settings); SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; // ignored w/domains - var requestMock = Mock.Get(_umbracoSettings.RequestHandler); - requestMock.Setup(x => x.UseDomainPrefixes).Returns(false); - InitializeLanguagesAndDomains(); SetDomains4(); string ignore; @@ -361,21 +339,20 @@ namespace Umbraco.Tests.Routing [Test] public void Get_Nice_Url_Relative_Or_Absolute() { - var routingContext = GetRoutingContext("http://domain1.com/test", 1111); + var settings = SettingsForTests.GenerateMockSettings(); + var requestMock = Mock.Get(settings.RequestHandler); + requestMock.Setup(x => x.UseDomainPrefixes).Returns(false); + + var routingContext = GetRoutingContext("http://domain1.com/test", 1111, umbracoSettings: settings); SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; - InitializeLanguagesAndDomains(); SetDomains4(); - - //mock the Umbraco settings that we need - var requestMock = Mock.Get(_umbracoSettings.RequestHandler); - requestMock.Setup(x => x.UseDomainPrefixes).Returns(false); Assert.AreEqual("/en/1001-1-1/", routingContext.UrlProvider.GetUrl(100111)); Assert.AreEqual("http://domain3.com/en/1003-1-1/", routingContext.UrlProvider.GetUrl(100311)); - + requestMock.Setup(x => x.UseDomainPrefixes).Returns(true); Assert.AreEqual("http://domain1.com/en/1001-1-1/", routingContext.UrlProvider.GetUrl(100111)); @@ -391,12 +368,12 @@ namespace Umbraco.Tests.Routing [Test] public void Get_Nice_Url_Alternate() { - var routingContext = GetRoutingContext("http://domain1.com/en/test", 1111); + var settings = SettingsForTests.GenerateMockSettings(); + var routingContext = GetRoutingContext("http://domain1.com/en/test", 1111,umbracoSettings:settings); SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; - InitializeLanguagesAndDomains(); SetDomains5(); var url = routingContext.UrlProvider.GetUrl(100111, true); diff --git a/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs b/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs new file mode 100644 index 0000000000..86e5edc833 --- /dev/null +++ b/src/Umbraco.Tests/Routing/UrlRoutingTestBase.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using System.Linq; +using Moq; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Logging; +using Umbraco.Core.Models; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.SqlSyntax; +using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers; + +namespace Umbraco.Tests.Routing +{ + [DatabaseTestBehavior(DatabaseBehavior.NoDatabasePerFixture)] + [TestFixture] + public abstract class UrlRoutingTestBase : BaseRoutingTest + { + /// + /// Sets up the mock domain service + /// + /// + protected void SetupDomainServiceMock(IEnumerable allDomains) + { + var domainService = Mock.Get(ServiceContext.DomainService); + //setup mock domain service + domainService.Setup(service => service.GetAll(It.IsAny())) + .Returns((bool incWildcards) => incWildcards ? allDomains : allDomains.Where(d => d.IsWildcard == false)); + domainService.Setup(service => service.GetAssignedDomains(It.IsAny(), It.IsAny())) + .Returns((int id, bool incWildcards) => allDomains.Where(d => d.RootContent.Id == id && (incWildcards || d.IsWildcard == false))); + } + + protected ServiceContext GetServiceContext(IUmbracoSettingsSection umbracoSettings, ILogger logger) + { + //get the mocked service context to get the mocked domain service + var svcCtx = MockHelper.GetMockedServiceContext(umbracoSettings, logger); + var domainService = Mock.Get(svcCtx.DomainService); + //setup mock domain service + domainService.Setup(service => service.GetAll(It.IsAny())) + .Returns((bool incWildcards) => new[] + { + new UmbracoDomain{Id = 1,DomainName = "domain1.com/", Language = new Language("de-DE"), RootContent = new Content("test1", -1, new ContentType(-1)){ Id = 1001}}, + new UmbracoDomain{Id = 1,DomainName = "domain1.com/en", Language = new Language("en-US"), RootContent = new Content("test2", -1, new ContentType(-1)){ Id = 10011}}, + new UmbracoDomain{Id = 1,DomainName = "domain1.com/fr", Language = new Language("fr-FR"), RootContent = new Content("test3", -1, new ContentType(-1)){ Id = 10012}} + }); + return svcCtx; + } + + protected override void SetupApplicationContext() + { + var settings = SettingsForTests.GetDefault(); + ApplicationContext.Current = new ApplicationContext( + new DatabaseContext(Mock.Of(), Logger, Mock.Of(), "test"), + GetServiceContext(settings, Logger), + CacheHelper, + ProfilingLogger) + { + IsReady = true + }; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs b/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs index f1a80de06d..9cd4474dcb 100644 --- a/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs +++ b/src/Umbraco.Tests/Routing/UrlsWithNestedDomains.cs @@ -1,19 +1,21 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Text; using Moq; using NUnit.Framework; using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Web.PublishedCache.XmlPublishedCache; -using umbraco.cms.businesslogic.web; -using umbraco.cms.businesslogic.language; using Umbraco.Web.Routing; +using umbraco.cms.businesslogic.web; +using System.Configuration; namespace Umbraco.Tests.Routing { - [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerFixture)] [TestFixture] - public class UrlsWithNestedDomains : BaseRoutingTest + public class UrlsWithNestedDomains : UrlRoutingTestBase { // in the case of nested domains more than 1 url may resolve to a document // but only one route can be cached - the 'canonical' route ie the route @@ -25,17 +27,18 @@ namespace Umbraco.Tests.Routing { SettingsForTests.UseDirectoryUrls = true; SettingsForTests.HideTopLevelNodeFromPath = false; // ignored w/domains - var requestMock = Mock.Get(_umbracoSettings.RequestHandler); - requestMock.Setup(x => x.UseDomainPrefixes).Returns(true); - InitializeLanguagesAndDomains(); + var settings = SettingsForTests.GenerateMockSettings(); + var request = Mock.Get(settings.RequestHandler); + request.Setup(x => x.UseDomainPrefixes).Returns(true); + SetDomains1(); RoutingContext routingContext; string url = "http://domain1.com/1001-1/1001-1-1"; // get the nice url for 100111 - routingContext = GetRoutingContext(url); + routingContext = GetRoutingContext(url, 9999, umbracoSettings: settings); Assert.AreEqual("http://domain2.com/1001-1-1/", routingContext.UrlProvider.GetUrl(100111, true)); // check that the proper route has been cached @@ -67,46 +70,20 @@ namespace Umbraco.Tests.Routing //Assert.AreEqual("http://domain1.com/1001-1/1001-1-1", routingContext.NiceUrlProvider.GetNiceUrl(100111, true)); // bad } - private IUmbracoSettingsSection _umbracoSettings; - - public override void Initialize() - { - base.Initialize(); - - // ensure we can create them although the content is not in the database - TestHelper.DropForeignKeys("umbracoDomains"); - - //generate new mock settings and assign so we can configure in individual tests - _umbracoSettings = SettingsForTests.GenerateMockSettings(); - SettingsForTests.ConfigureSettings(_umbracoSettings); - } - protected override void FreezeResolution() { SiteDomainHelperResolver.Current = new SiteDomainHelperResolver(new SiteDomainHelper()); base.FreezeResolution(); } - void InitializeLanguagesAndDomains() - { - var domains = Domain.GetDomains(); - foreach (var d in domains) - d.Delete(); - - var langs = Language.GetAllAsList(); - foreach (var l in langs.Skip(1)) - l.Delete(); - - Language.MakeNew("fr-FR"); - } - void SetDomains1() { - var langEn = Language.GetByCultureCode("en-US"); - var langFr = Language.GetByCultureCode("fr-FR"); + SetupDomainServiceMock(new[] + { + new UmbracoDomain {Id = 1, DomainName = "http://domain1.com/", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 1001}}, + new UmbracoDomain {Id = 1, DomainName = "http://domain2.com/", Language = new Language("en-US"), RootContent = new Content("test1", -1, new ContentType(-1)) {Id = 10011}} + }); - Domain.MakeNew("http://domain1.com/", 1001, langEn.id); - Domain.MakeNew("http://domain2.com/", 10011, langEn.id); } protected override string GetXmlContent(int templateId) diff --git a/src/Umbraco.Tests/Routing/uQueryGetNodeIdByUrlTests.cs b/src/Umbraco.Tests/Routing/uQueryGetNodeIdByUrlTests.cs index e387642ef2..bf24ce70f0 100644 --- a/src/Umbraco.Tests/Routing/uQueryGetNodeIdByUrlTests.cs +++ b/src/Umbraco.Tests/Routing/uQueryGetNodeIdByUrlTests.cs @@ -20,6 +20,10 @@ namespace Umbraco.Tests.Routing { base.Initialize(); + //generate new mock settings and assign so we can configure in individual tests + _umbracoSettings = SettingsForTests.GenerateMockSettings(); + SettingsForTests.ConfigureSettings(_umbracoSettings); + var url = "/test"; var lookup = new Umbraco.Web.Routing.ContentFinderByNiceUrl(); @@ -28,7 +32,7 @@ namespace Umbraco.Tests.Routing var t = Template.MakeNew("test", new User(0)); var umbracoContext = GetUmbracoContext(url, t.Id); - var urlProvider = new UrlProvider(umbracoContext, new IUrlProvider[] { new DefaultUrlProvider() }); + var urlProvider = new UrlProvider(umbracoContext, _umbracoSettings.WebRouting, new IUrlProvider[] { new DefaultUrlProvider() }); var routingContext = new RoutingContext( umbracoContext, lookups, @@ -42,9 +46,7 @@ namespace Umbraco.Tests.Routing //umbracoContext.RoutingContext = routingContext; Umbraco.Web.UmbracoContext.Current = routingContext.UmbracoContext; - //generate new mock settings and assign so we can configure in individual tests - _umbracoSettings = SettingsForTests.GenerateMockSettings(); - SettingsForTests.ConfigureSettings(_umbracoSettings); + } public override void TearDown() diff --git a/src/Umbraco.Tests/TestHelpers/BaseRoutingTest.cs b/src/Umbraco.Tests/TestHelpers/BaseRoutingTest.cs index d55a437976..81e85ae2a0 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseRoutingTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseRoutingTest.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Web.Routing; using NUnit.Framework; using Umbraco.Core.Configuration; +using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web; @@ -13,7 +14,7 @@ namespace Umbraco.Tests.TestHelpers { [TestFixture, RequiresSTA] public abstract class BaseRoutingTest : BaseWebTest - { + { /// /// Return a new RoutingContext /// @@ -24,11 +25,17 @@ namespace Umbraco.Tests.TestHelpers /// /// /// set to true to also set the singleton UmbracoContext.Current to the context created with this method + /// /// - protected RoutingContext GetRoutingContext(string url, int templateId, RouteData routeData = null, bool setUmbracoContextCurrent = false) - { + protected RoutingContext GetRoutingContext(string url, int templateId, RouteData routeData = null, bool setUmbracoContextCurrent = false, IUmbracoSettingsSection umbracoSettings = null) + { + if (umbracoSettings == null) umbracoSettings = SettingsForTests.GetDefault(); + var umbracoContext = GetUmbracoContext(url, templateId, routeData); - var urlProvider = new UrlProvider(umbracoContext, new IUrlProvider[] { new DefaultUrlProvider() }); + var urlProvider = new UrlProvider(umbracoContext, umbracoSettings.WebRouting, new IUrlProvider[] + { + new DefaultUrlProvider(umbracoSettings.RequestHandler) + }); var routingContext = new RoutingContext( umbracoContext, Enumerable.Empty(), diff --git a/src/Umbraco.Tests/TestHelpers/MockHelper.cs b/src/Umbraco.Tests/TestHelpers/MockHelper.cs new file mode 100644 index 0000000000..ae7245b855 --- /dev/null +++ b/src/Umbraco.Tests/TestHelpers/MockHelper.cs @@ -0,0 +1,59 @@ +using Moq; +using Umbraco.Core; +using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.SqlSyntax; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Services; + +namespace Umbraco.Tests.TestHelpers +{ + public static class MockHelper + { + public static ServiceContext GetMockedServiceContext(IUmbracoSettingsSection settings = null, ILogger logger = null, CacheHelper cacheHelper = null, ISqlSyntaxProvider sqlSyntax = null) + { + if (settings == null) settings = SettingsForTests.GetDefault(); + if (logger == null) logger = Mock.Of(); + if (cacheHelper == null) cacheHelper = CacheHelper.CreateDisabledCacheHelper(); + if (sqlSyntax == null) sqlSyntax = Mock.Of(); + + return new ServiceContext( + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new PackagingService( + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new RepositoryFactory(cacheHelper, logger, sqlSyntax, settings), + new Mock().Object), + new Mock().Object, + new RelationService( + new Mock().Object, + new RepositoryFactory(cacheHelper, logger, sqlSyntax, settings), + logger, + new Mock().Object), + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/TestHelper.cs b/src/Umbraco.Tests/TestHelpers/TestHelper.cs index 7f881ba559..eec71f9a7f 100644 --- a/src/Umbraco.Tests/TestHelpers/TestHelper.cs +++ b/src/Umbraco.Tests/TestHelpers/TestHelper.cs @@ -12,7 +12,7 @@ using GlobalSettings = umbraco.GlobalSettings; namespace Umbraco.Tests.TestHelpers { - /// + /// /// Common helper properties and methods useful to testing /// public static class TestHelper diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 78abd56a66..02527308c2 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -178,6 +178,7 @@ + @@ -444,6 +445,7 @@ + diff --git a/src/Umbraco.Web/Routing/DefaultUrlProvider.cs b/src/Umbraco.Web/Routing/DefaultUrlProvider.cs index 13855e80b3..f15c485560 100644 --- a/src/Umbraco.Web/Routing/DefaultUrlProvider.cs +++ b/src/Umbraco.Web/Routing/DefaultUrlProvider.cs @@ -5,6 +5,7 @@ using System.Threading; using Umbraco.Core; using Umbraco.Core.Configuration; +using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; using Umbraco.Web.PublishedCache; using umbraco.cms.businesslogic.web; @@ -16,6 +17,19 @@ namespace Umbraco.Web.Routing /// public class DefaultUrlProvider : IUrlProvider { + private readonly IRequestHandlerSection _requestSettings; + + [Obsolete("Use the ctor that specifies the IRequestHandlerSection")] + public DefaultUrlProvider() + : this(UmbracoConfig.For.UmbracoSettings().RequestHandler) + { + } + + public DefaultUrlProvider(IRequestHandlerSection requestSettings) + { + _requestSettings = requestSettings; + } + #region GetUrl /// @@ -112,7 +126,7 @@ namespace Umbraco.Web.Routing if (mode == UrlProviderMode.AutoLegacy) { - mode = UmbracoConfig.For.UmbracoSettings().RequestHandler.UseDomainPrefixes + mode = _requestSettings.UseDomainPrefixes ? UrlProviderMode.Absolute : UrlProviderMode.Auto; } diff --git a/src/Umbraco.Web/Routing/UrlProvider.cs b/src/Umbraco.Web/Routing/UrlProvider.cs index 69c56603fa..f4b9181bef 100644 --- a/src/Umbraco.Web/Routing/UrlProvider.cs +++ b/src/Umbraco.Web/Routing/UrlProvider.cs @@ -19,8 +19,9 @@ namespace Umbraco.Web.Routing /// Initializes a new instance of the class with an Umbraco context and a list of url providers. /// /// The Umbraco context. + /// /// The list of url providers. - internal UrlProvider(UmbracoContext umbracoContext, IEnumerable urlProviders) + internal UrlProvider(UmbracoContext umbracoContext, IWebRoutingSection routingSettings, IEnumerable urlProviders) { _umbracoContext = umbracoContext; _urlProviders = urlProviders; @@ -28,7 +29,7 @@ namespace Umbraco.Web.Routing var provider = UrlProviderMode.Auto; Mode = provider; - if (Enum.TryParse(UmbracoConfig.For.UmbracoSettings().WebRouting.UrlProviderMode, out provider)) + if (Enum.TryParse(routingSettings.UrlProviderMode, out provider)) { Mode = provider; } diff --git a/src/Umbraco.Web/Search/ExamineEvents.cs b/src/Umbraco.Web/Search/ExamineEvents.cs index 8b21ce60ae..51d190436f 100644 --- a/src/Umbraco.Web/Search/ExamineEvents.cs +++ b/src/Umbraco.Web/Search/ExamineEvents.cs @@ -1,7 +1,6 @@ using System; using System.Globalization; using System.Linq; -using System.Security; using System.Xml; using System.Xml.Linq; using Examine; @@ -11,19 +10,11 @@ using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Logging; using Umbraco.Core.Models; -using Umbraco.Core.Models.EntityBase; -using Umbraco.Core.Services; using Umbraco.Core.Sync; using Umbraco.Web.Cache; using UmbracoExamine; -using umbraco; -using umbraco.BusinessLogic; -using umbraco.cms.businesslogic; -using umbraco.cms.businesslogic.member; -using umbraco.interfaces; using Content = umbraco.cms.businesslogic.Content; using Document = umbraco.cms.businesslogic.web.Document; -using Member = umbraco.cms.businesslogic.member.Member; namespace Umbraco.Web.Search { diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index ba2f6aee46..efbe290f15 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -1,13 +1,16 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Web; using Umbraco.Core; +using Umbraco.Core.Configuration; +using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Web.PublishedCache; using Umbraco.Web.Routing; using Umbraco.Web.Security; -using umbraco; using umbraco.BusinessLogic; using umbraco.presentation.preview; +using GlobalSettings = umbraco.GlobalSettings; using IOHelper = Umbraco.Core.IO.IOHelper; using SystemDirectories = Umbraco.Core.IO.SystemDirectories; @@ -24,8 +27,8 @@ namespace Umbraco.Web private bool _replacing; private bool? _previewing; - private Lazy _contentCache; - private Lazy _mediaCache; + private readonly Lazy _contentCache; + private readonly Lazy _mediaCache; /// /// Used if not running in a web application (no real HttpContext) @@ -34,6 +37,57 @@ namespace Umbraco.Web private static UmbracoContext _umbracoContext; #region EnsureContext methods + + #region Obsolete + [Obsolete("Use the method that specifies IUmbracoSettings instead")] + [EditorBrowsable(EditorBrowsableState.Never)] + public static UmbracoContext EnsureContext( + HttpContextBase httpContext, + ApplicationContext applicationContext, + WebSecurity webSecurity) + { + return EnsureContext(httpContext, applicationContext, webSecurity, false); + } + [Obsolete("Use the method that specifies IUmbracoSettings instead")] + [EditorBrowsable(EditorBrowsableState.Never)] + public static UmbracoContext EnsureContext( + HttpContextBase httpContext, + ApplicationContext applicationContext) + { + return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), false); + } + [Obsolete("Use the method that specifies IUmbracoSettings instead")] + [EditorBrowsable(EditorBrowsableState.Never)] + public static UmbracoContext EnsureContext( + HttpContextBase httpContext, + ApplicationContext applicationContext, + bool replaceContext) + { + return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), replaceContext); + } + [Obsolete("Use the method that specifies IUmbracoSettings instead")] + [EditorBrowsable(EditorBrowsableState.Never)] + public static UmbracoContext EnsureContext( + HttpContextBase httpContext, + ApplicationContext applicationContext, + WebSecurity webSecurity, + bool replaceContext) + { + return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), replaceContext, null); + } + [Obsolete("Use the method that specifies IUmbracoSettings instead")] + [EditorBrowsable(EditorBrowsableState.Never)] + public static UmbracoContext EnsureContext( + HttpContextBase httpContext, + ApplicationContext applicationContext, + WebSecurity webSecurity, + bool replaceContext, + bool? preview) + { + return EnsureContext(httpContext, applicationContext, webSecurity, UmbracoConfig.For.UmbracoSettings(), replaceContext, preview); + } + #endregion + /// /// This is a helper method which is called to ensure that the singleton context is created and the nice url and routing /// context is created and assigned. @@ -41,6 +95,7 @@ namespace Umbraco.Web /// /// /// + /// /// /// The Singleton context object /// @@ -53,17 +108,21 @@ namespace Umbraco.Web public static UmbracoContext EnsureContext( HttpContextBase httpContext, ApplicationContext applicationContext, - WebSecurity webSecurity) + WebSecurity webSecurity, + IUmbracoSettingsSection umbracoSettings) { - return EnsureContext(httpContext, applicationContext, webSecurity, false); + return EnsureContext(httpContext, applicationContext, webSecurity, umbracoSettings, false); } + + /// /// This is a helper method which is called to ensure that the singleton context is created and the nice url and routing /// context is created and assigned. /// /// /// + /// /// /// The Singleton context object /// @@ -75,17 +134,21 @@ namespace Umbraco.Web /// public static UmbracoContext EnsureContext( HttpContextBase httpContext, - ApplicationContext applicationContext) + ApplicationContext applicationContext, + IUmbracoSettingsSection umbracoSettings) { - return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), false); + return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), umbracoSettings, false); } + + /// /// This is a helper method which is called to ensure that the singleton context is created and the nice url and routing /// context is created and assigned. /// /// /// + /// /// /// if set to true will replace the current singleton with a new one, this is generally only ever used because /// during application startup the base url domain will not be available so after app startup we'll replace the current @@ -103,11 +166,15 @@ namespace Umbraco.Web public static UmbracoContext EnsureContext( HttpContextBase httpContext, ApplicationContext applicationContext, + IUmbracoSettingsSection umbracoSettings, bool replaceContext) { - return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), replaceContext); + return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), umbracoSettings, replaceContext); } + + + /// /// This is a helper method which is called to ensure that the singleton context is created and the nice url and routing /// context is created and assigned. @@ -115,6 +182,7 @@ namespace Umbraco.Web /// /// /// + /// /// /// if set to true will replace the current singleton with a new one, this is generally only ever used because /// during application startup the base url domain will not be available so after app startup we'll replace the current @@ -133,11 +201,14 @@ namespace Umbraco.Web HttpContextBase httpContext, ApplicationContext applicationContext, WebSecurity webSecurity, + IUmbracoSettingsSection umbracoSettings, bool replaceContext) { - return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), replaceContext, null); + return EnsureContext(httpContext, applicationContext, new WebSecurity(httpContext, applicationContext), umbracoSettings, replaceContext, null); } + + /// /// This is a helper method which is called to ensure that the singleton context is created and the nice url and routing /// context is created and assigned. @@ -145,6 +216,7 @@ namespace Umbraco.Web /// /// /// + /// /// /// if set to true will replace the current singleton with a new one, this is generally only ever used because /// during application startup the base url domain will not be available so after app startup we'll replace the current @@ -164,12 +236,13 @@ namespace Umbraco.Web HttpContextBase httpContext, ApplicationContext applicationContext, WebSecurity webSecurity, + IUmbracoSettingsSection umbracoSettings, bool replaceContext, bool? preview) { if (UmbracoContext.Current != null) { - if (!replaceContext) + if (replaceContext == false) return UmbracoContext.Current; UmbracoContext.Current._replacing = true; } @@ -189,8 +262,9 @@ namespace Umbraco.Web // create the nice urls provider // there's one per request because there are some behavior parameters that can be changed new Lazy( - () => new UrlProvider( + () => new UrlProvider( umbracoContext, + umbracoSettings.WebRouting, UrlProviderResolver.Current.Providers), false));