Skip to content

Commit 2e374a1

Browse files
committed
refactor: have render_context take a moodleform again
Specifically requiring a question_edit_form is too much of a headache in UTs.
1 parent e106ff0 commit 2e374a1

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

classes/local/form/context/render_context.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,21 @@ abstract class render_context {
4747
/**
4848
* Initializes a new render context.
4949
*
50-
* @param question_edit_form $moodleform Target {@see question_edit_form} instance, such as {@see \qtype_questionpy_edit_form}
50+
* @param moodleform $moodleform The target {@see moodleform} instance. In reality, this is a
51+
* {@see \qtype_questionpy_edit_form}, but a raw {@see moodleform} is much easier to
52+
* produce in unit tests).
5153
* @param MoodleQuickForm $mform Target {@see MoodleQuickForm} instance, as passed to
5254
* {@see \question_edit_form::definition_inner}
5355
* @param object $question The current question being edited
5456
* @param string $prefix Prefix for the names of elements in this context
5557
* @param array $data The current form data as of last save, in {@see self::register_rich_conversion() QPy server format}
5658
*/
5759
public function __construct(
58-
/** @var moodleform target {@see moodleform} instance, such as {@see \qtype_questionpy_edit_form} */
59-
public question_edit_form $moodleform,
60+
/**
61+
* @var moodleform The target {@see moodleform} instance. In reality, this is a {@see \qtype_questionpy_edit_form}, but a
62+
* raw {@see moodleform} is much easier to produce in unit tests).
63+
*/
64+
public moodleform $moodleform,
6065
/**
6166
* @var MoodleQuickForm target {@see MoodleQuickForm} instance, as passed to
6267
* {@see \question_edit_form::definition_inner}

classes/local/form/elements/file_upload_element.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function render_to(render_context $context): void {
123123
}
124124

125125
$ofs = di::get(options_file_service::class);
126-
$ofs->prepare_draft_area($context->moodleform->context->id, $questionid, $files, $USER->id, $draftitemid);
126+
$ofs->prepare_draft_area($context->question->contextid, $questionid, $files, $USER->id, $draftitemid);
127127
}
128128

129129
utils::array_set_nested($alldata, $element->getName(), $draftitemid);

classes/local/form/elements/wysiwyg_editor_element.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use coding_exception;
2020
use context_user;
21+
use core\context;
2122
use core\di;
2223
use moodle_exception;
2324
use moodle_url;
@@ -88,7 +89,7 @@ public function render_to(render_context $context): void {
8889
[
8990
'maxfiles' => EDITOR_UNLIMITED_FILES,
9091
'subdirs' => self::SUBDIRS,
91-
'context' => $context->moodleform->context,
92+
'context' => context::instance_by_id($context->question->contextid),
9293
]
9394
);
9495
$context->set_type($this->name, PARAM_RAW);
@@ -115,7 +116,7 @@ public function render_to(render_context $context): void {
115116
// do this _before_ replacing URLs.
116117
if ($format != FORMAT_HTML && $this->includehtml) {
117118
$html = format_text($markup, $format, options: [
118-
'context' => $context->moodleform->context,
119+
'context' => context::instance_by_id($context->question->contextid),
119120
// We leave cleaning to when the content is output. (Placeholder values are cleaned by default, for instance.)
120121
'noclean' => true,
121122
// If filter is true (default), format_text replaces draftfile URLs with brokenfile.
@@ -173,7 +174,7 @@ public function render_to(render_context $context): void {
173174
}
174175

175176
$ofs = di::get(options_file_service::class);
176-
$ofs->prepare_draft_area($context->moodleform->context->id, $questionid, $mydata->files, $USER->id, $draftitemid);
177+
$ofs->prepare_draft_area($context->question->contextid, $questionid, $mydata->files, $USER->id, $draftitemid);
177178
}
178179

179180
$filenamebyfileref = array_flip(array_map(fn($fmeta) => $fmeta->fileref, $mydata->files));

tests/local/form/elements/test_moodleform.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use qtype_questionpy\local\form\context\root_render_context;
2525
use qtype_questionpy\local\form\qpy_renderable;
26+
use stdClass;
2627

2728
/**
2829
* Stub {@see \moodleform} implementation for tests.
@@ -54,7 +55,11 @@ public function __construct(qpy_renderable $element) {
5455
* Output can be retrieved using {@see render}, which calls this method.
5556
*/
5657
protected function definition() {
57-
$context = new root_render_context($this, $this->_form, 'qpy_form', []);
58+
global $PAGE;
59+
$question = (object) [
60+
'contextid' => $PAGE->context->id,
61+
];
62+
$context = new root_render_context($this, $this->_form, $question, 'qpy_form', []);
5863
$context->uuidgen = fn() => '24daab97-7eeb-422d-a2f3-f4e770fb11f6';
5964
$this->element->render_to($context);
6065
}

0 commit comments

Comments
 (0)