From e42995cf101347d82ac3069ccb293711fde33f57 Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 17 Jan 2019 18:03:07 +0100 Subject: [PATCH] Fixes for the IPublishedContent Linq/Culture PR --- .../PublishedContentDataTableTests.cs | 74 ++++----- .../PublishedContent/PublishedContentTests.cs | 156 +++++++++--------- .../Accessors/TestVariationContextAccessor.cs | 6 +- .../DictionaryPublishedContent.cs | 5 +- .../XmlPublishedCache/XmlPublishedContent.cs | 5 +- src/Umbraco.Web/PublishedContentExtensions.cs | 27 ++- 6 files changed, 140 insertions(+), 133 deletions(-) diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs index fa60b050cc..73f3cd1537 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentDataTableTests.cs @@ -73,44 +73,44 @@ namespace Umbraco.Tests.PublishedContent Umbraco.Web.PublishedContentExtensions.GetPropertyAliasesAndNames = null; } -// [Test] -// public void To_DataTable() -// { -// var doc = GetContent(true, 1); -// var dt = doc.ChildrenAsTable(Current.Services); -// -// Assert.AreEqual(11, dt.Columns.Count); -// Assert.AreEqual(3, dt.Rows.Count); -// Assert.AreEqual("value4", dt.Rows[0]["Property 1"]); -// Assert.AreEqual("value5", dt.Rows[0]["Property 2"]); -// Assert.AreEqual("value6", dt.Rows[0]["Property 4"]); -// Assert.AreEqual("value7", dt.Rows[1]["Property 1"]); -// Assert.AreEqual("value8", dt.Rows[1]["Property 2"]); -// Assert.AreEqual("value9", dt.Rows[1]["Property 4"]); -// Assert.AreEqual("value10", dt.Rows[2]["Property 1"]); -// Assert.AreEqual("value11", dt.Rows[2]["Property 2"]); -// Assert.AreEqual("value12", dt.Rows[2]["Property 4"]); -// } + [Test] + public void To_DataTable() + { + var doc = GetContent(true, 1); + var dt = doc.ChildrenAsTable(Current.Services); -// [Test] -// public void To_DataTable_With_Filter() -// { -// var doc = GetContent(true, 1); -// //change a doc type alias -// var c = (TestPublishedContent) doc.Children.ElementAt(0); -// c.ContentType = new PublishedContentType(22, "DontMatch", PublishedItemType.Content, Enumerable.Empty(), Enumerable.Empty(), ContentVariation.Nothing); -// -// var dt = doc.ChildrenAsTable(Current.Services, "Child"); -// -// Assert.AreEqual(11, dt.Columns.Count); -// Assert.AreEqual(2, dt.Rows.Count); -// Assert.AreEqual("value7", dt.Rows[0]["Property 1"]); -// Assert.AreEqual("value8", dt.Rows[0]["Property 2"]); -// Assert.AreEqual("value9", dt.Rows[0]["Property 4"]); -// Assert.AreEqual("value10", dt.Rows[1]["Property 1"]); -// Assert.AreEqual("value11", dt.Rows[1]["Property 2"]); -// Assert.AreEqual("value12", dt.Rows[1]["Property 4"]); -// } + Assert.AreEqual(11, dt.Columns.Count); + Assert.AreEqual(3, dt.Rows.Count); + Assert.AreEqual("value4", dt.Rows[0]["Property 1"]); + Assert.AreEqual("value5", dt.Rows[0]["Property 2"]); + Assert.AreEqual("value6", dt.Rows[0]["Property 4"]); + Assert.AreEqual("value7", dt.Rows[1]["Property 1"]); + Assert.AreEqual("value8", dt.Rows[1]["Property 2"]); + Assert.AreEqual("value9", dt.Rows[1]["Property 4"]); + Assert.AreEqual("value10", dt.Rows[2]["Property 1"]); + Assert.AreEqual("value11", dt.Rows[2]["Property 2"]); + Assert.AreEqual("value12", dt.Rows[2]["Property 4"]); + } + + [Test] + public void To_DataTable_With_Filter() + { + var doc = GetContent(true, 1); + //change a doc type alias + var c = (TestPublishedContent)doc.Children.ElementAt(0); + c.ContentType = new PublishedContentType(22, "DontMatch", PublishedItemType.Content, Enumerable.Empty(), Enumerable.Empty(), ContentVariation.Nothing); + + var dt = doc.ChildrenAsTable(Current.Services, "Child"); + + Assert.AreEqual(11, dt.Columns.Count); + Assert.AreEqual(2, dt.Rows.Count); + Assert.AreEqual("value7", dt.Rows[0]["Property 1"]); + Assert.AreEqual("value8", dt.Rows[0]["Property 2"]); + Assert.AreEqual("value9", dt.Rows[0]["Property 4"]); + Assert.AreEqual("value10", dt.Rows[1]["Property 1"]); + Assert.AreEqual("value11", dt.Rows[1]["Property 2"]); + Assert.AreEqual("value12", dt.Rows[1]["Property 4"]); + } [Test] public void To_DataTable_No_Rows() diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs index 855d864cc4..603464e18b 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs @@ -326,18 +326,18 @@ namespace Umbraco.Tests.PublishedContent } } -// [Test] -// public void Descendants_Ordered_Properly() -// { -// var doc = GetNode(1046); -// -// var expected = new[] { 1046, 1173, 1174, 117, 1177, 1178, 1179, 1176, 1175, 4444, 1172 }; -// var exindex = 0; -// -// // must respect the XPath descendants-or-self axis! -// foreach (var d in doc.DescendantsOrSelf()) -// Assert.AreEqual(expected[exindex++], d.Id); -// } + [Test] + public void Descendants_Ordered_Properly() + { + var doc = GetNode(1046); + + var expected = new[] { 1046, 1173, 1174, 117, 1177, 1178, 1179, 1176, 1175, 4444, 1172 }; + var exindex = 0; + + // must respect the XPath descendants-or-self axis! + foreach (var d in doc.DescendantsOrSelf()) + Assert.AreEqual(expected[exindex++], d.Id); + } [Test] public void Get_Property_Value_Recursive() @@ -367,18 +367,18 @@ namespace Umbraco.Tests.PublishedContent Assert.AreEqual("
This is some content
", propVal3.ToString()); } -// [Test] -// public void Complex_Linq() -// { -// var doc = GetNode(1173); -// -// var result = doc.Ancestors().OrderBy(x => x.Level) -// .Single() -// .Descendants() -// .FirstOrDefault(x => x.Value("selectedNodes", defaultValue: "").Split(',').Contains("1173")); -// -// Assert.IsNotNull(result); -// } + [Test] + public void Complex_Linq() + { + var doc = GetNode(1173); + + var result = doc.Ancestors().OrderBy(x => x.Level) + .Single() + .Descendants() + .FirstOrDefault(x => x.Value("selectedNodes", defaultValue: "").Split(',').Contains("1173")); + + Assert.IsNotNull(result); + } [Test] public void Children_GroupBy_DocumentTypeAlias() @@ -437,36 +437,36 @@ namespace Umbraco.Tests.PublishedContent } -// [Test] -// public void FirstChild() -// { -// var doc = GetNode(1173); // has child nodes -// Assert.IsNotNull(doc.FirstChild()); -// Assert.IsNotNull(doc.FirstChild(x => true)); -// Assert.IsNotNull(doc.FirstChild()); -// -// doc = GetNode(1175); // does not have child nodes -// Assert.IsNull(doc.FirstChild()); -// Assert.IsNull(doc.FirstChild(x => true)); -// Assert.IsNull(doc.FirstChild()); -// } + [Test] + public void FirstChild() + { + var doc = GetNode(1173); // has child nodes + Assert.IsNotNull(doc.FirstChild()); + Assert.IsNotNull(doc.FirstChild(x => true)); + Assert.IsNotNull(doc.FirstChild()); -// [Test] -// public void FirstChildAsT() -// { -// var doc = GetNode(1046); // has child nodes -// -// var model = doc.FirstChild(x => true); // predicate -// -// Assert.IsNotNull(model); -// Assert.IsTrue(model.Id == 1173); -// Assert.IsInstanceOf(model); -// Assert.IsInstanceOf(model); -// -// doc = GetNode(1175); // does not have child nodes -// Assert.IsNull(doc.FirstChild()); -// Assert.IsNull(doc.FirstChild(x => true)); -// } + doc = GetNode(1175); // does not have child nodes + Assert.IsNull(doc.FirstChild()); + Assert.IsNull(doc.FirstChild(x => true)); + Assert.IsNull(doc.FirstChild()); + } + + [Test] + public void FirstChildAsT() + { + var doc = GetNode(1046); // has child nodes + + var model = doc.FirstChild(x => true); // predicate + + Assert.IsNotNull(model); + Assert.IsTrue(model.Id == 1173); + Assert.IsInstanceOf(model); + Assert.IsInstanceOf(model); + + doc = GetNode(1175); // does not have child nodes + Assert.IsNull(doc.FirstChild()); + Assert.IsNull(doc.FirstChild(x => true)); + } [Test] public void IsComposedOf() @@ -668,31 +668,31 @@ namespace Umbraco.Tests.PublishedContent } -// [Test] -// public void Descendants_Or_Self() -// { -// var doc = GetNode(1046); -// -// var result = doc.DescendantsOrSelf().ToArray(); -// -// Assert.IsNotNull(result); -// -// Assert.AreEqual(10, result.Count()); -// Assert.IsTrue(result.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1046, 1173, 1174, 1176, 1175 })); -// } -// -// [Test] -// public void Descendants() -// { -// var doc = GetNode(1046); -// -// var result = doc.Descendants().ToArray(); -// -// Assert.IsNotNull(result); -// -// Assert.AreEqual(9, result.Count()); -// Assert.IsTrue(result.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1173, 1174, 1176, 1175, 4444 })); -// } + [Test] + public void Descendants_Or_Self() + { + var doc = GetNode(1046); + + var result = doc.DescendantsOrSelf().ToArray(); + + Assert.IsNotNull(result); + + Assert.AreEqual(10, result.Count()); + Assert.IsTrue(result.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1046, 1173, 1174, 1176, 1175 })); + } + + [Test] + public void Descendants() + { + var doc = GetNode(1046); + + var result = doc.Descendants().ToArray(); + + Assert.IsNotNull(result); + + Assert.AreEqual(9, result.Count()); + Assert.IsTrue(result.Select(x => ((dynamic)x).Id).ContainsAll(new dynamic[] { 1173, 1174, 1176, 1175, 4444 })); + } [Test] public void IsDescendant() diff --git a/src/Umbraco.Tests/Testing/Objects/Accessors/TestVariationContextAccessor.cs b/src/Umbraco.Tests/Testing/Objects/Accessors/TestVariationContextAccessor.cs index 4b56799fa4..134b709447 100644 --- a/src/Umbraco.Tests/Testing/Objects/Accessors/TestVariationContextAccessor.cs +++ b/src/Umbraco.Tests/Testing/Objects/Accessors/TestVariationContextAccessor.cs @@ -8,10 +8,6 @@ namespace Umbraco.Tests.Testing.Objects.Accessors public class TestVariationContextAccessor : IVariationContextAccessor { /// - public VariationContext VariationContext - { - get; - set; - } = new VariationContext(); + public VariationContext VariationContext { get; set; } } } diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/DictionaryPublishedContent.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/DictionaryPublishedContent.cs index 6f6a39144a..c418b7a525 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/DictionaryPublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/DictionaryPublishedContent.cs @@ -154,9 +154,10 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache public override string Name => _name; - public override PublishedCultureInfo GetCulture(string culture = null) => throw new NotSupportedException(); + public override PublishedCultureInfo GetCulture(string culture = null) => null; - public override IReadOnlyDictionary Cultures => throw new NotSupportedException(); + private static readonly Lazy> NoCultures = new Lazy>(() => new Dictionary()); + public override IReadOnlyDictionary Cultures => NoCultures.Value; public override string UrlSegment => _urlName; diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedContent.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedContent.cs index d8436c05b5..65c7dc7229 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlPublishedContent.cs @@ -136,9 +136,10 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache } } - public override PublishedCultureInfo GetCulture(string culture = null) => throw new NotSupportedException(); + public override PublishedCultureInfo GetCulture(string culture = null) => null; - public override IReadOnlyDictionary Cultures => new Dictionary(); + private static readonly Lazy> NoCultures = new Lazy>(() => new Dictionary()); + public override IReadOnlyDictionary Cultures => NoCultures.Value; public override string WriterName { diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index 4efe6bc82b..0ada3d8c5b 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -267,19 +267,20 @@ namespace Umbraco.Web => content.Cultures.ContainsKey(culture); /// - /// Filter by the specific culture or invariant. If null is provided, the method uses the current culture instead. + /// Filters a sequence of to return invariant items, and items that are published for the specified culture. /// - /// The content. - /// The specific culture to filter for. If null is used the current culture is used. (Default is null) - /// These of the inputs that has the specified culture or are invariant. - internal static IEnumerable WhereHasCultureOrInvariant(this IEnumerable contents, string culture = null) + /// The content items. + /// The specific culture to filter for. If null is used the current culture is used. (Default is null). + internal static IEnumerable WhereIsInvariantOrHasCulture(this IEnumerable contents, string culture = null) { if (contents == null) throw new ArgumentNullException(nameof(contents)); - var actualCulture = culture ?? Current.VariationContextAccessor.VariationContext.Culture; + culture = culture ?? Current.VariationContextAccessor.VariationContext?.Culture ?? ""; - return contents.Where(x=>x.HasCulture(actualCulture) || !x.Cultures.Any()); + // either does not vary by culture, or has the specified culture + return contents.Where(x => !x.ContentType.VariesByCulture() || x.HasCulture(culture)); } + #endregion #region Search @@ -1036,7 +1037,6 @@ namespace Umbraco.Web #region Axes: children - /// /// Gets the children of the content. /// @@ -1050,7 +1050,16 @@ namespace Umbraco.Web public static IEnumerable Children(this IPublishedContent content, string culture = null) { if (content == null) throw new ArgumentNullException(nameof(content)); - return content.Children.WhereHasCultureOrInvariant(culture); + + // + return content.Children.Where(x => + { + if (!x.ContentType.VariesByCulture()) return true; // invariant = always ok + return x.HasCulture(culture); + return false; + }); + + return content.Children.WhereIsInvariantOrHasCulture(culture); } ///