Audit service rework (#19346)

* Introduce new AuditEntryService

- Moved logic related to the IAuditEntryRepository from the AuditService to the new service
- Introduced new Async methods
  - Using ids (for easier transition from the previous Write method)
  - Using keys
- Moved and updated integration tests related to the audit entries to a new test class `AuditEntryServiceTests`
- Added unit tests class `AuditEntryServiceTests` and added a few unit tests
- Added migration to add columns for `performingUserKey` and `affectedUserKey` and convert existing user ids
- Adjusted usages of the old AuditService.Write method to use the new one (mostly notification handlers)

* Audit service rework

- Added new async and paged methods
- Marked (now) redundant methods as obsolete
- Updated all of the usages to use the non-obsolete methods
- Added unit tests class `AuditServiceTests` and some unit tests
- Updated existing integration test

* Apply suggestions from code review

* Small improvement

* Update src/Umbraco.Core/Services/AuditService.cs

* Some minor adjustments following the merge

* Delete unnecessary file

* Small cleanup on the tests
This commit is contained in:
Laura Neto
2025-07-01 09:12:37 +02:00
committed by GitHub
parent ceb745a7bd
commit 447cb881bd
13 changed files with 836 additions and 323 deletions

View File

@@ -1018,7 +1018,7 @@ internal sealed class ContentServiceTests : UmbracoIntegrationTestWithContent
}
[Test]
public void Can_Publish_Content_Variation_And_Detect_Changed_Cultures()
public async Task Can_Publish_Content_Variation_And_Detect_Changed_Cultures()
{
CreateEnglishAndFrenchDocumentType(out var langUk, out var langFr, out var contentType);
@@ -1028,7 +1028,7 @@ internal sealed class ContentServiceTests : UmbracoIntegrationTestWithContent
var published = ContentService.Publish(content, new[] { langFr.IsoCode });
// audit log will only show that french was published
var lastLog = AuditService.GetLogs(content.Id).Last();
var lastLog = (await AuditService.GetItemsByEntityAsync(content.Id, 0, 1)).Items.First();
Assert.AreEqual("Published languages: fr-FR", lastLog.Comment);
// re-get
@@ -1038,7 +1038,7 @@ internal sealed class ContentServiceTests : UmbracoIntegrationTestWithContent
published = ContentService.Publish(content, new[] { langUk.IsoCode });
// audit log will only show that english was published
lastLog = AuditService.GetLogs(content.Id).Last();
lastLog = (await AuditService.GetItemsByEntityAsync(content.Id, 0, 1)).Items.First();
Assert.AreEqual("Published languages: en-GB", lastLog.Comment);
}
@@ -1075,7 +1075,7 @@ internal sealed class ContentServiceTests : UmbracoIntegrationTestWithContent
var unpublished = ContentService.Unpublish(content, langFr.IsoCode);
// audit log will only show that french was unpublished
var lastLog = AuditService.GetLogs(content.Id).Last();
var lastLog = (await AuditService.GetItemsByEntityAsync(content.Id, 0, 1)).Items.First();
Assert.AreEqual("Unpublished languages: fr-FR", lastLog.Comment);
// re-get
@@ -1084,7 +1084,7 @@ internal sealed class ContentServiceTests : UmbracoIntegrationTestWithContent
unpublished = ContentService.Unpublish(content, langGb.IsoCode);
// audit log will only show that english was published
var logs = AuditService.GetLogs(content.Id).ToList();
var logs = (await AuditService.GetItemsByEntityAsync(content.Id, 0, int.MaxValue, Direction.Ascending)).Items.ToList();
Assert.AreEqual("Unpublished languages: en-GB", logs[^2].Comment);
Assert.AreEqual("Unpublished (mandatory language unpublished)", logs[^1].Comment);
}

View File

@@ -1,13 +1,12 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Implement;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
@@ -18,22 +17,26 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
internal sealed class AuditServiceTests : UmbracoIntegrationTest
{
[Test]
public void GetUserLogs()
public async Task GetUserLogs()
{
var sut = (AuditService)Services.GetRequiredService<IAuditService>();
var eventDateUtc = DateTime.UtcNow.AddDays(-1);
var numberOfEntries = 10;
const int numberOfEntries = 10;
for (var i = 0; i < numberOfEntries; i++)
{
eventDateUtc = eventDateUtc.AddMinutes(1);
sut.Add(AuditType.Unpublish, -1, 33, string.Empty, "blah");
await sut.AddAsync(AuditType.Unpublish, Constants.Security.SuperUserKey, 33, string.Empty, "blah");
}
sut.Add(AuditType.Publish, -1, 33, string.Empty, "blah");
await sut.AddAsync(AuditType.Publish, Constants.Security.SuperUserKey, 33, string.Empty, "blah");
var logs = sut.GetUserLogs(-1, AuditType.Unpublish).ToArray();
var logs = (await sut.GetPagedItemsByUserAsync(
Constants.Security.SuperUserKey,
0,
int.MaxValue,
auditTypeFilter: [AuditType.Unpublish])).Items.ToArray();
Assert.Multiple(() =>
{

View File

@@ -25,7 +25,7 @@ public partial class ContentEditingServiceTests : ContentEditingServiceTestsBase
}
protected override void CustomTestSetup(IUmbracoBuilder builder)
=> builder.AddNotificationHandler<ContentCopiedNotification, RelateOnCopyNotificationHandler>();
=> builder.AddNotificationAsyncHandler<ContentCopiedNotification, RelateOnCopyNotificationHandler>();
private ITemplateService TemplateService => GetRequiredService<ITemplateService>();