Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Core/Services/ContentEditingServiceTests.cs
Sven Geusens 26761cc04a Implemented culture based authorization for content (#15580)
* Implemented culture based authorization for content

* Implemented culture auth for create/update of documents

* Applied culture authorization to dictionary create/update

* Added an integration test to test an assumption about the ContentTypeEditingService.CreateAsync method

* Fix processing when result is already false;

* Apply suggestions from code review

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

* Refactor method to async + clarify and consilidate comments regarding dictionary locks

---------

Co-authored-by: Sven Geusens <sge@umbraco.dk>
Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
2024-01-22 21:08:20 +01:00

134 lines
4.8 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;
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.Core.Services;
[TestFixture]
[UmbracoTest(
Database = UmbracoTestOptions.Database.NewSchemaPerTest,
PublishedRepositoryEvents = true,
WithApplication = true)]
public class ContentEditingServiceTests : UmbracoIntegrationTestWithContent
{
[SetUp]
public void Setup() => ContentRepositoryBase.ThrowOnWarning = true;
[TearDown]
public void Teardown() => ContentRepositoryBase.ThrowOnWarning = false;
private IContentEditingService ContentTypeEditingService => GetRequiredService<IContentEditingService>();
private ILanguageService LanguageService => GetRequiredService<ILanguageService>();
[Test]
public async Task Only_Supplied_Cultures_Are_Updated()
{
var variantTestData = await SetupVariantTest();
var documentKey = Guid.NewGuid();
var propertyAlias = "title";
var originalPropertyValue = "original";
var updatedPropertyValue = "updated";
var createModel = new ContentCreateModel
{
Key = documentKey,
ContentTypeKey = variantTestData.contentType.Key,
Variants = new[]
{
new VariantModel
{
Name = variantTestData.LangEn.CultureName,
Culture = variantTestData.LangEn.IsoCode,
Properties = new[]
{
new PropertyValueModel
{
Alias = propertyAlias, Value = originalPropertyValue
}
}
},
new VariantModel
{
Name = variantTestData.LangDa.CultureName,
Culture = variantTestData.LangDa.IsoCode,
Properties = new[]
{
new PropertyValueModel
{
Alias = propertyAlias, Value = originalPropertyValue
}
}
}
}
};
await ContentTypeEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
var content = ContentService.GetById(documentKey)!;
var updateModel = new ContentUpdateModel
{
Variants = new[]
{
new VariantModel
{
Name = updatedPropertyValue,
Culture = variantTestData.LangEn.IsoCode,
Properties = new[] { new PropertyValueModel { Alias = propertyAlias, Value = updatedPropertyValue } }
}
}
};
await ContentTypeEditingService.UpdateAsync(content, updateModel, Constants.Security.SuperUserKey);
var updatedContent = ContentService.GetById(documentKey)!;
Assert.AreEqual(originalPropertyValue, updatedContent.GetValue(propertyAlias,variantTestData.LangDa.IsoCode));
Assert.AreEqual(updatedPropertyValue, updatedContent.GetValue(propertyAlias,variantTestData.LangEn.IsoCode));
Assert.AreEqual(variantTestData.LangDa.CultureName, updatedContent.GetCultureName(variantTestData.LangDa.IsoCode));
Assert.AreEqual(updatedPropertyValue, updatedContent.GetCultureName(variantTestData.LangEn.IsoCode));
}
private async Task<(ILanguage LangEn, ILanguage LangDa, IContentType contentType)> SetupVariantTest()
{
var langEn = (await LanguageService.GetAsync("en-US"))!;
var langDa = new LanguageBuilder()
.WithCultureInfo("da-DK")
.Build();
await LanguageService.CreateAsync(langDa, Constants.Security.SuperUserKey);
var template = TemplateBuilder.CreateTextPageTemplate();
FileService.SaveTemplate(template);
var contentType = new ContentTypeBuilder()
.WithAlias("variantContent")
.WithName("Variant Content")
.WithContentVariation(ContentVariation.Culture)
.AddPropertyGroup()
.WithAlias("content")
.WithName("Content")
.WithSupportsPublishing(true)
.AddPropertyType()
.WithAlias("title")
.WithName("Title")
.WithVariations(ContentVariation.Culture)
.WithMandatory(true)
.Done()
.Done()
.Build();
contentType.AllowedAsRoot = true;
ContentTypeService.Save(contentType);
return (langEn, langDa, contentType);
}
}