From 2b54cc50ab4c80fa75f6b7f9dbd1ebb4f060970b Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 16 Apr 2019 18:32:33 +0200 Subject: [PATCH] Refactor IPublishedContent.UrlSegment() --- .../PublishedContent/IPublishedContent.cs | 10 +++----- .../PublishedContentWrapped.cs | 2 +- .../PublishedContent/PublishedCultureInfos.cs | 2 +- .../PublishedMediaCacheTests.cs | 2 +- .../DictionaryPublishedContent.cs | 2 +- .../PublishedContentCache.cs | 2 +- .../XmlPublishedContent.cs | 9 +++---- .../Published/NestedContentTests.cs | 2 +- .../PublishedContentDataTableTests.cs | 6 +++-- .../PublishedContentLanguageVariantTests.cs | 6 ++--- .../PublishedContentMoreTests.cs | 6 ++--- .../SolidPublishedSnapshot.cs | 4 +++- .../TestHelpers/Stubs/TestPublishedContent.cs | 4 +++- .../PublishedContentHashtableConverter.cs | 2 +- .../Models/PublishedContentBase.cs | 2 +- .../PublishedCache/NuCache/ContentCache.cs | 12 +++++----- .../NuCache/Navigable/NavigableContent.cs | 2 +- .../NuCache/PublishedContent.cs | 19 +++++++-------- .../PublishedCache/PublishedMember.cs | 2 +- src/Umbraco.Web/PublishedContentExtensions.cs | 24 ------------------- 20 files changed, 47 insertions(+), 73 deletions(-) diff --git a/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs b/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs index 95b943d751..304066196d 100644 --- a/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs +++ b/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs @@ -29,18 +29,14 @@ namespace Umbraco.Core.Models.PublishedContent /// /// Gets the name of the content item. /// - /// The specific culture to filter for. If null is used the current culture is used. (Default is null) - /// The name of the content. + /// The specific culture to get the name for. If null is used the current culture is used (Default is null). string Name(string culture = null); /// /// Gets the url segment of the content item. /// - /// - /// The value of this property is contextual. When the content type is multi-lingual, - /// this is the name for the 'current' culture. Otherwise, it is the invariant url segment. - /// - string UrlSegment { get; } + /// The specific culture to get the url segment for. If null is used the current culture is used (Default is null). + string UrlSegment(string culture = null); /// /// Gets the sort order of the content item. diff --git a/src/Umbraco.Core/Models/PublishedContent/PublishedContentWrapped.cs b/src/Umbraco.Core/Models/PublishedContent/PublishedContentWrapped.cs index 753f75c3cb..16732840ea 100644 --- a/src/Umbraco.Core/Models/PublishedContent/PublishedContentWrapped.cs +++ b/src/Umbraco.Core/Models/PublishedContent/PublishedContentWrapped.cs @@ -61,7 +61,7 @@ namespace Umbraco.Core.Models.PublishedContent public virtual string Name(string culture = null) => _content.Name(culture); /// - public virtual string UrlSegment => _content.UrlSegment; + public virtual string UrlSegment(string culture = null) => _content.UrlSegment(culture); /// public virtual int SortOrder => _content.SortOrder; diff --git a/src/Umbraco.Core/Models/PublishedContent/PublishedCultureInfos.cs b/src/Umbraco.Core/Models/PublishedContent/PublishedCultureInfos.cs index 9c5977e8a6..eedc97dbfc 100644 --- a/src/Umbraco.Core/Models/PublishedContent/PublishedCultureInfos.cs +++ b/src/Umbraco.Core/Models/PublishedContent/PublishedCultureInfos.cs @@ -35,7 +35,7 @@ namespace Umbraco.Core.Models.PublishedContent /// /// Gets the url segment of the item. /// - public string UrlSegment { get; } + internal string UrlSegment { get; } /// /// Gets the date associated with the culture. diff --git a/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs b/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs index 8dd5cf3890..2245f600dc 100644 --- a/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs +++ b/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs @@ -397,7 +397,7 @@ namespace Umbraco.Tests.Cache.PublishedCache Assert.AreEqual(keyVal, doc.Key); Assert.AreEqual(templateIdVal, doc.TemplateId); Assert.AreEqual(sortOrderVal, doc.SortOrder); - Assert.AreEqual(urlNameVal, doc.UrlSegment); + Assert.AreEqual(urlNameVal, doc.UrlSegment()); Assert.AreEqual(nodeTypeAliasVal, doc.ContentType.Alias); Assert.AreEqual(nodeTypeIdVal, doc.ContentType.Id); Assert.AreEqual(writerNameVal, doc.WriterName); diff --git a/src/Umbraco.Tests/LegacyXmlPublishedCache/DictionaryPublishedContent.cs b/src/Umbraco.Tests/LegacyXmlPublishedCache/DictionaryPublishedContent.cs index c6c16a2466..9e615e6745 100644 --- a/src/Umbraco.Tests/LegacyXmlPublishedCache/DictionaryPublishedContent.cs +++ b/src/Umbraco.Tests/LegacyXmlPublishedCache/DictionaryPublishedContent.cs @@ -163,7 +163,7 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache private static readonly Lazy> NoCultures = new Lazy>(() => new Dictionary()); public override IReadOnlyDictionary Cultures => NoCultures.Value; - public override string UrlSegment => _urlName; + public override string UrlSegment(string culture = null) => _urlName; public override string WriterName => _creatorName; diff --git a/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedContentCache.cs b/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedContentCache.cs index d69799dfdf..1ad6e045c6 100644 --- a/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedContentCache.cs +++ b/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedContentCache.cs @@ -271,7 +271,7 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache while (hasDomains == false && n != null) // n is null at root { // get the url - var urlName = n.UrlSegment; + var urlName = n.UrlSegment(); pathParts.Add(urlName); // move to parent node diff --git a/src/Umbraco.Tests/LegacyXmlPublishedCache/XmlPublishedContent.cs b/src/Umbraco.Tests/LegacyXmlPublishedCache/XmlPublishedContent.cs index c547512ba8..8f30bdd789 100644 --- a/src/Umbraco.Tests/LegacyXmlPublishedCache/XmlPublishedContent.cs +++ b/src/Umbraco.Tests/LegacyXmlPublishedCache/XmlPublishedContent.cs @@ -212,13 +212,10 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache } } - public override string UrlSegment + public override string UrlSegment(string culture = null) { - get - { - EnsureNodeInitialized(); - return _urlName; - } + EnsureNodeInitialized(); + return _urlName; } public override int Level diff --git a/src/Umbraco.Tests/Published/NestedContentTests.cs b/src/Umbraco.Tests/Published/NestedContentTests.cs index 35288c1f75..1cf291b263 100644 --- a/src/Umbraco.Tests/Published/NestedContentTests.cs +++ b/src/Umbraco.Tests/Published/NestedContentTests.cs @@ -287,7 +287,7 @@ namespace Umbraco.Tests.Published public override string Name(string culture = null) => default; public override PublishedCultureInfo GetCulture(string culture = ".") => throw new NotSupportedException(); public override IReadOnlyDictionary Cultures => throw new NotSupportedException(); - public override string UrlSegment { get; } + public override string UrlSegment(string culture = null) => default; public override string WriterName { get; } public override string CreatorName { get; } public override int WriterId { get; } diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs index 220909e237..7d1ef25dcf 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs @@ -139,7 +139,6 @@ namespace Umbraco.Tests.PublishedContent TemplateId = 5, UpdateDate = DateTime.Now, Path = "-1,3", - UrlSegment = "home-page", Version = Guid.NewGuid(), WriterId = 1, WriterName = "Shannon", @@ -148,6 +147,7 @@ namespace Umbraco.Tests.PublishedContent Children = new List() }; d.SetName("Page" + Guid.NewGuid()); + d.SetUrlSegment("home-page"); d.Properties = new Collection(new List { new RawValueProperty(factory.CreatePropertyType("property1", 1), d, "value" + indexVals), @@ -184,6 +184,7 @@ namespace Umbraco.Tests.PublishedContent private class TestPublishedContent : IPublishedContent { private readonly Dictionary _names = new Dictionary(); + private readonly Dictionary _urlSegments = new Dictionary(); public string Url { get; set; } public string GetUrl(string culture = null) => throw new NotSupportedException(); @@ -209,7 +210,8 @@ namespace Umbraco.Tests.PublishedContent public void SetName(string name, string culture = null) => _names[culture ?? ""] = name; public PublishedCultureInfo GetCulture(string culture = null) => throw new NotSupportedException(); public IReadOnlyDictionary Cultures => throw new NotSupportedException(); - public string UrlSegment { get; set; } + public string UrlSegment(string culture = null) => _urlSegments.TryGetValue(culture ?? "", out var urlSegment) ? urlSegment : null; + public void SetUrlSegment(string urlSegment, string culture = null) => _urlSegments[culture ?? ""] = urlSegment; public string WriterName { get; set; } public string CreatorName { get; set; } public int WriterId { get; set; } diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentLanguageVariantTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentLanguageVariantTests.cs index 9cec962a38..bf84413fae 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentLanguageVariantTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentLanguageVariantTests.cs @@ -122,7 +122,6 @@ namespace Umbraco.Tests.PublishedContent { Id = 1, SortOrder = 0, - UrlSegment = "content-1", Path = "/1", Level = 1, Url = "/content-1", @@ -134,12 +133,12 @@ namespace Umbraco.Tests.PublishedContent } }; item1.SetName("Content 1"); + item1.SetUrlSegment("content-1"); var item2 = new SolidPublishedContent(contentType1) { Id = 2, SortOrder = 0, - UrlSegment = "content-2", Path = "/1/2", Level = 2, Url = "/content-1/content-2", @@ -151,6 +150,7 @@ namespace Umbraco.Tests.PublishedContent } }; item2.SetName("Content 2"); + item2.SetUrlSegment("content-2"); var prop4 = new SolidPublishedPropertyWithLanguageVariants { @@ -164,7 +164,6 @@ namespace Umbraco.Tests.PublishedContent { Id = 3, SortOrder = 0, - UrlSegment = "content-3", Path = "/1/2/3", Level = 3, Url = "/content-1/content-2/content-3", @@ -176,6 +175,7 @@ namespace Umbraco.Tests.PublishedContent } }; item3.SetName("Content 3"); + item3.SetUrlSegment("content-3"); item1.Children = new List { item2 }; item2.Parent = item1; diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentMoreTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentMoreTests.cs index ffe88f5d13..9f9f3b8d41 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentMoreTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentMoreTests.cs @@ -29,7 +29,6 @@ namespace Umbraco.Tests.PublishedContent { Id = 1, SortOrder = 0, - UrlSegment = "content-1", Path = "/1", Level = 1, Url = "/content-1", @@ -47,13 +46,13 @@ namespace Umbraco.Tests.PublishedContent } }; content.SetName("Content 1"); + content.SetUrlSegment("content-1"); cache.Add(content); content = new SolidPublishedContent(contentType2) { Id = 2, SortOrder = 1, - UrlSegment = "content-2", Path = "/2", Level = 1, Url = "/content-2", @@ -71,13 +70,13 @@ namespace Umbraco.Tests.PublishedContent } }; content.SetName("Content 2"); + content.SetUrlSegment("content-2"); cache.Add(content); content = new SolidPublishedContent(contentType2Sub) { Id = 3, SortOrder = 2, - UrlSegment = "content-2sub", Path = "/3", Level = 1, Url = "/content-2sub", @@ -95,6 +94,7 @@ namespace Umbraco.Tests.PublishedContent } }; content.SetName("Content 2Sub"); + content.SetUrlSegment("content-2sub"); cache.Add(content); } diff --git a/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs b/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs index a25faea07a..e1c8cda3a9 100644 --- a/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs +++ b/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs @@ -156,6 +156,7 @@ namespace Umbraco.Tests.PublishedContent internal class SolidPublishedContent : IPublishedContent { private readonly Dictionary _names = new Dictionary(); + private readonly Dictionary _urlSegments = new Dictionary(); #region Constructor @@ -183,7 +184,8 @@ namespace Umbraco.Tests.PublishedContent public void SetName(string name, string culture = null) => _names[culture ?? ""] = name; public PublishedCultureInfo GetCulture(string culture = null) => throw new NotSupportedException(); public IReadOnlyDictionary Cultures => throw new NotSupportedException(); - public string UrlSegment { get; set; } + public string UrlSegment(string culture = null) => _urlSegments.TryGetValue(culture ?? "", out var urlSegment) ? urlSegment : null; + public void SetUrlSegment(string urlSegment, string culture = null) => _urlSegments[culture ?? ""] = urlSegment; public string WriterName { get; set; } public string CreatorName { get; set; } public int WriterId { get; set; } diff --git a/src/Umbraco.Tests/TestHelpers/Stubs/TestPublishedContent.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestPublishedContent.cs index 81319a619c..d621197bf8 100644 --- a/src/Umbraco.Tests/TestHelpers/Stubs/TestPublishedContent.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestPublishedContent.cs @@ -8,6 +8,7 @@ namespace Umbraco.Tests.TestHelpers.Stubs internal class TestPublishedContent : PublishedElement, IPublishedContent { private readonly Dictionary _names = new Dictionary(); + private readonly Dictionary _urlSegments = new Dictionary(); public TestPublishedContent(IPublishedContentType contentType, int id, Guid key, Dictionary values, bool previewing, Dictionary cultures = null) : base(contentType, key, values, previewing) @@ -35,7 +36,8 @@ namespace Umbraco.Tests.TestHelpers.Stubs return Cultures.TryGetValue(culture, out var cultureInfos) ? cultureInfos : null; } public IReadOnlyDictionary Cultures { get; set; } - public string UrlSegment { get; set; } + public string UrlSegment(string culture = null) => _urlSegments.TryGetValue(culture ?? "", out var urlSegment) ? urlSegment : null; + public void SetUrlSegment(string urlSegment, string culture = null) => _urlSegments[culture ?? ""] = urlSegment; public string DocumentTypeAlias => ContentType.Alias; public int DocumentTypeId { get; set; } public string WriterName { get; set; } diff --git a/src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs b/src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs index f516aca080..977cd6fdc9 100644 --- a/src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs +++ b/src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs @@ -259,7 +259,7 @@ namespace Umbraco.Web.Macros } } - public string UrlSegment => throw new NotImplementedException(); + public string UrlSegment(string culture = null) => throw new NotImplementedException(); public string WriterName { get; } diff --git a/src/Umbraco.Web/Models/PublishedContentBase.cs b/src/Umbraco.Web/Models/PublishedContentBase.cs index 04b5a64d07..f93a6d08c4 100644 --- a/src/Umbraco.Web/Models/PublishedContentBase.cs +++ b/src/Umbraco.Web/Models/PublishedContentBase.cs @@ -44,7 +44,7 @@ namespace Umbraco.Web.Models public abstract string Name(string culture = null); /// - public abstract string UrlSegment { get; } + public abstract string UrlSegment(string culture = null); /// public abstract int SortOrder { get; } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs b/src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs index d070b959ed..08664f0a7a 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs @@ -109,8 +109,8 @@ namespace Umbraco.Web.PublishedCache.NuCache // hideTopLevelNode = support legacy stuff, look for /*/path/to/node // else normal, look for /path/to/node content = hideTopLevelNode.Value - ? GetAtRoot(preview).SelectMany(x => x.Children).FirstOrDefault(x => x.GetUrlSegment(culture) == parts[0]) - : GetAtRoot(preview).FirstOrDefault(x => x.GetUrlSegment(culture) == parts[0]); + ? GetAtRoot(preview).SelectMany(x => x.Children).FirstOrDefault(x => x.UrlSegment(culture) == parts[0]) + : GetAtRoot(preview).FirstOrDefault(x => x.UrlSegment(culture) == parts[0]); content = FollowRoute(content, parts, 1, culture); } @@ -119,7 +119,7 @@ namespace Umbraco.Web.PublishedCache.NuCache // have to look for /foo (see note in ApplyHideTopLevelNodeFromPath). if (content == null && hideTopLevelNode.Value && parts.Length == 1) { - content = GetAtRoot(preview).FirstOrDefault(x => x.GetUrlSegment(culture) == parts[0]); + content = GetAtRoot(preview).FirstOrDefault(x => x.UrlSegment(culture) == parts[0]); } return content; @@ -149,7 +149,7 @@ namespace Umbraco.Web.PublishedCache.NuCache // or we reach the content root, collecting urls in the way var pathParts = new List(); var n = node; - var urlSegment = n.GetUrlSegment(culture); + var urlSegment = n.UrlSegment(culture); var hasDomains = _domainHelper.NodeHasDomains(n.Id); while (hasDomains == false && n != null) // n is null at root { @@ -161,7 +161,7 @@ namespace Umbraco.Web.PublishedCache.NuCache // move to parent node n = n.Parent; if (n != null) - urlSegment = n.GetUrlSegment(culture); + urlSegment = n.UrlSegment(culture); hasDomains = n != null && _domainHelper.NodeHasDomains(n.Id); } @@ -191,7 +191,7 @@ namespace Umbraco.Web.PublishedCache.NuCache var part = parts[i++]; content = content.Children.FirstOrDefault(x => { - var urlSegment = x.GetUrlSegment(culture); + var urlSegment = x.UrlSegment(culture); return urlSegment == part; }); } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/Navigable/NavigableContent.cs b/src/Umbraco.Web/PublishedCache/NuCache/Navigable/NavigableContent.cs index bd6ad97d32..c9dd493ee6 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/Navigable/NavigableContent.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/Navigable/NavigableContent.cs @@ -28,7 +28,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.Navigable XmlString(i++, _content.TemplateId), XmlString(i++, _content.WriterId), XmlString(i++, _content.CreatorId), - XmlString(i++, _content.UrlSegment), + XmlString(i++, _content.UrlSegment()), XmlString(i, _content.IsDraft()) }; } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs b/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs index f208061c18..e51220dfab 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs @@ -200,19 +200,18 @@ namespace Umbraco.Web.PublishedCache.NuCache } /// - public override string UrlSegment + public override string UrlSegment(string culture = null) { - get - { - if (!ContentType.VariesByCulture()) - return _urlSegment; + // handle context culture + if (culture == null) + culture = VariationContextAccessor?.VariationContext?.Culture ?? ""; - var culture = VariationContextAccessor?.VariationContext?.Culture ?? ""; - if (culture == "") - return _urlSegment; + // invariant culture + if (culture == "") + return ContentType.VariesByCulture() ? null : _urlSegment; - return Cultures.TryGetValue(culture, out var cultureInfos) ? cultureInfos.UrlSegment : null; - } + // explicit culture + return Cultures.TryGetValue(culture, out var cultureInfos) ? cultureInfos.UrlSegment : null; } /// diff --git a/src/Umbraco.Web/PublishedCache/PublishedMember.cs b/src/Umbraco.Web/PublishedCache/PublishedMember.cs index d87d75059e..733b4068e0 100644 --- a/src/Umbraco.Web/PublishedCache/PublishedMember.cs +++ b/src/Umbraco.Web/PublishedCache/PublishedMember.cs @@ -146,7 +146,7 @@ namespace Umbraco.Web.PublishedCache public override IReadOnlyDictionary Cultures => throw new NotSupportedException(); - public override string UrlSegment => throw new NotSupportedException(); + public override string UrlSegment(string culture = null) => throw new NotSupportedException(); // TODO: ARGH! need to fix this - this is not good because it uses ApplicationContext.Current public override string WriterName => _member.GetCreatorProfile().Name; diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index 29f32c1316..2495415834 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -60,30 +60,6 @@ namespace Umbraco.Web } } - /// - /// Gets the Url segment. - /// - /// - /// Gets the url segment for the document, taking its content type and a specified - /// culture in account. For invariant content types, the culture is ignored, else it is - /// used to try and find the segment corresponding to the culture. May return null. - /// - public static string GetUrlSegment(this IPublishedContent content, string culture = null) - { - // for invariant content, return the invariant url segment - if (!content.ContentType.VariesByCulture()) - return content.UrlSegment; - - // content.GetCulture(culture) will use the 'current' culture (via accessor) in case 'culture' - // is null (meaning, 'current') - and can return 'null' if that culture is not published - and - // will return 'null' if the content is variant and culture is invariant - - // else try and get the culture info - // return the corresponding url segment, or null if none - var cultureInfo = content.GetCulture(culture); - return cultureInfo?.UrlSegment; - } - public static bool IsAllowedTemplate(this IPublishedContent content, int templateId) { if (Current.Configs.Settings().WebRouting.DisableAlternativeTemplates)