Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentBlueprintEditingServiceTests.cs
Laura Neto 55506bac3a Simplify creating content from a blueprint programmatically (#19528)
* Rename `IContentService.CreateContentFromBlueprint` to `CreateBlueprintFromContent`

In reality, this method is used by the core to create a blueprint from content, and not the other way around, which doesn't need new ids. This was causing confusion, so the old name has been marked as deprecated in favor of the new name. If developers want to create content from blueprints they should use `IContentBlueprintEditingService.GetScaffoldedAsync()` instead, which is what is used by the management api.

* Added integration tests to verify that new block ids are generated when creating content from a blueprint

* Return copy of the blueprint in `ContentBlueprintEditingService.GetScaffoldedAsync` instead of the blueprint itself

* Update CreateContentFromBlueprint xml docs to mention both replacement methods

* Fix tests for rich text blocks

* Small re-organization

* Adjusted tests that were still referencing `ContentService.CreateContentFromBlueprint`

* Add default implementation to new CreateBlueprintFromContent method

* Update tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentBlueprintEditingServiceTests.GetScaffold.cs

Co-authored-by: Andy Butland <abutland73@gmail.com>

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>
2025-06-24 13:43:34 +02:00

105 lines
4.2 KiB
C#

using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
/// <summary>
/// Tests for the content blueprint editing service. Please notice that a lot of the functional tests are covered by the content
/// editing service tests, since these services share the same base implementation.
/// </summary>
public partial class ContentBlueprintEditingServiceTests : ContentEditingServiceTestsBase
{
private IContentBlueprintContainerService ContentBlueprintContainerService => GetRequiredService<IContentBlueprintContainerService>();
private IEntityService EntityService => GetRequiredService<IEntityService>();
private IJsonSerializer JsonSerializer => GetRequiredService<IJsonSerializer>();
private async Task<IContent> CreateInvariantContentBlueprint()
{
var contentType = CreateInvariantContentType();
var createModel = new ContentBlueprintCreateModel
{
ContentTypeKey = contentType.Key,
ParentKey = Constants.System.RootKey,
Variants = [new VariantModel { Name = "Initial Blueprint Name" }],
Properties =
[
new PropertyValueModel { Alias = "title", Value = "The initial title" },
new PropertyValueModel { Alias = "text", Value = "The initial text" }
],
};
var result = await ContentBlueprintEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
Assert.IsTrue(result.Success);
return result.Result.Content!;
}
private async Task<IContent> CreateVariantContentBlueprint()
{
var contentType = await CreateVariantContentType();
var createModel = new ContentBlueprintCreateModel
{
ContentTypeKey = contentType.Key,
ParentKey = Constants.System.RootKey,
Properties =
[
new PropertyValueModel { Alias = "invariantTitle", Value = "The initial invariant title" },
new PropertyValueModel { Alias = "variantTitle", Value = "The initial English title", Culture = "en-US" },
new PropertyValueModel { Alias = "variantTitle", Value = "The initial Danish title", Culture = "da-DK" }
],
Variants =
[
new VariantModel { Culture = "en-US", Name = "Initial Blueprint English Name" },
new VariantModel { Culture = "da-DK", Name = "Initial Blueprint Danish Name" }
],
};
var result = await ContentBlueprintEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
Assert.IsTrue(result.Success);
return result.Result.Content!;
}
private ContentBlueprintCreateModel SimpleContentBlueprintCreateModel(Guid blueprintKey, Guid? containerKey)
{
var createModel = new ContentBlueprintCreateModel
{
Key = blueprintKey,
ContentTypeKey = ContentType.Key,
ParentKey = containerKey,
Variants = [new VariantModel { Name = "Blueprint #1" }],
Properties =
[
new PropertyValueModel { Alias = "title", Value = "The title value" },
new PropertyValueModel { Alias = "author", Value = "The author value" },
],
};
return createModel;
}
private ContentBlueprintUpdateModel SimpleContentBlueprintUpdateModel()
{
var createModel = new ContentBlueprintUpdateModel
{
Variants = [new VariantModel { Name = "Blueprint #1 updated" }],
Properties =
[
new PropertyValueModel { Alias = "title", Value = "The title value updated" },
new PropertyValueModel { Alias = "author", Value = "The author value updated" }
],
};
return createModel;
}
private IEntitySlim[] GetBlueprintChildren(Guid? containerKey)
=> EntityService.GetPagedChildren(containerKey, [UmbracoObjectTypes.DocumentBlueprintContainer], UmbracoObjectTypes.DocumentBlueprint, 0, 100, out _).ToArray();
}