// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.Collections.ObjectModel; using FakeItEasy; using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.HandleRules.EnrichedEvents; using Squidex.Domain.Apps.Core.HandleRules.Triggers; using Squidex.Domain.Apps.Core.Rules.Triggers; using Squidex.Domain.Apps.Core.Scripting; using Squidex.Domain.Apps.Events.Assets; using Squidex.Domain.Apps.Events.Contents; using Squidex.Infrastructure; using Xunit; #pragma warning disable SA1401 // Fields must be private #pragma warning disable RECS0070 namespace Squidex.Domain.Apps.Core.Operations.HandleRules.Triggers { public class ContentChangedTriggerTests { private readonly IScriptEngine scriptEngine = A.Fake(); private readonly IRuleTriggerHandler sut; private static readonly NamedId SchemaMatch = NamedId.Of(Guid.NewGuid(), "my-schema1"); private static readonly NamedId SchemaNonMatch = NamedId.Of(Guid.NewGuid(), "my-schema2"); public ContentChangedTriggerTests() { sut = new ContentChangedTriggerHandler(scriptEngine); A.CallTo(() => scriptEngine.Evaluate("event", A.Ignored, "true")) .Returns(true); A.CallTo(() => scriptEngine.Evaluate("event", A.Ignored, "false")) .Returns(false); } [Fact] public void Should_not_trigger_precheck_when_event_type_not_correct() { TestForTrigger(handleAll: true, schemaId: null, condition: null, action: trigger => { var result = sut.Triggers(new AssetCreated(), trigger); Assert.False(result); }); } [Fact] public void Should_not_trigger_precheck_when_trigger_contains_no_schemas() { TestForTrigger(handleAll: false, schemaId: null, condition: null, action: trigger => { var result = sut.Triggers(new ContentCreated { SchemaId = SchemaMatch }, trigger); Assert.False(result); }); } [Fact] public void Should_trigger_precheck_when_handling_all_events() { TestForTrigger(handleAll: true, schemaId: SchemaMatch, condition: null, action: trigger => { var result = sut.Triggers(new ContentCreated { SchemaId = SchemaMatch }, trigger); Assert.True(result); }); } [Fact] public void Should_trigger_precheck_when_condition_is_empty() { TestForTrigger(handleAll: false, schemaId: SchemaMatch, condition: string.Empty, action: trigger => { var result = sut.Triggers(new ContentCreated { SchemaId = SchemaMatch }, trigger); Assert.True(result); }); } [Fact] public void Should_not_trigger_precheck_when_schema_id_does_not_match() { TestForTrigger(handleAll: false, schemaId: SchemaNonMatch, condition: null, action: trigger => { var result = sut.Triggers(new ContentCreated { SchemaId = SchemaMatch }, trigger); Assert.False(result); }); } [Fact] public void Should_not_trigger_check_when_event_type_not_correct() { TestForTrigger(handleAll: true, schemaId: null, condition: null, action: trigger => { var result = sut.Triggers(new EnrichedAssetEvent(), trigger); Assert.False(result); }); } [Fact] public void Should_not_trigger_check_when_trigger_contains_no_schemas() { TestForTrigger(handleAll: false, schemaId: null, condition: null, action: trigger => { var result = sut.Triggers(new EnrichedContentEvent { SchemaId = SchemaMatch }, trigger); Assert.False(result); }); } [Fact] public void Should_trigger_check_when_handling_all_events() { TestForTrigger(handleAll: true, schemaId: SchemaMatch, condition: null, action: trigger => { var result = sut.Triggers(new EnrichedContentEvent { SchemaId = SchemaMatch }, trigger); Assert.True(result); }); } [Fact] public void Should_trigger_check_when_condition_is_empty() { TestForTrigger(handleAll: false, schemaId: SchemaMatch, condition: string.Empty, action: trigger => { var result = sut.Triggers(new EnrichedContentEvent { SchemaId = SchemaMatch }, trigger); Assert.True(result); }); } [Fact] public void Should_trigger_check_when_condition_matchs() { TestForTrigger(handleAll: false, schemaId: SchemaMatch, condition: "true", action: trigger => { var result = sut.Triggers(new EnrichedContentEvent { SchemaId = SchemaMatch }, trigger); Assert.True(result); }); } [Fact] public void Should_not_trigger_check_when_schema_id_does_not_match() { TestForTrigger(handleAll: false, schemaId: SchemaNonMatch, condition: null, action: trigger => { var result = sut.Triggers(new EnrichedContentEvent { SchemaId = SchemaMatch }, trigger); Assert.False(result); }); } [Fact] public void Should_not_trigger_check_when_condition_does_not_matchs() { TestForTrigger(handleAll: false, schemaId: SchemaMatch, condition: "false", action: trigger => { var result = sut.Triggers(new EnrichedContentEvent { SchemaId = SchemaMatch }, trigger); Assert.False(result); }); } private void TestForTrigger(bool handleAll, NamedId schemaId, string condition, Action action) { var trigger = new ContentChangedTriggerV2 { HandleAll = handleAll }; if (schemaId != null) { trigger.Schemas = new ReadOnlyCollection(new List { new ContentChangedTriggerSchemaV2 { SchemaId = schemaId.Id, Condition = condition } }); } action(trigger); if (string.IsNullOrWhiteSpace(condition)) { A.CallTo(() => scriptEngine.Evaluate("event", A.Ignored, condition)) .MustNotHaveHappened(); } else { A.CallTo(() => scriptEngine.Evaluate("event", A.Ignored, condition)) .MustHaveHappened(); } } } }