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

Introduce OrchardCore.Testing APIs #13018

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open

Introduce OrchardCore.Testing APIs #13018

wants to merge 38 commits into from

Conversation

hishamco
Copy link
Member

@hishamco hishamco commented Dec 31, 2022

This is one of the PRs that I'm working on testing part of Orchard Core, the plan is to provide an essential testing APIs infrastructure for creating unit tests easily for OC APIs and modules

For reference #11865 (comment)
Fixes #11865.

@hishamco hishamco mentioned this pull request Dec 31, 2022
# Conflicts:
#	src/OrchardCore/OrchardCore.Testing/Apis/Security/PermissionContextAuthorizationHandler.cs
#	src/OrchardCore/OrchardCore.Testing/Data/TablePrefixGenerator.cs
#	src/OrchardCore/OrchardCore.Testing/Extensions/HttpContentExtensions.cs
#	test/OrchardCore.Testing.Tests/Data/TablePrefixGeneratorTests.cs
#	test/OrchardCore.Testing.Tests/Security/PermissionHandlerTests.cs
#	test/OrchardCore.Tests/Apis/ContentManagement/ContentApiController/BlogPostApiControllerTests.cs
#	test/OrchardCore.Tests/Apis/ContentManagement/DeploymentPlans/BlogPostCreateDeploymentPlanTests.cs
#	test/OrchardCore.Tests/Apis/ContentManagement/DeploymentPlans/BlogPostUpdateDeploymentPlanTests.cs
#	test/OrchardCore.Tests/Apis/Context/BlogPostApiControllerContext.cs
#	test/OrchardCore.Tests/Apis/Context/Extensions/HttpRequestExtensions.cs
#	test/OrchardCore.Tests/Apis/Context/MvcTestFixture.cs
#	test/OrchardCore.Tests/Apis/Context/SiteContext.cs
#	test/OrchardCore.Tests/Apis/Context/SiteStartup.cs
#	test/OrchardCore.Tests/Apis/Context/TestRecipeHarvester.cs
#	test/OrchardCore.Tests/Apis/GraphQL/ValidationRules/RequiresPermissionValidationRuleTests.cs
#	test/OrchardCore.Tests/Apis/Lucene/LuceneQueryTests.cs
#	test/OrchardCore.Tests/DisplayManagement/Decriptors/DefaultShapeTableManagerTests.cs
#	test/OrchardCore.Tests/DisplayManagement/DefaultDisplayManagerTests.cs
#	test/OrchardCore.Tests/DisplayManagement/ShapeFactoryTests.cs
#	test/OrchardCore.Tests/DisplayManagement/ShapeHelperTests.cs
#	test/OrchardCore.Tests/DisplayManagement/ShapeSerializerTests.cs
#	test/OrchardCore.Tests/Email/EmailTests.cs
#	test/OrchardCore.Tests/Extensions/ExtensionManagerTests.cs
#	test/OrchardCore.Tests/Localization/PortableObjectStringLocalizerFactoryTests.cs
#	test/OrchardCore.Tests/Localization/PortableObjectStringLocalizerTests.cs
#	test/OrchardCore.Tests/Modules/OrchardCore.OpenId/OpenIdServerDeploymentSourceTests.cs
#	test/OrchardCore.Tests/Modules/OrchardCore.Roles/RolesPermissionsHandlerTests.cs
#	test/OrchardCore.Tests/OrchardCore.Users/UserValidatorTests.cs
#	test/OrchardCore.Tests/OrchardCore.Users/UsersMockHelper.cs
#	test/OrchardCore.Tests/ResourceManagement/ResourceDefinitionTests.cs
#	test/OrchardCore.Tests/ResourceManagement/ResourceManagerTests.cs
#	test/OrchardCore.Tests/Routing/AutorouteEntriesTests.cs
#	test/OrchardCore.Tests/Security/PermissionHandlerHelper.cs
#	test/OrchardCore.Tests/Shell/ShellContainerFactoryTests.cs
#	test/OrchardCore.Tests/Stubs/MemoryFileBuilder.cs
#	test/OrchardCore.Tests/Stubs/StubExtensionManager.cs
#	test/OrchardCore.Tests/Stubs/StubHostingEnvironment.cs
#	test/OrchardCore.Tests/Stubs/StubPoFileLocationProvider.cs
#	test/OrchardCore.Tests/Workflows/WorkflowManagerTests.cs
@hishamco
Copy link
Member Author

