From 0e9067d2c8fe4a80e9fdfd14c0faf823dba5e04d Mon Sep 17 00:00:00 2001 From: Andreas Zerbst <73799582+andr317c@users.noreply.github.com> Date: Thu, 22 Aug 2024 14:03:35 +0200 Subject: [PATCH] V15 updated the hybrid caching integration tests to use ContentEditingService (#16947) * Added builder extension withParentKey * Created builder with ContentEditingService * Added usage of the ContentEditingService to SETUP * Started using ContentEditingService builder in tests * Updated builder extensions * Fixed builder * Clean up * Clean up, not done * Added Ids --- .../Builders/ContentEditingBuilder.cs | 172 +++++++++ .../ContentEditingBuilderExtensions.cs | 58 ++++ .../IWithContentTypeKeyBuilder.cs | 6 + .../IWithInvariantNameBuilder.cs | 6 + .../IWithInvariantPropertiesBuilder.cs | 8 + .../IWithParentKeyBuilder.cs | 9 + .../IWithTemplateKeyBuilder.cs | 6 + .../IWithVariantsBuilder.cs | 8 + ...mbracoIntegrationTestWithContentEditing.cs | 133 +++++++ .../Services/ContentHybridCacheTests.cs | 327 ++++++++++++------ 10 files changed, 619 insertions(+), 114 deletions(-) create mode 100644 tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs create mode 100644 tests/Umbraco.Tests.Common/Builders/Extensions/ContentEditingBuilderExtensions.cs create mode 100644 tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithContentTypeKeyBuilder.cs create mode 100644 tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithInvariantNameBuilder.cs create mode 100644 tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithInvariantPropertiesBuilder.cs create mode 100644 tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithParentKeyBuilder.cs create mode 100644 tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithTemplateKeyBuilder.cs create mode 100644 tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithVariantsBuilder.cs create mode 100644 tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContentEditing.cs diff --git a/tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs b/tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs new file mode 100644 index 000000000000..92f65bbc39ea --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs @@ -0,0 +1,172 @@ +// Copyright (c) Umbraco. +// See LICENSE for more details. + +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Tests.Common.Builders.Extensions; +using Umbraco.Cms.Tests.Common.Builders.Interfaces; +using Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel; + +namespace Umbraco.Cms.Tests.Common.Builders; + +public class ContentEditingBuilder + : BuilderBase, + IWithInvariantNameBuilder, + IWithInvariantPropertiesBuilder, + IWithVariantsBuilder, + IWithKeyBuilder, + IWithContentTypeKeyBuilder, + IWithParentKeyBuilder, + IWithTemplateKeyBuilder +{ + private IContentType _contentType; + private ContentTypeBuilder _contentTypeBuilder; + private IEnumerable _invariantProperties = []; + private IEnumerable _variants = []; + private Guid _contentTypeKey; + private Guid? _parentKey; + private Guid? _templateKey; + private Guid? _key; + private string _invariantName; + + Guid? IWithKeyBuilder.Key + { + get => _key; + set => _key = value; + } + + string IWithInvariantNameBuilder.InvariantName + { + get => _invariantName; + set => _invariantName = value; + } + + IEnumerable IWithInvariantPropertiesBuilder.InvariantProperties + { + get => _invariantProperties; + set => _invariantProperties = value; + } + + IEnumerable IWithVariantsBuilder.Variants + { + get => _variants; + set => _variants = value; + } + + Guid? IWithParentKeyBuilder.ParentKey + { + get => _parentKey; + set => _parentKey = value; + } + + Guid IWithContentTypeKeyBuilder.ContentTypeKey + { + get => _contentTypeKey; + set => _contentTypeKey = value; + } + + Guid? IWithTemplateKeyBuilder.TemplateKey + { + get => _templateKey; + set => _templateKey = value; + } + + public ContentEditingBuilder WithInvariantName(string invariantName) + { + _invariantName = invariantName; + return this; + } + + public ContentEditingBuilder WithInvariantProperty(string alias, object value) + { + var property = new PropertyValueModel { Alias = alias, Value = value }; + _invariantProperties = _invariantProperties.Concat(new[] { property }); + return this; + } + + public ContentEditingBuilder AddVariant(string culture, string segment, string name, + IEnumerable properties) + { + var variant = new VariantModel { Culture = culture, Segment = segment, Name = name, Properties = properties }; + _variants = _variants.Concat(new[] { variant }); + return this; + } + + public ContentEditingBuilder WithParentKey(Guid parentKey) + { + _parentKey = parentKey; + return this; + } + + public ContentEditingBuilder WithTemplateKey(Guid templateKey) + { + _templateKey = templateKey; + return this; + } + + public ContentEditingBuilder WithContentType(IContentType contentType) + { + _contentTypeBuilder = null; + _contentType = contentType; + return this; + } + + public override ContentCreateModel Build() + { + var key = _key ?? Guid.NewGuid(); + var parentKey = _parentKey; + var templateKey = _templateKey; + var invariantName = _invariantName ?? Guid.NewGuid().ToString(); + var invariantProperties = _invariantProperties; + var variants = _variants; + + if (_contentTypeBuilder is null && _contentType is null) + { + throw new InvalidOperationException( + "A content item cannot be constructed without providing a content type. Use AddContentType() or WithContentType()."); + } + + var contentType = _contentType ?? _contentTypeBuilder.Build(); + var content = new ContentCreateModel(); + + content.InvariantName = invariantName; + if (parentKey is not null) + { + content.ParentKey = parentKey; + } + + if (templateKey is not null) + { + content.TemplateKey = templateKey; + } + + content.ContentTypeKey = contentType.Key; + content.Key = key; + content.InvariantProperties = invariantProperties; + content.Variants = variants; + + return content; + } + + public static ContentCreateModel CreateBasicContent(IContentType contentType, Guid? key) => + new ContentEditingBuilder() + .WithKey(key) + .WithContentType(contentType) + .WithInvariantName("Home") + .Build(); + + public static ContentCreateModel CreateSimpleContent(IContentType contentType) => + new ContentEditingBuilder() + .WithContentType(contentType) + .WithInvariantName("Home") + .WithInvariantProperty("title", "Welcome to our Home page") + .Build(); + + public static ContentCreateModel CreateSimpleContent(IContentType contentType, string name, Guid? parentKey) => + new ContentEditingBuilder() + .WithContentType(contentType) + .WithInvariantName(name) + .WithParentKey(parentKey) + .WithInvariantProperty("title", "Welcome to our Home page") + .Build(); +} diff --git a/tests/Umbraco.Tests.Common/Builders/Extensions/ContentEditingBuilderExtensions.cs b/tests/Umbraco.Tests.Common/Builders/Extensions/ContentEditingBuilderExtensions.cs new file mode 100644 index 000000000000..a02c4e5b126a --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Extensions/ContentEditingBuilderExtensions.cs @@ -0,0 +1,58 @@ +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Tests.Common.Builders.Interfaces; +using Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel; + +namespace Umbraco.Cms.Tests.Common.Builders.Extensions; + +public static class ContentEditingBuilderExtensions +{ + public static T WithInvariantName(this T Builder, string invariantName) + where T : IWithInvariantNameBuilder + { + Builder.InvariantName = invariantName; + return Builder; + } + + public static T WithInvariantProperties(this T Builder, IEnumerable invariantProperties) + where T : IWithInvariantPropertiesBuilder + { + Builder.InvariantProperties = invariantProperties; + return Builder; + } + + public static T WithVariants(this T Builder, IEnumerable variants) + where T : IWithVariantsBuilder + { + Builder.Variants = variants; + return Builder; + } + + public static T WithKey(this T Builder, Guid? key) + where T : IWithKeyBuilder + { + Builder.Key = key; + return Builder; + } + + public static T WithContentTypeKey(this T Builder, Guid contentTypeKey) + where T : IWithContentTypeKeyBuilder + { + Builder.ContentTypeKey = contentTypeKey; + return Builder; + } + + public static T WithParentKey(this T Builder, Guid? parentKey) + where T : IWithParentKeyBuilder + { + Builder.ParentKey = parentKey; + return Builder; + } + + + public static T WithTemplateKey(this T Builder, Guid? templateKey) + where T : IWithTemplateKeyBuilder + { + Builder.TemplateKey = templateKey; + return Builder; + } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithContentTypeKeyBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithContentTypeKeyBuilder.cs new file mode 100644 index 000000000000..cdb743ca67ac --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithContentTypeKeyBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel; + +public interface IWithContentTypeKeyBuilder +{ + public Guid ContentTypeKey { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithInvariantNameBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithInvariantNameBuilder.cs new file mode 100644 index 000000000000..27698b13951a --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithInvariantNameBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel; + +public interface IWithInvariantNameBuilder +{ + public string? InvariantName { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithInvariantPropertiesBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithInvariantPropertiesBuilder.cs new file mode 100644 index 000000000000..7320ac6523e2 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithInvariantPropertiesBuilder.cs @@ -0,0 +1,8 @@ +using Umbraco.Cms.Core.Models.ContentEditing; + +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel; + +public interface IWithInvariantPropertiesBuilder +{ + public IEnumerable InvariantProperties { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithParentKeyBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithParentKeyBuilder.cs new file mode 100644 index 000000000000..e4fa282a7ce6 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithParentKeyBuilder.cs @@ -0,0 +1,9 @@ +// Copyright (c) Umbraco. +// See LICENSE for more details. + +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithParentKeyBuilder +{ + Guid? ParentKey { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithTemplateKeyBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithTemplateKeyBuilder.cs new file mode 100644 index 000000000000..0b05a70548dd --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithTemplateKeyBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel; + +public interface IWithTemplateKeyBuilder +{ + public Guid? TemplateKey { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithVariantsBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithVariantsBuilder.cs new file mode 100644 index 000000000000..ca4e54f1c480 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/ContentCreateModel/IWithVariantsBuilder.cs @@ -0,0 +1,8 @@ +using Umbraco.Cms.Core.Models.ContentEditing; + +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel; + +public interface IWithVariantsBuilder +{ + public IEnumerable Variants { get; set; } +} diff --git a/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContentEditing.cs b/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContentEditing.cs new file mode 100644 index 000000000000..e8a8c0690511 --- /dev/null +++ b/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContentEditing.cs @@ -0,0 +1,133 @@ +// Copyright (c) Umbraco. +// See LICENSE for more details. + +using NUnit.Framework; +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Core.Models.ContentPublishing; +using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Tests.Common.Builders; + +namespace Umbraco.Cms.Tests.Integration.Testing; + +public abstract class UmbracoIntegrationTestWithContentEditing : UmbracoIntegrationTest +{ + private IContentTypeService ContentTypeService => GetRequiredService(); + + protected ITemplateService TemplateService => GetRequiredService(); + + private ContentEditingService ContentEditingService => + (ContentEditingService)GetRequiredService(); + + private ContentPublishingService ContentPublishingService => + (ContentPublishingService)GetRequiredService(); + + + protected ContentCreateModel Subpage2 { get; private set; } + protected ContentCreateModel Subpage3 { get; private set; } + + protected ContentCreateModel Subpage { get; private set; } + + protected ContentCreateModel Textpage { get; private set; } + + protected ContentScheduleCollection contentSchedule { get; private set; } + + protected CultureAndScheduleModel cultureAndSchedule { get; private set; } + + protected int TextpageId { get; private set; } + + protected int SubpageId { get; private set; } + + protected int Subpage2Id { get; private set; } + + protected int Subpage3Id { get; private set; } + + protected ContentType ContentType { get; private set; } + + [SetUp] + public new void Setup() => CreateTestData(); + + protected async void CreateTestData() + { + // NOTE Maybe not the best way to create/save test data as we are using the services, which are being tested. + var template = TemplateBuilder.CreateTextPageTemplate("defaultTemplate"); + await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey); + + // Create and Save ContentType "umbTextpage" -> 1051 (template), 1052 (content type) + ContentType = + ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage", defaultTemplateId: template.Id); + ContentType.Key = new Guid("1D3A8E6E-2EA9-4CC1-B229-1AEE19821522"); + ContentType.AllowedAsRoot = true; + ContentType.AllowedContentTypes = new[] { new ContentTypeSort(ContentType.Key, 0, ContentType.Alias) }; + var contentTypeResult = await ContentTypeService.CreateAsync(ContentType, Constants.Security.SuperUserKey); + Assert.IsTrue(contentTypeResult.Success); + + // Create and Save Content "Homepage" based on "umbTextpage" -> 1053 + Textpage = ContentEditingBuilder.CreateSimpleContent(ContentType); + Textpage.Key = new Guid("B58B3AD4-62C2-4E27-B1BE-837BD7C533E0"); + var createContentResultTextPage = await ContentEditingService.CreateAsync(Textpage, Constants.Security.SuperUserKey); + Assert.IsTrue(createContentResultTextPage.Success); + + if (!Textpage.Key.HasValue) + { + throw new InvalidOperationException("The content page key is null."); + } + + if (createContentResultTextPage.Result.Content != null) + { + TextpageId = createContentResultTextPage.Result.Content.Id; + } + + // Sets the culture and schedule for the content, in this case, we are publishing immediately for all cultures + contentSchedule = new ContentScheduleCollection(); + cultureAndSchedule = new CultureAndScheduleModel + { + CulturesToPublishImmediately = new HashSet { "*" }, Schedules = contentSchedule, + }; + + // Create and Save Content "Text Page 1" based on "umbTextpage" -> 1054 + Subpage = ContentEditingBuilder.CreateSimpleContent(ContentType, "Text Page 1", Textpage.Key); + var createContentResultSubPage = await ContentEditingService.CreateAsync(Subpage, Constants.Security.SuperUserKey); + Assert.IsTrue(createContentResultSubPage.Success); + + if (!Subpage.Key.HasValue) + { + throw new InvalidOperationException("The content page key is null."); + } + + if (createContentResultSubPage.Result.Content != null) + { + SubpageId = createContentResultSubPage.Result.Content.Id; + } + + await ContentPublishingService.PublishAsync(Subpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + + // Create and Save Content "Text Page 1" based on "umbTextpage" -> 1055 + Subpage2 = ContentEditingBuilder.CreateSimpleContent(ContentType, "Text Page 2", Textpage.Key); + var createContentResultSubPage2 = await ContentEditingService.CreateAsync(Subpage2, Constants.Security.SuperUserKey); + Assert.IsTrue(createContentResultSubPage2.Success); + if (!Subpage2.Key.HasValue) + { + throw new InvalidOperationException("The content page key is null."); + } + + if (createContentResultSubPage2.Result.Content != null) + { + Subpage2Id = createContentResultSubPage2.Result.Content.Id; + } + + Subpage3 = ContentEditingBuilder.CreateSimpleContent(ContentType, "Text Page 3", Textpage.Key); + var createContentResultSubPage3 = await ContentEditingService.CreateAsync(Subpage3, Constants.Security.SuperUserKey); + Assert.IsTrue(createContentResultSubPage3.Success); + if (!Subpage3.Key.HasValue) + { + throw new InvalidOperationException("The content page key is null."); + } + + if (createContentResultSubPage3.Result.Content != null) + { + Subpage3Id = createContentResultSubPage3.Result.Content.Id; + } + } +} diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentHybridCacheTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentHybridCacheTests.cs index 75cb29b63e47..a5989604c9a9 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentHybridCacheTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentHybridCacheTests.cs @@ -1,10 +1,11 @@ using Microsoft.Extensions.Caching.Hybrid; using NUnit.Framework; -using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Models.ContentEditing; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PublishedCache; +using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Web; -using Umbraco.Cms.Infrastructure.HybridCache.Services; using Umbraco.Cms.Tests.Common.Testing; using Umbraco.Cms.Tests.Integration.Testing; @@ -12,21 +13,29 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services; [TestFixture] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] -public class ContentHybridCacheTests : UmbracoIntegrationTestWithContent +public class ContentHybridCacheTests : UmbracoIntegrationTestWithContentEditing { protected override void CustomTestSetup(IUmbracoBuilder builder) => builder.AddUmbracoHybridCache(); private IPublishedContentCache PublishedContentHybridCache => GetRequiredService(); + private IContentEditingService ContentEditingService => GetRequiredService(); + + private IContentPublishingService ContentPublishingService => GetRequiredService(); + private IUmbracoContextFactory UmbracoContextFactory => GetRequiredService(); + private const string NewName = "New Name"; + private const string NewTitle = "New Title"; + + // Create CRUD Tests for Content, Also cultures. [Test] public async Task Can_Get_Draft_Content_By_Id() { //Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); //Assert AssertTextPage(textPage); @@ -36,7 +45,7 @@ public async Task Can_Get_Draft_Content_By_Id() public async Task Can_Get_Draft_Content_By_Key() { // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); // Assert AssertTextPage(textPage); @@ -46,10 +55,10 @@ public async Task Can_Get_Draft_Content_By_Key() public async Task Can_Get_Published_Content_By_Id() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId); // Assert AssertTextPage(textPage); @@ -59,10 +68,10 @@ public async Task Can_Get_Published_Content_By_Id() public async Task Can_Get_Published_Content_By_Key() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value); // Assert AssertTextPage(textPage); @@ -71,9 +80,8 @@ public async Task Can_Get_Published_Content_By_Key() [Test] public async Task Has_Content_By_Id_Returns_False_If_Not_In_Cache() { - // Arrange // Act - var hasContent = await PublishedContentHybridCache.HasByIdAsync(Textpage.Id); + var hasContent = await PublishedContentHybridCache.HasByIdAsync(TextpageId); // Assert Assert.IsFalse(hasContent); @@ -83,7 +91,7 @@ public async Task Has_Content_By_Id_Returns_False_If_Not_In_Cache() public async Task Has_Content_By_Id_Returns_True_If_In_Cache() { // Act - var hasContent = await PublishedContentHybridCache.HasByIdAsync(Textpage.Id, true); + var hasContent = await PublishedContentHybridCache.HasByIdAsync(TextpageId, true); // Assert Assert.IsTrue(hasContent); @@ -93,15 +101,15 @@ public async Task Has_Content_By_Id_Returns_True_If_In_Cache() public async Task Has_Content_By_Id_Has_Content_After_Load() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); var hybridCache = GetRequiredService(); await hybridCache.RemoveAsync(Textpage.Key.ToString()); - var hasContent = await PublishedContentHybridCache.HasByIdAsync(Textpage.Id); + var hasContent = await PublishedContentHybridCache.HasByIdAsync(TextpageId); Assert.IsFalse(hasContent); - await PublishedContentHybridCache.GetByIdAsync(Textpage.Id); + await PublishedContentHybridCache.GetByIdAsync(TextpageId); // Act - var hasContent2 = await PublishedContentHybridCache.HasByIdAsync(Textpage.Id); + var hasContent2 = await PublishedContentHybridCache.HasByIdAsync(TextpageId); // Assert Assert.IsTrue(hasContent2); @@ -111,10 +119,10 @@ public async Task Has_Content_By_Id_Has_Content_After_Load() public async Task Can_Get_Draft_Of_Published_Content_By_Id() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); // Assert AssertTextPage(textPage); @@ -125,10 +133,10 @@ public async Task Can_Get_Draft_Of_Published_Content_By_Id() public async Task Can_Get_Draft_Of_Published_Content_By_Key() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); // Assert AssertTextPage(textPage); @@ -139,32 +147,42 @@ public async Task Can_Get_Draft_Of_Published_Content_By_Key() public async Task Can_Get_Updated_Draft_Content_By_Id() { // Arrange - await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); - string newName = "New Name"; - Textpage.Name = newName; - ContentService.Save(Textpage, -1); + Textpage.InvariantName = NewName; + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = NewName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var updatedPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); + var updatedPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); // Assert - Assert.AreEqual(newName, updatedPage.Name); + Assert.AreEqual(NewName, updatedPage.Name); } [Test] public async Task Can_Get_Updated_Draft_Content_By_Key() { // Arrange - await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); - string newName = "New Name"; - Textpage.Name = newName; - ContentService.Save(Textpage, -1); + Textpage.InvariantName = NewName; + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = NewName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var updatedPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var updatedPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); // Assert - Assert.AreEqual(newName, updatedPage.Name); + Assert.AreEqual(NewName, updatedPage.Name); } [Test] @@ -174,16 +192,22 @@ public async Task Can_Get_Updated_Draft_Content_By_Key() public async Task Can_Get_Updated_Draft_Published_Content_By_Id(bool preview, bool result) { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - string newName = "New Name"; - Textpage.Name = newName; - ContentService.Save(Textpage, -1); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + Textpage.InvariantName = NewName; + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = NewName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, preview); // Assert - Assert.AreEqual(result, newName.Equals(textPage.Name)); + Assert.AreEqual(result, NewName.Equals(textPage.Name)); } [Test] @@ -193,27 +217,35 @@ public async Task Can_Get_Updated_Draft_Published_Content_By_Id(bool preview, bo public async Task Can_Get_Updated_Draft_Published_Content_By_Key(bool preview, bool result) { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - string newName = "New Name"; - Textpage.Name = newName; - ContentService.Save(Textpage, -1); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + + Textpage.InvariantName = NewName; + + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = NewName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, preview); // Assert - Assert.AreEqual(result, newName.Equals(textPage.Name)); + Assert.AreEqual(result, NewName.Equals(textPage.Name)); } [Test] public async Task Can_Get_Draft_Content_Property_By_Id() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - var titleValue = Textpage.GetValue("title"); + var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); @@ -224,11 +256,10 @@ public async Task Can_Get_Draft_Content_Property_By_Id() public async Task Can_Get_Draft_Content_Property_By_Key() { // Arrange - ContentService.Save(Textpage, -1); - var titleValue = Textpage.GetValue("title"); + var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); @@ -239,11 +270,11 @@ public async Task Can_Get_Draft_Content_Property_By_Key() public async Task Can_Get_Published_Content_Property_By_Id() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - var titleValue = Textpage.GetValue("title"); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); @@ -254,11 +285,11 @@ public async Task Can_Get_Published_Content_Property_By_Id() public async Task Can_Get_Published_Content_Property_By_Key() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - var titleValue = Textpage.GetValue("title"); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); @@ -269,11 +300,11 @@ public async Task Can_Get_Published_Content_Property_By_Key() public async Task Can_Get_Draft_Of_Published_Content_Property_By_Id() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - var titleValue = Textpage.GetValue("title"); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); @@ -284,11 +315,11 @@ public async Task Can_Get_Draft_Of_Published_Content_Property_By_Id() public async Task Can_Get_Draft_Of_Published_Content_Property_By_Key() { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - var titleValue = Textpage.GetValue("title"); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); @@ -299,63 +330,121 @@ public async Task Can_Get_Draft_Of_Published_Content_Property_By_Key() public async Task Can_Get_Updated_Draft_Content_Property_By_Id() { // Arrange - string newTitle = "New Name"; - Textpage.SetValue("title", newTitle); - ContentService.Save(Textpage, -1); + Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; + + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = Textpage.InvariantName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); - Assert.AreEqual(newTitle, textPage.Value("title")); + Assert.AreEqual(NewTitle, textPage.Value("title")); } [Test] public async Task Can_Get_Updated_Draft_Content_Property_By_Key() { // Arrange - string newTitle = "New Name"; - Textpage.SetValue("title", newTitle); - ContentService.Save(Textpage, -1); + Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; + + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = Textpage.InvariantName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); + + // Act + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); + + // Assert + using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); + Assert.AreEqual(NewTitle, textPage.Value("title")); + } + + [Test] + public async Task Can_Get_Updated_Published_Content_Property_By_Id() + { + // Arrange + Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; + + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = Textpage.InvariantName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); - Assert.AreEqual(newTitle, textPage.Value("title")); + Assert.AreEqual(NewTitle, textPage.Value("title")); } [Test] public async Task Can_Get_Updated_Published_Content_Property_By_Key() { // Arrange - string newTitle = "New Name"; - Textpage.SetValue("title", newTitle); - ContentService.Save(Textpage, -1); - ContentService.Publish(Textpage, Array.Empty()); + Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; + + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = Textpage.InvariantName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); - Assert.AreEqual(newTitle, textPage.Value("title")); + Assert.AreEqual(NewTitle, textPage.Value("title")); } [Test] - [TestCase(true, "New Name")] + [TestCase(true, "New Title")] [TestCase(false, "Welcome to our Home page")] public async Task Can_Get_Updated_Draft_Of_Published_Content_Property_By_Id(bool preview, string titleName) { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - Textpage.SetValue("title", "New Name"); - ContentService.Save(Textpage, -1); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; + + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = Textpage.InvariantName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, preview); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); @@ -363,33 +452,42 @@ public async Task Can_Get_Updated_Draft_Of_Published_Content_Property_By_Id(bool } [Test] - public async Task Can_Get_Updated_Draft_Of_Published_Content_Property_By_Key() + [TestCase(true, "New Name")] + [TestCase(false, "Welcome to our Home page")] + public async Task Can_Get_Updated_Draft_Of_Published_Content_Property_By_Key(bool preview, string titleName) { // Arrange - string newTitle = "New Name"; - ContentService.Publish(Textpage, Array.Empty()); - Textpage.SetValue("title", newTitle); - ContentService.Save(Textpage, -1); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + Textpage.InvariantProperties.First(x => x.Alias == "title").Value = titleName; + + ContentUpdateModel updateModel = new ContentUpdateModel + { + InvariantName = Textpage.InvariantName, + InvariantProperties = Textpage.InvariantProperties, + Variants = Textpage.Variants, + TemplateKey = Textpage.TemplateKey, + }; + + await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); // Assert using var contextReference = UmbracoContextFactory.EnsureUmbracoContext(); - Assert.AreEqual(newTitle, textPage.Value("title")); + Assert.AreEqual(titleName, textPage.Value("title")); } [Test] public async Task Can_Not_Get_Deleted_Content_By_Id() { // Arrange - var content = await PublishedContentHybridCache.GetByIdAsync(Subpage3.Id, true); - + var content = await PublishedContentHybridCache.GetByIdAsync(Subpage3Id, true); Assert.IsNotNull(content); - ContentService.Delete(Textpage); + await ContentEditingService.DeleteAsync(Subpage3.Key.Value, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Subpage3Id, true); // Assert Assert.IsNull(textPage); @@ -399,14 +497,16 @@ public async Task Can_Not_Get_Deleted_Content_By_Id() public async Task Can_Not_Get_Deleted_Content_By_Key() { // Arrange - await PublishedContentHybridCache.GetByIdAsync(Subpage3.Key, true); - ContentService.Delete(Textpage); + await PublishedContentHybridCache.GetByIdAsync(Subpage3.Key.Value, true); + var hasContent = await PublishedContentHybridCache.HasByIdAsync(Subpage3Id, true); + Assert.IsTrue(hasContent); + var result = await ContentEditingService.DeleteAsync(Subpage3.Key.Value, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Subpage3.Key.Value, true); // Assert - Assert.AreEqual(null, textPage); + Assert.IsNull(textPage); } [Test] @@ -415,14 +515,14 @@ public async Task Can_Not_Get_Deleted_Content_By_Key() public async Task Can_Not_Get_Deleted_Published_Content_By_Id(bool preview) { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - ContentService.Delete(Textpage); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + await ContentEditingService.DeleteAsync(Textpage.Key.Value, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Id, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, preview); // Assert - Assert.AreEqual(null, textPage); + Assert.IsNull(textPage); } [Test] @@ -431,14 +531,14 @@ public async Task Can_Not_Get_Deleted_Published_Content_By_Id(bool preview) public async Task Can_Not_Get_Deleted_Published_Content_By_Key(bool preview) { // Arrange - ContentService.Publish(Textpage, Array.Empty()); - ContentService.Delete(Textpage); + await ContentPublishingService.PublishAsync(Textpage.Key.Value, cultureAndSchedule, Constants.Security.SuperUserKey); + await ContentEditingService.DeleteAsync(Textpage.Key.Value, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, preview); // Assert - Assert.AreEqual(null, textPage); + Assert.IsNull(textPage); } private void AssertTextPage(IPublishedContent textPage) @@ -446,16 +546,15 @@ private void AssertTextPage(IPublishedContent textPage) Assert.Multiple(() => { Assert.IsNotNull(textPage); - Assert.AreEqual(Textpage.Id, textPage.Id); Assert.AreEqual(Textpage.Key, textPage.Key); - Assert.AreEqual(Textpage.CreatorId, textPage.CreatorId); - Assert.AreEqual(Textpage.CreateDate, textPage.CreateDate); - Assert.AreEqual(Textpage.Name, textPage.Name); + Assert.AreEqual(Textpage.ContentTypeKey, textPage.ContentType.Key); + Assert.AreEqual(Textpage.InvariantName, textPage.Name); }); - AssertProperties(Textpage.Properties, textPage.Properties); + + AssertProperties(Textpage.InvariantProperties, textPage.Properties); } - private void AssertProperties(IPropertyCollection propertyCollection, IEnumerable publishedProperties) + private void AssertProperties(IEnumerable propertyCollection, IEnumerable publishedProperties) { foreach (var prop in propertyCollection) { @@ -463,12 +562,12 @@ private void AssertProperties(IPropertyCollection propertyCollection, IEnumerabl } } - private void AssertProperty(IProperty property, IPublishedProperty publishedProperty) + private void AssertProperty(PropertyValueModel property, IPublishedProperty publishedProperty) { Assert.Multiple(() => { Assert.AreEqual(property.Alias, publishedProperty.Alias); - Assert.AreEqual(property.PropertyType.Alias, publishedProperty.PropertyType.Alias); + Assert.AreEqual(property.Value, publishedProperty.GetSourceValue()); }); } }