Search in sources :

Example 71 with Content

use of com.enonic.xp.content.Content in project xp by enonic.

the class OccurrenceValidatorTest method given_required_input_at_top_and_inside_formOptionSet_and_formOptionSet_have_other_unrequired_data_when_validate_then_two_errors_are_found.

@Test
public void given_required_input_at_top_and_inside_formOptionSet_and_formOptionSet_have_other_unrequired_data_when_validate_then_two_errors_are_found() {
    FormOptionSet.Builder myOptionSet = FormOptionSet.create().occurrences(Occurrences.create(1, 0)).name("myOptionSet");
    FormOptionSetOption.Builder option1 = FormOptionSetOption.create().name("option1").defaultOption(true);
    option1.addFormItem(Input.create().name("myRequiredInput").label("Input").inputType(InputTypeName.TEXT_LINE).required(true).build());
    myOptionSet.addOptionSetOption(option1.build());
    contentType.getForm().getFormItems().add(myOptionSet.build());
    contentType.getForm().getFormItems().add(Input.create().name("myOtherRequiredInput").label("Other input").inputType(InputTypeName.TEXT_LINE).required(true).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myOptionSet.option1.myUnrequiredData", "1");
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream()).hasSize(1);
    assertThat(validationResults.stream().findFirst()).containsInstanceOf(DataValidationError.class).get().extracting(ValidationError::getArgs, LIST).containsExactly("myOtherRequiredInput", 1, 0);
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 72 with Content

use of com.enonic.xp.content.Content in project xp by enonic.

the class OccurrenceValidatorTest method given_formoptionset_with_input_with_minOccur1_with_one_null_value_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError.

@Test
public void given_formoptionset_with_input_with_minOccur1_with_one_null_value_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError() {
    FormOptionSet.Builder crimes = FormOptionSet.create().occurrences(Occurrences.create(1, 0)).name("crimes");
    FormOptionSetOption.Builder option1 = FormOptionSetOption.create().name("option1").defaultOption(true);
    option1.addFormItem(Input.create().name("description").label("Description").minimumOccurrences(1).inputType(InputTypeName.TEXT_LINE).build());
    option1.addFormItem(Input.create().name("year").label("Year").inputType(InputTypeName.TEXT_LINE).build());
    crimes.addOptionSetOption(option1.build());
    contentType.getForm().getFormItems().add(crimes.build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("crimes[0].option1.description", null);
    content.getData().setString("crimes[0].option1.year", "1989");
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertFalse(validationResults.hasErrors());
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) Test(org.junit.jupiter.api.Test)

Example 73 with Content

use of com.enonic.xp.content.Content in project xp by enonic.

the class OccurrenceValidatorTest method given_input_with_maxOccur1_with_two_nonnull_data_when_validate_then_MaximumOccurrencesValidationError.

@Test
public void given_input_with_maxOccur1_with_two_nonnull_data_when_validate_then_MaximumOccurrencesValidationError() {
    contentType.getForm().getFormItems().add(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).maximumOccurrences(1).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myInput[0]", "1");
    content.getData().setString("myInput[1]", "2");
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream()).allMatch(ve -> ve instanceof DataValidationError);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 74 with Content

use of com.enonic.xp.content.Content in project xp by enonic.

the class OccurrenceValidatorTest method given_required_optionset_with_no_data_and_other_optionset_with_data_when_validate_then_MinimumOccurrencesValidationError.

@Test
public void given_required_optionset_with_no_data_and_other_optionset_with_data_when_validate_then_MinimumOccurrencesValidationError() {
    FormOptionSet.Builder personalia = FormOptionSet.create().multiple(false).required(true).name("personalia");
    FormOptionSetOption.Builder option1 = FormOptionSetOption.create().name("option1").defaultOption(true);
    option1.addFormItem(Input.create().name("eyeColour").label("Eye color").inputType(InputTypeName.TEXT_LINE).build());
    option1.addFormItem(Input.create().name("hairColour").label("Hair color").inputType(InputTypeName.TEXT_LINE).build());
    personalia.addOptionSetOption(option1.build());
    contentType.getForm().getFormItems().add(personalia.build());
    FormOptionSet.Builder crimes = FormOptionSet.create().multiple(false).name("crimes");
    FormOptionSetOption.Builder crimesOption1 = FormOptionSetOption.create().name("option1").defaultOption(true);
    crimesOption1.addFormItem(Input.create().name("description").label("Description").inputType(InputTypeName.TEXT_LINE).build());
    crimesOption1.addFormItem(Input.create().name("year").label("Year").inputType(InputTypeName.TEXT_LINE).build());
    personalia.addOptionSetOption(crimesOption1.build());
    contentType.getForm().getFormItems().add(crimes.build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("name", "Thomas");
    content.getData().setString("crimes[0].option1.description", "Stole tomatoes from neighbour");
    content.getData().setString("crimes[0].option1..year", "1989");
    content.getData().setString("crimes[1].option1..description", "Stole a chocolate from the Matbua shop");
    content.getData().setString("crimes[1].option1..year", "1990");
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream().findFirst()).containsInstanceOf(DataValidationError.class).get().extracting(ValidationError::getArgs, LIST).containsExactly("personalia", 1, 0);
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 75 with Content

use of com.enonic.xp.content.Content in project xp by enonic.

the class OccurrenceValidatorTest method given_formitemset_with_two_inputs_with_minOccur_with_data_when_validate_then_hasErrors_returns_false.

@Test
public void given_formitemset_with_two_inputs_with_minOccur_with_data_when_validate_then_hasErrors_returns_false() {
    FormItemSet crimes = FormItemSet.create().name("crimes").multiple(true).build();
    contentType.getForm().getFormItems().add(crimes);
    crimes.add(Input.create().name("description").label("Description").minimumOccurrences(1).inputType(InputTypeName.TEXT_LINE).build());
    crimes.add(Input.create().name("year").label("Year").minimumOccurrences(2).maximumOccurrences(0).inputType(InputTypeName.TEXT_LINE).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("crimes[0].description", "descr");
    content.getData().setString("crimes[0].year[0]", "1989");
    content.getData().setString("crimes[0].year[1]", "1990");
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertFalse(validationResults.hasErrors());
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormItemSet(com.enonic.xp.form.FormItemSet) Test(org.junit.jupiter.api.Test)

Aggregations

Content (com.enonic.xp.content.Content)560 Test (org.junit.jupiter.api.Test)432 PropertyTree (com.enonic.xp.data.PropertyTree)130 CreateContentParams (com.enonic.xp.content.CreateContentParams)57 ContentId (com.enonic.xp.content.ContentId)50 UpdateContentParams (com.enonic.xp.content.UpdateContentParams)48 ContentPath (com.enonic.xp.content.ContentPath)47 ValidationErrors (com.enonic.xp.content.ValidationErrors)47 Site (com.enonic.xp.site.Site)43 PropertySet (com.enonic.xp.data.PropertySet)30 GetContentTypeParams (com.enonic.xp.schema.content.GetContentTypeParams)27 Page (com.enonic.xp.page.Page)26 Contents (com.enonic.xp.content.Contents)25 Node (com.enonic.xp.node.Node)25 ContentIds (com.enonic.xp.content.ContentIds)24 FindContentByParentParams (com.enonic.xp.content.FindContentByParentParams)24 ContentQuery (com.enonic.xp.content.ContentQuery)23 FindContentByParentResult (com.enonic.xp.content.FindContentByParentResult)23 DataValidationError (com.enonic.xp.content.DataValidationError)21 ContentType (com.enonic.xp.schema.content.ContentType)20