Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentEditingServiceTests.cs
Kenn Jacobsen 0096addcb9 Content and media type CRUD controllers and services (#14665)
* Add GetAsync method

* Fix up delete document type controller

* Add scope to delete async

* Add some scaffolding

* Add create model

* Start working on validation

* Move validation to its own service

* Use GetAllAsync instead of GetAsync

* Add initial composition support

Still need to figure out some kinks

* Validate compositions when creating

* Add initial folder support

* Initial handling of generic properties

* Add operation status responses

* Move create operation into service

* Add first test

* Fix issued shown by test

* Ensure a specific key can be specified when creating

* Rename container id to container key

Let's try and be consistent

* Create basic composition test

* Ensure new property groups are created with the correct key

* Add test showing property type issue

* Fix property types not using the expected key.

* Validate against model fetched from content type service

Just to make sure nothing explodes on the round trip

* Make helper for creating create models

* Add helper for creating container

* Make helper methods simpler to use

* Add test for compositions using compositions

* Add more composition tests

* Fix bug allowing element types to be composed by non element types

* Remove validators

This can just be a part of the editing service

* Minor cleanup

* Ensure that multiple levels of inheritance is possible

* Ensure doctype cannot be used as both composition and inheritance on the same doctype

* Ensure no duplicate aliases from composition and that compositions exists

* Minor cleanup

* Address todos

* Add SaveAsync method

* Renamed some models

* Rename from DocumentType to ContentType

* Clarify ParentKey as being container only + untangle things a tiny bit

* Clean out another TODO (less duplicate code) + more tests

* Refactor for reuse across different content types + add media type editing service + unit tests

* Refactor in preparation for update handling

* More tests + fixed bugs found while testing

* Simplify things a bit

* Content type update + a lot of unit tests + some refactor + fix bugs found while testing

* Begin building presentation factories for mapping view models to editing models

* Use async save

* Mapping factories and some clean-up

* Rename Key to Id (ParentKey to ParentId)

* Fix slight typo

* Use editing service in document type controllers and introduce media type controllers

* Validate containers and align container aliases with the current backoffice

* Remove ParentId from response

* Fix scope handling in DeleteAsync

* Refactor ContentTypeSort

* A little renaming for clarity + safeguard against changes to inheritance

* Persist allowed content types

* Fix bad merge + update controller response annotations

* Update OpenAPI JSON

* Update src/Umbraco.Cms.Api.Management/Controllers/DocumentType/DocumentTypeControllerBase.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Fix review comments

* Update usage of MapCreateAsync to ValidateAndMapForCreationAsync

---------

Co-authored-by: Nikolaj <nikolajlauridsen@protonmail.ch>
2023-08-17 12:28:16 +02:00

201 lines
7.1 KiB
C#

using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
[TestFixture]
[UmbracoTest(
Database = UmbracoTestOptions.Database.NewSchemaPerTest,
PublishedRepositoryEvents = true,
WithApplication = true)]
public partial class ContentEditingServiceTests : UmbracoIntegrationTestWithContent
{
[SetUp]
public void Setup() => ContentRepositoryBase.ThrowOnWarning = true;
protected override void CustomTestSetup(IUmbracoBuilder builder) =>
builder.AddNotificationHandler<ContentCopiedNotification, RelateOnCopyNotificationHandler>();
private ITemplateService TemplateService => GetRequiredService<ITemplateService>();
private ILanguageService LanguageService => GetRequiredService<ILanguageService>();
private IContentEditingService ContentEditingService => GetRequiredService<IContentEditingService>();
private IContentType CreateInvariantContentType(params ITemplate[] templates)
{
var contentTypeBuilder = new ContentTypeBuilder()
.WithAlias("invariantTest")
.WithName("Invariant Test")
.WithContentVariation(ContentVariation.Nothing)
.AddPropertyType()
.WithAlias("title")
.WithName("Title")
.WithVariations(ContentVariation.Nothing)
.Done()
.AddPropertyType()
.WithAlias("text")
.WithName("Text")
.WithVariations(ContentVariation.Nothing)
.Done();
foreach (var template in templates)
{
contentTypeBuilder
.AddAllowedTemplate()
.WithId(template.Id)
.WithAlias(template.Alias)
.WithName(template.Name ?? template.Alias)
.Done();
}
if (templates.Any())
{
contentTypeBuilder.WithDefaultTemplateId(templates.First().Id);
}
var contentType = contentTypeBuilder.Build();
contentType.AllowedAsRoot = true;
ContentTypeService.Save(contentType);
return contentType;
}
private async Task<IContentType> CreateVariantContentType()
{
var language = new LanguageBuilder()
.WithCultureInfo("da-DK")
.Build();
await LanguageService.CreateAsync(language, Constants.Security.SuperUserKey);
var contentType = new ContentTypeBuilder()
.WithAlias("cultureVariationTest")
.WithName("Culture Variation Test")
.WithContentVariation(ContentVariation.Culture)
.AddPropertyType()
.WithAlias("variantTitle")
.WithName("Variant Title")
.WithVariations(ContentVariation.Culture)
.Done()
.AddPropertyType()
.WithAlias("invariantTitle")
.WithName("Invariant Title")
.WithVariations(ContentVariation.Nothing)
.Done()
.Build();
contentType.AllowedAsRoot = true;
ContentTypeService.Save(contentType);
return contentType;
}
private async Task<IContent> CreateInvariantContent(params ITemplate[] templates)
{
var contentType = CreateInvariantContentType(templates);
var createModel = new ContentCreateModel
{
ContentTypeKey = contentType.Key,
ParentKey = Constants.System.RootKey,
InvariantName = "Initial Name",
TemplateKey = templates.FirstOrDefault()?.Key,
InvariantProperties = new[]
{
new PropertyValueModel { Alias = "title", Value = "The initial title" },
new PropertyValueModel { Alias = "text", Value = "The initial text" }
}
};
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
Assert.IsTrue(result.Success);
return result.Result!;
}
private async Task<IContent> CreateVariantContent()
{
var contentType = await CreateVariantContentType();
var createModel = new ContentCreateModel
{
ContentTypeKey = contentType.Key,
ParentKey = Constants.System.RootKey,
InvariantProperties = new[]
{
new PropertyValueModel { Alias = "invariantTitle", Value = "The initial invariant title" }
},
Variants = new []
{
new VariantModel
{
Culture = "en-US",
Name = "Initial English Name",
Properties = new []
{
new PropertyValueModel { Alias = "variantTitle", Value = "The initial English title" }
}
},
new VariantModel
{
Culture = "da-DK",
Name = "Initial Danish Name",
Properties = new []
{
new PropertyValueModel { Alias = "variantTitle", Value = "The initial Danish title" }
}
}
}
};
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
Assert.IsTrue(result.Success);
return result.Result!;
}
private async Task<IContentType> CreateTextPageContentTypeAsync()
{
var template = TemplateBuilder.CreateTextPageTemplate();
await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey);
var contentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: template.Id);
contentType.AllowedAsRoot = true;
ContentTypeService.Save(contentType);
return contentType;
}
private async Task<(IContent root, IContent child)> CreateRootAndChildAsync(IContentType contentType, string rootName = "The Root", string childName = "The Child")
{
var createModel = new ContentCreateModel
{
ContentTypeKey = contentType.Key,
ParentKey = Constants.System.RootKey,
InvariantName = rootName
};
var root = (await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey)).Result!;
contentType.AllowedContentTypes = new List<ContentTypeSort>
{
new (contentType.Key, 1, contentType.Alias)
};
ContentTypeService.Save(contentType);
createModel.ParentKey = root.Key;
createModel.InvariantName = childName;
var child = (await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey)).Result!;
Assert.AreEqual(root.Id, child.ParentId);
return (root, child);
}
}