diff --git a/src/Umbraco.PublishedCache.HybridCache/NotificationHandlers/SeedingNotificationHandler.cs b/src/Umbraco.PublishedCache.HybridCache/NotificationHandlers/SeedingNotificationHandler.cs index 4641af7b9589..50baf83bdcf5 100644 --- a/src/Umbraco.PublishedCache.HybridCache/NotificationHandlers/SeedingNotificationHandler.cs +++ b/src/Umbraco.PublishedCache.HybridCache/NotificationHandlers/SeedingNotificationHandler.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.Options; -using Umbraco.Cms.Core.Events; -using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Infrastructure.HybridCache.Services; diff --git a/tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs b/tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs index 92f65bbc39ea..069a0d82b216 100644 --- a/tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs +++ b/tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs @@ -1,7 +1,6 @@ // 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; @@ -17,10 +16,10 @@ public class ContentEditingBuilder IWithKeyBuilder, IWithContentTypeKeyBuilder, IWithParentKeyBuilder, - IWithTemplateKeyBuilder + IWithTemplateKeyBuilder, + IBuildContentTypes { - private IContentType _contentType; - private ContentTypeBuilder _contentTypeBuilder; + private ContentTypeEditingBuilder _contentTypeEditingBuilder; private IEnumerable _invariantProperties = []; private IEnumerable _variants = []; private Guid _contentTypeKey; @@ -84,8 +83,7 @@ public ContentEditingBuilder WithInvariantProperty(string alias, object value) return this; } - public ContentEditingBuilder AddVariant(string culture, string segment, string name, - IEnumerable properties) + 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 }); @@ -104,13 +102,6 @@ public ContentEditingBuilder WithTemplateKey(Guid templateKey) return this; } - public ContentEditingBuilder WithContentType(IContentType contentType) - { - _contentTypeBuilder = null; - _contentType = contentType; - return this; - } - public override ContentCreateModel Build() { var key = _key ?? Guid.NewGuid(); @@ -120,15 +111,7 @@ public override ContentCreateModel Build() 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) { @@ -140,7 +123,7 @@ public override ContentCreateModel Build() content.TemplateKey = templateKey; } - content.ContentTypeKey = contentType.Key; + content.ContentTypeKey = _contentTypeKey; content.Key = key; content.InvariantProperties = invariantProperties; content.Variants = variants; @@ -148,25 +131,39 @@ public override ContentCreateModel Build() return content; } - public static ContentCreateModel CreateBasicContent(IContentType contentType, Guid? key) => + public static ContentCreateModel CreateBasicContent(Guid contentTypeKey, Guid? key) => new ContentEditingBuilder() .WithKey(key) - .WithContentType(contentType) + .WithContentTypeKey(contentTypeKey) .WithInvariantName("Home") .Build(); - public static ContentCreateModel CreateSimpleContent(IContentType contentType) => + public static ContentCreateModel CreateSimpleContent(Guid contentTypeKey) => new ContentEditingBuilder() - .WithContentType(contentType) + .WithContentTypeKey(contentTypeKey) .WithInvariantName("Home") .WithInvariantProperty("title", "Welcome to our Home page") .Build(); - public static ContentCreateModel CreateSimpleContent(IContentType contentType, string name, Guid? parentKey) => + public static ContentCreateModel CreateSimpleContent(Guid contentTypeKey, string name, Guid? parentKey) => new ContentEditingBuilder() - .WithContentType(contentType) + .WithContentTypeKey(contentTypeKey) .WithInvariantName(name) .WithParentKey(parentKey) .WithInvariantProperty("title", "Welcome to our Home page") .Build(); + + public static ContentCreateModel CreateSimpleContent(Guid contentTypeKey, string name) => + new ContentEditingBuilder() + .WithContentTypeKey(contentTypeKey) + .WithInvariantName(name) + .WithInvariantProperty("title", "Welcome to our Home page") + .Build(); + + public static ContentCreateModel CreateContentWithTwoVariantProperties(Guid contentTypeKey, string firstCulture, string secondCulture, string propertyAlias, string propertyName) => + new ContentEditingBuilder() + .WithContentTypeKey(contentTypeKey) + .AddVariant(firstCulture, null, firstCulture, new[] { new PropertyValueModel { Alias = propertyAlias, Value = propertyName } }) + .AddVariant(secondCulture, null, secondCulture, new[] { new PropertyValueModel { Alias = propertyAlias, Value = propertyName } }) + .Build(); } diff --git a/tests/Umbraco.Tests.Common/Builders/ContentTypeEditingBuilder.cs b/tests/Umbraco.Tests.Common/Builders/ContentTypeEditingBuilder.cs new file mode 100644 index 000000000000..bcbcbcada04d --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/ContentTypeEditingBuilder.cs @@ -0,0 +1,240 @@ +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models.ContentTypeEditing; +using Umbraco.Cms.Tests.Common.Builders.Extensions; +using Umbraco.Cms.Tests.Common.Builders.Interfaces; + +namespace Umbraco.Cms.Tests.Common.Builders; + +public class ContentTypeEditingBuilder + : ContentTypeBaseBuilder, + IBuildPropertyTypes +{ + private Guid? _key; + private Guid? _containerKey; + private ContentTypeCleanup _cleanup = new(); + private IEnumerable _allowedTemplateKeys; + private Guid? _defaultTemplateKey; + private bool? _allowAtRoot; + private bool? _isElement; + private bool? _variesByCulture; + private bool? _variesBySegment; + private readonly List _propertyTypeBuilders = []; + private readonly List> _propertyTypeContainerBuilders = []; + private readonly List _allowedContentTypeBuilders = []; + + public ContentTypeEditingBuilder() + : base(null) + { + } + + public ContentTypeEditingBuilder(ContentEditingBuilder parentBuilder) + : base(parentBuilder) + { + } + + public ContentTypeEditingBuilder WithDefaultTemplateKey(Guid templateKey) + { + _defaultTemplateKey = templateKey; + return this; + } + + public ContentTypeEditingBuilder WithIsElement(bool isElement) + { + _isElement = isElement; + return this; + } + + public PropertyTypeContainerBuilder AddPropertyGroup() + { + var builder = new PropertyTypeContainerBuilder(this); + _propertyTypeContainerBuilders.Add(builder); + return builder; + } + + public PropertyTypeEditingBuilder AddPropertyType() + { + var builder = new PropertyTypeEditingBuilder(this); + _propertyTypeBuilders.Add(builder); + return builder; + } + + + public ContentTypeSortBuilder AddAllowedContentType() + { + var builder = new ContentTypeSortBuilder(this); + _allowedContentTypeBuilders.Add(builder); + return builder; + } + + public ContentTypeEditingBuilder AddAllowedTemplateKeys(IEnumerable templateKeys) + { + _allowedTemplateKeys = templateKeys; + return this; + } + + public ContentTypeEditingBuilder WithAllowAtRoot(bool allowAtRoot) + { + _allowAtRoot = allowAtRoot; + return this; + } + + public ContentTypeEditingBuilder WithVariesByCulture(bool variesByCulture) + { + _variesByCulture = variesByCulture; + return this; + } + + public ContentTypeEditingBuilder WithVariesBySegment(bool variesBySegment) + { + _variesBySegment = variesBySegment; + return this; + } + + public override ContentTypeCreateModel Build() + { + ContentTypeCreateModel contentType = new ContentTypeCreateModel(); + contentType.Name = GetName(); + contentType.Alias = GetAlias(); + contentType.Key = GetKey(); + contentType.ContainerKey = _containerKey; + contentType.Cleanup = _cleanup; + contentType.AllowedTemplateKeys = _allowedTemplateKeys ?? Array.Empty(); + contentType.DefaultTemplateKey = _defaultTemplateKey; + contentType.IsElement = _isElement ?? false; + contentType.VariesByCulture = _variesByCulture ?? false; + contentType.VariesBySegment = _variesBySegment ?? false; + contentType.AllowedAsRoot = _allowAtRoot ?? false; + contentType.Properties = _propertyTypeBuilders.Select(x => x.Build()); + contentType.Containers = _propertyTypeContainerBuilders.Select(x => x.Build()); + contentType.AllowedContentTypes = _allowedContentTypeBuilders.Select(x => x.Build()); + + return contentType; + } + + public static ContentTypeCreateModel CreateBasicContentType(string alias = "umbTextpage", string name = "TextPage", IContentType parent = null) + { + var builder = new ContentTypeEditingBuilder(); + return (ContentTypeCreateModel)builder + .WithAlias(alias) + .WithName(name) + .WithParentContentType(parent) + .Build(); + } + + public static ContentTypeCreateModel CreateSimpleContentType(string alias = "umbTextpage", string name = "TextPage", IContentType parent = null, string propertyGroupName = "Content", Guid? defaultTemplateKey = null) + { + var containerKey = Guid.NewGuid(); + var builder = new ContentTypeEditingBuilder(); + return (ContentTypeCreateModel)builder + .WithAlias(alias) + .WithName(name) + .WithAllowAtRoot(true) + .WithParentContentType(parent) + .AddPropertyGroup() + .WithKey(containerKey) + .WithName(propertyGroupName) + .Done() + .AddPropertyType() + .WithAlias("title") + .WithDataTypeKey(Constants.DataTypes.Guids.TextareaGuid) + .WithName("Title") + .WithContainerKey(containerKey) + .Done() + .WithDefaultTemplateKey(defaultTemplateKey ?? Guid.Empty) + .AddAllowedTemplateKeys([defaultTemplateKey ?? Guid.Empty]) + .Build(); + } + + public static ContentTypeCreateModel CreateTextPageContentType(string alias = "textPage", string name = "Text Page", Guid defaultTemplateKey = default) + { + var containerKeyOne = Guid.NewGuid(); + var containerKeyTwo = Guid.NewGuid(); + + var builder = new ContentTypeEditingBuilder(); + return (ContentTypeCreateModel)builder + .WithAlias(alias) + .WithName(name) + .WithAllowAtRoot(true) + .AddPropertyGroup() + .WithName("Content") + .WithKey(containerKeyOne) + .WithSortOrder(1) + .Done() + .AddPropertyType() + .WithAlias("title") + .WithName("Title") + .WithContainerKey(containerKeyOne) + .WithSortOrder(1) + .Done() + .AddPropertyType() + .WithDataTypeKey(Constants.DataTypes.Guids.RichtextEditorGuid) + .WithAlias("bodyText") + .WithName("Body text") + .WithContainerKey(containerKeyOne) + .WithSortOrder(2) + .Done() + .AddPropertyGroup() + .WithName("Meta") + .WithSortOrder(2) + .WithKey(containerKeyTwo) + .Done() + .AddPropertyType() + .WithAlias("keywords") + .WithName("Keywords") + .WithContainerKey(containerKeyTwo) + .WithSortOrder(1) + .Done() + .AddPropertyType() + .WithAlias("description") + .WithName("Description") + .WithContainerKey(containerKeyTwo) + .WithSortOrder(2) + .Done() + .AddAllowedTemplateKeys([defaultTemplateKey]) + .WithDefaultTemplateKey(defaultTemplateKey) + .Build(); + } + + public static ContentTypeCreateModel CreateElementType(string alias = "textElement", string name = "Text Element") + { + var containerKey = Guid.NewGuid(); + var builder = new ContentTypeEditingBuilder(); + return (ContentTypeCreateModel)builder + .WithAlias(alias) + .WithName(name) + .WithIsElement(true) + .AddPropertyGroup() + .WithName("Content") + .WithKey(containerKey) + .Done() + .AddPropertyType() + .WithDataTypeKey(Constants.DataTypes.Guids.RichtextEditorGuid) + .WithAlias("bodyText") + .WithName("Body text") + .WithContainerKey(containerKey) + .Done() + .Build(); + } + + public static ContentTypeCreateModel CreateContentTypeWithDataTypeKey(Guid dataTypeKey, string alias = "textElement", string name = "Text Element" ) + { + var containerKey = Guid.NewGuid(); + var builder = new ContentTypeEditingBuilder(); + return (ContentTypeCreateModel)builder + .WithAlias(alias) + .WithName(name) + .WithIsElement(true) + .AddPropertyGroup() + .WithName("Content") + .WithKey(containerKey) + .Done() + .AddPropertyType() + .WithDataTypeKey(dataTypeKey) + .WithAlias("dataType") + .WithName("Data Type") + .WithContainerKey(containerKey) + .Done() + .Build(); + } +} diff --git a/tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs b/tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs index 7a4deca5f755..a63205d4d289 100644 --- a/tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs +++ b/tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs @@ -28,6 +28,11 @@ public ContentTypeSortBuilder(ContentTypeBuilder parentBuilder) { } + public ContentTypeSortBuilder(ContentTypeEditingBuilder parentBuilder) + : base(null) + { + } + string IWithAliasBuilder.Alias { get => _alias; diff --git a/tests/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs b/tests/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs index 1a4660ed6478..f4cc7db311c5 100644 --- a/tests/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs +++ b/tests/Umbraco.Tests.Common/Builders/Extensions/BuilderExtensions.cs @@ -80,6 +80,13 @@ public static T WithKey(this T builder, Guid key) return builder; } + public static T WithDataTypeKey(this T builder, Guid key) + where T : IWithDataTypeKeyBuilder + { + builder.DataTypeKey = key; + return builder; + } + public static T WithParentId(this T builder, int parentId) where T : IWithParentIdBuilder { diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWIthContainerKeyBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWIthContainerKeyBuilder.cs new file mode 100644 index 000000000000..cc184eea48ff --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWIthContainerKeyBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWIthContainerKeyBuilder +{ + Guid? ContainerKey { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithDataTypeKeyBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithDataTypeKeyBuilder.cs new file mode 100644 index 000000000000..ff188b801702 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithDataTypeKeyBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithDataTypeKeyBuilder +{ + Guid? DataTypeKey { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithLabelOnTop.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithLabelOnTop.cs new file mode 100644 index 000000000000..e86c18a1dc98 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithLabelOnTop.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithLabelOnTop +{ + public bool? LabelOnTop { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithMandatoryBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithMandatoryBuilder.cs new file mode 100644 index 000000000000..04bea1ecb94d --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithMandatoryBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithMandatoryBuilder +{ + bool? Mandatory { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithMandatoryMessageBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithMandatoryMessageBuilder.cs new file mode 100644 index 000000000000..0f5c510dc016 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithMandatoryMessageBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithMandatoryMessageBuilder +{ + string MandatoryMessage { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithRegularExpressionBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithRegularExpressionBuilder.cs new file mode 100644 index 000000000000..b6cdc8165147 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithRegularExpressionBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithRegularExpressionBuilder +{ + string RegularExpression { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithRegularExpressionMessage.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithRegularExpressionMessage.cs new file mode 100644 index 000000000000..6d2e0eef6624 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithRegularExpressionMessage.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithRegularExpressionMessage +{ + string RegularExpressionMessage { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithTypeBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithTypeBuilder.cs new file mode 100644 index 000000000000..8d25ce8a551d --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithTypeBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithTypeBuilder +{ + public string Type { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithVariesByCultureBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithVariesByCultureBuilder.cs new file mode 100644 index 000000000000..fe347f30c0bb --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithVariesByCultureBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithVariesByCultureBuilder +{ + bool VariesByCulture { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithVariesBySegmentBuilder.cs b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithVariesBySegmentBuilder.cs new file mode 100644 index 000000000000..0d9ea748e1f0 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/Interfaces/IWithVariesBySegmentBuilder.cs @@ -0,0 +1,6 @@ +namespace Umbraco.Cms.Tests.Common.Builders.Interfaces; + +public interface IWithVariesBySegmentBuilder +{ + bool VariesBySegment { get; set; } +} diff --git a/tests/Umbraco.Tests.Common/Builders/PropertyTypeAppearanceBuilder.cs b/tests/Umbraco.Tests.Common/Builders/PropertyTypeAppearanceBuilder.cs new file mode 100644 index 000000000000..4917b2861baf --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/PropertyTypeAppearanceBuilder.cs @@ -0,0 +1,22 @@ +using Umbraco.Cms.Core.Models.ContentTypeEditing; +using Umbraco.Cms.Tests.Common.Builders.Interfaces; + +namespace Umbraco.Cms.Tests.Common.Builders; + +public class PropertyTypeAppearanceBuilder + : ChildBuilderBase, IBuildPropertyTypes, IWithLabelOnTop +{ + private bool? _labelOnTop; + + public PropertyTypeAppearanceBuilder(PropertyTypeEditingBuilder parentBuilder) : base(parentBuilder) + { + } + + bool? IWithLabelOnTop.LabelOnTop + { + get => _labelOnTop; + set => _labelOnTop = value; + } + + public override PropertyTypeAppearance Build() => new() { LabelOnTop = _labelOnTop ?? false }; +} diff --git a/tests/Umbraco.Tests.Common/Builders/PropertyTypeContainerBuilder.cs b/tests/Umbraco.Tests.Common/Builders/PropertyTypeContainerBuilder.cs new file mode 100644 index 000000000000..b5af22eb5003 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/PropertyTypeContainerBuilder.cs @@ -0,0 +1,66 @@ +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models.ContentTypeEditing; +using Umbraco.Cms.Tests.Common.Builders.Interfaces; + +namespace Umbraco.Cms.Tests.Common.Builders; + +public class PropertyTypeContainerBuilder(TParent parentBuilder) + : ChildBuilderBase(parentBuilder), + IBuildPropertyTypes, IWithKeyBuilder, IWithParentKeyBuilder, IWithNameBuilder, IWithTypeBuilder, + IWithSortOrderBuilder +{ + private Guid? _key; + private Guid? _parentKey; + private string _name; + private string _type; + private int? _sortOrder; + + Guid? IWithKeyBuilder.Key + { + get => _key; + set => _key = value; + } + + Guid? IWithParentKeyBuilder.ParentKey + { + get => _parentKey; + set => _parentKey = value; + } + + string IWithNameBuilder.Name + { + get => _name; + set => _name = value; + } + + string IWithTypeBuilder.Type + { + get => _type; + set => _type = value; + } + + int? IWithSortOrderBuilder.SortOrder + { + get => _sortOrder; + set => _sortOrder = value; + } + + public override ContentTypePropertyContainerModel Build() + { + var key = _key ?? Guid.NewGuid(); + var parentKey = _parentKey; + var name = _name ?? "Container"; + var type = _type ?? "Group"; + var sortOrder = _sortOrder ?? 0; + + + return new ContentTypePropertyContainerModel + { + Key = key, + ParentKey = parentKey, + Name = name, + Type = type, + SortOrder = sortOrder, + }; + } +} diff --git a/tests/Umbraco.Tests.Common/Builders/PropertyTypeEditingBuilder.cs b/tests/Umbraco.Tests.Common/Builders/PropertyTypeEditingBuilder.cs new file mode 100644 index 000000000000..301d77f37b85 --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/PropertyTypeEditingBuilder.cs @@ -0,0 +1,176 @@ +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Models.ContentTypeEditing; +using Umbraco.Cms.Tests.Common.Builders.Interfaces; + +namespace Umbraco.Cms.Tests.Common.Builders; + +public class PropertyTypeEditingBuilder + : ChildBuilderBase, IBuildPropertyTypes, IWithKeyBuilder, + IWIthContainerKeyBuilder, + IWithSortOrderBuilder, IWithAliasBuilder, IWithNameBuilder, IWithDescriptionBuilder, IWithDataTypeKeyBuilder, + IWithVariesByCultureBuilder, IWithVariesBySegmentBuilder +{ + private Guid? _key; + private Guid? _containerKey; + private int? _sortOrder; + private string _alias; + private string? _name; + private string? _description; + private Guid? _dataTypeKey; + private bool _variesByCulture; + private bool _variesBySegment; + private PropertyTypeValidationEditingBuilder _validationBuilder; + private PropertyTypeAppearanceBuilder _appearanceBuilder; + + public PropertyTypeEditingBuilder(ContentTypeEditingBuilder parentBuilder) : base(parentBuilder) + { + _validationBuilder = new PropertyTypeValidationEditingBuilder(this); + _appearanceBuilder = new PropertyTypeAppearanceBuilder(this); + } + + Guid? IWithKeyBuilder.Key + { + get => _key; + set => _key = value; + } + + Guid? IWIthContainerKeyBuilder.ContainerKey + { + get => _containerKey; + set => _containerKey = value; + } + + int? IWithSortOrderBuilder.SortOrder + { + get => _sortOrder; + set => _sortOrder = value; + } + + string IWithAliasBuilder.Alias + { + get => _alias; + set => _alias = value; + } + + string IWithNameBuilder.Name + { + get => _name; + set => _name = value; + } + + string IWithDescriptionBuilder.Description + { + get => _description; + set => _description = value; + } + + Guid? IWithDataTypeKeyBuilder.DataTypeKey + { + get => _dataTypeKey; + set => _dataTypeKey = value; + } + + bool IWithVariesByCultureBuilder.VariesByCulture + { + get => _variesByCulture; + set => _variesByCulture = value; + } + + bool IWithVariesBySegmentBuilder.VariesBySegment + { + get => _variesBySegment; + set => _variesBySegment = value; + } + + public PropertyTypeValidationEditingBuilder AddValidation() + { + var builder = new PropertyTypeValidationEditingBuilder(this); + _validationBuilder = builder; + return builder; + } + + public PropertyTypeAppearanceBuilder AddAppearance() + { + var builder = new PropertyTypeAppearanceBuilder(this); + _appearanceBuilder = builder; + return builder; + } + + public PropertyTypeEditingBuilder WithContainerKey(Guid? containerKey) + { + _containerKey = containerKey; + return this; + } + + public PropertyTypeEditingBuilder WithSortOrder(int sortOrder) + { + _sortOrder = sortOrder; + return this; + } + + public PropertyTypeEditingBuilder WithAlias(string alias) + { + _alias = alias; + return this; + } + + public PropertyTypeEditingBuilder WithName(string name) + { + _name = name; + return this; + } + + public PropertyTypeEditingBuilder WithDescription(string description) + { + _description = description; + return this; + } + + public PropertyTypeEditingBuilder WithDataTypeKey(Guid dataTypeKey) + { + _dataTypeKey = dataTypeKey; + return this; + } + + public PropertyTypeEditingBuilder WithVariesByCulture(bool variesByCulture) + { + _variesByCulture = variesByCulture; + return this; + } + + public PropertyTypeEditingBuilder WithVariesBySegment(bool variesBySegment) + { + _variesBySegment = variesBySegment; + return this; + } + + public override ContentTypePropertyTypeModel Build() + { + var key = _key ?? Guid.NewGuid(); + var containerKey = _containerKey; + var sortOrder = _sortOrder ?? 0; + var alias = _alias ?? "title"; + var name = _name ?? "Title"; + var description = _description; + var dataTypeKey = _dataTypeKey ?? Constants.DataTypes.Guids.TextareaGuid; + var variesByCulture = _variesByCulture; + var variesBySegment = _variesBySegment; + var validation = _validationBuilder.Build(); + var appearance = _appearanceBuilder.Build(); + + return new ContentTypePropertyTypeModel + { + Key = key, + ContainerKey = containerKey, + SortOrder = sortOrder, + Alias = alias, + Name = name, + Description = description, + DataTypeKey = dataTypeKey, + VariesByCulture = variesByCulture, + VariesBySegment = variesBySegment, + Validation = validation, + Appearance = appearance, + }; + } +} diff --git a/tests/Umbraco.Tests.Common/Builders/PropertyTypeValidationEditingBuilder.cs b/tests/Umbraco.Tests.Common/Builders/PropertyTypeValidationEditingBuilder.cs new file mode 100644 index 000000000000..781463c760cc --- /dev/null +++ b/tests/Umbraco.Tests.Common/Builders/PropertyTypeValidationEditingBuilder.cs @@ -0,0 +1,55 @@ +using Umbraco.Cms.Core.Models.ContentTypeEditing; +using Umbraco.Cms.Tests.Common.Builders.Interfaces; + +namespace Umbraco.Cms.Tests.Common.Builders; + +public class PropertyTypeValidationEditingBuilder + : ChildBuilderBase, IBuildPropertyTypes, IWithMandatoryBuilder, + IWithMandatoryMessageBuilder, IWithRegularExpressionBuilder, IWithRegularExpressionMessage +{ + private bool? _mandatory; + private string? _mandatoryMessage; + private string? _regularExpression; + private string? _regularExpressionMessage; + + public PropertyTypeValidationEditingBuilder(PropertyTypeEditingBuilder parentBuilder) : base(parentBuilder) + { + } + + bool? IWithMandatoryBuilder.Mandatory + { + get => _mandatory; + set => _mandatory = value; + } + + string? IWithMandatoryMessageBuilder.MandatoryMessage + { + get => _mandatoryMessage; + set => _mandatoryMessage = value; + } + + string? IWithRegularExpressionBuilder.RegularExpression + { + get => _regularExpression; + set => _regularExpression = value; + } + + string? IWithRegularExpressionMessage.RegularExpressionMessage + { + get => _regularExpressionMessage; + set => _regularExpressionMessage = value; + } + + public override PropertyTypeValidation Build() + { + var validation = new PropertyTypeValidation + { + Mandatory = _mandatory ?? false, + MandatoryMessage = _mandatoryMessage ?? null, + RegularExpression = _regularExpression ?? null, + RegularExpressionMessage = _regularExpressionMessage ?? null, + }; + + return validation; + } +} diff --git a/tests/Umbraco.Tests.Common/TestHelpers/ContentTypeUpdateHelper.cs b/tests/Umbraco.Tests.Common/TestHelpers/ContentTypeUpdateHelper.cs new file mode 100644 index 000000000000..d2d083cde70b --- /dev/null +++ b/tests/Umbraco.Tests.Common/TestHelpers/ContentTypeUpdateHelper.cs @@ -0,0 +1,83 @@ +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models.ContentTypeEditing; +using Umbraco.Extensions; + +namespace Umbraco.Cms.Tests.Common.TestHelpers; + +public class ContentTypeUpdateHelper +{ + public ContentTypeUpdateModel CreateContentTypeUpdateModel(IContentType contentType) + { + var updateModel = new ContentTypeUpdateModel(); + var model = MapBaseProperties(contentType, updateModel); + return model; + } + + private T MapBaseProperties(IContentType contentType, T model) where T : ContentTypeModelBase + { + model.Alias = contentType.Alias; + model.Name = contentType.Name; + model.Description = contentType.Description; + model.Icon = contentType.Icon; + model.AllowedAsRoot = contentType.AllowedAsRoot; + model.VariesByCulture = contentType.VariesByCulture(); + model.VariesBySegment = contentType.VariesBySegment(); + model.IsElement = contentType.IsElement; + model.ListView = contentType.ListView; + model.Cleanup = new ContentTypeCleanup() + { + PreventCleanup = contentType.HistoryCleanup.PreventCleanup, + KeepAllVersionsNewerThanDays = contentType.HistoryCleanup.KeepAllVersionsNewerThanDays, + KeepLatestVersionPerDayForDays = contentType.HistoryCleanup.KeepLatestVersionPerDayForDays + }; + + model.AllowedTemplateKeys = contentType.AllowedTemplates.Select(x => x.Key); + model.DefaultTemplateKey = contentType.DefaultTemplate?.Key; + + var tempContainerList = model.Containers.ToList(); + + foreach (var container in contentType.PropertyGroups) + { + var containerModel = new ContentTypePropertyContainerModel() + { + Key = container.Key, + Name = container.Name, + SortOrder = container.SortOrder, + Type = container.Type.ToString() + }; + tempContainerList.Add(containerModel); + } + + model.Containers = tempContainerList.AsEnumerable(); + + var tempPropertyList = model.Properties.ToList(); + + foreach (var propertyType in contentType.PropertyTypes) + { + var propertyModel = new ContentTypePropertyTypeModel + { + Key = propertyType.Key, + ContainerKey = contentType.PropertyGroups.Single(x => x.PropertyTypes.Contains(propertyType)).Key, + SortOrder = propertyType.SortOrder, + Alias = propertyType.Alias, + Name = propertyType.Name, + Description = propertyType.Description, + DataTypeKey = propertyType.DataTypeKey, + VariesByCulture = propertyType.VariesByCulture(), + VariesBySegment = propertyType.VariesBySegment(), + Validation = new PropertyTypeValidation() + { + Mandatory = propertyType.Mandatory, + MandatoryMessage = propertyType.ValidationRegExp, + RegularExpression = propertyType.ValidationRegExp, + RegularExpressionMessage = propertyType.ValidationRegExpMessage, + }, + Appearance = new PropertyTypeAppearance() { LabelOnTop = propertyType.LabelOnTop, } + }; + tempPropertyList.Add(propertyModel); + } + + model.Properties = tempPropertyList.AsEnumerable(); + return model; + } +} diff --git a/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContentEditing.cs b/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContentEditing.cs index 10dd0cb46776..e1f047dd9047 100644 --- a/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContentEditing.cs +++ b/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestWithContentEditing.cs @@ -6,28 +6,31 @@ using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.ContentEditing; using Umbraco.Cms.Core.Models.ContentPublishing; +using Umbraco.Cms.Core.Models.ContentTypeEditing; using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Core.Services.ContentTypeEditing; using Umbraco.Cms.Tests.Common.Builders; +using Umbraco.Cms.Tests.Common.TestHelpers; namespace Umbraco.Cms.Tests.Integration.Testing; public abstract class UmbracoIntegrationTestWithContentEditing : UmbracoIntegrationTest { - protected IContentTypeService ContentTypeService => GetRequiredService(); + protected IContentTypeEditingService ContentTypeEditingService => GetRequiredService(); protected ITemplateService TemplateService => GetRequiredService(); - private ContentEditingService ContentEditingService => - (ContentEditingService)GetRequiredService(); + private IContentEditingService ContentEditingService => (IContentEditingService)GetRequiredService(); - private ContentPublishingService ContentPublishingService => - (ContentPublishingService)GetRequiredService(); + private IContentPublishingService ContentPublishingService => (IContentPublishingService)GetRequiredService(); + protected int TemplateId { get; private set; } + + protected ContentCreateModel Subpage1 { get; private set; } protected ContentCreateModel Subpage2 { get; private set; } - protected ContentCreateModel Subpage3 { get; private set; } - protected ContentCreateModel Subpage { get; private set; } + protected ContentCreateModel PublishedTextPage { get; private set; } protected ContentCreateModel Textpage { get; private set; } @@ -37,13 +40,17 @@ public abstract class UmbracoIntegrationTestWithContentEditing : UmbracoIntegrat protected int TextpageId { get; private set; } - protected int SubpageId { get; private set; } + protected int PublishedTextPageId { get; private set; } + + protected int Subpage1Id { get; private set; } protected int Subpage2Id { get; private set; } - protected int Subpage3Id { get; private set; } + protected ContentTypeCreateModel ContentTypeCreateModel { get; private set; } + + protected ContentTypeUpdateModel ContentTypeUpdateModel { get; private set; } - protected ContentType ContentType { get; private set; } + protected IContentType ContentType { get; private set; } [SetUp] public new void Setup() => CreateTestData(); @@ -53,19 +60,24 @@ 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); - + TemplateId = template.Id; // 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); + ContentTypeCreateModel = ContentTypeEditingBuilder.CreateSimpleContentType("umbTextpage", "Textpage", defaultTemplateKey: template.Key); + var contentTypeAttempt = await ContentTypeEditingService.CreateAsync(ContentTypeCreateModel, Constants.Security.SuperUserKey); + Assert.IsTrue(contentTypeAttempt.Success); + + var contentTypeResult = contentTypeAttempt.Result; + ContentTypeUpdateHelper contentTypeUpdateHelper = new ContentTypeUpdateHelper(); + ContentTypeUpdateModel = contentTypeUpdateHelper.CreateContentTypeUpdateModel(contentTypeResult); ContentTypeUpdateModel.AllowedContentTypes = new[] + { + new ContentTypeSort(contentTypeResult.Key, 0, ContentTypeCreateModel.Alias), + }; + var updatedContentTypeResult = await ContentTypeEditingService.UpdateAsync(contentTypeResult, ContentTypeUpdateModel, Constants.Security.SuperUserKey); + Assert.IsTrue(updatedContentTypeResult.Success); + ContentType = updatedContentTypeResult.Result; // Create and Save Content "Homepage" based on "umbTextpage" -> 1053 - Textpage = ContentEditingBuilder.CreateSimpleContent(ContentType); - Textpage.Key = new Guid("B58B3AD4-62C2-4E27-B1BE-837BD7C533E0"); + Textpage = ContentEditingBuilder.CreateSimpleContent(ContentType.Key); var createContentResultTextPage = await ContentEditingService.CreateAsync(Textpage, Constants.Security.SuperUserKey); Assert.IsTrue(createContentResultTextPage.Success); @@ -87,47 +99,48 @@ protected async void CreateTestData() }; // 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); + PublishedTextPage = ContentEditingBuilder.CreateSimpleContent(ContentType.Key, "Published Page"); + var createContentResultPublishPage = await ContentEditingService.CreateAsync(PublishedTextPage, Constants.Security.SuperUserKey); + Assert.IsTrue(createContentResultPublishPage.Success); - if (!Subpage.Key.HasValue) + if (!PublishedTextPage.Key.HasValue) { throw new InvalidOperationException("The content page key is null."); } - if (createContentResultSubPage.Result.Content != null) + if (createContentResultPublishPage.Result.Content != null) { - SubpageId = createContentResultSubPage.Result.Content.Id; + PublishedTextPageId = createContentResultPublishPage.Result.Content.Id; } - await ContentPublishingService.PublishAsync(Subpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); + var publishResult = await ContentPublishingService.PublishAsync(PublishedTextPage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); + Assert.IsTrue(publishResult.Success); // 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) + Subpage1 = ContentEditingBuilder.CreateSimpleContent(ContentType.Key, "Text Page 1", Textpage.Key); + var createContentResultSubPage1 = await ContentEditingService.CreateAsync(Subpage1, Constants.Security.SuperUserKey); + Assert.IsTrue(createContentResultSubPage1.Success); + if (!Subpage1.Key.HasValue) { throw new InvalidOperationException("The content page key is null."); } - if (createContentResultSubPage2.Result.Content != null) + if (createContentResultSubPage1.Result.Content != null) { - Subpage2Id = createContentResultSubPage2.Result.Content.Id; + Subpage1Id = createContentResultSubPage1.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) + Subpage2 = ContentEditingBuilder.CreateSimpleContent(ContentType.Key, "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 (createContentResultSubPage3.Result.Content != null) + if (createContentResultSubPage2.Result.Content != null) { - Subpage3Id = createContentResultSubPage3.Result.Content.Id; + Subpage2Id = createContentResultSubPage2.Result.Content.Id; } } } diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Core/Cache/PublishedContentTypeCacheTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Core/Cache/PublishedContentTypeCacheTests.cs new file mode 100644 index 000000000000..7c4b62f2af6d --- /dev/null +++ b/tests/Umbraco.Tests.Integration/Umbraco.Core/Cache/PublishedContentTypeCacheTests.cs @@ -0,0 +1,63 @@ +using NUnit.Framework; +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Models.ContentTypeEditing; +using Umbraco.Cms.Core.Models.PublishedContent; +using Umbraco.Cms.Core.PublishedCache; +using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Tests.Common.TestHelpers; +using Umbraco.Cms.Tests.Common.Testing; +using Umbraco.Cms.Tests.Integration.Testing; + +namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Cache; + +[TestFixture] +[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] +[Platform("Linux", Reason = "This uses too much memory when running both caches, should be removed when nuchache is removed")] +public class PublishedContentTypeCacheTests : UmbracoIntegrationTestWithContentEditing +{ + protected override void CustomTestSetup(IUmbracoBuilder builder) => builder.AddUmbracoHybridCache(); + + private IPublishedContentTypeCache PublishedContentTypeCache => GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); + + [Test] + public async Task Can_Get_Published_DocumentType_By_Key() + { + // Act + var contentType = PublishedContentTypeCache.Get(PublishedItemType.Content, ContentType.Key); + + // Assert + Assert.IsNotNull(contentType); + } + + [Test] + public async Task Can_Get_Updated_Published_DocumentType_By_Key() + { + // Arrange + var contentType = PublishedContentTypeCache.Get(PublishedItemType.Content, Textpage.ContentTypeKey); + Assert.IsNotNull(contentType); + Assert.AreEqual(1, ContentType.PropertyTypes.Count()); + // Update the content type + ContentTypeUpdateHelper contentTypeUpdateHelper = new ContentTypeUpdateHelper(); + var updateModel = contentTypeUpdateHelper.CreateContentTypeUpdateModel(ContentType); + updateModel.Properties = new List(); + await ContentTypeEditingService.UpdateAsync(ContentType, updateModel, Constants.Security.SuperUserKey); + + // Act + var updatedContentType = PublishedContentTypeCache.Get(PublishedItemType.Content, ContentType.Key); + + // Assert + Assert.IsNotNull(updatedContentType); + Assert.AreEqual(0, updatedContentType.PropertyTypes.Count()); + } + + [Test] + public async Task Published_DocumentType_Gets_Deleted() + { + var contentType = PublishedContentTypeCache.Get(PublishedItemType.Content, ContentType.Key); + Assert.IsNotNull(contentType); + + await ContentTypeService.DeleteAsync(contentType.Key, Constants.Security.SuperUserKey); + Assert.Catch(() => PublishedContentTypeCache.Get(PublishedItemType.Content, ContentType.Key)); + } +} diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheDocumentTypeTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheDocumentTypeTests.cs index f0c70c591196..57503c71f220 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheDocumentTypeTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheDocumentTypeTests.cs @@ -1,7 +1,7 @@ using NUnit.Framework; using Umbraco.Cms.Core; -using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PublishedCache; +using Umbraco.Cms.Core.Services; using Umbraco.Cms.Tests.Common.Testing; using Umbraco.Cms.Tests.Integration.Testing; @@ -16,16 +16,16 @@ public class DocumentHybridCacheDocumentTypeTests : UmbracoIntegrationTestWithCo private IPublishedContentCache PublishedContentHybridCache => GetRequiredService(); - private IPublishedContentTypeCache PublishedContentTypeCache => GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); [Test] public async Task Can_Get_Draft_Content_By_Id() { //Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); + await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); ContentType.RemovePropertyType("title"); - ContentTypeService.Save(ContentType); + await ContentTypeService.UpdateAsync(ContentType, Constants.Security.SuperUserKey); // Assert var newTextPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); @@ -36,10 +36,10 @@ 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.Value, true); + await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); ContentType.RemovePropertyType("title"); - ContentTypeService.Save(ContentType); + await ContentTypeService.UpdateAsync(ContentType, Constants.Security.SuperUserKey); //Assert var newTextPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); Assert.IsNull(newTextPage.Value("title")); @@ -57,26 +57,4 @@ public async Task Content_Gets_Removed_When_DocumentType_Is_Deleted() var textpageAgain = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, preview: true); Assert.IsNull(textpageAgain); } - - - // TODO: Copy this into PublishedContentTypeCache - [Test] - public async Task Can_Get_Published_DocumentType_By_Key() - { - var contentType = PublishedContentTypeCache.Get(PublishedItemType.Content, Textpage.ContentTypeKey); - Assert.IsNotNull(contentType); - var contentTypeAgain = PublishedContentTypeCache.Get(PublishedItemType.Content, Textpage.ContentTypeKey); - Assert.IsNotNull(contentType); - } - - [Test] - public async Task Published_DocumentType_Gets_Deleted() - { - var contentType = PublishedContentTypeCache.Get(PublishedItemType.Content, Textpage.ContentTypeKey); - Assert.IsNotNull(contentType); - - await ContentTypeService.DeleteAsync(contentType.Key, Constants.Security.SuperUserKey); - // PublishedContentTypeCache just explodes if it doesn't exist - Assert.Catch(() => PublishedContentTypeCache.Get(PublishedItemType.Content, Textpage.ContentTypeKey)); - } } diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs index 8e7ed6e1e4da..5fc467f2f68e 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs @@ -1,4 +1,4 @@ -using Microsoft.Extensions.Options; +using Microsoft.Extensions.Options; using Moq; using NUnit.Framework; using Umbraco.Cms.Core; diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCachePropertyTest.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCachePropertyTest.cs index 0cfc342917b1..68eddf35dfcc 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCachePropertyTest.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCachePropertyTest.cs @@ -6,6 +6,7 @@ using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PublishedCache; using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Core.Services.ContentTypeEditing; using Umbraco.Cms.Core.Services.OperationStatus; using Umbraco.Cms.Tests.Common.Builders; using Umbraco.Cms.Tests.Common.Builders.Extensions; @@ -25,23 +26,25 @@ public class DocumentHybridCachePropertyTest : UmbracoIntegrationTest private ITemplateService TemplateService => GetRequiredService(); - private IContentTypeService ContentTypeService => GetRequiredService(); - private IContentEditingService ContentEditingService => GetRequiredService(); - private IContentPublishingService ContentPublishingService => GetRequiredService(); + private IContentTypeEditingService ContentTypeEditingService => GetRequiredService(); + private IContentPublishingService ContentPublishingService => GetRequiredService(); [Test] public async Task Can_Get_Value_From_ContentPicker() { + // Arrange var template = TemplateBuilder.CreateTextPageTemplate(); await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey); - var textPage = await CreateTextPageDocument(template.Id); - var contentPickerDocument = await CreateContentPickerDocument(template.Id, textPage.Key); + var textPage = await CreateTextPageDocument(template.Key); + var contentPickerDocument = await CreateContentPickerDocument(template.Key, textPage.Key); + // Act var contentPickerPage = await CacheManager.Content.GetByIdAsync(contentPickerDocument.Id); + // Assert IPublishedContent contentPickerValue = (IPublishedContent)contentPickerPage.Value("contentPicker"); Assert.AreEqual(textPage.Key, contentPickerValue.Key); Assert.AreEqual(textPage.Id, contentPickerValue.Id); @@ -52,10 +55,11 @@ public async Task Can_Get_Value_From_ContentPicker() [Test] public async Task Can_Get_Value_From_Updated_ContentPicker() { + // Arrange var template = TemplateBuilder.CreateTextPageTemplate(); await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey); - var textPage = await CreateTextPageDocument(template.Id); - var contentPickerDocument = await CreateContentPickerDocument(template.Id, textPage.Key); + var textPage = await CreateTextPageDocument(template.Key); + var contentPickerDocument = await CreateContentPickerDocument(template.Key, textPage.Key); // Get for caching var notUpdatedContent = await CacheManager.Content.GetByIdAsync(contentPickerDocument.Id); @@ -88,46 +92,42 @@ public async Task Can_Get_Value_From_Updated_ContentPicker() Assert.IsTrue(publishResult); + // Act var contentPickerPage = await CacheManager.Content.GetByIdAsync(contentPickerDocument.Id); - IPublishedContent updatedPickerValue = (IPublishedContent)contentPickerPage.Value("contentPicker"); - + // Assert + IPublishedContent updatedPickerValue = (IPublishedContent)contentPickerPage.Value("contentPicker"); Assert.AreEqual(textPage.Key, updatedPickerValue.Key); Assert.AreEqual(textPage.Id, updatedPickerValue.Id); Assert.AreEqual(textPage.Name, updatedPickerValue.Name); Assert.AreEqual("Updated title", updatedPickerValue.Properties.First(x => x.Alias == "title").GetValue()); } - private async Task CreateContentPickerDocument(int templateId, Guid textPageKey) + private async Task CreateContentPickerDocument(Guid templateKey, Guid textPageKey) { - var builder = new ContentTypeBuilder(); - var pickerContentType = (ContentType)builder + var builder = new ContentTypeEditingBuilder(); + var pickerContentType = builder .WithAlias("test") .WithName("TestName") - .AddAllowedTemplate() - .WithId(templateId) - .Done() + .WithAllowAtRoot(true) + .AddAllowedTemplateKeys([templateKey]) .AddPropertyGroup() - .WithName("Content") - .WithSupportsPublishing(true) + .WithName("Content") + .Done() .AddPropertyType() - .WithAlias("contentPicker") - .WithName("Content Picker") - .WithDataTypeId(1046) - .WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.ContentPicker) - .WithValueStorageType(ValueStorageType.Integer) - .WithSortOrder(16) - .Done() - .Done() + .WithAlias("contentPicker") + .WithName("Content Picker") + .WithDataTypeKey(Constants.DataTypes.Guids.ContentPickerGuid) + .WithSortOrder(16) + .Done() .Build(); - pickerContentType.AllowedAsRoot = true; - ContentTypeService.Save(pickerContentType); + await ContentTypeEditingService.CreateAsync(pickerContentType, Constants.Security.SuperUserKey); var createOtherModel = new ContentCreateModel { - ContentTypeKey = pickerContentType.Key, + ContentTypeKey = pickerContentType.Key.Value, ParentKey = Constants.System.RootKey, InvariantName = "Test Create", InvariantProperties = new[] { new PropertyValueModel { Alias = "contentPicker", Value = textPageKey }, }, @@ -149,15 +149,14 @@ private async Task CreateContentPickerDocument(int templateId, Guid te return result.Result.Content; } - private async Task CreateTextPageDocument(int templateId) + private async Task CreateTextPageDocument(Guid templateKey) { - var textContentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: templateId); - textContentType.AllowedAsRoot = true; - ContentTypeService.Save(textContentType); + var textContentType = ContentTypeEditingBuilder.CreateTextPageContentType(defaultTemplateKey: templateKey); + await ContentTypeEditingService.CreateAsync(textContentType, Constants.Security.SuperUserKey); var createModel = new ContentCreateModel { - ContentTypeKey = textContentType.Key, + ContentTypeKey = textContentType.Key.Value, ParentKey = Constants.System.RootKey, InvariantName = "Root Create", InvariantProperties = new[] diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheTemplateTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheTemplateTests.cs new file mode 100644 index 000000000000..d7d04b64fbac --- /dev/null +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheTemplateTests.cs @@ -0,0 +1,44 @@ +using NUnit.Framework; +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Core.PublishedCache; +using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Core.Services.OperationStatus; +using Umbraco.Cms.Tests.Common.Testing; +using Umbraco.Cms.Tests.Integration.Testing; + +namespace Umbraco.Cms.Tests.Integration.Umbraco.PublishedCache.HybridCache; + +[TestFixture] +[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] +[Platform("Linux", Reason = "This uses too much memory when running both caches, should be removed when nuchache is removed")] +public class DocumentHybridCacheTemplateTests : UmbracoIntegrationTestWithContentEditing +{ + protected override void CustomTestSetup(IUmbracoBuilder builder) => builder.AddUmbracoHybridCache(); + + private IPublishedContentCache PublishedContentHybridCache => GetRequiredService(); + + private IContentEditingService ContentEditingService => GetRequiredService(); + + [Test] + public async Task Can_Get_Document_After_Removing_Template() + { + // Arrange + var textPageBefore = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); + Assert.AreEqual(textPageBefore.TemplateId, TemplateId); + var updateModel = new ContentUpdateModel(); + { + updateModel.TemplateKey = null; + updateModel.InvariantName = textPageBefore.Name; + } + + // Act + var updateContentResult = await ContentEditingService.UpdateAsync(textPageBefore.Key, updateModel, Constants.Security.SuperUserKey); + + // Assert + Assert.AreEqual(updateContentResult.Status, ContentEditingOperationStatus.Success); + var textPageAfter = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); + // Should this not be null? + Assert.AreEqual(textPageAfter.TemplateId, null); + } +} diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheTests.cs index 7d8d4123e1f8..bf2bfaddb4fa 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheTests.cs @@ -25,16 +25,13 @@ public class DocumentHybridCacheTests : UmbracoIntegrationTestWithContentEditing 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 + // Act var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); - //Assert + // Assert AssertTextPage(textPage); } @@ -51,54 +48,42 @@ public async Task Can_Get_Draft_Content_By_Key() [Test] public async Task Can_Get_Published_Content_By_Id() { - // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId); // Assert - AssertTextPage(textPage); + AssertPublishedTextPage(textPage); } [Test] public async Task Can_Get_Published_Content_By_Key() { - // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value); // Assert - AssertTextPage(textPage); + AssertPublishedTextPage(textPage); } [Test] public async Task Can_Get_Draft_Of_Published_Content_By_Id() { - // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId, true); // Assert - AssertTextPage(textPage); + AssertPublishedTextPage(textPage); Assert.IsFalse(textPage.IsPublished()); } [Test] public async Task Can_Get_Draft_Of_Published_Content_By_Key() { - // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value, true); // Assert - AssertTextPage(textPage); + AssertPublishedTextPage(textPage); Assert.IsFalse(textPage.IsPublished()); } @@ -151,19 +136,18 @@ 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 - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - Textpage.InvariantName = NewName; + PublishedTextPage.InvariantName = NewName; ContentUpdateModel updateModel = new ContentUpdateModel { InvariantName = NewName, - InvariantProperties = Textpage.InvariantProperties, - Variants = Textpage.Variants, - TemplateKey = Textpage.TemplateKey, + InvariantProperties = PublishedTextPage.InvariantProperties, + Variants = PublishedTextPage.Variants, + TemplateKey = PublishedTextPage.TemplateKey, }; - await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); + await ContentEditingService.UpdateAsync(PublishedTextPage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId, preview); // Assert Assert.AreEqual(result, NewName.Equals(textPage.Name)); @@ -176,22 +160,18 @@ 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 - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - - Textpage.InvariantName = NewName; - + PublishedTextPage.InvariantName = NewName; ContentUpdateModel updateModel = new ContentUpdateModel { InvariantName = NewName, - InvariantProperties = Textpage.InvariantProperties, - Variants = Textpage.Variants, - TemplateKey = Textpage.TemplateKey, + InvariantProperties = PublishedTextPage.InvariantProperties, + Variants = PublishedTextPage.Variants, + TemplateKey = PublishedTextPage.TemplateKey, }; - - await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); + await ContentEditingService.UpdateAsync(PublishedTextPage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value, preview); // Assert Assert.AreEqual(result, NewName.Equals(textPage.Name)); @@ -227,11 +207,10 @@ public async Task Can_Get_Draft_Content_Property_By_Key() public async Task Can_Get_Published_Content_Property_By_Id() { // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; + var titleValue = PublishedTextPage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId, true); // Assert Assert.AreEqual(titleValue, textPage.Value("title")); @@ -241,11 +220,10 @@ public async Task Can_Get_Published_Content_Property_By_Id() public async Task Can_Get_Published_Content_Property_By_Key() { // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; + var titleValue = PublishedTextPage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value, true); // Assert Assert.AreEqual(titleValue, textPage.Value("title")); @@ -255,11 +233,10 @@ public async Task Can_Get_Published_Content_Property_By_Key() public async Task Can_Get_Draft_Of_Published_Content_Property_By_Id() { // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; + var titleValue = PublishedTextPage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId, true); // Assert Assert.AreEqual(titleValue, textPage.Value("title")); @@ -269,11 +246,10 @@ 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 - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - var titleValue = Textpage.InvariantProperties.First(x => x.Alias == "title").Value; + var titleValue = PublishedTextPage.InvariantProperties.First(x => x.Alias == "title").Value; // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value, true); // Assert Assert.AreEqual(titleValue, textPage.Value("title")); @@ -284,7 +260,6 @@ public async Task Can_Get_Updated_Draft_Content_Property_By_Id() { // Arrange Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; - ContentUpdateModel updateModel = new ContentUpdateModel { InvariantName = Textpage.InvariantName, @@ -292,7 +267,6 @@ public async Task Can_Get_Updated_Draft_Content_Property_By_Id() Variants = Textpage.Variants, TemplateKey = Textpage.TemplateKey, }; - await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act @@ -307,7 +281,6 @@ public async Task Can_Get_Updated_Draft_Content_Property_By_Key() { // Arrange Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; - ContentUpdateModel updateModel = new ContentUpdateModel { InvariantName = Textpage.InvariantName, @@ -315,7 +288,6 @@ public async Task Can_Get_Updated_Draft_Content_Property_By_Key() Variants = Textpage.Variants, TemplateKey = Textpage.TemplateKey, }; - await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act @@ -329,21 +301,19 @@ public async Task Can_Get_Updated_Draft_Content_Property_By_Key() public async Task Can_Get_Updated_Published_Content_Property_By_Id() { // Arrange - Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; - + PublishedTextPage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; ContentUpdateModel updateModel = new ContentUpdateModel { - InvariantName = Textpage.InvariantName, - InvariantProperties = Textpage.InvariantProperties, - Variants = Textpage.Variants, - TemplateKey = Textpage.TemplateKey, + InvariantName = PublishedTextPage.InvariantName, + InvariantProperties = PublishedTextPage.InvariantProperties, + Variants = PublishedTextPage.Variants, + TemplateKey = PublishedTextPage.TemplateKey, }; - - await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); + await ContentEditingService.UpdateAsync(PublishedTextPage.Key.Value, updateModel, Constants.Security.SuperUserKey); + await ContentPublishingService.PublishAsync(PublishedTextPage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value, true); // Assert Assert.AreEqual(NewTitle, textPage.Value("title")); @@ -353,21 +323,19 @@ public async Task Can_Get_Updated_Published_Content_Property_By_Id() public async Task Can_Get_Updated_Published_Content_Property_By_Key() { // Arrange - Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; - + PublishedTextPage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; ContentUpdateModel updateModel = new ContentUpdateModel { - InvariantName = Textpage.InvariantName, - InvariantProperties = Textpage.InvariantProperties, - Variants = Textpage.Variants, - TemplateKey = Textpage.TemplateKey, + InvariantName = PublishedTextPage.InvariantName, + InvariantProperties = PublishedTextPage.InvariantProperties, + Variants = PublishedTextPage.Variants, + TemplateKey = PublishedTextPage.TemplateKey, }; - - await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); + await ContentEditingService.UpdateAsync(PublishedTextPage.Key.Value, updateModel, Constants.Security.SuperUserKey); + await ContentPublishingService.PublishAsync(PublishedTextPage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value); // Assert Assert.AreEqual(NewTitle, textPage.Value("title")); @@ -379,21 +347,18 @@ public async Task Can_Get_Updated_Published_Content_Property_By_Key() public async Task Can_Get_Updated_Draft_Of_Published_Content_Property_By_Id(bool preview, string titleName) { // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - Textpage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; - + PublishedTextPage.InvariantProperties.First(x => x.Alias == "title").Value = NewTitle; ContentUpdateModel updateModel = new ContentUpdateModel { - InvariantName = Textpage.InvariantName, - InvariantProperties = Textpage.InvariantProperties, - Variants = Textpage.Variants, - TemplateKey = Textpage.TemplateKey, + InvariantName = PublishedTextPage.InvariantName, + InvariantProperties = PublishedTextPage.InvariantProperties, + Variants = PublishedTextPage.Variants, + TemplateKey = PublishedTextPage.TemplateKey, }; - - await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); + await ContentEditingService.UpdateAsync(PublishedTextPage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId, preview); // Assert Assert.AreEqual(titleName, textPage.Value("title")); @@ -405,21 +370,18 @@ public async Task Can_Get_Updated_Draft_Of_Published_Content_Property_By_Id(bool public async Task Can_Get_Updated_Draft_Of_Published_Content_Property_By_Key(bool preview, string titleName) { // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - Textpage.InvariantProperties.First(x => x.Alias == "title").Value = titleName; - + PublishedTextPage.InvariantProperties.First(x => x.Alias == "title").Value = titleName; ContentUpdateModel updateModel = new ContentUpdateModel { - InvariantName = Textpage.InvariantName, - InvariantProperties = Textpage.InvariantProperties, - Variants = Textpage.Variants, - TemplateKey = Textpage.TemplateKey, + InvariantName = PublishedTextPage.InvariantName, + InvariantProperties = PublishedTextPage.InvariantProperties, + Variants = PublishedTextPage.Variants, + TemplateKey = PublishedTextPage.TemplateKey, }; - - await ContentEditingService.UpdateAsync(Textpage.Key.Value, updateModel, Constants.Security.SuperUserKey); + await ContentEditingService.UpdateAsync(PublishedTextPage.Key.Value, updateModel, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value, true); // Assert Assert.AreEqual(titleName, textPage.Value("title")); @@ -429,12 +391,14 @@ public async Task Can_Get_Updated_Draft_Of_Published_Content_Property_By_Key(boo public async Task Can_Not_Get_Deleted_Content_By_Id() { // Arrange - var content = await PublishedContentHybridCache.GetByIdAsync(Subpage3Id, true); + var content = await PublishedContentHybridCache.GetByIdAsync(Subpage1Id, true); Assert.IsNotNull(content); - await ContentEditingService.DeleteAsync(Subpage3.Key.Value, Constants.Security.SuperUserKey); + await ContentEditingService.DeleteAsync(Subpage1.Key.Value, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Subpage3Id, true); + var textPagePublishedContent = await PublishedContentHybridCache.GetByIdAsync(Subpage1Id, false); + + var textPage = await PublishedContentHybridCache.GetByIdAsync(Subpage1Id, true); // Assert Assert.IsNull(textPage); @@ -444,11 +408,13 @@ 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.Value, true); - var result = await ContentEditingService.DeleteAsync(Subpage3.Key.Value, Constants.Security.SuperUserKey); + await PublishedContentHybridCache.GetByIdAsync(Subpage1.Key.Value, true); + var hasContent = await PublishedContentHybridCache.GetByIdAsync(Subpage1Id, true); + Assert.IsNotNull(hasContent); + await ContentEditingService.DeleteAsync(Subpage1.Key.Value, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Subpage3.Key.Value, true); + var textPage = await PublishedContentHybridCache.GetByIdAsync(Subpage1.Key.Value, true); // Assert Assert.IsNull(textPage); @@ -460,11 +426,10 @@ public async Task Can_Not_Get_Deleted_Content_By_Key() public async Task Can_Not_Get_Deleted_Published_Content_By_Id(bool preview) { // Arrange - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - await ContentEditingService.DeleteAsync(Textpage.Key.Value, Constants.Security.SuperUserKey); + await ContentEditingService.DeleteAsync(PublishedTextPage.Key.Value, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(TextpageId, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPageId, preview); // Assert Assert.IsNull(textPage); @@ -476,11 +441,10 @@ 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 - await ContentPublishingService.PublishAsync(Textpage.Key.Value, CultureAndSchedule, Constants.Security.SuperUserKey); - await ContentEditingService.DeleteAsync(Textpage.Key.Value, Constants.Security.SuperUserKey); + await ContentEditingService.DeleteAsync(PublishedTextPage.Key.Value, Constants.Security.SuperUserKey); // Act - var textPage = await PublishedContentHybridCache.GetByIdAsync(Textpage.Key.Value, preview); + var textPage = await PublishedContentHybridCache.GetByIdAsync(PublishedTextPage.Key.Value, preview); // Assert Assert.IsNull(textPage); @@ -499,6 +463,19 @@ private void AssertTextPage(IPublishedContent textPage) AssertProperties(Textpage.InvariantProperties, textPage.Properties); } + private void AssertPublishedTextPage(IPublishedContent textPage) + { + Assert.Multiple(() => + { + Assert.IsNotNull(textPage); + Assert.AreEqual(PublishedTextPage.Key, textPage.Key); + Assert.AreEqual(PublishedTextPage.ContentTypeKey, textPage.ContentType.Key); + Assert.AreEqual(PublishedTextPage.InvariantName, textPage.Name); + }); + + AssertProperties(PublishedTextPage.InvariantProperties, textPage.Properties); + } + private void AssertProperties(IEnumerable propertyCollection, IEnumerable publishedProperties) { foreach (var prop in propertyCollection) diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheVariantsTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheVariantsTests.cs index 8f06be20c30e..34e69c03448d 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheVariantsTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheVariantsTests.cs @@ -4,6 +4,7 @@ using Umbraco.Cms.Core.Models.ContentEditing; using Umbraco.Cms.Core.PublishedCache; using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Core.Services.ContentTypeEditing; using Umbraco.Cms.Core.Web; using Umbraco.Cms.Tests.Common.Builders; using Umbraco.Cms.Tests.Common.Builders.Extensions; @@ -24,12 +25,12 @@ public class DocumentHybridCacheVariantsTests : UmbracoIntegrationTest private string _invariantTitleAlias = "invariantTitle"; private string _invariantTitleName = "Invariant Title"; - private IContentTypeService ContentTypeService => GetRequiredService(); - private ILanguageService LanguageService => GetRequiredService(); private IContentEditingService ContentEditingService => GetRequiredService(); + private IContentTypeEditingService ContentTypeEditingService => GetRequiredService(); + private IUmbracoContextFactory UmbracoContextFactory => GetRequiredService(); private IPublishedContentCache PublishedContentHybridCache => GetRequiredService(); @@ -49,31 +50,35 @@ public async Task Can_Set_Invariant_Title() var updatedInvariantTitle = "Updated Invariant Title"; var updatedVariantTitle = "Updated Variant Title"; - var updateModel = new ContentUpdateModel { - InvariantProperties = new[] - { - new PropertyValueModel { Alias = _invariantTitleAlias, Value = updatedInvariantTitle } - }, - Variants = new [] + InvariantProperties = + new[] { new PropertyValueModel { Alias = _invariantTitleAlias, Value = updatedInvariantTitle } }, + Variants = new[] { new VariantModel { Culture = _englishIsoCode, Name = "Updated English Name", - Properties = new [] - { - new PropertyValueModel { Alias = _variantTitleAlias, Value = updatedVariantTitle } - } + Properties = + new[] + { + new PropertyValueModel + { + Alias = _variantTitleAlias, Value = updatedVariantTitle + } + }, }, new VariantModel { Culture = _danishIsoCode, Name = "Updated Danish Name", - Properties = new [] + Properties = new[] { - new PropertyValueModel { Alias = _variantTitleAlias, Value = updatedVariantTitle } + new PropertyValueModel + { + Alias = _variantTitleAlias, Value = updatedVariantTitle + }, }, }, }, @@ -100,28 +105,29 @@ public async Task Can_Set_Invariant_Title_On_One_Culture() var updatedInvariantTitle = "Updated Invariant Title"; var updatedVariantTitle = "Updated Invariant Title"; - var updateModel = new ContentUpdateModel { - InvariantProperties = new[] - { - new PropertyValueModel { Alias = _invariantTitleAlias, Value = updatedInvariantTitle } - }, - Variants = new [] + InvariantProperties = + new[] { new PropertyValueModel { Alias = _invariantTitleAlias, Value = updatedInvariantTitle } }, + Variants = new[] { new VariantModel { Culture = _englishIsoCode, Name = "Updated English Name", - Properties = new [] + Properties = new[] { - new PropertyValueModel { Alias = _variantTitleAlias, Value = updatedVariantTitle } - } + new PropertyValueModel + { + Alias = _variantTitleAlias, Value = updatedVariantTitle + }, + }, }, }, }; - var result = await ContentEditingService.UpdateAsync(VariantPage.Key, updateModel, Constants.Security.SuperUserKey); + var result = + await ContentEditingService.UpdateAsync(VariantPage.Key, updateModel, Constants.Security.SuperUserKey); Assert.IsTrue(result.Success); // Act @@ -134,59 +140,42 @@ public async Task Can_Set_Invariant_Title_On_One_Culture() Assert.AreEqual(_variantTitleName, textPage.Value(_variantTitleAlias, _danishIsoCode)); } - private async Task CreateTestData() { - // NOTE Maybe not the best way to create/save test data as we are using the services, which are being tested. var language = new LanguageBuilder() .WithCultureInfo(_danishIsoCode) .Build(); - await LanguageService.CreateAsync(language, Constants.Security.SuperUserKey); - var contentType = new ContentTypeBuilder() + var groupKey = Guid.NewGuid(); + var contentType = new ContentTypeEditingBuilder() .WithAlias("cultureVariationTest") .WithName("Culture Variation Test") - .WithContentVariation(ContentVariation.Culture) + .WithAllowAtRoot(true) + .WithVariesByCulture(true) .AddPropertyType() - .WithAlias(_variantTitleAlias) - .WithName(_variantTitleName) - .WithVariations(ContentVariation.Culture) - .Done() + .WithAlias(_variantTitleAlias) + .WithName(_variantTitleName) + .WithVariesByCulture(true) + .WithContainerKey(groupKey) + .Done() .AddPropertyType() - .WithAlias(_invariantTitleAlias) - .WithName(_invariantTitleName) - .WithVariations(ContentVariation.Nothing) - .Done() + .WithAlias(_invariantTitleAlias) + .WithName(_invariantTitleName) + .WithContainerKey(groupKey) + .Done() + .AddPropertyGroup() + .WithName("content") + .WithKey(groupKey) + .Done() .Build(); - contentType.AllowedAsRoot = true; - ContentTypeService.Save(contentType); - var rootContentCreateModel = new ContentCreateModel + var contentTypeAttempt = await ContentTypeEditingService.CreateAsync(contentType, Constants.Security.SuperUserKey); + if (!contentTypeAttempt.Success) { - ContentTypeKey = contentType.Key, - Variants = new[] - { - new VariantModel - { - Culture = "en-US", - Name = "English Page", - Properties = new [] - { - new PropertyValueModel { Alias = _variantTitleAlias, Value = _variantTitleName } - }, - }, - new VariantModel - { - Culture = "da-DK", - Name = "Danish Page", - Properties = new [] - { - new PropertyValueModel { Alias = _variantTitleAlias, Value = _variantTitleName } - }, - }, - }, - }; + throw new Exception("Failed to create content type"); + } + var rootContentCreateModel = ContentEditingBuilder.CreateContentWithTwoVariantProperties(contentTypeAttempt.Result.Key, "en-US", "da-DK", _variantTitleAlias, _variantTitleName); var result = await ContentEditingService.CreateAsync(rootContentCreateModel, Constants.Security.SuperUserKey); VariantPage = result.Result.Content; }