Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions tests/Umbraco.TestData/Extensions/UmbracoBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
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;
}

Expand All @@ -26,7 +26,6 @@ public static IUmbracoBuilder AddUmbracoTestData(this IUmbracoBuilder builder)

builder.Services.Configure<TestDataSettings>(testDataSection);


builder.Services.Configure<UmbracoPipelineOptions>(options =>
options.AddFilter(new UmbracoPipelineFilter(nameof(LoadTestController))
{
Expand All @@ -41,4 +40,26 @@ public static IUmbracoBuilder AddUmbracoTestData(this IUmbracoBuilder builder)

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;
}
}
2 changes: 1 addition & 1 deletion tests/Umbraco.TestData/LoadTestComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
10 changes: 10 additions & 0 deletions tests/Umbraco.TestData/TestDataComposer.cs
Original file line number Diff line number Diff line change
@@ -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();
}
54 changes: 9 additions & 45 deletions tests/Umbraco.TestData/UmbracoTestDataController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Bogus;
using Bogus;

Check notice on line 1 in tests/Umbraco.TestData/UmbracoTestDataController.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v17/dev)

✅ No longer an issue: Primitive Obsession

The ratio of primivite types in function arguments is no longer above the threshold
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
Expand All @@ -15,7 +12,6 @@
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;
Expand All @@ -27,9 +23,6 @@
/// </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;
Expand Down Expand Up @@ -268,47 +261,18 @@
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;
}
}
14 changes: 9 additions & 5 deletions tests/Umbraco.TestData/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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

Expand Down