Files
Umbraco-CMS/tests/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs

193 lines
8.1 KiB
C#
Raw Normal View History

using System;
2017-07-17 17:59:46 +02:00
using System.Web.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
2017-07-17 17:59:46 +02:00
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Configuration.Models;
2021-03-02 14:15:26 +01:00
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.PublishedCache;
using Umbraco.Cms.Infrastructure.PublishedCache.Persistence;
using Umbraco.Cms.Tests.Common;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Extensions;
2017-07-17 17:59:46 +02:00
using Umbraco.Tests.TestHelpers;
using Umbraco.Web;
using Umbraco.Web.Composing;
2017-07-17 17:59:46 +02:00
namespace Umbraco.Tests.Scoping
{
[TestFixture]
2017-10-31 12:48:24 +01:00
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true)]
2017-07-17 17:59:46 +02:00
public class ScopedNuCacheTests : TestWithDatabaseBase
{
private DistributedCacheBinder _distributedCacheBinder;
2017-07-17 17:59:46 +02:00
protected override void Compose()
{
base.Compose();
// the cache refresher component needs to trigger to refresh caches
// but then, it requires a lot of plumbing ;(
// FIXME: and we cannot inject a DistributedCache yet
2017-07-17 17:59:46 +02:00
// so doing all this mess
Builder.Services.AddUnique<IServerMessenger, ScopedXmlTests.LocalServerMessenger>();
Builder.Services.AddUnique(f => Mock.Of<IServerRoleAccessor>());
Builder.WithCollectionBuilder<CacheRefresherCollectionBuilder>()
.Add(() => Builder.TypeLoader.GetCacheRefreshers());
Builder.AddNotificationHandler<ContentPublishedNotification, NotificationHandler>();
2021-03-02 14:15:26 +01:00
}
public class NotificationHandler : INotificationHandler<ContentPublishedNotification>
2021-03-02 14:15:26 +01:00
{
public void Handle(ContentPublishedNotification notification) => PublishedContent?.Invoke(notification);
2021-03-02 14:15:26 +01:00
public static Action<ContentPublishedNotification> PublishedContent { get; set; }
2017-07-17 17:59:46 +02:00
}
public override void TearDown()
{
base.TearDown();
2021-03-02 14:15:26 +01:00
NotificationHandler.PublishedContent = null;
2017-07-17 17:59:46 +02:00
}
2020-09-08 13:03:43 +02:00
protected override IPublishedSnapshotService CreatePublishedSnapshotService(GlobalSettings globalSettings = null)
2017-07-17 17:59:46 +02:00
{
var options = new PublishedSnapshotServiceOptions { IgnoreLocalDb = true };
var publishedSnapshotAccessor = new UmbracoContextPublishedSnapshotAccessor(Current.UmbracoContextAccessor);
2017-07-17 17:59:46 +02:00
var runtimeStateMock = new Mock<IRuntimeState>();
runtimeStateMock.Setup(x => x.Level).Returns(() => RuntimeLevel.Run);
var contentTypeFactory = Factory.GetRequiredService<IPublishedContentTypeFactory>();
var documentRepository = Mock.Of<IDocumentRepository>();
var mediaRepository = Mock.Of<IMediaRepository>();
var memberRepository = Mock.Of<IMemberRepository>();
var hostingEnvironment = TestHelper.GetHostingEnvironment();
2017-10-17 17:43:15 +02:00
var typeFinder = TestHelper.GetTypeFinder();
2020-08-24 16:06:09 +02:00
2020-09-21 21:06:24 +02:00
var nuCacheSettings = new NuCacheSettings();
var lifetime = new Mock<IUmbracoApplicationLifetime>();
var repository = new NuCacheContentRepository(ScopeProvider, AppCaches.Disabled, Mock.Of<ILogger<NuCacheContentRepository>>(), memberRepository, documentRepository, mediaRepository, Mock.Of<IShortStringHelper>(), new UrlSegmentProviderCollection(new[] { new DefaultUrlSegmentProvider(ShortStringHelper) }));
var snapshotService = new PublishedSnapshotService(
2017-07-17 17:59:46 +02:00
options,
null,
ServiceContext,
2017-10-17 17:43:15 +02:00
contentTypeFactory,
2017-10-31 12:48:24 +01:00
publishedSnapshotAccessor,
2018-04-30 21:29:49 +02:00
Mock.Of<IVariationContextAccessor>(),
base.ProfilingLogger,
NullLoggerFactory.Instance,
2017-12-15 11:19:03 +01:00
ScopeProvider,
new NuCacheContentService(repository, ScopeProvider, NullLoggerFactory.Instance, Mock.Of<IEventMessagesFactory>()),
2018-04-30 21:29:49 +02:00
DefaultCultureAccessor,
2020-09-21 21:06:24 +02:00
Microsoft.Extensions.Options.Options.Create(globalSettings ?? new GlobalSettings()),
Factory.GetRequiredService<IEntityXmlSerializer>(),
new NoopPublishedModelFactory(),
hostingEnvironment,
2020-08-24 16:06:09 +02:00
Microsoft.Extensions.Options.Options.Create(nuCacheSettings));
return snapshotService;
2017-07-17 17:59:46 +02:00
}
2020-03-12 15:30:22 +01:00
protected IUmbracoContext GetUmbracoContextNu(string url, RouteData routeData = null, bool setSingleton = false)
2017-07-17 17:59:46 +02:00
{
2017-10-31 12:48:24 +01:00
// ensure we have a PublishedSnapshotService
var service = PublishedSnapshotService as PublishedSnapshotService;
2017-07-17 17:59:46 +02:00
var httpContext = GetHttpContextFactory(url, routeData).HttpContext;
var httpContextAccessor = TestHelper.GetHttpContextAccessor(httpContext);
var globalSettings = TestObjects.GetGlobalSettings();
2017-07-17 17:59:46 +02:00
var umbracoContext = new UmbracoContext(
httpContextAccessor,
2017-07-17 17:59:46 +02:00
service,
Mock.Of<IBackOfficeSecurity>(),
globalSettings,
HostingEnvironment,
2019-12-04 14:03:39 +01:00
new TestVariationContextAccessor(),
UriUtility,
new AspNetCookieManager(httpContextAccessor));
2017-07-17 17:59:46 +02:00
if (setSingleton)
Umbraco.Web.Composing.Current.UmbracoContextAccessor.UmbracoContext = umbracoContext;
return umbracoContext;
}
[TestCase(true)]
[TestCase(false)]
2017-07-18 19:24:27 +02:00
public void TestScope(bool complete)
2017-07-17 17:59:46 +02:00
{
var umbracoContext = GetUmbracoContextNu("http://example.com/", setSingleton: true);
// wire cache refresher
Netcore: Migrate RepositoryBase and ContentTypeServiceBase events (#10141) * Remove ScopeEntityRemove from ContentRepositoryBase and rely on "ing" notifications * Remove old event handler from ContentEventsTests * Remove ScopedVersionRemove from ContentRepositoryBase and rely on service notifications instead * Remove unused ScopedVersionEventArgs from ContentRepositoryBase * Migrate ScopeEntityRefresh to notification pattern Unfortunately it's still published from the repository base * Add simple content type notifications * Publish Notifications instead of events in ContentTypeServiceBase for simple events * Switch OnChanged to use Notifications for ContentTypeServices * Publish notifications instead of raising ScopedRefreshedEntity on ContentTypeServiceBase * Hook up to the new ContentType notifications * Remove DistributedCacheBinderTests There are no longer any events to really test on. * Remove ContentTypeChange EventArgs * Remove ContentService_Copied from DistributedCacheBinder It's no longer used * Cleanup * Cleanup * Removed uncommented code * Fixed issue with unattented installs * Re-add ContentTreeChangeNotification to DistributedCache * Add new notification for ScopedEntityRemove Marked as obsolete/hidden in editor, since this should only be used for nucache for now, and should really be changed in the future * Mark Refresh notifications as obsolete/hidden These should not be used anywhere outside Nucache, and should be changed to tree change at some point. * Raise ScopedEntityRemoveNotification on repos and use in nucache Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 12:17:11 +02:00
_distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(Current.ServerMessenger, Current.CacheRefreshers));
2017-07-17 17:59:46 +02:00
// create document type, document
var contentType = new ContentType(ShortStringHelper, -1) { Alias = "CustomDocument", Name = "Custom Document" };
ServiceContext.ContentTypeService.Save(contentType);
2017-07-17 17:59:46 +02:00
var item = new Content("name", -1, contentType);
2017-07-18 19:24:27 +02:00
// event handler
var evented = 0;
2021-03-02 14:15:26 +01:00
NotificationHandler.PublishedContent = notification =>
2017-07-18 19:24:27 +02:00
{
evented++;
var e = umbracoContext.Content.GetById(item.Id);
2017-07-18 19:24:27 +02:00
// during events, due to LiveSnapshot, we see the changes
Assert.IsNotNull(e);
Assert.AreEqual("changed", e.Name(VariationContextAccessor));
2017-07-18 19:24:27 +02:00
};
2017-07-17 17:59:46 +02:00
using (var scope = ScopeProvider.CreateScope())
{
ServiceContext.ContentService.SaveAndPublish(item);
2017-07-18 19:24:27 +02:00
scope.Complete();
}
// been created
var x = umbracoContext.Content.GetById(item.Id);
2017-07-18 19:24:27 +02:00
Assert.IsNotNull(x);
Assert.AreEqual("name", x.Name(VariationContextAccessor));
2017-07-18 19:24:27 +02:00
using (var scope = ScopeProvider.CreateScope())
{
2017-07-17 17:59:46 +02:00
item.Name = "changed";
ServiceContext.ContentService.SaveAndPublish(item);
2017-07-17 17:59:46 +02:00
if (complete)
scope.Complete();
}
2017-07-18 19:24:27 +02:00
// only 1 event occuring because we are publishing twice for the same event for
// the same object and the scope deduplicates the events (uses the latest)
Assert.AreEqual(complete ? 1 : 0, evented);
2017-07-17 17:59:46 +02:00
2017-07-18 19:24:27 +02:00
// after the scope,
// if completed, we see the changes
// else changes have been rolled back
x = umbracoContext.Content.GetById(item.Id);
2017-07-18 19:24:27 +02:00
Assert.IsNotNull(x);
Assert.AreEqual(complete ? "changed" : "name", x.Name(VariationContextAccessor));
2017-07-17 17:59:46 +02:00
}
}
}