diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/LocalizedTemplateContentReaderFactory.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/LocalizedTemplateContentReaderFactory.cs index 3323ddfd31..981d1f8819 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/LocalizedTemplateContentReaderFactory.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/LocalizedTemplateContentReaderFactory.cs @@ -1,4 +1,5 @@ -using System.Collections.Concurrent; +using System; +using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; @@ -52,7 +53,13 @@ namespace Volo.Abp.TextTemplating.VirtualFiles var fileInfo = VirtualFileProvider.GetFileInfo(virtualPath); if (!fileInfo.Exists) { - throw new AbpException("Could not find a file/folder at the location: " + virtualPath); + var directoryContents = VirtualFileProvider.GetDirectoryContents(virtualPath); + if (!directoryContents.Exists) + { + throw new AbpException("Could not find a file/folder at the location: " + virtualPath); + } + + fileInfo = new VirtualDirectoryFileInfo(virtualPath, virtualPath, DateTimeOffset.UtcNow); } if (fileInfo.IsDirectory) diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFolderLocalizedTemplateContentReader.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFolderLocalizedTemplateContentReader.cs index 638a7db190..6fc7ff4847 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFolderLocalizedTemplateContentReader.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFolderLocalizedTemplateContentReader.cs @@ -11,18 +11,18 @@ namespace Volo.Abp.TextTemplating.VirtualFiles private Dictionary _dictionary; public async Task ReadContentsAsync( - IVirtualFileProvider virtualFileProvider, + IVirtualFileProvider virtualFileProvider, string virtualPath) { _dictionary = new Dictionary(); - var directoryInfo = virtualFileProvider.GetFileInfo(virtualPath); - if (!directoryInfo.IsDirectory) + var directoryContents = virtualFileProvider.GetDirectoryContents(virtualPath); + if (!directoryContents.Exists) { - throw new AbpException("Given virtual path is not a folder: " + virtualPath); + throw new AbpException("Could not find a folder at the location: " + virtualPath); } - foreach (var file in virtualFileProvider.GetDirectoryContents(virtualPath)) + foreach (var file in directoryContents) { if (file.IsDirectory) { @@ -43,4 +43,4 @@ namespace Volo.Abp.TextTemplating.VirtualFiles return _dictionary.GetOrDefault(cultureName); } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.TextTemplating.Tests/Volo.Abp.TextTemplating.Tests.csproj b/framework/test/Volo.Abp.TextTemplating.Tests/Volo.Abp.TextTemplating.Tests.csproj index 57c440341e..555c2934b0 100644 --- a/framework/test/Volo.Abp.TextTemplating.Tests/Volo.Abp.TextTemplating.Tests.csproj +++ b/framework/test/Volo.Abp.TextTemplating.Tests/Volo.Abp.TextTemplating.Tests.csproj @@ -10,8 +10,9 @@ - - + + Always + diff --git a/framework/test/Volo.Abp.TextTemplating.Tests/Volo/Abp/TextTemplating/VirtualFiles/LocalizedTemplateContentReaderFactory_Tests.cs b/framework/test/Volo.Abp.TextTemplating.Tests/Volo/Abp/TextTemplating/VirtualFiles/LocalizedTemplateContentReaderFactory_Tests.cs new file mode 100644 index 0000000000..b1dba4bdb5 --- /dev/null +++ b/framework/test/Volo.Abp.TextTemplating.Tests/Volo/Abp/TextTemplating/VirtualFiles/LocalizedTemplateContentReaderFactory_Tests.cs @@ -0,0 +1,59 @@ +using System.IO; +using System.Threading.Tasks; +using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.Primitives; +using Shouldly; +using Volo.Abp.VirtualFileSystem; +using Xunit; + +namespace Volo.Abp.TextTemplating.VirtualFiles +{ + public class LocalizedTemplateContentReaderFactory_Tests: AbpTextTemplatingTestBase + { + private readonly ITemplateDefinitionManager _templateDefinitionManager; + + public LocalizedTemplateContentReaderFactory_Tests() + { + _templateDefinitionManager = GetRequiredService(); + } + + [Fact] + public async Task Create_Should_Work_With_PhysicalFileProvider() + { + var localizedTemplateContentReaderFactory = new LocalizedTemplateContentReaderFactory( + new PhysicalFileVirtualFileProvider( + new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), + @"Volo\Abp\TextTemplating\")))); + + var reader = await localizedTemplateContentReaderFactory.CreateAsync(_templateDefinitionManager.Get(TestTemplates.WelcomeEmail)); + + reader.GetContentOrNull("en").ShouldBe("Welcome {{model.name}} to the abp.io!"); + reader.GetContentOrNull("tr").ShouldBe("Merhaba {{model.name}}, abp.io'ya hoşgeldiniz!"); + } + + class PhysicalFileVirtualFileProvider : IVirtualFileProvider + { + private readonly PhysicalFileProvider _physicalFileProvider; + + public PhysicalFileVirtualFileProvider(PhysicalFileProvider physicalFileProvider) + { + _physicalFileProvider = physicalFileProvider; + } + + public IFileInfo GetFileInfo(string subpath) + { + return _physicalFileProvider.GetFileInfo(subpath); + } + + public IDirectoryContents GetDirectoryContents(string subpath) + { + return _physicalFileProvider.GetDirectoryContents(subpath); + } + + public IChangeToken Watch(string filter) + { + return _physicalFileProvider.Watch(filter); + } + } + } +} diff --git a/framework/test/Volo.Abp.TextTemplating.Tests/Volo/Abp/TextTemplating/VirtualFiles/VirtualFileTemplateContributor_Tests.cs b/framework/test/Volo.Abp.TextTemplating.Tests/Volo/Abp/TextTemplating/VirtualFiles/VirtualFileTemplateContributor_Tests.cs index da2d4179de..e5c7138118 100644 --- a/framework/test/Volo.Abp.TextTemplating.Tests/Volo/Abp/TextTemplating/VirtualFiles/VirtualFileTemplateContributor_Tests.cs +++ b/framework/test/Volo.Abp.TextTemplating.Tests/Volo/Abp/TextTemplating/VirtualFiles/VirtualFileTemplateContributor_Tests.cs @@ -4,46 +4,42 @@ using Xunit; namespace Volo.Abp.TextTemplating.VirtualFiles { - //TODO: Make tests running again! - //public class VirtualFileTemplateContributor_Tests : AbpTextTemplatingTestBase - //{ - // [Fact] - // public async Task Should_Get_Localized_Content_By_Culture() - // { - // var contributor = new VirtualFileTemplateContentContributor( - // "/SampleTemplates/WelcomeEmail" - // ); + public class VirtualFileTemplateContributor_Tests : AbpTextTemplatingTestBase + { + private readonly ITemplateDefinitionManager _templateDefinitionManager; + private readonly VirtualFileTemplateContentContributor _virtualFileTemplateContentContributor; - // contributor.Initialize( - // new TemplateContentContributorInitializationContext( - // new TemplateDefinition("Test"), - // ServiceProvider - // ) - // ); + public VirtualFileTemplateContributor_Tests() + { + _templateDefinitionManager = GetRequiredService(); + _virtualFileTemplateContentContributor = GetRequiredService(); + } - // (await contributor - // .GetOrNullAsync("en")).ShouldBe("Welcome {{model.name}} to the abp.io!"); + [Fact] + public async Task Should_Get_Localized_Content_By_Culture() + { + (await _virtualFileTemplateContentContributor.GetOrNullAsync( + new TemplateContentContributorContext(_templateDefinitionManager.Get(TestTemplates.WelcomeEmail), + ServiceProvider, + "en"))) + .ShouldBe("Welcome {{model.name}} to the abp.io!"); - // (await contributor - // .GetOrNullAsync("tr")).ShouldBe("Merhaba {{model.name}}, abp.io'ya hoşgeldiniz!"); - // } + (await _virtualFileTemplateContentContributor.GetOrNullAsync( + new TemplateContentContributorContext(_templateDefinitionManager.Get(TestTemplates.WelcomeEmail), + ServiceProvider, + "tr"))) + .ShouldBe("Merhaba {{model.name}}, abp.io'ya hoşgeldiniz!"); + } - // [Fact] - // public async Task Should_Get_Non_Localized_Template_Content() - // { - // var contributor = new VirtualFileTemplateContentContributor( - // "/SampleTemplates/ForgotPasswordEmail.tpl" - // ); - - // contributor.Initialize( - // new TemplateContentContributorInitializationContext( - // new TemplateDefinition("Test"), - // ServiceProvider - // ) - // ); - - // (await contributor - // .GetOrNullAsync()).ShouldBe("{{l \"HelloText\"}}. Please click to the following link to get an email to reset your password!"); - // } - //} + [Fact] + public async Task Should_Get_Non_Localized_Template_Content() + { + (await _virtualFileTemplateContentContributor.GetOrNullAsync( + new TemplateContentContributorContext( + _templateDefinitionManager.Get(TestTemplates.ForgotPasswordEmail), + ServiceProvider, + null))) + .ShouldBe("{{L \"HelloText\" model.name}}, {{L \"HowAreYou\" }}. Please click to the following link to get an email to reset your password!"); + } + } }