@sebastienros could you please have a look, coz we need to move forward after this to cover UI testing

@sebastienros
Copy link
Member

Can you give more details about the changes in this PR? Like "New project named ... that users can reference to create tests", and other new interfaces or methods.

Copy link
Contributor

@MichaelPetrinolis MichaelPetrinolis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OrchardCore.Testing should not depend on CMS.Web project

}

public static ConcurrentDictionary<string, PermissionsContext> PermissionsContexts { get; set; }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a static action to be able to configure the IHostBuilder
public static Action ConfigureHostBuilderAction { get; set; }
So in a Context, in a static constructor one could configure the host eg. to use NServicebus

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might defer this for now and add it later, the aim is to extract what we have nowadays into OC.Testing then we can add helper features


public class OrchardCoreTestFixture<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

override the CreateHost method to configure the IHostBuilder based on the SiteContextOptions.ConfigureHostBuilderAction
eg.

    protected override IHost CreateHost(IHostBuilder builder)
    {
        if (SiteContextOptions.ConfigureHostBuilderAction != null)
        {
            SiteContextOptions.ConfigureHostBuilderAction.Invoke(builder);
        }
        return base.CreateHost(builder);
    }

@hishamco
Copy link
Member Author

Thanks @MichaelPetrinolis for your comment, I will try to react to them, but still I'm asking can we add it as a separated NuGet package or for OC use

@MichaelPetrinolis
Copy link
Contributor

MichaelPetrinolis commented Feb 24, 2023

@hishamco an official OC Testing NuGet package, which is used internally by OC project for unit/integration testing, would be beneficial for the community. As a developer, having OOB stubs and mocks that I can use in my own module testing, would be nice to have. Also easily configuring a Site Context and perform integration testing to my module controllers, would be awesome.

please note that the UseCultureAttribute causes issues when running dotnet test from CLI. The reference to xunit seems to cause the failure.

  An assembly specified in the application dependencies manifest (OrchardCore.Testing.deps.json) was not found:
    package: 'AngleSharp', version: '0.17.1'
    path: 'lib/netstandard2.0/AngleSharp.dll'
. Please check the diagnostic logs for more information.

@hishamco
Copy link
Member Author

@hishamco an official OC Testing NuGet package, which is used internally by OC project for unit/integration testing, would be beneficial for the community. As a developer, having OOB stubs and mocks that I can use in my own module testing, would be nice to have. Also easily configuring a Site Context and perform integration testing to my module controllers, would be awesome.

My proposal from the begging to have OrchardCore.Testing for unit testing purpose and probably functional testing, I'm still prefer to separate integration and UI testing into another packages after this PR

please note that the UseCultureAttribute causes issues when running dotnet test from CLI. The reference to xunit seems to cause the failure.

AngleSharp is not related to what I did

# Conflicts:
#	test/OrchardCore.Tests/Apis/Context/SiteContext.cs
#	test/OrchardCore.Tests/Stubs/TestShapeBindingResolver.cs
#	test/OrchardCore.Tests/Stubs/TestShapeTableManager.cs
Copy link

This pull request has merge conflicts. Please resolve those before requesting a review.

@Piedone
Copy link
Member

Piedone commented Mar 19, 2024

Do you want to get back to this anytime soon, @hishamco? Starting with addressing #13018 (comment).

Otherwise, I welcome refactoring the tests and providing some common packages to make it easier for community members to write tests for their OC modules. (A bit of that we also address with the Lombiq Testing Toolbox but this would be better as part of OC itself.) I didn't look deeply into the code under this PR yet.

Also, you may want to look into AutoMocker: I briefly checked out the code and I see a lot of manual dependency resolution with mocks, while this library takes care of that.

@hishamco
Copy link
Member Author

I am delaying this because I've been working on Playwright and OC integrations last few weeks, hope to have a demo ASAP. Regarding I will try AutoMock and MockQueryable.Moq because they are so useful

@MichaelPetrinolis
Copy link
Contributor

Hello @hishamco, will this PR be a part of OC 2?

@hishamco
Copy link
Member Author

I will try to resolve the changes, there're too much :)

@MichaelPetrinolis
Copy link
Contributor

@hishamco Perfect! If I can help, please let me know.

@hishamco
Copy link
Member Author

@MichaelPetrinolis if you fix the conflict I will be thankful, coz I'm working on other PRs and issues

@MichaelPetrinolis
Copy link
Contributor

@hishamco weekend is over but i will try to get time for it this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OrchardCore Testing
4 participants