3838final class editform_test_class extends \qtype_stack_edit_form {
3939
4040 // phpcs:ignore moodle.Commenting.MissingDocblock.MissingTestcaseMethodDescription
41- public function __construct ($ questiontext , $ specificfeedback ) {
41+ public function __construct ($ questiontext , $ specificfeedback, $ quizmoduleid ) {
4242 global $ USER ;
43- $ syscontext = \context_system::instance ();
44- $ category = question_make_default_categories ([$ syscontext ]);
43+ // ISS1325 - Use quiz context rather than system context as
44+ // question categories only allowed in modules from Moodle 5.
45+ $ quizcontext = \context_module::instance ($ quizmoduleid );
46+ if (function_exists ('question_get_default_category ' )) {
47+ // This function exists from Moodle 4.5 onwards but the second parameter
48+ // which creates the category if it doesn't exist is 5.0 onwards.
49+ $ category = question_get_default_category ($ quizcontext ->id , true );
50+ if (!$ category ) {
51+ $ category = $ category = question_make_default_categories ([$ quizcontext ]);
52+ }
53+ } else {
54+ // Deprecated from 5.0.
55+ $ category = $ category = question_make_default_categories ([$ quizcontext ]);
56+ }
4557 $ fakequestion = new \stdClass ();
4658 $ fakequestion ->qtype = 'stack ' ;
4759 $ fakequestion ->category = $ category ->id ;
48- $ fakequestion ->contextid = $ syscontext ->id ;
60+ $ fakequestion ->contextid = $ quizcontext ->id ;
4961 $ fakequestion ->createdby = $ USER ->id ;
5062 $ fakequestion ->modifiedby = $ USER ->id ;
5163 $ fakequestion ->questiontext = $ questiontext ;
@@ -57,9 +69,9 @@ public function __construct($questiontext, $specificfeedback) {
5769 $ fakequestion ->inputs = null ;
5870 // Support both Moodle 4.x and 3.x.
5971 if (class_exists ('\core_question\local\bank\question_edit_contexts ' )) {
60- $ contexts = new \core_question \local \bank \question_edit_contexts ($ syscontext );
72+ $ contexts = new \core_question \local \bank \question_edit_contexts ($ quizcontext );
6173 } else {
62- $ contexts = new \question_edit_contexts ($ syscontext );
74+ $ contexts = new \question_edit_contexts ($ quizcontext );
6375 }
6476 parent ::__construct (new \moodle_url ('/ ' ), $ fakequestion , $ category , $ contexts );
6577 }
@@ -81,8 +93,12 @@ final class editform_test extends \advanced_testcase {
8193 protected function get_form ($ questiontext , $ specificfeedback ) {
8294 $ this ->setAdminUser ();
8395 $ this ->resetAfterTest ();
84-
85- return new editform_test_class ($ questiontext , $ specificfeedback );
96+ $ quizgenerator = self ::getDataGenerator ()->get_plugin_generator ('mod_quiz ' );
97+ $ site = get_site ();
98+ // Add a quiz to the site course.
99+ $ quiz = $ quizgenerator ->create_instance (['course ' => $ site ->id , 'grade ' => 100.0 , 'sumgrades ' => 2 , 'layout ' => '1,0 ' ]);
100+ $ quizmoduleid = $ quiz ->cmid ;
101+ return new editform_test_class ($ questiontext , $ specificfeedback , $ quizmoduleid );
86102 }
87103
88104 public function test_get_input_names_from_question_text_default (): void {
0 commit comments