diff --git a/src/Umbraco.Core/PropertyEditors/DataValueEditor.cs b/src/Umbraco.Core/PropertyEditors/DataValueEditor.cs index d93ead5838..afd77a3479 100644 --- a/src/Umbraco.Core/PropertyEditors/DataValueEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/DataValueEditor.cs @@ -20,9 +20,8 @@ namespace Umbraco.Core.PropertyEditors /// public class DataValueEditor : IDataValueEditor { - protected readonly IDataTypeService _dataTypeService; - protected readonly ILocalizationService _localizationService; - private string _view; + protected IDataTypeService DataTypeService { get; } + protected ILocalizationService LocalizationService { get; } /// /// Initializes a new instance of the class. @@ -31,8 +30,8 @@ namespace Umbraco.Core.PropertyEditors { ValueType = ValueTypes.String; Validators = new List(); - _dataTypeService = dataTypeService; - _localizationService = localizationService; + DataTypeService = dataTypeService; + LocalizationService = localizationService; } /// @@ -50,8 +49,8 @@ namespace Umbraco.Core.PropertyEditors ValueType = attribute.ValueType; HideLabel = attribute.HideLabel; - _dataTypeService = dataTypeService; - _localizationService = localizationService; + DataTypeService = dataTypeService; + LocalizationService = localizationService; } /// @@ -111,12 +110,12 @@ namespace Umbraco.Core.PropertyEditors /// /// Gets the validator used to validate the special property type -level "required". /// - public virtual IValueRequiredValidator RequiredValidator => new RequiredValidator(); + public virtual IValueRequiredValidator RequiredValidator => new RequiredValidator(); //TODO: Pass in the ILocalizedTextService here and not rely on Current! /// /// Gets the validator used to validate the special property type -level "format". /// - public virtual IValueFormatValidator FormatValidator => new RegexValidator(); + public virtual IValueFormatValidator FormatValidator => new RegexValidator(); //TODO: Pass in the ILocalizedTextService here and not rely on Current! /// /// If this is true than the editor will be displayed full width without a label diff --git a/src/Umbraco.Core/PropertyEditors/Validators/RegexValidator.cs b/src/Umbraco.Core/PropertyEditors/Validators/RegexValidator.cs index 4036adba42..2bd3e0ab40 100644 --- a/src/Umbraco.Core/PropertyEditors/Validators/RegexValidator.cs +++ b/src/Umbraco.Core/PropertyEditors/Validators/RegexValidator.cs @@ -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"; + /// 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 and the regular expression /// is supplied via the method. - public RegexValidator() : this(Current.Services.TextService, null) + public RegexValidator() : this(Current.HasFactory ? Current.Services.TextService : null, null) { } /// @@ -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" }); + } } } } diff --git a/src/Umbraco.Core/PropertyEditors/Validators/RequiredValidator.cs b/src/Umbraco.Core/PropertyEditors/Validators/RequiredValidator.cs index e83997b170..30342fbf2d 100644 --- a/src/Umbraco.Core/PropertyEditors/Validators/RequiredValidator.cs +++ b/src/Umbraco.Core/PropertyEditors/Validators/RequiredValidator.cs @@ -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" }); } } } diff --git a/src/Umbraco.Tests/Manifest/ManifestContentAppTests.cs b/src/Umbraco.Tests/Manifest/ManifestContentAppTests.cs index c949e0bb67..fe3e8a6bdf 100644 --- a/src/Umbraco.Tests/Manifest/ManifestContentAppTests.cs +++ b/src/Umbraco.Tests/Manifest/ManifestContentAppTests.cs @@ -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("{" + (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); diff --git a/src/Umbraco.Tests/Manifest/ManifestParserTests.cs b/src/Umbraco.Tests/Manifest/ManifestParserTests.cs index 936138155e..955c7ba776 100644 --- a/src/Umbraco.Tests/Manifest/ManifestParserTests.cs +++ b/src/Umbraco.Tests/Manifest/ManifestParserTests.cs @@ -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(); - Current.Factory = factory; - - var serviceContext = ServiceContext.CreatePartial( - localizedTextService: Mock.Of()); - - Mock.Get(factory) - .Setup(x => x.GetInstance(It.IsAny())) - .Returns(x => - { - if (x == typeof(ServiceContext)) return serviceContext; - throw new Exception("oops"); - }); - var validators = new IManifestValueValidator[] { new RequiredValidator(Mock.Of()), new RegexValidator(Mock.Of(), null) }; - _parser = new ManifestParser(AppCaches.Disabled, new ManifestValueValidatorCollection(validators), new ManifestFilterCollection(Array.Empty()), Mock.Of(), Current.IOHelper, Mock.Of(), Mock.Of(), new JsonNetSerializer()); + _parser = new ManifestParser(AppCaches.Disabled, new ManifestValueValidatorCollection(validators), new ManifestFilterCollection(Array.Empty()), Mock.Of(), IOHelper.Default, Mock.Of(), Mock.Of(), new JsonNetSerializer()); } [Test] diff --git a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyValueEditor.cs b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyValueEditor.cs index 99e2f8c727..1dab41d4e6 100644 --- a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyValueEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyValueEditor.cs @@ -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()); @@ -172,7 +172,7 @@ namespace Umbraco.Web.PropertyEditors return val; // more magic here ;-( - var configuration = _dataTypeService.GetDataType(propertyType.DataTypeId).ConfigurationAs(); + var configuration = DataTypeService.GetDataType(propertyType.DataTypeId).ConfigurationAs(); var crops = configuration?.Crops ?? Array.Empty(); return JsonConvert.SerializeObject(new diff --git a/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs index 3c9dc53de7..45083a12a1 100644 --- a/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs @@ -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);