// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using Squidex.Domain.Apps.Core; using Squidex.Domain.Apps.Core.Schemas; using Squidex.Domain.Apps.Entities.Schemas.Commands; using Squidex.Infrastructure; using Xunit; #pragma warning disable SA1310 // Field names must not contain underscore #pragma warning disable SA1401 // Fields must be private namespace Squidex.Domain.Apps.Entities.Schemas.Guards { public class GuardSchemaFieldTests { private readonly Schema schema_0; private readonly StringFieldProperties validProperties = new StringFieldProperties(); private readonly StringFieldProperties invalidProperties = new StringFieldProperties { MinLength = 10, MaxLength = 5 }; public GuardSchemaFieldTests() { schema_0 = new Schema("my-schema") .AddString(1, "field1", Partitioning.Invariant) .AddString(2, "field2", Partitioning.Invariant) .AddArray(3, "field3", Partitioning.Invariant, f => f .AddNumber(301, "field301")); } private static Action A(Action method) where T : FieldCommand { return new Action(method); } private static Func S(Func method) { return new Func(method); } public static IEnumerable FieldCommandData = new[] { new object[] { A(GuardSchemaField.CanEnable) }, new object[] { A(GuardSchemaField.CanDelete) }, new object[] { A(GuardSchemaField.CanDisable) }, new object[] { A(GuardSchemaField.CanHide) }, new object[] { A(GuardSchemaField.CanLock) }, new object[] { A(GuardSchemaField.CanShow) }, new object[] { A(GuardSchemaField.CanUpdate) } }; public static IEnumerable InvalidStates = new[] { new object[] { A(GuardSchemaField.CanDisable), S(s => s.DisableField(1)) }, new object[] { A(GuardSchemaField.CanEnable), S(s => s) }, new object[] { A(GuardSchemaField.CanHide), S(s => s.HideField(1)) }, new object[] { A(GuardSchemaField.CanShow), S(s => s.LockField(1)) }, new object[] { A(GuardSchemaField.CanLock), S(s => s.LockField(1)) } }; public static IEnumerable InvalidNestedStates = new[] { new object[] { A(GuardSchemaField.CanDisable), S(s => s.DisableField(301, 3)) }, new object[] { A(GuardSchemaField.CanEnable), S(s => s) }, new object[] { A(GuardSchemaField.CanHide), S(s => s.HideField(301, 3)) }, new object[] { A(GuardSchemaField.CanShow), S(s => s) }, new object[] { A(GuardSchemaField.CanLock), S(s => s.LockField(301, 3)) } }; public static IEnumerable ValidStates = new[] { new object[] { A(GuardSchemaField.CanDisable), S(s => s) }, new object[] { A(GuardSchemaField.CanEnable), S(s => s.DisableField(1)) }, new object[] { A(GuardSchemaField.CanHide), S(s => s) }, new object[] { A(GuardSchemaField.CanShow), S(s => s.HideField(1)) } }; public static IEnumerable ValidNestedStates = new[] { new object[] { A(GuardSchemaField.CanEnable), S(s => s.DisableField(301, 3)) }, new object[] { A(GuardSchemaField.CanDisable), S(s => s) }, new object[] { A(GuardSchemaField.CanHide), S(s => s) }, new object[] { A(GuardSchemaField.CanShow), S(s => s.HideField(301, 3)) } }; [Theory] [MemberData(nameof(FieldCommandData))] public void Commands_should_throw_exception_if_field_not_found(Action action) where T : FieldCommand, new() { var command = new T { FieldId = 4 }; Assert.Throws(() => action(schema_0, command)); } [Theory] [MemberData(nameof(FieldCommandData))] public void Commands_should_throw_exception_if_parent_field_not_found(Action action) where T : FieldCommand, new() { var command = new T { ParentFieldId = 4, FieldId = 401 }; Assert.Throws(() => action(schema_0, command)); } [Theory] [MemberData(nameof(FieldCommandData))] public void Commands_should_throw_exception_if_child_field_not_found(Action action) where T : FieldCommand, new() { var command = new T { ParentFieldId = 3, FieldId = 302 }; Assert.Throws(() => action(schema_0, command)); } [Theory] [MemberData(nameof(InvalidStates))] public void Commands_should_throw_exception_if_state_not_valid(Action action, Func updater) where T : FieldCommand, new() { var command = new T { FieldId = 1 }; Assert.Throws(() => action(updater(schema_0), command)); } [Theory] [MemberData(nameof(InvalidNestedStates))] public void Commands_should_throw_exception_if_nested_state_not_valid(Action action, Func updater) where T : FieldCommand, new() { var command = new T { ParentFieldId = 3, FieldId = 301 }; Assert.Throws(() => action(updater(schema_0), command)); } [Theory] [MemberData(nameof(ValidStates))] public void Commands_should_not_throw_exception_if_state_valid(Action action, Func updater) where T : FieldCommand, new() { var command = new T { FieldId = 1 }; action(updater(schema_0), command); } [Theory] [MemberData(nameof(ValidNestedStates))] public void Commands_should_not_throw_exception_if_nested_state_valid(Action action, Func updater) where T : FieldCommand, new() { var command = new T { ParentFieldId = 3, FieldId = 301 }; action(updater(schema_0), command); } [Fact] public void CanDelete_should_throw_exception_if_locked() { var command = new DeleteField { FieldId = 1 }; var schema_1 = schema_0.UpdateField(1, f => f.Lock()); Assert.Throws(() => GuardSchemaField.CanDelete(schema_1, command)); } [Fact] public void CanHide_should_throw_exception_if_locked() { var command = new HideField { FieldId = 1 }; var schema_1 = schema_0.UpdateField(1, f => f.Lock()); Assert.Throws(() => GuardSchemaField.CanHide(schema_1, command)); } [Fact] public void CanDelete_should_not_throw_exception_if_not_locked() { var command = new DeleteField { FieldId = 1 }; GuardSchemaField.CanDelete(schema_0, command); } [Fact] public void CanUpdate_should_throw_exception_if_locked() { var command = new UpdateField { FieldId = 1, Properties = validProperties }; var schema_1 = schema_0.UpdateField(1, f => f.Lock()); Assert.Throws(() => GuardSchemaField.CanUpdate(schema_1, command)); } [Fact] public void CanUpdate_should_not_throw_exception_if_not_locked() { var command = new UpdateField { FieldId = 1, Properties = validProperties }; GuardSchemaField.CanUpdate(schema_0, command); } [Fact] public void CanUpdate_should_throw_exception_if_properties_null() { var command = new UpdateField { FieldId = 2, Properties = null }; Assert.Throws(() => GuardSchemaField.CanUpdate(schema_0, command)); } [Fact] public void CanUpdate_should_throw_exception_if_properties_not_valid() { var command = new UpdateField { FieldId = 2, Properties = new StringFieldProperties { MinLength = 10, MaxLength = 5 } }; Assert.Throws(() => GuardSchemaField.CanUpdate(schema_0, command)); } [Fact] public void CanAdd_should_throw_exception_if_field_already_exists() { var command = new AddField { Name = "field1", Properties = validProperties }; Assert.Throws(() => GuardSchemaField.CanAdd(schema_0, command)); } [Fact] public void CanAdd_should_throw_exception_if_nested_field_already_exists() { var command = new AddField { Name = "field301", Properties = validProperties, ParentFieldId = 3 }; Assert.Throws(() => GuardSchemaField.CanAdd(schema_0, command)); } [Fact] public void CanAdd_should_throw_exception_if_name_not_valid() { var command = new AddField { Name = "INVALID_NAME", Properties = validProperties }; Assert.Throws(() => GuardSchemaField.CanAdd(schema_0, command)); } [Fact] public void CanAdd_should_throw_exception_if_properties_not_valid() { var command = new AddField { Name = "field3", Properties = invalidProperties }; Assert.Throws(() => GuardSchemaField.CanAdd(schema_0, command)); } [Fact] public void CanAdd_should_throw_exception_if_properties_null() { var command = new AddField { Name = "field3", Properties = null }; Assert.Throws(() => GuardSchemaField.CanAdd(schema_0, command)); } [Fact] public void CanAdd_should_throw_exception_if_partitioning_not_valid() { var command = new AddField { Name = "field3", Partitioning = "INVALID_PARTITIONING", Properties = validProperties }; Assert.Throws(() => GuardSchemaField.CanAdd(schema_0, command)); } [Fact] public void CanAdd_should_throw_exception_if_parent_not_exists() { var command = new AddField { Name = "field302", Properties = validProperties, ParentFieldId = 99 }; Assert.Throws(() => GuardSchemaField.CanAdd(schema_0, command)); } [Fact] public void CanAdd_should_not_throw_exception_if_field_not_exists() { var command = new AddField { Name = "field4", Properties = validProperties }; GuardSchemaField.CanAdd(schema_0, command); } [Fact] public void CanAdd_should_not_throw_exception_if_field_exists_in_root() { var command = new AddField { Name = "field1", Properties = validProperties, ParentFieldId = 3 }; GuardSchemaField.CanAdd(schema_0, command); } } }