Retrofit ContentEditingBaseBuilder to match new create/update models (#19129)
This commit is contained in:
@@ -6,7 +6,6 @@ using Umbraco.Extensions;
|
||||
namespace Umbraco.Cms.Tests.Common.Builders;
|
||||
|
||||
public abstract class ContentEditingBaseBuilder<TCreateModel> : BuilderBase<TCreateModel>,
|
||||
IWithInvariantNameBuilder,
|
||||
IWithKeyBuilder,
|
||||
IWithContentTypeKeyBuilder,
|
||||
IWithParentKeyBuilder,
|
||||
@@ -14,12 +13,11 @@ public abstract class ContentEditingBaseBuilder<TCreateModel> : BuilderBase<TCre
|
||||
where TCreateModel : ContentCreationModelBase, new()
|
||||
{
|
||||
protected TCreateModel _model = new();
|
||||
private List<ContentEditingPropertyValueBuilder<ContentEditingBaseBuilder<TCreateModel>>> _invariantProperties = [];
|
||||
private List<ContentEditingPropertyValueBuilder<ContentEditingBaseBuilder<TCreateModel>>> _properties = [];
|
||||
private List<ContentEditingVariantBuilder<ContentEditingBaseBuilder<TCreateModel>>> _variants = [];
|
||||
private Guid _contentTypeKey;
|
||||
private Guid? _parentKey;
|
||||
private Guid? _key;
|
||||
private string _invariantName;
|
||||
|
||||
Guid? IWithKeyBuilder.Key
|
||||
{
|
||||
@@ -27,12 +25,6 @@ public abstract class ContentEditingBaseBuilder<TCreateModel> : BuilderBase<TCre
|
||||
set => _key = value;
|
||||
}
|
||||
|
||||
string IWithInvariantNameBuilder.InvariantName
|
||||
{
|
||||
get => _invariantName;
|
||||
set => _invariantName = value;
|
||||
}
|
||||
|
||||
Guid? IWithParentKeyBuilder.ParentKey
|
||||
{
|
||||
get => _parentKey;
|
||||
@@ -45,13 +37,6 @@ public abstract class ContentEditingBaseBuilder<TCreateModel> : BuilderBase<TCre
|
||||
set => _contentTypeKey = value;
|
||||
}
|
||||
|
||||
public ContentEditingPropertyValueBuilder<ContentEditingBaseBuilder<TCreateModel>> AddInvariantProperty()
|
||||
{
|
||||
var builder = new ContentEditingPropertyValueBuilder<ContentEditingBaseBuilder<TCreateModel>>(this);
|
||||
_invariantProperties.Add(builder);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public ContentEditingVariantBuilder<ContentEditingBaseBuilder<TCreateModel>> AddVariant()
|
||||
{
|
||||
var builder = new ContentEditingVariantBuilder<ContentEditingBaseBuilder<TCreateModel>>(this);
|
||||
@@ -59,8 +44,20 @@ public abstract class ContentEditingBaseBuilder<TCreateModel> : BuilderBase<TCre
|
||||
return builder;
|
||||
}
|
||||
|
||||
public ContentEditingPropertyValueBuilder<ContentEditingBaseBuilder<TCreateModel>> AddProperty()
|
||||
{
|
||||
var builder = new ContentEditingPropertyValueBuilder<ContentEditingBaseBuilder<TCreateModel>>(this);
|
||||
_properties.Add(builder);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public override TCreateModel Build()
|
||||
{
|
||||
if (_variants.Any() is false)
|
||||
{
|
||||
throw new InvalidOperationException("Expected at least one variant (invariant is also a variant).");
|
||||
}
|
||||
|
||||
if (_parentKey is not null)
|
||||
{
|
||||
_model.ParentKey = _parentKey;
|
||||
@@ -68,21 +65,7 @@ public abstract class ContentEditingBaseBuilder<TCreateModel> : BuilderBase<TCre
|
||||
|
||||
_model.ContentTypeKey = _contentTypeKey;
|
||||
_model.Key = _key ?? Guid.NewGuid();
|
||||
_model.Properties = _invariantProperties
|
||||
.Select(p => p.Build())
|
||||
.Union(_variants.SelectMany(variant => variant.GetProperties().Select(p => p.Build())))
|
||||
.ToArray();
|
||||
|
||||
if (_invariantName.IsNullOrWhiteSpace() is false)
|
||||
{
|
||||
if (_variants.Any())
|
||||
{
|
||||
throw new InvalidOperationException("Cannot combine invariant and variant variants.");
|
||||
}
|
||||
|
||||
AddVariant().WithName(_invariantName);
|
||||
}
|
||||
|
||||
_model.Properties = _properties.Select(p => p.Build()).ToArray();
|
||||
_model.Variants = _variants.Select(x => x.Build()).ToList();
|
||||
|
||||
return _model;
|
||||
|
||||
@@ -27,14 +27,18 @@ public class ContentEditingBuilder : ContentEditingBaseBuilder<ContentCreateMode
|
||||
new ContentEditingBuilder()
|
||||
.WithKey(key)
|
||||
.WithContentTypeKey(contentTypeKey)
|
||||
.WithInvariantName("Home")
|
||||
.AddVariant()
|
||||
.WithName("Home")
|
||||
.Done()
|
||||
.Build();
|
||||
|
||||
public static ContentCreateModel CreateSimpleContent(Guid contentTypeKey) =>
|
||||
new ContentEditingBuilder()
|
||||
.WithContentTypeKey(contentTypeKey)
|
||||
.WithInvariantName("Home")
|
||||
.AddInvariantProperty()
|
||||
.AddVariant()
|
||||
.WithName("Home")
|
||||
.Done()
|
||||
.AddProperty()
|
||||
.WithAlias("title")
|
||||
.WithValue("Welcome to our Home page")
|
||||
.Done()
|
||||
@@ -43,9 +47,11 @@ public class ContentEditingBuilder : ContentEditingBaseBuilder<ContentCreateMode
|
||||
public static ContentCreateModel CreateSimpleContent(Guid contentTypeKey, string name, Guid? parentKey) =>
|
||||
new ContentEditingBuilder()
|
||||
.WithContentTypeKey(contentTypeKey)
|
||||
.WithInvariantName(name)
|
||||
.AddVariant()
|
||||
.WithName(name)
|
||||
.Done()
|
||||
.WithParentKey(parentKey)
|
||||
.AddInvariantProperty()
|
||||
.AddProperty()
|
||||
.WithAlias("title")
|
||||
.WithValue("Welcome to our Home page")
|
||||
.Done()
|
||||
@@ -54,8 +60,10 @@ public class ContentEditingBuilder : ContentEditingBaseBuilder<ContentCreateMode
|
||||
public static ContentCreateModel CreateSimpleContent(Guid contentTypeKey, string name) =>
|
||||
new ContentEditingBuilder()
|
||||
.WithContentTypeKey(contentTypeKey)
|
||||
.WithInvariantName(name)
|
||||
.AddInvariantProperty()
|
||||
.AddVariant()
|
||||
.WithName(name)
|
||||
.Done()
|
||||
.AddProperty()
|
||||
.WithAlias("title")
|
||||
.WithValue("Welcome to our Home page")
|
||||
.Done()
|
||||
@@ -64,13 +72,15 @@ public class ContentEditingBuilder : ContentEditingBaseBuilder<ContentCreateMode
|
||||
public static ContentCreateModel CreateContentWithTwoInvariantProperties(Guid contentTypeKey, string name, string firstPropertyAlias, string firstPropertyValue, string secondPropertyAlias, string secondPropertyValue, Guid? parentKey) =>
|
||||
new ContentEditingBuilder()
|
||||
.WithContentTypeKey(contentTypeKey)
|
||||
.WithInvariantName(name)
|
||||
.AddVariant()
|
||||
.WithName(name)
|
||||
.Done()
|
||||
.WithParentKey(parentKey)
|
||||
.AddInvariantProperty()
|
||||
.AddProperty()
|
||||
.WithAlias(firstPropertyAlias)
|
||||
.WithValue(firstPropertyValue)
|
||||
.Done()
|
||||
.AddInvariantProperty()
|
||||
.AddProperty()
|
||||
.WithAlias(secondPropertyAlias)
|
||||
.WithValue(secondPropertyValue)
|
||||
.Done()
|
||||
@@ -79,8 +89,10 @@ public class ContentEditingBuilder : ContentEditingBaseBuilder<ContentCreateMode
|
||||
public static ContentCreateModel CreateContentWithOneInvariantProperty(Guid contentTypeKey, string name, string propertyAlias, object propertyValue) =>
|
||||
new ContentEditingBuilder()
|
||||
.WithContentTypeKey(contentTypeKey)
|
||||
.WithInvariantName(name)
|
||||
.AddInvariantProperty()
|
||||
.AddVariant()
|
||||
.WithName(name)
|
||||
.Done()
|
||||
.AddProperty()
|
||||
.WithAlias(propertyAlias)
|
||||
.WithValue(propertyValue)
|
||||
.Done()
|
||||
@@ -92,18 +104,20 @@ public class ContentEditingBuilder : ContentEditingBaseBuilder<ContentCreateMode
|
||||
.AddVariant()
|
||||
.WithCulture(firstCulture)
|
||||
.WithName(firstCulture)
|
||||
.AddProperty()
|
||||
.WithAlias(propertyAlias)
|
||||
.WithValue(propertyName)
|
||||
.Done()
|
||||
.Done()
|
||||
.AddVariant()
|
||||
.WithCulture(secondCulture)
|
||||
.WithName(secondCulture)
|
||||
.AddProperty()
|
||||
.WithAlias(propertyAlias)
|
||||
.WithValue(propertyName)
|
||||
.Done()
|
||||
.Done()
|
||||
.AddProperty()
|
||||
.WithCulture(firstCulture)
|
||||
.WithAlias(propertyAlias)
|
||||
.WithValue(propertyName)
|
||||
.Done()
|
||||
.AddProperty()
|
||||
.WithCulture(secondCulture)
|
||||
.WithAlias(propertyAlias)
|
||||
.WithValue(propertyName)
|
||||
.Done()
|
||||
.Build();
|
||||
}
|
||||
|
||||
@@ -4,11 +4,12 @@ using Umbraco.Cms.Tests.Common.Builders.Interfaces;
|
||||
namespace Umbraco.Cms.Tests.Common.Builders;
|
||||
|
||||
public class ContentEditingPropertyValueBuilder<TParent>(TParent parentBuilder)
|
||||
: ChildBuilderBase<TParent, PropertyValueModel>(parentBuilder), IWithAliasBuilder, IWithValueBuilder, IWithCultureBuilder
|
||||
: ChildBuilderBase<TParent, PropertyValueModel>(parentBuilder), IWithAliasBuilder, IWithValueBuilder, IWithCultureBuilder, IWithSegmentBuilder
|
||||
{
|
||||
private string _alias;
|
||||
private object? _value;
|
||||
private string? _culture;
|
||||
private string? _segment;
|
||||
|
||||
string IWithAliasBuilder.Alias
|
||||
{
|
||||
@@ -22,11 +23,17 @@ public class ContentEditingPropertyValueBuilder<TParent>(TParent parentBuilder)
|
||||
set => _value = value;
|
||||
}
|
||||
|
||||
string IWithCultureBuilder.Culture
|
||||
string? IWithCultureBuilder.Culture
|
||||
{
|
||||
get => _culture;
|
||||
set => _culture = value;
|
||||
}
|
||||
|
||||
string? IWithSegmentBuilder.Segment
|
||||
{
|
||||
get => _segment;
|
||||
set => _segment = value;
|
||||
}
|
||||
|
||||
public override PropertyValueModel Build() => new() { Alias = _alias, Value = _value, Culture = _culture };
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ public class ContentEditingVariantBuilder<TParent>(TParent parentBuilder)
|
||||
private string? _culture;
|
||||
private string? _segment;
|
||||
private string _name;
|
||||
private readonly List<ContentEditingPropertyValueBuilder<ContentEditingVariantBuilder<TParent>>> _properties = new();
|
||||
|
||||
string? IWithCultureBuilder.Culture
|
||||
{
|
||||
@@ -30,23 +29,6 @@ public class ContentEditingVariantBuilder<TParent>(TParent parentBuilder)
|
||||
set => _name = value;
|
||||
}
|
||||
|
||||
public ContentEditingPropertyValueBuilder<ContentEditingVariantBuilder<TParent>> AddProperty()
|
||||
{
|
||||
var builder = new ContentEditingPropertyValueBuilder<ContentEditingVariantBuilder<TParent>>((ContentEditingVariantBuilder<TParent>)this);
|
||||
_properties.Add(builder);
|
||||
return builder;
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<ContentEditingPropertyValueBuilder<ContentEditingVariantBuilder<TParent>>> GetProperties()
|
||||
{
|
||||
if (_culture is null)
|
||||
{
|
||||
throw new InvalidOperationException("Culture must be defined for the variant before building.");
|
||||
}
|
||||
|
||||
return _properties.Select(property => property.WithCulture(_culture)).ToList();
|
||||
}
|
||||
|
||||
public override VariantModel Build() =>
|
||||
new()
|
||||
{
|
||||
|
||||
@@ -247,13 +247,6 @@ public static class BuilderExtensions
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static T WithInvariantName<T>(this T builder, string invariantName)
|
||||
where T : IWithInvariantNameBuilder
|
||||
{
|
||||
builder.InvariantName = invariantName;
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static T WithKey<T>(this T builder, Guid? key)
|
||||
where T : IWithKeyBuilder
|
||||
{
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Umbraco.Cms.Tests.Common.Builders.Interfaces;
|
||||
|
||||
public interface IWithInvariantNameBuilder
|
||||
{
|
||||
public string? InvariantName { get; set; }
|
||||
}
|
||||
@@ -9,22 +9,28 @@ public class MediaEditingBuilder : ContentEditingBaseBuilder<MediaCreateModel>
|
||||
new MediaEditingBuilder()
|
||||
.WithKey(key)
|
||||
.WithContentTypeKey(mediaTypeKey)
|
||||
.WithInvariantName("Media")
|
||||
.AddVariant()
|
||||
.WithName("Media")
|
||||
.Done()
|
||||
.Build();
|
||||
|
||||
public static MediaCreateModel CreateSimpleMedia(Guid mediaTypeKey, string name, Guid? parentKey) =>
|
||||
new MediaEditingBuilder()
|
||||
.WithContentTypeKey(mediaTypeKey)
|
||||
.WithInvariantName(name)
|
||||
.AddVariant()
|
||||
.WithName(name)
|
||||
.Done()
|
||||
.WithParentKey(parentKey)
|
||||
.Build();
|
||||
|
||||
public static MediaCreateModel CreateMediaWithAProperty(Guid mediaTypeKey, string name, Guid? parentKey, string propertyAlias = "testProperty", string propertyValue = "TestValue") =>
|
||||
new MediaEditingBuilder()
|
||||
.WithContentTypeKey(mediaTypeKey)
|
||||
.WithInvariantName(name)
|
||||
.AddVariant()
|
||||
.WithName(name)
|
||||
.Done()
|
||||
.WithParentKey(parentKey)
|
||||
.AddInvariantProperty()
|
||||
.AddProperty()
|
||||
.WithAlias(propertyAlias)
|
||||
.WithValue(propertyValue)
|
||||
.Done()
|
||||
|
||||
Reference in New Issue
Block a user