V15: Only cache items if all ancestors are published (#18337)

* Introduce IsDocumentPublishedInAnyCulture

Sometimes we don't care about culture

* Check ancestor path when resolving cache items

* Fix tests

* Rebuild NavigationService

* Only set node if it has a published ancestor path

* Remove branch when unpublished

* Add tests

* Add seed test

* Consider published ancestor path when seeding documents

* Introduce MediaBreadthFirstKeyProviderTests

This is needed since the logic of document and media is no longer the same

* Remove unused services

* Move assert page to helper

* Add variant tests

* Add tests

* Filter keys in ContentTypeSeedKeyProvider

* Fix tests

* Add failing test showing refreshing issue

* Don't blow up if we can't resolve the node from navigation cache

Turns out that this can actually happen :D Should be fine to just return false

* Refactor cache refresher check

* Make NavigationQueryService service protected

* Add comment on how to refactor breadth first key provider

* Refactor if statement
This commit is contained in:
Mole
2025-02-17 12:51:33 +01:00
committed by GitHub
parent 69e251ad17
commit c76d764598
15 changed files with 680 additions and 28 deletions

View File

@@ -20,7 +20,7 @@ public abstract class UmbracoIntegrationTestWithContentEditing : UmbracoIntegrat
protected ITemplateService TemplateService => GetRequiredService<ITemplateService>();
private IContentEditingService ContentEditingService => (IContentEditingService)GetRequiredService<IContentEditingService>();
protected IContentEditingService ContentEditingService => (IContentEditingService)GetRequiredService<IContentEditingService>();
private IContentPublishingService ContentPublishingService => (IContentPublishingService)GetRequiredService<IContentPublishingService>();

View File

@@ -0,0 +1,54 @@
using NUnit.Framework;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
namespace Umbraco.Cms.Tests.Integration.Umbraco.PublishedCache.HybridCache;
internal static class CacheTestsHelper
{
internal static void AssertPage(IContent baseContent, IPublishedContent? comparisonContent, bool isPublished = true)
{
Assert.Multiple(() =>
{
Assert.IsNotNull(comparisonContent);
if (baseContent.ContentType.VariesByCulture())
{
foreach (var culture in baseContent.CultureInfos ?? Enumerable.Empty<ContentCultureInfos>())
{
if (comparisonContent.Cultures.TryGetValue(culture.Culture, out var publishedCulture) is false)
{
continue;
}
Assert.That(publishedCulture.Name, Is.EqualTo(culture.Name));
}
}
else
{
Assert.That(comparisonContent.Name, Is.EqualTo(baseContent.Name));
}
Assert.That(comparisonContent.IsPublished(), Is.EqualTo(isPublished));
});
AssertProperties(baseContent.Properties, comparisonContent!.Properties);
}
internal static void AssertProperties(IPropertyCollection propertyCollection,
IEnumerable<IPublishedProperty> publishedProperties)
{
foreach (var prop in propertyCollection)
{
AssertProperty(prop, publishedProperties.First(x => x.Alias == prop.Alias));
}
}
internal static void AssertProperty(IProperty property, IPublishedProperty publishedProperty)
{
Assert.Multiple(() =>
{
Assert.AreEqual(property.Alias, publishedProperty.Alias);
Assert.AreEqual(property.PropertyType.Alias, publishedProperty.PropertyType.Alias);
});
}
}

View File

@@ -0,0 +1,93 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Infrastructure.HybridCache.Services;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
namespace Umbraco.Cms.Tests.Integration.Umbraco.PublishedCache.HybridCache;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class DocumentHybridCacheAncestryTests : UmbracoIntegrationTestWithContent
{
private IContentPublishingService ContentPublishingService => GetRequiredService<IContentPublishingService>();
private IPublishedContentCache PublishedContentCache => GetRequiredService<IPublishedContentCache>();
private IDocumentCacheService DocumentCacheService => GetRequiredService<IDocumentCacheService>();
private Content SubSubPage;
protected override void CustomTestSetup(IUmbracoBuilder builder)
{
builder.AddNotificationHandler<ContentTreeChangeNotification, ContentTreeChangeDistributedCacheNotificationHandler>();
builder.Services.AddUnique<IServerMessenger, ContentEventsTests.LocalServerMessenger>();
}
public override void Setup()
{
base.Setup();
// Publish documents
SubSubPage = ContentBuilder.CreateSimpleContent(ContentType, "SubSubPage", Subpage.Id);
SubSubPage.Key = Guid.Parse("E4C369B5-CCCA-4981-ADAC-389824CF6B0B");
ContentService.Save(SubSubPage, -1);
}
[Test]
public async Task CantGetPublishedContentIfParentIsUnpublished()
{
// Text Page
// Sub Page <-- Unpublished
// Sub Sub Page
await ContentPublishingService.PublishBranchAsync(Textpage.Key, Array.Empty<string>(), true, Constants.Security.SuperUserKey);
await ContentPublishingService.UnpublishAsync(Subpage.Key, null, Constants.Security.SuperUserKey);
var published = await PublishedContentCache.GetByIdAsync(SubSubPage.Key);
Assert.IsNull(published);
}
[Test]
public async Task CanGetPublishedContentIfParentIsPublished()
{
await ContentPublishingService.PublishBranchAsync(Textpage.Key, Array.Empty<string>(), true, Constants.Security.SuperUserKey);
var published = await PublishedContentCache.GetByIdAsync(SubSubPage.Key);
CacheTestsHelper.AssertPage(SubSubPage, published);
}
[Test]
public async Task CantGetPublishedContentAfterSeedingIfParentIsUnpublished()
{
// Text Page
// Sub Page <-- Unpublished
// Sub Sub Page
await ContentPublishingService.PublishBranchAsync(Textpage.Key, Array.Empty<string>(), true, Constants.Security.SuperUserKey);
await ContentPublishingService.UnpublishAsync(Subpage.Key, null, Constants.Security.SuperUserKey);
// Clear cache also seeds, but we have to reset the seed keys first since these are cached from test startup
var cacheService = DocumentCacheService as DocumentCacheService;
cacheService!.ResetSeedKeys();
await DocumentCacheService.ClearMemoryCacheAsync(CancellationToken.None);
var unpublishedSubSubPage = await PublishedContentCache.GetByIdAsync(SubSubPage.Key);
var unpublishedSubPage = await PublishedContentCache.GetByIdAsync(Subpage.Key);
Assert.IsNull(unpublishedSubSubPage);
Assert.IsNull(unpublishedSubPage);
// We should however be able to get the still published root Text Page
var publishedTextPage = await PublishedContentCache.GetByIdAsync(Textpage.Key);
CacheTestsHelper.AssertPage(Textpage, publishedTextPage);
}
}

View File

@@ -0,0 +1,184 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentPublishing;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.ContentTypeEditing;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.TestHelpers;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
namespace Umbraco.Cms.Tests.Integration.Umbraco.PublishedCache.HybridCache;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class DocumentHybridCacheAncestryVariantTests : UmbracoIntegrationTest
{
private string _englishIsoCode = "en-US";
private string _danishIsoCode = "da-DK";
private string _variantTitleAlias = "variantTitle";
private string _variantTitleName = "Variant Title";
private string _invariantTitleAlias = "invariantTitle";
private string _invariantTitleName = "Invariant Title";
private IContent rootContent;
private IContent childNode;
private IContent grandChildNode;
private ILanguageService LanguageService => GetRequiredService<ILanguageService>();
private IContentEditingService ContentEditingService => GetRequiredService<IContentEditingService>();
private IContentTypeEditingService ContentTypeEditingService => GetRequiredService<IContentTypeEditingService>();
private IContentPublishingService ContentPublishingService => GetRequiredService<IContentPublishingService>();
private IPublishedContentCache PublishedContentCache => GetRequiredService<IPublishedContentCache>();
protected override void CustomTestSetup(IUmbracoBuilder builder)
{
builder.AddNotificationHandler<ContentTreeChangeNotification, ContentTreeChangeDistributedCacheNotificationHandler>();
builder.Services.AddUnique<IServerMessenger, ContentEventsTests.LocalServerMessenger>();
}
[SetUp]
public async Task Setup() => await CreateTestData();
[Test]
[TestCase(true)]
[TestCase(false)]
public async Task AllCulturesUnpublished(bool preview)
{
// Publish branch in all cultures
var publishAttempt = await ContentPublishingService.PublishBranchAsync(rootContent.Key, [_englishIsoCode, _danishIsoCode], true, Constants.Security.SuperUserKey);
Assert.IsTrue(publishAttempt.Success);
Assert.That(publishAttempt.Result.SucceededItems.Count(), Is.EqualTo(3));
// Unpublish all cultures in child
var unpublishAttempt = await ContentPublishingService.UnpublishAsync(childNode.Key, new HashSet<string>([_englishIsoCode, _danishIsoCode]), Constants.Security.SuperUserKey);
Assert.IsTrue(unpublishAttempt.Success);
var publishedGrandChild = await PublishedContentCache.GetByIdAsync(grandChildNode.Key, preview);
if (preview)
{
CacheTestsHelper.AssertPage(grandChildNode, publishedGrandChild, false);
}
else
{
Assert.IsNull(publishedGrandChild);
}
}
[Test]
public async Task SingleCultureUnpublished()
{
var publishAttempt = await ContentPublishingService.PublishBranchAsync(rootContent.Key, [_englishIsoCode, _danishIsoCode], true, Constants.Security.SuperUserKey);
Assert.IsTrue(publishAttempt.Success);
Assert.That(publishAttempt.Result.SucceededItems.Count(), Is.EqualTo(3));
// Unpublish only english culture
var unpublishAttempt = await ContentPublishingService.UnpublishAsync(childNode.Key, new HashSet<string> { _englishIsoCode }, Constants.Security.SuperUserKey);
Assert.IsTrue(unpublishAttempt.Success);
var publishedGrandChild = await PublishedContentCache.GetByIdAsync(grandChildNode.Key, false);
CacheTestsHelper.AssertPage(grandChildNode, publishedGrandChild, false);
Assert.IsTrue(publishedGrandChild!.IsPublished(_danishIsoCode));
}
[Test]
public async Task SingleCulturePublished()
{
var publishAttempt = await ContentPublishingService.PublishAsync(
rootContent.Key,
new List<CulturePublishScheduleModel>
{
new() { Culture = _danishIsoCode },
new() { Culture = _englishIsoCode },
},
Constants.Security.SuperUserKey);
Assert.IsTrue(publishAttempt.Success);
// Publish only single culture.
var publishChildAttempt = await ContentPublishingService.PublishAsync(
childNode.Key,
new List<CulturePublishScheduleModel>
{
new() { Culture = _danishIsoCode },
},
Constants.Security.SuperUserKey);
Assert.IsTrue(publishChildAttempt.Success);
var publishGrandChildAttempt = await ContentPublishingService.PublishAsync(
grandChildNode.Key,
new List<CulturePublishScheduleModel>
{
new() { Culture = _danishIsoCode },
},
Constants.Security.SuperUserKey);
Assert.IsTrue(publishGrandChildAttempt.Success);
var publishedGrandChild = await PublishedContentCache.GetByIdAsync(grandChildNode.Key, false);
CacheTestsHelper.AssertPage(grandChildNode, publishedGrandChild, false);
Assert.IsTrue(publishedGrandChild!.IsPublished(_danishIsoCode));
Assert.IsFalse(publishedGrandChild.IsPublished(_englishIsoCode));
}
private async Task CreateTestData()
{
var language = new LanguageBuilder()
.WithCultureInfo(_danishIsoCode)
.Build();
await LanguageService.CreateAsync(language, Constants.Security.SuperUserKey);
var contentTypeCreateModel = ContentTypeEditingBuilder.CreateContentTypeWithTwoPropertiesOneVariantAndOneInvariant(
"cultureVariationTest", "Culture Variation Test", _variantTitleAlias, _variantTitleName,
_invariantTitleAlias, _invariantTitleName);
contentTypeCreateModel.AllowedAsRoot = true;
var contentTypeAttempt = await ContentTypeEditingService.CreateAsync(contentTypeCreateModel, Constants.Security.SuperUserKey);
if (contentTypeAttempt.Success is false)
{
throw new Exception("Failed to create content type");
}
var contentType = contentTypeAttempt.Result!;
var updateModel = ContentTypeUpdateHelper.CreateContentTypeUpdateModel(contentType);
updateModel.AllowedContentTypes = [new ContentTypeSort { Alias = contentType.Alias, Key = contentType.Key, SortOrder = 0 }];
var updateAttempt = await ContentTypeEditingService.UpdateAsync(contentType, updateModel, Constants.Security.SuperUserKey);
if (updateAttempt.Success is false)
{
throw new Exception("Failed to update content type");
}
var contentCreateModel = ContentEditingBuilder.CreateContentWithTwoVariantProperties(
contentTypeAttempt.Result.Key,
_danishIsoCode,
_englishIsoCode,
_variantTitleAlias,
_variantTitleName);
var rootResult = await ContentEditingService.CreateAsync(contentCreateModel, Constants.Security.SuperUserKey);
Assert.IsTrue(rootResult.Success);
rootContent = rootResult.Result.Content!;
contentCreateModel.ParentKey = rootContent.Key;
contentCreateModel.Key = Guid.NewGuid();
var childResult = await ContentEditingService.CreateAsync(contentCreateModel, Constants.Security.SuperUserKey);
Assert.IsTrue(childResult.Success);
childNode = childResult.Result.Content!;
contentCreateModel.ParentKey = childNode.Key;
contentCreateModel.Key = Guid.NewGuid();
var grandChildResult = await ContentEditingService.CreateAsync(contentCreateModel, Constants.Security.SuperUserKey);
Assert.IsTrue(grandChildResult.Success);
grandChildNode = grandChildResult.Result.Content!;
}
}

View File

@@ -90,6 +90,9 @@ public class DocumentHybridCacheMockTests : UmbracoIntegrationTestWithContent
_mockedNucacheRepository.Setup(r => r.DeleteContentItemAsync(It.IsAny<int>()));
var mockedPublishedStatusService = new Mock<IPublishStatusQueryService>();
mockedPublishedStatusService.Setup(x => x.IsDocumentPublishedInAnyCulture(It.IsAny<Guid>())).Returns(true);
_mockDocumentCacheService = new DocumentCacheService(
_mockedNucacheRepository.Object,
GetRequiredService<IIdKeyMap>(),
@@ -97,10 +100,12 @@ public class DocumentHybridCacheMockTests : UmbracoIntegrationTestWithContent
GetRequiredService<Microsoft.Extensions.Caching.Hybrid.HybridCache>(),
GetRequiredService<IPublishedContentFactory>(),
GetRequiredService<ICacheNodeFactory>(),
GetSeedProviders(),
GetSeedProviders(mockedPublishedStatusService.Object),
new OptionsWrapper<CacheSettings>(new CacheSettings()),
GetRequiredService<IPublishedModelFactory>(),
GetRequiredService<IPreviewService>());
GetRequiredService<IPreviewService>(),
mockedPublishedStatusService.Object,
GetRequiredService<IDocumentNavigationQueryService>());
_mockedCache = new DocumentCache(_mockDocumentCacheService,
GetRequiredService<IPublishedContentTypeCache>(),
@@ -111,7 +116,7 @@ public class DocumentHybridCacheMockTests : UmbracoIntegrationTestWithContent
// We want to be able to alter the settings for the providers AFTER the test has started
// So we'll manually create them with a magic options mock.
private IEnumerable<IDocumentSeedKeyProvider> GetSeedProviders()
private IEnumerable<IDocumentSeedKeyProvider> GetSeedProviders(IPublishStatusQueryService publishStatusQueryService)
{
_cacheSettings = new CacheSettings();
_cacheSettings.DocumentBreadthFirstSeedCount = 0;
@@ -121,8 +126,8 @@ public class DocumentHybridCacheMockTests : UmbracoIntegrationTestWithContent
return new List<IDocumentSeedKeyProvider>
{
new ContentTypeSeedKeyProvider(GetRequiredService<ICoreScopeProvider>(), GetRequiredService<IDatabaseCacheRepository>(), mock.Object),
new DocumentBreadthFirstKeyProvider(GetRequiredService<IDocumentNavigationQueryService>(), mock.Object),
new ContentTypeSeedKeyProvider(GetRequiredService<ICoreScopeProvider>(), GetRequiredService<IDatabaseCacheRepository>(), mock.Object, publishStatusQueryService),
new DocumentBreadthFirstKeyProvider(GetRequiredService<IDocumentNavigationQueryService>(), mock.Object, publishStatusQueryService),
};
}

View File

@@ -1,10 +1,15 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models.ContentPublishing;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
namespace Umbraco.Cms.Tests.Integration.Umbraco.PublishedCache.HybridCache;
@@ -12,7 +17,11 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.PublishedCache.HybridCache;
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class DocumentHybridCacheScopeTests : UmbracoIntegrationTestWithContentEditing
{
protected override void CustomTestSetup(IUmbracoBuilder builder) => builder.AddUmbracoHybridCache();
protected override void CustomTestSetup(IUmbracoBuilder builder)
{
builder.AddNotificationHandler<ContentTreeChangeNotification, ContentTreeChangeDistributedCacheNotificationHandler>();
builder.Services.AddUnique<IServerMessenger, ContentEventsTests.LocalServerMessenger>();
}
private IPublishedContentCache PublishedContentHybridCache => GetRequiredService<IPublishedContentCache>();
@@ -81,4 +90,26 @@ public class DocumentHybridCacheScopeTests : UmbracoIntegrationTestWithContentEd
// Published page should not be in cache, as we rolled scope back.
Assert.IsNotNull(publishedPage);
}
[Test]
public async Task Can_Save_And_Publish_In_Same_Scope()
{
var key = Guid.NewGuid();
using (var scope = CoreScopeProvider.CreateCoreScope())
{
Textpage.Key = key;
var result = await ContentEditingService.CreateAsync(Textpage, Constants.Security.SuperUserKey);
Assert.IsTrue(result);
var publishResult = await ContentPublishingService.PublishAsync(Textpage.Key.Value, new List<CulturePublishScheduleModel>
{
new() { Culture = "*" },
}, Constants.Security.SuperUserKey);
Assert.IsTrue(publishResult.Success);
scope.Complete();
}
var published = await PublishedContentHybridCache.GetByIdAsync(key);
Assert.IsNotNull(published);
}
}