-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSitemapXmlHelpersTests.cs
More file actions
249 lines (211 loc) · 8.65 KB
/
SitemapXmlHelpersTests.cs
File metadata and controls
249 lines (211 loc) · 8.65 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
using System.Globalization;
using DotnetSitemapGenerator;
using EssentialCSharp.Web.Helpers;
using EssentialCSharp.Web.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace EssentialCSharp.Web.Tests;
public class SitemapXmlHelpersTests : IClassFixture<WebApplicationFactory>
{
private readonly WebApplicationFactory _Factory;
public SitemapXmlHelpersTests(WebApplicationFactory factory)
{
_Factory = factory;
}
[Fact]
public void EnsureSitemapHealthy_WithValidSiteMappings_DoesNotThrow()
{
// Arrange
var siteMappings = new List<SiteMapping>
{
CreateSiteMapping(1, 1, true),
CreateSiteMapping(1, 2, true),
CreateSiteMapping(2, 1, true)
};
// Act & Assert
var exception = Record.Exception(() => SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings));
Assert.Null(exception);
}
[Fact]
public void 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
var exception = Assert.Throws<InvalidOperationException>(() =>
SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings));
Assert.Contains("Chapter 1, Page 1", exception.Message);
Assert.Contains("more than one canonical link", exception.Message);
}
[Fact]
public void 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
var exception = Assert.Throws<InvalidOperationException>(() =>
SitemapXmlHelpers.EnsureSitemapHealthy(siteMappings));
Assert.Contains("Chapter 1, Page 1", exception.Message);
}
[Fact]
public void GenerateSitemapXml_DoesNotIncludeIdentityRoutes()
{
// Arrange
var tempDir = new DirectoryInfo(Path.GetTempPath());
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(
tempDir,
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
// Verify no Identity routes are included
Assert.DoesNotContain(allUrls, url => url.Contains("Identity", StringComparison.OrdinalIgnoreCase));
Assert.DoesNotContain(allUrls, url => url.Contains("Account", StringComparison.OrdinalIgnoreCase));
// But verify that expected routes are included
Assert.Contains(allUrls, url => url.Contains("/home", StringComparison.OrdinalIgnoreCase));
Assert.Contains(allUrls, url => url.Contains("/about", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public void GenerateSitemapXml_IncludesBaseUrl()
{
// Arrange
var tempDir = new DirectoryInfo(Path.GetTempPath());
var siteMappings = new List<SiteMapping>();
var baseUrl = "https://test.example.com/";
// Act & Assert
var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
tempDir,
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
Assert.Contains(nodes, node => node.Url == baseUrl);
// Verify the root URL has highest priority
var rootNode = nodes.First(node => node.Url == baseUrl);
Assert.Equal(1.0M, rootNode.Priority);
Assert.Equal(ChangeFrequency.Daily, rootNode.ChangeFrequency);
}
[Fact]
public void GenerateSitemapXml_IncludesSiteMappingsMarkedForXml()
{
// Arrange
var tempDir = new DirectoryInfo(Path.GetTempPath());
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(
tempDir,
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
Assert.Contains(allUrls, url => url.Contains("test-page-1"));
Assert.DoesNotContain(allUrls, url => url.Contains("test-page-2")); // Not marked for XML
Assert.Contains(allUrls, url => url.Contains("test-page-3"));
}
[Fact]
public void GenerateSitemapXml_DoesNotIncludeIndexRoutes()
{
// Arrange
var tempDir = new DirectoryInfo(Path.GetTempPath());
var siteMappings = new List<SiteMapping>();
var baseUrl = "https://test.example.com/";
// Act & Assert
var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
tempDir,
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
// Should not include Index action routes (they're the default)
Assert.DoesNotContain(allUrls, url => url.Contains("/Index", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public void GenerateSitemapXml_DoesNotIncludeErrorRoutes()
{
// Arrange
var tempDir = new DirectoryInfo(Path.GetTempPath());
var siteMappings = new List<SiteMapping>();
var baseUrl = "https://test.example.com/";
// Act & Assert
var routeConfigurationService = _Factory.Services.GetRequiredService<IRouteConfigurationService>();
SitemapXmlHelpers.GenerateSitemapXml(
tempDir,
siteMappings,
routeConfigurationService,
baseUrl,
out var nodes);
var allUrls = nodes.Select(n => n.Url).ToList();
// Should not include Error action routes
Assert.DoesNotContain(allUrls, url => url.Contains("/Error", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public void GenerateSitemapXml_UsesLastModifiedDateFromSiteMapping()
{
// Arrange
var tempDir = new DirectoryInfo(Path.GetTempPath());
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(
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",
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
);
}
}