diff --git a/src/OrchardCore.Modules/OrchardCore.Liquid/Startup.cs b/src/OrchardCore.Modules/OrchardCore.Liquid/Startup.cs index f2a5fa79451..49fba8d9f5f 100644 --- a/src/OrchardCore.Modules/OrchardCore.Liquid/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.Liquid/Startup.cs @@ -57,6 +57,7 @@ public override void ConfigureServices(IServiceCollection services) { JsonObject o => new ObjectValue(o), JsonDynamicObject o => new ObjectValue((JsonObject)o), + JsonValue o => o.GetObjectValue(), DateTime d => new ObjectValue(d), _ => null }; diff --git a/test/OrchardCore.Tests/DisplayManagement/LiquidTests.cs b/test/OrchardCore.Tests/DisplayManagement/LiquidTests.cs new file mode 100644 index 00000000000..1653d73d35e --- /dev/null +++ b/test/OrchardCore.Tests/DisplayManagement/LiquidTests.cs @@ -0,0 +1,122 @@ +using System.Text.Json; +using Fluid; +using OrchardCore.ContentFields.Fields; +using OrchardCore.ContentManagement; +using OrchardCore.Liquid; +using OrchardCore.Tests.Apis.Context; + +namespace OrchardCore.Tests.DisplayManagement; + +public class LiquidTests +{ + [Fact] + public async Task ComparingTextField_ReturnsCorrectValue() + { + var context = new SiteContext(); + await context.InitializeAsync(); + await context.UsingTenantScopeAsync(async scope => + { + var template = """ + {% if Model.ContentItem.Content.MyPart.myField.Text == "Some test value" %}true{% else %}false{% endif %} + """; + + var contentItem = new ContentItem(); + contentItem.GetOrCreate(); + contentItem.Alter(x => x.Text = "Some text"); + contentItem.Alter(x => + { + x.GetOrCreate("myField"); + x.Alter("myField", f => f.Text = "Some test value"); + }); + + var json = JConvert.SerializeObject(contentItem); + + var testModel = JConvert.DeserializeObject(json); + + var liquidTemplateManager = scope.ServiceProvider.GetRequiredService(); + var result = await liquidTemplateManager.RenderStringAsync(template, + NullEncoder.Default, + testModel); + + Assert.Equal("true", result); + }); + } + + [Fact] + public async Task ComparingDateTimeField_ReturnsCorrectValue() + { + var context = new SiteContext(); + await context.InitializeAsync(); + await context.UsingTenantScopeAsync(async scope => + { + var template = """ + {% assign myDate = "2024-05-14 08:00:00Z" | date %} + {% assign myDateField = Model.ContentItem.Content.MyPart.myField.Value | date %} + {% if myDateField == myDate %}true{% else %}false{% endif %} + """; + + var contentItem = new ContentItem(); + contentItem.GetOrCreate(); + contentItem.Alter(x => x.Text = "Some text"); + contentItem.Alter(x => + { + x.GetOrCreate("myField"); + x.Alter("myField", f => f.Value = new DateTime(2024, 5, 14, 8, 0, 0, DateTimeKind.Utc)); + }); + + var json = JConvert.SerializeObject(contentItem); + + var testModel = JConvert.DeserializeObject(json); + + var liquidTemplateManager = scope.ServiceProvider.GetRequiredService(); + var result = await liquidTemplateManager.RenderStringAsync(template, + NullEncoder.Default, + testModel); + + Assert.Contains("true", result); + }); + } + + [Fact] + public async Task ComparingNumericField_ReturnsCorrectValue() + { + var context = new SiteContext(); + await context.InitializeAsync(); + await context.UsingTenantScopeAsync(async scope => + { + var template = """ + {% if Model.ContentItem.Content.MyPart.myField.Value == 123 %}true{% else %}false{% endif %} + """; + + var contentItem = new ContentItem(); + contentItem.GetOrCreate(); + contentItem.Alter(x => x.Text = "Some text"); + contentItem.Alter(x => + { + x.GetOrCreate("myField"); + x.Alter("myField", f => f.Value = 123); + }); + + var json = JConvert.SerializeObject(contentItem); + + var testModel = JConvert.DeserializeObject(json); + + var liquidTemplateManager = scope.ServiceProvider.GetRequiredService(); + var result = await liquidTemplateManager.RenderStringAsync(template, + NullEncoder.Default, + testModel); + + Assert.Equal("true", result); + }); + } + + public class MyPart : ContentPart + { + public string Text { get; set; } + } + + public class MyField : ContentField + { + public int Value { get; set; } + } +}