Skip to content

Commit

Permalink
Add appendPageQueryParam parameter (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbristow authored May 17, 2020
1 parent 90829d5 commit 81d52a3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 22 deletions.
17 changes: 12 additions & 5 deletions src/SimpleSitemap/SitemapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ public class SitemapNode
public SitemapNode(Uri url,
DateTime lastModified,
SitemapFrequency? frequency = SitemapFrequency.Daily,
double? priority = 0.5)
double? priority = 0.5,
bool appendPageQueryParam = true)
{
Url = url;
Priority = priority;
Frequency = frequency;
LastModified = lastModified;
AppendPageQueryParam = appendPageQueryParam;
}

public Uri Url { get; set; }
public DateTimeOffset LastModified { get; set; }
public SitemapFrequency? Frequency { get; set; }
public double? Priority { get; set; }
public Uri Url { get; }

public DateTimeOffset LastModified { get; }

public SitemapFrequency? Frequency { get; }

public double? Priority { get; }

public bool AppendPageQueryParam { get; }

public override string ToString()
{
Expand Down
19 changes: 16 additions & 3 deletions src/SimpleSitemap/SitemapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ private static XElement CreateXmlSitemapIndex(ICollection<SitemapNode> sitemapNo
var page = 0;
foreach (var sitemapNode in sitemapNodes)
{
var loc = new XElement(xmlns + "loc",
Uri.EscapeUriString(string.Format("{0}/?page={1}", sitemapNode.Url, ++page)));

page++;

var loc = GenerateLocXElement(sitemapNode, page, sitemapNode.AppendPageQueryParam);

var lastMod = new XElement(xmlns + "lastmod",
sitemapNode.LastModified.ToString("yyyy-MM-ddTHH:mm:sszzz", CultureInfo.InvariantCulture));

Expand All @@ -81,6 +82,18 @@ private static XElement CreateXmlSitemapIndex(ICollection<SitemapNode> sitemapNo
}

return root;

XElement GenerateLocXElement(SitemapNode sitemapNode, int page, bool appendPageQueryParam)
{
var uriString = sitemapNode.Url.ToString();

if (appendPageQueryParam)
{
uriString = $"{uriString}/?page={page}";
}

return new XElement(xmlns + "loc", uriString);
}
}

private static XElement CreateXmlUrlSet(IEnumerable<SitemapNode> sitemapNodes)
Expand Down
39 changes: 27 additions & 12 deletions tests/SimpleSiteMap.Tests/ConvertToSiteMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,48 @@ namespace SimpleSiteMap.Tests
{
public class ConvertToSiteMapTests
{
private static readonly DateTime StartDate = DateTime.SpecifyKind(new DateTime(2014, 11, 21, 18, 58, 00), DateTimeKind.Utc);

private static readonly SitemapService SitemapService = new SitemapService();

[Fact]
public void GivenDataWith10Items_ConvertToSiteMap_CreatesASitemapResult()
{
// Arrange.
var startDate = DateTime.SpecifyKind(new DateTime(2014, 11, 21, 18, 58, 00), DateTimeKind.Utc);
var data = SimpleSiteMapHelpers.CreateFakeSitemapNodes(100, startDate).ToPartition(10);
var siteMapService = new SitemapService();
var data = SimpleSiteMapHelpers.CreateFakeSitemapNodes(100, StartDate).ToPartition(10);

// Act.
var result = siteMapService.ConvertToXmlSitemap(data);
var result = SitemapService.ConvertToXmlSitemap(data);

// Assert.
var expectedXml = File.ReadAllText("Result Data//SitemapWith10Items.xml");

CompareTwoSitemapDocuments(result, expectedXml);
}

[Fact]
public void GivenDataWith1Item_ConvertToSiteMap_WithAppendPageParamQueryDisabled_CreatesASitemapResult()
{
// Act.
var data = SimpleSiteMapHelpers.CreateFakeSitemapNodes(1, StartDate, pageParamQuery: false).ToPartition(10);

// Act.
var result = SitemapService.ConvertToXmlSitemap(data);

// Assert.
var expectedXml = File.ReadAllText("Result Data//SitemapWithNoPageQueryParam.xml");

CompareTwoSitemapDocuments(result, expectedXml);
}

[Fact]
public void GivenNoPartitionedData_ConvertToSitemap_CreatesASitemapResult()
{
// Arrange.r
var startDate = DateTime.SpecifyKind(new DateTime(2014, 11, 21, 18, 58, 00), DateTimeKind.Utc);
var partitionedData = SimpleSiteMapHelpers.CreateFakeSitemapNodes(0, startDate);
var siteMapService = new SitemapService();
// Arrange.
var partitionedData = SimpleSiteMapHelpers.CreateFakeSitemapNodes(0, StartDate);

// Act.
var result = siteMapService.ConvertToXmlSitemap(partitionedData);
var result = SitemapService.ConvertToXmlSitemap(partitionedData);

// Assert.
var expectedXml = File.ReadAllText("Result Data//SitemapWith0Items.xml");
Expand All @@ -60,12 +75,12 @@ private static void CompareTwoSitemapDocuments(string actualXml, string expected
.Count()
.ShouldBe(actualXmlDocument.DescendantNodes().Count());

var actuaElements = actualXmlDocument.Root.Elements().ToList();
var actualElements = actualXmlDocument.Root.Elements().ToList();
var expectedElements = expectedXmlDocument.Root.Elements().ToList();

for (int i = 0; i < actuaElements.Count; i++)
for (int i = 0; i < actualElements.Count; i++)
{
var actualChildrenElements = actuaElements[i].Elements().ToList();
var actualChildrenElements = actualElements[i].Elements().ToList();
var expectedChildrenElements = expectedElements[i].Elements().ToList();

// loc.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.foo.com/sitemap/foos</loc>
<lastmod>2014-11-21T18:58:00+00:00</lastmod>
</sitemap>
</sitemapindex>
3 changes: 3 additions & 0 deletions tests/SimpleSiteMap.Tests/SimpleSiteMap.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<None Update="Result Data\UrlsetWith10ItemsNoChangeFreqOrPriority.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Result Data\SitemapWithNoPageQueryParam.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
8 changes: 6 additions & 2 deletions tests/SimpleSiteMap.Tests/SimpleSiteMapHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ internal static class SimpleSiteMapHelpers
internal static IList<SitemapNode> CreateFakeSitemapNodes(int numberOfNodes,
DateTime startTime,
SitemapFrequency? frequency = SitemapFrequency.Daily,
double? priority = 0.5)
double? priority = 0.5,
bool pageParamQuery = true)
{
var result = new List<SitemapNode>();

for (var i = 0; i < numberOfNodes; i++)
{
result.Add(new SitemapNode(
new Uri("http://www.foo.com/sitemap/foos"),
startTime.AddSeconds(-i), frequency, priority));
startTime.AddSeconds(-i),
frequency,
priority,
pageParamQuery));
}

return result;
Expand Down

0 comments on commit 81d52a3

Please sign in to comment.