Exemplo n.º 1
0
        public void ValidationTests()
        {
            PropertyType propertyType = new PropertyTypeBuilder()
                                        .WithAlias("prop")
                                        .WithSupportsPublishing(true)
                                        .Build();

            var prop = new Property(propertyType);

            prop.SetValue("a");
            Assert.AreEqual("a", prop.GetValue());
            Assert.IsNull(prop.GetValue(published: true));
            PropertyValidationService propertyValidationService = GetPropertyValidationService();

            Assert.IsTrue(propertyValidationService.IsPropertyValid(prop));

            propertyType.Mandatory = true;
            Assert.IsTrue(propertyValidationService.IsPropertyValid(prop));

            prop.SetValue(null);
            Assert.IsFalse(propertyValidationService.IsPropertyValid(prop));

            // can publish, even though invalid
            prop.PublishValues();
        }
        private void MockObjects(out PropertyValidationService validationService, out IDataType dt)
        {
            var textService = new Mock <ILocalizedTextService>();

            textService.Setup(x => x.Localize(It.IsAny <string>(), It.IsAny <string>(), Thread.CurrentThread.CurrentCulture, null)).Returns("Localized text");

            var       dataTypeService = new Mock <IDataTypeService>();
            IDataType dataType        = Mock.Of <IDataType>(
                x => x.Configuration == (object)string.Empty && // irrelevant but needs a value
                x.DatabaseType == ValueStorageType.Nvarchar &&
                x.EditorAlias == Constants.PropertyEditors.Aliases.TextBox);

            dataTypeService.Setup(x => x.GetDataType(It.IsAny <int>())).Returns(() => dataType);
            dt = dataType;

            // new data editor that returns a TextOnlyValueEditor which will do the validation for the properties
            IDataEditor dataEditor = Mock.Of <IDataEditor>(
                x => x.Type == EditorType.PropertyValue &&
                x.Alias == Constants.PropertyEditors.Aliases.TextBox);

            Mock.Get(dataEditor).Setup(x => x.GetValueEditor(It.IsAny <object>()))
            .Returns(new CustomTextOnlyValueEditor(new DataEditorAttribute(Constants.PropertyEditors.Aliases.TextBox, "Test Textbox", "textbox"), textService.Object, Mock.Of <IShortStringHelper>(), new JsonNetSerializer(), Mock.Of <IIOHelper>()));

            var propEditors = new PropertyEditorCollection(new DataEditorCollection(() => new[] { dataEditor }));

            validationService = new PropertyValidationService(propEditors, dataTypeService.Object, Mock.Of <ILocalizedTextService>(), new ValueEditorCache());
        }
Exemplo n.º 3
0
        public void ValidationTests()
        {
            var propertyType = new PropertyType("editor", ValueStorageType.Nvarchar)
            {
                Alias = "prop", SupportsPublishing = true
            };
            var prop = new Property(propertyType);

            prop.SetValue("a");
            Assert.AreEqual("a", prop.GetValue());
            Assert.IsNull(prop.GetValue(published: true));
            var propertyValidationService = new PropertyValidationService(
                Current.Factory.GetInstance <PropertyEditorCollection>(),
                Current.Factory.GetInstance <ServiceContext>().DataTypeService,
                Current.Factory.GetInstance <ServiceContext>().TextService);

            Assert.IsTrue(propertyValidationService.IsPropertyValid(prop));

            propertyType.Mandatory = true;
            Assert.IsTrue(propertyValidationService.IsPropertyValid(prop));

            prop.SetValue(null);
            Assert.IsFalse(propertyValidationService.IsPropertyValid(prop));

            // can publish, even though invalid
            prop.PublishValues();
        }
Exemplo n.º 4
0
        public void ContentPublishValuesWithMixedPropertyTypeVariations()
        {
            var propertyValidationService = new PropertyValidationService(
                Current.Factory.GetInstance <PropertyEditorCollection>(),
                Current.Factory.GetInstance <ServiceContext>().DataTypeService,
                Current.Factory.GetInstance <ServiceContext>().TextService);
            const string langFr = "fr-FR";

            // content type varies by Culture
            // prop1 varies by Culture
            // prop2 is invariant

            var contentType = new ContentType(-1)
            {
                Alias = "contentType"
            };

            contentType.Variations |= ContentVariation.Culture;

            var variantPropType = new PropertyType("editor", ValueStorageType.Nvarchar)
            {
                Alias = "prop1", Variations = ContentVariation.Culture, Mandatory = true
            };
            var invariantPropType = new PropertyType("editor", ValueStorageType.Nvarchar)
            {
                Alias = "prop2", Variations = ContentVariation.Nothing, Mandatory = true
            };

            contentType.AddPropertyType(variantPropType);
            contentType.AddPropertyType(invariantPropType);

            var content = new Content("content", -1, contentType)
            {
                Id = 1, VersionId = 1
            };

            content.SetCultureName("hello", langFr);

            //for this test we'll make the french culture the default one - this is needed for publishing invariant property values
            var langFrImpact = CultureImpact.Explicit(langFr, true);

            Assert.IsTrue(content.PublishCulture(langFrImpact));                                         // succeeds because names are ok (not validating properties here)
            Assert.IsFalse(propertyValidationService.IsPropertyDataValid(content, out _, langFrImpact)); // fails because prop1 is mandatory

            content.SetValue("prop1", "a", langFr);
            Assert.IsTrue(content.PublishCulture(langFrImpact)); // succeeds because names are ok (not validating properties here)
            // fails because prop2 is mandatory and invariant and the item isn't published.
            // Invariant is validated against the default language except when there isn't a published version, in that case it's always validated.
            Assert.IsFalse(propertyValidationService.IsPropertyDataValid(content, out _, langFrImpact));
            content.SetValue("prop2", "x");
            Assert.IsTrue(content.PublishCulture(langFrImpact));                                        // still ok...
            Assert.IsTrue(propertyValidationService.IsPropertyDataValid(content, out _, langFrImpact)); // now it's ok

            Assert.AreEqual("a", content.GetValue("prop1", langFr, published: true));
            Assert.AreEqual("x", content.GetValue("prop2", published: true));
        }
