diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMapBuilder.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMapBuilder.kt index e0be3d21..416bc109 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMapBuilder.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMapBuilder.kt @@ -148,6 +148,14 @@ internal class RuleVariableValueMapBuilder { listOf(eventDate), currentDate.toString(), ) + } else { + valueMap[RuleEngineUtils.ENV_VAR_EVENT_DATE] = + RuleVariableValue( + RuleValueType.TEXT, + null, + listOf(), + currentDate.toString(), + ); } if (ruleEvent.dueDate != null) { val dueDate = ruleEvent.dueDate diff --git a/src/commonTest/kotlin/org/hisp/dhis/rules/RuleEngineFunctionTest.kt b/src/commonTest/kotlin/org/hisp/dhis/rules/RuleEngineFunctionTest.kt index bac378f3..cc3f3908 100644 --- a/src/commonTest/kotlin/org/hisp/dhis/rules/RuleEngineFunctionTest.kt +++ b/src/commonTest/kotlin/org/hisp/dhis/rules/RuleEngineFunctionTest.kt @@ -166,6 +166,72 @@ class RuleEngineFunctionTest { assertEquals(ruleAction, ruleEffects[0].ruleAction) } + @Test + fun evaluateHasValueFunctionMustReturnFalseIfValueNotSpecified() { + val ruleAction = + RuleAction( + "d2:hasValue(V{event_date})", + "DISPLAYTEXT", + mapOf(Pair("content", "test_action_content"), Pair("location", "feedback")), + ) + val rule = Rule("d2:hasValue(V{event_date})", listOf(ruleAction), "", "") + val ruleEngineContext = RuleEngineTestUtils.getRuleEngineContext(rule, listOf()) + val ruleEvent = + RuleEvent( + "test_event", + "test_program_stage", + "", + RuleEventStatus.ACTIVE, + Instant.DISTANT_FUTURE, + Clock.System.now(), + LocalDate.currentDate(), + null, + "", + null, + listOf( + RuleDataValue( + "test_data_element_one", + "test_value", + ), + ), + ) + val ruleEffects = RuleEngine.getInstance().evaluate(ruleEvent, null, emptyList(), ruleEngineContext) + assertEquals(0, ruleEffects.size) + } + + @Test + fun evaluateNotHasValueFunctionMustReturnTrueIfValueNotSpecified() { + val ruleAction = + RuleAction( + "true", + "DISPLAYTEXT", + mapOf(Pair("content", "test_action_content"), Pair("location", "feedback")), + ) + val rule = Rule("!d2:hasValue(V{event_date})", listOf(ruleAction), "", "") + val ruleEngineContext = RuleEngineTestUtils.getRuleEngineContext(rule, listOf()) + val ruleEvent = + RuleEvent( + "test_event", + "test_program_stage", + "", + RuleEventStatus.ACTIVE, + Instant.DISTANT_FUTURE, + Clock.System.now(), + LocalDate.currentDate(), + null, + "", + null, + listOf( + RuleDataValue( + "test_data_element_one", + "test_value", + ), + ), + ) + val ruleEffects = RuleEngine.getInstance().evaluate(ruleEvent, null, emptyList(), ruleEngineContext) + assertEquals(1, ruleEffects.size) + } + @Test fun optionSetNameShouldBeUsed() { val option1 = Option("name1", "code1")