* Run code cleanup * Dotnet format benchmarks project * Fix up Test.Common * Run dotnet format + manual cleanup * Run code cleanup for unit tests * Run dotnet format * Fix up errors * Manual cleanup of Unit test project * Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix according to review * Fix after merge * Fix errors Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> Co-authored-by: Zeegaan <nge@umbraco.dk>
131 lines
5.2 KiB
C#
131 lines
5.2 KiB
C#
// Copyright (c) Umbraco.
|
|
// See LICENSE for more details.
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
using Umbraco.Cms.Core.Services;
|
|
using Umbraco.Extensions;
|
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.BackOffice.Extensions;
|
|
|
|
[TestFixture]
|
|
public class ModelStateExtensionsTests
|
|
{
|
|
[Test]
|
|
public void Get_Cultures_With_Errors()
|
|
{
|
|
var ms = new ModelStateDictionary();
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null); // invariant property
|
|
ms.AddPropertyError(new ValidationResult("title missing"), "title", "en-US"); // variant property
|
|
|
|
var result = ms.GetVariantsWithErrors("en-US");
|
|
|
|
// even though there are 2 errors, they are both for en-US since that is the default language and one of the errors is for an invariant property
|
|
Assert.AreEqual(1, result.Count);
|
|
Assert.AreEqual("en-US", result[0].culture);
|
|
|
|
ms = new ModelStateDictionary();
|
|
ms.AddVariantValidationError("en-US", null, "generic culture error");
|
|
|
|
result = ms.GetVariantsWithErrors("en-US");
|
|
|
|
Assert.AreEqual(1, result.Count);
|
|
Assert.AreEqual("en-US", result[0].culture);
|
|
}
|
|
|
|
[Test]
|
|
public void Get_Cultures_With_Property_Errors()
|
|
{
|
|
var ms = new ModelStateDictionary();
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null); // invariant property
|
|
ms.AddPropertyError(new ValidationResult("title missing"), "title", "en-US"); // variant property
|
|
|
|
var result = ms.GetVariantsWithPropertyErrors("en-US");
|
|
|
|
// even though there are 2 errors, they are both for en-US since that is the default language and one of the errors is for an invariant property
|
|
Assert.AreEqual(1, result.Count);
|
|
Assert.AreEqual("en-US", result[0].culture);
|
|
}
|
|
|
|
[Test]
|
|
public void Add_Invariant_Property_Error()
|
|
{
|
|
var ms = new ModelStateDictionary();
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null); // invariant property
|
|
|
|
Assert.AreEqual("_Properties.headerImage.invariant.null", ms.Keys.First());
|
|
}
|
|
|
|
[Test]
|
|
public void Add_Variant_Property_Error()
|
|
{
|
|
var ms = new ModelStateDictionary();
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", "en-US"); // variant property
|
|
|
|
Assert.AreEqual("_Properties.headerImage.en-US.null", ms.Keys.First());
|
|
}
|
|
|
|
[Test]
|
|
public void Add_Invariant_Segment_Property_Error()
|
|
{
|
|
var ms = new ModelStateDictionary();
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null, "mySegment"); // invariant/segment property
|
|
|
|
Assert.AreEqual("_Properties.headerImage.invariant.mySegment", ms.Keys.First());
|
|
}
|
|
|
|
[Test]
|
|
public void Add_Variant_Segment_Property_Error()
|
|
{
|
|
var ms = new ModelStateDictionary();
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", "en-US", "mySegment"); // variant/segment property
|
|
|
|
Assert.AreEqual("_Properties.headerImage.en-US.mySegment", ms.Keys.First());
|
|
}
|
|
|
|
[Test]
|
|
public void Add_Invariant_Segment_Field_Property_Error()
|
|
{
|
|
var ms = new ModelStateDictionary();
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
|
|
|
ms.AddPropertyError(new ValidationResult("no header image", new[] { "myField" }), "headerImage", null, "mySegment"); // invariant/segment property
|
|
|
|
Assert.AreEqual("_Properties.headerImage.invariant.mySegment.myField", ms.Keys.First());
|
|
}
|
|
|
|
[Test]
|
|
public void Add_Variant_Segment_Field_Property_Error()
|
|
{
|
|
var ms = new ModelStateDictionary();
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
|
|
|
ms.AddPropertyError(new ValidationResult("no header image", new[] { "myField" }), "headerImage", "en-US", "mySegment"); // variant/segment property
|
|
|
|
Assert.AreEqual("_Properties.headerImage.en-US.mySegment.myField", ms.Keys.First());
|
|
}
|
|
}
|