2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2019-03-14 00:59:51 +11:00
|
|
|
using System.Linq;
|
2020-08-07 14:00:56 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
2019-03-14 00:59:51 +11:00
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Services;
|
2021-06-25 10:29:18 -06:00
|
|
|
using Umbraco.Extensions;
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.BackOffice.Extensions;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class ModelStateExtensionsTests
|
2019-03-14 00:59:51 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Get_Cultures_With_Errors()
|
2019-03-14 00:59:51 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var ms = new ModelStateDictionary();
|
|
|
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
|
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
2019-03-14 14:18:42 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null); // invariant property
|
|
|
|
|
ms.AddPropertyError(new ValidationResult("title missing"), "title", "en-US"); // variant property
|
2019-03-14 14:18:42 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var result = ms.GetVariantsWithErrors("en-US");
|
2019-03-14 14:18:42 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// 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);
|
2019-03-14 14:18:42 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms = new ModelStateDictionary();
|
|
|
|
|
ms.AddVariantValidationError("en-US", null, "generic culture error");
|
2019-03-14 14:18:42 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
result = ms.GetVariantsWithErrors("en-US");
|
2019-03-14 14:18:42 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(1, result.Count);
|
|
|
|
|
Assert.AreEqual("en-US", result[0].culture);
|
|
|
|
|
}
|
2019-03-14 14:18:42 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[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");
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null); // invariant property
|
|
|
|
|
ms.AddPropertyError(new ValidationResult("title missing"), "title", "en-US"); // variant property
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var result = ms.GetVariantsWithPropertyErrors("en-US");
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// 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);
|
|
|
|
|
}
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Add_Invariant_Property_Error()
|
|
|
|
|
{
|
|
|
|
|
var ms = new ModelStateDictionary();
|
|
|
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
|
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null); // invariant property
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("_Properties.headerImage.invariant.null", ms.Keys.First());
|
|
|
|
|
}
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Add_Variant_Property_Error()
|
|
|
|
|
{
|
|
|
|
|
var ms = new ModelStateDictionary();
|
|
|
|
|
var localizationService = new Mock<ILocalizationService>();
|
|
|
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-US");
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", "en-US"); // variant property
|
2019-03-14 00:59:51 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("_Properties.headerImage.en-US.null", ms.Keys.First());
|
|
|
|
|
}
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[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");
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", null, "mySegment"); // invariant/segment property
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("_Properties.headerImage.invariant.mySegment", ms.Keys.First());
|
|
|
|
|
}
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[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");
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms.AddPropertyError(new ValidationResult("no header image"), "headerImage", "en-US", "mySegment"); // variant/segment property
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("_Properties.headerImage.en-US.mySegment", ms.Keys.First());
|
|
|
|
|
}
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[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");
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms.AddPropertyError(new ValidationResult("no header image", new[] { "myField" }), "headerImage", null, "mySegment"); // invariant/segment property
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("_Properties.headerImage.invariant.mySegment.myField", ms.Keys.First());
|
|
|
|
|
}
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[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");
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
ms.AddPropertyError(new ValidationResult("no header image", new[] { "myField" }), "headerImage", "en-US", "mySegment"); // variant/segment property
|
2020-04-17 16:12:04 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual("_Properties.headerImage.en-US.mySegment.myField", ms.Keys.First());
|
2019-03-14 00:59:51 +11:00
|
|
|
}
|
|
|
|
|
}
|