Browse Source

Validation improvements finalized.

pull/303/head
Sebastian 8 years ago
parent
commit
11210c19f1
  1. 28
      src/Squidex.Domain.Apps.Entities/Contents/Guards/GuardContent.cs
  2. 24
      tests/Squidex.Domain.Apps.Entities.Tests/Contents/Guard/GuardContentTests.cs

28
src/Squidex.Domain.Apps.Entities/Contents/Guards/GuardContent.cs

@ -48,13 +48,10 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guards
{ {
Guard.NotNull(command, nameof(command)); Guard.NotNull(command, nameof(command));
Validate.It(() => "Cannot discard pending changes.", e =>
{
if (!isPending) if (!isPending)
{ {
e("The content has no pending changes."); throw new DomainException("The content has no pending changes.");
} }
});
} }
public static void CanChangeContentStatus(bool isPending, Status status, ChangeContentStatus command) public static void CanChangeContentStatus(bool isPending, Status status, ChangeContentStatus command)
@ -63,14 +60,23 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guards
Validate.It(() => "Cannot change status.", e => Validate.It(() => "Cannot change status.", e =>
{ {
var isAllowedPendingUpdate = if (!StatusFlow.Exists(command.Status))
status == command.Status && {
status == Status.Published && e("Status is not valid.", nameof(command.Status));
isPending; }
else if (!StatusFlow.CanChange(status, command.Status))
if (!StatusFlow.Exists(command.Status) || (!StatusFlow.CanChange(status, command.Status) && !isAllowedPendingUpdate)) {
if (status == command.Status && status == Status.Published)
{ {
e($"Content cannot be changed from status {status} to {command.Status}.", nameof(command.Status)); if (!isPending)
{
e("Content has no changes to publish.", nameof(command.Status));
}
}
else
{
e($"Cannot change status from {status} to {command.Status}.", nameof(command.Status));
}
} }
if (command.DueTime.HasValue && command.DueTime.Value < SystemClock.Instance.GetCurrentInstant()) if (command.DueTime.HasValue && command.DueTime.Value < SystemClock.Instance.GetCurrentInstant())

24
tests/Squidex.Domain.Apps.Entities.Tests/Contents/Guard/GuardContentTests.cs

@ -9,6 +9,7 @@ using NodaTime;
using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.Contents;
using Squidex.Domain.Apps.Entities.Contents.Commands; using Squidex.Domain.Apps.Entities.Contents.Commands;
using Squidex.Domain.Apps.Entities.Contents.Guards; using Squidex.Domain.Apps.Entities.Contents.Guards;
using Squidex.Domain.Apps.Entities.TestHelpers;
using Squidex.Infrastructure; using Squidex.Infrastructure;
using Xunit; using Xunit;
@ -23,7 +24,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
{ {
var command = new CreateContent(); var command = new CreateContent();
Assert.Throws<ValidationException>(() => GuardContent.CanCreate(command)); ValidationAssert.Throws(() => GuardContent.CanCreate(command),
new ValidationError("Data is required.", "Data"));
} }
[Fact] [Fact]
@ -39,7 +41,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
{ {
var command = new UpdateContent(); var command = new UpdateContent();
Assert.Throws<ValidationException>(() => GuardContent.CanUpdate(command)); ValidationAssert.Throws(() => GuardContent.CanUpdate(command),
new ValidationError("Data is required.", "Data"));
} }
[Fact] [Fact]
@ -55,7 +58,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
{ {
var command = new PatchContent(); var command = new PatchContent();
Assert.Throws<ValidationException>(() => GuardContent.CanPatch(command)); ValidationAssert.Throws(() => GuardContent.CanPatch(command),
new ValidationError("Data is required.", "Data"));
} }
[Fact] [Fact]
@ -71,7 +75,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
{ {
var command = new ChangeContentStatus { Status = (Status)10 }; var command = new ChangeContentStatus { Status = (Status)10 };
Assert.Throws<ValidationException>(() => GuardContent.CanChangeContentStatus(false, Status.Archived, command)); ValidationAssert.Throws(() => GuardContent.CanChangeContentStatus(false, Status.Archived, command),
new ValidationError("Status is not valid.", "Status"));
} }
[Fact] [Fact]
@ -79,7 +84,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
{ {
var command = new ChangeContentStatus { Status = Status.Published }; var command = new ChangeContentStatus { Status = Status.Published };
Assert.Throws<ValidationException>(() => GuardContent.CanChangeContentStatus(false, Status.Archived, command)); ValidationAssert.Throws(() => GuardContent.CanChangeContentStatus(false, Status.Archived, command),
new ValidationError("Cannot change status from Archived to Published.", "Status"));
} }
[Fact] [Fact]
@ -87,7 +93,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
{ {
var command = new ChangeContentStatus { Status = Status.Published, DueTime = dueTimeInPast }; var command = new ChangeContentStatus { Status = Status.Published, DueTime = dueTimeInPast };
Assert.Throws<ValidationException>(() => GuardContent.CanChangeContentStatus(false, Status.Draft, command)); ValidationAssert.Throws(() => GuardContent.CanChangeContentStatus(false, Status.Draft, command),
new ValidationError("Due time must be in the future.", "DueTime"));
} }
[Fact] [Fact]
@ -95,7 +102,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
{ {
var command = new ChangeContentStatus { Status = Status.Published }; var command = new ChangeContentStatus { Status = Status.Published };
Assert.Throws<ValidationException>(() => GuardContent.CanChangeContentStatus(false, Status.Published, command)); ValidationAssert.Throws(() => GuardContent.CanChangeContentStatus(false, Status.Published, command),
new ValidationError("Content has no changes to publish.", "Status"));
} }
[Fact] [Fact]
@ -119,7 +127,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard
{ {
var command = new DiscardChanges(); var command = new DiscardChanges();
Assert.Throws<ValidationException>(() => GuardContent.CanDiscardChanges(false, command)); Assert.Throws<DomainException>(() => GuardContent.CanDiscardChanges(false, command));
} }
[Fact] [Fact]

Loading…
Cancel
Save