mirror of https://github.com/Squidex/squidex.git
25 changed files with 510 additions and 12 deletions
@ -1,6 +1,6 @@ |
|||||
{ |
{ |
||||
"projects": [ "src" ], |
"projects": [ "src", "tests" ], |
||||
"sdk": { |
"sdk": { |
||||
"version": "1.0.0-preview2-003133" |
"version": "1.0.0-preview2-1-003177" |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,41 @@ |
|||||
|
// ==========================================================================
|
||||
|
// BooleanField.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Globalization; |
||||
|
using Squidex.Core.Schemas.Validators; |
||||
|
using Squidex.Infrastructure; |
||||
|
|
||||
|
namespace Squidex.Core.Schemas |
||||
|
{ |
||||
|
public sealed class BooleanField : Field<BooleanFieldProperties> |
||||
|
{ |
||||
|
public BooleanField(long id, string name, BooleanFieldProperties properties) |
||||
|
: base(id, name, properties) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
protected override IEnumerable<IValidator> CreateValidators() |
||||
|
{ |
||||
|
if (Properties.IsRequired) |
||||
|
{ |
||||
|
yield return new RequiredValidator(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected override object ConvertValue(PropertyValue property) |
||||
|
{ |
||||
|
return property.ToNullableBoolean(CultureInfo.InvariantCulture); |
||||
|
} |
||||
|
|
||||
|
protected override Field Clone() |
||||
|
{ |
||||
|
return new BooleanField(Id, Name, Properties); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
// ==========================================================================
|
||||
|
// BooleanFieldEditor.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Core.Schemas |
||||
|
{ |
||||
|
public enum BooleanFieldEditor |
||||
|
{ |
||||
|
Checkbox, |
||||
|
Toggle |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
// ==========================================================================
|
||||
|
// BooleanFieldProperties.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using Squidex.Infrastructure; |
||||
|
|
||||
|
namespace Squidex.Core.Schemas |
||||
|
{ |
||||
|
[TypeName("BooleanField")] |
||||
|
public sealed class BooleanFieldProperties : FieldProperties |
||||
|
{ |
||||
|
private BooleanFieldEditor editor; |
||||
|
private bool? defaultValue; |
||||
|
|
||||
|
public bool? DefaultValue |
||||
|
{ |
||||
|
get { return defaultValue; } |
||||
|
set |
||||
|
{ |
||||
|
ThrowIfFrozen(); |
||||
|
|
||||
|
defaultValue = value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public BooleanFieldEditor Editor |
||||
|
{ |
||||
|
get { return editor; } |
||||
|
set |
||||
|
{ |
||||
|
ThrowIfFrozen(); |
||||
|
|
||||
|
editor = value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected override IEnumerable<ValidationError> ValidateCore() |
||||
|
{ |
||||
|
if (!Editor.IsEnumValue()) |
||||
|
{ |
||||
|
yield return new ValidationError("Editor ist not a valid value", nameof(Editor)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
// ==========================================================================
|
||||
|
// SchemaPublished.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.CQRS.Events; |
||||
|
|
||||
|
namespace Squidex.Events.Schemas |
||||
|
{ |
||||
|
[TypeName("SchemaPublished")] |
||||
|
public class SchemaPublished : IEvent |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
// ==========================================================================
|
||||
|
// SchemaUnpublished.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.CQRS.Events; |
||||
|
|
||||
|
namespace Squidex.Events.Schemas |
||||
|
{ |
||||
|
[TypeName("SchemaUnpublished")] |
||||
|
public class SchemaUnpublished : IEvent |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
// ==========================================================================
|
||||
|
// PublishSchema.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Write.Schemas.Commands |
||||
|
{ |
||||
|
public class PublishSchema : AppCommand |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
// ==========================================================================
|
||||
|
// UnpublishShema.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Write.Schemas.Commands |
||||
|
{ |
||||
|
public class UnpublishSchema : AppCommand |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
// ==========================================================================
|
||||
|
// BooleanFieldPropertiesTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Core.Schemas |
||||
|
{ |
||||
|
public class BooleanFieldPropertiesTests |
||||
|
{ |
||||
|
private readonly List<ValidationError> errors = new List<ValidationError>(); |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_error_if_editor_is_not_valid() |
||||
|
{ |
||||
|
var sut = new BooleanFieldProperties { Editor = (BooleanFieldEditor)123 }; |
||||
|
|
||||
|
sut.Validate(errors); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new List<ValidationError> |
||||
|
{ |
||||
|
new ValidationError("Editor ist not a valid value", "Editor") |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,66 @@ |
|||||
|
// ==========================================================================
|
||||
|
// BooleanFieldTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Core.Schemas |
||||
|
{ |
||||
|
public class BooleanFieldTests |
||||
|
{ |
||||
|
private readonly List<string> errors = new List<string>(); |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_instantiate_field() |
||||
|
{ |
||||
|
var sut = new BooleanField(1, "name", new BooleanFieldProperties()); |
||||
|
|
||||
|
Assert.Equal("name", sut.Name); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_clone_object() |
||||
|
{ |
||||
|
var sut = new BooleanField(1, "name", new BooleanFieldProperties()); |
||||
|
|
||||
|
Assert.NotEqual(sut, sut.Enable()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_add_errors_if_boolean_is_required() |
||||
|
{ |
||||
|
var sut = new BooleanField(1, "name", new BooleanFieldProperties { Label = "Name", IsRequired = true }); |
||||
|
|
||||
|
await sut.ValidateAsync(CreateValue(null), errors); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new[] { "Name is required" }); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_add_errors_if_value_is_not_valid() |
||||
|
{ |
||||
|
var sut = new BooleanField(1, "name", new BooleanFieldProperties { Label = "Name" }); |
||||
|
|
||||
|
await sut.ValidateAsync(CreateValue("Invalid"), errors); |
||||
|
|
||||
|
errors.ShouldBeEquivalentTo( |
||||
|
new[] { "Name is not a valid value" }); |
||||
|
} |
||||
|
|
||||
|
private static PropertyValue CreateValue(object v) |
||||
|
{ |
||||
|
var bag = new PropertiesBag().Set("value", v); |
||||
|
|
||||
|
return bag["value"]; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue