Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function getSupportedValueOperators(): array
return [
ValueOperator::EQUALS,
ValueOperator::NOT_EQUALS,
ValueOperator::LESS_THAN,
ValueOperator::LESS_THAN_OR_EQUALS,
ValueOperator::GREATER_THAN,
ValueOperator::GREATER_THAN_OR_EQUALS,
];
}

Expand Down Expand Up @@ -85,8 +89,12 @@ public function applyValueOperator(
$b = strtolower(strval($b));

return match ($operator) {
ValueOperator::EQUALS => $a === $b,
ValueOperator::NOT_EQUALS => $a !== $b,
ValueOperator::EQUALS => $a === $b,
ValueOperator::NOT_EQUALS => $a !== $b,
ValueOperator::LESS_THAN => $a < $b,
ValueOperator::LESS_THAN_OR_EQUALS => $a <= $b,
ValueOperator::GREATER_THAN => $a > $b,
ValueOperator::GREATER_THAN_OR_EQUALS => $a >= $b,

// Unsupported operators
default => false,
Expand Down
52 changes: 50 additions & 2 deletions tests/cypress/e2e/form/editor/conditions.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,31 @@ describe ('Conditions', () => {
operator: 'Do not match regular expression',
value: '^Option [1-4]$',
valueType: 'string'
}
},
{
logic: 'Or',
operator: 'Is greater than',
value: 'Option 1',
valueType: 'dropdown'
},
{
logic: 'Or',
operator: 'Is less than',
value: 'Option 2',
valueType: 'dropdown'
},
{
logic: 'Or',
operator: 'Is greater than or equals to',
value: 'Option 3',
valueType: 'dropdown'
},
{
logic: 'Or',
operator: 'Is less than or equals to',
value: 'Option 4',
valueType: 'dropdown'
},
]
}
],
Expand Down Expand Up @@ -1873,7 +1897,31 @@ describe ('Conditions', () => {
operator: 'Do not match regular expression',
value: '^Option [1-4]$',
valueType: 'string'
}
},
{
logic: 'Or',
operator: 'Is greater than',
value: 'Option 1',
valueType: 'dropdown'
},
{
logic: 'Or',
operator: 'Is less than',
value: 'Option 2',
valueType: 'dropdown'
},
{
logic: 'Or',
operator: 'Is greater than or equals to',
value: 'Option 3',
valueType: 'dropdown'
},
{
logic: 'Or',
operator: 'Is less than or equals to',
value: 'Option 4',
valueType: 'dropdown'
},
]
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public static function conditionHandlerProvider(): iterable
"option_b" => "option B",
"option_c" => "option C",
"option_d" => "option D",
"1" => 1,
"2" => 2,
"3" => 3,
];

yield from self::getCasesForTypeAndConfig(
Expand Down Expand Up @@ -114,5 +117,109 @@ private static function getCasesForTypeAndConfig(
'expected_result' => false,
'question_extra_data' => $extra_data,
];

// Test with the LESS_THAN operator
yield "Less than check - case 1 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::LESS_THAN,
'condition_value' => "2",
'submitted_answer' => "1",
'expected_result' => true,
'question_extra_data' => $extra_data,
];
yield "Less than check - case 2 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::LESS_THAN,
'condition_value' => "2",
'submitted_answer' => "2",
'expected_result' => false,
'question_extra_data' => $extra_data,
];
yield "Less than check - case 3 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::LESS_THAN,
'condition_value' => "2",
'submitted_answer' => "3",
'expected_result' => false,
'question_extra_data' => $extra_data,
];

// Test with the LESS_THAN_OR_EQUALS operator
yield "Less than or equals check - case 1 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::LESS_THAN_OR_EQUALS,
'condition_value' => "2",
'submitted_answer' => "1",
'expected_result' => true,
'question_extra_data' => $extra_data,
];
yield "Less than or equals check - case 2 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::LESS_THAN_OR_EQUALS,
'condition_value' => "2",
'submitted_answer' => "2",
'expected_result' => true,
'question_extra_data' => $extra_data,
];
yield "Less than or equals check - case 3 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::LESS_THAN_OR_EQUALS,
'condition_value' => "2",
'submitted_answer' => "3",
'expected_result' => false,
'question_extra_data' => $extra_data,
];

