Skip to content

Commit

Permalink
Merge pull request #17095 from umbraco/v15/QA/hybrid-caching-tests
Browse files Browse the repository at this point in the history
V15 QA hybrid caching integration tests
  • Loading branch information
nikolajlauridsen authored Sep 19, 2024
2 parents 847dc3e + 92b9e24 commit 714e446
Show file tree
Hide file tree
Showing 28 changed files with 1,077 additions and 305 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
53 changes: 25 additions & 28 deletions tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,10 +16,10 @@ public class ContentEditingBuilder
IWithKeyBuilder,
IWithContentTypeKeyBuilder,
IWithParentKeyBuilder,
IWithTemplateKeyBuilder
IWithTemplateKeyBuilder,
IBuildContentTypes
{
private IContentType _contentType;
private ContentTypeBuilder _contentTypeBuilder;
private ContentTypeEditingBuilder _contentTypeEditingBuilder;
private IEnumerable<PropertyValueModel> _invariantProperties = [];
private IEnumerable<VariantModel> _variants = [];
private Guid _contentTypeKey;
Expand Down Expand Up @@ -84,8 +83,7 @@ public ContentEditingBuilder WithInvariantProperty(string alias, object value)
return this;
}

public ContentEditingBuilder AddVariant(string culture, string segment, string name,
IEnumerable<PropertyValueModel> properties)
public ContentEditingBuilder AddVariant(string culture, string segment, string name, IEnumerable<PropertyValueModel> properties)
{
var variant = new VariantModel { Culture = culture, Segment = segment, Name = name, Properties = properties };
_variants = _variants.Concat(new[] { variant });
Expand All @@ -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();
Expand All @@ -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)
{
Expand All @@ -140,33 +123,47 @@ public override ContentCreateModel Build()
content.TemplateKey = templateKey;
}

content.ContentTypeKey = contentType.Key;
content.ContentTypeKey = _contentTypeKey;
content.Key = key;
content.InvariantProperties = invariantProperties;
content.Variants = variants;

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();
}
240 changes: 240 additions & 0 deletions tests/Umbraco.Tests.Common/Builders/ContentTypeEditingBuilder.cs
Original file line number Diff line number Diff line change
@@ -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<ContentEditingBuilder, ContentTypeCreateModel>,
IBuildPropertyTypes
{
private Guid? _key;
private Guid? _containerKey;
private ContentTypeCleanup _cleanup = new();
private IEnumerable<Guid> _allowedTemplateKeys;
private Guid? _defaultTemplateKey;
private bool? _allowAtRoot;
private bool? _isElement;
private bool? _variesByCulture;
private bool? _variesBySegment;
private readonly List<PropertyTypeEditingBuilder> _propertyTypeBuilders = [];
private readonly List<PropertyTypeContainerBuilder<ContentTypeEditingBuilder>> _propertyTypeContainerBuilders = [];
private readonly List<ContentTypeSortBuilder> _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<ContentTypeEditingBuilder> AddPropertyGroup()
{
var builder = new PropertyTypeContainerBuilder<ContentTypeEditingBuilder>(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<Guid> 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<Guid>();
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();
}
}
5 changes: 5 additions & 0 deletions tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public ContentTypeSortBuilder(ContentTypeBuilder parentBuilder)
{
}

public ContentTypeSortBuilder(ContentTypeEditingBuilder parentBuilder)
: base(null)
{
}

string IWithAliasBuilder.Alias
{
get => _alias;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public static T WithKey<T>(this T builder, Guid key)
return builder;
}

public static T WithDataTypeKey<T>(this T builder, Guid key)
where T : IWithDataTypeKeyBuilder
{
builder.DataTypeKey = key;
return builder;
}

public static T WithParentId<T>(this T builder, int parentId)
where T : IWithParentIdBuilder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Umbraco.Cms.Tests.Common.Builders.Interfaces;

public interface IWIthContainerKeyBuilder
{
Guid? ContainerKey { get; set; }
}
Loading

0 comments on commit 714e446

Please sign in to comment.