Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests for populating the type feature provider #16399

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions test/OrchardCore.Tests.Modules/BaseThemeSample/DependentTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OrchardCore.Modules;

namespace BaseThemeSample;

public class BaseThemeFeatureIndependentStartup : StartupBase
{
}

[Feature("BaseThemeSample")]
public class BaseThemeSampleStartup : StartupBase
{
}
17 changes: 17 additions & 0 deletions test/OrchardCore.Tests.Modules/ModuleSample/DependentTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using OrchardCore.Environment.Extensions;
using OrchardCore.Modules;

namespace ModuleSample;

public class FeatureIndependentStartup : StartupBase { }

[Feature("Sample1")]
public class Sample1Startup : StartupBase
{
}

[Feature("Sample2")]
[FeatureTypeDiscovery(SkipExtension = true)]
public class SkippedDependentType
{
}
56 changes: 54 additions & 2 deletions test/OrchardCore.Tests/Extensions/ExtensionManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BaseThemeSample;
using ModuleSample;
using OrchardCore.DisplayManagement.Events;
using OrchardCore.DisplayManagement.Extensions;
using OrchardCore.Environment.Extensions;
Expand All @@ -24,14 +26,16 @@ private static readonly IApplicationContext _applicationContext
private readonly ExtensionManager _moduleScopedExtensionManager;
private readonly ExtensionManager _themeScopedExtensionManager;
private readonly ExtensionManager _moduleThemeScopedExtensionManager;

private readonly TypeFeatureProvider _moduleScopedTypeFeatureProvider = new TypeFeatureProvider();

public ExtensionManagerTests()
{
_moduleScopedExtensionManager = new ExtensionManager(
_applicationContext,
new[] { new ExtensionDependencyStrategy() },
new[] { new ExtensionPriorityStrategy() },
new TypeFeatureProvider(),
_moduleScopedTypeFeatureProvider,
_moduleFeatureProvider,
new NullLogger<ExtensionManager>()
);
Expand Down Expand Up @@ -167,7 +171,7 @@ public void GetFeaturesWithAIdShouldNotReturnFeaturesTheHaveADependencyOutsideOf
/* Theme Base Theme Dependencies */

[Fact]
public void GetFeaturesShouldReturnCorrectThemeHeirarchy()
public void GetFeaturesShouldReturnCorrectThemeHierarchy()
{
var features = _themeScopedExtensionManager.GetFeatures(["DerivedThemeSample"]);

Expand Down Expand Up @@ -222,5 +226,53 @@ public void ShouldReturnNotFoundExtensionInfoWhenNotFound()

Assert.False(extension.Exists);
}

/* The extension manager must populate the ITypeFeatureProvider correctly */

[Fact]
public void TypeFeatureProviderIsPopulatedWithComponentTypes()
{
var feature = _moduleScopedExtensionManager.GetFeatures(["Sample1"]).First();
var types = _moduleScopedTypeFeatureProvider.GetTypesForFeature(feature);

Assert.Equal(2, types.Count());
Assert.Contains(typeof(Sample1Startup), types);
Assert.Contains(typeof(FeatureIndependentStartup), types);
}

[Fact]
public void TypeFeatureProviderTypeMustBeMappedToAllFeatures()
{
// Types in modules that have no feature that matches the extension ID must be mapped to all features.
var features = _moduleScopedExtensionManager.GetFeatures(["Sample1", "Sample2", "Sample3", "Sample4"]);

foreach (var feature in features)
{
var types = _moduleScopedTypeFeatureProvider.GetTypesForFeature(feature);

Assert.Contains(typeof(FeatureIndependentStartup), types);
}
}

[Fact]
public void TypeFeatureProviderTypeMustBeMappedToExtensionFeature()
{
// Types in modules that have a feature that matches the extension ID must be mapped to that feature.
var feature = _moduleScopedExtensionManager.GetFeatures(["BaseThemeSample"]).First();
var types = _moduleScopedTypeFeatureProvider.GetTypesForFeature(feature);

Assert.Equal(2, types.Count());
Assert.Contains(typeof(BaseThemeSampleStartup), types);
Assert.Contains(typeof(BaseThemeFeatureIndependentStartup), types);
}

[Fact]
public void TypeFeatureProviderTypeMustBeSkipped()
{
var feature = _moduleScopedExtensionManager.GetFeatures(["Sample2"]).First();
var types = _moduleScopedTypeFeatureProvider.GetTypesForFeature(feature);

Assert.DoesNotContain(typeof(SkippedDependentType), types);
}
}
}