Skip to content

Commit b4fc264

Browse files
committed
test: add tests for invalid value warnings
1 parent c8a218f commit b4fc264

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

tests/attempt_ui/question_ui_renderer_test.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,55 @@ public function test_should_add_fallback_option_to_select_when_value_isnt_presen
526526
EXPECTED, $result->html);
527527
}
528528

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+
529578
/**
530579
* Creates a stub question attempt which should fulfill the needs of most tests.
531580
*
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!--
2+
- This file is part of the QuestionPy Moodle plugin - https://questionpy.org
3+
-
4+
- Moodle is free software: you can redistribute it and/or modify
5+
- it under the terms of the GNU General Public License as published by
6+
- the Free Software Foundation, either version 3 of the License, or
7+
- (at your option) any later version.
8+
-
9+
- Moodle is distributed in the hope that it will be useful,
10+
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
- GNU General Public License for more details.
13+
-
14+
- You should have received a copy of the GNU General Public License
15+
- along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
-->
17+
18+
<!-- Please keep me in sync with input-values.xhtml. -->
19+
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:qpy="http://questionpy.org/ns/question" id="my_div">
20+
<input type="text" name="my_text" value="original"/>
21+
22+
<input type="checkbox" name="my_checkbox_value" value="value" qpy:warn-on-unknown-option="no"/>
23+
<input type="checkbox" name="my_checkbox_on" qpy:warn-on-unknown-option="no"/>
24+
25+
<input type="radio" name="my_radio" value="value1" qpy:warn-on-unknown-option="no"/>
26+
<input type="radio" name="my_radio" value="value2" checked="checked"/>
27+
28+
<select name="my_select" qpy:warn-on-unknown-option="no">
29+
<option value="value1"/>
30+
<option value="value2" selected="selected"/>
31+
<option value="value3"/>
32+
</select>
33+
34+
<input type="hidden" name="my_hidden" value="original"/>
35+
36+
<input name="my_button" type="button" value="value1"/>
37+
<input name="my_button" type="button" value="value2"/>
38+
</div>

0 commit comments

Comments
 (0)