Skip to content

Commit

Permalink
V15 updated the hybrid caching integration tests to use ContentEditin…
Browse files Browse the repository at this point in the history
…gService (#16947)

* Added builder extension withParentKey

* Created builder with ContentEditingService

* Added usage of the ContentEditingService to SETUP

* Started using ContentEditingService builder in tests

* Updated builder extensions

* Fixed builder

* Clean up

* Clean up, not done

* Added Ids
  • Loading branch information
andr317c authored Aug 22, 2024
1 parent 89cb8ff commit 0e9067d
Show file tree
Hide file tree
Showing 10 changed files with 619 additions and 114 deletions.
172 changes: 172 additions & 0 deletions tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Copyright (c) Umbraco.

Check warning on line 1 in tests/Umbraco.Tests.Common/Builders/ContentEditingBuilder.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

❌ New issue: Primitive Obsession

In this module, 68.8% of all function arguments are primitive types, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
// See LICENSE for more details.

using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.Builders.Interfaces;
using Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel;

namespace Umbraco.Cms.Tests.Common.Builders;

public class ContentEditingBuilder
: BuilderBase<ContentCreateModel>,
IWithInvariantNameBuilder,
IWithInvariantPropertiesBuilder,
IWithVariantsBuilder,
IWithKeyBuilder,
IWithContentTypeKeyBuilder,
IWithParentKeyBuilder,
IWithTemplateKeyBuilder
{
private IContentType _contentType;
private ContentTypeBuilder _contentTypeBuilder;
private IEnumerable<PropertyValueModel> _invariantProperties = [];
private IEnumerable<VariantModel> _variants = [];
private Guid _contentTypeKey;
private Guid? _parentKey;
private Guid? _templateKey;
private Guid? _key;
private string _invariantName;

Guid? IWithKeyBuilder.Key
{
get => _key;
set => _key = value;
}

string IWithInvariantNameBuilder.InvariantName
{
get => _invariantName;
set => _invariantName = value;
}

IEnumerable<PropertyValueModel> IWithInvariantPropertiesBuilder.InvariantProperties
{
get => _invariantProperties;
set => _invariantProperties = value;
}

IEnumerable<VariantModel> IWithVariantsBuilder.Variants
{
get => _variants;
set => _variants = value;
}

Guid? IWithParentKeyBuilder.ParentKey
{
get => _parentKey;
set => _parentKey = value;
}

Guid IWithContentTypeKeyBuilder.ContentTypeKey
{
get => _contentTypeKey;
set => _contentTypeKey = value;
}

Guid? IWithTemplateKeyBuilder.TemplateKey
{
get => _templateKey;
set => _templateKey = value;
}

public ContentEditingBuilder WithInvariantName(string invariantName)
{
_invariantName = invariantName;
return this;
}

public ContentEditingBuilder WithInvariantProperty(string alias, object value)
{
var property = new PropertyValueModel { Alias = alias, Value = value };
_invariantProperties = _invariantProperties.Concat(new[] { property });
return this;
}

public ContentEditingBuilder AddVariant(string culture, string segment, string name,
IEnumerable<PropertyValueModel> properties)
{
var variant = new VariantModel { Culture = culture, Segment = segment, Name = name, Properties = properties };
_variants = _variants.Concat(new[] { variant });
return this;
}

public ContentEditingBuilder WithParentKey(Guid parentKey)
{
_parentKey = parentKey;
return this;
}

public ContentEditingBuilder WithTemplateKey(Guid templateKey)
{
_templateKey = templateKey;
return this;
}

public ContentEditingBuilder WithContentType(IContentType contentType)
{
_contentTypeBuilder = null;
_contentType = contentType;
return this;
}

public override ContentCreateModel Build()
{
var key = _key ?? Guid.NewGuid();
var parentKey = _parentKey;
var templateKey = _templateKey;
var invariantName = _invariantName ?? Guid.NewGuid().ToString();
var invariantProperties = _invariantProperties;
var variants = _variants;

if (_contentTypeBuilder is null && _contentType is null)
{
throw new InvalidOperationException(
"A content item cannot be constructed without providing a content type. Use AddContentType() or WithContentType().");
}

var contentType = _contentType ?? _contentTypeBuilder.Build();
var content = new ContentCreateModel();

content.InvariantName = invariantName;
if (parentKey is not null)
{
content.ParentKey = parentKey;
}

if (templateKey is not null)
{
content.TemplateKey = templateKey;
}

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

return content;
}

public static ContentCreateModel CreateBasicContent(IContentType contentType, Guid? key) =>
new ContentEditingBuilder()
.WithKey(key)
.WithContentType(contentType)
.WithInvariantName("Home")
.Build();

public static ContentCreateModel CreateSimpleContent(IContentType contentType) =>
new ContentEditingBuilder()
.WithContentType(contentType)
.WithInvariantName("Home")
.WithInvariantProperty("title", "Welcome to our Home page")
.Build();

public static ContentCreateModel CreateSimpleContent(IContentType contentType, string name, Guid? parentKey) =>
new ContentEditingBuilder()
.WithContentType(contentType)
.WithInvariantName(name)
.WithParentKey(parentKey)
.WithInvariantProperty("title", "Welcome to our Home page")
.Build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Tests.Common.Builders.Interfaces;
using Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel;

namespace Umbraco.Cms.Tests.Common.Builders.Extensions;

public static class ContentEditingBuilderExtensions
{
public static T WithInvariantName<T>(this T Builder, string invariantName)
where T : IWithInvariantNameBuilder
{
Builder.InvariantName = invariantName;
return Builder;
}

public static T WithInvariantProperties<T>(this T Builder, IEnumerable<PropertyValueModel> invariantProperties)
where T : IWithInvariantPropertiesBuilder
{
Builder.InvariantProperties = invariantProperties;
return Builder;
}

public static T WithVariants<T>(this T Builder, IEnumerable<VariantModel> variants)
where T : IWithVariantsBuilder
{
Builder.Variants = variants;
return Builder;
}

public static T WithKey<T>(this T Builder, Guid? key)
where T : IWithKeyBuilder
{
Builder.Key = key;
return Builder;
}

public static T WithContentTypeKey<T>(this T Builder, Guid contentTypeKey)
where T : IWithContentTypeKeyBuilder
{
Builder.ContentTypeKey = contentTypeKey;
return Builder;
}

public static T WithParentKey<T>(this T Builder, Guid? parentKey)
where T : IWithParentKeyBuilder
{
Builder.ParentKey = parentKey;
return Builder;
}


public static T WithTemplateKey<T>(this T Builder, Guid? templateKey)
where T : IWithTemplateKeyBuilder
{
Builder.TemplateKey = templateKey;
return Builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel;

public interface IWithContentTypeKeyBuilder
{
public Guid ContentTypeKey { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel;

public interface IWithInvariantNameBuilder
{
public string? InvariantName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Umbraco.Cms.Core.Models.ContentEditing;

namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel;

public interface IWithInvariantPropertiesBuilder
{
public IEnumerable<PropertyValueModel> InvariantProperties { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.

namespace Umbraco.Cms.Tests.Common.Builders.Interfaces;

public interface IWithParentKeyBuilder
{
Guid? ParentKey { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel;

public interface IWithTemplateKeyBuilder
{
public Guid? TemplateKey { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Umbraco.Cms.Core.Models.ContentEditing;

namespace Umbraco.Cms.Tests.Common.Builders.Interfaces.ContentCreateModel;

public interface IWithVariantsBuilder
{
public IEnumerable<VariantModel> Variants { get; set; }
}
Loading

0 comments on commit 0e9067d

Please sign in to comment.