// Test with the GREATER_THAN operator
yield "Greater than check - case 1 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::GREATER_THAN,
'condition_value' => "2",
'submitted_answer' => "1",
'expected_result' => false,
'question_extra_data' => $extra_data,
];
yield "Greater than check - case 2 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::GREATER_THAN,
'condition_value' => "2",
'submitted_answer' => "2",
'expected_result' => false,
'question_extra_data' => $extra_data,
];
yield "Greater than check - case 3 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::GREATER_THAN,
'condition_value' => "2",
'submitted_answer' => "3",
'expected_result' => true,
'question_extra_data' => $extra_data,
];

// Test with the GREATER_THAN_OR_EQUALS operator
yield "Greater than or equals check - case 1 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::GREATER_THAN_OR_EQUALS,
'condition_value' => "2",
'submitted_answer' => "1",
'expected_result' => false,
'question_extra_data' => $extra_data,
];
yield "Greater than or equals check - case 2 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::GREATER_THAN_OR_EQUALS,
'condition_value' => "2",
'submitted_answer' => "2",
'expected_result' => true,
'question_extra_data' => $extra_data,
];
yield "Greater than or equals check - case 3 for $type" => [
'question_type' => $type,
'condition_operator' => ValueOperator::GREATER_THAN_OR_EQUALS,
'condition_value' => "2",
'submitted_answer' => "3",
'expected_result' => true,
'question_extra_data' => $extra_data,
];
}
}
27 changes: 12 additions & 15 deletions tests/functional/Glpi/Form/Migration/FormMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1558,19 +1558,7 @@ public static function provideFormMigrationVisibilityConditionsForQuestions(): i
'expected_conditions' => [],
];

yield 'QuestionTypeDropdown - Visible if with a value operator not supported by the question type' => [
'field_type' => 'select',
'show_rule' => 2,
'conditions' => [
[
'show_condition' => 3, // Less than operator, not supported by item type
'show_value' => 'Test item',
'show_logic' => 1,
],
],
'expected_visibility_strategy' => VisibilityStrategy::ALWAYS_VISIBLE,
'expected_conditions' => [],
];
// There is no 'QuestionTypeDropdown - Visible if with a value operator not supported by the question type' case because all operators are supported

yield 'QuestionTypeShortText - Visible if value is not empty' => [
'field_type' => 'text',
Expand Down Expand Up @@ -1890,7 +1878,6 @@ public static function provideFormMigrationValidationConditionsForQuestions(): i
],
];

// QuestionTypeDropdown does not support any validation operators
yield 'QuestionTypeDropdown - Regex and range validation' => [
'select',
[
Expand All @@ -1906,6 +1893,16 @@ public static function provideFormMigrationValidationConditionsForQuestions(): i
],
ValidationStrategy::VALID_IF,
[
[
'value_operator' => ValueOperator::GREATER_THAN_OR_EQUALS,
'value' => 5,
'logic_operator' => LogicOperator::AND,
],
[
'value_operator' => ValueOperator::LESS_THAN_OR_EQUALS,
'value' => 50,
'logic_operator' => LogicOperator::AND,
],
[
'value_operator' => ValueOperator::MATCH_REGEX,
'value' => '/^[A-Za-z0-9]+$/',
Expand Down Expand Up @@ -3089,7 +3086,7 @@ public function testFormMigrationQuestionWithUnsupportedValueOperator(): void
// Insert target question
$DB->insert('glpi_plugin_formcreator_questions', [
'name' => 'Target question with unsupported value operator',
'fieldtype' => 'select',
'fieldtype' => 'checkboxes',
'plugin_formcreator_sections_id' => $sectionId,
]);
$targetQuestionId = $DB->insertId();
Expand Down