Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/AuditServiceTests.cs
Laura Neto 7fc2bc84de Audit entries rework (#19345)
* 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)

* Apply suggestions from code review

* Small improvement

* Some adjustments following code review. Removed UnknownUserKey and used null instead.

* Small adjustments

* Better handle audits performed during the migration state

* Update TODO comment
2025-06-06 13:12:35 +02:00

47 lines
1.4 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
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;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
internal sealed class AuditServiceTests : UmbracoIntegrationTest
{
[Test]
public void GetUserLogs()
{
var sut = (AuditService)Services.GetRequiredService<IAuditService>();
var eventDateUtc = DateTime.UtcNow.AddDays(-1);
var numberOfEntries = 10;
for (var i = 0; i < numberOfEntries; i++)
{
eventDateUtc = eventDateUtc.AddMinutes(1);
sut.Add(AuditType.Unpublish, -1, 33, string.Empty, "blah");
}
sut.Add(AuditType.Publish, -1, 33, string.Empty, "blah");
var logs = sut.GetUserLogs(-1, AuditType.Unpublish).ToArray();
Assert.Multiple(() =>
{
Assert.IsNotNull(logs);
CollectionAssert.AllItemsAreNotNull(logs);
Assert.AreEqual(numberOfEntries, logs.Length);
Assert.AreEqual(numberOfEntries, logs.Count(x => x.AuditType == AuditType.Unpublish));
});
}
}