public async Task LoadTemplates( string templateName, string culture, string expectedCulture, string defaultCulture) { var email = "alice@localhost"; var emailSender = new Mock <IEmailSender>(); var model = new Dictionary <string, object> { { "Name", "Foo" } }; emailSender .Setup(c => c.SendEmailAsync(It.IsAny <EmailMessage>())) .Returns(new Func <EmailMessage, Task>(async(emailMessage) => { Assert.NotNull(emailMessage); Assert.Equal(email, emailMessage.EmailTo); string subject = $"Subject {templateName} {expectedCulture} Foo"; Assert.Equal(subject, emailMessage.Subject); string html = $"HTML LayoutStart {expectedCulture} Html {templateName} {expectedCulture} Foo HTML LayoutEnd {expectedCulture}"; Assert.Equal(html, emailMessage.Html); string text = $"Text LayoutStart {expectedCulture} Text {templateName} {expectedCulture} Foo Text LayoutEnd {expectedCulture}"; Assert.Equal(text, emailMessage.Text); })); var logger = new NullLogger <DefaultEmailService>(); var options = new DefaultEmailServiceOptions { DefaultCulture = defaultCulture, TemplateDirectoryPath = "../../../Email/Templates" }; var httpContextAccessorMock = new Mock <IHttpContextAccessor>(); var emailService = new DefaultEmailService( options, logger, emailSender.Object, httpContextAccessorMock.Object); // Set culture CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture; CultureInfo originalUICulture = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentCulture = new CultureInfo(culture, false); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture, false); CultureInfo.CurrentCulture.ClearCachedData(); CultureInfo.CurrentUICulture.ClearCachedData(); // Send email await emailService.SendEmailAsync( templateName, email, model, true); // Reset UI Culture Thread.CurrentThread.CurrentCulture = originalCulture; Thread.CurrentThread.CurrentUICulture = originalUICulture; CultureInfo.CurrentCulture.ClearCachedData(); CultureInfo.CurrentUICulture.ClearCachedData(); }
//[Theory] //[InlineData("Template1", "en-US", "en-US", "en-US")] //[InlineData("Template1", "de-DE", "de-DE", "de-DE")] //[InlineData("Template1", "ru-RU", "en-US", "en-US")] public async Task LoadTemplates( string templateName, string culture, string expectedCulture, string defaultCulture) { string email = "alice@localhost"; Mock <IEmailSender> emailSender = new Mock <IEmailSender>(); Dictionary <string, object> model = new Dictionary <string, object> { { "Name", "Foo" } }; emailSender .Setup(c => c.SendEmailAsync(It.IsAny <EmailMessage>())) .Returns(new Func <EmailMessage, Task>((emailMessage) => { Assert.NotNull(emailMessage); Assert.Equal(email, emailMessage.EmailTo); string subject = $"Subject {templateName} {expectedCulture} Foo"; Assert.Equal(subject, emailMessage.Subject); string html = $"HTML LayoutStart {expectedCulture} Html {templateName} {expectedCulture} Foo HTML LayoutEnd {expectedCulture}"; Assert.Equal(html, emailMessage.Html); string text = $"Text LayoutStart {expectedCulture} Text {templateName} {expectedCulture} Foo Text LayoutEnd {expectedCulture}"; Assert.Equal(text, emailMessage.Text); return(Task.FromResult(0)); })); NullLogger <DefaultEmailService> logger = new NullLogger <DefaultEmailService>(); DefaultEmailServiceOptions options = new DefaultEmailServiceOptions { DefaultCulture = defaultCulture }; Mock <IHttpContextAccessor> httpContextAccessorMock = new Mock <IHttpContextAccessor>(); Mock <IResourceStore> resourceStoreMock = new Mock <IResourceStore>(); resourceStoreMock .Setup(c => c.GetEmailTemplateAsync( It.Is <string>(s => s.Equals(culture)), It.Is <string>(s => s.Equals(templateName)) )) .Returns(new Func <string, string, Task <Resource> >((tplCulture, tplKey) => { return(Task.FromResult(new Resource { Culture = tplCulture, Key = tplKey, Value = $"HTML LayoutStart {expectedCulture} Html {templateName} {expectedCulture} Foo HTML LayoutEnd {expectedCulture}" })); })); DefaultEmailService emailService = new DefaultEmailService( options, emailSender.Object, resourceStoreMock.Object, logger, new DefaultTokenizer()); // Set culture CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture; CultureInfo originalUICulture = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentCulture = new CultureInfo(culture, false); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture, false); CultureInfo.CurrentCulture.ClearCachedData(); CultureInfo.CurrentUICulture.ClearCachedData(); // Send email await emailService.SendEmailAsync( templateName, email, model, true); // Reset UI Culture Thread.CurrentThread.CurrentCulture = originalCulture; Thread.CurrentThread.CurrentUICulture = originalUICulture; CultureInfo.CurrentCulture.ClearCachedData(); CultureInfo.CurrentUICulture.ClearCachedData(); }