|
| 1 | +using System.Globalization; |
| 2 | +using DotnetSitemapGenerator; |
| 3 | +using EssentialCSharp.Web.Helpers; |
| 4 | +using EssentialCSharp.Web.Services; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | + |
| 8 | +namespace EssentialCSharp.Web.Tests; |
| 9 | + |
| 10 | +public class SitemapXmlHelpersTests : IClassFixture<WebApplicationFactory> |
| 11 | +{ |
| 12 | + private readonly WebApplicationFactory _Factory; |
| 13 | + |
| 14 | + public SitemapXmlHelpersTests(WebApplicationFactory factory) |
| 15 | + { |
| 16 | + _Factory = factory; |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void EnsureSitemapHealthy_WithValidSiteMappings_DoesNotThrow() |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + var siteMappings = new List<SiteMapping> |
| 24 | + { |
| 25 | + CreateSiteMapping(1, 1, true), |
| 26 | + CreateSiteMapping(1, 2, true), |
| 27 | + CreateSiteMapping(2, 1, true) |
| 28 | + }; |
| 29 | + |
| 30 | + // Act & Assert |
| 31 | + var exception = Record.Exception(() => SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings)); |
| 32 | + Assert.Null(exception); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void EnsureSitemapHealthy_WithMultipleCanonicalLinksForSamePage_ThrowsException() |
| 37 | + { |
| 38 | + // Arrange - Two mappings for the same chapter/page both marked as canonical |
| 39 | + var siteMappings = new List<SiteMapping> |
| 40 | + { |
| 41 | + CreateSiteMapping(1, 1, true), |
| 42 | + CreateSiteMapping(1, 1, true) // Same chapter/page, also canonical |
| 43 | + }; |
| 44 | + |
| 45 | + // Act & Assert |
| 46 | + var exception = Assert.Throws<InvalidOperationException>(() => |
| 47 | + SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings)); |
| 48 | + |
| 49 | + Assert.Contains("Chapter 1, Page 1", exception.Message); |
| 50 | + Assert.Contains("more than one canonical link", exception.Message); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void EnsureSitemapHealthy_WithNoCanonicalLinksForPage_ThrowsException() |
| 55 | + { |
| 56 | + // Arrange - No mappings marked as canonical for this page |
| 57 | + var siteMappings = new List<SiteMapping> |
| 58 | + { |
| 59 | + CreateSiteMapping(1, 1, false), |
| 60 | + CreateSiteMapping(1, 1, false) // Same chapter/page, neither canonical |
| 61 | + }; |
| 62 | + |
| 63 | + // Act & Assert |
| 64 | + var exception = Assert.Throws<InvalidOperationException>(() => |
| 65 | + SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings)); |
| 66 | + |
| 67 | + Assert.Contains("Chapter 1, Page 1", exception.Message); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void GenerateSitemapXml_DoesNotIncludeIdentityRoutes() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + var tempDir = new DirectoryInfo(Path.GetTempPath()); |
| 75 | + var siteMappings = new List<SiteMapping> { CreateSiteMapping(1, 1, true) }; |
| 76 | + var baseUrl = "https://test.example.com/"; |
| 77 | + |
| 78 | + // Act & Assert |
| 79 | + var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>(); |
| 80 | + SitemapXmlHelpers.GenerateSitemapXml( |
| 81 | + tempDir, |
| 82 | + siteMappings, |
| 83 | + routeConfigurationService, |
| 84 | + baseUrl, |
| 85 | + out var nodes); |
| 86 | + |
| 87 | + var allUrls = nodes.Select(n => n.Url).ToList(); |
| 88 | + |
| 89 | + // Verify no Identity routes are included |
| 90 | + Assert.DoesNotContain(allUrls, url => url.Contains("Identity", StringComparison.OrdinalIgnoreCase)); |
| 91 | + Assert.DoesNotContain(allUrls, url => url.Contains("Account", StringComparison.OrdinalIgnoreCase)); |
| 92 | + |
| 93 | + // But verify that expected routes are included |
| 94 | + Assert.Contains(allUrls, url => url.Contains("/home", StringComparison.OrdinalIgnoreCase)); |
| 95 | + Assert.Contains(allUrls, url => url.Contains("/about", StringComparison.OrdinalIgnoreCase)); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void GenerateSitemapXml_IncludesBaseUrl() |
| 100 | + { |
| 101 | + // Arrange |
| 102 | + var tempDir = new DirectoryInfo(Path.GetTempPath()); |
| 103 | + var siteMappings = new List<SiteMapping>(); |
| 104 | + var baseUrl = "https://test.example.com/"; |
| 105 | + |
| 106 | + // Act & Assert |
| 107 | + var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>(); |
| 108 | + SitemapXmlHelpers.GenerateSitemapXml( |
| 109 | + tempDir, |
| 110 | + siteMappings, |
| 111 | + routeConfigurationService, |
| 112 | + baseUrl, |
| 113 | + out var nodes); |
| 114 | + |
| 115 | + Assert.Contains(nodes, node => node.Url == baseUrl); |
| 116 | + |
| 117 | + // Verify the root URL has highest priority |
| 118 | + var rootNode = nodes.First(node => node.Url == baseUrl); |
| 119 | + Assert.Equal(1.0M, rootNode.Priority); |
| 120 | + Assert.Equal(ChangeFrequency.Daily, rootNode.ChangeFrequency); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void GenerateSitemapXml_IncludesSiteMappingsMarkedForXml() |
| 125 | + { |
| 126 | + // Arrange |
| 127 | + var tempDir = new DirectoryInfo(Path.GetTempPath()); |
| 128 | + var baseUrl = "https://test.example.com/"; |
| 129 | + |
| 130 | + var siteMappings = new List<SiteMapping> |
| 131 | + { |
| 132 | + CreateSiteMapping(1, 1, true, "test-page-1"), |
| 133 | + CreateSiteMapping(1, 2, false, "test-page-2"), // Not included in XML |
| 134 | + CreateSiteMapping(2, 1, true, "test-page-3") |
| 135 | + }; |
| 136 | + |
| 137 | + // Act & Assert |
| 138 | + var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>(); |
| 139 | + SitemapXmlHelpers.GenerateSitemapXml( |
| 140 | + tempDir, |
| 141 | + siteMappings, |
| 142 | + routeConfigurationService, |
| 143 | + baseUrl, |
| 144 | + out var nodes); |
| 145 | + |
| 146 | + var allUrls = nodes.Select(n => n.Url).ToList(); |
| 147 | + |
| 148 | + Assert.Contains(allUrls, url => url.Contains("test-page-1")); |
| 149 | + Assert.DoesNotContain(allUrls, url => url.Contains("test-page-2")); // Not marked for XML |
| 150 | + Assert.Contains(allUrls, url => url.Contains("test-page-3")); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void GenerateSitemapXml_DoesNotIncludeIndexRoutes() |
| 155 | + { |
| 156 | + // Arrange |
| 157 | + var tempDir = new DirectoryInfo(Path.GetTempPath()); |
| 158 | + var siteMappings = new List<SiteMapping>(); |
| 159 | + var baseUrl = "https://test.example.com/"; |
| 160 | + |
| 161 | + // Act & Assert |
| 162 | + var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>(); |
| 163 | + SitemapXmlHelpers.GenerateSitemapXml( |
| 164 | + tempDir, |
| 165 | + siteMappings, |
| 166 | + routeConfigurationService, |
| 167 | + baseUrl, |
| 168 | + out var nodes); |
| 169 | + |
| 170 | + var allUrls = nodes.Select(n => n.Url).ToList(); |
| 171 | + |
| 172 | + // Should not include Index action routes (they're the default) |
| 173 | + Assert.DoesNotContain(allUrls, url => url.Contains("/Index", StringComparison.OrdinalIgnoreCase)); |
| 174 | + } |
| 175 | + |
| 176 | + [Fact] |
| 177 | + public void GenerateSitemapXml_DoesNotIncludeErrorRoutes() |
| 178 | + { |
| 179 | + // Arrange |
| 180 | + var tempDir = new DirectoryInfo(Path.GetTempPath()); |
| 181 | + var siteMappings = new List<SiteMapping>(); |
| 182 | + var baseUrl = "https://test.example.com/"; |
| 183 | + |
| 184 | + // Act & Assert |
| 185 | + var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>(); |
| 186 | + SitemapXmlHelpers.GenerateSitemapXml( |
| 187 | + tempDir, |
| 188 | + siteMappings, |
| 189 | + routeConfigurationService, |
| 190 | + baseUrl, |
| 191 | + out var nodes); |
| 192 | + |
| 193 | + var allUrls = nodes.Select(n => n.Url).ToList(); |
| 194 | + |
| 195 | + // Should not include Error action routes |
| 196 | + Assert.DoesNotContain(allUrls, url => url.Contains("/Error", StringComparison.OrdinalIgnoreCase)); |
| 197 | + } |
| 198 | + |
| 199 | + [Fact] |
| 200 | + public void GenerateAndSerializeSitemapXml_CreatesFileSuccessfully() |
| 201 | + { |
| 202 | + // Arrange |
| 203 | + var logger = _Factory.Services.GetRequiredService<ILogger<SitemapXmlHelpersTests>>(); |
| 204 | + var tempDir = new DirectoryInfo(Path.GetTempPath()); |
| 205 | + var siteMappings = new List<SiteMapping> { CreateSiteMapping(1, 1, true) }; |
| 206 | + var baseUrl = "https://test.example.com/"; |
| 207 | + |
| 208 | + // Clean up any existing file |
| 209 | + var expectedXmlPath = Path.Combine(tempDir.FullName, "sitemap.xml"); |
| 210 | + File.Delete(expectedXmlPath); |
| 211 | + |
| 212 | + try |
| 213 | + { |
| 214 | + // Act |
| 215 | + var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>(); |
| 216 | + SitemapXmlHelpers.GenerateAndSerializeSitemapXml( |
| 217 | + tempDir, |
| 218 | + siteMappings, |
| 219 | + logger, |
| 220 | + routeConfigurationService, |
| 221 | + baseUrl); |
| 222 | + |
| 223 | + // Assert |
| 224 | + Assert.True(File.Exists(expectedXmlPath)); |
| 225 | + |
| 226 | + var xmlContent = File.ReadAllText(expectedXmlPath); |
| 227 | + Assert.Contains("<?xml", xmlContent); |
| 228 | + Assert.Contains("<urlset", xmlContent); |
| 229 | + Assert.Contains(baseUrl, xmlContent); |
| 230 | + } |
| 231 | + finally |
| 232 | + { |
| 233 | + // Clean up |
| 234 | + File.Delete(expectedXmlPath); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + private static SiteMapping CreateSiteMapping( |
| 239 | + int chapterNumber, |
| 240 | + int pageNumber, |
| 241 | + bool includeInSitemapXml, |
| 242 | + string key = "test-key") |
| 243 | + { |
| 244 | + return new SiteMapping( |
| 245 | + keys: [key], |
| 246 | + primaryKey: key, |
| 247 | + pagePath: ["Chapters", chapterNumber.ToString("00", CultureInfo.InvariantCulture), "Pages", $"{pageNumber:00}.html"], |
| 248 | + chapterNumber: chapterNumber, |
| 249 | + pageNumber: pageNumber, |
| 250 | + orderOnPage: 0, |
| 251 | + chapterTitle: $"Chapter {chapterNumber}", |
| 252 | + rawHeading: "Test Heading", |
| 253 | + anchorId: key, |
| 254 | + indentLevel: 1, |
| 255 | + contentHash: "TestHash123", |
| 256 | + includeInSitemapXml: includeInSitemapXml |
| 257 | + ); |
| 258 | + } |
| 259 | +} |
0 commit comments