Skip to content

Commit

Permalink
add typepart check OrchardCMS#15979
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzx86 committed May 5, 2024
1 parent c967b5d commit 19889be
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Records;
Expand Down Expand Up @@ -58,6 +59,11 @@ private Task UpdateContentTypeAsync(ContentTypeDefinition type, ContentTypeDefin
foreach (var part in record.ContentTypePartDefinitionRecords)
{
if (string.IsNullOrEmpty(part.PartName))
{
throw new Exception(string.Format("Failed to add part to ContentType '{0}', The PartName property is not allowed to be empty", type.Name));
}
builder.WithPart(part.Name, part.PartName, partBuilder => partBuilder.MergeSettings(part.Settings));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
Expand Down
2 changes: 2 additions & 0 deletions test/OrchardCore.Tests/OrchardCore.Tests.csproj
Original file line number Diff line number Diff line change
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
23 changes: 23 additions & 0 deletions test/OrchardCore.Tests/Recipes/RecipeExecutorTests.cs
Original file line number Diff line number Diff line change
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,28 @@ public async Task ShouldTrimValidScriptExpression(string recipeName, string expe
});
}

[Fact]
public async Task IncorrectTypeDefinitionImportsShouldBeBlocked()
{
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") };
try
{
await recipeExecutor.ExecuteAsync(executionId, recipeDescriptor, new Dictionary<string, object>(), CancellationToken.None);
}
catch (Exception ex)
{
Assert.Equal("Failed to add part to ContentType 'Message', The PartName property is not allowed to be empty", ex.Message);
}
});
}

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

private static ShellContext CreateShellContext() => new()
Expand Down
58 changes: 58 additions & 0 deletions test/OrchardCore.Tests/Recipes/RecipeFiles/recipe6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "Recipe6",
"displayName": "Recipe 6",
"description": "This is a recipe for a bug to verify that the type-checking function works as expected",
"author": "Tony Han",
"website": "",
"version": "1.0.0",
"issetuprecipe": false,
"categories": [ "test" ],
"tags": [],
"variables": {
"messageContentItemId": "[js:uuid()]",
"now": "[js: new Date().toISOString()]"
},
"steps": [
{
"name": "feature",
"enable": [
"OrchardCore.Contents",
"OrchardCore.ContentTypes",
"OrchardCore.Title"
]
},
{
"name": "ContentDefinition",
"ContentTypes": [
{
"Name": "Message",
"DisplayName": "Message",
"Settings": {
"ContentTypeSettings": {
"Creatable": true,
"Draftable": true,
"Versionable": true,
"Listable": true,
"Securable": true
}
},
"ContentTypePartDefinitionRecords": [
{
"PartName": "Message",
"Name": "Message",
"Settings": {
"ContentTypePartSettings": {
"Position": "5"
}
}
},
{
//"PartName": "TitlePart",//test part validate
"Name": "TitlePart"
}
]
}
]
}
]
}

0 comments on commit 19889be

Please sign in to comment.