From edb56295453528cf0e2f292f501ba268b8bfd56f Mon Sep 17 00:00:00 2001 From: hieuvu Date: Tue, 30 Sep 2025 13:28:20 +0700 Subject: [PATCH] Make upgrade prototype only run when upgrade. Add prototype creation into unit test. --- db/upgradelib.php | 3 + tests/graphui_save_test.php | 1 + tests/penaltyregime_test.php | 4 +- tests/precheckwalkthrough_test.php | 3 +- tests/test.php | 15 ++++- tests/walkthrough_combinator_grader_test.php | 3 +- tests/walkthrough_display_feedback_test.php | 4 +- tests/walkthrough_extras_test.php | 3 +- tests/walkthrough_multilang_test.php | 4 +- tests/walkthrough_randomisation_test.php | 3 +- tests/walkthrough_test.php | 6 +- tests/walkthrough_testbase.php | 63 ++++++++++++++++++++ 12 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 tests/walkthrough_testbase.php diff --git a/db/upgradelib.php b/db/upgradelib.php index f6d7d1ca4..9b087c851 100644 --- a/db/upgradelib.php +++ b/db/upgradelib.php @@ -86,6 +86,9 @@ function update_question_types() { function update_question_types_internal() { mtrace("Setting up CodeRunner question prototypes..."); $result = false; + if (PHPUNIT_TEST) { + return $result; + } try { if (qtype_coderunner_util::using_mod_qbank()) { $result = update_question_types_with_qbank(); diff --git a/tests/graphui_save_test.php b/tests/graphui_save_test.php index 340639e94..a8a1c3b7e 100644 --- a/tests/graphui_save_test.php +++ b/tests/graphui_save_test.php @@ -40,6 +40,7 @@ class graphui_save_test extends \qtype_coderunner_testcase { protected $qtype; protected function setUp(): void { + parent::setUp(); $this->resetAfterTest(true); $this->qtype = new \qtype_coderunner(); } diff --git a/tests/penaltyregime_test.php b/tests/penaltyregime_test.php index 774ce113c..a6d0a0326 100644 --- a/tests/penaltyregime_test.php +++ b/tests/penaltyregime_test.php @@ -32,7 +32,7 @@ require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/helper.php'); - +require_once($CFG->dirroot . '/question/type/coderunner/tests/walkthrough_testbase.php'); /** * More extensive testing of penalty regime. * @@ -41,7 +41,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class penaltyregime_test extends \qbehaviour_walkthrough_test_base { +class penaltyregime_test extends walkthrough_testbase { protected function setUp(): void { parent::setUp(); \qtype_coderunner_testcase::setup_test_sandbox_configuration(); diff --git a/tests/precheckwalkthrough_test.php b/tests/precheckwalkthrough_test.php index 9ad6b2c5d..9ef47ab69 100644 --- a/tests/precheckwalkthrough_test.php +++ b/tests/precheckwalkthrough_test.php @@ -32,13 +32,14 @@ require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); require_once($CFG->dirroot . '/question/type/coderunner/question.php'); +require_once($CFG->dirroot . '/question/type/coderunner/tests/walkthrough_testbase.php'); use qtype_coderunner\constants; /** * @coversNothing */ -class precheckwalkthrough_test extends \qbehaviour_walkthrough_test_base { +class precheckwalkthrough_test extends walkthrough_testbase { protected function setUp(): void { parent::setUp(); \qtype_coderunner_testcase::setup_test_sandbox_configuration(); diff --git a/tests/test.php b/tests/test.php index e05831c37..c4a281547 100644 --- a/tests/test.php +++ b/tests/test.php @@ -29,7 +29,7 @@ global $CFG; require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/question.php'); - +require_once($CFG->dirroot . '/question/type/coderunner/db/upgradelib.php'); /** * @coversNothing */ @@ -39,11 +39,22 @@ class qtype_coderunner_testcase extends advanced_testcase { /** @var stdClass Holds question category.*/ protected $category; + protected static bool $prototypesinstalled = false; + protected function setUp(): void { parent::setUp(); self::setup_test_sandbox_configuration(); - $this->resetAfterTest(false); + $this->resetAfterTest(true); $this->setAdminUser(); + ob_start(); + if (!self::$prototypesinstalled) { + if (\qtype_coderunner_util::using_mod_qbank()) { + update_question_types_with_qbank(); + } else { + update_question_types_legacy(); + } + } + ob_end_clean(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $this->category = $generator->create_question_category([]); } diff --git a/tests/walkthrough_combinator_grader_test.php b/tests/walkthrough_combinator_grader_test.php index bb34d1f7d..5e0e28db9 100644 --- a/tests/walkthrough_combinator_grader_test.php +++ b/tests/walkthrough_combinator_grader_test.php @@ -32,6 +32,7 @@ global $CFG; require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); +require_once($CFG->dirroot . '/question/type/coderunner/tests/walkthrough_testbase.php'); /** * Unit tests for the coderunner question type. @@ -40,7 +41,7 @@ * @copyright 2011, 2020 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class walkthrough_combinator_grader_test extends \qbehaviour_walkthrough_test_base { +class walkthrough_combinator_grader_test extends walkthrough_testbase { protected function setUp(): void { global $CFG; parent::setUp(); diff --git a/tests/walkthrough_display_feedback_test.php b/tests/walkthrough_display_feedback_test.php index 2432afde2..9dcde7a83 100644 --- a/tests/walkthrough_display_feedback_test.php +++ b/tests/walkthrough_display_feedback_test.php @@ -22,7 +22,7 @@ require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); require_once($CFG->dirroot . '/question/type/coderunner/question.php'); - +require_once($CFG->dirroot . '/question/type/coderunner/tests/walkthrough_testbase.php'); /** * Use a walkthrough test to validate the new (2019) display-feedback options. @@ -33,7 +33,7 @@ * @copyright 2019 Richard Lobb, The University of Canterbury * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class walkthrough_display_feedback_test extends \qbehaviour_walkthrough_test_base { +class walkthrough_display_feedback_test extends walkthrough_testbase { protected function setUp(): void { parent::setUp(); \qtype_coderunner_testcase::setup_test_sandbox_configuration(); diff --git a/tests/walkthrough_extras_test.php b/tests/walkthrough_extras_test.php index afcc4780c..1e95e31d8 100644 --- a/tests/walkthrough_extras_test.php +++ b/tests/walkthrough_extras_test.php @@ -24,6 +24,7 @@ require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); require_once($CFG->dirroot . '/question/type/coderunner/question.php'); +require_once($CFG->dirroot . '/question/type/coderunner/tests/walkthrough_testbase.php'); define('PRELOAD_TEST', "# TEST COMMENT TO CHECK PRELOAD IS WORKING\n"); @@ -38,7 +39,7 @@ * @copyright 2012, 2014 Richard Lobb, The University of Canterbury * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class walkthrough_extras_test extends \qbehaviour_walkthrough_test_base { +class walkthrough_extras_test extends walkthrough_testbase { protected function setUp(): void { parent::setUp(); \qtype_coderunner_testcase::setup_test_sandbox_configuration(); diff --git a/tests/walkthrough_multilang_test.php b/tests/walkthrough_multilang_test.php index ab02feb74..a32e328dd 100644 --- a/tests/walkthrough_multilang_test.php +++ b/tests/walkthrough_multilang_test.php @@ -24,7 +24,7 @@ require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); require_once($CFG->dirroot . '/question/type/coderunner/question.php'); - +require_once($CFG->dirroot . '/question/type/coderunner/tests/walkthrough_testbase.php'); /** * A walkthrough of a simple multilanguage question that asks for a program @@ -37,7 +37,7 @@ * @copyright 2018 Richard Lobb, The University of Canterbury * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class walkthrough_multilang_test extends \qbehaviour_walkthrough_test_base { +class walkthrough_multilang_test extends walkthrough_testbase { protected function setUp(): void { parent::setUp(); \qtype_coderunner_testcase::setup_test_sandbox_configuration(); diff --git a/tests/walkthrough_randomisation_test.php b/tests/walkthrough_randomisation_test.php index acd286d00..61bb135d3 100644 --- a/tests/walkthrough_randomisation_test.php +++ b/tests/walkthrough_randomisation_test.php @@ -22,6 +22,7 @@ require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); require_once($CFG->dirroot . '/question/type/coderunner/question.php'); +require_once($CFG->dirroot . '/question/type/coderunner/tests/walkthrough_testbase.php'); /** * Further walkthrough tests for the CodeRunner plugin, testing the @@ -33,7 +34,7 @@ * @copyright 2018 Richard Lobb, The University of Canterbury * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class walkthrough_randomisation_test extends \qbehaviour_walkthrough_test_base { +class walkthrough_randomisation_test extends walkthrough_testbase { protected function setUp(): void { parent::setUp(); \qtype_coderunner_testcase::setup_test_sandbox_configuration(); diff --git a/tests/walkthrough_test.php b/tests/walkthrough_test.php index 45c8692ec..1d7c24926 100644 --- a/tests/walkthrough_test.php +++ b/tests/walkthrough_test.php @@ -31,6 +31,8 @@ global $CFG; require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); +require_once($CFG->dirroot . '/question/type/coderunner/question.php'); +require_once($CFG->dirroot . '/question/type/coderunner/tests/walkthrough_testbase.php'); /** * Unit tests for the coderunner question type. @@ -38,7 +40,7 @@ * @copyright 2011 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class walkthrough_test extends \qbehaviour_walkthrough_test_base { +class walkthrough_test extends walkthrough_testbase { protected function setUp(): void { parent::setUp(); \qtype_coderunner_testcase::setup_test_sandbox_configuration(); @@ -117,6 +119,8 @@ public function test_view_hidden_testcases_capability() { $user = $generator->create_user(); $coursecontext = \context_course::instance($course->id); $generator->enrol_user($user->id, $course->id, 'editingteacher'); + // Reset the page after we installed the prototypes. + $PAGE->reset_theme_and_output(); $this->setUser($user); $PAGE->set_course($course); diff --git a/tests/walkthrough_testbase.php b/tests/walkthrough_testbase.php new file mode 100644 index 000000000..54f030057 --- /dev/null +++ b/tests/walkthrough_testbase.php @@ -0,0 +1,63 @@ +. + +/** + * Walkthrough base for the coderunner question type. + * @group qtype_coderunner + * + * @package qtype + * @subpackage coderunner + * @copyright 2011, 2020 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace qtype_coderunner; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); +require_once($CFG->dirroot . '/question/type/coderunner/tests/test.php'); +require_once($CFG->dirroot . '/question/type/coderunner/db/upgradelib.php'); + +/** + * Walkthrough base for the coderunner question type. + * + * @coversNothing + * @copyright 2011, 2020 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class walkthrough_testbase extends \qbehaviour_walkthrough_test_base { + protected static bool $prototypesinstalled = false; + + protected function setUp(): void { + parent::setUp(); + $this->resetAfterTest(true); + $this->setAdminUser(); + ob_start(); + if (!self::$prototypesinstalled) { + if (\qtype_coderunner_util::using_mod_qbank()) { + update_question_types_with_qbank(); + } else { + update_question_types_legacy(); + } + } + ob_end_clean(); + } + + public function test_dummy_walkthrough_testbase(): void { + /* Present to avoid a warning about no testcases. */ + } +} \ No newline at end of file