2019-03-20 10:20:00 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2019-03-14 00:59:51 +11:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Web.Http.ModelBinding;
|
|
|
|
|
|
using Moq;
|
|
|
|
|
|
using NUnit.Framework;
|
2019-03-20 10:20:00 +01:00
|
|
|
|
using Umbraco.Core.Models;
|
2019-03-14 00:59:51 +11:00
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Web
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
|
public class ModelStateExtensionsTests
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2019-03-14 14:18:42 +11:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Get_Cultures_With_Errors()
|
|
|
|
|
|
{
|
|
|
|
|
|
var ms = new ModelStateDictionary();
|
|
|
|
|
|
var localizationService = new Mock<ILocalizationService>();
|
2019-03-20 10:20:00 +01:00
|
|
|
|
var languageList = new List<ILanguage> { new Language("en-US") };
|
|
|
|
|
|
|
|
|
|
|
|
localizationService.Setup(x => x.GetAllLanguages()).Returns(languageList);
|
|
|
|
|
|
localizationService.Setup(x => x.GetDefaultLanguageIsoCode()).Returns("en-us");
|
2019-03-14 14:18:42 +11:00
|
|
|
|
|
|
|
|
|
|
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.GetCulturesWithErrors(localizationService.Object);
|
|
|
|
|
|
|
|
|
|
|
|
//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]);
|
|
|
|
|
|
|
|
|
|
|
|
ms = new ModelStateDictionary();
|
|
|
|
|
|
ms.AddCultureValidationError("en-US", "generic culture error");
|
|
|
|
|
|
|
|
|
|
|
|
result = ms.GetCulturesWithErrors(localizationService.Object);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, result.Count);
|
|
|
|
|
|
Assert.AreEqual("en-US", result[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-14 00:59:51 +11:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Get_Cultures_With_Property_Errors()
|
|
|
|
|
|
{
|
|
|
|
|
|
var ms = new ModelStateDictionary();
|
|
|
|
|
|
var localizationService = new Mock<ILocalizationService>();
|
2019-03-20 10:20:00 +01:00
|
|
|
|
var languageList = new List<ILanguage> { new Language("en-US") };
|
|
|
|
|
|
|
|
|
|
|
|
localizationService.Setup(x => x.GetAllLanguages()).Returns(languageList);
|
2019-03-14 00:59:51 +11:00
|
|
|
|
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.GetCulturesWithPropertyErrors(localizationService.Object);
|
|
|
|
|
|
|
|
|
|
|
|
//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]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[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", 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"); //invariant property
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual("_Properties.headerImage.en-US", ms.Keys.First());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|