Refactor IPublishedContent.Parent()

This commit is contained in:
Stephan
2019-04-22 11:59:06 +02:00
parent a996d46b6f
commit 41e14a62c0
24 changed files with 62 additions and 67 deletions

View File

@@ -155,7 +155,7 @@ namespace Umbraco.Core.Models.PublishedContent
/// Gets the parent of the content item.
/// </summary>
/// <remarks>The parent of root content is <c>null</c>.</remarks>
IPublishedContent Parent { get; }
IPublishedContent Parent();
/// <summary>
/// Gets the children of the content item.

View File

@@ -113,7 +113,7 @@ namespace Umbraco.Core.Models.PublishedContent
#region Tree
/// <inheritdoc />
public virtual IPublishedContent Parent => _content.Parent;
public virtual IPublishedContent Parent() => _content.Parent();
/// <inheritdoc />
public virtual IEnumerable<IPublishedContent> Children => _content.Children;

View File

@@ -111,7 +111,7 @@ namespace Umbraco.Tests.Cache.PublishedCache
Assert.AreEqual(mRoot.Name, publishedMedia.Name());
Assert.AreEqual(mRoot.Path, publishedMedia.Path);
Assert.AreEqual(mRoot.SortOrder, publishedMedia.SortOrder);
Assert.IsNull(publishedMedia.Parent);
Assert.IsNull(publishedMedia.Parent());
}
[TestCase("id")]
@@ -212,7 +212,7 @@ namespace Umbraco.Tests.Cache.PublishedCache
var doc = store.CreateFromCacheValues(store.ConvertFromSearchResult(result));
DoAssert(doc, 1234, key, null, 0, "/media/test.jpg", "Image", 23, "Shannon", "Shannon", 0, 0, "-1,1234", DateTime.Parse("2012-07-17T10:34:09"), DateTime.Parse("2012-07-16T10:34:09"), 2);
Assert.AreEqual(null, doc.Parent);
Assert.AreEqual(null, doc.Parent());
}
[Test]
@@ -228,7 +228,7 @@ namespace Umbraco.Tests.Cache.PublishedCache
var doc = cache.CreateFromCacheValues(cache.ConvertFromXPathNavigator(navigator, true));
DoAssert(doc, 2000, key, null, 2, "image1", "Image", 23, "Shannon", "Shannon", 33, 33, "-1,2000", DateTime.Parse("2012-06-12T14:13:17"), DateTime.Parse("2012-07-20T18:50:43"), 1);
Assert.AreEqual(null, doc.Parent);
Assert.AreEqual(null, doc.Parent());
Assert.AreEqual(2, doc.Children.Count());
Assert.AreEqual(2001, doc.Children.ElementAt(0).Id);
Assert.AreEqual(2002, doc.Children.ElementAt(1).Id);

View File

@@ -139,7 +139,7 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache
private readonly Func<DictionaryPublishedContent, string, IPublishedProperty> _getProperty;
private readonly IAppCache _appCache;
public override IPublishedContent Parent => _getParent.Value;
public override IPublishedContent Parent() => _getParent.Value;
public int ParentId { get; private set; }

View File

