From 0c4b2604ff8e9d563f9e3fef020572fd628b55bf Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Wed, 17 Mar 2021 15:56:22 +0100 Subject: [PATCH] Revert "Updated "messaging" repositories (NotificationsRepository and CacheInstructionRepository) to use scopes provided as method parameters." This reverts commit b69b4ad7b8cbacc2a2272198b8284413471b2964. --- .../ICacheInstructionRepository.cs | 15 ++-- .../Repositories/INotificationsRepository.cs | 20 +++++ .../Repositories/INotificationsRepository.cs | 28 ------- .../Implement/CacheInstructionRepository.cs | 39 +++++---- .../Implement/NotificationsRepository.cs | 80 ++++++++++--------- .../Implement/CacheInstructionService.cs | 24 +++--- .../Services/Implement/NotificationService.cs | 18 ++--- .../CacheInstructionRepositoryTest.cs | 48 ++++++----- .../NotificationsRepositoryTest.cs | 28 +++---- 9 files changed, 158 insertions(+), 142 deletions(-) rename src/{Umbraco.Infrastructure => Umbraco.Core}/Persistence/Repositories/ICacheInstructionRepository.cs (73%) create mode 100644 src/Umbraco.Core/Persistence/Repositories/INotificationsRepository.cs delete mode 100644 src/Umbraco.Infrastructure/Persistence/Repositories/INotificationsRepository.cs diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/ICacheInstructionRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ICacheInstructionRepository.cs similarity index 73% rename from src/Umbraco.Infrastructure/Persistence/Repositories/ICacheInstructionRepository.cs rename to src/Umbraco.Core/Persistence/Repositories/ICacheInstructionRepository.cs index 75493282dc..e93f5829a1 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/ICacheInstructionRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ICacheInstructionRepository.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using Umbraco.Cms.Core.Models; -using Umbraco.Cms.Core.Scoping; namespace Umbraco.Cms.Core.Persistence.Repositories { @@ -13,39 +12,39 @@ namespace Umbraco.Cms.Core.Persistence.Repositories /// /// Gets the count of pending cache instruction records. /// - int CountAll(IScope scope); + int CountAll(); /// /// Gets the count of pending cache instructions. /// - int CountPendingInstructions(IScope scope, int lastId); + int CountPendingInstructions(int lastId); /// /// Gets the most recent cache instruction record Id. /// /// - int GetMaxId(IScope scope); + int GetMaxId(); /// /// Checks to see if a single cache instruction by Id exists. /// - bool Exists(IScope scope, int id); + bool Exists(int id); /// /// Adds a new cache instruction record. /// - void Add(IScope scope, CacheInstruction cacheInstruction); + void Add(CacheInstruction cacheInstruction); /// /// Gets a collection of cache instructions created later than the provided Id. /// /// Last id processed. /// The maximum number of instructions to retrieve. - IEnumerable GetPendingInstructions(IScope scope, int lastId, int maxNumberToRetrieve); + IEnumerable GetPendingInstructions(int lastId, int maxNumberToRetrieve); /// /// Deletes cache instructions older than the provided date. /// - void DeleteInstructionsOlderThan(IScope scope, DateTime pruneDate); + void DeleteInstructionsOlderThan(DateTime pruneDate); } } diff --git a/src/Umbraco.Core/Persistence/Repositories/INotificationsRepository.cs b/src/Umbraco.Core/Persistence/Repositories/INotificationsRepository.cs new file mode 100644 index 0000000000..069c31ef50 --- /dev/null +++ b/src/Umbraco.Core/Persistence/Repositories/INotificationsRepository.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models.Entities; +using Umbraco.Cms.Core.Models.Membership; + +namespace Umbraco.Cms.Core.Persistence.Repositories +{ + public interface INotificationsRepository : IRepository + { + Notification CreateNotification(IUser user, IEntity entity, string action); + int DeleteNotifications(IUser user); + int DeleteNotifications(IEntity entity); + int DeleteNotifications(IUser user, IEntity entity); + IEnumerable GetEntityNotifications(IEntity entity); + IEnumerable GetUserNotifications(IUser user); + IEnumerable GetUsersNotifications(IEnumerable userIds, string action, IEnumerable nodeIds, Guid objectType); + IEnumerable SetNotifications(IUser user, IEntity entity, string[] actions); + } +} diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/INotificationsRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/INotificationsRepository.cs deleted file mode 100644 index 466153c791..0000000000 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/INotificationsRepository.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using Umbraco.Cms.Core.Models; -using Umbraco.Cms.Core.Models.Entities; -using Umbraco.Cms.Core.Models.Membership; -using Umbraco.Cms.Core.Scoping; - -namespace Umbraco.Cms.Core.Persistence.Repositories -{ - public interface INotificationsRepository : IRepository - { - Notification CreateNotification(IScope scope, IUser user, IEntity entity, string action); - - int DeleteNotifications(IScope scope, IUser user); - - int DeleteNotifications(IScope scope, IEntity entity); - - int DeleteNotifications(IScope scope, IUser user, IEntity entity); - - IEnumerable GetEntityNotifications(IScope scope, IEntity entity); - - IEnumerable GetUserNotifications(IScope scope, IUser user); - - IEnumerable GetUsersNotifications(IScope scope, IEnumerable userIds, string action, IEnumerable nodeIds, Guid objectType); - - IEnumerable SetNotifications(IScope scope, IUser user, IEntity entity, string[] actions); - } -} diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CacheInstructionRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CacheInstructionRepository.cs index a988c7b034..d0e025e06e 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CacheInstructionRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CacheInstructionRepository.cs @@ -16,51 +16,58 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement /// internal class CacheInstructionRepository : ICacheInstructionRepository { + private readonly IScopeAccessor _scopeAccessor; + + public CacheInstructionRepository(IScopeAccessor scopeAccessor) => _scopeAccessor = scopeAccessor; + /// - public int CountAll(IScope scope) + private IScope AmbientScope => _scopeAccessor.AmbientScope; + + /// + public int CountAll() { - Sql sql = scope.SqlContext.Sql().Select("COUNT(*)") + Sql sql = AmbientScope.SqlContext.Sql().Select("COUNT(*)") .From(); - return scope.Database.ExecuteScalar(sql); + return AmbientScope.Database.ExecuteScalar(sql); } /// - public int CountPendingInstructions(IScope scope, int lastId) => - scope.Database.ExecuteScalar("SELECT SUM(instructionCount) FROM umbracoCacheInstruction WHERE id > @lastId", new { lastId }); + public int CountPendingInstructions(int lastId) => + AmbientScope.Database.ExecuteScalar("SELECT SUM(instructionCount) FROM umbracoCacheInstruction WHERE id > @lastId", new { lastId }); /// - public int GetMaxId(IScope scope) => - scope.Database.ExecuteScalar("SELECT MAX(id) FROM umbracoCacheInstruction"); + public int GetMaxId() => + AmbientScope.Database.ExecuteScalar("SELECT MAX(id) FROM umbracoCacheInstruction"); /// - public bool Exists(IScope scope, int id) => scope.Database.Exists(id); + public bool Exists(int id) => AmbientScope.Database.Exists(id); /// - public void Add(IScope scope, CacheInstruction cacheInstruction) + public void Add(CacheInstruction cacheInstruction) { CacheInstructionDto dto = CacheInstructionFactory.BuildDto(cacheInstruction); - scope.Database.Insert(dto); + AmbientScope.Database.Insert(dto); } /// - public IEnumerable GetPendingInstructions(IScope scope, int lastId, int maxNumberToRetrieve) + public IEnumerable GetPendingInstructions(int lastId, int maxNumberToRetrieve) { - Sql sql = scope.SqlContext.Sql().SelectAll() + Sql sql = AmbientScope.SqlContext.Sql().SelectAll() .From() .Where(dto => dto.Id > lastId) .OrderBy(dto => dto.Id); Sql topSql = sql.SelectTop(maxNumberToRetrieve); - return scope.Database.Fetch(topSql).Select(CacheInstructionFactory.BuildEntity); + return AmbientScope.Database.Fetch(topSql).Select(CacheInstructionFactory.BuildEntity); } /// - public void DeleteInstructionsOlderThan(IScope scope, DateTime pruneDate) + public void DeleteInstructionsOlderThan(DateTime pruneDate) { // Using 2 queries is faster than convoluted joins. - var maxId = scope.Database.ExecuteScalar("SELECT MAX(id) FROM umbracoCacheInstruction;"); + var maxId = AmbientScope.Database.ExecuteScalar("SELECT MAX(id) FROM umbracoCacheInstruction;"); Sql deleteSql = new Sql().Append(@"DELETE FROM umbracoCacheInstruction WHERE utcStamp < @pruneDate AND id < @maxId", new { pruneDate, maxId }); - scope.Database.Execute(deleteSql); + AmbientScope.Database.Execute(deleteSql); } } } diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/NotificationsRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/NotificationsRepository.cs index 87fe583acf..e5f3d84e2e 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/NotificationsRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/NotificationsRepository.cs @@ -1,7 +1,6 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; -using NPoco; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.Entities; using Umbraco.Cms.Core.Models.Membership; @@ -14,10 +13,19 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement { public class NotificationsRepository : INotificationsRepository { - public IEnumerable GetUsersNotifications(IScope scope, IEnumerable userIds, string action, IEnumerable nodeIds, Guid objectType) + private readonly IScopeAccessor _scopeAccessor; + + public NotificationsRepository(IScopeAccessor scopeAccessor) + { + _scopeAccessor = scopeAccessor; + } + + private IScope AmbientScope => _scopeAccessor.AmbientScope; + + public IEnumerable GetUsersNotifications(IEnumerable userIds, string action, IEnumerable nodeIds, Guid objectType) { var nodeIdsA = nodeIds.ToArray(); - Sql sql = scope.SqlContext.Sql() + var sql = AmbientScope.SqlContext.Sql() .Select("DISTINCT umbracoNode.id nodeId, umbracoUser.id userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action") .From() .InnerJoin().On(left => left.NodeId, right => right.NodeId) @@ -26,20 +34,17 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement .Where(x => x.Disabled == false) // only approved users .Where(x => x.Action == action); // on the specified action if (nodeIdsA.Length > 0) - { sql .WhereIn(x => x.NodeId, nodeIdsA); // for the specified nodes - } - sql .OrderBy(x => x.Id) .OrderBy(dto => dto.NodeId); - return scope.Database.Fetch(sql).Select(x => new Notification(x.nodeId, x.userId, x.action, objectType)); + return AmbientScope.Database.Fetch(sql).Select(x => new Notification(x.nodeId, x.userId, x.action, objectType)); } - public IEnumerable GetUserNotifications(IScope scope, IUser user) + public IEnumerable GetUserNotifications(IUser user) { - Sql sql = scope.SqlContext.Sql() + var sql = AmbientScope.SqlContext.Sql() .Select("DISTINCT umbracoNode.id, umbracoUser2NodeNotify.userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action") .From() .InnerJoin() @@ -47,21 +52,20 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement .Where(dto => dto.UserId == (int)user.Id) .OrderBy(dto => dto.NodeId); - List dtos = scope.Database.Fetch(sql); - - // Need to map the results + var dtos = AmbientScope.Database.Fetch(sql); + //need to map the results return dtos.Select(d => new Notification(d.id, d.userId, d.action, d.nodeObjectType)).ToList(); } - public IEnumerable SetNotifications(IScope scope, IUser user, IEntity entity, string[] actions) + public IEnumerable SetNotifications(IUser user, IEntity entity, string[] actions) { - DeleteNotifications(scope, user, entity); - return actions.Select(action => CreateNotification(scope, user, entity, action)).ToList(); + DeleteNotifications(user, entity); + return actions.Select(action => CreateNotification(user, entity, action)).ToList(); } - public IEnumerable GetEntityNotifications(IScope scope, IEntity entity) + public IEnumerable GetEntityNotifications(IEntity entity) { - Sql sql = scope.SqlContext.Sql() + var sql = AmbientScope.SqlContext.Sql() .Select("DISTINCT umbracoNode.id, umbracoUser2NodeNotify.userId, umbracoNode.nodeObjectType, umbracoUser2NodeNotify.action") .From() .InnerJoin() @@ -69,30 +73,34 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement .Where(dto => dto.NodeId == entity.Id) .OrderBy(dto => dto.NodeId); - List dtos = scope.Database.Fetch(sql); - - // Need to map the results + var dtos = AmbientScope.Database.Fetch(sql); + //need to map the results return dtos.Select(d => new Notification(d.id, d.userId, d.action, d.nodeObjectType)).ToList(); } - public int DeleteNotifications(IScope scope, IEntity entity) => - scope.Database.Delete("WHERE nodeId = @nodeId", new { nodeId = entity.Id }); - - public int DeleteNotifications(IScope scope, IUser user) => - scope.Database.Delete("WHERE userId = @userId", new { userId = user.Id }); - - public int DeleteNotifications(IScope scope, IUser user, IEntity entity) => - - // Delete all settings on the node for this user - scope.Database.Delete("WHERE userId = @userId AND nodeId = @nodeId", new { userId = user.Id, nodeId = entity.Id }); - - public Notification CreateNotification(IScope scope, IUser user, IEntity entity, string action) + public int DeleteNotifications(IEntity entity) { - Sql sql = scope.SqlContext.Sql() + return AmbientScope.Database.Delete("WHERE nodeId = @nodeId", new { nodeId = entity.Id }); + } + + public int DeleteNotifications(IUser user) + { + return AmbientScope.Database.Delete("WHERE userId = @userId", new { userId = user.Id }); + } + + public int DeleteNotifications(IUser user, IEntity entity) + { + // delete all settings on the node for this user + return AmbientScope.Database.Delete("WHERE userId = @userId AND nodeId = @nodeId", new { userId = user.Id, nodeId = entity.Id }); + } + + public Notification CreateNotification(IUser user, IEntity entity, string action) + { + var sql = AmbientScope.SqlContext.Sql() .Select("DISTINCT nodeObjectType") .From() .Where(nodeDto => nodeDto.NodeId == entity.Id); - Guid nodeType = scope.Database.ExecuteScalar(sql); + var nodeType = AmbientScope.Database.ExecuteScalar(sql); var dto = new User2NodeNotifyDto { @@ -100,7 +108,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement NodeId = entity.Id, UserId = user.Id }; - scope.Database.Insert(dto); + AmbientScope.Database.Insert(dto); return new Notification(dto.NodeId, dto.UserId, dto.Action, nodeType); } } diff --git a/src/Umbraco.Infrastructure/Services/Implement/CacheInstructionService.cs b/src/Umbraco.Infrastructure/Services/Implement/CacheInstructionService.cs index dfce0e4507..0968c3897c 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/CacheInstructionService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/CacheInstructionService.cs @@ -59,7 +59,7 @@ namespace Umbraco.Cms.Core.Services.Implement { if (lastId == 0) { - var count = _cacheInstructionRepository.CountAll(scope); + var count = _cacheInstructionRepository.CountAll(); // If there are instructions but we haven't synced, then a cold boot is necessary. if (count > 0) @@ -70,7 +70,7 @@ namespace Umbraco.Cms.Core.Services.Implement else { // If the last synced instruction is not found in the db, then a cold boot is necessary. - if (!_cacheInstructionRepository.Exists(scope, lastId)) + if (!_cacheInstructionRepository.Exists(lastId)) { return true; } @@ -87,7 +87,7 @@ namespace Umbraco.Cms.Core.Services.Implement { // Check for how many instructions there are to process, each row contains a count of the number of instructions contained in each // row so we will sum these numbers to get the actual count. - count = _cacheInstructionRepository.CountPendingInstructions(scope, lastId); + count = _cacheInstructionRepository.CountPendingInstructions(lastId); return count > limit; } } @@ -97,7 +97,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) { - return _cacheInstructionRepository.GetMaxId(scope); + return _cacheInstructionRepository.GetMaxId(); } } @@ -108,7 +108,7 @@ namespace Umbraco.Cms.Core.Services.Implement using (IScope scope = ScopeProvider.CreateScope()) { - _cacheInstructionRepository.Add(scope, entity); + _cacheInstructionRepository.Add(entity); scope.Complete(); } } @@ -122,7 +122,7 @@ namespace Umbraco.Cms.Core.Services.Implement foreach (IEnumerable instructionsBatch in instructions.InGroupsOf(_globalSettings.DatabaseServerMessenger.MaxProcessingInstructionCount)) { CacheInstruction entity = CreateCacheInstruction(instructionsBatch, localIdentity); - _cacheInstructionRepository.Add(scope, entity); + _cacheInstructionRepository.Add(entity); } scope.Complete(); @@ -138,7 +138,7 @@ namespace Umbraco.Cms.Core.Services.Implement using (_profilingLogger.DebugDuration("Syncing from database...")) using (IScope scope = ScopeProvider.CreateScope()) { - var numberOfInstructionsProcessed = ProcessDatabaseInstructions(scope, released, localIdentity, out int lastId); + var numberOfInstructionsProcessed = ProcessDatabaseInstructions(released, localIdentity, out int lastId); // Check for pruning throttling. if (released || (DateTime.UtcNow - lastPruned) <= _globalSettings.DatabaseServerMessenger.TimeBetweenPruneOperations) @@ -152,7 +152,7 @@ namespace Umbraco.Cms.Core.Services.Implement { case ServerRole.Single: case ServerRole.Master: - PruneOldInstructions(scope); + PruneOldInstructions(); instructionsWerePruned = true; break; } @@ -172,7 +172,7 @@ namespace Umbraco.Cms.Core.Services.Implement /// Thread safety: this is NOT thread safe. Because it is NOT meant to run multi-threaded. /// /// Number of instructions processed. - private int ProcessDatabaseInstructions(IScope scope, bool released, string localIdentity, out int lastId) + private int ProcessDatabaseInstructions(bool released, string localIdentity, out int lastId) { // NOTE: // We 'could' recurse to ensure that no remaining instructions are pending in the table before proceeding but I don't think that @@ -200,7 +200,7 @@ namespace Umbraco.Cms.Core.Services.Implement // some memory however we cannot do that because inside of this loop the cache refreshers are also // performing some lookups which cannot be done with an active reader open. lastId = 0; - foreach (CacheInstruction instruction in _cacheInstructionRepository.GetPendingInstructions(scope, lastId, MaxInstructionsToRetrieve)) + foreach (CacheInstruction instruction in _cacheInstructionRepository.GetPendingInstructions(lastId, MaxInstructionsToRetrieve)) { // If this flag gets set it means we're shutting down! In this case, we need to exit asap and cannot // continue processing anything otherwise we'll hold up the app domain shutdown. @@ -443,10 +443,10 @@ namespace Umbraco.Cms.Core.Services.Implement /// the site to cold boot if there's been no instruction activity for more than TimeToRetainInstructions. /// See: http://issues.umbraco.org/issue/U4-7643#comment=67-25085 /// - private void PruneOldInstructions(IScope scope) + private void PruneOldInstructions() { DateTime pruneDate = DateTime.UtcNow - _globalSettings.DatabaseServerMessenger.TimeToRetainInstructions; - _cacheInstructionRepository.DeleteInstructionsOlderThan(scope, pruneDate); + _cacheInstructionRepository.DeleteInstructionsOlderThan(pruneDate); } } } diff --git a/src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs b/src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs index 9a467cb2c6..e91aa8ce33 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; @@ -141,7 +141,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (var scope = _uowProvider.CreateScope(autoComplete: true)) { - return _notificationsRepository.GetUsersNotifications(scope, userIds, action, nodeIds, objectType); + return _notificationsRepository.GetUsersNotifications(userIds, action, nodeIds, objectType); } } /// @@ -153,7 +153,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (var scope = _uowProvider.CreateScope(autoComplete: true)) { - return _notificationsRepository.GetUserNotifications(scope, user); + return _notificationsRepository.GetUserNotifications(user); } } @@ -192,7 +192,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (var scope = _uowProvider.CreateScope(autoComplete: true)) { - return _notificationsRepository.GetEntityNotifications(scope, entity); + return _notificationsRepository.GetEntityNotifications(entity); } } @@ -204,7 +204,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (var scope = _uowProvider.CreateScope()) { - _notificationsRepository.DeleteNotifications(scope, entity); + _notificationsRepository.DeleteNotifications(entity); scope.Complete(); } } @@ -217,7 +217,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (var scope = _uowProvider.CreateScope()) { - _notificationsRepository.DeleteNotifications(scope, user); + _notificationsRepository.DeleteNotifications(user); scope.Complete(); } } @@ -231,7 +231,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (var scope = _uowProvider.CreateScope()) { - _notificationsRepository.DeleteNotifications(scope, user, entity); + _notificationsRepository.DeleteNotifications(user, entity); scope.Complete(); } } @@ -249,7 +249,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (var scope = _uowProvider.CreateScope()) { - var notifications = _notificationsRepository.SetNotifications(scope, user, entity, actions); + var notifications = _notificationsRepository.SetNotifications(user, entity, actions); scope.Complete(); return notifications; } @@ -266,7 +266,7 @@ namespace Umbraco.Cms.Core.Services.Implement { using (var scope = _uowProvider.CreateScope()) { - var notification = _notificationsRepository.CreateNotification(scope, user, entity, action); + var notification = _notificationsRepository.CreateNotification(user, entity, action); scope.Complete(); return notification; } diff --git a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/CacheInstructionRepositoryTest.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/CacheInstructionRepositoryTest.cs index c2d7ac450d..faa5fd9d6e 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/CacheInstructionRepositoryTest.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/CacheInstructionRepositoryTest.cs @@ -4,9 +4,13 @@ using System; using System.Collections.Generic; using System.Linq; +using Microsoft.Extensions.Logging; using NUnit.Framework; +using Umbraco.Cms.Core; using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Persistence.Querying; using Umbraco.Cms.Core.Scoping; +using Umbraco.Cms.Infrastructure.Persistence; using Umbraco.Cms.Infrastructure.Persistence.Dtos; using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement; using Umbraco.Cms.Tests.Common.Testing; @@ -28,15 +32,16 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos { const int Count = 5; + IScopeProvider sp = ScopeProvider; using (IScope scope = ScopeProvider.CreateScope()) { - var repo = new CacheInstructionRepository(); + var repo = new CacheInstructionRepository((IScopeAccessor)sp); for (var i = 0; i < Count; i++) { - repo.Add(scope, new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); + repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); } - var count = repo.CountAll(scope); + var count = repo.CountAll(); Assert.That(count, Is.EqualTo(Count)); } @@ -45,15 +50,16 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos [Test] public void Can_Count_Pending_Instructions() { + IScopeProvider sp = ScopeProvider; using (IScope scope = ScopeProvider.CreateScope()) { - var repo = new CacheInstructionRepository(); + var repo = new CacheInstructionRepository((IScopeAccessor)sp); for (var i = 0; i < 5; i++) { - repo.Add(scope, new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); + repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); } - var count = repo.CountPendingInstructions(scope, 2); + var count = repo.CountPendingInstructions(2); Assert.That(count, Is.EqualTo(3)); } @@ -62,15 +68,16 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos [Test] public void Can_Check_Exists() { + IScopeProvider sp = ScopeProvider; using (IScope scope = ScopeProvider.CreateScope()) { - var repo = new CacheInstructionRepository(); + var repo = new CacheInstructionRepository((IScopeAccessor)sp); - var existsBefore = repo.Exists(scope, 1); + var existsBefore = repo.Exists(1); - repo.Add(scope, new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); + repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); - var existsAfter = repo.Exists(scope, 1); + var existsAfter = repo.Exists(1); Assert.That(existsBefore, Is.False); Assert.That(existsAfter, Is.True); @@ -85,10 +92,11 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos var date = new DateTime(2021, 7, 3, 10, 30, 0); const int InstructionCount = 1; + IScopeProvider sp = ScopeProvider; using (IScope scope = ScopeProvider.CreateScope()) { - var repo = new CacheInstructionRepository(); - repo.Add(scope, new CacheInstruction(0, date, Instructions, OriginIdentiy, InstructionCount)); + var repo = new CacheInstructionRepository((IScopeAccessor)sp); + repo.Add(new CacheInstruction(0, date, Instructions, OriginIdentiy, InstructionCount)); List dtos = scope.Database.Fetch("WHERE id > -1"); @@ -105,15 +113,16 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos [Test] public void Can_Get_Pending_Instructions() { + IScopeProvider sp = ScopeProvider; using (IScope scope = ScopeProvider.CreateScope()) { - var repo = new CacheInstructionRepository(); + var repo = new CacheInstructionRepository((IScopeAccessor)sp); for (var i = 0; i < 5; i++) { - repo.Add(scope, new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); + repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); } - IEnumerable instructions = repo.GetPendingInstructions(scope, 2, 2); + IEnumerable instructions = repo.GetPendingInstructions(2, 2); Assert.That(instructions.Count(), Is.EqualTo(2)); @@ -124,18 +133,19 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos [Test] public void Can_Delete_Old_Instructions() { + IScopeProvider sp = ScopeProvider; using (IScope scope = ScopeProvider.CreateScope()) { - var repo = new CacheInstructionRepository(); + var repo = new CacheInstructionRepository((IScopeAccessor)sp); for (var i = 0; i < 5; i++) { DateTime date = i == 0 ? DateTime.UtcNow.AddDays(-2) : DateTime.UtcNow; - repo.Add(scope, new CacheInstruction(0, date, Instructions, OriginIdentiy, InstructionCount)); + repo.Add(new CacheInstruction(0, date, Instructions, OriginIdentiy, InstructionCount)); } - repo.DeleteInstructionsOlderThan(scope, DateTime.UtcNow.AddDays(-1)); + repo.DeleteInstructionsOlderThan(DateTime.UtcNow.AddDays(-1)); - var count = repo.CountAll(scope); + var count = repo.CountAll(); Assert.That(count, Is.EqualTo(4)); // 5 have been added, 1 is older and deleted. } } diff --git a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/NotificationsRepositoryTest.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/NotificationsRepositoryTest.cs index 39cbfe9a75..be625e4afd 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/NotificationsRepositoryTest.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/NotificationsRepositoryTest.cs @@ -29,7 +29,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos IScopeProvider provider = ScopeProvider; using (IScope scope = provider.CreateScope()) { - var repo = new NotificationsRepository(); + var repo = new NotificationsRepository((IScopeAccessor)provider); var node = new NodeDto // create bogus item so we can add a notification { @@ -48,7 +48,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos IEntity entity = Mock.Of(e => e.Id == node.NodeId); IUser user = Mock.Of(e => e.Id == node.UserId); - Notification notification = repo.CreateNotification(scope, user, entity, "A"); + Notification notification = repo.CreateNotification(user, entity, "A"); Assert.AreEqual("A", notification.Action); Assert.AreEqual(node.NodeId, notification.EntityId); @@ -63,7 +63,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos IScopeProvider provider = ScopeProvider; using (IScope scope = provider.CreateScope()) { - var repo = new NotificationsRepository(); + var repo = new NotificationsRepository((IScopeAccessor)provider); var userDto = new UserDto { Email = "test", Login = "test", Password = "test", UserName = "test", UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now }; scope.Database.Insert(userDto); @@ -76,10 +76,10 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = -1 }; object result = scope.Database.Insert(node); IEntity entity = Mock.Of(e => e.Id == node.NodeId); - Notification notification = repo.CreateNotification(scope, (i % 2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture)); + Notification notification = repo.CreateNotification((i % 2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture)); } - IEnumerable notifications = repo.GetUserNotifications(scope, userAdmin); + IEnumerable notifications = repo.GetUserNotifications(userAdmin); Assert.AreEqual(5, notifications.Count()); } @@ -91,7 +91,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos IScopeProvider provider = ScopeProvider; using (IScope scope = provider.CreateScope()) { - var repo = new NotificationsRepository(); + var repo = new NotificationsRepository((IScopeAccessor)provider); var node1 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1,1", SortOrder = 1, Text = "hello1", Trashed = false, UniqueId = Guid.NewGuid(), UserId = -1 }; scope.Database.Insert(node1); @@ -105,10 +105,10 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos var userDto = new UserDto { Email = "test" + i, Login = "test" + i, Password = "test", UserName = "test" + i, UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now }; scope.Database.Insert(userDto); IUser userNew = Mock.Of(e => e.Id == userDto.Id); - Notification notification = repo.CreateNotification(scope, userNew, (i % 2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture)); + Notification notification = repo.CreateNotification(userNew, (i % 2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture)); } - IEnumerable notifications = repo.GetEntityNotifications(scope, entity1); + IEnumerable notifications = repo.GetEntityNotifications(entity1); Assert.AreEqual(5, notifications.Count()); } @@ -120,7 +120,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos IScopeProvider provider = ScopeProvider; using (IScope scope = provider.CreateScope()) { - var repo = new NotificationsRepository(); + var repo = new NotificationsRepository((IScopeAccessor)provider); var node1 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1,1", SortOrder = 1, Text = "hello1", Trashed = false, UniqueId = Guid.NewGuid(), UserId = -1 }; scope.Database.Insert(node1); @@ -134,10 +134,10 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos var userDto = new UserDto { Email = "test" + i, Login = "test" + i, Password = "test", UserName = "test" + i, UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now }; scope.Database.Insert(userDto); IUser userNew = Mock.Of(e => e.Id == userDto.Id); - Notification notification = repo.CreateNotification(scope, userNew, (i % 2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture)); + Notification notification = repo.CreateNotification(userNew, (i % 2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture)); } - int delCount = repo.DeleteNotifications(scope, entity1); + int delCount = repo.DeleteNotifications(entity1); Assert.AreEqual(5, delCount); } @@ -149,7 +149,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos IScopeProvider provider = ScopeProvider; using (IScope scope = provider.CreateScope()) { - var repo = new NotificationsRepository(); + var repo = new NotificationsRepository((IScopeAccessor)provider); var userDto = new UserDto { Email = "test", Login = "test", Password = "test", UserName = "test", UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now }; scope.Database.Insert(userDto); @@ -162,10 +162,10 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repos var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = -1 }; object result = scope.Database.Insert(node); IEntity entity = Mock.Of(e => e.Id == node.NodeId); - Notification notification = repo.CreateNotification(scope, (i % 2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture)); + Notification notification = repo.CreateNotification((i % 2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture)); } - int delCount = repo.DeleteNotifications(scope, userAdmin); + int delCount = repo.DeleteNotifications(userAdmin); Assert.AreEqual(5, delCount); }