mirror of https://github.com/Squidex/squidex.git
44 changed files with 645 additions and 318 deletions
@ -0,0 +1,41 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Orleans; |
|||
using Squidex.Domain.Apps.Entities.Contents.Commands; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Contents |
|||
{ |
|||
public sealed class ContentCommandMiddleware : GrainCommandMiddleware<ContentCommand, IContentGrain> |
|||
{ |
|||
private readonly IContentEnricher contentEnricher; |
|||
|
|||
public ContentCommandMiddleware(IGrainFactory grainFactory, IContentEnricher contentEnricher) |
|||
: base(grainFactory) |
|||
{ |
|||
Guard.NotNull(contentEnricher, nameof(contentEnricher)); |
|||
|
|||
this.contentEnricher = contentEnricher; |
|||
} |
|||
|
|||
public override async Task HandleAsync(CommandContext context, Func<Task> next) |
|||
{ |
|||
await HandleAsync(context, next); |
|||
|
|||
if (context.PlainResult is IContentEntity content) |
|||
{ |
|||
var enriched = await contentEnricher.EnrichAsync(content); |
|||
|
|||
context.Complete(enriched); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using FakeItEasy; |
|||
using Orleans; |
|||
using Squidex.Domain.Apps.Entities.Contents.Commands; |
|||
using Squidex.Domain.Apps.Entities.Contents.State; |
|||
using Squidex.Domain.Apps.Entities.TestHelpers; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Contents |
|||
{ |
|||
public sealed class ContentCommandMiddlewareTests : HandlerTestBase<ContentState> |
|||
{ |
|||
private readonly IContentEnricher contentEnricher = A.Fake<IContentEnricher>(); |
|||
private readonly Guid contentId = Guid.NewGuid(); |
|||
private readonly ContentCommandMiddleware sut; |
|||
|
|||
protected override Guid Id |
|||
{ |
|||
get { return contentId; } |
|||
} |
|||
|
|||
public ContentCommandMiddlewareTests() |
|||
{ |
|||
sut = new ContentCommandMiddleware(A.Fake<IGrainFactory>(), contentEnricher); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_not_invoke_enricher_for_other_result() |
|||
{ |
|||
var command = CreateCommand(new CreateContent()); |
|||
var context = CreateContextForCommand(command); |
|||
|
|||
context.Complete(12); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
A.CallTo(() => contentEnricher.EnrichAsync(A<IContentEntityEnriched>.Ignored)) |
|||
.MustNotHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_enrich_content_result() |
|||
{ |
|||
var result = new ContentEntity(); |
|||
|
|||
var command = CreateCommand(new CreateContent()); |
|||
var context = CreateContextForCommand(command); |
|||
|
|||
context.Complete(result); |
|||
|
|||
var enriched = new ContentEntity(); |
|||
|
|||
A.CallTo(() => contentEnricher.EnrichAsync(result)) |
|||
.Returns(enriched); |
|||
|
|||
await sut.HandleAsync(context); |
|||
|
|||
Assert.Equal(enriched, context.Result<IContentEntityEnriched>()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,85 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using FakeItEasy; |
|||
using Squidex.Domain.Apps.Core.Contents; |
|||
using Squidex.Infrastructure; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Contents |
|||
{ |
|||
public class ContentEnricherTests |
|||
{ |
|||
private readonly IContentWorkflow workflow = A.Fake<IContentWorkflow>(); |
|||
private readonly NamedId<Guid> schemaId = NamedId.Of(Guid.NewGuid(), "my-schema"); |
|||
private readonly ContentEnricher sut; |
|||
|
|||
public ContentEnricherTests() |
|||
{ |
|||
sut = new ContentEnricher(workflow); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_enrich_content_with_status_color() |
|||
{ |
|||
var source = new ContentEntity { Status = Status.Published, SchemaId = schemaId }; |
|||
|
|||
A.CallTo(() => workflow.GetInfoAsync(Status.Published)) |
|||
.Returns(new StatusInfo(Status.Published, StatusColors.Published)); |
|||
|
|||
var result = await sut.EnrichAsync(source); |
|||
|
|||
Assert.Equal(StatusColors.Published, result.StatusColor); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_enrich_content_with_default_color_if_not_found() |
|||
{ |
|||
var source = new ContentEntity { Status = Status.Published, SchemaId = schemaId }; |
|||
|
|||
A.CallTo(() => workflow.GetInfoAsync(Status.Published)) |
|||
.Returns(Task.FromResult<StatusInfo>(null)); |
|||
|
|||
var result = await sut.EnrichAsync(source); |
|||
|
|||
Assert.Equal(StatusColors.Draft, result.StatusColor); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_enrich_content_with_can_update() |
|||
{ |
|||
var source = new ContentEntity { SchemaId = schemaId }; |
|||
|
|||
A.CallTo(() => workflow.CanUpdateAsync(source)) |
|||
.Returns(true); |
|||
|
|||
var result = await sut.EnrichAsync(source); |
|||
|
|||
Assert.True(result.CanUpdate); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_enrich_multiple_contents_and_cache_color() |
|||
{ |
|||
var source1 = new ContentEntity { Status = Status.Published, SchemaId = schemaId }; |
|||
var source2 = new ContentEntity { Status = Status.Published, SchemaId = schemaId }; |
|||
|
|||
A.CallTo(() => workflow.GetInfoAsync(Status.Published)) |
|||
.Returns(new StatusInfo(Status.Published, StatusColors.Published)); |
|||
|
|||
var result = await sut.EnrichAsync(new[] { source1, source2 }); |
|||
|
|||
Assert.Equal(StatusColors.Published, result[0].StatusColor); |
|||
Assert.Equal(StatusColors.Published, result[1].StatusColor); |
|||
|
|||
A.CallTo(() => workflow.GetInfoAsync(Status.Published)) |
|||
.MustHaveHappenedOnceExactly(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue