diff --git a/form-editor-studio-lib/pom.xml b/form-editor-studio-lib/pom.xml index 109d430c..65cf3134 100644 --- a/form-editor-studio-lib/pom.xml +++ b/form-editor-studio-lib/pom.xml @@ -55,8 +55,16 @@ com.coremedia.cms coremedia-rest-plugins + + com.coremedia.blueprint.base + bpbase-uapi-util + + + org.springframework + spring-beans + org.springframework spring-core @@ -67,6 +75,11 @@ + + com.coremedia.cms + coremedia-spring + test + junit junit diff --git a/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/ConsentFormValidator.java b/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/ConsentFormValidator.java new file mode 100644 index 00000000..fd9f7e3d --- /dev/null +++ b/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/ConsentFormValidator.java @@ -0,0 +1,55 @@ +/* + * Copyright 2018 Tallence AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.tallence.formeditor.studio.validator.field; + +import com.coremedia.blueprint.base.util.StructUtil; +import com.coremedia.cap.struct.Struct; +import com.coremedia.rest.validation.Issues; +import com.coremedia.rest.validation.Severity; +import com.tallence.formeditor.contentbeans.FormEditor; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import java.util.Collections; +import java.util.List; + +/** + * Validates, that ConsentFormCheckBoxes have a text and a link. + */ +@Component +public class ConsentFormValidator implements FieldValidator { + @Override + public List resonsibleFor() { + return Collections.singletonList("ConsentFormCheckBox"); + } + + @Override + public void validateField(Struct fieldData, String action, Issues issues) { + + String name = StructUtil.getString(fieldData, "name"); + if (fieldData.get("linkTarget") == null) { + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_missing_linkTarget", name); + } + + String hint = StructUtil.getString(fieldData, "hint"); + if (StringUtils.isEmpty(hint)) { + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_missing_hint", name); + } else if (!hint.matches(".*%.+%.*")) { + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_invalid_hint", name); + } + } +} diff --git a/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/NameNotEmptyValidator.java b/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/NameNotEmptyValidator.java index 4a431466..861f48bc 100644 --- a/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/NameNotEmptyValidator.java +++ b/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/NameNotEmptyValidator.java @@ -16,6 +16,7 @@ package com.tallence.formeditor.studio.validator.field; +import com.coremedia.blueprint.base.util.StructUtil; import com.coremedia.cap.struct.Struct; import com.coremedia.rest.validation.Issues; import com.coremedia.rest.validation.Severity; @@ -39,8 +40,8 @@ public List resonsibleFor() { @Override public void validateField(Struct fieldData, String action, Issues issues) { - if (!StringUtils.hasText(fieldData.getString("name"))) { - issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "missing_name"); + if (!StringUtils.hasText(StructUtil.getString(fieldData, "name"))) { + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formField_missing_name", fieldData.get("type")); } } } diff --git a/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/OptionsNotEmptyValidator.java b/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/OptionsNotEmptyValidator.java new file mode 100644 index 00000000..103ac3ad --- /dev/null +++ b/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/OptionsNotEmptyValidator.java @@ -0,0 +1,55 @@ +/* + * Copyright 2018 Tallence AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.tallence.formeditor.studio.validator.field; + +import com.coremedia.blueprint.base.util.StructUtil; +import com.coremedia.cap.struct.Struct; +import com.coremedia.rest.validation.Issues; +import com.coremedia.rest.validation.Severity; +import com.tallence.formeditor.contentbeans.FormEditor; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +/** + * Validates, that CheckBoxes, DropDowns and RadioButtons have at least one groupElement. + */ +@Component +public class OptionsNotEmptyValidator implements FieldValidator { + @Override + public List resonsibleFor() { + return Arrays.asList("RadioButtons", "CheckBoxes", "SelectBox"); + } + + @Override + public void validateField(Struct fieldData, String action, Issues issues) { + + boolean noElements = Optional.ofNullable(StructUtil.getSubstruct(fieldData, "groupElements")) + .map(s -> s.getProperties().isEmpty()) + .orElse(true); + if (noElements) { + + String messageKey = fieldData.getString("type").toLowerCase() + "_missing_options"; + + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, messageKey, fieldData.getString("name")); + } + + } + +} diff --git a/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/TextFieldValidator.java b/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/TextFieldValidator.java index 3d6f84ec..f03c6c0c 100644 --- a/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/TextFieldValidator.java +++ b/form-editor-studio-lib/src/main/java/com/tallence/formeditor/studio/validator/field/TextFieldValidator.java @@ -16,6 +16,7 @@ package com.tallence.formeditor.studio.validator.field; +import com.coremedia.blueprint.base.util.StructUtil; import com.coremedia.cap.struct.Struct; import com.coremedia.rest.validation.Issues; import com.coremedia.rest.validation.Severity; @@ -44,7 +45,7 @@ public List resonsibleFor() { @Override public void validateField(Struct fieldData, String action, Issues issues) { - Struct validator = (Struct) fieldData.get(VALIDATOR); + Struct validator = StructUtil.getSubstruct(fieldData, VALIDATOR); if (validator != null) { validateFieldValidators(validator, issues); } @@ -52,13 +53,13 @@ public void validateField(Struct fieldData, String action, Issues issues) { private void validateFieldValidators(Struct validator, Issues issues) { // Size constraints - Integer minSize = (Integer) validator.get("minSize"); - Integer maxSize = (Integer) validator.get("maxSize"); + Integer minSize = StructUtil.getInteger(validator, "minSize"); + Integer maxSize = StructUtil.getInteger(validator, "maxSize"); validateMinSize(minSize, issues); validateMaxSize(maxSize, minSize, issues); // Regex - String regex = (String) validator.get(REGEX); + String regex = StructUtil.getString(validator, REGEX); if (StringUtils.hasLength(regex)) { validateRegex(regex, issues); } @@ -66,15 +67,15 @@ private void validateFieldValidators(Struct validator, Issues issues) { private void validateMinSize(Integer minSize, Issues issues) { if (minSize != null && minSize < 0) { - issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "invalid_minsize"); + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formfield_validator_invalid_minsize", minSize); } } private void validateMaxSize(Integer maxSize, Integer minSize, Issues issues) { if (maxSize != null && maxSize < 0) { - issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "invalid_maxsize"); + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formfield_validator_invalid_maxsize", maxSize); } else if (maxSize != null && minSize != null && maxSize < minSize) { - issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "maxsize_smaller_minsize"); + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formfield_validator_maxsize_smaller_minsize"); } } @@ -82,7 +83,7 @@ private void validateRegex(String regex, Issues issues) { try { Pattern.compile(regex); } catch (PatternSyntaxException e) { - issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "form_action_invalid_regexp"); + issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formfield_validator_invalid_regexp"); } } diff --git a/form-editor-studio-lib/src/test/java/com/tallence/formeditor/studio/validator/FormEditorValidatorTest.java b/form-editor-studio-lib/src/test/java/com/tallence/formeditor/studio/validator/FormEditorValidatorTest.java index b2594abe..0ca5ccb9 100644 --- a/form-editor-studio-lib/src/test/java/com/tallence/formeditor/studio/validator/FormEditorValidatorTest.java +++ b/form-editor-studio-lib/src/test/java/com/tallence/formeditor/studio/validator/FormEditorValidatorTest.java @@ -37,6 +37,10 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import java.util.Collections; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.hasItems; import static org.junit.Assert.*; @@ -88,6 +92,40 @@ public void testInvaildMailAction() { assertThat(issues.getByProperty().get(FormEditor.FORM_ELEMENTS), hasItem(issue2)); } + @Test + public void testEmptyOptions() { + Content testContent = contentRepository.getContent("10"); + IssuesImpl issues = new IssuesImpl<>(testContent, testContent.getProperties().keySet()); + formEditorValidator.validate(testContent, issues); + + Issue issue1 = new Issue<>(testContent, Severity.ERROR, FormEditor.FORM_ELEMENTS, "missing_options", Collections.singletonList("without buttons")); + Set> formElementIssues = issues.getByProperty().get(FormEditor.FORM_ELEMENTS); + assertThat(formElementIssues, hasItem(issue1)); + + assertThat(formElementIssues.size(), equalTo(1)); + } + + @Test + public void testConsentForm() { + Content testContent = contentRepository.getContent("12"); + IssuesImpl issues = new IssuesImpl<>(testContent, testContent.getProperties().keySet()); + formEditorValidator.validate(testContent, issues); + + Set> formElementIssues = issues.getByProperty().get(FormEditor.FORM_ELEMENTS); + + Issue issue1 = new Issue<>(testContent, Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_missing_linkTarget", Collections.singletonList("ConsentForm missing target")); + assertThat(formElementIssues, hasItem(issue1)); + + Issue issue2 = new Issue<>(testContent, Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_missing_hint", Collections.singletonList("ConsentForm missing hint")); + assertThat(formElementIssues, hasItem(issue2)); + + Issue issue3 = new Issue<>(testContent, Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_invalid_hint", Collections.singletonList("ConsentForm invalid hint")); + assertThat(formElementIssues, hasItem(issue3)); + + + assertThat(formElementIssues.size(), equalTo(3)); + } + @Test public void testInvalidFieldName() { Content testContent = contentRepository.getContent("8"); diff --git a/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/10formElements.xml b/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/10formElements.xml new file mode 100644 index 00000000..bac5e2bf --- /dev/null +++ b/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/10formElements.xml @@ -0,0 +1,39 @@ + + + + + + + + RadioButtons + without buttons + + + + + RadioButtons + with button + + + + + + + + + + \ No newline at end of file diff --git a/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/12formElements.xml b/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/12formElements.xml new file mode 100644 index 00000000..27589773 --- /dev/null +++ b/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/12formElements.xml @@ -0,0 +1,52 @@ + + + + + + + + ConsentFormCheckBox + ConsentForm valid + That is the %target% + + + + + + ConsentFormCheckBox + ConsentForm missing target + That is the %target% + + + + + ConsentFormCheckBox + ConsentForm missing hint + + + + + + ConsentFormCheckBox + ConsentForm invalid hint + + Hint without placeholder + + + + + \ No newline at end of file diff --git a/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/FormEditorValidatorTest-content.xml b/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/FormEditorValidatorTest-content.xml index f719a64a..309b49f1 100644 --- a/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/FormEditorValidatorTest-content.xml +++ b/form-editor-studio-lib/src/test/resources/com/tallence/formeditor/studio/validator/FormEditorValidatorTest-content.xml @@ -52,6 +52,18 @@ + + + + + + + + + + + + diff --git a/form-editor-studio-plugin/src/main/joo/com/tallence/formeditor/studio/bundles/FormValidation.properties b/form-editor-studio-plugin/src/main/joo/com/tallence/formeditor/studio/bundles/FormValidation.properties index 7d4a6359..a6135bb6 100644 --- a/form-editor-studio-plugin/src/main/joo/com/tallence/formeditor/studio/bundles/FormValidation.properties +++ b/form-editor-studio-plugin/src/main/joo/com/tallence/formeditor/studio/bundles/FormValidation.properties @@ -1,3 +1,14 @@ #Custom Validator messages Validator_form_action_mail_file_text = The mail action "mail" cannot be used with fileUpload-fields. -Validator_form_action_mail_text = The mail action "mail" cannot be used without entering a mail address. \ No newline at end of file +Validator_form_action_mail_text = The mail action "mail" cannot be used without entering a mail address. +Validator_consentForm_missing_linkTarget_text = The ConsentForm '{0}' must link to a document, describing the consent. +Validator_consentForm_missing_hint_text = The ConsentForm '{0}' must contain a Text, describing the consent. +Validator_consentForm_invalid_hint_text = The ConsentForm '{0}' must contain a Text, containing a placeholder like "%click here for more info%". +Validator_radiobuttons_missing_options_text = The RadioButtons '{0}' must have at least one button. +Validator_checkboxes_missing_options_text = The CheckBoxes '{0}' must have at least one checkBox. +Validator_selectbox_missing_options_text = The DropDown '{0}' must have at least one option. +Validator_formField_missing_name_text = The FormField of the type '{0}' must have a name, please enter one. +Validator_formfield_validator_invalid_minsize_text = The validator of the FormField '{0}' has an invalid minSize: '{1}'. +Validator_formfield_validator_invalid_maxsize_text = The validator of the FormField '{0}' has an invalid maxSize: '{1}'. +Validator_formfield_validator_maxsize_smaller_minsize_text = The validator of the FormField '{0}' has a minSize which is greater than the maxSize. +Validator_formfield_validator_invalid_regexp_text = The validator of the FormField '{0}' uses an invalid regular expression to validate the input. \ No newline at end of file diff --git a/form-editor-studio-plugin/src/main/joo/com/tallence/formeditor/studio/bundles/FormValidation_de.properties b/form-editor-studio-plugin/src/main/joo/com/tallence/formeditor/studio/bundles/FormValidation_de.properties index e1404780..392821fd 100644 --- a/form-editor-studio-plugin/src/main/joo/com/tallence/formeditor/studio/bundles/FormValidation_de.properties +++ b/form-editor-studio-plugin/src/main/joo/com/tallence/formeditor/studio/bundles/FormValidation_de.properties @@ -1,3 +1,14 @@ #Custom Validator messages Validator_form_action_mail_file_text = Der Formular-Typ Mail kann nicht mit einem Formularelement vom Typ "Dateiupload" verwendet werden. -Validator_form_action_mail_text = Bitte geben Sie eine E-Mail-Adresse ein wenn der Formular-Typ "Mail" genutzt wird. \ No newline at end of file +Validator_form_action_mail_text = Bitte geben Sie eine E-Mail-Adresse ein wenn der Formular-Typ "Mail" genutzt wird. +Validator_consentForm_missing_linkTarget_text = Die Einverst\u00E4ndniserkl\u00E4rung '{0}' muss einen Link zu einem Dokument mit mehr Informationen enthalten. +Validator_consentForm_missing_hint_text = Die Einverst\u00E4ndniserkl\u00E4rung '{0}' muss einen Text enthalten. +Validator_consentForm_invalid_hint_text = Die Einverst\u00E4ndniserkl\u00E4rung '{0}' muss einen Text mit einem Platzhalter, z.B. "bitte die %Erkl\u00E4rung% best\u00E4tigen". +Validator_radiobuttons_missing_options_text = Die RadioButtons '{0}' m\u00FCssen mindestens einen Button haben. +Validator_checkboxes_missing_options_text = Die CheckBoxen '{0}' m\u00FCssen mindestens eine CheckBox haben. +Validator_selectbox_missing_options_text = Die DropDown-Liste '{0}' muss mindestens eine Option haben. +Validator_formField_missing_name_text = Das Formular-Element vom Typ '{0}' muss einen Namen haben. +Validator_formfield_validator_invalid_minsize_text = Der Validator des Formular-Elements '{0}' hat eine invalide minimale Gr\u00F6\u00DFe: '{1}'. +Validator_formfield_validator_invalid_maxsize_text = Der Validator des Formular-Elements '{0}' hat eine invalide maximale Gr\u00F6\u00DFe: '{1}'. +Validator_formfield_validator_maxsize_smaller_minsize_text = Der Validator des Formular-Elements '{0}' nutzt eine minimale Gr\u00F6\u00DFe, die gr\u00F6\u00DFer als die maximale Gr\u00F6\u00DFe ist. +Validator_formfield_validator_invalid_regexp_text = Der Validator des Formular-Elements '{0}' nutzt einen invaliden regul\u00E4ren Ausdruck. \ No newline at end of file