Introduced both Siblings and SiblingsAndSelf

This commit is contained in:
Bjarke Berg
2019-03-19 14:59:52 +01:00
parent e73029cf4f
commit e4c6ef05a9
2 changed files with 144 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ using Umbraco.Web;
using Umbraco.Web.PublishedCache;
using Umbraco.Core.Composing;
using Moq;
using Newtonsoft.Json;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
@@ -32,6 +33,8 @@ namespace Umbraco.Tests.PublishedContent
protected override void Compose()
{
base.Compose();
_publishedSnapshotAccessorMock = new Mock<IPublishedSnapshotAccessor>();
Composition.RegisterUnique<IPublishedSnapshotAccessor>(_publishedSnapshotAccessorMock.Object);
Composition.RegisterUnique<IPublishedModelFactory>(f => new PublishedModelFactory(f.GetInstance<TypeLoader>().GetTypes<PublishedContentModel>()));
Composition.RegisterUnique<IPublishedContentTypeFactory, PublishedContentTypeFactory>();
@@ -87,6 +90,7 @@ namespace Umbraco.Tests.PublishedContent
}
private readonly Guid _node1173Guid = Guid.NewGuid();
private Mock<IPublishedSnapshotAccessor> _publishedSnapshotAccessorMock;
protected override string GetXmlContent(int templateId)
{
@@ -792,6 +796,91 @@ namespace Umbraco.Tests.PublishedContent
Assert.IsTrue(customDoc3.IsDescendantOrSelf(customDoc3));
}
[Test]
public void SiblingsAndSelf()
{
// Structure:
// - Root : 1046 (no parent)
// -- Level1.1: 1173 (parent 1046)
// --- Level1.1.1: 1174 (parent 1173)
// --- Level1.1.2: 117 (parent 1173)
// --- Level1.1.3: 1177 (parent 1173)
// --- Level1.1.4: 1178 (parent 1173)
// --- Level1.1.5: 1176 (parent 1173)
// -- Level1.2: 1175 (parent 1046)
// -- Level1.3: 4444 (parent 1046)
var root = GetNode(1046);
var level1_1 = GetNode(1173);
var level1_1_1 = GetNode(1174);
var level1_1_2 = GetNode(117);
var level1_1_3 = GetNode(1177);
var level1_1_4 = GetNode(1178);
var level1_1_5 = GetNode(1176);
var level1_2 = GetNode(1175);
var level1_3 = GetNode(4444);
_publishedSnapshotAccessorMock.Setup(x => x.PublishedSnapshot.Content.GetAtRoot()).Returns(new []{root});
CollectionAssertAreEqual(new []{root}, root.SiblingsAndSelf());
CollectionAssertAreEqual( new []{level1_1, level1_2, level1_3}, level1_1.SiblingsAndSelf());
CollectionAssertAreEqual( new []{level1_1, level1_2, level1_3}, level1_2.SiblingsAndSelf());
CollectionAssertAreEqual( new []{level1_1, level1_2, level1_3}, level1_3.SiblingsAndSelf());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_2, level1_1_3, level1_1_4, level1_1_5}, level1_1_1.SiblingsAndSelf());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_2, level1_1_3, level1_1_4, level1_1_5}, level1_1_2.SiblingsAndSelf());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_2, level1_1_3, level1_1_4, level1_1_5}, level1_1_3.SiblingsAndSelf());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_2, level1_1_3, level1_1_4, level1_1_5}, level1_1_4.SiblingsAndSelf());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_2, level1_1_3, level1_1_4, level1_1_5}, level1_1_5.SiblingsAndSelf());
}
[Test]
public void Siblings()
{
// Structure:
// - Root : 1046 (no parent)
// -- Level1.1: 1173 (parent 1046)
// --- Level1.1.1: 1174 (parent 1173)
// --- Level1.1.2: 117 (parent 1173)
// --- Level1.1.3: 1177 (parent 1173)
// --- Level1.1.4: 1178 (parent 1173)
// --- Level1.1.5: 1176 (parent 1173)
// -- Level1.2: 1175 (parent 1046)
// -- Level1.3: 4444 (parent 1046)
var root = GetNode(1046);
var level1_1 = GetNode(1173);
var level1_1_1 = GetNode(1174);
var level1_1_2 = GetNode(117);
var level1_1_3 = GetNode(1177);
var level1_1_4 = GetNode(1178);
var level1_1_5 = GetNode(1176);
var level1_2 = GetNode(1175);
var level1_3 = GetNode(4444);
_publishedSnapshotAccessorMock.Setup(x => x.PublishedSnapshot.Content.GetAtRoot()).Returns(new []{root});
CollectionAssertAreEqual(new IPublishedContent[0], root.Siblings());
CollectionAssertAreEqual( new []{level1_2, level1_3}, level1_1.Siblings());
CollectionAssertAreEqual( new []{level1_1, level1_3}, level1_2.Siblings());
CollectionAssertAreEqual( new []{level1_1, level1_2}, level1_3.Siblings());
CollectionAssertAreEqual( new []{ level1_1_2, level1_1_3, level1_1_4, level1_1_5}, level1_1_1.Siblings());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_3, level1_1_4, level1_1_5}, level1_1_2.Siblings());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_2, level1_1_4, level1_1_5}, level1_1_3.Siblings());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_2, level1_1_3, level1_1_5}, level1_1_4.Siblings());
CollectionAssertAreEqual( new []{level1_1_1, level1_1_2, level1_1_3, level1_1_4}, level1_1_5.Siblings());
}
private void CollectionAssertAreEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
where T: IPublishedContent
{
var e = expected.Select(x => x.Id);
var a = actual.Select(x => x.Id);
CollectionAssert.AreEquivalent(e, a, $"\nExpected:\n{string.Join(", ", e)}\n\nActual:\n{string.Join(", ", a)}");
}
[Test]
public void FragmentProperty()

