From e5dbcf9188d550b1f314bc9e924efb53a7198277 Mon Sep 17 00:00:00 2001 From: Shubham Goyal Date: Thu, 24 Oct 2024 12:48:12 +0200 Subject: [PATCH] Fix instance accumulation for menus with same id --- .../org/commcare/session/CommCareSession.java | 4 +--- .../backend/session/test/MenuTests.java | 19 +++++++++++++++++++ src/test/resources/app_structure/suite.xml | 3 ++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/commcare/session/CommCareSession.java b/src/main/java/org/commcare/session/CommCareSession.java index 76b5cb7bc..ae2fb9dcb 100644 --- a/src/main/java/org/commcare/session/CommCareSession.java +++ b/src/main/java/org/commcare/session/CommCareSession.java @@ -633,7 +633,6 @@ public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif, Vector entries = getEntriesForCommand(command); Vector menus = getMenusForCommand(command); - Menu menu = null; Entry entry = null; Hashtable instancesInScope = new Hashtable<>(); Hashtable menuInstances = null; @@ -649,8 +648,7 @@ public EvaluationContext getEvaluationContext(InstanceInitializationFactory iif, } } - if (!menus.isEmpty()) { - menu = menus.elementAt(0); + for (Menu menu : menus) { if (menu != null) { menuInstances = menu.getInstances(instancesToInclude); } diff --git a/src/test/java/org/commcare/backend/session/test/MenuTests.java b/src/test/java/org/commcare/backend/session/test/MenuTests.java index 5ecb9d94e..0da6b63d3 100644 --- a/src/test/java/org/commcare/backend/session/test/MenuTests.java +++ b/src/test/java/org/commcare/backend/session/test/MenuTests.java @@ -2,7 +2,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.commcare.modern.session.SessionWrapper; +import org.commcare.session.CommCareSession; import org.commcare.suite.model.AssertionSet; import org.commcare.suite.model.Menu; import org.commcare.suite.model.Suite; @@ -12,6 +15,8 @@ import org.junit.Before; import org.junit.Test; +import java.util.List; + /** * Tests for assertions in menus */ @@ -36,4 +41,18 @@ public void testAssertionsEvaluated() throws Exception { assertNotNull(assertions); assertEquals("custom_assertion.m0.0", assertionFailures.getArgument()); } + + /** + * When there are multiple menu blocks with same ids, we should accumulate the required instances from all + * of the menus and their contained entries + */ + @Test + public void testMenuInstances_WhenMenuHaveSameIds() { + SessionWrapper currentSession = appWithMenuAssertions.getSession(); + EvaluationContext ec = currentSession.getEvaluationContext(currentSession.getIIF(), "m3", null); + List instanceIds = ec.getInstanceIds(); + assertEquals(2, instanceIds.size()); + assertTrue(instanceIds.contains("my_instance")); + assertTrue(instanceIds.contains("casedb")); + } } diff --git a/src/test/resources/app_structure/suite.xml b/src/test/resources/app_structure/suite.xml index 1bbaecc46..72c3a538d 100644 --- a/src/test/resources/app_structure/suite.xml +++ b/src/test/resources/app_structure/suite.xml @@ -244,9 +244,10 @@ Menu - + Menu +