From 7db7346d4aa5642dd0cfdb39fcd8419f0d899593 Mon Sep 17 00:00:00 2001 From: Nicklas Kramer Date: Tue, 23 Sep 2025 13:22:29 +0200 Subject: [PATCH] Test Data: Fixing endpoint not working #20044 (#20227) * It worked before i must have broken it somehow. Commit as checkpoint * Adding a reference from Web.UI.csproj to TestData to allow composers to be composed * Changing readme and removing project reference --- .../Extensions/UmbracoBuilderExtensions.cs | 27 ++++++++-- tests/Umbraco.TestData/LoadTestComposer.cs | 2 +- tests/Umbraco.TestData/TestDataComposer.cs | 10 ++++ .../UmbracoTestDataController.cs | 54 ++++--------------- tests/Umbraco.TestData/readme.md | 14 +++-- 5 files changed, 53 insertions(+), 54 deletions(-) create mode 100644 tests/Umbraco.TestData/TestDataComposer.cs diff --git a/tests/Umbraco.TestData/Extensions/UmbracoBuilderExtensions.cs b/tests/Umbraco.TestData/Extensions/UmbracoBuilderExtensions.cs index 9158f9141b..aa131f4f85 100644 --- a/tests/Umbraco.TestData/Extensions/UmbracoBuilderExtensions.cs +++ b/tests/Umbraco.TestData/Extensions/UmbracoBuilderExtensions.cs @@ -3,17 +3,17 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Cms.Web.Common.ApplicationBuilder; +using Umbraco.Cms.Web.Website.Collections; using Umbraco.TestData.Configuration; namespace Umbraco.TestData.Extensions; public static class UmbracoBuilderExtensions { - public static IUmbracoBuilder AddUmbracoTestData(this IUmbracoBuilder builder) + public static IUmbracoBuilder AddUmbracoLoadTest(this IUmbracoBuilder builder) { if (builder.Services.Any(x => x.ServiceType == typeof(LoadTestController))) { - // We assume the test data project is composed if any implementations of LoadTestController exist. return builder; } @@ -26,7 +26,6 @@ public static class UmbracoBuilderExtensions builder.Services.Configure(testDataSection); - builder.Services.Configure(options => options.AddFilter(new UmbracoPipelineFilter(nameof(LoadTestController)) { @@ -41,4 +40,26 @@ public static class UmbracoBuilderExtensions return builder; } + + public static IUmbracoBuilder AddUmbracoTestData(this IUmbracoBuilder builder) + { + if (builder.Services.Any(x => x.ServiceType == typeof(UmbracoTestDataController))) + { + return builder; + } + + var testDataSection = builder.Config.GetSection("Umbraco:CMS:TestData"); + var config = testDataSection.Get(); + if (config == null || config.Enabled == false) + { + return builder; + } + + builder.Services.Configure(testDataSection); + + builder.WithCollectionBuilder() + .Add(); + + return builder; + } } diff --git a/tests/Umbraco.TestData/LoadTestComposer.cs b/tests/Umbraco.TestData/LoadTestComposer.cs index f44d8edfb2..589aa68739 100644 --- a/tests/Umbraco.TestData/LoadTestComposer.cs +++ b/tests/Umbraco.TestData/LoadTestComposer.cs @@ -8,5 +8,5 @@ namespace Umbraco.TestData; public class LoadTestComposer : IComposer { - public void Compose(IUmbracoBuilder builder) => builder.AddUmbracoTestData(); + public void Compose(IUmbracoBuilder builder) => builder.AddUmbracoLoadTest(); } diff --git a/tests/Umbraco.TestData/TestDataComposer.cs b/tests/Umbraco.TestData/TestDataComposer.cs new file mode 100644 index 0000000000..905397c664 --- /dev/null +++ b/tests/Umbraco.TestData/TestDataComposer.cs @@ -0,0 +1,10 @@ +using Umbraco.Cms.Core.Composing; +using Umbraco.Cms.Core.DependencyInjection; +using Umbraco.TestData.Extensions; + +namespace Umbraco.TestData; + +public class TestDataComposer : IComposer +{ + public void Compose(IUmbracoBuilder builder) => builder.AddUmbracoTestData(); +} diff --git a/tests/Umbraco.TestData/UmbracoTestDataController.cs b/tests/Umbraco.TestData/UmbracoTestDataController.cs index 3fd3754cc5..3c390e289d 100644 --- a/tests/Umbraco.TestData/UmbracoTestDataController.cs +++ b/tests/Umbraco.TestData/UmbracoTestDataController.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Bogus; +using Bogus; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Umbraco.Cms.Core; @@ -15,7 +12,6 @@ using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Strings; using Umbraco.Cms.Core.Web; using Umbraco.Cms.Infrastructure.Persistence; -using Umbraco.Cms.Infrastructure.Serialization; using Umbraco.Cms.Web.Website.Controllers; using Umbraco.Extensions; using Umbraco.TestData.Configuration; @@ -27,9 +23,6 @@ namespace Umbraco.TestData; /// public class UmbracoTestDataController : SurfaceController { - private const string RichTextDataTypeName = "UmbracoTestDataContent.RTE"; - private const string MediaPickerDataTypeName = "UmbracoTestDataContent.MediaPicker"; - private const string TextDataTypeName = "UmbracoTestDataContent.Text"; private const string TestDataContentTypeAlias = "umbTestDataContent"; private readonly PropertyEditorCollection _propertyEditors; private readonly ICoreScopeProvider _scopeProvider; @@ -268,47 +261,18 @@ public class UmbracoTestDataController : SurfaceController Icon = "icon-science color-green" }; docType.AddPropertyGroup("content", "Content"); - docType.AddPropertyType(new PropertyType(_shortStringHelper, GetOrCreateRichText(), "review") + docType.AddPropertyType( + new PropertyType(_shortStringHelper, Constants.PropertyEditors.Aliases.RichText, ValueStorageType.Ntext, "review") { - Name = "Review" - }); - docType.AddPropertyType(new PropertyType(_shortStringHelper, GetOrCreateText(), "desc") { Name = "Description" }); + Name = "Review", + }, "content"); + docType.AddPropertyType(new PropertyType(_shortStringHelper, Constants.PropertyEditors.Aliases.TextBox, ValueStorageType.Ntext, "desc") { Name = "Description" }, "content"); + docType.AddPropertyType(new PropertyType(_shortStringHelper, Constants.PropertyEditors.Aliases.MediaPicker3, ValueStorageType.Integer, "media") { Name = "Media" }, "content"); + + Services.ContentTypeService.Save(docType); docType.AllowedContentTypes = new[] { new ContentTypeSort(docType.Key, 0, docType.Alias) }; Services.ContentTypeService.Save(docType); return docType; } - - private IDataType GetOrCreateRichText() => - GetOrCreateDataType(RichTextDataTypeName, Constants.PropertyEditors.Aliases.RichText); - - private IDataType GetOrCreateText() => - GetOrCreateDataType(TextDataTypeName, Constants.PropertyEditors.Aliases.TextBox); - - private IDataType GetOrCreateDataType(string name, string editorAlias) - { - var dt = Services.DataTypeService.GetDataType(name); - if (dt != null) - { - return dt; - } - - var editor = _propertyEditors.FirstOrDefault(x => x.Alias == editorAlias); - if (editor == null) - { - throw new InvalidOperationException($"No {editorAlias} editor found"); - } - - var serializer = new SystemTextConfigurationEditorJsonSerializer(new DefaultJsonSerializerEncoderFactory()); - - dt = new DataType(editor, serializer) - { - Name = name, - ConfigurationData = editor.GetConfigurationEditor().DefaultConfiguration, - DatabaseType = ValueStorageType.Ntext - }; - - Services.DataTypeService.Save(dt); - return dt; - } } diff --git a/tests/Umbraco.TestData/readme.md b/tests/Umbraco.TestData/readme.md index 454184c238..d752ad14f2 100644 --- a/tests/Umbraco.TestData/readme.md +++ b/tests/Umbraco.TestData/readme.md @@ -14,14 +14,21 @@ It has to be enabled by an appSetting: "Umbraco": { "CMS": { "TestData": { - "Enabled" : true, + "Enabled" : true } } } } ``` -Once this is enabled this endpoint can be executed: +After this, you also need to add a reference to the `Umbraco.TestData.csproj`, from `Umbraco.Web.UI.csproj` so the composers will be recognized. +```xml + + + +``` + +Once this is done this endpoint can be executed: `/umbraco/surface/umbracotestdata/CreateTree?count=100&depth=5` @@ -43,9 +50,6 @@ All values are generated using the very handy `Bogus` package. This will install some schema items: * `umbTestDataContent` Document Type. __TIP__: If you want to delete all of the content data generated with this tool, just delete this content type -* `UmbracoTestDataContent.RTE` Data Type -* `UmbracoTestDataContent.MediaPicker` Data Type -* `UmbracoTestDataContent.Text` Data Type For media, the normal folder and image is used