mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.7 KiB
121 lines
3.7 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
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 Xunit;
|
|
|
|
namespace Squidex.Domain.Apps.Core.Operations.HandleRules.Triggers
|
|
{
|
|
public class AssetChangedTriggerTests
|
|
{
|
|
private readonly IScriptEngine scriptEngine = A.Fake<IScriptEngine>();
|
|
private readonly IRuleTriggerHandler sut;
|
|
|
|
public AssetChangedTriggerTests()
|
|
{
|
|
sut = new AssetChangedTriggerHandler(scriptEngine);
|
|
|
|
A.CallTo(() => scriptEngine.Evaluate("event", A<object>.Ignored, "true"))
|
|
.Returns(true);
|
|
|
|
A.CallTo(() => scriptEngine.Evaluate("event", A<object>.Ignored, "false"))
|
|
.Returns(false);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_not_trigger_precheck_when_event_type_not_correct()
|
|
{
|
|
TestForCondition(string.Empty, trigger =>
|
|
{
|
|
var result = sut.Triggers(new ContentCreated(), trigger);
|
|
|
|
Assert.False(result);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_trigger_precheck_when_event_type_correct()
|
|
{
|
|
TestForCondition(string.Empty, trigger =>
|
|
{
|
|
var result = sut.Triggers(new AssetCreated(), trigger);
|
|
|
|
Assert.True(result);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_not_trigger_check_when_event_type_not_correct()
|
|
{
|
|
TestForCondition(string.Empty, trigger =>
|
|
{
|
|
var result = sut.Triggers(new EnrichedContentEvent(), trigger);
|
|
|
|
Assert.False(result);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_trigger_check_when_condition_is_empty()
|
|
{
|
|
TestForCondition(string.Empty, trigger =>
|
|
{
|
|
var result = sut.Triggers(new EnrichedAssetEvent(), trigger);
|
|
|
|
Assert.True(result);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_trigger_check_when_condition_matchs()
|
|
{
|
|
TestForCondition("true", trigger =>
|
|
{
|
|
var result = sut.Triggers(new EnrichedAssetEvent(), trigger);
|
|
|
|
Assert.True(result);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_not_trigger_check_when_condition_does_not_matchs()
|
|
{
|
|
TestForCondition("false", trigger =>
|
|
{
|
|
var result = sut.Triggers(new EnrichedAssetEvent(), trigger);
|
|
|
|
Assert.False(result);
|
|
});
|
|
}
|
|
|
|
private void TestForCondition(string condition, Action<AssetChangedTriggerV2> action)
|
|
{
|
|
var trigger = new AssetChangedTriggerV2 { Condition = condition };
|
|
|
|
action(trigger);
|
|
|
|
if (string.IsNullOrWhiteSpace(condition))
|
|
{
|
|
A.CallTo(() => scriptEngine.Evaluate("event", A<object>.Ignored, condition))
|
|
.MustNotHaveHappened();
|
|
}
|
|
else
|
|
{
|
|
A.CallTo(() => scriptEngine.Evaluate("event", A<object>.Ignored, condition))
|
|
.MustHaveHappened();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|