|
| 1 | +using System; |
| 2 | +using Microsoft.AspNetCore.Http; |
| 3 | +using Moq; |
| 4 | +using NUnit.Framework; |
| 5 | +using Umbraco.Cms.Core.DependencyInjection; |
| 6 | +using Umbraco.Cms.Core.Events; |
| 7 | +using Umbraco.Cms.Core.Models; |
| 8 | +using Umbraco.Cms.Core.Models.PublishedContent; |
| 9 | +using Umbraco.Cms.Core.Notifications; |
| 10 | +using Umbraco.Cms.Core.Services; |
| 11 | +using Umbraco.Cms.Core.Sync; |
| 12 | +using Umbraco.Cms.Core.Web; |
| 13 | +using Umbraco.Cms.Infrastructure.DependencyInjection; |
| 14 | +using Umbraco.Cms.Tests.Common; |
| 15 | +using Umbraco.Cms.Tests.Common.Testing; |
| 16 | +using Umbraco.Cms.Tests.Integration.Testing; |
| 17 | +using Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Scoping; |
| 18 | +using Umbraco.Extensions; |
| 19 | + |
| 20 | +namespace Umbraco.Tests.Scoping |
| 21 | +{ |
| 22 | + [TestFixture] |
| 23 | + [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] |
| 24 | + public class ScopedNuCacheTests : UmbracoIntegrationTest |
| 25 | + { |
| 26 | + private IContentService ContentService => GetRequiredService<IContentService>(); |
| 27 | + private Mock<IHttpContextAccessor> MockHttpContextAccessor { get; set; } = CreateMockHttpContextAccessor(); |
| 28 | + private IUmbracoContextFactory UmbracoContextFactory => GetRequiredService<IUmbracoContextFactory>(); |
| 29 | + private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>(); |
| 30 | + private IVariationContextAccessor VariationContextAccessor => GetRequiredService<IVariationContextAccessor>(); |
| 31 | + |
| 32 | + protected override void CustomTestSetup(IUmbracoBuilder builder) |
| 33 | + { |
| 34 | + NotificationHandler.PublishedContent = notification => { }; |
| 35 | + builder.Services.AddUnique<IServerMessenger, ScopedRepositoryTests.LocalServerMessenger>(); |
| 36 | + builder.AddCoreNotifications(); |
| 37 | + builder.AddNotificationHandler<ContentPublishedNotification, NotificationHandler>(); |
| 38 | + builder.Services.AddUnique<IUmbracoContextAccessor, TestUmbracoContextAccessor>(); |
| 39 | + builder.Services.AddUnique<IHttpContextAccessor>(MockHttpContextAccessor.Object); |
| 40 | + builder.AddNuCache(); |
| 41 | + } |
| 42 | + |
| 43 | + public class NotificationHandler : INotificationHandler<ContentPublishedNotification> |
| 44 | + { |
| 45 | + public void Handle(ContentPublishedNotification notification) => PublishedContent?.Invoke(notification); |
| 46 | + |
| 47 | + public static Action<ContentPublishedNotification> PublishedContent { get; set; } |
| 48 | + } |
| 49 | + |
| 50 | + private static Mock<IHttpContextAccessor> CreateMockHttpContextAccessor() |
| 51 | + { |
| 52 | + var mockHttpContextAccessor = new Mock<IHttpContextAccessor>(); |
| 53 | + var httpContext = new DefaultHttpContext(); |
| 54 | + httpContext.Request.Scheme = "http"; |
| 55 | + httpContext.Request.Host = new HostString("localhost"); |
| 56 | + |
| 57 | + mockHttpContextAccessor.SetupGet(x => x.HttpContext).Returns(httpContext); |
| 58 | + return mockHttpContextAccessor; |
| 59 | + } |
| 60 | + |
| 61 | + [TestCase(true)] |
| 62 | + [TestCase(false)] |
| 63 | + public void TestScope(bool complete) |
| 64 | + { |
| 65 | + var umbracoContext = UmbracoContextFactory.EnsureUmbracoContext().UmbracoContext; |
| 66 | + |
| 67 | + // create document type, document |
| 68 | + var contentType = new ContentType(ShortStringHelper, -1) { Alias = "CustomDocument", Name = "Custom Document" }; |
| 69 | + ContentTypeService.Save(contentType); |
| 70 | + var item = new Content("name", -1, contentType); |
| 71 | + |
| 72 | + using (var scope = ScopeProvider.CreateScope()) |
| 73 | + { |
| 74 | + ContentService.SaveAndPublish(item); |
| 75 | + scope.Complete(); |
| 76 | + } |
| 77 | + |
| 78 | + // event handler |
| 79 | + var evented = 0; |
| 80 | + NotificationHandler.PublishedContent = notification => |
| 81 | + { |
| 82 | + evented++; |
| 83 | + |
| 84 | + var e = umbracoContext.Content.GetById(item.Id); |
| 85 | + |
| 86 | + // during events, due to LiveSnapshot, we see the changes |
| 87 | + Assert.IsNotNull(e); |
| 88 | + Assert.AreEqual("changed", e.Name(VariationContextAccessor)); |
| 89 | + }; |
| 90 | + |
| 91 | + // been created |
| 92 | + var x = umbracoContext.Content.GetById(item.Id); |
| 93 | + Assert.IsNotNull(x); |
| 94 | + Assert.AreEqual("name", x.Name(VariationContextAccessor)); |
| 95 | + |
| 96 | + using (var scope = ScopeProvider.CreateScope()) |
| 97 | + { |
| 98 | + item.Name = "changed"; |
| 99 | + ContentService.SaveAndPublish(item); |
| 100 | + |
| 101 | + if (complete) |
| 102 | + { |
| 103 | + scope.Complete(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // only 1 event occuring because we are publishing twice for the same event for |
| 108 | + // the same object and the scope deduplicates the events (uses the latest) |
| 109 | + Assert.AreEqual(complete ? 1 : 0, evented); |
| 110 | + |
| 111 | + // after the scope, |
| 112 | + // if completed, we see the changes |
| 113 | + // else changes have been rolled back |
| 114 | + x = umbracoContext.Content.GetById(item.Id); |
| 115 | + Assert.IsNotNull(x); |
| 116 | + Assert.AreEqual(complete ? "changed" : "name", x.Name(VariationContextAccessor)); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments