Refactor IPublishedContent.UrlSegment()

This commit is contained in:
Stephan
2019-04-16 18:32:33 +02:00
parent 99319e0b49
commit 2b54cc50ab
20 changed files with 47 additions and 73 deletions

View File

@@ -29,18 +29,14 @@ namespace Umbraco.Core.Models.PublishedContent
/// <summary>
/// Gets the name of the content item.
/// </summary>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The name of the content.</returns>
/// <param name="culture">The specific culture to get the name for. If null is used the current culture is used (Default is null).</param>
string Name(string culture = null);
/// <summary>
/// Gets the url segment of the content item.
/// </summary>
/// <remarks>
/// <para>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.</para>
/// </remarks>
string UrlSegment { get; }
/// <param name="culture">The specific culture to get the url segment for. If null is used the current culture is used (Default is null).</param>
string UrlSegment(string culture = null);
/// <summary>
/// Gets the sort order of the content item.

View File

@@ -61,7 +61,7 @@ namespace Umbraco.Core.Models.PublishedContent
public virtual string Name(string culture = null) => _content.Name(culture);
/// <inheritdoc />
public virtual string UrlSegment => _content.UrlSegment;
public virtual string UrlSegment(string culture = null) => _content.UrlSegment(culture);
/// <inheritdoc />
public virtual int SortOrder => _content.SortOrder;

View File

@@ -35,7 +35,7 @@ namespace Umbraco.Core.Models.PublishedContent
/// <summary>
/// Gets the url segment of the item.
/// </summary>
public string UrlSegment { get; }
internal string UrlSegment { get; }
/// <summary>
/// Gets the date associated with the culture.

View File

@@ -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);

View File

@@ -163,7 +163,7 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache
private static readonly Lazy<Dictionary<string, PublishedCultureInfo>> NoCultures = new Lazy<Dictionary<string, PublishedCultureInfo>>(() => new Dictionary<string, PublishedCultureInfo>());
public override IReadOnlyDictionary<string, PublishedCultureInfo> Cultures => NoCultures.Value;
public override string UrlSegment => _urlName;
public override string UrlSegment(string culture = null) => _urlName;
public override string WriterName => _creatorName;

View File

@@ -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

View File

@@ -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

View File

@@ -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<string, PublishedCultureInfo> 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; }

View File

@@ -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<IPublishedContent>()
};
d.SetName("Page" + Guid.NewGuid());
d.SetUrlSegment("home-page");
d.Properties = new Collection<IPublishedProperty>(new List<IPublishedProperty>
{
new RawValueProperty(factory.CreatePropertyType("property1", 1), d, "value" + indexVals),
@@ -184,6 +184,7 @@ namespace Umbraco.Tests.PublishedContent
private class TestPublishedContent : IPublishedContent
{
private readonly Dictionary<string, string> _names = new Dictionary<string, string>();
private readonly Dictionary<string, string> _urlSegments = new Dictionary<string, string>();
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<string, PublishedCultureInfo> 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; }

View File

@@ -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<IPublishedContent> { item2 };
item2.Parent = item1;

View File

@@ -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);
}

View File

@@ -156,6 +156,7 @@ namespace Umbraco.Tests.PublishedContent
internal class SolidPublishedContent : IPublishedContent
{
private readonly Dictionary<string, string> _names = new Dictionary<string, string>();
private readonly Dictionary<string, string> _urlSegments = new Dictionary<string, string>();
#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<string, PublishedCultureInfo> 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; }

View File

@@ -8,6 +8,7 @@ namespace Umbraco.Tests.TestHelpers.Stubs
internal class TestPublishedContent : PublishedElement, IPublishedContent
{
private readonly Dictionary<string, string> _names = new Dictionary<string, string>();
private readonly Dictionary<string, string> _urlSegments = new Dictionary<string, string>();
public TestPublishedContent(IPublishedContentType contentType, int id, Guid key, Dictionary<string, object> values, bool previewing, Dictionary<string, PublishedCultureInfo> 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<string, PublishedCultureInfo> 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; }

View File

@@ -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; }

View File

@@ -44,7 +44,7 @@ namespace Umbraco.Web.Models
public abstract string Name(string culture = null);
/// <inheritdoc />
public abstract string UrlSegment { get; }
public abstract string UrlSegment(string culture = null);
/// <inheritdoc />
public abstract int SortOrder { get; }

View File

@@ -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<string>();
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;
});
}

View File

@@ -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())
};
}

View File

@@ -200,19 +200,18 @@ namespace Umbraco.Web.PublishedCache.NuCache
}
/// <inheritdoc />
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;
}
/// <inheritdoc />

View File

@@ -146,7 +146,7 @@ namespace Umbraco.Web.PublishedCache
public override IReadOnlyDictionary<string, PublishedCultureInfo> 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;

View File

@@ -60,30 +60,6 @@ namespace Umbraco.Web
}
}
/// <summary>
/// Gets the Url segment.
/// </summary>
/// <remarks>
/// <para>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.</para>
/// </remarks>
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)