@@ -241,6 +241,81 @@ public void shouldRestrictToVisitLocationAndChildrenIfRestrictToVisitLocationEna
241241 TestUtil .assertFuzzyContains ("Jamaica Plain" , htmlToDisplay );
242242 }
243243
244+ @ Test
245+ public void shouldRestrictToSessionVisitLocationAndChildrenIfRestrictToSessionVisitLocationEnabledAndUserLoginLocationIsVisitLocation ()
246+ throws Exception {
247+ executeVersionedDataSet ("org/openmrs/module/htmlformentry/data/encounterLocationTest.xml" );
248+
249+ // "outpatient" (id=1002) is itself tagged as a Visit Location
250+ Location loginLocation = Context .getLocationService ().getLocation (1002 );
251+ Context .getUserContext ().setLocation (loginLocation );
252+
253+ String htmlform = "<htmlform><encounterLocation restrictToSessionVisitLocation=\" true\" /></htmlform>" ;
254+ FormEntrySession session = new FormEntrySession (null , htmlform , null );
255+
256+ String htmlToDisplay = session .getHtmlToDisplay ();
257+
258+ // "outpatient" and its descendants should be included
259+ TestUtil .assertFuzzyContains ("out patient" , htmlToDisplay );
260+ TestUtil .assertFuzzyContains ("pharmacy" , htmlToDisplay );
261+ TestUtil .assertFuzzyContains ("dentist room" , htmlToDisplay );
262+ TestUtil .assertFuzzyContains ("nurse's room" , htmlToDisplay );
263+ TestUtil .assertFuzzyContains ("dispensary" , htmlToDisplay );
264+
265+ // ancestors and locations outside the subtree should not appear
266+ TestUtil .assertFuzzyDoesNotContain ("amani hospital" , htmlToDisplay );
267+ TestUtil .assertFuzzyDoesNotContain ("in patient" , htmlToDisplay );
268+ TestUtil .assertFuzzyDoesNotContain ("opd" , htmlToDisplay );
269+ TestUtil .assertFuzzyDoesNotContain ("casuality" , htmlToDisplay );
270+ TestUtil .assertFuzzyDoesNotContain ("intensive care" , htmlToDisplay );
271+ }
272+
273+ @ Test
274+ public void shouldRestrictToNearestVisitLocationAncestorIfRestrictToSessionVisitLocationEnabledAndUserLoginLocationIsNotVisitLocation ()
275+ throws Exception {
276+ executeVersionedDataSet ("org/openmrs/module/htmlformentry/data/encounterLocationTest.xml" );
277+
278+ // "intensive care" (id=1010) is NOT tagged as a Visit Location; its parent "in patient" (id=1003) is
279+ Location loginLocation = Context .getLocationService ().getLocation (1010 );
280+ Context .getUserContext ().setLocation (loginLocation );
281+
282+ String htmlform = "<htmlform><encounterLocation restrictToSessionVisitLocation=\" true\" /></htmlform>" ;
283+ FormEntrySession session = new FormEntrySession (null , htmlform , null );
284+
285+ String htmlToDisplay = session .getHtmlToDisplay ();
286+
287+ // the nearest visit-location ancestor ("in patient") and its descendants should be included
288+ TestUtil .assertFuzzyContains ("in patient" , htmlToDisplay );
289+ TestUtil .assertFuzzyContains ("casuality" , htmlToDisplay );
290+ TestUtil .assertFuzzyContains ("intensive care" , htmlToDisplay );
291+
292+ TestUtil .assertFuzzyDoesNotContain ("amani hospital" , htmlToDisplay );
293+ TestUtil .assertFuzzyDoesNotContain ("out patient" , htmlToDisplay );
294+ TestUtil .assertFuzzyDoesNotContain ("opd" , htmlToDisplay );
295+ TestUtil .assertFuzzyDoesNotContain ("pharmacy" , htmlToDisplay );
296+ TestUtil .assertFuzzyDoesNotContain ("dentist room" , htmlToDisplay );
297+ }
298+
299+ @ Test
300+ public void shouldNotRestrictLocationsIfRestrictToSessionVisitLocationEnabledButUserHasNoLoginLocation ()
301+ throws Exception {
302+ executeVersionedDataSet ("org/openmrs/module/htmlformentry/data/encounterLocationTest.xml" );
303+
304+ Context .getUserContext ().setLocation (null );
305+
306+ String htmlform = "<htmlform><encounterLocation restrictToSessionVisitLocation=\" true\" /></htmlform>" ;
307+ FormEntrySession session = new FormEntrySession (null , htmlform , null );
308+
309+ String htmlToDisplay = session .getHtmlToDisplay ();
310+
311+ // no restriction applied - all non-retired locations should be present
312+ TestUtil .assertFuzzyContains ("amani hospital" , htmlToDisplay );
313+ TestUtil .assertFuzzyContains ("out patient" , htmlToDisplay );
314+ TestUtil .assertFuzzyContains ("in patient" , htmlToDisplay );
315+ TestUtil .assertFuzzyContains ("opd" , htmlToDisplay );
316+ TestUtil .assertFuzzyContains ("intensive care" , htmlToDisplay );
317+ }
318+
244319 @ Test
245320 public void getAllVisitsAndChildLocations_shouldReturnAllVisitsAndTheirChildLocations () throws Exception {
246321 executeVersionedDataSet ("org/openmrs/module/htmlformentry/data/encounterLocationTest.xml" );
0 commit comments