Changes protected field to property, makes Regex/Required valiidators more resilient to testing removes the need for setting up Current in the ManifestParserTests

This commit is contained in:
Shannon
2019-11-13 11:43:16 +11:00
parent 5bcf9ef102
commit 46b27d1257
7 changed files with 32 additions and 36 deletions

View File

@@ -20,9 +20,8 @@ namespace Umbraco.Core.PropertyEditors
/// </summary>
public class DataValueEditor : IDataValueEditor
{
protected readonly IDataTypeService _dataTypeService;
protected readonly ILocalizationService _localizationService;
private string _view;
protected IDataTypeService DataTypeService { get; }
protected ILocalizationService LocalizationService { get; }
/// <summary>
/// Initializes a new instance of the <see cref="DataValueEditor"/> class.
@@ -31,8 +30,8 @@ namespace Umbraco.Core.PropertyEditors
{
ValueType = ValueTypes.String;
Validators = new List<IValueValidator>();
_dataTypeService = dataTypeService;
_localizationService = localizationService;
DataTypeService = dataTypeService;
LocalizationService = localizationService;
}
/// <summary>
@@ -50,8 +49,8 @@ namespace Umbraco.Core.PropertyEditors
ValueType = attribute.ValueType;
HideLabel = attribute.HideLabel;
_dataTypeService = dataTypeService;
_localizationService = localizationService;
DataTypeService = dataTypeService;
LocalizationService = localizationService;
}
/// <summary>
@@ -111,12 +110,12 @@ namespace Umbraco.Core.PropertyEditors
/// <summary>
/// Gets the validator used to validate the special property type -level "required".
/// </summary>
public virtual IValueRequiredValidator RequiredValidator => new RequiredValidator();
public virtual IValueRequiredValidator RequiredValidator => new RequiredValidator(); //TODO: Pass in the ILocalizedTextService here and not rely on Current!
/// <summary>
/// Gets the validator used to validate the special property type -level "format".
/// </summary>
public virtual IValueFormatValidator FormatValidator => new RegexValidator();
public virtual IValueFormatValidator FormatValidator => new RegexValidator(); //TODO: Pass in the ILocalizedTextService here and not rely on Current!
/// <summary>
/// If this is true than the editor will be displayed full width without a label

View File

@@ -16,6 +16,8 @@ namespace Umbraco.Core.PropertyEditors.Validators
private readonly ILocalizedTextService _textService;
private string _regex;
const string ValueIsInvalid = "Value is invalid, it does not match the correct pattern";
/// <inheritdoc cref="IManifestValueValidator.ValidationName"/>
public string ValidationName => "Regex";
@@ -26,7 +28,7 @@ namespace Umbraco.Core.PropertyEditors.Validators
/// and the regular expression is supplied at validation time. This constructor is also used when
/// the validator is used as an <see cref="IManifestValueValidator"/> and the regular expression
/// is supplied via the <see cref="Configuration"/> method.</remarks>
public RegexValidator() : this(Current.Services.TextService, null)
public RegexValidator() : this(Current.HasFactory ? Current.Services.TextService : null, null)
{ }
/// <summary>
@@ -68,7 +70,9 @@ namespace Umbraco.Core.PropertyEditors.Validators
{
if (string.IsNullOrWhiteSpace(format)) throw new ArgumentNullOrEmptyException(nameof(format));
if (value == null || !new Regex(format).IsMatch(value.ToString()))
yield return new ValidationResult(_textService.Localize("validation", "invalidPattern"), new[] { "value" });
{
yield return new ValidationResult(_textService?.Localize("validation", "invalidPattern") ?? ValueIsInvalid, new[] { "value" });
}
}
}
}

View File

