Skip to content

Commit

Permalink
test: add test of set_input_values_and_readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Nov 15, 2024
1 parent bdf9eba commit 2c3309e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/question_ui_renderer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,53 @@ public function test_should_replace_qpy_urls(): void {
// phpcs:enable moodle.Files.LineLength.MaxExceeded
}

/**
* Tests {@see question_ui_renderer::set_input_values_and_readonly}.
*
* @return void
* @throws coding_exception
* @covers \qtype_questionpy\question_ui_renderer::set_input_values_and_readonly
*/
public function test_should_correctly_fill_data(): void {
$input = file_get_contents(__DIR__ . '/question_uis/input-values.xhtml');
$qa = $this->create_question_attempt_stub('deadbeef');
$newvalues = [
'my_text' => 'new',
'my_checkbox' => 'value',
'my_radio' => 'value1',
'my_select' => 'value3',
'my_hidden' => 'new',
'my_button' => 'should be ignored',
];
$qa->method('get_last_qt_var')
->willReturnCallback(fn($name) => $newvalues[$name]);

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

$this->assert_html_string_equals_html_string(<<<EXPECTED
<div xmlns="http://www.w3.org/1999/xhtml" id="mangled:my_div">
<input class="form-control qpy-input" type="text" name="mangled:my_text" value="new"/>
<input class="qpy-input" type="checkbox" name="mangled:my_checkbox" value="value" checked="checked"/>
<input class="qpy-input" type="radio" name="mangled:my_radio" value="value1" checked="checked"/>
<input class="qpy-input" type="radio" name="mangled:my_radio" value="value2"/>
<select class="form-control qpy-input" name="mangled:my_select">
<option value="value1"/>
<option value="value2"/>
<option value="value3" selected="selected"/>
</select>
<input class="form-control qpy-input" type="hidden" name="mangled:my_hidden" value="new"/>
<input class="btn btn-primary qpy-input" name="mangled:my_button" type="button" value="value1"/>
<input class="btn btn-primary qpy-input" name="mangled:my_button" type="button" value="value2"/>
</div>
EXPECTED, $result);
}

/**
* Creates a stub question attempt which should fulfill the needs of most tests.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/question_uis/input-values.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
- 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/>.
-->

<div xmlns="http://www.w3.org/1999/xhtml" id="my_div">
<input type="text" name="my_text" value="original"/>

<input type="checkbox" name="my_checkbox" value="value"/>

<input type="radio" name="my_radio" value="value1"/>
<input type="radio" name="my_radio" value="value2" checked="checked"/>

<select name="my_select">
<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 2c3309e

Please sign in to comment.