Refactor document name sanitization for consistent filename generation

- Introduced FdsDocumentNames class to centralize the logic for sanitizing document names used in downloads.
- Updated FdsMfr to utilize the new SafeFileName method for generating safe filenames.
- Enhanced documentation for FdsDocumentNames and its methods to clarify usage and purpose.
- Added global.json file to configure testing framework settings.
This commit is contained in:
Stefan
2026-09-16 22:18:42 +02:00
parent a60d4c2eaf
commit 944a5bb442
18 changed files with 245 additions and 34 deletions
+51
View File
@@ -26,8 +26,59 @@ public class ReportDocFileNameTests
[InlineData("")]
[InlineData(" ")]
[InlineData("<p></p>")]
[InlineData("<p> </p>")]
public void SafeDocumentFileName_EmptyDocumentName_UsesFallback(string? documentName)
{
Assert.Equal("Rechnung_W7VAL.pdf", IntranetController.SafeDocumentFileName(documentName!, "Rechnung_W7VAL.pdf"));
}
/// <summary>
/// The stored-document path (<c>FdsMfr.GetReportDoc</c>) replaced exactly these characters
/// before the sanitiser was shared: <c>= &gt; | ! &lt; /</c>. The shared helper must still
/// produce the identical result for them — unifying the two paths may not loosen either.
/// </summary>
[Theory]
[InlineData("a>b|c!d<e/f=g.pdf", "a_b_c_d_e_f_g.pdf")]
[InlineData("<p>Summe=100!.pdf</p>", "Summe_100_.pdf")]
public void SafeFileName_KeepsEveryReplacementTheStoredDocumentPathMade(string documentName, string expected)
{
Assert.Equal(expected, fds.FdsDocumentNames.SafeFileName(documentName, "X.pdf"));
}
/// <summary>
/// Characters the old stored-document path did NOT catch but Windows rejects — the reason
/// the sanitiser was unified (code review on ADR 0025): a stored and a regenerated document
/// must never diverge in filename safety.
/// </summary>
[Theory]
[InlineData("Angebot A\\B.pdf", "Angebot A_B.pdf")]
[InlineData("Re: 2026.pdf", "Re_ 2026.pdf")]
[InlineData("x*y?z\".pdf", "x_y_z_.pdf")]
[InlineData("tab\there.pdf", "tab_here.pdf")]
public void SafeFileName_ReplacesInvalidCharsTheStoredDocumentPathMissed(string documentName, string expected)
{
Assert.Equal(expected, fds.FdsDocumentNames.SafeFileName(documentName, "X.pdf"));
}
[Theory]
[InlineData("Rechnung R2026-0258.pdf")]
[InlineData("Wartung 2025/26 R2026-0001.pdf")]
[InlineData("a>b|c!d<e/f=g.pdf")]
[InlineData("Angebot A\\B: \"x\" <y>|z?*.pdf")]
[InlineData("<p></p>")]
[InlineData(null)]
public void SafeDocumentFileName_MatchesSharedSanitiserForEveryInput(string? documentName)
{
Assert.Equal(
fds.FdsDocumentNames.SafeFileName(documentName, "Rechnung_X.pdf"),
IntranetController.SafeDocumentFileName(documentName!, "Rechnung_X.pdf"));
}
[Fact]
public void SafeFileName_ReturnsFallbackVerbatim_WhenNothingUsableRemains()
{
// The fallback is the caller's responsibility and is not sanitised or second-guessed.
Assert.Equal("W7VAL", fds.FdsDocumentNames.SafeFileName("<p></p>", "W7VAL"));
Assert.Equal("", fds.FdsDocumentNames.SafeFileName("", ""));
}
}