Skip to content

Commit

Permalink
test: add tests for invalid value warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Dec 18, 2024
1 parent c8a218f commit b4fc264
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/attempt_ui/question_ui_renderer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,55 @@ public function test_should_add_fallback_option_to_select_when_value_isnt_presen
EXPECTED, $result->html);
}

/**
* Tests that render warnings are generated when the last response contains invalid values.
*
* @return void
* @throws coding_exception
* @covers \qtype_questionpy\attempt_ui\question_ui_renderer::extract_available_options
* @covers \qtype_questionpy\attempt_ui\question_ui_renderer::check_for_unknown_options
*/
public function test_should_warn_about_invalid_values(): void {
$input = file_get_contents(__DIR__ . '/question_uis/input-values.xhtml');
$qa = $this->create_question_attempt_stub('deadbeef', lastresponse: [
'my_checkbox_value' => 'other_value',
'my_checkbox_on' => 'schmon',
'my_radio' => 'value13',
'my_select' => 'value42',
]);

$ui = new question_ui_renderer($input, [], new \question_display_options(), $qa);
$result = $ui->render();
$this->assertEqualsCanonicalizing([
new invalid_option_warning('my_checkbox_value', 'other_value', ['value']),
new invalid_option_warning('my_checkbox_on', 'schmon', ['on']),
new invalid_option_warning('my_radio', 'value13', ['value1', 'value2']),
new invalid_option_warning('my_select', 'value42', ['value1', 'value2', 'value3']),
], $result->warnings);
}

/**
* Tests that render warnings are NOT generated when the input element has `qpy:warn-on-unknown-option="no"`.
*
* @return void
* @throws coding_exception
* @covers \qtype_questionpy\attempt_ui\question_ui_renderer::extract_available_options
* @covers \qtype_questionpy\attempt_ui\question_ui_renderer::check_for_unknown_options
*/
public function test_should_not_warn_about_invalid_values_when_input_opts_out(): void {
$input = file_get_contents(__DIR__ . '/question_uis/input-values-nowarn.xhtml');
$qa = $this->create_question_attempt_stub('deadbeef', lastresponse: [
'my_checkbox_value' => 'other_value',
'my_checkbox_on' => 'schmon',
'my_radio' => 'value13',
'my_select' => 'value42',
]);

$ui = new question_ui_renderer($input, [], new \question_display_options(), $qa);
$result = $ui->render();
$this->assertEmpty($result->warnings);
}

/**
* Creates a stub question attempt which should fulfill the needs of most tests.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/attempt_ui/question_uis/input-values-nowarn.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--
- This file is part of the QuestionPy Moodle plugin - https://questionpy.org
-
- Moodle is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Moodle is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Moodle. If not, see <http://www.gnu.org/licenses/>.
-->

<!-- Please keep me in sync with input-values.xhtml. -->
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:qpy="http://questionpy.org/ns/question" id="my_div">
<input type="text" name="my_text" value="original"/>

<input type="checkbox" name="my_checkbox_value" value="value" qpy:warn-on-unknown-option="no"/>
<input type="checkbox" name="my_checkbox_on" qpy:warn-on-unknown-option="no"/>

<input type="radio" name="my_radio" value="value1" qpy:warn-on-unknown-option="no"/>
<input type="radio" name="my_radio" value="value2" checked="checked"/>

<select name="my_select" qpy:warn-on-unknown-option="no">
<option value="value1"/>
<option value="value2" selected="selected"/>
<option value="value3"/>
</select>

<input type="hidden" name="my_hidden" value="original"/>

<input name="my_button" type="button" value="value1"/>
<input name="my_button" type="button" value="value2"/>
</div>

0 comments on commit b4fc264

Please sign in to comment.