|
| 1 | +package org.openmrs.module.htmlformentry; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | +import org.openmrs.api.context.Context; |
| 6 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 7 | + |
| 8 | +import java.util.Date; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests that fields hidden by <controls>/<when thenDisplay> are exempt from required |
| 13 | + * validation, and that required validation still fires when the field is visible. |
| 14 | + */ |
| 15 | +public class ControlsWhenRequiredTest extends BaseHtmlFormEntryTest { |
| 16 | + |
| 17 | + @Before |
| 18 | + public void loadData() throws Exception { |
| 19 | + executeVersionedDataSet("org/openmrs/module/htmlformentry/data/RegressionTest-data-openmrs-2.8.xml"); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * When the controlling obs value does not match the thenDisplay condition the section is hidden |
| 24 | + * client-side and the JS populates hfeHiddenFields with the widget name. The server should skip |
| 25 | + * required validation for those fields. |
| 26 | + */ |
| 27 | + @Test |
| 28 | + public void requiredFieldHiddenByControls_shouldNotProduceValidationError() throws Exception { |
| 29 | + final Date date = new Date(); |
| 30 | + new RegressionTestHelper() { |
| 31 | + |
| 32 | + @Override |
| 33 | + public String getFormName() { |
| 34 | + return "obsWithControlsAndRequired"; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String[] widgetLabels() { |
| 39 | + return new String[] { "Date:", "Location:", "Provider:", "Allergy:", "Details:" }; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) { |
| 44 | + request.addParameter(widgets.get("Date:"), dateAsString(date)); |
| 45 | + request.addParameter(widgets.get("Location:"), "2"); |
| 46 | + request.addParameter(widgets.get("Provider:"), "502"); |
| 47 | + // Allergy is left blank — section #allergy-followup stays hidden. |
| 48 | + // Simulate what the JS sends: the hidden field's widget name in hfeHiddenFields. |
| 49 | + request.addParameter("hfeHiddenFields", widgets.get("Details:")); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void testResults(SubmissionResults results) { |
| 54 | + results.assertNoErrors(); |
| 55 | + results.assertEncounterCreated(); |
| 56 | + } |
| 57 | + }.run(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * When the controlling obs value matches the thenDisplay condition the section is shown. The |
| 62 | + * required field inside it must be filled; leaving it empty should produce a validation error. |
| 63 | + */ |
| 64 | + @Test |
| 65 | + public void requiredFieldVisibleByControls_withNoValue_shouldProduceValidationError() throws Exception { |
| 66 | + final Date date = new Date(); |
| 67 | + new RegressionTestHelper() { |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getFormName() { |
| 71 | + return "obsWithControlsAndRequired"; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String[] widgetLabels() { |
| 76 | + return new String[] { "Date:", "Location:", "Provider:", "Allergy:", "Details:" }; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) { |
| 81 | + request.addParameter(widgets.get("Date:"), dateAsString(date)); |
| 82 | + request.addParameter(widgets.get("Location:"), "2"); |
| 83 | + request.addParameter(widgets.get("Provider:"), "502"); |
| 84 | + // CATS (1002) triggers the thenDisplay — section is visible. |
| 85 | + request.addParameter(widgets.get("Allergy:"), "1002"); |
| 86 | + // Details field is visible but intentionally left empty. |
| 87 | + // hfeHiddenFields is not set for the Details widget. |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void testResults(SubmissionResults results) { |
| 92 | + results.assertErrors(1); |
| 93 | + results.assertNoEncounterCreated(); |
| 94 | + } |
| 95 | + }.run(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * When the controlling obs value matches the thenDisplay condition and the required field is |
| 100 | + * filled, the form should submit successfully and save both obs. |
| 101 | + */ |
| 102 | + @Test |
| 103 | + public void requiredFieldVisibleByControls_withValue_shouldSucceed() throws Exception { |
| 104 | + final Date date = new Date(); |
| 105 | + new RegressionTestHelper() { |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getFormName() { |
| 109 | + return "obsWithControlsAndRequired"; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String[] widgetLabels() { |
| 114 | + return new String[] { "Date:", "Location:", "Provider:", "Allergy:", "Details:" }; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) { |
| 119 | + request.addParameter(widgets.get("Date:"), dateAsString(date)); |
| 120 | + request.addParameter(widgets.get("Location:"), "2"); |
| 121 | + request.addParameter(widgets.get("Provider:"), "502"); |
| 122 | + // CATS (1002) triggers the thenDisplay — section is visible. |
| 123 | + request.addParameter(widgets.get("Allergy:"), "1002"); |
| 124 | + // Required Details field is filled. |
| 125 | + request.addParameter(widgets.get("Details:"), "Follow-up details here"); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void testResults(SubmissionResults results) { |
| 130 | + results.assertNoErrors(); |
| 131 | + results.assertEncounterCreated(); |
| 132 | + results.assertObsCreated(1000, Context.getConceptService().getConcept(1002)); |
| 133 | + results.assertObsCreated(60000, "Follow-up details here"); |
| 134 | + } |
| 135 | + }.run(); |
| 136 | + } |
| 137 | +} |
0 commit comments