Skip to content

Commit

Permalink
fix: Add programs param if no program requested [DHIS2-17410][2.39] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
muilpp authored May 15, 2024
1 parent 2dc8765 commit 7f8f88e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public List<TrackedEntityInstance> getTrackedEntityInstances(
params.addFiltersIfNotExist(QueryItem.getQueryItems(attributes));
}

if (params.getProgram() == null) {
if (!params.hasProgram()) {
params.setPrograms(getTrackerPrograms(params.getUser()));
}

Expand Down Expand Up @@ -231,7 +231,7 @@ public List<Long> getTrackedEntityInstanceIds(

handleSortAttributes(params);

if (params.getProgram() == null) {
if (!params.hasProgram()) {
params.setPrograms(getTrackerPrograms(params.getUser()));
}

Expand Down Expand Up @@ -291,6 +291,11 @@ public int getTrackedEntityInstanceCount(
TrackedEntityInstanceQueryParams params,
boolean skipAccessValidation,
boolean skipSearchScopeValidation) {

if (!params.hasProgram()) {
params.setPrograms(getTrackerPrograms(params.getUser()));
}

decideAccess(params);

if (!skipAccessValidation) {
Expand All @@ -315,6 +320,10 @@ public int getTrackedEntityInstanceCount(
@Override
@Transactional(readOnly = true)
public Grid getTrackedEntityInstancesGrid(TrackedEntityInstanceQueryParams params) {
if (!params.hasProgram()) {
params.setPrograms(getTrackerPrograms(params.getUser()));
}

decideAccess(params);
validate(params);
validateSearchScope(params, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,35 +295,17 @@ void testStoredByColumnForTrackedEntityInstance() {

@Test
void testTrackedEntityAttributeFilter() {
injectSecurityContext(superUser);
filtH.setDisplayInListNoProgram(true);
attributeService.addTrackedEntityAttribute(filtH);

User user =
createAndAddUser(
false,
"attributeFilterUser",
Sets.newHashSet(organisationUnit),
Sets.newHashSet(organisationUnit));
injectSecurityContext(user);

entityInstanceA1.setTrackedEntityType(trackedEntityType);
entityInstanceService.addTrackedEntityInstance(entityInstanceA1);

TrackedEntityAttributeValue trackedEntityAttributeValue = new TrackedEntityAttributeValue();

trackedEntityAttributeValue.setAttribute(filtH);
trackedEntityAttributeValue.setEntityInstance(entityInstanceA1);
trackedEntityAttributeValue.setValue(ATTRIBUTE_VALUE);
TrackedEntityInstanceQueryParams params = getTrackedEntityInstanceParams();
params.setPrograms(List.of(program));

attributeValueService.addTrackedEntityAttributeValue(trackedEntityAttributeValue);
Grid grid = entityInstanceService.getTrackedEntityInstancesGrid(params);

TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
params.setOrganisationUnits(Sets.newHashSet(organisationUnit));
params.setTrackedEntityType(trackedEntityType);
assertEquals(1, grid.getHeight());
}

params.setQuery(new QueryFilter(QueryOperator.LIKE, ATTRIBUTE_VALUE));
params.setPrograms(List.of(program));
@Test
void testTrackedEntityAttributeFilterWhenProgramNotProvided() {
TrackedEntityInstanceQueryParams params = getTrackedEntityInstanceParams();

Grid grid = entityInstanceService.getTrackedEntityInstancesGrid(params);

Expand Down Expand Up @@ -862,6 +844,17 @@ void shouldCountOneEntityWhenOnePresent() {
assertEquals(1, counter);
}

@Test
void shouldCountOneEntityWhenOnePresentAndNoProgramProvided() {
entityInstanceA1.setTrackedEntityType(trackedEntityType);
entityInstanceService.addTrackedEntityInstance(entityInstanceA1);
TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();

int counter = entityInstanceService.getTrackedEntityInstanceCount(params, true, true);

assertEquals(1, counter);
}

@Test
void shouldCountZeroEntitiesWhenNonePresent() {
TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
Expand Down Expand Up @@ -988,6 +981,39 @@ void shouldFailWhenModeAllAndRegularUser() {
exception.getMessage());
}

private TrackedEntityInstanceQueryParams getTrackedEntityInstanceParams() {
injectSecurityContext(superUser);
filtH.setDisplayInListNoProgram(true);
attributeService.addTrackedEntityAttribute(filtH);

User user =
createAndAddUser(
false,
"attributeFilterUser",
Sets.newHashSet(organisationUnit),
Sets.newHashSet(organisationUnit));
injectSecurityContext(user);

entityInstanceA1.setTrackedEntityType(trackedEntityType);
entityInstanceService.addTrackedEntityInstance(entityInstanceA1);

TrackedEntityAttributeValue trackedEntityAttributeValue = new TrackedEntityAttributeValue();

trackedEntityAttributeValue.setAttribute(filtH);
trackedEntityAttributeValue.setEntityInstance(entityInstanceA1);
trackedEntityAttributeValue.setValue(ATTRIBUTE_VALUE);

attributeValueService.addTrackedEntityAttributeValue(trackedEntityAttributeValue);

TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
params.setOrganisationUnits(Sets.newHashSet(organisationUnit));
params.setTrackedEntityType(trackedEntityType);

params.setQuery(new QueryFilter(QueryOperator.LIKE, ATTRIBUTE_VALUE));

return params;
}

private void initializeEntityInstance(TrackedEntityInstance entityInstance) {
entityInstance.setTrackedEntityType(trackedEntityType);
entityInstanceService.addTrackedEntityInstance(entityInstance);
Expand Down

0 comments on commit 7f8f88e

Please sign in to comment.