From 4baadf3144a5748a715f9ea864db1f96017b6b06 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Sun, 15 Nov 2026 16:10:22 +0100 Subject: [PATCH] ASF-injectie 4/5: tests voor OfferingsHtmlTransform (regio's, data-blok, JSON-LD, inline, fail-open) Co-Authored-By: Claude Sonnet 5.5 --- .../Services/OfferingsHtmlTransformTests.cs | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsHtmlTransformTests.cs diff --git a/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsHtmlTransformTests.cs b/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsHtmlTransformTests.cs new file mode 100644 index 0000000..5c2ca35 --- /dev/null +++ b/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsHtmlTransformTests.cs @@ -0,0 +1,225 @@ +using System.Text.Json; +using FluentAssertions; +using NSubstitute; +using NSubstitute.ExceptionExtensions; +using SlpModularCms.Core.Hosting.Website; +using SlpModularCms.Modules.Offerings.Models; +using SlpModularCms.Modules.Offerings.Services; + +namespace SlpModularCms.Modules.Offerings.Tests.Services; + +public class OfferingsHtmlTransformTests +{ + private readonly IOfferingsService _offerings = Substitute.For(); + private readonly IOfferingBannersService _banners = Substitute.For(); + private static readonly HtmlTransformContext Context = new("https://test.andreaslop.nl"); + + private OfferingsHtmlTransform Sut() => new(_offerings, _banners); + + private static OfferingDto Offering(string slug, string title, string price, int order = 0, bool featured = false) => new( + Guid.NewGuid().ToString(), slug, title, "Beschrijving", price, "eenmalig", ["1 uur fotografie", "Online galerij"], "Boek", featured, order); + + private void Live(params OfferingDto[] list) => _offerings.GetOfferingsAsync().Returns(list); + + private void Banner(string position, PublicOfferingBannerDto? banner) => _banners.GetActiveAsync(position).Returns(banner); + + // ---- what a page asks for decides what is loaded ---- + + [Theory] + [InlineData("

nothing

", false)] + [InlineData("", true)] + [InlineData("x", true)] + public void Wants_OnlyPagesWithCmsMarkers(string html, bool expected) => Sut().Wants(html).Should().Be(expected); + + [Fact] + public async Task APageWithOnlyAnOfferingsRegion_NeverLoadsBanners() + { + Live(Offering("newborn", "Newborn", "€ 185")); + + await Sut().ApplyAsync("x", Context, default); + + await _banners.DidNotReceiveWithAnyArgs().GetActiveAsync(default!); + } + + [Fact] + public async Task APageWithOnlyABannerRegion_NeverLoadsOfferings() + { + Banner("top", null); + + await Sut().ApplyAsync("x", Context, default); + + await _offerings.DidNotReceive().GetOfferingsAsync(); + } + + // ---- regions ---- + + [Fact] + public async Task TheRegions_AreFilledFromLiveData_AndAnEndedBannerEmptiesItsRegion() + { + Live(Offering("newborn", "Newborn", "€ 185")); + Banner("top", new PublicOfferingBannerDto("", "Nog twee plekken", null, null, "top")); + Banner("bottom", null); + var html = "baked top" + + "baked list" + + "baked bottom"; + + var result = await Sut().ApplyAsync(html, Context, default); + + result.Should().Contain(""); + result.Should().Contain("
  • "); + result.Should().Contain("", "no active banner (204) empties the region"); + result.Should().NotContain("baked"); + } + + [Fact] + public async Task WhenNoPackageIsLive_TheListRegionIsEmptied() + { + Live(); + + var result = await Sut().ApplyAsync("baked", Context, default); + + result.Should().Be(""); + } + + // ---- data block ---- + + [Fact] + public async Task TheDataBlock_HasTheShapeOfThePublicApi_PlusTheSiteUrl() + { + var newborn = Offering("newborn", "Newborn", "€ 185", featured: true); + Live(newborn); + var top = new PublicOfferingBannerDto("Titel", "Tekst", "Meer", "/contact", "top"); + Banner("top", top); + Banner("bottom", null); + + var html = await Sut().ApplyAsync("", Context, default); + + html.Should().StartWith(""); + var json = html["".Length]; + var data = JsonDocument.Parse(json).RootElement; + data.GetProperty("siteUrl").GetString().Should().Be("https://test.andreaslop.nl"); + + // The same JSON as the public endpoints write (camelCase, nulls included). + var api = new JsonSerializerOptions(JsonSerializerDefaults.Web); + data.GetProperty("offerings").GetRawText().Should().Be(JsonSerializer.Serialize(new[] { newborn }, api)); + data.GetProperty("banners").GetProperty("top").GetRawText().Should().Be(JsonSerializer.Serialize(top, api)); + data.GetProperty("banners").GetProperty("bottom").ValueKind.Should().Be(JsonValueKind.Null); + data.GetProperty("offerings")[0].GetProperty("slug").GetString().Should().Be("newborn"); + } + + [Fact] + public async Task TheDataBlock_CannotCloseTheScriptElement() + { + Live(Offering("x", " & \u2028", "€ 100")); + Banner("top", null); + Banner("bottom", null); + + var html = await Sut().ApplyAsync("", Context, default); + + var inner = html["".Length]; + inner.Should().NotContain("<").And.NotContain(">").And.NotContain("&").And.NotContain("\u2028"); + JsonDocument.Parse(inner).RootElement.GetProperty("offerings")[0].GetProperty("title").GetString() + .Should().Be(" & \u2028"); + } + + // ---- JSON-LD ---- + + private const string Service = "{\"@context\":\"https://schema.org\",\"@type\":\"WebPage\",\"mainEntity\":{\"@type\":\"Service\",\"@id\":\"https://andreaslop.nl/soorten-fotoshoots/newborn#service\",\"name\":\"Newborn é\",\"offers\":{\"@type\":\"Offer\",\"price\":150,\"priceCurrency\":\"EUR\"}}}"; + + private static string Page(string json, string slug = "newborn") => + $""; + + [Fact] + public async Task JsonLd_ThePriceComesFromTheLivePackage_AndTheRestOfTheBlockStays() + { + Live(Offering("newborn", "Newborn", "€ 1.250,-")); + + var html = await Sut().ApplyAsync(Page(Service), Context, default); + + html.Should().StartWith("").Length.Should().Be(2, "only the closing tag of the block itself"); + } + + // ---- inline ---- + + [Fact] + public async Task Inline_ThePriceOfASpanFollowsTheLivePackage() + { + Live(Offering("newborn", "Newborn", "€ 1.250"), Offering("gezin", "Gezin", "op aanvraag")); + var html = "

    € 185 € 175 € 1

    "; + + var result = await Sut().ApplyAsync(html, Context, default); + + result.Should().Be("

    € 1.250 € 175 € 1

    "); + } + + // ---- failure is the injector's business ---- + + [Fact] + public async Task ADatabaseFailure_Propagates_SoTheInjectorServesTheBakedPage() + { + _offerings.GetOfferingsAsync().ThrowsAsync(new InvalidOperationException("db down")); + + var act = () => Sut().ApplyAsync("baked", Context, default); + + await act.Should().ThrowAsync(); + } +}