Browse Source

Fixes for rule event formatter.

pull/528/head
Sebastian 6 years ago
parent
commit
568037f8a2
  1. 2
      backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleEventFormatter.cs
  2. 46
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs

2
backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleEventFormatter.cs

@ -29,7 +29,7 @@ namespace Squidex.Domain.Apps.Core.HandleRules
{ {
private const string Fallback = "null"; private const string Fallback = "null";
private static readonly Regex RegexPatternOld = new Regex(@"^(?<FullPath>(?<Type>[^_]*)_(?<Path>[^\s]*))", RegexOptions.Compiled); private static readonly Regex RegexPatternOld = new Regex(@"^(?<FullPath>(?<Type>[^_]*)_(?<Path>[^\s]*))", RegexOptions.Compiled);
private static readonly Regex RegexPatternNew = new Regex(@"^\{(?<FullPath>(?<Type>[^_]*)_(?<Path>[^\s]*))([\s]*\|[\s]*(?<Transform>.*)){0,1}\}", RegexOptions.Compiled); private static readonly Regex RegexPatternNew = new Regex(@"^\{(?<FullPath>(?<Type>[\w]*)_(?<Path>[\w\.\-]*))[\s]*(\|[\s]*(?<Transform>[^\}]*)){0,1}\}", RegexOptions.Compiled);
private readonly List<(string Pattern, Func<EnrichedEvent, string?> Replacer)> patterns = new List<(string Pattern, Func<EnrichedEvent, string?> Replacer)>(); private readonly List<(string Pattern, Func<EnrichedEvent, string?> Replacer)> patterns = new List<(string Pattern, Func<EnrichedEvent, string?> Replacer)>();
private readonly IJsonSerializer jsonSerializer; private readonly IJsonSerializer jsonSerializer;
private readonly IUrlGenerator urlGenerator; private readonly IUrlGenerator urlGenerator;

46
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleEventFormatterTests.cs

@ -97,7 +97,7 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
[Theory] [Theory]
[InlineData("Name $APP_NAME has id $APP_ID")] [InlineData("Name $APP_NAME has id $APP_ID")]
[InlineData("Name ${$EVENT_APPID.NAME} has id ${EVENT_APPID.ID}")] [InlineData("Name ${EVENT_APPID.NAME} has id ${EVENT_APPID.ID}")]
[InlineData("Script(`Name ${event.appId.name} has id ${event.appId.id}`)")] [InlineData("Script(`Name ${event.appId.name} has id ${event.appId.id}`)")]
public void Should_format_app_information_from_event(string script) public void Should_format_app_information_from_event(string script)
{ {
@ -543,15 +543,15 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
} }
[Theory] [Theory]
[InlineData("Found in ${ASSET_FILENAME | Escape | Upper}.docx", "Found in DONALD\\\"DUCK .docx")] [InlineData("Found in ${ASSET_FILENAME | Escape | Upper}.docx", "Found in DONALD\\\"DUCK.docx", "Donald\"Duck")]
[InlineData("Found in ${ASSET_FILENAME | Escape}.docx", "Found in Donald\\\"Duck .docx")] [InlineData("Found in ${ASSET_FILENAME | Escape}.docx", "Found in Donald\\\"Duck.docx", "Donald\"Duck")]
[InlineData("Found in ${ASSET_FILENAME | Upper}.docx", "Found in DONALD\"DUCK .docx")] [InlineData("Found in ${ASSET_FILENAME | Upper}.docx", "Found in DONALD DUCK.docx", "Donald Duck")]
[InlineData("Found in ${ASSET_FILENAME | Lower}.docx", "Found in donald\"duck .docx")] [InlineData("Found in ${ASSET_FILENAME | Lower}.docx", "Found in donald duck.docx", "Donald Duck")]
[InlineData("Found in ${ASSET_FILENAME | Slugify}.docx", "Found in donald-duck.docx")] [InlineData("Found in ${ASSET_FILENAME | Slugify}.docx", "Found in donald-duck.docx", "Donald Duck")]
[InlineData("Found in ${ASSET_FILENAME | Trim}.docx", "Found in Donald\"Duck.docx")] [InlineData("Found in ${ASSET_FILENAME | Trim}.docx", "Found in Donald Duck.docx", "Donald Duck ")]
public void Should_transform_replacements(string script, string expect) public void Should_transform_replacements(string script, string expect, string name)
{ {
var @event = new EnrichedAssetEvent { FileName = "Donald\"Duck " }; var @event = new EnrichedAssetEvent { FileName = name };
var result = sut.Format(script, @event); var result = sut.Format(script, @event);
@ -559,18 +559,30 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
} }
[Theory] [Theory]
[InlineData("From ${USER_NAME | Escape | Upper}", "From DONALD\\\"DUCK ")] [InlineData("From ${USER_NAME | Escape | Upper}", "From DONALD\\\"DUCK", "Donald\"Duck")]
[InlineData("From ${USER_NAME | Escape}", "From Donald\\\"Duck ")] [InlineData("From ${USER_NAME | Escape}", "From Donald\\\"Duck", "Donald\"Duck")]
[InlineData("From ${USER_NAME | Upper}", "From DONALD\"DUCK ")] [InlineData("From ${USER_NAME | Upper}", "From DONALD DUCK", "Donald Duck")]
[InlineData("From ${USER_NAME | Lower}", "From donald\"duck ")] [InlineData("From ${USER_NAME | Lower}", "From donald duck", "Donald Duck")]
[InlineData("From ${USER_NAME | Slugify}", "From donald-duck")] [InlineData("From ${USER_NAME | Slugify}", "From donald-duck", "Donald Duck")]
[InlineData("From ${USER_NAME | Trim}", "From Donald\"Duck")] [InlineData("From ${USER_NAME | Trim}", "From Donald Duck", "Donald Duck ")]
public void Should_transform_replacements_with_simple_pattern(string script, string expect) public void Should_transform_replacements_with_simple_pattern(string script, string expect, string name)
{ {
var @event = new EnrichedContentEvent { User = user }; var @event = new EnrichedContentEvent { User = user };
A.CallTo(() => user.Claims) A.CallTo(() => user.Claims)
.Returns(new List<Claim> { new Claim(SquidexClaimTypes.DisplayName, "Donald\"Duck ") }); .Returns(new List<Claim> { new Claim(SquidexClaimTypes.DisplayName, name) });
var result = sut.Format(script, @event);
Assert.Equal(expect, result);
}
[Theory]
[InlineData("{'Key':'${ASSET_FILENAME | Upper}'}", "{'Key':'DONALD DUCK'}")]
[InlineData("{'Key':'${ASSET_FILENAME}'}", "{'Key':'Donald Duck'}")]
public void Should_transform_json_examples(string script, string expect)
{
var @event = new EnrichedAssetEvent { FileName = "Donald Duck" };
var result = sut.Format(script, @event); var result = sut.Format(script, @event);

Loading…
Cancel
Save