@@ -273,14 +273,14 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache
pathParts.Add(urlName);
// move to parent node
n = n.Parent;
n = n.Parent();
hasDomains = n != null && _domainCache.HasAssigned(n.Id);
}
// no domain, respect HideTopLevelNodeFromPath for legacy purposes
if (hasDomains == false && _globalSettings.HideTopLevelNodeFromPath)
{
if (node.Parent == null)
if (node.Parent() == null)
{
var rootNode = GetByRoute(preview, "/", true);
if (rootNode == null)

View File

@@ -91,13 +91,10 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache
return _properties.TryGetValue(alias, out property) ? property : null;
}
public override IPublishedContent Parent
public override IPublishedContent Parent()
{
get
{
EnsureNodeInitialized(andParent: true);
return _parent;
}
EnsureNodeInitialized(andParent: true);
return _parent;
}
public override int Id

View File

@@ -274,7 +274,7 @@ namespace Umbraco.Tests.Published
// ReSharper disable UnassignedGetOnlyAutoProperty
public override bool IsDraft(string culture = null) => false;
public override bool IsPublished(string culture = null) => true;
public override IPublishedContent Parent { get; }
public override IPublishedContent Parent() => null;
public override IEnumerable<IPublishedContent> Children { get; }
public override IPublishedContentType ContentType { get; }
// ReSharper restore UnassignedGetOnlyAutoProperty

View File

@@ -188,10 +188,7 @@ namespace Umbraco.Tests.PublishedContent
public string Url(string culture = null, UrlMode mode = UrlMode.Auto) => default;
IPublishedContent IPublishedContent.Parent
{
get { return Parent; }
}
IPublishedContent IPublishedContent.Parent() => Parent;
IEnumerable<IPublishedContent> IPublishedContent.Children
{
@@ -238,7 +235,7 @@ namespace Umbraco.Tests.PublishedContent
IPublishedContent content = this;
while (content != null && (property == null || property.HasValue() == false))
{
content = content.Parent;
content = content.Parent();
property = content == null ? null : content.GetProperty(alias);
}

View File

@@ -178,10 +178,10 @@ namespace Umbraco.Tests.PublishedContent
item3.SetUrl("/content-1/content-2/content-3");
item1.Children = new List<IPublishedContent> { item2 };
item2.Parent = item1;
item2.SetParent(item1);
item2.Children = new List<IPublishedContent> { item3 };
item3.Parent = item2;
item3.SetParent(item2);
cache.Add(item1);
cache.Add(item2);

View File

@@ -416,13 +416,13 @@ namespace Umbraco.Tests.PublishedContent
var mSubChild3 = MakeNewMedia("SubChild3", mType, user, mChild1.Id);
var publishedRoot = GetNode(mRoot.Id);
Assert.AreEqual(null, publishedRoot.Parent);
Assert.AreEqual(null, publishedRoot.Parent());
var publishedChild1 = GetNode(mChild1.Id);
Assert.AreEqual(mRoot.Id, publishedChild1.Parent.Id);
Assert.AreEqual(mRoot.Id, publishedChild1.Parent().Id);
var publishedSubChild1 = GetNode(mSubChild1.Id);
Assert.AreEqual(mChild1.Id, publishedSubChild1.Parent.Id);
Assert.AreEqual(mChild1.Id, publishedSubChild1.Parent().Id);
}
[Test]

View File

@@ -64,7 +64,7 @@ namespace Umbraco.Tests.PublishedContent
pc.Setup(content => content.CreateDate).Returns(DateTime.Now);
pc.Setup(content => content.UpdateDate).Returns(DateTime.Now);
pc.Setup(content => content.Path).Returns("-1,1");
pc.Setup(content => content.Parent).Returns(() => null);
pc.Setup(content => content.Parent()).Returns(() => null);
pc.Setup(content => content.Properties).Returns(new Collection<IPublishedProperty>());
pc.Setup(content => content.ContentType).Returns(new PublishedContentType(22, "anything", PublishedItemType.Content, Enumerable.Empty<string>(), Enumerable.Empty<PublishedPropertyType>(), ContentVariation.Nothing));
return pc;

View File

@@ -20,7 +20,7 @@ namespace Umbraco.Tests.PublishedContent
content = ctx.ContentCache.GetById(1046);
Assert.IsNotNull(content);
Assert.AreEqual(1, content.Level);
Assert.IsNull(content.Parent);
Assert.IsNull(content.Parent());
// non-existing content is null
content = ctx.ContentCache.GetById(666);

View File

@@ -99,7 +99,7 @@ namespace Umbraco.Tests.PublishedContent
public override IEnumerable<IPublishedContent> GetAtRoot(bool preview)
{
return _content.Values.Where(x => x.Parent == null);
return _content.Values.Where(x => x.Parent() == null);
}
public override IPublishedContent GetSingleByXPath(bool preview, string xpath, Core.Xml.XPathVariable[] vars)
@@ -209,7 +209,9 @@ namespace Umbraco.Tests.PublishedContent
public int ParentId { get; set; }
public IEnumerable<int> ChildIds { get; set; }
public IPublishedContent Parent { get; set; }
private IPublishedContent _parent;
public IPublishedContent Parent() => _parent;
public void SetParent(IPublishedContent parent) => _parent = parent;
public IEnumerable<IPublishedContent> Children { get; set; }
#endregion
@@ -237,7 +239,7 @@ namespace Umbraco.Tests.PublishedContent
IPublishedContent content = this;
while (content != null && (property == null || property.HasValue() == false))
{
content = content.Parent;
content = content.Parent();
property = content == null ? null : content.GetProperty(alias);
}

View File

@@ -53,7 +53,9 @@ namespace Umbraco.Tests.TestHelpers.Stubs
public string Url(string culture = null, UrlMode mode = UrlMode.Auto) => throw new NotSupportedException();
public bool IsDraft(string culture = null) => false;
public bool IsPublished(string culture = null) => true;
public IPublishedContent Parent { get; set; }
private IPublishedContent _parent;
public IPublishedContent Parent() => _parent;
public void SetParent(IPublishedContent parent) => _parent = parent;
public IEnumerable<IPublishedContent> Children { get; set; }
// copied from PublishedContentBase
@@ -66,7 +68,7 @@ namespace Umbraco.Tests.TestHelpers.Stubs
var firstNonNullProperty = property;
while (content != null && (property == null || property.HasValue() == false))
{
content = content.Parent;
content = content.Parent();
property = content?.GetProperty(alias);
if (firstNonNullProperty == null && property != null) firstNonNullProperty = property;
}

View File

@@ -37,7 +37,7 @@ namespace Umbraco.Web.Macros
PopulatePageData(frequest.PublishedContent.Id,
frequest.PublishedContent.Name(), frequest.PublishedContent.ContentType.Id, frequest.PublishedContent.ContentType.Alias,
frequest.PublishedContent.WriterName, frequest.PublishedContent.CreatorName, frequest.PublishedContent.CreateDate, frequest.PublishedContent.UpdateDate,
frequest.PublishedContent.Path, frequest.PublishedContent.Parent?.Id ?? -1);
frequest.PublishedContent.Path, frequest.PublishedContent.Parent()?.Id ?? -1);
if (frequest.HasTemplate)
{
@@ -59,7 +59,7 @@ namespace Umbraco.Web.Macros
PopulatePageData(doc.Id,
doc.Name(), doc.ContentType.Id, doc.ContentType.Alias,
doc.WriterName, doc.CreatorName, doc.CreateDate, doc.UpdateDate,
doc.Path, doc.Parent?.Id ?? -1);
doc.Path, doc.Parent()?.Id ?? -1);
if (doc.TemplateId.HasValue)
{
@@ -182,8 +182,8 @@ namespace Umbraco.Web.Macros
{
private readonly IContent _inner;
private readonly IPublishedProperty[] _properties;
private IReadOnlyDictionary<string, PublishedCultureInfo> _cultureInfos;
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly IPublishedContent _parent;
private static readonly IReadOnlyDictionary<string, PublishedCultureInfo> NoCultureInfos = new Dictionary<string, PublishedCultureInfo>();
@@ -215,7 +215,7 @@ namespace Umbraco.Web.Macros
.Cast<IPublishedProperty>()
.ToArray();
Parent = new PagePublishedContent(_inner.ParentId);
_parent = new PagePublishedContent(_inner.ParentId);
}
public IPublishedContentType ContentType { get; }
@@ -288,7 +288,7 @@ namespace Umbraco.Web.Macros
throw new NotImplementedException();
}
public IPublishedContent Parent { get; }
public IPublishedContent Parent() => _parent;
public IEnumerable<IPublishedContent> Children => throw new NotImplementedException();

