-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSitemapXmlHelpersTests.cs
More file actions
354 lines (305 loc) · 12.9 KB
/
Copy pathSitemapXmlHelpersTests.cs
File metadata and controls
354 lines (305 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System.Globalization;
using DotnetSitemapGenerator;
using EssentialCSharp.Web.Helpers;
using EssentialCSharp.Web.Services;
using Microsoft.Extensions.DependencyInjection;
namespace EssentialCSharp.Web.Tests;
public class SitemapXmlHelpersTests : IntegrationTestBase
{
[Test]
public async Task EnsureSitemapHealthy_WithValidSiteMappings_DoesNotThrow()
{
// Arrange
var siteMappings = new List<SiteMapping>
{
CreateSiteMapping(1, 1, true),
CreateSiteMapping(1, 2, true),
CreateSiteMapping(2, 1, true)
};
// Act & Assert
await Assert.That(() => SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings)).ThrowsNothing();
}
[Test]
public async Task EnsureSitemapHealthy_WithMultipleCanonicalLinksForSamePage_ThrowsException()
{
// Arrange - Two mappings for the same chapter/page both marked as canonical
var siteMappings = new List<SiteMapping>
{
CreateSiteMapping(1, 1, true),
CreateSiteMapping(1, 1, true) // Same chapter/page, also canonical
};
// Act & Assert
await Assert.That(() => SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings))
.Throws<InvalidOperationException>()
.WithMessageContaining("Chapter 1, Page 1")
.And.WithMessageContaining("more than one canonical link");
}
[Test]
public async Task EnsureSitemapHealthy_WithNoCanonicalLinksForPage_ThrowsException()
{
// Arrange - No mappings marked as canonical for this page
var siteMappings = new List<SiteMapping>
{
CreateSiteMapping(1, 1, false),
CreateSiteMapping(1, 1, false) // Same chapter/page, neither canonical
};
// Act & Assert
await Assert.That(() => SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings))
.Throws<InvalidOperationException>()
.WithMessageContaining("Chapter 1, Page 1");
}
[Test]
public async Task GenerateSitemapXml_DoesNotIncludeIdentityRoutes()
{
// Arrange
var siteMappings = new List<SiteMapping> { CreateSiteMapping(1, 1, true) };
var baseUrl = "https://test.example.com/";
// Act & Assert
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
// Verify no Identity routes are included
await Assert.That(allUrls).DoesNotContain(url => url.Contains("Identity", StringComparison.OrdinalIgnoreCase));
await Assert.That(allUrls).DoesNotContain(url => url.Contains("Account", StringComparison.OrdinalIgnoreCase));
// But verify that expected routes are included
await Assert.That(allUrls).Contains(url => url.Contains("/home", StringComparison.OrdinalIgnoreCase));
await Assert.That(allUrls).Contains(url => url.Contains("/about", StringComparison.OrdinalIgnoreCase));
}
[Test]
public async Task GenerateSitemapXml_IncludesBaseUrl()
{
// Arrange
var siteMappings = new List<SiteMapping>();
var baseUrl = "https://test.example.com/";
// Act & Assert
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
await Assert.That(nodes).Contains(node => node.Url == baseUrl);
// Verify the root URL has highest priority
var rootNode = nodes.First(node => node.Url == baseUrl);
using (Assert.Multiple())
{
await Assert.That(rootNode.Priority).IsEqualTo(1.0M);
await Assert.That(rootNode.ChangeFrequency).IsEqualTo(ChangeFrequency.Daily);
}
}
[Test]
public async Task GenerateSitemapXml_IncludesSiteMappingsMarkedForXml()
{
// Arrange
var baseUrl = "https://test.example.com/";
var siteMappings = new List<SiteMapping>
{
CreateSiteMapping(1, 1, true, "test-page-1"),
CreateSiteMapping(1, 2, false, "test-page-2"), // Not included in XML
CreateSiteMapping(2, 1, true, "test-page-3")
};
// Act & Assert
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
await Assert.That(allUrls).Contains(url => url.Contains("test-page-1", StringComparison.OrdinalIgnoreCase));
await Assert.That(allUrls).DoesNotContain(url => url.Contains("test-page-2", StringComparison.OrdinalIgnoreCase)); // Not marked for XML
await Assert.That(allUrls).Contains(url => url.Contains("test-page-3", StringComparison.OrdinalIgnoreCase));
}
[Test]
public async Task GenerateSitemapXml_DoesNotIncludeIndexRoutes()
{
// Arrange
var siteMappings = new List<SiteMapping>();
var baseUrl = "https://test.example.com/";
// Act & Assert
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
// Should not include Index action routes (they're the default)
await Assert.That(allUrls).DoesNotContain(url => url.Contains("/Index", StringComparison.OrdinalIgnoreCase));
}
[Test]
public async Task GenerateSitemapXml_DoesNotIncludeErrorRoutes()
{
// Arrange
var siteMappings = new List<SiteMapping>();
var baseUrl = "https://test.example.com/";
// Act & Assert
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
// Should not include Error action routes
await Assert.That(allUrls).DoesNotContain(url => url.Contains("/Error", StringComparison.OrdinalIgnoreCase));
}
[Test]
public async Task GenerateSitemapXml_DoesNotIncludeSitemapRoute()
{
// Arrange
var siteMappings = new List<SiteMapping>();
var baseUrl = "https://test.example.com/";
// Act
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
// /sitemap.xml should not list itself
await Assert.That(allUrls).DoesNotContain(url => url.EndsWith("/sitemap.xml", StringComparison.OrdinalIgnoreCase));
}
[Test]
public async Task GenerateSitemapXml_UsesLastModifiedDateFromSiteMapping()
{
// Arrange
var baseUrl = "https://test.example.com/";
var specificLastModified = new DateTime(2023, 5, 15, 10, 30, 0, DateTimeKind.Utc);
var siteMappings = new List<SiteMapping>
{
CreateSiteMapping(1, 1, true, "test-page-1", specificLastModified)
};
// Act
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
// Assert
var siteMappingNode = nodes.First(node => node.Url.Contains("test-page-1"));
await Assert.That(siteMappingNode.LastModificationDate).IsEqualTo(specificLastModified);
}
[Test]
public async Task GenerateSitemapXml_DoesNotSetLastModifiedDateWhenSiteMappingDateIsMissing()
{
// Arrange
var baseUrl = "https://test.example.com/";
var siteMappings = new List<SiteMapping>
{
CreateSiteMapping(1, 1, true, "test-page-1")
};
// Act
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
// Assert
var siteMappingNode = nodes.First(node => node.Url.Contains("test-page-1"));
await Assert.That(siteMappingNode.LastModificationDate).IsNull();
}
[Test]
public async Task GenerateSitemapXml_UsesLastModifiedDateFromStaticRouteLookup()
{
// Arrange
var baseUrl = "https://test.example.com/";
var homeLastModified = new DateTime(2025, 1, 2, 3, 4, 5, DateTimeKind.Utc);
var staticRouteLastModifiedDates = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase)
{
["/home"] = homeLastModified
};
var siteMappings = new List<SiteMapping>();
// Act
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
staticRouteLastModifiedDates,
out var nodes);
// Assert
var homeNode = nodes.First(node => node.Url.EndsWith("/home", StringComparison.OrdinalIgnoreCase));
await Assert.That(homeNode.LastModificationDate).IsEqualTo(homeLastModified);
}
[Test]
public async Task GenerateSitemapXml_AppliesLastModifiedDateForAllMappedStaticRoutes()
{
// Arrange
var baseUrl = "https://test.example.com/";
var staticRouteLastModifiedDates = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase)
{
["/home"] = new DateTime(2025, 1, 2, 3, 4, 5, DateTimeKind.Utc),
["/about"] = new DateTime(2025, 2, 3, 4, 5, 6, DateTimeKind.Utc),
["/guidelines"] = new DateTime(2025, 3, 4, 5, 6, 7, DateTimeKind.Utc)
};
var siteMappings = new List<SiteMapping>();
// Act
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
staticRouteLastModifiedDates,
out var nodes);
// Assert
foreach ((var route, var expectedLastModified) in staticRouteLastModifiedDates)
{
var routeNode = nodes.First(node => node.Url.EndsWith(route, StringComparison.OrdinalIgnoreCase));
await Assert.That(routeNode.LastModificationDate).IsEqualTo(expectedLastModified);
}
}
[Test]
public async Task GenerateSitemapXml_UsesHomeLastModifiedDateForRootNode()
{
// Arrange
var baseUrl = "https://test.example.com/";
var homeLastModified = new DateTime(2025, 2, 3, 4, 5, 6, DateTimeKind.Utc);
var staticRouteLastModifiedDates = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase)
{
["/home"] = homeLastModified
};
var siteMappings = new List<SiteMapping>();
// Act
var routeConfigurationService = Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
siteMappings,
routeConfigurationService,
baseUrl,
staticRouteLastModifiedDates,
out var nodes);
// Assert
var rootNode = nodes.First(node => node.Url == baseUrl);
await Assert.That(rootNode.LastModificationDate).IsEqualTo(homeLastModified);
}
private static SiteMapping CreateSiteMapping(
int chapterNumber,
int pageNumber,
bool includeInSitemapXml,
string key = "test-key",
DateTime? lastModified = null)
{
return new SiteMapping(
keys: [key],
primaryKey: key,
pagePath: ["Chapters", chapterNumber.ToString("00", CultureInfo.InvariantCulture), "Pages", $"{pageNumber:00}.html"],
chapterNumber: chapterNumber,
pageNumber: pageNumber,
orderOnPage: 0,
chapterTitle: $"Chapter {chapterNumber}",
rawHeading: "Test Heading",
anchorId: key,
indentLevel: 1,
contentHash: "TestHash123",
includeInSitemapXml: includeInSitemapXml,
lastModified: lastModified
);
}
}