mirror of https://github.com/abpframework/abp.git
Browse Source
Render the `lang` and the `dir` of the current culture in email templatespull/25975/head
committed by
GitHub
17 changed files with 323 additions and 16 deletions
@ -0,0 +1,44 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.Testing; |
||||
|
using Volo.Abp.TextTemplating; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Emailing.Templates; |
||||
|
|
||||
|
public abstract class AbpEmailingTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule> |
||||
|
where TStartupModule : IAbpModule |
||||
|
{ |
||||
|
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
options.UseAutofac(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class StandardEmailTemplates_Tests : AbpEmailingTestBase<AbpEmailingTestModule> |
||||
|
{ |
||||
|
private readonly ITemplateRenderer _templateRenderer; |
||||
|
|
||||
|
public StandardEmailTemplates_Tests() |
||||
|
{ |
||||
|
_templateRenderer = GetRequiredService<ITemplateRenderer>(); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData("en", "<html lang=\"en\" dir=\"ltr\"")] |
||||
|
[InlineData("fr", "<html lang=\"fr\" dir=\"ltr\"")] |
||||
|
[InlineData("ar", "<html lang=\"ar\" dir=\"rtl\"")] |
||||
|
public async Task Should_Declare_The_Language_And_The_Direction_Of_The_Rendering_Culture( |
||||
|
string cultureName, string expectedHtmlTag) |
||||
|
{ |
||||
|
var result = await _templateRenderer.RenderAsync( |
||||
|
StandardEmailTemplates.Message, |
||||
|
model: new { message = "This is email body..." }, |
||||
|
cultureName: cultureName |
||||
|
); |
||||
|
|
||||
|
result.ShouldContain(expectedHtmlTag); |
||||
|
result.ShouldContain("This is email body..."); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.TextTemplating.Razor.SampleTemplates; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating.Razor; |
||||
|
|
||||
|
public class RazorTemplateRenderingEngine_CultureContext_Tests : AbpTextTemplatingTestBase<RazorTextTemplatingTestModule> |
||||
|
{ |
||||
|
private readonly ITemplateRenderer _templateRenderer; |
||||
|
|
||||
|
public RazorTemplateRenderingEngine_CultureContext_Tests() |
||||
|
{ |
||||
|
_templateRenderer = GetRequiredService<ITemplateRenderer>(); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData("en", "<html lang=\"en\" dir=\"ltr\">")] |
||||
|
[InlineData("fr", "<html lang=\"fr\" dir=\"ltr\">")] |
||||
|
[InlineData("ar", "<html lang=\"ar\" dir=\"rtl\">")] |
||||
|
public async Task Should_Render_Culture_And_Text_Direction_Of_The_Rendering_Culture(string cultureName, string expected) |
||||
|
{ |
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
RazorTestTemplates.CultureContext, |
||||
|
cultureName: cultureName |
||||
|
)).Trim().ShouldBe(expected); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Not_Leak_The_Culture_Of_A_Rendering_Into_The_Next_One() |
||||
|
{ |
||||
|
var globalContext = new Dictionary<string, object>(); |
||||
|
|
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
RazorTestTemplates.CultureContext, |
||||
|
cultureName: "en", |
||||
|
globalContext: globalContext |
||||
|
)).Trim().ShouldBe("<html lang=\"en\" dir=\"ltr\">"); |
||||
|
|
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
RazorTestTemplates.CultureContext, |
||||
|
cultureName: "ar", |
||||
|
globalContext: globalContext |
||||
|
)).Trim().ShouldBe("<html lang=\"ar\" dir=\"rtl\">"); |
||||
|
|
||||
|
globalContext.ShouldBeEmpty(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Keep_The_Comparer_Of_The_Context_Of_The_Caller() |
||||
|
{ |
||||
|
var globalContext = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) |
||||
|
{ |
||||
|
["ABP_CULTURE"] = "custom" |
||||
|
}; |
||||
|
|
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
RazorTestTemplates.CultureContext, |
||||
|
cultureName: "ar", |
||||
|
globalContext: globalContext |
||||
|
)).Trim().ShouldBe("<html lang=\"custom\" dir=\"rtl\">"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,2 @@ |
|||||
|
@inherits Volo.Abp.TextTemplating.Razor.RazorTemplatePageBase |
||||
|
<html lang="@GlobalContext["abp_culture"]" dir="@GlobalContext["abp_dir"]"> |
||||
@ -0,0 +1 @@ |
|||||
|
<html lang="{{abp_culture}}" dir="{{abp_dir}}"> |
||||
@ -0,0 +1,107 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Globalization; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Localization; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating.Scriban; |
||||
|
|
||||
|
public class ScribanTemplateRenderingEngine_CultureContext_Tests : AbpTextTemplatingTestBase<ScribanTextTemplatingTestModule> |
||||
|
{ |
||||
|
private readonly ITemplateRenderer _templateRenderer; |
||||
|
|
||||
|
public ScribanTemplateRenderingEngine_CultureContext_Tests() |
||||
|
{ |
||||
|
_templateRenderer = GetRequiredService<ITemplateRenderer>(); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData("en", "<html lang=\"en\" dir=\"ltr\">")] |
||||
|
[InlineData("fr", "<html lang=\"fr\" dir=\"ltr\">")] |
||||
|
[InlineData("ar", "<html lang=\"ar\" dir=\"rtl\">")] |
||||
|
public async Task Should_Render_Culture_And_Text_Direction_Of_The_Rendering_Culture(string cultureName, string expected) |
||||
|
{ |
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
ScribanTestTemplateDefinitionProvider.CultureContext, |
||||
|
cultureName: cultureName |
||||
|
)).ShouldBe(expected); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Keep_The_Values_Passed_By_The_Caller() |
||||
|
{ |
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
ScribanTestTemplateDefinitionProvider.CultureContext, |
||||
|
cultureName: "ar", |
||||
|
globalContext: new Dictionary<string, object> |
||||
|
{ |
||||
|
[TemplateRenderingEngineBase.CultureContextKey] = "custom" |
||||
|
} |
||||
|
)).ShouldBe("<html lang=\"custom\" dir=\"rtl\">"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Not_Leak_The_Culture_Of_A_Rendering_Into_The_Next_One() |
||||
|
{ |
||||
|
var globalContext = new Dictionary<string, object>(); |
||||
|
|
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
ScribanTestTemplateDefinitionProvider.CultureContext, |
||||
|
cultureName: "en", |
||||
|
globalContext: globalContext |
||||
|
)).ShouldBe("<html lang=\"en\" dir=\"ltr\">"); |
||||
|
|
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
ScribanTestTemplateDefinitionProvider.CultureContext, |
||||
|
cultureName: "ar", |
||||
|
globalContext: globalContext |
||||
|
)).ShouldBe("<html lang=\"ar\" dir=\"rtl\">"); |
||||
|
|
||||
|
globalContext.ShouldBeEmpty(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Not_Write_The_Layout_Content_Into_The_Context_Of_The_Caller() |
||||
|
{ |
||||
|
var globalContext = new Dictionary<string, object>(); |
||||
|
|
||||
|
await _templateRenderer.RenderAsync( |
||||
|
TestTemplates.ForgotPasswordEmail, |
||||
|
model: new { link = "http://abp.io" }, |
||||
|
cultureName: "en", |
||||
|
globalContext: globalContext |
||||
|
); |
||||
|
|
||||
|
globalContext.ShouldBeEmpty(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Keep_The_Comparer_Of_The_Context_Of_The_Caller() |
||||
|
{ |
||||
|
// Scriban looks the key up as the template writes it, so the value of a case insensitive context
|
||||
|
// has to be re-inserted under the canonical casing to stay reachable.
|
||||
|
var globalContext = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) |
||||
|
{ |
||||
|
["ABP_CULTURE"] = "custom" |
||||
|
}; |
||||
|
|
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
ScribanTestTemplateDefinitionProvider.CultureContext, |
||||
|
cultureName: "ar", |
||||
|
globalContext: globalContext |
||||
|
)).ShouldBe("<html lang=\"custom\" dir=\"rtl\">"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Fall_Back_To_English_For_The_Invariant_Culture() |
||||
|
{ |
||||
|
using (CultureHelper.Use(CultureInfo.InvariantCulture)) |
||||
|
{ |
||||
|
(await _templateRenderer.RenderAsync( |
||||
|
ScribanTestTemplateDefinitionProvider.CultureContext |
||||
|
)).ShouldBe("<html lang=\"en\" dir=\"ltr\">"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue