Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes comparing content fields in Liquid #16053

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/OrchardCore.Modules/OrchardCore.Liquid/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
122 changes: 122 additions & 0 deletions test/OrchardCore.Tests/DisplayManagement/LiquidTests.cs
Original file line number Diff line number Diff line change
@@ -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<MyPart>();
contentItem.Alter<MyPart>(x => x.Text = "Some text");
contentItem.Alter<MyPart>(x =>
{
x.GetOrCreate<TextField>("myField");
x.Alter<TextField>("myField", f => f.Text = "Some test value");
});

var json = JConvert.SerializeObject(contentItem);

var testModel = JConvert.DeserializeObject<ContentItem>(json);

var liquidTemplateManager = scope.ServiceProvider.GetRequiredService<ILiquidTemplateManager>();
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<MyPart>();
contentItem.Alter<MyPart>(x => x.Text = "Some text");
contentItem.Alter<MyPart>(x =>
{
x.GetOrCreate<DateTimeField>("myField");
x.Alter<DateTimeField>("myField", f => f.Value = new DateTime(2024, 5, 14, 8, 0, 0, DateTimeKind.Utc));
});

var json = JConvert.SerializeObject(contentItem);

var testModel = JConvert.DeserializeObject<ContentItem>(json);

gvkries marked this conversation as resolved.
Show resolved Hide resolved
var liquidTemplateManager = scope.ServiceProvider.GetRequiredService<ILiquidTemplateManager>();
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<MyPart>();
contentItem.Alter<MyPart>(x => x.Text = "Some text");
contentItem.Alter<MyPart>(x =>
{
x.GetOrCreate<MyField>("myField");
x.Alter<MyField>("myField", f => f.Value = 123);
});

gvkries marked this conversation as resolved.
Show resolved Hide resolved
var json = JConvert.SerializeObject(contentItem);

var testModel = JConvert.DeserializeObject<ContentItem>(json);

var liquidTemplateManager = scope.ServiceProvider.GetRequiredService<ILiquidTemplateManager>();
var result = await liquidTemplateManager.RenderStringAsync(template,
NullEncoder.Default,
testModel);

Assert.Equal("true", result);
});
}

gvkries marked this conversation as resolved.
Show resolved Hide resolved
public class MyPart : ContentPart
{
public string Text { get; set; }
}

gvkries marked this conversation as resolved.
Show resolved Hide resolved
public class MyField : ContentField
{
public int Value { get; set; }
}
}