|
12 | 12 | import java.util.Collection; |
13 | 13 | import java.util.List; |
14 | 14 | import java.util.Map; |
| 15 | +import java.util.Vector; |
15 | 16 |
|
16 | 17 | import org.junit.Assert; |
17 | 18 | import org.junit.Ignore; |
@@ -134,4 +135,117 @@ public void findPatientsByIdentifier_shouldGetResultsForPatientsForGivenIdentifi |
134 | 135 | Assert.assertEquals(2, resultObjects.size()); |
135 | 136 | } |
136 | 137 |
|
| 138 | + /** |
| 139 | + * LUI-208: Tests the findDuplicatePatients DWR method that is called from |
| 140 | + * findDuplicatePatients.jsp. On legacyUI >=2.x this triggers a JavaScript error |
| 141 | + * "Node.appendChild: The new child is an ancestor of the parent" because the newer |
| 142 | + * DWR util.js from org.openmrs.contrib handles addRows differently than the old |
| 143 | + * org.openmrs.directwebremoting version. |
| 144 | + * |
| 145 | + * @see DWRPatientService#findDuplicatePatients(String[]) |
| 146 | + */ |
| 147 | + @Test |
| 148 | + public void findDuplicatePatients_shouldReturnPatientListItemsForDuplicateGivenNames() throws Exception { |
| 149 | + executeDataSet("org/openmrs/web/dwr/include/DWRPatientService-duplicatePatients.xml"); |
| 150 | + DWRPatientService dwrService = new DWRPatientService(); |
| 151 | + Vector<Object> results = dwrService.findDuplicatePatients(new String[] { "givenName" }); |
| 152 | + |
| 153 | + // Should find the two patients with the same given name "DuplicateFirst" |
| 154 | + Assert.assertFalse("Should find duplicate patients by givenName", results.isEmpty()); |
| 155 | + |
| 156 | + // All results should be PatientListItem instances (not error strings) |
| 157 | + for (Object result : results) { |
| 158 | + Assert.assertTrue("Each result should be a PatientListItem, not a String. " |
| 159 | + + "The JS cellCreator in OpenmrsSearch.js returns options.data directly when it is " |
| 160 | + + "a TD element, and the newer DWR util.js then fails with appendChild error.", |
| 161 | + result instanceof PatientListItem); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * LUI-208: Tests that findDuplicatePatients returns results with all the fields needed |
| 167 | + * by the JavaScript getCellFunctions() in PatientSearch.js. These functions (getNumber, |
| 168 | + * getId, getGiven, getMiddle, getFamily, getAge, getGender, getBirthday) create TD |
| 169 | + * elements from PatientListItem properties. The newer DWR util.js fails when cellCreator |
| 170 | + * returns these TD elements directly (instead of creating new ones). |
| 171 | + * |
| 172 | + * @see DWRPatientService#findDuplicatePatients(String[]) |
| 173 | + */ |
| 174 | + @Test |
| 175 | + public void findDuplicatePatients_shouldReturnResultsWithFieldsRequiredByJsRendering() throws Exception { |
| 176 | + executeDataSet("org/openmrs/web/dwr/include/DWRPatientService-duplicatePatients.xml"); |
| 177 | + DWRPatientService dwrService = new DWRPatientService(); |
| 178 | + Vector<Object> results = dwrService.findDuplicatePatients(new String[] { "givenName" }); |
| 179 | + |
| 180 | + Assert.assertFalse("Should return results for duplicate patients", results.isEmpty()); |
| 181 | + |
| 182 | + // Verify each result has the fields the JS rendering code needs. |
| 183 | + // The JS functions getId(), getGiven(), getFamily(), getGender(), getAge() |
| 184 | + // create TD elements and return them. When the newer DWR util.js processes |
| 185 | + // these via cellCreator -> addRows, it tries to appendChild(data) to the cell |
| 186 | + // that IS the data, causing "The new child is an ancestor of the parent". |
| 187 | + for (Object result : results) { |
| 188 | + PatientListItem item = (PatientListItem) result; |
| 189 | + Assert.assertNotNull("patientId is needed by getId() in PatientSearch.js", item.getPatientId()); |
| 190 | + Assert.assertNotNull("identifier is needed by getId() which creates a TD with an anchor", |
| 191 | + item.getIdentifier()); |
| 192 | + Assert.assertNotNull("givenName is needed by getGiven() in PersonSearch.js", item.getGivenName()); |
| 193 | + Assert.assertNotNull("familyName is needed by getFamily() in PersonSearch.js", item.getFamilyName()); |
| 194 | + Assert.assertNotNull("gender is needed by getGender() which creates a TD with an img element", |
| 195 | + item.getGender()); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * LUI-208: Tests findDuplicatePatients with multiple search attributes. |
| 201 | + * This simulates the findDuplicatePatients.jsp flow where a user selects |
| 202 | + * multiple checkboxes (e.g., givenName + gender) before clicking Search. |
| 203 | + * |
| 204 | + * @see DWRPatientService#findDuplicatePatients(String[]) |
| 205 | + */ |
| 206 | + @Test |
| 207 | + public void findDuplicatePatients_shouldReturnResultsWhenSearchingOnMultipleAttributes() throws Exception { |
| 208 | + executeDataSet("org/openmrs/web/dwr/include/DWRPatientService-duplicatePatients.xml"); |
| 209 | + DWRPatientService dwrService = new DWRPatientService(); |
| 210 | + |
| 211 | + // Search on givenName + gender (both test patients have givenName="DuplicateFirst", gender="F") |
| 212 | + Vector<Object> results = dwrService.findDuplicatePatients(new String[] { "givenName", "gender" }); |
| 213 | + |
| 214 | + Assert.assertFalse("Should find duplicates when searching on multiple attributes", results.isEmpty()); |
| 215 | + for (Object result : results) { |
| 216 | + Assert.assertTrue("Result should be PatientListItem not error string", |
| 217 | + result instanceof PatientListItem); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * LUI-208: Tests findPatientsByIdentifier returns PatientListItem objects with all |
| 223 | + * properties needed by the JavaScript rendering pipeline. This method is called from |
| 224 | + * findDuplicatePatients.jsp's "search by identifiers" feature, and the results are |
| 225 | + * rendered using the same dwr.util.addRows code that triggers the appendChild bug. |
| 226 | + * |
| 227 | + * @see DWRPatientService#findPatientsByIdentifier(String[]) |
| 228 | + */ |
| 229 | + @Test |
| 230 | + public void findPatientsByIdentifier_shouldReturnResultsWithFieldsRequiredByJsRendering() throws Exception { |
| 231 | + executeDataSet("org/openmrs/web/dwr/include/DWRPatientService-findByIdentifier.xml"); |
| 232 | + DWRPatientService dwrService = new DWRPatientService(); |
| 233 | + Collection<Object> results = dwrService |
| 234 | + .findPatientsByIdentifier(new String[] { "Identifier1", "Identifier2" }); |
| 235 | + |
| 236 | + Assert.assertEquals(2, results.size()); |
| 237 | + |
| 238 | + // Verify results have all fields needed by the JS getCellFunctions pipeline |
| 239 | + // that triggers the DWR util.js appendChild bug |
| 240 | + for (Object result : results) { |
| 241 | + Assert.assertTrue("Each result must be PatientListItem", result instanceof PatientListItem); |
| 242 | + PatientListItem item = (PatientListItem) result; |
| 243 | + Assert.assertNotNull("patientId required by JS getNumber/getId", item.getPatientId()); |
| 244 | + Assert.assertNotNull("identifier required by JS getId() TD creation", item.getIdentifier()); |
| 245 | + Assert.assertFalse("identifier should not be empty", item.getIdentifier().isEmpty()); |
| 246 | + Assert.assertNotNull("givenName required by JS getGiven() cell rendering", item.getGivenName()); |
| 247 | + Assert.assertNotNull("familyName required by JS getFamily() cell rendering", item.getFamilyName()); |
| 248 | + } |
| 249 | + } |
| 250 | + |
137 | 251 | } |
0 commit comments