|
42 | 42 | use Glpi\Form\Condition\LogicOperator;
|
43 | 43 | use Glpi\Form\Condition\Type;
|
44 | 44 | use Glpi\Form\Condition\ValueOperator;
|
| 45 | +use Glpi\Form\Condition\VisibilityStrategy; |
45 | 46 | use Glpi\Form\Destination\FormDestinationChange;
|
46 | 47 | use Glpi\Form\Destination\FormDestinationProblem;
|
47 | 48 | use Glpi\Form\Destination\FormDestinationTicket;
|
@@ -164,6 +165,117 @@ public function testSaveAnswers(): void
|
164 | 165 | );
|
165 | 166 | }
|
166 | 167 |
|
| 168 | + public function testValidateAnswers(): void |
| 169 | + { |
| 170 | + self::login(); |
| 171 | + |
| 172 | + // Create a form with both mandatory and optional questions |
| 173 | + $builder = new FormBuilder("Validation Test Form"); |
| 174 | + $builder |
| 175 | + ->addQuestion("Mandatory Name", QuestionTypeShortText::class, is_mandatory: true) |
| 176 | + ->addQuestion("Mandatory Email", QuestionTypeEmail::class, is_mandatory: true) |
| 177 | + ->addQuestion("Optional Comment", QuestionTypeLongText::class, is_mandatory: false) |
| 178 | + ; |
| 179 | + $form = self::createForm($builder); |
| 180 | + |
| 181 | + // Get handler instance |
| 182 | + $handler = AnswersHandler::getInstance(); |
| 183 | + |
| 184 | + // Test 1: All mandatory fields are filled - should be valid |
| 185 | + $complete_answers = [ |
| 186 | + self::getQuestionId($form, "Mandatory Name") => "John Doe", |
| 187 | + self::getQuestionId($form, "Mandatory Email") => "john.doe@example.com", |
| 188 | + self::getQuestionId($form, "Optional Comment") => "This is an optional comment", |
| 189 | + ]; |
| 190 | + $result = $handler->validateAnswers($form, $complete_answers); |
| 191 | + $this->assertTrue($result->isValid(), "Validation should pass when all mandatory fields are filled"); |
| 192 | + $this->assertCount(0, $result->getErrors(), "There should be no errors when all mandatory fields are filled"); |
| 193 | + |
| 194 | + // Test 2: Missing one mandatory field - should be invalid |
| 195 | + $missing_name_answers = [ |
| 196 | + self::getQuestionId($form, "Mandatory Email") => "john.doe@example.com", |
| 197 | + self::getQuestionId($form, "Optional Comment") => "This is an optional comment", |
| 198 | + ]; |
| 199 | + $result = $handler->validateAnswers($form, $missing_name_answers); |
| 200 | + $this->assertFalse($result->isValid(), "Validation should fail when a mandatory field is missing"); |
| 201 | + $this->assertCount(1, $result->getErrors(), "There should be one error when one mandatory field is missing"); |
| 202 | + |
| 203 | + // Test 3: Missing all mandatory fields - should be invalid with multiple errors |
| 204 | + $only_optional_answers = [ |
| 205 | + self::getQuestionId($form, "Optional Comment") => "This is an optional comment", |
| 206 | + ]; |
| 207 | + $result = $handler->validateAnswers($form, $only_optional_answers); |
| 208 | + $this->assertFalse($result->isValid(), "Validation should fail when all mandatory fields are missing"); |
| 209 | + $this->assertCount(2, $result->getErrors(), "There should be two errors when both mandatory fields are missing"); |
| 210 | + |
| 211 | + // Test 4: Empty answers - should be invalid with multiple errors |
| 212 | + $empty_answers = []; |
| 213 | + $result = $handler->validateAnswers($form, $empty_answers); |
| 214 | + $this->assertFalse($result->isValid(), "Validation should fail when answers are empty"); |
| 215 | + $this->assertCount(2, $result->getErrors(), "There should be two errors when answers are empty"); |
| 216 | + |
| 217 | + // Test 5: Empty string in mandatory field - should be invalid |
| 218 | + $empty_string_answers = [ |
| 219 | + self::getQuestionId($form, "Mandatory Name") => "", |
| 220 | + self::getQuestionId($form, "Mandatory Email") => "john.doe@example.com", |
| 221 | + ]; |
| 222 | + $result = $handler->validateAnswers($form, $empty_string_answers); |
| 223 | + $this->assertFalse($result->isValid(), "Validation should fail when a mandatory field contains an empty string"); |
| 224 | + $this->assertCount(1, $result->getErrors(), "There should be one error when a mandatory field contains an empty string"); |
| 225 | + } |
| 226 | + |
| 227 | + public function testValidateAnswersWithConditions(): void |
| 228 | + { |
| 229 | + self::login(); |
| 230 | + |
| 231 | + // Create a form with conditional questions |
| 232 | + $builder = new FormBuilder("Conditional Validation Test Form"); |
| 233 | + $builder |
| 234 | + ->addQuestion("Main Question", QuestionTypeShortText::class, is_mandatory: true) |
| 235 | + ->addQuestion("Conditional Question", QuestionTypeShortText::class, is_mandatory: true) |
| 236 | + ->setQuestionVisibility( |
| 237 | + "Conditional Question", |
| 238 | + VisibilityStrategy::VISIBLE_IF, |
| 239 | + [ |
| 240 | + [ |
| 241 | + 'logic_operator' => LogicOperator::AND, |
| 242 | + 'item_name' => "Main Question", |
| 243 | + 'item_type' => Type::QUESTION, |
| 244 | + 'value_operator' => ValueOperator::EQUALS, |
| 245 | + 'value' => "Show Conditional", |
| 246 | + ], |
| 247 | + ] |
| 248 | + ) |
| 249 | + ; |
| 250 | + $form = self::createForm($builder); |
| 251 | + |
| 252 | + // Get handler instance |
| 253 | + $handler = AnswersHandler::getInstance(); |
| 254 | + |
| 255 | + // Test 1: Conditional question is shown - should be valid |
| 256 | + $conditional_answers = [ |
| 257 | + self::getQuestionId($form, "Main Question") => "Show Conditional", |
| 258 | + self::getQuestionId($form, "Conditional Question") => "This is a conditional answer", |
| 259 | + ]; |
| 260 | + $result = $handler->validateAnswers($form, $conditional_answers); |
| 261 | + $this->assertTrue($result->isValid(), "Validation should pass when the conditional question is shown and filled"); |
| 262 | + |
| 263 | + // Test 2: Conditional question is not shown - should be valid |
| 264 | + $non_conditional_answers = [ |
| 265 | + self::getQuestionId($form, "Main Question") => "Do not show", |
| 266 | + ]; |
| 267 | + $result = $handler->validateAnswers($form, $non_conditional_answers); |
| 268 | + $this->assertTrue($result->isValid(), "Validation should pass when the conditional question is not shown"); |
| 269 | + |
| 270 | + // Test 3: Conditional question is shown but not filled - should be invalid |
| 271 | + $missing_conditional_answers = [ |
| 272 | + self::getQuestionId($form, "Main Question") => "Show Conditional", |
| 273 | + self::getQuestionId($form, "Conditional Question") => "", |
| 274 | + ]; |
| 275 | + $result = $handler->validateAnswers($form, $missing_conditional_answers); |
| 276 | + $this->assertFalse($result->isValid(), "Validation should fail when the conditional question is shown but not filled"); |
| 277 | + } |
| 278 | + |
167 | 279 | private function validateAnswers(
|
168 | 280 | Form $form,
|
169 | 281 | array $answers,
|
|
0 commit comments