Skip to content

Commit

Permalink
Fixes bug in AllChildren extension that didnt pass the include childr…
Browse files Browse the repository at this point in the history
…en poredicate to the recursive function
  • Loading branch information
Philo committed Nov 30, 2016
1 parent 7759db6 commit 4672b74
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Sitemapify.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SitemapifyUmbracoSample", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sitemapify.CastleWindsor", "src\Sitemapify.CastleWindsor\Sitemapify.CastleWindsor.csproj", "{D8A6AE48-A51D-4CF4-BCD1-47036A7443D4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0D96622F-D175-48DC-B230-675BFA77A987}"
ProjectSection(SolutionItems) = preProject
cake.config = cake.config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
2 changes: 1 addition & 1 deletion src/Sitemapify.Umbraco/Extensions/ContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static IEnumerable<IPublishedContent> AllChildren(this IPublishedContent
{
foreach (var child in node.Children(predicate))
{
foreach (var grandChild in child.AllChildren(predicate))
foreach (var grandChild in child.AllChildren(predicate, includeChildren))
yield return grandChild;
}
}
Expand Down
131 changes: 129 additions & 2 deletions test/Sitemapify.Tests/SitemapUrlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Runtime.Caching;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
Expand All @@ -17,7 +18,7 @@

namespace Sitemapify.Tests
{

public class SitemapContentProviderTests
{
public SitemapContentProviderTests()
Expand All @@ -42,11 +43,137 @@ public void When()
(from urls in
document.Descendants(XName.Get("urlset", SitemapUrl.SitemapNs))
.Elements(XName.Get("url", SitemapUrl.SitemapNs))
select urls).Count().Should().Be(1);
select urls).Count().Should().Be(1);
}
}

public class SitemapUrlTests
{
}

public class SitemapifyRecursiveContentLookupTests
{
public class ContentModelStub
{
public ContentModelStub(ContentModelStub parent)
{
Parent = parent;
}

public string Id { get; set; } = Guid.NewGuid().ToString("N");

public ContentModelStub Parent { get; }

public bool HideFromSitemap { get; set; }

public bool HideChildrenFromSitemap { get; set; }

public IEnumerable<ContentModelStub> ChildNodes { get; set; }

public IEnumerable<ContentModelStub> Children(Func<ContentModelStub, bool> where = null)
{
if (where == null) return Enumerable.Empty<ContentModelStub>();
return ChildNodes?.Where(where) ?? Enumerable.Empty<ContentModelStub>();
}

public override string ToString()
{
return $"({Parent}-{HideFromSitemap}-{HideChildrenFromSitemap})";
}
}

public IEnumerable<ContentModelStub> Matching(ContentModelStub node, Func<ContentModelStub, bool> predicate = null, Func<ContentModelStub, bool> includeChildren = null)
{
if (predicate?.Invoke(node) ?? true)
{
yield return node;
}

if (includeChildren?.Invoke(node) ?? true)
{
foreach (var child in node.Children(predicate))
{
foreach (var grandChild in Matching(child, predicate, includeChildren))
{
yield return grandChild;
}
}
}
}

[Fact]
public void TopNodeIsHidden()
{
// arrange
var top = new ContentModelStub(null);
top.HideFromSitemap = true;

var result = Matching(top, c => !c.HideFromSitemap, c => !c.HideChildrenFromSitemap).ToList();
result.Count.Should().Be(0);
}

[Fact]
public void TopNodeIsVisible()
{
// arrange
var top = new ContentModelStub(null);

var result = Matching(top, c => !c.HideFromSitemap, c => !c.HideChildrenFromSitemap).ToList();
result.Count.Should().Be(1);
}

[Fact]
public void TopNodeHasChildren()
{
// arrange
var top = new ContentModelStub(null);
top.ChildNodes = new[]
{
new ContentModelStub(top),
new ContentModelStub(top),
new ContentModelStub(top)
};

var result = Matching(top, c => !c.HideFromSitemap, c => !c.HideChildrenFromSitemap).ToList();
result.Count.Should().Be(4);
}

[Fact]
public void TopNodeHasHiddenChildren()
{
// arrange
var top = new ContentModelStub(null);
top.HideChildrenFromSitemap = true;
top.ChildNodes = new[]
{
new ContentModelStub(top),
new ContentModelStub(top),
new ContentModelStub(top)
};

var result = Matching(top, c => !c.HideFromSitemap, c => !c.HideChildrenFromSitemap).ToList();
result.Count.Should().Be(1);
}

[Fact]
public void TopNodeTree()
{
// arrange
var top = new ContentModelStub(null);

var child = new ContentModelStub(top) {HideChildrenFromSitemap = true};
child.ChildNodes = new[]
{
new ContentModelStub(child)
};

top.ChildNodes = new[]
{
child
};

var result = Matching(top, c => !c.HideFromSitemap, c => !c.HideChildrenFromSitemap).ToList();
result.Count.Should().Be(2);
}
}
}

0 comments on commit 4672b74

Please sign in to comment.