Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Oct 31, 2019
1 parent 14c9b84 commit b881949
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.Localization;
using OrchardCore.Tests;
using Xunit;

namespace OrchardCore.Tests.Localization
{
public class DataAnnotationsLocalizationTests
{
[Fact]
public async Task LocalizerReturnsDataAnnotationsTranslationFromInnerClass()
=> await StartupRunner.Run(typeof(DataAnnotationsLocalizationStartup),"ar", "مرحبا");

public class DataAnnotationsLocalizationStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddLocalization();
services.AddPortableObjectLocalization(options => options.ResourcesPath = "Localization/PoFiles");
services.Replace(ServiceDescriptor.Singleton<ILocalizationFileLocationProvider, StubPoFileLocationProvider>());
}

public void Configure(
IApplicationBuilder app,
IStringLocalizer<Model> localizer)
{
var supportedCultures = new[] { "ar", "en" };
app.UseRequestLocalization(options =>
options
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures)
.SetDefaultCulture("ar")
);

app.Run(async (context) =>
{
await context.Response.WriteAsync(localizer["Hello"]);
});
}
}

public class Model
{
[Display(Name ="مرحبا")]
public string Hello { get; set; }
}
}
}
4 changes: 4 additions & 0 deletions test/OrchardCore.Tests/Localization/PoFiles/ar.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#: OrchardCore.Tests.Localization\DataAnnotationsLocalizationTests.cs:77
msgctxt "OrchardCore.Tests.Localization.DataAnnotationsLocalizationTests.Model"
msgid "Hello"
msgstr "مرحبا"
37 changes: 37 additions & 0 deletions test/OrchardCore.Tests/Stubs/StubPoFileLocationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.Localization;
using OrchardCore.Tests;

namespace OrchardCore.Tests
{
public class StubPoFileLocationProvider : ILocalizationFileLocationProvider
{
private readonly IFileProvider _fileProvider;
private readonly string _resourcesContainer;

public StubPoFileLocationProvider(IHostEnvironment hostingEnvironment, IOptions<LocalizationOptions> localizationOptions)
{
var rootPath = new DirectoryInfo(hostingEnvironment.ContentRootPath).Parent.Parent.Parent.FullName;
_fileProvider = new PhysicalFileProvider(rootPath);
_resourcesContainer = localizationOptions.Value.ResourcesPath;
}

public IEnumerable<IFileInfo> GetLocations(string cultureName)
{
yield return _fileProvider.GetFileInfo(Path.Combine(_resourcesContainer, cultureName + ".po"));
}
}
}
30 changes: 30 additions & 0 deletions test/OrchardCore.Tests/Utilities/StartupRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.TestHost;
using Xunit;

namespace OrchardCore.Tests
{
internal static class StartupRunner
{
public static async Task Run(Type startupType, string culture, string expected)
{
var webHostBuilder = new WebHostBuilder().UseStartup(startupType);
var testHost = new TestServer(webHostBuilder);

var client = testHost.CreateClient();
var request = new HttpRequestMessage();
var cookieValue = $"c={culture}|uic={culture}";
request.Headers.Add("Cookie", $"{CookieRequestCultureProvider.DefaultCookieName}={cookieValue}");

var response = await client.SendAsync(request);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expected, await response.Content.ReadAsStringAsync());
}
}
}

0 comments on commit b881949

Please sign in to comment.