View File

@@ -160,7 +160,7 @@ namespace Umbraco.Web.Models.PublishedContent
IPublishedProperty property; // if we are here, content's property has no value
do
{
content = content.Parent;
content = content.Parent();
var propertyType = content?.ContentType.GetPropertyType(alias);

View File

@@ -151,7 +151,7 @@ namespace Umbraco.Web.Models
#region Tree
/// <inheritdoc />
public abstract IPublishedContent Parent { get; }
public abstract IPublishedContent Parent();
/// <inheritdoc />
public abstract IEnumerable<IPublishedContent> Children { get; }

View File

@@ -157,7 +157,7 @@ namespace Umbraco.Web.PublishedCache.NuCache
pathParts.Add(urlSegment);
// move to parent node
n = n.Parent;
n = n.Parent();
if (n != null)
urlSegment = n.UrlSegment(culture);
@@ -206,7 +206,7 @@ namespace Umbraco.Web.PublishedCache.NuCache
// "/foo" fails (looking for "/*/foo") we try also "/foo".
// this does not make much sense anyway esp. if both "/foo/" and "/bar/foo" exist, but
// that's the way it works pre-4.10 and we try to be backward compat for the time being
if (content.Parent == null)
if (content.Parent() == null)
{
var rootNode = GetByRoute(preview, "/", true);
if (rootNode == null)

View File

@@ -325,21 +325,18 @@ namespace Umbraco.Web.PublishedCache.NuCache
#region Tree
/// <inheritdoc />
public override IPublishedContent Parent
public override IPublishedContent Parent()
{
get
// have to use the "current" cache because a PublishedContent can be shared
// amongst many snapshots and other content depend on the snapshots
switch (_contentNode.ContentType.ItemType)
{
// have to use the "current" cache because a PublishedContent can be shared
// amongst many snapshots and other content depend on the snapshots
switch (_contentNode.ContentType.ItemType)
{
case PublishedItemType.Content:
return GetContentById(IsPreviewing, _contentNode.ParentContentId);
case PublishedItemType.Media:
return GetMediaById(IsPreviewing, _contentNode.ParentContentId);
default:
throw new Exception($"Panic: unsupported item type \"{_contentNode.ContentType.ItemType}\".");
}
case PublishedItemType.Content:
return GetContentById(IsPreviewing, _contentNode.ParentContentId);
case PublishedItemType.Media:
return GetMediaById(IsPreviewing, _contentNode.ParentContentId);
default:
throw new Exception($"Panic: unsupported item type \"{_contentNode.ContentType.ItemType}\".");
}
}

View File

@@ -82,7 +82,7 @@ namespace Umbraco.Web.PublishedCache
public override bool IsPublished(string culture = null) => true;
public override IPublishedContent Parent => null;
public override IPublishedContent Parent() => null;
public override IEnumerable<IPublishedContent> Children => Enumerable.Empty<IPublishedContent>();

View File

@@ -548,7 +548,7 @@ namespace Umbraco.Web
/// <remarks>This method is here for consistency purposes but does not make much sense.</remarks>
public static IPublishedContent Ancestor(this IPublishedContent content)
{
return content.Parent;
return content.Parent();
}
/// <summary>
@@ -680,7 +680,7 @@ namespace Umbraco.Web
{
if (content == null) throw new ArgumentNullException(nameof(content));
if (orSelf) yield return content;
while ((content = content.Parent) != null)
while ((content = content.Parent()) != null)
yield return content;
}
@@ -884,7 +884,7 @@ namespace Umbraco.Web
where T : class, IPublishedContent
{
if (content == null) throw new ArgumentNullException(nameof(content));
return content.Parent as T;
return content.Parent() as T;
}
#endregion
@@ -1121,8 +1121,8 @@ namespace Umbraco.Web
/// <returns>The siblings of the content including the node itself.</returns>
public static IEnumerable<IPublishedContent> SiblingsAndSelf(this IPublishedContent content, string culture = null)
{
return content.Parent != null
? content.Parent.Children(culture)
return content.Parent() != null
? content.Parent().Children(culture)
: PublishedSnapshot.Content.GetAtRoot().WhereIsInvariantOrHasCulture(culture);
}
@@ -1135,8 +1135,8 @@ namespace Umbraco.Web
/// <returns>The siblings of the content including the node itself, of the given content type.</returns>
public static IEnumerable<IPublishedContent> SiblingsAndSelfOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
{
return content.Parent != null
? content.Parent.ChildrenOfType(contentTypeAlias, culture)
return content.Parent() != null
? content.Parent().ChildrenOfType(contentTypeAlias, culture)
: PublishedSnapshot.Content.GetAtRoot().OfTypes(contentTypeAlias).WhereIsInvariantOrHasCulture(culture);
}
@@ -1150,8 +1150,8 @@ namespace Umbraco.Web
public static IEnumerable<T> SiblingsAndSelf<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
{
return content.Parent != null
? content.Parent.Children<T>(culture)
return content.Parent() != null
? content.Parent().Children<T>(culture)
: PublishedSnapshot.Content.GetAtRoot().OfType<T>().WhereIsInvariantOrHasCulture(culture);
}

View File

@@ -65,7 +65,7 @@ namespace Umbraco.Web.Routing
while (domainUris == null && n != null) // n is null at root
{
// move to parent node
n = n.Parent;
n = n.Parent();
domainUris = n == null ? null : DomainUtilities.DomainsForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainHelper, n.Id, current, excludeDefault: false);
}

View File

@@ -87,7 +87,7 @@ namespace Umbraco.Web.Routing
var domainUris = DomainUtilities.DomainsForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainHelper, n.Id, current, false);
while (domainUris == null && n != null) // n is null at root
{
n = n.Parent; // move to parent node
n = n.Parent(); // move to parent node
domainUris = n == null ? null : DomainUtilities.DomainsForNode(umbracoContext.PublishedSnapshot.Domains, _siteDomainHelper, n.Id, current, excludeDefault: true);
}

View File

@@ -188,7 +188,7 @@ namespace Umbraco.Web.Routing
while (o != null)
{
l.Add(o.Name());
o = o.Parent;
o = o.Parent();
}
l.Reverse();
var s = "/" + string.Join("/", l) + " (id=" + pcr.PublishedContent.Id + ")";