@@ -11,8 +11,10 @@ namespace Umbraco.Core.PropertyEditors.Validators
public sealed class RequiredValidator : IValueRequiredValidator, IManifestValueValidator
{
private readonly ILocalizedTextService _textService;
const string ValueCannotBeNull = "Value cannot be null";
const string ValueCannotBeEmpty = "Value cannot be empty";
public RequiredValidator() : this(Current.Services.TextService)
public RequiredValidator() : this(Current.HasFactory ? Current.Services.TextService : null)
{
}
@@ -35,20 +37,24 @@ namespace Umbraco.Core.PropertyEditors.Validators
{
if (value == null)
{
yield return new ValidationResult(_textService.Localize("validation", "invalidNull"), new[] {"value"});
yield return new ValidationResult(_textService?.Localize("validation", "invalidNull") ?? ValueCannotBeNull, new[] {"value"});
yield break;
}
if (valueType.InvariantEquals(ValueTypes.Json))
{
if (value.ToString().DetectIsEmptyJson())
yield return new ValidationResult(_textService.Localize("validation", "invalidEmpty"), new[] { "value" });
{
yield return new ValidationResult(_textService?.Localize("validation", "invalidEmpty") ?? ValueCannotBeEmpty, new[] { "value" });
}
yield break;
}
if (value.ToString().IsNullOrWhiteSpace())
{
yield return new ValidationResult(_textService.Localize("validation", "invalidEmpty"), new[] { "value" });
yield return new ValidationResult(_textService?.Localize("validation", "invalidEmpty") ?? ValueCannotBeEmpty, new[] { "value" });
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Linq;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using Umbraco.Core.IO;
using Umbraco.Core.Manifest;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Membership;
@@ -68,7 +69,7 @@ namespace Umbraco.Tests.Manifest
private void AssertDefinition(object source, bool expected, string[] show, IReadOnlyUserGroup[] groups)
{
var definition = JsonConvert.DeserializeObject<ManifestContentAppDefinition>("{" + (show.Length == 0 ? "" : " \"show\": [" + string.Join(",", show.Select(x => "\"" + x + "\"")) + "] ") + "}");
var factory = new ManifestContentAppFactory(definition, Current.IOHelper);
var factory = new ManifestContentAppFactory(definition, IOHelper.Default);
var app = factory.GetContentAppFor(source, groups);
if (expected)
Assert.IsNotNull(app);

View File

@@ -13,6 +13,7 @@ using Umbraco.Core.PropertyEditors;
using Umbraco.Core.PropertyEditors.Validators;
using Umbraco.Core.Services;
using Umbraco.Core.Dashboards;
using Umbraco.Core.IO;
using Umbraco.Core.Serialization;
namespace Umbraco.Tests.Manifest
@@ -25,27 +26,12 @@ namespace Umbraco.Tests.Manifest
[SetUp]
public void Setup()
{
Current.Reset();
var factory = Mock.Of<IFactory>();
Current.Factory = factory;
var serviceContext = ServiceContext.CreatePartial(
localizedTextService: Mock.Of<ILocalizedTextService>());
Mock.Get(factory)
.Setup(x => x.GetInstance(It.IsAny<Type>()))
.Returns<Type>(x =>
{
if (x == typeof(ServiceContext)) return serviceContext;
throw new Exception("oops");
});
var validators = new IManifestValueValidator[]
{
new RequiredValidator(Mock.Of<ILocalizedTextService>()),
new RegexValidator(Mock.Of<ILocalizedTextService>(), null)
};
_parser = new ManifestParser(AppCaches.Disabled, new ManifestValueValidatorCollection(validators), new ManifestFilterCollection(Array.Empty<IManifestFilter>()), Mock.Of<ILogger>(), Current.IOHelper, Mock.Of<IDataTypeService>(), Mock.Of<ILocalizationService>(), new JsonNetSerializer());
_parser = new ManifestParser(AppCaches.Disabled, new ManifestValueValidatorCollection(validators), new ManifestFilterCollection(Array.Empty<IManifestFilter>()), Mock.Of<ILogger>(), IOHelper.Default, Mock.Of<IDataTypeService>(), Mock.Of<ILocalizationService>(), new JsonNetSerializer());
}
[Test]

View File

@@ -47,7 +47,7 @@ namespace Umbraco.Web.PropertyEditors
value = new ImageCropperValue { Src = val.ToString() };
}
var dataType = _dataTypeService.GetDataType(property.PropertyType.DataTypeId);
var dataType = DataTypeService.GetDataType(property.PropertyType.DataTypeId);
if (dataType?.Configuration != null)
value.ApplyConfiguration(dataType.ConfigurationAs<ImageCropperConfiguration>());
@@ -172,7 +172,7 @@ namespace Umbraco.Web.PropertyEditors
return val;
// more magic here ;-(
var configuration = _dataTypeService.GetDataType(propertyType.DataTypeId).ConfigurationAs<ImageCropperConfiguration>();
var configuration = DataTypeService.GetDataType(propertyType.DataTypeId).ConfigurationAs<ImageCropperConfiguration>();
var crops = configuration?.Crops ?? Array.Empty<ImageCropperConfiguration.Crop>();
return JsonConvert.SerializeObject(new

View File

@@ -133,7 +133,7 @@ namespace Umbraco.Web.PropertyEditors
{
continue;
}
var tempConfig = _dataTypeService.GetDataType(propType.DataTypeId).Configuration;
var tempConfig = DataTypeService.GetDataType(propType.DataTypeId).Configuration;
var valEditor = propEditor.GetValueEditor(tempConfig);
var convValue = valEditor.ConvertDbToString(propType, propValues[propAlias]?.ToString());
propValues[propAlias] = convValue;
@@ -201,7 +201,7 @@ namespace Umbraco.Web.PropertyEditors
propValues[propAlias] = tempProp.GetValue()?.ToString();
continue;
}
var tempConfig = _dataTypeService.GetDataType(propType.DataTypeId).Configuration;
var tempConfig = DataTypeService.GetDataType(propType.DataTypeId).Configuration;
var valEditor = propEditor.GetValueEditor(tempConfig);
var convValue = valEditor.ToEditor(tempProp);
propValues[propAlias] = convValue == null ? null : JToken.FromObject(convValue);