Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/AuditRepositoryTest.cs
Mole d9f8faf509 Load balancing: Load balance isolated caches to allow the backoffice to be load balanced (#20417)
* V16:  Cache Version Mechanism (#19747)

* Add RepositoryCacheVersion table

* Add repository

* Add Cache version lock

* Add GetAll method to repository

* Add RepositoryCacheVersionService

* Remember to add lock in data creator

* Work my way out of constructor hell

This is why we use DI folks. 🤦

* Add checks to specific cache policies

* Fix migration

* Add to schema creator

* Fix database access

* Initialize the cache version on in memory miss

* Make cache version service internal

* Add tests

* Apply suggestions from code review

Co-authored-by: Andy Butland <abutland73@gmail.com>

* Add missing obsoletions

* Prefer full name

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>

* fixed merge

* V16/feature/move last synced id to db (#19884)

* Foundation work for moving last synced id

* register manager and repo in dependency injection

* Fixing to make tests work

* Replacing the use of the old LastSyncedFileManager.cs with the new LastSyncedManager.cs

* Testing to delete out of sync id and old entries

* changing some stuff to please the reviewer.

* Inverted saving methods id check and fixed documentation mishaps

* Loadbalancing: Add Cache Sync service to allow us to roll forward isolated caches when backoffice is load balanced. (#20398)

* Split cache refreshers into internal and external caches

* Add obsolete constructor for CacheInstructionsPruningJob

* Add xml docs

* Move lastID management into CacheInstructionService

* Cache last synced ids in memory

* Lock when processing instructions

* Sync caches when out of sync

* Fix constructors for ICacheSyncService

* Cache version on request

* Register caches as synced when instructions are processed

* Rename CacheVersionAccessor to IRepositoryCacheVersionAccessor

* Set caches as synced before actually syncing the caches

* Set caches as synced before syncing, within scope, this should also lock the cache version from being written to whilst updating caches

* Only check version for backoffice requests

* Clear request cache when caches are syned

* Default to using NOOP cache version service

* Don't generate local identity in database server messenger anymore

* Fix ambiguous constructor

* Add helper method to switch to load balanced isolated caches

* Fix LastSyncedManagerTests

* Fix RepositoryCacheVersionServiceTests

* Fix DefaultCachePolicyTests

* Use correct constructor in FullDataSetRepositoryCachePolicy

* Minor cleanup

* Add XML docs

* Add more xml docs

* Apply suggestions from code review

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

---------

Co-authored-by: Zeegaan <skrivdetud@gmail.com>
Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

* Fix migration plan

* fix tests

* Fix integration tests

* Fix changes from github review

* Move premigrations to v17

* Make lock constantws sequential

* Fix comment

* Make IRepositoryCacheVersionService and ICacheSyncService protected on EntityRepositoryBase

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>
Co-authored-by: Nicklas Kramer <nik@umbraco.dk>
Co-authored-by: NillasKA <kramernicklas@gmail.com>
Co-authored-by: Zeegaan <skrivdetud@gmail.com>
Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
2025-10-08 14:55:50 +02:00

233 lines
8.8 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
using Umbraco.Cms.Infrastructure.Scoping;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositories;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console)]
internal sealed class AuditRepositoryTest : UmbracoIntegrationTest
{
[SetUp]
public void Prepare() => _logger = LoggerFactory.CreateLogger<AuditRepository>();
private ILogger<AuditRepository> _logger;
private IAuditRepository AuditRepository => GetRequiredService<IAuditRepository>();
private IAuditItem GetAuditItem(int id) => new AuditItem(id, AuditType.System, -1, UmbracoObjectTypes.Document.GetName(), "This is a System audit trail");
[Test]
public void Can_Add_Audit_Entry()
{
var sp = ScopeProvider;
using (var scope = ScopeProvider.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
repo.Save(new AuditItem(-1, AuditType.System, -1, UmbracoObjectTypes.Document.GetName(), "This is a System audit trail"));
var dtos = ScopeAccessor.AmbientScope.Database.Fetch<LogDto>("WHERE id > -1");
Assert.That(dtos.Any(), Is.True);
Assert.That(dtos.First().Comment, Is.EqualTo("This is a System audit trail"));
}
}
[Test]
public void Has_Create_Date_When_Get_By_Id()
{
using var scope = ScopeProvider.CreateScope();
AuditRepository.Save(GetAuditItem(1));
var auditEntry = AuditRepository.Get(1);
Assert.That(auditEntry.CreateDate, Is.Not.EqualTo(default(DateTime)));
}
[Test]
public void Has_Create_Date_When_Get_By_Query()
{
using var scope = ScopeProvider.CreateScope();
AuditRepository.Save(GetAuditItem(1));
var auditEntry = AuditRepository.Get(AuditType.System, ScopeProvider.CreateQuery<IAuditItem>().Where(x => x.Id == 1)).FirstOrDefault();
Assert.That(auditEntry, Is.Not.Null);
Assert.That(auditEntry.CreateDate, Is.Not.EqualTo(default(DateTime)));
}
[Test]
public void Has_Create_Date_When_Get_By_Paged_Query()
{
using var scope = ScopeProvider.CreateScope();
AuditRepository.Save(GetAuditItem(1));
var auditEntry = AuditRepository.GetPagedResultsByQuery(ScopeProvider.CreateQuery<IAuditItem>().Where(x => x.Id == 1),0, 10, out long total, Direction.Ascending, null, null).FirstOrDefault();
Assert.That(auditEntry, Is.Not.Null);
Assert.That(auditEntry.CreateDate, Is.Not.EqualTo(default(DateTime)));
}
[Test]
public void Get_Paged_Items()
{
var sp = ScopeProvider;
using (var scope = sp.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
for (var i = 0; i < 100; i++)
{
repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} created"));
repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} published"));
}
scope.Complete();
}
using (var scope = sp.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
var page = repo.GetPagedResultsByQuery(sp.CreateQuery<IAuditItem>(), 0, 10, out var total, Direction.Descending, null, null);
Assert.AreEqual(10, page.Count());
Assert.AreEqual(200, total);
}
}
[Test]
public void Get_Paged_Items_By_User_Id_With_Query_And_Filter()
{
var sp = ScopeProvider;
using (var scope = sp.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
for (var i = 0; i < 100; i++)
{
repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} created"));
repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} published"));
}
scope.Complete();
}
using (var scope = sp.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
var query = sp.CreateQuery<IAuditItem>().Where(x => x.UserId == -1);
try
{
ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlTrace = true;
ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlCount = true;
var page = repo.GetPagedResultsByQuery(
query,
0,
10,
out var total,
Direction.Descending,
new[] { AuditType.Publish },
sp.CreateQuery<IAuditItem>()
.Where(x => x.UserId > -2));
Assert.AreEqual(10, page.Count());
Assert.AreEqual(100, total);
}
finally
{
ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlTrace = false;
ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlCount = false;
}
}
}
[Test]
public void Get_Paged_Items_With_AuditType_Filter()
{
var sp = ScopeProvider;
using (var scope = sp.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
for (var i = 0; i < 100; i++)
{
repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} created"));
repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} published"));
}
scope.Complete();
}
using (var scope = sp.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
var page = repo.GetPagedResultsByQuery(
sp.CreateQuery<IAuditItem>(),
0,
9,
out var total,
Direction.Descending,
new[] { AuditType.Publish },
null)
.ToArray();
Assert.AreEqual(9, page.Length);
Assert.IsTrue(page.All(x => x.AuditType == AuditType.Publish));
Assert.AreEqual(100, total);
}
}
[Test]
public void Get_Paged_Items_With_Custom_Filter()
{
var sp = ScopeProvider;
using (var scope = sp.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
for (var i = 0; i < 100; i++)
{
repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), "Content created"));
repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), "Content published"));
}
scope.Complete();
}
using (var scope = sp.CreateScope())
{
var repo = new AuditRepository((IScopeAccessor)sp, _logger, Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
var page = repo.GetPagedResultsByQuery(
sp.CreateQuery<IAuditItem>(),
0,
8,
out var total,
Direction.Descending,
null,
sp.CreateQuery<IAuditItem>()
.Where(item => item.Comment == "Content created"))
.ToArray();
Assert.AreEqual(8, page.Length);
Assert.IsTrue(page.All(x => x.Comment == "Content created"));
Assert.AreEqual(100, total);
}
}
}