From 4e7f570a18ae647716aa96e30c8929d70ebd91f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20S=C3=A9nave?= <59770457+nsenave@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:04:03 +0200 Subject: [PATCH] fix: subsequence pagination in regrouping treatment (#1095) * fix: subsequence pagination in regrouping treatment * test: add unit test for regrouping pagination --- build.gradle | 2 +- .../LunaticPaginationQuestionMode.java | 24 +++++-- .../LunaticPaginationRegrouping.java | 6 +- ...cPaginationRegroupingIntegrationTest.java} | 2 +- .../LunaticPaginationRegroupingUnitTest.java | 64 +++++++++++++++++++ 5 files changed, 89 insertions(+), 9 deletions(-) rename eno-treatments/src/test/java/fr/insee/eno/treatments/{LunaticPaginationRegroupingTest.java => LunaticPaginationRegroupingIntegrationTest.java} (99%) create mode 100644 eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingUnitTest.java diff --git a/build.gradle b/build.gradle index f787713c0..018819b23 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ tasks.withType(JavaCompile).configureEach { allprojects { group = 'fr.insee.eno' - version = '3.24.1' + version = '3.24.2' } subprojects { diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/pagination/LunaticPaginationQuestionMode.java b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/pagination/LunaticPaginationQuestionMode.java index f9f573b7e..c6c2f276b 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/pagination/LunaticPaginationQuestionMode.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/pagination/LunaticPaginationQuestionMode.java @@ -26,7 +26,7 @@ public boolean canIncrementPageCount(ComponentType component, boolean isParentPa // if component is a subsequence and has no declarations set, it will regroup with next component, so no // increment in this specific case return !component.getComponentType().equals(ComponentTypeEnum.SUBSEQUENCE) || - (component.getDeclarations() != null && !component.getDeclarations().isEmpty()); + hasDeclarationOrDescription(component); } @Override @@ -40,25 +40,41 @@ public void applyLoopPaginationProperty(Loop loop) { @Override public void applyNumPageOnSubsequence(Subsequence subsequence, String numPagePrefix, int pageCount, boolean isParentPaginated) { + // Clear page attributes in case of previous pagination + subsequence.setPage(null); + subsequence.setGoToPage(null); String numPage = numPagePrefix + pageCount; // special case where a subsequence has no declarations (so no page attribute set) and must link to next component - if(isParentPaginated && (subsequence.getDeclarations() == null || subsequence.getDeclarations().isEmpty())) { + if (isParentPaginated && !hasDeclarationOrDescription(subsequence)) { int pageSequence = pageCount + 1; numPage = numPagePrefix + pageSequence; } // if parent paginated or empty declarations - if (subsequence.getDeclarations() == null || subsequence.getDeclarations().isEmpty() || isParentPaginated) { + if (isParentPaginated || !hasDeclarationOrDescription(subsequence)) { subsequence.setGoToPage(numPage); } // if parent not paginated and declarations set - if(!isParentPaginated || (subsequence.getDeclarations() != null && !subsequence.getDeclarations().isEmpty())) { + if (!isParentPaginated || hasDeclarationOrDescription(subsequence)) { subsequence.setPage(numPage); } } + /** + * Checks if the given component has a declaration (one or more) or a description. + * @param component Lunatic component. + * @return True if the component has a declaration or a description. + */ + private static boolean hasDeclarationOrDescription(ComponentType component) { + if (component.getDescription() != null) + return true; + if (component.getDeclarations() != null) + return !component.getDeclarations().isEmpty(); + return false; + } + /** * is Loop linked * @param loop to check diff --git a/eno-treatments/src/main/java/fr/insee/eno/treatments/LunaticPaginationRegrouping.java b/eno-treatments/src/main/java/fr/insee/eno/treatments/LunaticPaginationRegrouping.java index da461a63a..e61abb5dd 100644 --- a/eno-treatments/src/main/java/fr/insee/eno/treatments/LunaticPaginationRegrouping.java +++ b/eno-treatments/src/main/java/fr/insee/eno/treatments/LunaticPaginationRegrouping.java @@ -32,6 +32,9 @@ public LunaticPaginationRegrouping(Regroupements regroupements) { */ @Override public boolean canIncrementPageCount(ComponentType component, boolean isParentPaginated) { + if(component instanceof Question question) + return canIncrementPageCount(question.getComponents().getFirst(), isParentPaginated); + if(!super.canIncrementPageCount(component, isParentPaginated)) { return false; } @@ -42,9 +45,6 @@ public boolean canIncrementPageCount(ComponentType component, boolean isParentPa responseName = simpleResponse.getResponse().getName(); } - if(component instanceof Question question) - return canIncrementPageCount(question.getComponents().getFirst(), isParentPaginated); - // no response name, so no regroupement, we can increment if(responseName == null) { return true; diff --git a/eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingTest.java b/eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingIntegrationTest.java similarity index 99% rename from eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingTest.java rename to eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingIntegrationTest.java index 8ea8db8fc..dfe91035e 100644 --- a/eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingTest.java +++ b/eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingIntegrationTest.java @@ -11,7 +11,7 @@ import static org.junit.jupiter.api.Assertions.*; -class LunaticPaginationRegroupingTest { +class LunaticPaginationRegroupingIntegrationTest { private Questionnaire questionnaire; private Sequence s1, s4, s8, s9; diff --git a/eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingUnitTest.java b/eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingUnitTest.java new file mode 100644 index 000000000..1947e6809 --- /dev/null +++ b/eno-treatments/src/test/java/fr/insee/eno/treatments/LunaticPaginationRegroupingUnitTest.java @@ -0,0 +1,64 @@ +package fr.insee.eno.treatments; + +import fr.insee.eno.treatments.dto.Regroupements; +import fr.insee.lunatic.model.flat.*; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; + +class LunaticPaginationRegroupingUnitTest { + + @Test + void subsequence_noDescription() { + // + Questionnaire questionnaire = new Questionnaire(); + Sequence sequence = new Sequence(); + sequence.setComponentType(ComponentTypeEnum.SEQUENCE); + Subsequence subsequence = new Subsequence(); + subsequence.setComponentType(ComponentTypeEnum.SUBSEQUENCE); + Question question = new Question(); + Input input = new Input(); + input.setResponse(new ResponseType()); + input.getResponse().setName("INPUT_VAR"); + question.getComponents().add(input); + questionnaire.getComponents().add(sequence); + questionnaire.getComponents().add(subsequence); + questionnaire.getComponents().add(question); + // + new LunaticPaginationRegrouping(new Regroupements(new ArrayList<>())).apply(questionnaire); + // + assertEquals("1", questionnaire.getComponents().get(0).getPage()); + assertNull(questionnaire.getComponents().get(1).getPage()); + assertEquals("2", ((Subsequence) questionnaire.getComponents().get(1)).getGoToPage()); + assertEquals("2", questionnaire.getComponents().get(2).getPage()); + } + + @Test + void subsequence_withDescription() { + // + Questionnaire questionnaire = new Questionnaire(); + Sequence sequence = new Sequence(); + sequence.setComponentType(ComponentTypeEnum.SEQUENCE); + Subsequence subsequence = new Subsequence(); + subsequence.setComponentType(ComponentTypeEnum.SUBSEQUENCE); + subsequence.setDescription(new LabelType()); + Question question = new Question(); + Input input = new Input(); + input.setResponse(new ResponseType()); + input.getResponse().setName("INPUT_VAR"); + question.getComponents().add(input); + questionnaire.getComponents().add(sequence); + questionnaire.getComponents().add(subsequence); + questionnaire.getComponents().add(question); + // + new LunaticPaginationRegrouping(new Regroupements(new ArrayList<>())).apply(questionnaire); + // + assertEquals("1", questionnaire.getComponents().get(0).getPage()); + assertEquals("2", questionnaire.getComponents().get(1).getPage()); + assertEquals("2", ((Subsequence) questionnaire.getComponents().get(1)).getGoToPage()); + assertEquals("3", questionnaire.getComponents().get(2).getPage()); + } + +}