@@ -526,6 +526,55 @@ public function test_should_add_fallback_option_to_select_when_value_isnt_presen
526
526
EXPECTED , $ result ->html );
527
527
}
528
528
529
+ /**
530
+ * Tests that render warnings are generated when the last response contains invalid values.
531
+ *
532
+ * @return void
533
+ * @throws coding_exception
534
+ * @covers \qtype_questionpy\attempt_ui\question_ui_renderer::extract_available_options
535
+ * @covers \qtype_questionpy\attempt_ui\question_ui_renderer::check_for_unknown_options
536
+ */
537
+ public function test_should_warn_about_invalid_values (): void {
538
+ $ input = file_get_contents (__DIR__ . '/question_uis/input-values.xhtml ' );
539
+ $ qa = $ this ->create_question_attempt_stub ('deadbeef ' , lastresponse: [
540
+ 'my_checkbox_value ' => 'other_value ' ,
541
+ 'my_checkbox_on ' => 'schmon ' ,
542
+ 'my_radio ' => 'value13 ' ,
543
+ 'my_select ' => 'value42 ' ,
544
+ ]);
545
+
546
+ $ ui = new question_ui_renderer ($ input , [], new \question_display_options (), $ qa );
547
+ $ result = $ ui ->render ();
548
+ $ this ->assertEqualsCanonicalizing ([
549
+ new invalid_option_warning ('my_checkbox_value ' , 'other_value ' , ['value ' ]),
550
+ new invalid_option_warning ('my_checkbox_on ' , 'schmon ' , ['on ' ]),
551
+ new invalid_option_warning ('my_radio ' , 'value13 ' , ['value1 ' , 'value2 ' ]),
552
+ new invalid_option_warning ('my_select ' , 'value42 ' , ['value1 ' , 'value2 ' , 'value3 ' ]),
553
+ ], $ result ->warnings );
554
+ }
555
+
556
+ /**
557
+ * Tests that render warnings are NOT generated when the input element has `qpy:warn-on-unknown-option="no"`.
558
+ *
559
+ * @return void
560
+ * @throws coding_exception
561
+ * @covers \qtype_questionpy\attempt_ui\question_ui_renderer::extract_available_options
562
+ * @covers \qtype_questionpy\attempt_ui\question_ui_renderer::check_for_unknown_options
563
+ */
564
+ public function test_should_not_warn_about_invalid_values_when_input_opts_out (): void {
565
+ $ input = file_get_contents (__DIR__ . '/question_uis/input-values-nowarn.xhtml ' );
566
+ $ qa = $ this ->create_question_attempt_stub ('deadbeef ' , lastresponse: [
567
+ 'my_checkbox_value ' => 'other_value ' ,
568
+ 'my_checkbox_on ' => 'schmon ' ,
569
+ 'my_radio ' => 'value13 ' ,
570
+ 'my_select ' => 'value42 ' ,
571
+ ]);
572
+
573
+ $ ui = new question_ui_renderer ($ input , [], new \question_display_options (), $ qa );
574
+ $ result = $ ui ->render ();
575
+ $ this ->assertEmpty ($ result ->warnings );
576
+ }
577
+
529
578
/**
530
579
* Creates a stub question attempt which should fulfill the needs of most tests.
531
580
*
0 commit comments