diff --git a/src/main/java/org/commcare/formplayer/application/DebuggerController.java b/src/main/java/org/commcare/formplayer/application/DebuggerController.java index 0cc593d30..848288c93 100644 --- a/src/main/java/org/commcare/formplayer/application/DebuggerController.java +++ b/src/main/java/org/commcare/formplayer/application/DebuggerController.java @@ -147,7 +147,15 @@ public EvaluateXPathResponseBean menuEvaluateXpath(@RequestBody EvaluateXPathMen public EvaluateXPathResponseBean evaluateXpath(@RequestBody EvaluateXPathRequestBean evaluateXPathRequestBean, @CookieValue(Constants.POSTGRES_DJANGO_SESSION_ID) String authToken) throws Exception { SerializableFormSession serializableFormSession = formSessionService.getSessionById(evaluateXPathRequestBean.getSessionId()); - FormSession formEntrySession = formSessionFactory.getFormSession(serializableFormSession, evaluateXPathRequestBean.getWindowWidth()); + FormSession formEntrySession; + String locale = evaluateXPathRequestBean.getLocale(); + if (locale != null && locale.equals("test-locale")) { + formEntrySession = formSessionFactory.getFormSessionForTest(serializableFormSession, + evaluateXPathRequestBean.getWindowWidth(), evaluateXPathRequestBean.getLocale()); + } else { + formEntrySession = formSessionFactory.getFormSession(serializableFormSession, evaluateXPathRequestBean.getWindowWidth()); + } + EvaluateXPathResponseBean evaluateXPathResponseBean = new EvaluateXPathResponseBean( formEntrySession.getFormEntryModel().getForm().getEvaluationContext(), evaluateXPathRequestBean.getXpath(), diff --git a/src/main/java/org/commcare/formplayer/application/FormSessionFactory.java b/src/main/java/org/commcare/formplayer/application/FormSessionFactory.java index 1af7b43d1..8d3d996e9 100644 --- a/src/main/java/org/commcare/formplayer/application/FormSessionFactory.java +++ b/src/main/java/org/commcare/formplayer/application/FormSessionFactory.java @@ -10,6 +10,7 @@ import org.commcare.formplayer.session.FormSession; import org.commcare.session.CommCareSession; import org.javarosa.core.model.actions.FormSendCalloutHandler; +import org.javarosa.core.services.locale.Localization; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.lang.Nullable; @@ -60,4 +61,13 @@ public FormSession getFormSession(SerializableFormSession serializableFormSessio windowWidth ); } + + public FormSession getFormSessionForTest(SerializableFormSession serializableFormSession, String windowWidth, String locale) throws Exception { + CommCareSession commCareSession = commCareSessionFactory.getCommCareSession(serializableFormSession.getMenuSessionId()); + if (locale != null) { + Localization.getGlobalLocalizerAdvanced().addAvailableLocale(locale); + Localization.setLocale(locale); + } + return getFormSession(serializableFormSession, commCareSession, windowWidth); + } } diff --git a/src/main/java/org/commcare/formplayer/beans/AuthenticatedRequestBean.java b/src/main/java/org/commcare/formplayer/beans/AuthenticatedRequestBean.java index 4a641747b..fb34fe4ea 100644 --- a/src/main/java/org/commcare/formplayer/beans/AuthenticatedRequestBean.java +++ b/src/main/java/org/commcare/formplayer/beans/AuthenticatedRequestBean.java @@ -22,6 +22,7 @@ public class AuthenticatedRequestBean { private int timezoneOffsetMillis = -1; private String timezoneFromBrowser = null; private String windowWidth; + private String locale; public String getUsername() { return username; @@ -111,4 +112,14 @@ public String getWindowWidth() { public void setWindowWidth(String windowWidth) { this.windowWidth = windowWidth; } + + @JsonGetter(value = "locale") + public String getLocale() { + return locale; + } + + @JsonSetter(value = "locale") + public void setLocale(String locale) { + this.locale = locale; + } } diff --git a/src/test/java/org/commcare/formplayer/junit/request/EvaluateXpathRequest.kt b/src/test/java/org/commcare/formplayer/junit/request/EvaluateXpathRequest.kt index c157aee21..1b03316e5 100644 --- a/src/test/java/org/commcare/formplayer/junit/request/EvaluateXpathRequest.kt +++ b/src/test/java/org/commcare/formplayer/junit/request/EvaluateXpathRequest.kt @@ -16,7 +16,8 @@ class EvaluateXpathRequest( private val sessionId: String, private val xpath: String, private val formSessionService: FormSessionService, - private val windowWidth: String? + private val windowWidth: String?, + private val locale: String? ) : MockRequest( mockMvc, Constants.URL_EVALUATE_XPATH, EvaluateXPathResponseBean::class.java ) { @@ -30,6 +31,7 @@ class EvaluateXpathRequest( bean.xpath = xpath bean.debugOutputLevel = Constants.BASIC_NO_TRACE bean.windowWidth = windowWidth + bean.locale = locale populateFromSession(bean) return requestWithBean(bean) diff --git a/src/test/java/org/commcare/formplayer/tests/BaseTestClass.java b/src/test/java/org/commcare/formplayer/tests/BaseTestClass.java index ac5855238..7fae8dd52 100644 --- a/src/test/java/org/commcare/formplayer/tests/BaseTestClass.java +++ b/src/test/java/org/commcare/formplayer/tests/BaseTestClass.java @@ -636,7 +636,7 @@ FormEntryResponseBean deleteRepeatRequest(String sessionId, String repeatIndex) } EvaluateXPathResponseBean evaluateXPath(String sessionId, String xPath) throws Exception { - return new EvaluateXpathRequest(mockDebuggerController, sessionId, xPath, formSessionService, null) + return new EvaluateXpathRequest(mockDebuggerController, sessionId, xPath, formSessionService, null, null) .request() .bean(); } @@ -676,7 +676,7 @@ EvaluateXPathResponseBean evaluateMenuXpath(String menuSessionId, String xpath) */ protected void checkXpath(String sessionId, String xpath, String expectedValue) throws Exception { - new EvaluateXpathRequest(mockDebuggerController, sessionId, xpath, formSessionService, null) + new EvaluateXpathRequest(mockDebuggerController, sessionId, xpath, formSessionService, null, null) .request() .andExpectAll( jsonPath("status", equalTo(Constants.ANSWER_RESPONSE_STATUS_POSITIVE)), diff --git a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java index 41973d059..1b0f5c594 100644 --- a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java +++ b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java @@ -91,7 +91,7 @@ public void testWindowWidthParam() throws Exception { NewFormResponse.class); EvaluateXPathResponseBean evaluateXpathResponseBean = new EvaluateXpathRequest(mockDebuggerController, formResp.getSessionId(), "instance('commcaresession')/session/context/window_width", - formSessionService, "test-window-width") + formSessionService, "test-window-width", null) .request() .bean(); @@ -99,6 +99,19 @@ public void testWindowWidthParam() throws Exception { assertThat(evaluateXpathResponseBean.getOutput(), hasXpath("/result", equalTo("test-window-width"))); } + @Test + public void testLocaleParam() throws Exception { + NewFormResponse newFormResponse = this.sessionNavigate(new String[]{"0", "0"}, APP, "test-locale", NewFormResponse.class); + EvaluateXPathResponseBean xpathBean = new EvaluateXpathRequest(mockDebuggerController, + newFormResponse.getSessionId(), "instance('commcaresession')/session/context/applanguage", + formSessionService, null, "test-locale") + .request() + .bean(); + + assertEquals(xpathBean.getStatus(), Constants.ANSWER_RESPONSE_STATUS_POSITIVE); + assertThat(xpathBean.getOutput(), hasXpath("/result", equalTo("test-locale"))); + } + @Test public void testConfirmedSelectionsForMultiSelectCaseList() { String[] selections = new String[]{"0", "1", "use_selected_values"};