Skip to content

Commit

Permalink
Add type checking when importing types from recipes #15979 (#15980)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzx86 committed May 9, 2024
1 parent c20c63e commit 548eaa1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
Expand Up @@ -58,6 +58,11 @@ private Task UpdateContentTypeAsync(ContentTypeDefinition type, ContentTypeDefin
foreach (var part in record.ContentTypePartDefinitionRecords)
{
if (string.IsNullOrEmpty(part.PartName))
{
throw new InvalidOperationException($"Unable to add content-part to the '{type.Name}' content-type. The part name cannot be null or empty.");
}
builder.WithPart(part.Name, part.PartName, partBuilder => partBuilder.MergeSettings(part.Settings));
}
});
Expand Down
2 changes: 2 additions & 0 deletions test/OrchardCore.Tests/OrchardCore.Tests.csproj
Expand Up @@ -29,6 +29,7 @@
<None Remove="Recipes\RecipeFiles\recipe3.json" />
<None Remove="Recipes\RecipeFiles\recipe4.json" />
<None Remove="Recipes\RecipeFiles\recipe5.json" />
<None Remove="Recipes\RecipeFiles\recipe6.json" />
</ItemGroup>

<ItemGroup>
Expand All @@ -44,6 +45,7 @@
<EmbeddedResource Include="Recipes\RecipeFiles\recipe3.json" />
<EmbeddedResource Include="Recipes\RecipeFiles\recipe4.json" />
<EmbeddedResource Include="Recipes\RecipeFiles\recipe5.json" />
<EmbeddedResource Include="Recipes\RecipeFiles\recipe6.json" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions test/OrchardCore.Tests/Recipes/RecipeExecutorTests.cs
Expand Up @@ -8,6 +8,7 @@
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;
using OrchardCore.Scripting;
using OrchardCore.Tests.Apis.Context;

namespace OrchardCore.Recipes
{
Expand Down Expand Up @@ -50,6 +51,27 @@ public async Task ShouldTrimValidScriptExpression(string recipeName, string expe
});
}

[Fact]
public async Task ContentDefinitionStep_WhenPartNameIsMissing_ThrowInvalidOperationException()
{
var context = new BlogContext();
await context.InitializeAsync();
await context.UsingTenantScopeAsync(async scope =>
{
var recipeExecutor = scope.ServiceProvider.GetRequiredService<IRecipeExecutor>();
// Act
var executionId = Guid.NewGuid().ToString("n");
var recipeDescriptor = new RecipeDescriptor { RecipeFileInfo = GetRecipeFileInfo("recipe6") };
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
await recipeExecutor.ExecuteAsync(executionId, recipeDescriptor, new Dictionary<string, object>(), CancellationToken.None);
});
Assert.Equal("Unable to add content-part to the 'Message' content-type. The part name cannot be null or empty.", exception.Message);
});
}

private static Task<ShellScope> GetScopeAsync() => ShellScope.Context.CreateScopeAsync();

private static ShellContext CreateShellContext() => new()
Expand Down
27 changes: 27 additions & 0 deletions test/OrchardCore.Tests/Recipes/RecipeFiles/recipe6.json
@@ -0,0 +1,27 @@
{
"name": "Recipe6",
"displayName": "Recipe 6",
"description": "This recipe is designed to uncover potential bugs during the content-definition import process. It serves as a validation for the import functionality.",
"author": "Tony Han",
"website": "",
"version": "1.0.0",
"issetuprecipe": false,
"categories": [ "test" ],
"steps": [
{
"name": "ContentDefinition",
"ContentTypes": [
{
"Name": "Message",
"DisplayName": "Message",
"ContentTypePartDefinitionRecords": [
{
// "PartName": "TitlePart", // Test PartName validation.
"Name": "TitlePart"
}
]
}
]
}
]
}

0 comments on commit 548eaa1

Please sign in to comment.