2025-03-21 18:02:31 +01:00
|
|
|
using NUnit.Framework;
|
2024-09-10 00:49:18 +09:00
|
|
|
using Umbraco.Cms.Core;
|
2024-10-28 15:31:39 +01:00
|
|
|
using Umbraco.Cms.Core.Cache;
|
2024-09-10 00:49:18 +09:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
2024-10-28 15:31:39 +01:00
|
|
|
using Umbraco.Cms.Core.Notifications;
|
2024-09-10 00:49:18 +09:00
|
|
|
using Umbraco.Cms.Core.PublishedCache;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
2024-09-24 09:39:23 +02:00
|
|
|
using Umbraco.Cms.Core.Services.ContentTypeEditing;
|
2024-10-28 15:31:39 +01:00
|
|
|
using Umbraco.Cms.Core.Sync;
|
2024-09-10 00:49:18 +09:00
|
|
|
using Umbraco.Cms.Core.Web;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Builders;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Builders.Extensions;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
2024-10-28 15:31:39 +01:00
|
|
|
using Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
|
2024-09-10 00:49:18 +09:00
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.PublishedCache.HybridCache;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
2025-03-21 18:02:31 +01:00
|
|
|
internal sealed class DocumentHybridCacheVariantsTests : UmbracoIntegrationTest
|
2024-09-10 00:49:18 +09:00
|
|
|
{
|
|
|
|
|
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 ILanguageService LanguageService => GetRequiredService<ILanguageService>();
|
|
|
|
|
|
|
|
|
|
private IContentEditingService ContentEditingService => GetRequiredService<IContentEditingService>();
|
|
|
|
|
|
2024-09-24 09:39:23 +02:00
|
|
|
private IContentTypeEditingService ContentTypeEditingService => GetRequiredService<IContentTypeEditingService>();
|
|
|
|
|
|
2024-09-10 00:49:18 +09:00
|
|
|
private IUmbracoContextFactory UmbracoContextFactory => GetRequiredService<IUmbracoContextFactory>();
|
|
|
|
|
|
|
|
|
|
private IPublishedContentCache PublishedContentHybridCache => GetRequiredService<IPublishedContentCache>();
|
|
|
|
|
|
|
|
|
|
private IContent VariantPage { get; set; }
|
|
|
|
|
|
2024-10-28 15:31:39 +01:00
|
|
|
protected override void CustomTestSetup(IUmbracoBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
builder.AddNotificationHandler<ContentTreeChangeNotification, ContentTreeChangeDistributedCacheNotificationHandler>();
|
|
|
|
|
builder.Services.AddUnique<IServerMessenger, ContentEventsTests.LocalServerMessenger>();
|
|
|
|
|
}
|
2024-09-10 00:49:18 +09:00
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public async Task Setup() => await CreateTestData();
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Can_Set_Invariant_Title()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
await PublishedContentHybridCache.GetByIdAsync(VariantPage.Id, true);
|
|
|
|
|
var updatedInvariantTitle = "Updated Invariant Title";
|
|
|
|
|
var updatedVariantTitle = "Updated Variant Title";
|
|
|
|
|
|
|
|
|
|
var updateModel = new ContentUpdateModel
|
|
|
|
|
{
|
2025-04-23 14:54:51 +02:00
|
|
|
Properties = [
|
|
|
|
|
new PropertyValueModel { Alias = _invariantTitleAlias, Value = updatedInvariantTitle },
|
|
|
|
|
new PropertyValueModel { Alias = _variantTitleAlias, Value = updatedVariantTitle, Culture = _englishIsoCode },
|
|
|
|
|
new PropertyValueModel { Alias = _variantTitleAlias, Value = updatedVariantTitle, Culture = _danishIsoCode }
|
|
|
|
|
],
|
|
|
|
|
Variants =
|
|
|
|
|
[
|
|
|
|
|
new VariantModel { Culture = _englishIsoCode, Name = "Updated English Name" },
|
|
|
|
|
new VariantModel { Culture = _danishIsoCode, Name = "Updated Danish Name" }
|
|
|
|
|
],
|
2024-09-10 00:49:18 +09:00
|
|
|
};
|
|
|
|
|
|
2024-10-02 13:36:26 +02:00
|
|
|
var result =
|
|
|
|
|
await ContentEditingService.UpdateAsync(VariantPage.Key, updateModel, Constants.Security.SuperUserKey);
|
2024-09-10 00:49:18 +09:00
|
|
|
Assert.IsTrue(result.Success);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var textPage = await PublishedContentHybridCache.GetByIdAsync(VariantPage.Id, true);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
using var contextReference = UmbracoContextFactory.EnsureUmbracoContext();
|
2024-10-02 14:32:31 +02:00
|
|
|
Assert.AreEqual(updatedInvariantTitle, textPage.Value(_invariantTitleAlias, string.Empty, string.Empty));
|
2024-09-10 00:49:18 +09:00
|
|
|
Assert.AreEqual(updatedVariantTitle, textPage.Value(_variantTitleAlias, _englishIsoCode));
|
|
|
|
|
Assert.AreEqual(updatedVariantTitle, textPage.Value(_variantTitleAlias, _danishIsoCode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Can_Set_Invariant_Title_On_One_Culture()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
await PublishedContentHybridCache.GetByIdAsync(VariantPage.Id, true);
|
|
|
|
|
var updatedInvariantTitle = "Updated Invariant Title";
|
|
|
|
|
var updatedVariantTitle = "Updated Invariant Title";
|
|
|
|
|
|
|
|
|
|
var updateModel = new ContentUpdateModel
|
|
|
|
|
{
|
2025-04-23 14:54:51 +02:00
|
|
|
Properties =
|
|
|
|
|
[
|
|
|
|
|
new PropertyValueModel { Alias = _invariantTitleAlias, Value = updatedInvariantTitle },
|
|
|
|
|
new PropertyValueModel { Alias = _variantTitleAlias, Value = updatedVariantTitle, Culture = _englishIsoCode }
|
|
|
|
|
],
|
|
|
|
|
Variants =
|
|
|
|
|
[
|
|
|
|
|
new VariantModel { Culture = _englishIsoCode, Name = "Updated English Name" }
|
|
|
|
|
],
|
2024-09-10 00:49:18 +09:00
|
|
|
};
|
|
|
|
|
|
2024-09-24 09:39:23 +02:00
|
|
|
var result =
|
|
|
|
|
await ContentEditingService.UpdateAsync(VariantPage.Key, updateModel, Constants.Security.SuperUserKey);
|
2024-09-10 00:49:18 +09:00
|
|
|
Assert.IsTrue(result.Success);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var textPage = await PublishedContentHybridCache.GetByIdAsync(VariantPage.Id, true);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
using var contextReference = UmbracoContextFactory.EnsureUmbracoContext();
|
2024-10-02 14:32:31 +02:00
|
|
|
Assert.AreEqual(updatedInvariantTitle, textPage.Value(_invariantTitleAlias, string.Empty, string.Empty));
|
2024-09-10 00:49:18 +09:00
|
|
|
Assert.AreEqual(updatedVariantTitle, textPage.Value(_variantTitleAlias, _englishIsoCode));
|
|
|
|
|
Assert.AreEqual(_variantTitleName, textPage.Value(_variantTitleAlias, _danishIsoCode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CreateTestData()
|
|
|
|
|
{
|
|
|
|
|
var language = new LanguageBuilder()
|
|
|
|
|
.WithCultureInfo(_danishIsoCode)
|
|
|
|
|
.Build();
|
|
|
|
|
await LanguageService.CreateAsync(language, Constants.Security.SuperUserKey);
|
|
|
|
|
|
2024-10-02 13:36:26 +02:00
|
|
|
var contentType = ContentTypeEditingBuilder.CreateContentTypeWithTwoPropertiesOneVariantAndOneInvariant(
|
|
|
|
|
"cultureVariationTest", "Culture Variation Test", _variantTitleAlias, _variantTitleName,
|
|
|
|
|
_invariantTitleAlias, _invariantTitleName);
|
2024-09-24 09:39:23 +02:00
|
|
|
var contentTypeAttempt = await ContentTypeEditingService.CreateAsync(contentType, Constants.Security.SuperUserKey);
|
|
|
|
|
if (!contentTypeAttempt.Success)
|
2024-09-10 00:49:18 +09:00
|
|
|
{
|
2024-09-24 09:39:23 +02:00
|
|
|
throw new Exception("Failed to create content type");
|
|
|
|
|
}
|
2024-09-10 00:49:18 +09:00
|
|
|
|
2024-10-02 13:36:26 +02:00
|
|
|
var rootContentCreateModel =
|
|
|
|
|
ContentEditingBuilder.CreateContentWithTwoVariantProperties(contentTypeAttempt.Result.Key, "en-US", "da-DK",
|
|
|
|
|
_variantTitleAlias, _variantTitleName);
|
2024-09-10 00:49:18 +09:00
|
|
|
var result = await ContentEditingService.CreateAsync(rootContentCreateModel, Constants.Security.SuperUserKey);
|
|
|
|
|
VariantPage = result.Result.Content;
|
|
|
|
|
}
|
|
|
|
|
}
|