Exemplo n.º 5
0
        public void ContentPublishValuesWithMixedPropertyTypeVariations()
        {
            PropertyValidationService propertyValidationService = GetPropertyValidationService();
            const string langFr = "fr-FR";

            // content type varies by Culture
            // prop1 varies by Culture
            // prop2 is invariant
            IContentType contentType = new ContentTypeBuilder()
                                       .WithAlias("contentType")
                                       .Build();

            contentType.Variations |= ContentVariation.Culture;

            PropertyType variantPropType = new PropertyTypeBuilder()
                                           .WithAlias("prop1")
                                           .WithVariations(ContentVariation.Culture)
                                           .WithMandatory(true)
                                           .Build();
            PropertyType invariantPropType = new PropertyTypeBuilder()
                                             .WithAlias("prop2")
                                             .WithVariations(ContentVariation.Nothing)
                                             .WithMandatory(true)
                                             .Build();

            contentType.AddPropertyType(variantPropType);
            contentType.AddPropertyType(invariantPropType);

            Content content = CreateContent(contentType);

            content.SetCultureName("hello", langFr);

            // for this test we'll make the french culture the default one - this is needed for publishing invariant property values
            var langFrImpact = CultureImpact.Explicit(langFr, true);

            Assert.IsTrue(content.PublishCulture(langFrImpact));                                         // succeeds because names are ok (not validating properties here)
            Assert.IsFalse(propertyValidationService.IsPropertyDataValid(content, out _, langFrImpact)); // fails because prop1 is mandatory

            content.SetValue("prop1", "a", langFr);
            Assert.IsTrue(content.PublishCulture(langFrImpact)); // succeeds because names are ok (not validating properties here)

            // Fails because prop2 is mandatory and invariant and the item isn't published.
            // Invariant is validated against the default language except when there isn't a published version, in that case it's always validated.
            Assert.IsFalse(propertyValidationService.IsPropertyDataValid(content, out _, langFrImpact));
            content.SetValue("prop2", "x");
            Assert.IsTrue(content.PublishCulture(langFrImpact));                                        // still ok...
            Assert.IsTrue(propertyValidationService.IsPropertyDataValid(content, out _, langFrImpact)); // now it's ok

            Assert.AreEqual("a", content.GetValue("prop1", langFr, published: true));
            Assert.AreEqual("x", content.GetValue("prop2", published: true));
        }
Exemplo n.º 6
0
        public void ContentPublishValuesWithMixedPropertyTypeVariations()
        {
            var          propertyValidationService = new PropertyValidationService(Current.Factory.GetInstance <PropertyEditorCollection>(), Current.Factory.GetInstance <ServiceContext>().DataTypeService);
            const string langFr = "fr-FR";

            // content type varies by Culture
            // prop1 varies by Culture
            // prop2 is invariant

            var contentType = new ContentType(-1)
            {
                Alias = "contentType"
            };

            contentType.Variations |= ContentVariation.Culture;

            var variantPropType = new PropertyType("editor", ValueStorageType.Nvarchar)
            {
                Alias = "prop1", Variations = ContentVariation.Culture, Mandatory = true
            };
            var invariantPropType = new PropertyType("editor", ValueStorageType.Nvarchar)
            {
                Alias = "prop2", Variations = ContentVariation.Nothing, Mandatory = true
            };

            contentType.AddPropertyType(variantPropType);
            contentType.AddPropertyType(invariantPropType);

            var content = new Content("content", -1, contentType)
            {
                Id = 1, VersionId = 1
            };

            content.SetCultureName("hello", langFr);

            Assert.IsTrue(content.PublishCulture(langFr));                                         // succeeds because names are ok (not validating properties here)
            Assert.IsFalse(propertyValidationService.IsPropertyDataValid(content, out _, langFr)); // fails because prop1 is mandatory

            content.SetValue("prop1", "a", langFr);
            Assert.IsTrue(content.PublishCulture(langFr));                                         // succeeds because names are ok (not validating properties here)
            Assert.IsFalse(propertyValidationService.IsPropertyDataValid(content, out _, langFr)); // fails because prop2 is mandatory and invariant
            content.SetValue("prop2", "x");
            Assert.IsTrue(content.PublishCulture(langFr));                                         // still ok...
            Assert.IsTrue(propertyValidationService.IsPropertyDataValid(content, out _, langFr));  // now it's ok

            Assert.AreEqual("a", content.GetValue("prop1", langFr, published: true));
            Assert.AreEqual("x", content.GetValue("prop2", published: true));
        }
Exemplo n.º 7
0
 public ComplexEditorValidator(PropertyEditorCollection propertyEditors, IDataTypeService dataTypeService, ILocalizedTextService textService)
 {
     _propertyValidationService = new PropertyValidationService(propertyEditors, dataTypeService, textService);
 }