Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
/*
The MIT License (MIT)

Copyright (c) 2007 - 2026 Microting A/S

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

namespace ItemsPlanning.Pn.Test;

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ItemsPlanning.Pn.Infrastructure.Helpers;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microting.eFormApi.BasePn.Infrastructure.Models.Application.NavigationMenu;
using Microting.EformAngularFrontendBase.Infrastructure.Data;
using Microting.EformAngularFrontendBase.Infrastructure.Data.Entities.Menu;
using Microting.EformAngularFrontendBase.Infrastructure.Data.Entities.Permissions;
using NSubstitute;
using NUnit.Framework;
using Testcontainers.MariaDb;

/// <summary>
/// Plugin start removes the dead "Reports" menu entry (#2136) from the host's Angular database: the item,
/// its template and their child rows go; a second start changes nothing; other menu entries are untouched.
/// Runs <see cref="EformItemsPlanningPlugin.Configure"/> against a real MariaDB, the way the host calls it.
/// </summary>
[TestFixture]
public class DeadReportsMenuCleanupTests
{
private const string PlanningLink = "/plugins/items-planning-pn/plannings";

private readonly MariaDbContainer _mariaDbContainer = new MariaDbBuilder("mariadb:11.2")
.WithDatabase("items-planning-dead-reports-menu")
.WithPassword("secretpassword")
.Build();

private DbContextOptions<BaseDbContext> _dbContextOptions;
private ServiceProvider _serviceProvider;
private IApplicationBuilder _appBuilder;

[OneTimeSetUp]
public async Task OneTimeSetUp()
{
await _mariaDbContainer.StartAsync();
var connectionString = _mariaDbContainer.GetConnectionString();

_dbContextOptions = new DbContextOptionsBuilder<BaseDbContext>()
.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
.Options;
// The plugin resolves BaseDbContext from the host's container; register the same options there.
_serviceProvider = new ServiceCollection()
.AddLogging()
.AddScoped(_ => new BaseDbContext(_dbContextOptions))
.BuildServiceProvider();

_appBuilder = Substitute.For<IApplicationBuilder>();
_appBuilder.ApplicationServices.Returns(_serviceProvider);
}

[OneTimeTearDown]
public async Task OneTimeTearDown()
{
await _serviceProvider.DisposeAsync();
await _mariaDbContainer.DisposeAsync();
}

[SetUp]
public async Task SetUp()
{
await using var db = NewDbContext();
await db.Database.EnsureDeletedAsync();
await db.Database.EnsureCreatedAsync();
}

[Test]
public async Task PluginStart_RemovesTheDeadReportsEntry_AndASecondStartIsANoOp()
{
int planningItemId, planningTemplateId, reportsItemId, detachedItemId;
await using (var db = NewDbContext())
{
var securityGroup = new SecurityGroup { Name = "Example group" };
db.SecurityGroups.Add(securityGroup);
await db.SaveChangesAsync();

var dropdown = new MenuItem
{
Name = "Dropdown", E2EId = "items-planning-pn", Link = "", Type = MenuItemTypeEnum.Dropdown
};
db.MenuItems.Add(dropdown);
await db.SaveChangesAsync();

var planning = AddLinkWithTemplate(db, dropdown, securityGroup, "Planning",
"items-planning-pn-plannings", PlanningLink);
var reports = AddLinkWithTemplate(db, dropdown, securityGroup, "Reports",
DeadReportsMenuCleaner.E2EId, DeadReportsMenuCleaner.Link);
// An entry someone re-pointed elsewhere but that still hangs on the dead template: it stays,
// detached from the template.
var detached = new MenuItem
{
Name = "Example link",
E2EId = "example-link",
Link = "/example",
Type = MenuItemTypeEnum.Link,
Parent = dropdown,
MenuTemplate = reports.MenuTemplate
};
db.MenuItems.Add(detached);
await db.SaveChangesAsync();

planningItemId = planning.Id;
planningTemplateId = planning.MenuTemplateId!.Value;
reportsItemId = reports.Id;
detachedItemId = detached.Id;
}

// BaseDbContext seeds its own default menu, templates and permissions, so assert against a snapshot.
var before = await SnapshotAsync();

new EformItemsPlanningPlugin().Configure(_appBuilder);

var after = await SnapshotAsync();
Assert.That(after, Is.EqualTo(before with
{
MenuItemIds = before.MenuItemIds.Where(x => x != reportsItemId).ToList(),
MenuItemTranslations = before.MenuItemTranslations - 1,
MenuItemSecurityGroups = before.MenuItemSecurityGroups - 1,
MenuTemplates = before.MenuTemplates - 1,
MenuTemplateTranslations = before.MenuTemplateTranslations - 1,
MenuTemplatePermissions = before.MenuTemplatePermissions - 1
}).Using<Snapshot>(SameSnapshot));

await using (var db = NewDbContext())
{
Assert.That(await db.MenuItems.AnyAsync(x => x.Link == DeadReportsMenuCleaner.Link), Is.False);
Assert.That(await db.MenuTemplates.AnyAsync(x => x.DefaultLink == DeadReportsMenuCleaner.Link),
Is.False);
Assert.That((await db.MenuItems.SingleAsync(x => x.Id == planningItemId)).MenuTemplateId,
Is.EqualTo(planningTemplateId));
Assert.That(await db.MenuItemTranslations.CountAsync(x => x.MenuItemId == planningItemId),
Is.EqualTo(1));
Assert.That(await db.MenuItemSecurityGroups.CountAsync(x => x.MenuItemId == planningItemId),
Is.EqualTo(1));
Assert.That((await db.MenuItems.SingleAsync(x => x.Id == detachedItemId)).MenuTemplateId, Is.Null);
}

new EformItemsPlanningPlugin().Configure(_appBuilder);

Assert.That(await SnapshotAsync(), Is.EqualTo(after).Using<Snapshot>(SameSnapshot),
"a second start changes nothing");
}

[Test]
public void PluginStart_WithoutTheAngularDatabase_DoesNotThrow()
{
using var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider();
var appBuilder = Substitute.For<IApplicationBuilder>();
appBuilder.ApplicationServices.Returns(serviceProvider);

Assert.DoesNotThrow(() => new EformItemsPlanningPlugin().Configure(appBuilder));
}

private BaseDbContext NewDbContext() => new(_dbContextOptions);

private record Snapshot(
List<int> MenuItemIds,
int MenuItemTranslations,
int MenuItemSecurityGroups,
int MenuTemplates,
int MenuTemplateTranslations,
int MenuTemplatePermissions,
int SecurityGroups,
int Permissions);

private static bool SameSnapshot(Snapshot x, Snapshot y) =>
x with { MenuItemIds = null } == y with { MenuItemIds = null }
&& x.MenuItemIds.SequenceEqual(y.MenuItemIds);

private async Task<Snapshot> SnapshotAsync()
{
await using var db = NewDbContext();
return new Snapshot(
await db.MenuItems.OrderBy(x => x.Id).Select(x => x.Id).ToListAsync(),
await db.MenuItemTranslations.CountAsync(),
await db.MenuItemSecurityGroups.CountAsync(),
await db.MenuTemplates.CountAsync(),
await db.MenuTemplateTranslations.CountAsync(),
await db.MenuTemplatePermissions.CountAsync(),
await db.SecurityGroups.CountAsync(),
await db.Permissions.CountAsync());
}

private static MenuItem AddLinkWithTemplate(BaseDbContext db, MenuItem parent, SecurityGroup securityGroup,
string name, string e2eId, string link)
{
var permissionType = new PermissionType { Name = $"{name} permissions" };
var template = new MenuTemplate
{
Name = name,
E2EId = e2eId,
DefaultLink = link,
Translations = { new MenuTemplateTranslation { Name = name, LocaleName = "en-US", Language = "English" } },
MenuTemplatePermissions =
{
new MenuTemplatePermission
{
Permission = new Permission
{
PermissionName = $"Obtain {name}",
ClaimName = $"{e2eId}_claim",
PermissionType = permissionType
}
}
}
};
var item = new MenuItem
{
Name = name,
E2EId = e2eId,
Link = link,
Type = MenuItemTypeEnum.Link,
Parent = parent,
MenuTemplate = template,
Translations = { new MenuItemTranslation { Name = name, LocaleName = "en-US", Language = "English" } },
MenuItemSecurityGroups = { new MenuItemSecurityGroup { SecurityGroup = securityGroup } }
};
db.MenuItems.Add(item);
return item;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Linq;
using ItemsPlanning.Pn.Infrastructure.Helpers;
using ItemsPlanning.Pn.Services.ItemsPlanningLocalizationService;
using NSubstitute;
using NUnit.Framework;

namespace ItemsPlanning.Pn.Test
{
/// <summary>
/// The "reports" route was removed from the Angular module, so a menu entry linking to it matches no route
/// and the frontend's catch-all silently redirects to "My eForms" (#2136). The plugin must no longer offer it.
/// </summary>
[TestFixture]
public class NavigationMenuTests
{
[Test]
public void GetNavigationMenu_DoesNotOfferTheRemovedReportsPage()
{
var menu = new EformItemsPlanningPlugin().GetNavigationMenu(Substitute.For<IServiceProvider>());

var items = menu.Concat(menu.SelectMany(x => x.ChildItems)).ToList();

Assert.That(items.Select(x => x.Link), Has.None.EqualTo(DeadReportsMenuCleaner.Link));
Assert.That(items.Where(x => x.MenuTemplate != null).Select(x => x.MenuTemplate.DefaultLink),
Has.None.EqualTo(DeadReportsMenuCleaner.Link));
Assert.That(items.Select(x => x.E2EId), Has.None.EqualTo(DeadReportsMenuCleaner.E2EId));
}

[Test]
public void HeaderMenu_DoesNotOfferTheRemovedReportsPage()
{
var localizationService = Substitute.For<IItemsPlanningLocalizationService>();
localizationService.GetString(Arg.Any<string>()).Returns(x => x.Arg<string>());
var serviceProvider = Substitute.For<IServiceProvider>();
serviceProvider.GetService(typeof(IItemsPlanningLocalizationService)).Returns(localizationService);

var menu = new EformItemsPlanningPlugin().HeaderMenu(serviceProvider);

var links = menu.LeftMenu.SelectMany(x => x.MenuItems).Select(x => x.Link).ToList();
Assert.That(links, Has.None.EqualTo(DeadReportsMenuCleaner.Link));
Assert.That(links, Does.Contain("/plugins/items-planning-pn/plannings"));
}
}
}
Loading
Loading