Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: write a test for the thing
Browse files Browse the repository at this point in the history
MHajoha committed Nov 13, 2024

Verified

This commit was signed with the committer’s verified signature.
MHajoha Maximilian Haye
1 parent 2074566 commit 67c67c5
Showing 2 changed files with 84 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/question_ui_renderer_test.php
Original file line number Diff line number Diff line change
@@ -378,6 +378,54 @@ public function test_should_replace_qpy_urls(): void {
// phpcs:enable moodle.Files.LineLength.MaxExceeded
}

/**
* Tests the replacement of QPy-URIs.
*
* @return void
* @throws coding_exception
* @covers \qtype_questionpy\question_ui_renderer::replace_qpy_urls
*/
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");
$qa->method("get_last_qt_var")
->willReturnMap([
["my_text", "new"],
["my_checkbox", "value"],
["my_radio", "value1"],
["my_select", "value3"],
["my_hidden", "new"],
["my_submit", "should be ignored"]
]);

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

// phpcs:disable moodle.Files.LineLength.MaxExceeded
$this->assert_html_string_equals_html_string(<<<EXPECTED
<div xmlns="http://www.w3.org/1999/xhtml" id="my_div">
<input type="text" name="my_text" value="new"/>
<input type="checkbox" name="my_checkbox" value="value" checked="checked"/>
<input type="radio" name="my_radio" value="value1" checked="checked"/>
<input type="radio" name="my_radio" value="value2"/>
<select name="my_select">
<option value="value1"/>
<option value="value2"/>
<option value="value3" selected="selected"/>
</select>
<input type="hidden" name="my_hidden" value="new"/>
<input name="my_submit" type="submit" value="value1"/>
<input name="my_submit" type="submit" value="value2"/>
</div>
EXPECTED, $result);
// phpcs:enable moodle.Files.LineLength.MaxExceeded
}

/**
* Creates a stub question attempt which should fulfill the needs of most tests.
*
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_submit" type="submit" value="value1"/>
<input name="my_submit" type="submit" value="value2"/>
</div>

0 comments on commit 67c67c5

Please sign in to comment.