Skip to content

Commit 06c0813

Browse files
committed
Add testing APIs
1 parent 36d7018 commit 06c0813

15 files changed

Lines changed: 414 additions & 2 deletions

OrchardCoreContrib.Testing.sln

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 17
4-
VisualStudioVersion = 17.9.34701.34
3+
# Visual Studio Version 18
4+
VisualStudioVersion = 18.1.11312.151 d18.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{20F306B8-D63F-4A61-AF6E-64B4E0918E30}"
77
EndProject
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrchardCoreContrib.Testing.
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrchardCoreContrib.Testing.UI.Tests", "test\OrchardCoreContrib.Testing.UI.Tests\OrchardCoreContrib.Testing.UI.Tests.csproj", "{985F5AD6-8C18-4E63-A35C-A5673F237A4D}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrchardCoreContrib.Testing", "src\OrchardCoreContrib.Testing\OrchardCoreContrib.Testing.csproj", "{60B9E226-55FF-4B5C-BFFE-57A14DE6CC58}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -25,13 +27,18 @@ Global
2527
{985F5AD6-8C18-4E63-A35C-A5673F237A4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
2628
{985F5AD6-8C18-4E63-A35C-A5673F237A4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
2729
{985F5AD6-8C18-4E63-A35C-A5673F237A4D}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{60B9E226-55FF-4B5C-BFFE-57A14DE6CC58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{60B9E226-55FF-4B5C-BFFE-57A14DE6CC58}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{60B9E226-55FF-4B5C-BFFE-57A14DE6CC58}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{60B9E226-55FF-4B5C-BFFE-57A14DE6CC58}.Release|Any CPU.Build.0 = Release|Any CPU
2834
EndGlobalSection
2935
GlobalSection(SolutionProperties) = preSolution
3036
HideSolutionNode = FALSE
3137
EndGlobalSection
3238
GlobalSection(NestedProjects) = preSolution
3339
{A9E5A40B-78F8-4F02-9E73-C74F395B5BBD} = {20F306B8-D63F-4A61-AF6E-64B4E0918E30}
3440
{985F5AD6-8C18-4E63-A35C-A5673F237A4D} = {27507B03-7D7C-492D-92A4-FF5812B9D7DB}
41+
{60B9E226-55FF-4B5C-BFFE-57A14DE6CC58} = {20F306B8-D63F-4A61-AF6E-64B4E0918E30}
3542
EndGlobalSection
3643
GlobalSection(ExtensibilityGlobals) = postSolution
3744
SolutionGuid = {DD153137-BF4D-4977-A4D7-4C448D23479A}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace OrchardCoreContrib.Testing;
2+
3+
public class AgencySiteContext<TEntryPoint> : SiteContextBase<OrchardCoreStartup<TEntryPoint>> where TEntryPoint : class
4+
{
5+
public AgencySiteContext() => Options.RecipeName = "Agency";
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace OrchardCoreContrib.Testing;
2+
3+
public class BlogSiteContext<TEntryPoint> : SiteContextBase<OrchardCoreStartup<TEntryPoint>> where TEntryPoint : class
4+
{
5+
public BlogSiteContext() => Options.RecipeName = "Blog";
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using OrchardCore.Environment.Shell;
2+
3+
namespace OrchardCoreContrib.Testing;
4+
5+
public interface ISiteContext : IDisposable
6+
{
7+
static IShellHost ShellHost { get; }
8+
9+
static IShellSettingsManager ShellSettingsManager { get; }
10+
11+
static HttpClient DefaultTenantClient { get; }
12+
13+
SiteContextOptions Options { init; get; }
14+
15+
HttpClient Client { get; }
16+
17+
string TenantName { get; }
18+
19+
Task InitializeAsync();
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace OrchardCoreContrib.Testing;
2+
3+
public interface ISiteContext<TSiteStartup> : ISiteContext where TSiteStartup : class
4+
{
5+
static OrchardCoreWebApplicationFactory<TSiteStartup> Site { get; }
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using OrchardCore.Modules;
2+
using OrchardCore.Modules.Manifest;
3+
using System.Reflection;
4+
5+
namespace OrchardCoreContrib.Testing;
6+
7+
internal sealed class ModuleNamesProvider : IModuleNamesProvider
8+
{
9+
private readonly IEnumerable<string> _moduleNames;
10+
11+
public ModuleNamesProvider(Assembly assembly)
12+
{
13+
ArgumentNullException.ThrowIfNull(assembly);
14+
15+
_moduleNames = Assembly.Load(new AssemblyName(assembly.GetName().Name))
16+
.GetCustomAttributes<ModuleNameAttribute>()
17+
.Select(m => m.Name);
18+
}
19+
20+
public IEnumerable<string> GetModuleNames() => _moduleNames;
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="OrchardCore.Application.Cms.Core.Targets" Version="2.2.1" />
10+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.24" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Routing;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using OrchardCore.Modules;
7+
using OrchardCoreContrib.Testing.Security;
8+
9+
namespace OrchardCoreContrib.Testing;
10+
11+
public class OrchardCoreStartup<TEntryPoint> : StartupBase where TEntryPoint : class
12+
{
13+
public override void ConfigureServices(IServiceCollection services)
14+
{
15+
services.AddOrchardCms(builder => builder
16+
.AddSetupFeatures("OrchardCore.Tenants")
17+
.ConfigureServices(serviceCollection =>
18+
{
19+
serviceCollection.AddScoped<IAuthorizationHandler, PermissionContextAuthorizationHandler>(sp =>
20+
new PermissionContextAuthorizationHandler(sp.GetRequiredService<IHttpContextAccessor>(), SiteContextOptions.PermissionsContexts));
21+
})
22+
.Configure(appBuilder => appBuilder.UseAuthorization()));
23+
24+
services.AddSingleton<IModuleNamesProvider>(new ModuleNamesProvider(typeof(TEntryPoint).Assembly));
25+
}
26+
27+
public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
28+
=> app.UseOrchardCore();
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.Mvc.Testing;
3+
using Microsoft.AspNetCore.TestHost;
4+
using Microsoft.Extensions.Hosting;
5+
6+
namespace OrchardCoreContrib.Testing;
7+
8+
public class OrchardCoreWebApplicationFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
9+
{
10+
protected override void ConfigureWebHost(IWebHostBuilder builder)
11+
{
12+
var shellsApplicationDataPath = Path.Combine(Directory.GetCurrentDirectory(), "App_Data");
13+
14+
if (Directory.Exists(shellsApplicationDataPath))
15+
{
16+
Directory.Delete(shellsApplicationDataPath, true);
17+
}
18+
19+
builder.UseContentRoot(Directory.GetCurrentDirectory());
20+
}
21+
22+
protected override IWebHostBuilder CreateWebHostBuilder()
23+
=> WebHostBuilderFactory.CreateFromAssemblyEntryPoint(typeof(TEntryPoint).Assembly, []);
24+
25+
protected override IHostBuilder CreateHostBuilder()
26+
=> Host.CreateDefaultBuilder().ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<TEntryPoint>());
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace OrchardCoreContrib.Testing;
2+
3+
public class SaasSiteContext<TEntryPoint> : SiteContextBase<OrchardCoreStartup<TEntryPoint>> where TEntryPoint : class
4+
{
5+
public SaasSiteContext() => Options.RecipeName = "Saas";
6+
}

0 commit comments

Comments
 (0)