Aanbiedingen: planning en aan/uit per pakket, admin-lijst /offerings/all, Annuleren op het pakketformulier

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 11:42:26 +01:00
co-authored by Claude Sonnet 5.5
parent 321234dfa6
commit 8af156c238
29 changed files with 916 additions and 73 deletions
@@ -0,0 +1,124 @@
using System.ComponentModel.DataAnnotations;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NSubstitute;
using SlpModularCms.Modules.Offerings.Data.Entities;
using SlpModularCms.Modules.Offerings.Models;
using SlpModularCms.Modules.Offerings.Repositories;
using SlpModularCms.Modules.Offerings.Services;
namespace SlpModularCms.Modules.Offerings.Tests.Services;
public class OfferingsScheduleTests
{
private sealed class FakeTimeProvider(DateTimeOffset now) : TimeProvider
{
public override DateTimeOffset GetUtcNow() => now;
}
private static readonly DateTimeOffset Now = new(2026, 11, 15, 12, 0, 0, TimeSpan.Zero);
private readonly IOfferingRepository _repo = Substitute.For<IOfferingRepository>();
private OfferingsService Sut() => new(
_repo, Substitute.For<IOfferingCategoryRepository>(), Substitute.For<ILogger<OfferingsService>>(),
null, new FakeTimeProvider(Now));
private static Offering Offer(string title, bool active = true, DateTimeOffset? starts = null, DateTimeOffset? ends = null) => new()
{
Id = Guid.NewGuid(), Slug = title.ToLowerInvariant(), Title = title, Description = "d", Price = "p", PriceNote = "n",
Features = ["f"], CtaLabel = "c", IsActive = active, StartsAt = starts, EndsAt = ends,
};
[Fact]
public async Task Public_list_shows_unscheduled_active_offering_so_existing_packages_stay_visible()
{
_repo.GetAllAsync().Returns([Offer("Plain")]);
(await Sut().GetOfferingsAsync()).Select(o => o.Title).Should().Equal("Plain");
}
[Fact]
public async Task Public_list_hides_switched_off_planned_and_expired_but_keeps_order()
{
_repo.GetAllAsync().Returns([
Offer("A"),
Offer("Off", active: false),
Offer("Planned", starts: Now.AddMinutes(1)),
Offer("Expired", ends: Now),
Offer("B", starts: Now, ends: Now.AddDays(1)),
]);
(await Sut().GetOfferingsAsync()).Select(o => o.Title).Should().Equal("A", "B");
}
[Fact]
public async Task Public_list_never_exposes_switch_or_schedule()
{
_repo.GetAllAsync().Returns([Offer("A")]);
var dto = (await Sut().GetOfferingsAsync()).Single();
dto.GetType().Should().Be(typeof(OfferingDto));
}
[Fact]
public async Task Admin_list_returns_everything_with_switch_and_schedule()
{
_repo.GetAllAsync().Returns([Offer("Off", active: false), Offer("Planned", starts: Now.AddDays(1), ends: Now.AddDays(2))]);
var list = await Sut().ListAllAsync();
list.Should().HaveCount(2);
list[0].IsActive.Should().BeFalse();
list[1].StartsAt.Should().Be(Now.AddDays(1));
list[1].EndsAt.Should().Be(Now.AddDays(2));
}
[Fact]
public async Task Create_without_schedule_fields_defaults_to_active_and_unscheduled()
{
_repo.GetMaxDisplayOrderAsync().Returns(0);
var request = new CreateOfferingRequest("T", "d", "p", "n", ["f"], "c");
var dto = await Sut().CreateAsync(request, Guid.NewGuid());
dto.IsActive.Should().BeTrue();
dto.StartsAt.Should().BeNull();
dto.EndsAt.Should().BeNull();
}
[Fact]
public async Task Create_and_update_store_switch_and_schedule()
{
_repo.GetMaxDisplayOrderAsync().Returns(0);
var create = new CreateOfferingRequest("T", "d", "p", "n", ["f"], "c", IsActive: false, StartsAt: Now, EndsAt: Now.AddDays(1));
var created = await Sut().CreateAsync(create, Guid.NewGuid());
created.IsActive.Should().BeFalse();
await _repo.Received(1).AddAsync(Arg.Is<Offering>(o => !o.IsActive && o.StartsAt == Now && o.EndsAt == Now.AddDays(1)));
var existing = Offer("E");
_repo.GetByIdAsync(existing.Id).Returns(existing);
var update = new UpdateOfferingRequest("E", "d", "p", "n", ["f"], "c", IsActive: false, StartsAt: Now, EndsAt: Now.AddHours(2));
var updated = await Sut().UpdateAsync(existing.Id, update, Guid.NewGuid());
updated!.IsActive.Should().BeFalse();
existing.EndsAt.Should().Be(Now.AddHours(2));
}
[Fact]
public void End_must_be_after_start_for_create_and_update()
{
var bad = new CreateOfferingRequest("T", "d", "p", "n", ["f"], "c", StartsAt: Now, EndsAt: Now);
var badUpdate = new UpdateOfferingRequest("T", "d", "p", "n", ["f"], "c", StartsAt: Now, EndsAt: Now.AddSeconds(-1));
var ok = new CreateOfferingRequest("T", "d", "p", "n", ["f"], "c", StartsAt: Now, EndsAt: Now.AddSeconds(1));
var ctx = new ValidationContext(bad);
bad.Validate(ctx).Should().ContainSingle();
badUpdate.Validate(ctx).Should().ContainSingle();
ok.Validate(ctx).Should().BeEmpty();
}
}