|
|
|
@ -5,6 +5,8 @@ |
|
|
|
// 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; |
|
|
|
@ -12,6 +14,7 @@ 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 |
|
|
|
{ |
|
|
|
@ -26,145 +29,126 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards |
|
|
|
schema_0 = |
|
|
|
new Schema("my-schema") |
|
|
|
.AddString(1, "field1", Partitioning.Invariant) |
|
|
|
.AddString(2, "field2", Partitioning.Invariant); |
|
|
|
.AddString(2, "field2", Partitioning.Invariant) |
|
|
|
.AddArray(3, "field3", Partitioning.Invariant, f => f |
|
|
|
.AddNumber(301, "field301")); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanHide_should_throw_exception_if_already_hidden() |
|
|
|
private static Action<Schema, T> A<T>(Action<Schema, T> method) where T : FieldCommand |
|
|
|
{ |
|
|
|
var command = new HideField { FieldId = 1 }; |
|
|
|
|
|
|
|
var schema_1 = schema_0.UpdateField(1, f => f.Hide()); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanHide(schema_1, command)); |
|
|
|
return new Action<Schema, T>(method); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanHide_should_throw_exception_if_not_found() |
|
|
|
private static Func<Schema, Schema> S(Func<Schema, Schema> method) |
|
|
|
{ |
|
|
|
var command = new HideField { FieldId = 3 }; |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanHide(schema_0, command)); |
|
|
|
return new Func<Schema, Schema>(method); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanHide_hould_not_throw_exception_if_visible() |
|
|
|
public static IEnumerable<object[]> FieldCommandData = new[] |
|
|
|
{ |
|
|
|
var command = new HideField { FieldId = 1 }; |
|
|
|
new object[] { A<EnableField>(GuardSchemaField.CanEnable) }, |
|
|
|
new object[] { A<DeleteField>(GuardSchemaField.CanDelete) }, |
|
|
|
new object[] { A<DisableField>(GuardSchemaField.CanDisable) }, |
|
|
|
new object[] { A<HideField>(GuardSchemaField.CanHide) }, |
|
|
|
new object[] { A<LockField>(GuardSchemaField.CanLock) }, |
|
|
|
new object[] { A<ShowField>(GuardSchemaField.CanShow) }, |
|
|
|
new object[] { A<UpdateField>(GuardSchemaField.CanUpdate) } |
|
|
|
}; |
|
|
|
|
|
|
|
GuardSchemaField.CanHide(schema_0, command); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanDisable_should_throw_exception_if_already_disabled() |
|
|
|
public static IEnumerable<object[]> InvalidStates = new[] |
|
|
|
{ |
|
|
|
var command = new DisableField { FieldId = 1 }; |
|
|
|
|
|
|
|
var schema_1 = schema_0.UpdateField(1, f => f.Disable()); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanDisable(schema_1, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanDisable_should_throw_exception_if_not_found() |
|
|
|
{ |
|
|
|
var command = new DisableField { FieldId = 3 }; |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanDisable(schema_0, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanDisable_Should_not_throw_exception_if_enabled() |
|
|
|
{ |
|
|
|
var command = new DisableField { FieldId = 1 }; |
|
|
|
|
|
|
|
GuardSchemaField.CanDisable(schema_0, command); |
|
|
|
} |
|
|
|
new object[] { A<EnableField>(GuardSchemaField.CanEnable), S(s => s) }, |
|
|
|
new object[] { A<DisableField>(GuardSchemaField.CanDisable), S(s => s.DisableField(1)) }, |
|
|
|
new object[] { A<HideField>(GuardSchemaField.CanHide), S(s => s.HideField(1)) }, |
|
|
|
new object[] { A<LockField>(GuardSchemaField.CanLock), S(s => s.LockField(1)) }, |
|
|
|
new object[] { A<ShowField>(GuardSchemaField.CanShow), S(s => s.LockField(1)) } |
|
|
|
}; |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanShow_should_throw_exception_if_already_shown() |
|
|
|
public static IEnumerable<object[]> InvalidNestedStates = new[] |
|
|
|
{ |
|
|
|
var command = new ShowField { FieldId = 1 }; |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanShow(schema_0, command)); |
|
|
|
} |
|
|
|
new object[] { A<EnableField>(GuardSchemaField.CanEnable), S(s => s) }, |
|
|
|
new object[] { A<DisableField>(GuardSchemaField.CanDisable), S(s => s.DisableField(301, 3)) }, |
|
|
|
new object[] { A<HideField>(GuardSchemaField.CanHide), S(s => s.HideField(301, 3)) }, |
|
|
|
new object[] { A<ShowField>(GuardSchemaField.CanShow), S(s => s) } |
|
|
|
}; |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanShow_should_throw_exception_if_not_found() |
|
|
|
public static IEnumerable<object[]> ValidStates = new[] |
|
|
|
{ |
|
|
|
var command = new ShowField { FieldId = 3 }; |
|
|
|
new object[] { A<EnableField>(GuardSchemaField.CanEnable), S(s => s.DisableField(1)) }, |
|
|
|
new object[] { A<DisableField>(GuardSchemaField.CanDisable), S(s => s) }, |
|
|
|
new object[] { A<HideField>(GuardSchemaField.CanHide), S(s => s) }, |
|
|
|
new object[] { A<ShowField>(GuardSchemaField.CanShow), S(s => s.HideField(1)) } |
|
|
|
}; |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanShow(schema_0, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanShow_should_not_throw_exception_if_hidden() |
|
|
|
public static IEnumerable<object[]> ValidNestedStates = new[] |
|
|
|
{ |
|
|
|
var command = new ShowField { FieldId = 1 }; |
|
|
|
|
|
|
|
var schema_1 = schema_0.UpdateField(1, f => f.Hide()); ; |
|
|
|
|
|
|
|
GuardSchemaField.CanShow(schema_1, command); |
|
|
|
} |
|
|
|
new object[] { A<EnableField>(GuardSchemaField.CanEnable), S(s => s.DisableField(301, 3)) }, |
|
|
|
new object[] { A<DisableField>(GuardSchemaField.CanDisable), S(s => s) }, |
|
|
|
new object[] { A<HideField>(GuardSchemaField.CanHide), S(s => s) }, |
|
|
|
new object[] { A<ShowField>(GuardSchemaField.CanShow), S(s => s.HideField(301, 3)) } |
|
|
|
}; |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanEnable_should_throw_exception_if_already_enabled() |
|
|
|
[Theory] |
|
|
|
[MemberData(nameof(FieldCommandData))] |
|
|
|
public void Commands_should_throw_exception_if_field_not_found<T>(Action<Schema, T> action) where T : FieldCommand, new() |
|
|
|
{ |
|
|
|
var command = new EnableField { FieldId = 1 }; |
|
|
|
var command = new T { FieldId = 4 }; |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanEnable(schema_0, command)); |
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => action(schema_0, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanEnable_should_throw_exception_if_not_found() |
|
|
|
[Theory] |
|
|
|
[MemberData(nameof(FieldCommandData))] |
|
|
|
public void Commands_should_throw_exception_if_parent_field_not_found<T>(Action<Schema, T> action) where T : FieldCommand, new() |
|
|
|
{ |
|
|
|
var command = new EnableField { FieldId = 3 }; |
|
|
|
var command = new T { ParentFieldId = 4, FieldId = 401 }; |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanEnable(schema_0, command)); |
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => action(schema_0, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanEnable_should_not_throw_exception_if_disabled() |
|
|
|
[Theory] |
|
|
|
[MemberData(nameof(FieldCommandData))] |
|
|
|
public void Commands_should_throw_exception_if_child_field_not_found<T>(Action<Schema, T> action) where T : FieldCommand, new() |
|
|
|
{ |
|
|
|
var command = new EnableField { FieldId = 1 }; |
|
|
|
var command = new T { ParentFieldId = 3, FieldId = 302 }; |
|
|
|
|
|
|
|
var schema_1 = schema_0.UpdateField(1, f => f.Disable()); |
|
|
|
|
|
|
|
GuardSchemaField.CanEnable(schema_1, command); |
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => action(schema_0, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanLock_should_throw_exception_if_already_locked() |
|
|
|
[Theory] |
|
|
|
[MemberData(nameof(InvalidStates))] |
|
|
|
public void Commands_should_throw_exception_if_state_not_valid<T>(Action<Schema, T> action, Func<Schema, Schema> updater) where T : FieldCommand, new() |
|
|
|
{ |
|
|
|
var command = new LockField { FieldId = 1 }; |
|
|
|
var command = new T { FieldId = 1 }; |
|
|
|
|
|
|
|
var schema_1 = schema_0.UpdateField(1, f => f.Lock()); |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => GuardSchemaField.CanLock(schema_1, command)); |
|
|
|
Assert.Throws<DomainException>(() => action(updater(schema_0), command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void LockField_should_throw_exception_if_not_found() |
|
|
|
[Theory] |
|
|
|
[MemberData(nameof(InvalidNestedStates))] |
|
|
|
public void Commands_should_throw_exception_if_nested_state_not_valid<T>(Action<Schema, T> action, Func<Schema, Schema> updater) where T : FieldCommand, new() |
|
|
|
{ |
|
|
|
var command = new LockField { FieldId = 3 }; |
|
|
|
var command = new T { ParentFieldId = 3, FieldId = 301 }; |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanLock(schema_0, command)); |
|
|
|
Assert.Throws<DomainException>(() => action(updater(schema_0), command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanLock_should_not_throw_exception_if_not_locked() |
|
|
|
[Theory] |
|
|
|
[MemberData(nameof(ValidStates))] |
|
|
|
public void Commands_should_not_throw_exception_if_state_valid<T>(Action<Schema, T> action, Func<Schema, Schema> updater) where T : FieldCommand, new() |
|
|
|
{ |
|
|
|
var command = new LockField { FieldId = 1 }; |
|
|
|
var command = new T { FieldId = 1 }; |
|
|
|
|
|
|
|
GuardSchemaField.CanLock(schema_0, command); |
|
|
|
action(updater(schema_0), command); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanDelete_should_throw_exception_if_not_found() |
|
|
|
[Theory] |
|
|
|
[MemberData(nameof(ValidNestedStates))] |
|
|
|
public void Commands_should_not_throw_exception_if_nested_state_valid<T>(Action<Schema, T> action, Func<Schema, Schema> updater) where T : FieldCommand, new() |
|
|
|
{ |
|
|
|
var command = new DeleteField { FieldId = 3 }; |
|
|
|
var command = new T { ParentFieldId = 3, FieldId = 301 }; |
|
|
|
|
|
|
|
Assert.Throws<DomainObjectNotFoundException>(() => GuardSchemaField.CanDelete(schema_0, command)); |
|
|
|
action(updater(schema_0), command); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
@ -262,7 +246,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards |
|
|
|
[Fact] |
|
|
|
public void CanAdd_should_not_throw_exception_if_field_not_exists() |
|
|
|
{ |
|
|
|
var command = new AddField { Name = "field3", Properties = new StringFieldProperties() }; |
|
|
|
var command = new AddField { Name = "field4", Properties = new StringFieldProperties() }; |
|
|
|
|
|
|
|
GuardSchemaField.CanAdd(schema_0, command); |
|
|
|
} |
|
|
|
|