diff --git a/Directory.Packages.props b/Directory.Packages.props index cb713801..ab079ca4 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -2,7 +2,7 @@ true false - 1.1.1.5540 + 1.1.1.17995 false https://api.nuget.org/v3/index.json; diff --git a/EssentialCSharp.Web.Tests/SitemapXmlHelpersTests.cs b/EssentialCSharp.Web.Tests/SitemapXmlHelpersTests.cs index 6c801d1e..be8edc90 100644 --- a/EssentialCSharp.Web.Tests/SitemapXmlHelpersTests.cs +++ b/EssentialCSharp.Web.Tests/SitemapXmlHelpersTests.cs @@ -197,49 +197,38 @@ public void GenerateSitemapXml_DoesNotIncludeErrorRoutes() } [Fact] - public void GenerateAndSerializeSitemapXml_CreatesFileSuccessfully() + public void GenerateSitemapXml_UsesLastModifiedDateFromSiteMapping() { // Arrange - var logger = _Factory.Services.GetRequiredService>(); var tempDir = new DirectoryInfo(Path.GetTempPath()); - var siteMappings = new List { CreateSiteMapping(1, 1, true) }; var baseUrl = "https://test.example.com/"; + var specificLastModified = new DateTime(2023, 5, 15, 10, 30, 0, DateTimeKind.Utc); - // Clean up any existing file - var expectedXmlPath = Path.Combine(tempDir.FullName, "sitemap.xml"); - File.Delete(expectedXmlPath); - - try - { - // Act - var routeConfigurationService = _Factory.Services.GetRequiredService(); - SitemapXmlHelpers.GenerateAndSerializeSitemapXml( - tempDir, - siteMappings, - logger, - routeConfigurationService, - baseUrl); - - // Assert - Assert.True(File.Exists(expectedXmlPath)); - - var xmlContent = File.ReadAllText(expectedXmlPath); - Assert.Contains(" { - // Clean up - File.Delete(expectedXmlPath); - } + CreateSiteMapping(1, 1, true, "test-page-1", specificLastModified) + }; + + // Act + var routeConfigurationService = _Factory.Services.GetRequiredService(); + SitemapXmlHelpers.GenerateSitemapXml( + tempDir, + siteMappings, + routeConfigurationService, + baseUrl, + out var nodes); + + // Assert + var siteMappingNode = nodes.First(node => node.Url.Contains("test-page-1")); + Assert.Equal(specificLastModified, siteMappingNode.LastModificationDate); } private static SiteMapping CreateSiteMapping( int chapterNumber, int pageNumber, bool includeInSitemapXml, - string key = "test-key") + string key = "test-key", + DateTime? lastModified = null) { return new SiteMapping( keys: [key], @@ -253,7 +242,8 @@ private static SiteMapping CreateSiteMapping( anchorId: key, indentLevel: 1, contentHash: "TestHash123", - includeInSitemapXml: includeInSitemapXml + includeInSitemapXml: includeInSitemapXml, + lastModified: lastModified ); } } diff --git a/EssentialCSharp.Web/Helpers/SitemapXmlHelpers.cs b/EssentialCSharp.Web/Helpers/SitemapXmlHelpers.cs index 0247b6f5..0032cf4a 100644 --- a/EssentialCSharp.Web/Helpers/SitemapXmlHelpers.cs +++ b/EssentialCSharp.Web/Helpers/SitemapXmlHelpers.cs @@ -68,7 +68,7 @@ public static void GenerateSitemapXml(DirectoryInfo wwwrootDirectory, List item.IncludeInSitemapXml).Select(siteMapping => new($"{baseUrl.TrimEnd('/')}/{siteMapping.Keys.First()}") { - LastModificationDate = newDateTime, + LastModificationDate = siteMapping.LastModified ?? newDateTime, ChangeFrequency = ChangeFrequency.Daily, Priority = 0.8M }));