View File

@@ -1120,15 +1120,60 @@ namespace Umbraco.Web
#endregion
#region Axes: Parent Children (siblings including self)
#region Axes: Siblings
/// <summary>
/// Gets the children of the parent of the content.
/// Gets the siblings of the content.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The children of the parent of the content.</returns>
public static IEnumerable<IPublishedContent> ParentChildren(this IPublishedContent content, string culture = null)
/// <returns>The siblings of the content.</returns>
/// <remarks>
/// <para>Note that in V7 this method also return the content node self.</para>
/// </remarks>
public static IEnumerable<IPublishedContent> Siblings(this IPublishedContent content, string culture = null)
{
return SiblingsAndSelf(content, culture).Where(x => x.Id != content.Id);
}
/// <summary>
/// Gets the siblings of the content, of a given content type.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <param name="contentTypeAlias">The content type alias.</param>
/// <returns>The siblings of the content, of the given content type.</returns>
/// <remarks>
/// <para>Note that in V7 this method also return the content node self.</para>
/// </remarks>
public static IEnumerable<IPublishedContent> SiblingsOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
{
return SiblingsAndSelfOfType(content, contentTypeAlias, culture).Where(x => x.Id != content.Id);
}
/// <summary>
/// Gets the siblings of the content, of a given content type.
/// </summary>
/// <typeparam name="T">The content type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The siblings of the content, of the given content type.</returns>
/// <remarks>
/// <para>Note that in V7 this method also return the content node self.</para>
/// </remarks>
public static IEnumerable<IPublishedContent> Siblings<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
{
return SiblingsAndSelf<T>(content, culture).Where(x => x.Id != content.Id);
}
/// <summary>
/// Gets the siblings of the content including the node itself to indicate the position.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <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)
@@ -1136,13 +1181,13 @@ namespace Umbraco.Web
}
/// <summary>
/// Gets the children of the parent of the content, of a given content type.
/// Gets the siblings of the content including the node itself to indicate the position, of a given content type.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <param name="contentTypeAlias">The content type alias.</param>
/// <returns>The children of the parent of the content, of the given content type.</returns>
public static IEnumerable<IPublishedContent> ParentChildrenOfType(this IPublishedContent content, string contentTypeAlias, string culture = null)
/// <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)
@@ -1150,13 +1195,13 @@ namespace Umbraco.Web
}
/// <summary>
/// Gets the children of the parent of the content, of a given content type.
/// Gets the siblings of the content including the node itself to indicate the position, of a given content type.
/// </summary>
/// <typeparam name="T">The content type.</typeparam>
/// <param name="content">The content.</param>
/// <param name="culture">The specific culture to filter for. If null is used the current culture is used. (Default is null)</param>
/// <returns>The children of the parent of the content, of the given content type.</returns>
public static IEnumerable<T> ParentChildren<T>(this IPublishedContent content, string culture = null)
/// <returns>The siblings of the content including the node itself, of the given content type.</returns>
public static IEnumerable<T> SiblingsAndSelf<T>(this IPublishedContent content, string culture = null)
where T : class, IPublishedContent
{
return content.Parent != null