Skip to content

Beautiful Open-Source Form Builder

Add tests for matrix condition checker with missing keys

Cursor Agentd64a484e46ba
diff --git a/api/tests/Feature/Forms/MatrixInputTest.php b/api/tests/Feature/Forms/MatrixInputTest.php
index 035277ab5..8c08ea9bd 100644
--- a/api/tests/Feature/Forms/MatrixInputTest.php
+++ b/api/tests/Feature/Forms/MatrixInputTest.php
@@ -183,3 +183,134 @@ it('can validate matrix input with precognition', function () {
]
]);
});
+
+it('can submit form with matrix logic condition when matrix has partial data', function () {
+ // Reproduces issue #1026: Matrix logic condition throws "Undefined array key"
+ // when checking conditions against partial matrix data
+ $user = $this->actingAsUser();
+ $workspace = $this->createUserWorkspace($user);
+ $form = $this->createForm($user, $workspace);
+
+ // Create a matrix property with German row names (as in the original bug report)
+ $matrixProperty = [
+ 'id' => 'matrix_field',
+ 'name' => 'Experience Matrix',
+ 'type' => 'matrix',
+ 'rows' => ['0-5 Jahre', '5-10 Jahre', '10-15 Jahre', '15+ Jahre'],
+ 'columns' => ['Keine', 'Wenig', 'Mittel', 'Viel'],
+ 'required' => true
+ ];
+
+ // Create a text field that is conditionally shown based on matrix value
+ $conditionalTextField = [
+ 'id' => 'conditional_text',
+ 'name' => 'Additional Details',
+ 'type' => 'text',
+ 'required' => false,
+ 'logic' => [
+ 'conditions' => [
+ 'operatorIdentifier' => 'and',
+ 'children' => [
+ [
+ 'value' => [
+ 'property_meta' => [
+ 'id' => 'matrix_field',
+ 'type' => 'matrix'
+ ],
+ 'operator' => 'equals',
+ 'value' => ['15+ Jahre' => 'Viel']
+ ]
+ ]
+ ]
+ ],
+ 'actions' => ['require-answer']
+ ]
+ ];
+
+ $form->properties = array_merge($form->properties, [$matrixProperty, $conditionalTextField]);
+ $form->update();
+
+ // Submit with only some rows filled (not including '15+ Jahre')
+ // This should NOT throw "Undefined array key" error
+ $submissionData = [
+ 'matrix_field' => [
+ '0-5 Jahre' => 'Wenig',
+ '5-10 Jahre' => 'Mittel',
+ '10-15 Jahre' => 'Viel',
+ '15+ Jahre' => 'Keine'
+ ],
+ 'conditional_text' => ''
+ ];
+
+ $formData = $this->generateFormSubmissionData($form, $submissionData);
+
+ $this->postJson(route('forms.answer', $form->slug), $formData)
+ ->assertSuccessful()
+ ->assertJson([
+ 'type' => 'success',
+ 'message' => 'Form submission saved.',
+ ]);
+});
+
+it('can submit form when matrix logic condition has missing rows in submission', function () {
+ // Additional test for issue #1026: Ensure no error when matrix field value
+ // doesn't have all rows that the condition checks for
+ $user = $this->actingAsUser();
+ $workspace = $this->createUserWorkspace($user);
+ $form = $this->createForm($user, $workspace);
+
+ $matrixProperty = [
+ 'id' => 'matrix_field',
+ 'name' => 'Experience Matrix',
+ 'type' => 'matrix',
+ 'rows' => ['Row A', 'Row B', 'Row C'],
+ 'columns' => ['Option 1', 'Option 2', 'Option 3'],
+ 'required' => false // Make it optional so we can submit partial data
+ ];
+
+ // Conditional field that checks for a specific matrix row/column combination
+ $conditionalTextField = [
+ 'id' => 'conditional_text',
+ 'name' => 'Conditional Field',
+ 'type' => 'text',
+ 'required' => false,
+ 'logic' => [
+ 'conditions' => [
+ 'operatorIdentifier' => 'and',
+ 'children' => [
+ [
+ 'value' => [
+ 'property_meta' => [
+ 'id' => 'matrix_field',
+ 'type' => 'matrix'
+ ],
+ 'operator' => 'equals',
+ 'value' => ['Row C' => 'Option 3'] // Check for Row C
+ ]
+ ]
+ ]
+ ],
+ 'actions' => ['require-answer']
+ ]
+ ];
+
+ $form->properties = array_merge($form->properties, [$matrixProperty, $conditionalTextField]);
+ $form->update();
+
+ // Submit with empty matrix (no rows filled)
+ // The condition checks for 'Row C' but our submission doesn't have it
+ // This should NOT throw "Undefined array key" error
+ $submissionData = [
+ 'matrix_field' => [],
+ 'conditional_text' => ''
+ ];
+
+ $formData = $this->generateFormSubmissionData($form, $submissionData);
+
+ $this->postJson(route('forms.answer', $form->slug), $formData)
+ ->assertSuccessful()
+ ->assertJson([
+ 'type' => 'success',
+ 'message' => 'Form submission saved.',
+ ]);
+});
diff --git a/api/tests/Unit/Service/Forms/FormLogicConditionCheckerTest.php b/api/tests/Unit/Service/Forms/FormLogicConditionCheckerTest.php
index cd6fef3c5..ea706314f 100644
--- a/api/tests/Unit/Service/Forms/FormLogicConditionCheckerTest.php
+++ b/api/tests/Unit/Service/Forms/FormLogicConditionCheckerTest.php
@@ -432,6 +432,80 @@ describe('FormLogicConditionChecker', function () {
$condition['value']['operator'] = 'contains';
expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeTrue();
});
+
+ it('handles missing rows in field value for equals operator', function () {
+ // Reproduces issue #1026: when a matrix condition checks for a specific row
+ // but the user submission doesn't have that row yet, it should return false
+ // instead of throwing "Undefined array key" error
+ $condition = [
+ 'value' => [
+ 'property_meta' => [
+ 'id' => 'matrix_field',
+ 'type' => 'matrix'
+ ],
+ 'operator' => 'equals',
+ 'value' => ['15+ Jahre' => 'Option A', 'row2' => 'col2']
+ ]
+ ];
+
+ // User has only filled in one row, condition expects two rows
+ $formData = ['matrix_field' => ['row2' => 'col2']];
+ expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeFalse();
+
+ // User has no data for matrix field
+ $formData = ['matrix_field' => []];
+ expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeFalse();
+
+ // User has null value for matrix field
+ $formData = ['matrix_field' => null];
+ expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeFalse();
+ });
+
+ it('handles missing rows in field value for does_not_equal operator', function () {
+ $condition = [
+ 'value' => [
+ 'property_meta' => [
+ 'id' => 'matrix_field',
+ 'type' => 'matrix'
+ ],
+ 'operator' => 'does_not_equal',
+ 'value' => ['15+ Jahre' => 'Option A', 'row2' => 'col2']
+ ]
+ ];
+
+ // User has only filled in one row - they "do not equal" the condition
+ $formData = ['matrix_field' => ['row2' => 'col2']];
+ expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeTrue();
+
+ // User has no data for matrix field
+ $formData = ['matrix_field' => []];
+ expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeTrue();
+ });
+
+ it('handles missing rows in field value for contains operator', function () {
+ $condition = [
+ 'value' => [
+ 'property_meta' => [
+ 'id' => 'matrix_field',
+ 'type' => 'matrix'
+ ],
+ 'operator' => 'contains',
+ 'value' => ['15+ Jahre' => 'Option A', 'row2' => 'col2']
+ ]
+ ];
+
+ // User has one matching row - contains should return true
+ $formData = ['matrix_field' => ['row2' => 'col2']];
+ expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeTrue();
+
+ // User has no matching rows
+ $formData = ['matrix_field' => ['row2' => 'col1']];
+ expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeFalse();
+
+ // User has empty matrix
+ $formData = ['matrix_field' => []];
+ expect(FormLogicConditionChecker::conditionsMet($condition, $formData))->toBeFalse();
+ });
});
describe('group conditions', function () {