Skip to content

Commit

Permalink
Copy over editform_test.php to fix CI in latest moodle (main). Part of
Browse files Browse the repository at this point in the history
  • Loading branch information
sangwinc committed Jan 21, 2025
1 parent a32dac2 commit 08d73d1
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions tests/editform_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Subclass of qtype_stack_edit_form_testable that is easier to use in unit tests.
*
* @package qtype_stack
* @copyright 2012 The Open University.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/

namespace qtype_stack;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -22,25 +30,34 @@
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
require_once(__DIR__ . '/../edit_stack_form.php');

// Subclass of qtype_stack_edit_form_testable that is easier to use in unit tests.
//
// @copyright 2012 The Open University.
// @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.

/**
* Subclass of qtype_stack_edit_form_testable that is easier to use in unit tests.
* @group qtype_stack
* @covers \qtype_stack_edit_form
*/
class editform_test_class extends \qtype_stack_edit_form {
final class editform_test_class extends \qtype_stack_edit_form {

public function __construct($questiontext, $specificfeedback) {
// phpcs:ignore moodle.Commenting.MissingDocblock.MissingTestcaseMethodDescription
public function __construct($questiontext, $specificfeedback, $quizmoduleid) {
global $USER;
$syscontext = \context_system::instance();
$category = question_make_default_categories([$syscontext]);
// ISS1325 - Use quiz context rather than system context as
// question categories only allowed in modules from Moodle 5.
$quizcontext = \context_module::instance($quizmoduleid);
if (function_exists('question_get_default_category')) {
// This function exists from Moodle 4.5 onwards but the second parameter
// which creates the category if it doesn't exist is 5.0 onwards.
$category = question_get_default_category($quizcontext->id, true);
if (!$category) {
$category = $category = question_make_default_categories([$quizcontext]);
}
} else {
// Deprecated from 5.0.
$category = $category = question_make_default_categories([$quizcontext]);
}
$fakequestion = new \stdClass();
$fakequestion->qtype = 'stack';
$fakequestion->category = $category->id;
$fakequestion->contextid = $syscontext->id;
$fakequestion->contextid = $quizcontext->id;
$fakequestion->createdby = $USER->id;
$fakequestion->modifiedby = $USER->id;
$fakequestion->questiontext = $questiontext;
Expand All @@ -52,9 +69,9 @@ public function __construct($questiontext, $specificfeedback) {
$fakequestion->inputs = null;
// Support both Moodle 4.x and 3.x.
if (class_exists('\core_question\local\bank\question_edit_contexts')) {
$contexts = new \core_question\local\bank\question_edit_contexts($syscontext);
$contexts = new \core_question\local\bank\question_edit_contexts($quizcontext);
} else {
$contexts = new \question_edit_contexts($syscontext);
$contexts = new \question_edit_contexts($quizcontext);
}
parent::__construct(new \moodle_url('/'), $fakequestion, $category, $contexts);
}
Expand All @@ -64,21 +81,28 @@ public function __construct($questiontext, $specificfeedback) {
/**
* Unit tests for Stack question editing form.
*
* @package qtype_stack
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @group qtype_stack
* @covers \qtype_stack_edit_form
*/
class editform_test extends \advanced_testcase {
final class editform_test extends \advanced_testcase {

// phpcs:ignore moodle.Commenting.MissingDocblock.MissingTestcaseMethodDescription
protected function get_form($questiontext, $specificfeedback) {
$this->setAdminUser();
$this->resetAfterTest();

return new editform_test_class($questiontext, $specificfeedback);
$quizgenerator = self::getDataGenerator()->get_plugin_generator('mod_quiz');
$site = get_site();
// Add a quiz to the site course.
$quiz = $quizgenerator->create_instance(['course' => $site->id, 'grade' => 100.0, 'sumgrades' => 2, 'layout' => '1,0']);
$quizmoduleid = $quiz->cmid;
return new editform_test_class($questiontext, $specificfeedback, $quizmoduleid);
}

public function test_get_input_names_from_question_text_default() {
public function test_get_input_names_from_question_text_default(): void {

$form = $this->get_form(\qtype_stack_edit_form::DEFAULT_QUESTION_TEXT,
\qtype_stack_edit_form::DEFAULT_SPECIFIC_FEEDBACK);
$qtype = new \qtype_stack();
Expand All @@ -87,7 +111,8 @@ public function test_get_input_names_from_question_text_default() {
$qtype->get_input_names_from_question_text(\qtype_stack_edit_form::DEFAULT_QUESTION_TEXT));
}

public function test_get_prt_names_from_question_default() {
public function test_get_prt_names_from_question_default(): void {

$form = $this->get_form(\qtype_stack_edit_form::DEFAULT_QUESTION_TEXT,
\qtype_stack_edit_form::DEFAULT_SPECIFIC_FEEDBACK);
$qtype = new \qtype_stack();
Expand Down

0 comments on commit 08d73d1

Please sign in to comment.