* 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>
463 lines
18 KiB
C#
463 lines
18 KiB
C#
using NUnit.Framework;
|
|
using Umbraco.Cms.Core;
|
|
using Umbraco.Cms.Core.Models;
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
using Umbraco.Cms.Core.Services.OperationStatus;
|
|
using Umbraco.Cms.Tests.Common.Builders;
|
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
|
|
|
|
public partial class ContentEditingServiceTests
|
|
{
|
|
[TestCase(true)]
|
|
[TestCase(false)]
|
|
public async Task Can_Create_At_Root(bool allowedAtRoot)
|
|
{
|
|
var template = TemplateBuilder.CreateTextPageTemplate();
|
|
await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey);
|
|
|
|
var contentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: template.Id);
|
|
contentType.AllowedAsRoot = allowedAtRoot;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
TemplateKey = template.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = "Test Create",
|
|
InvariantProperties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "title", Value = "The title value" },
|
|
new PropertyValueModel { Alias = "bodyText", Value = "The body text" }
|
|
}
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
|
|
if (allowedAtRoot)
|
|
{
|
|
Assert.IsTrue(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.Success, result.Status);
|
|
VerifyCreate(result.Result);
|
|
|
|
// re-get and re-test
|
|
VerifyCreate(await ContentEditingService.GetAsync(result.Result!.Key));
|
|
|
|
void VerifyCreate(IContent? createdContent)
|
|
{
|
|
Assert.IsNotNull(createdContent);
|
|
Assert.AreNotEqual(Guid.Empty, createdContent.Key);
|
|
Assert.IsTrue(createdContent.HasIdentity);
|
|
Assert.AreEqual("Test Create", createdContent.Name);
|
|
Assert.AreEqual("The title value", createdContent.GetValue<string>("title"));
|
|
Assert.AreEqual("The body text", createdContent.GetValue<string>("bodyText"));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.NotAllowed, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
}
|
|
|
|
[TestCase(true)]
|
|
[TestCase(false)]
|
|
public async Task Can_Create_As_Child(bool allowedAsChild)
|
|
{
|
|
var template = TemplateBuilder.CreateTextPageTemplate();
|
|
await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey);
|
|
|
|
var childContentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: template.Id);
|
|
childContentType.AllowedAsRoot = false;
|
|
ContentTypeService.Save(childContentType);
|
|
|
|
var rootContentType = ContentTypeBuilder.CreateBasicContentType();
|
|
rootContentType.AllowedAsRoot = true;
|
|
if (allowedAsChild)
|
|
{
|
|
rootContentType.AllowedContentTypes = new[]
|
|
{
|
|
new ContentTypeSort(childContentType.Key, 1, childContentType.Alias)
|
|
};
|
|
}
|
|
ContentTypeService.Save(rootContentType);
|
|
|
|
var rootKey = (await ContentEditingService.CreateAsync(
|
|
new ContentCreateModel
|
|
{
|
|
ContentTypeKey = rootContentType.Key, InvariantName = "Root", ParentKey = Constants.System.RootKey,
|
|
},
|
|
Constants.Security.SuperUserKey)).Result.Key;
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = childContentType.Key,
|
|
TemplateKey = template.Key,
|
|
ParentKey = rootKey,
|
|
InvariantName = "Test Create Child",
|
|
InvariantProperties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "title", Value = "The child title value" },
|
|
new PropertyValueModel { Alias = "bodyText", Value = "The child body text" }
|
|
}
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
|
|
if (allowedAsChild)
|
|
{
|
|
Assert.IsTrue(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.Success, result.Status);
|
|
|
|
var createdContent = result.Result;
|
|
Assert.NotNull(createdContent);
|
|
Assert.AreNotEqual(Guid.Empty, createdContent.Key);
|
|
Assert.IsTrue(createdContent.HasIdentity);
|
|
Assert.AreEqual("Test Create Child", createdContent.Name);
|
|
Assert.AreEqual("The child title value", createdContent.GetValue<string>("title"));
|
|
Assert.AreEqual("The child body text", createdContent.GetValue<string>("bodyText"));
|
|
}
|
|
else
|
|
{
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.NotAllowed, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task Can_Create_Without_Template()
|
|
{
|
|
var contentType = ContentTypeBuilder.CreateContentMetaContentType();
|
|
contentType.AllowedTemplates = null;
|
|
contentType.AllowedAsRoot = true;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = "Test Create",
|
|
InvariantProperties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "title", Value = "The title value" }
|
|
}
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsTrue(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.Success, result.Status);
|
|
Assert.IsNotNull(result.Result);
|
|
Assert.IsTrue(result.Result.HasIdentity);
|
|
Assert.AreEqual("The title value", result.Result.GetValue<string>("title"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Can_Create_Without_Properties()
|
|
{
|
|
var template = TemplateBuilder.CreateTextPageTemplate();
|
|
await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey);
|
|
|
|
var contentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: template.Id);
|
|
contentType.AllowedAsRoot = true;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = "Test Create"
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsTrue(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.Success, result.Status);
|
|
Assert.IsNotNull(result.Result);
|
|
Assert.IsTrue(result.Result.HasIdentity);
|
|
Assert.AreEqual(null, result.Result.GetValue<string>("title"));
|
|
Assert.AreEqual(null, result.Result.GetValue<string>("bodyText"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_With_Non_Existing_Parent()
|
|
{
|
|
var contentType = ContentTypeBuilder.CreateBasicContentType();
|
|
contentType.AllowedAsRoot = true;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
ParentKey = Guid.NewGuid(),
|
|
InvariantName = "Test Create"
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.ParentNotFound, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_Without_Content_Type()
|
|
{
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = Guid.NewGuid(),
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = "Test Create",
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.ContentTypeNotFound, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_With_Invalid_Template()
|
|
{
|
|
var template = TemplateBuilder.CreateTextPageTemplate();
|
|
await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey);
|
|
|
|
var contentType = ContentTypeBuilder.CreateBasicContentType();
|
|
contentType.AllowedAsRoot = true;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
TemplateKey = template.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = "Test Create"
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.TemplateNotAllowed, result.Status);
|
|
Assert.IsNotNull(result.Result);
|
|
Assert.IsFalse(result.Result.HasIdentity);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_With_Non_Existing_Template()
|
|
{
|
|
var contentType = ContentTypeBuilder.CreateBasicContentType();
|
|
contentType.AllowedAsRoot = true;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
TemplateKey = Guid.NewGuid(),
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = "Test Create"
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.TemplateNotFound, result.Status);
|
|
Assert.IsNotNull(result.Result);
|
|
Assert.IsFalse(result.Result.HasIdentity);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_With_Non_Existing_Properties()
|
|
{
|
|
var contentType = ContentTypeBuilder.CreateContentMetaContentType();
|
|
contentType.AllowedTemplates = null;
|
|
contentType.AllowedAsRoot = true;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = "Test Create",
|
|
InvariantProperties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "title", Value = "The title value" },
|
|
new PropertyValueModel { Alias = "no_such_property", Value = "No such property value" },
|
|
}
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.PropertyTypeNotFound, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_Invariant_Content_Without_Name()
|
|
{
|
|
var contentType = ContentTypeBuilder.CreateContentMetaContentType();
|
|
contentType.AllowedTemplates = null;
|
|
contentType.AllowedAsRoot = true;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = null,
|
|
InvariantProperties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "title", Value = "The title value" }
|
|
}
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.ContentTypeCultureVarianceMismatch, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_With_Variant_Property_Value_For_Invariant_Content()
|
|
{
|
|
var contentType = ContentTypeBuilder.CreateContentMetaContentType();
|
|
contentType.AllowedTemplates = null;
|
|
contentType.AllowedAsRoot = true;
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantName = "Test Create",
|
|
InvariantProperties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "title", Value = "The title value" }
|
|
},
|
|
Variants = new []
|
|
{
|
|
new VariantModel
|
|
{
|
|
Culture = "en-US",
|
|
Name = "The English Name",
|
|
Properties = new []
|
|
{
|
|
new PropertyValueModel { Alias = "bodyText", Value = "The body text value" }
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.ContentTypeCultureVarianceMismatch, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Can_Create_Culture_Variant()
|
|
{
|
|
var contentType = await CreateVariantContentType();
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantProperties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "invariantTitle", Value = "The Invariant Title" }
|
|
},
|
|
Variants = new[]
|
|
{
|
|
new VariantModel
|
|
{
|
|
Culture = "en-US",
|
|
Name = "The English Name",
|
|
Properties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "variantTitle", Value = "The English Title" }
|
|
}
|
|
},
|
|
new VariantModel
|
|
{
|
|
Culture = "da-DK",
|
|
Name = "The Danish Name",
|
|
Properties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "variantTitle", Value = "The Danish Title" }
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsTrue(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.Success, result.Status);
|
|
VerifyCreate(result.Result);
|
|
|
|
// re-get and re-test
|
|
VerifyCreate(await ContentEditingService.GetAsync(result.Result!.Key));
|
|
|
|
void VerifyCreate(IContent? createdContent)
|
|
{
|
|
Assert.IsNotNull(createdContent);
|
|
Assert.AreEqual("The English Name", createdContent.GetCultureName("en-US"));
|
|
Assert.AreEqual("The Danish Name", createdContent.GetCultureName("da-DK"));
|
|
Assert.AreEqual("The Invariant Title", createdContent.GetValue<string>("invariantTitle"));
|
|
Assert.AreEqual("The English Title", createdContent.GetValue<string>("variantTitle", "en-US"));
|
|
Assert.AreEqual("The Danish Title", createdContent.GetValue<string>("variantTitle", "da-DK"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_With_Invariant_Property_Value_For_Variant_Content()
|
|
{
|
|
var contentType = await CreateVariantContentType();
|
|
|
|
var createModel = new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key,
|
|
ParentKey = Constants.System.RootKey,
|
|
InvariantProperties = new[]
|
|
{
|
|
new PropertyValueModel { Alias = "invariantTitle", Value = "The Invariant Title" },
|
|
new PropertyValueModel { Alias = "variantTitle", Value = "The Variant Title" }
|
|
}
|
|
};
|
|
|
|
var result = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.PropertyTypeNotFound, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Cannot_Create_Under_Trashed_Parent()
|
|
{
|
|
var contentType = ContentTypeBuilder.CreateBasicContentType();
|
|
contentType.AllowedAsRoot = true;
|
|
contentType.AllowedContentTypes = new[]
|
|
{
|
|
new ContentTypeSort(contentType.Key, 1, contentType.Alias)
|
|
};
|
|
ContentTypeService.Save(contentType);
|
|
|
|
var rootKey = (await ContentEditingService.CreateAsync(
|
|
new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key, InvariantName = "Root", ParentKey = Constants.System.RootKey
|
|
},
|
|
Constants.Security.SuperUserKey)).Result.Key;
|
|
|
|
await ContentEditingService.MoveToRecycleBinAsync(rootKey, Constants.Security.SuperUserKey);
|
|
|
|
var result = await ContentEditingService.CreateAsync(
|
|
new ContentCreateModel
|
|
{
|
|
ContentTypeKey = contentType.Key, InvariantName = "Child", ParentKey = rootKey,
|
|
},
|
|
Constants.Security.SuperUserKey);
|
|
|
|
Assert.IsFalse(result.Success);
|
|
Assert.AreEqual(ContentEditingOperationStatus.InTrash, result.Status);
|
|
Assert.IsNull(result.Result);
|
|
}
|
|
}
|