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
This commit is contained in:
Nicklas Kramer
2025-09-23 13:22:29 +02:00
committed by GitHub
parent 859505e751
commit 7db7346d4a
5 changed files with 53 additions and 54 deletions

View File

@@ -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<TestDataSettings>(testDataSection);
builder.Services.Configure<UmbracoPipelineOptions>(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<TestDataSettings>();
if (config == null || config.Enabled == false)
{
return builder;
}
builder.Services.Configure<TestDataSettings>(testDataSection);
builder.WithCollectionBuilder<SurfaceControllerTypeCollectionBuilder>()
.Add<UmbracoTestDataController>();
return builder;
}
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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;
/// </summary>
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;
}
}

View File

@@ -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
<ItemGroup>
<ProjectReference Include="..\..\tests\Umbraco.TestData\Umbraco.TestData.csproj" />
</ItemGroup>
```
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