diff --git a/admin/presets/tests/manager_test.php b/admin/presets/tests/manager_test.php index cd4878f0e5c..2ab19814ebc 100644 --- a/admin/presets/tests/manager_test.php +++ b/admin/presets/tests/manager_test.php @@ -29,6 +29,14 @@ use stdClass; * @coversDefaultClass \core_adminpresets\manager */ class manager_test extends \advanced_testcase { + /** + * Include required libraries. + */ + public static function setUpBeforeClass(): void { + global $CFG; + require_once($CFG->libdir.'/adminlib.php'); + } + /** * Test the behaviour of protected get_site_settings method. * diff --git a/admin/tool/behat/tests/manager_util_test.php b/admin/tool/behat/tests/manager_util_test.php index 39fc8e09de2..5bb6627231c 100644 --- a/admin/tool/behat/tests/manager_util_test.php +++ b/admin/tool/behat/tests/manager_util_test.php @@ -25,6 +25,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace tool_behat; + +use behat_config_util; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -39,7 +43,7 @@ require_once($CFG->libdir . '/behat/classes/behat_config_manager.php'); * @copyright 2016 Rajesh Taneja * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class tool_behat_manager_util_testcase extends advanced_testcase { +class manager_util_test extends \advanced_testcase { /** @var array Fixtures features which are available. */ private $featurepaths = array( diff --git a/admin/tool/uploadcourse/tests/helper_test.php b/admin/tool/uploadcourse/tests/helper_test.php index 075af2bcd7a..5f58c13d181 100644 --- a/admin/tool/uploadcourse/tests/helper_test.php +++ b/admin/tool/uploadcourse/tests/helper_test.php @@ -107,6 +107,7 @@ class helper_test extends \advanced_testcase { public function test_get_restore_content_dir() { global $CFG; + require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); $this->resetAfterTest(true); $this->setAdminUser(); diff --git a/backup/util/plan/tests/fixtures/plan_fixtures.php b/backup/util/plan/tests/fixtures/plan_fixtures.php index 53f40906bd7..697d0db9ab2 100644 --- a/backup/util/plan/tests/fixtures/plan_fixtures.php +++ b/backup/util/plan/tests/fixtures/plan_fixtures.php @@ -27,6 +27,8 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); +require_once($CFG->dirroot . '/backup/moodle2/backup_custom_fields.php'); +require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php'); /** diff --git a/course/tests/backup_restore_activity_test.php b/course/tests/backup_restore_activity_test.php index 0fc1d9ebbb2..9759649927e 100644 --- a/course/tests/backup_restore_activity_test.php +++ b/course/tests/backup_restore_activity_test.php @@ -27,6 +27,14 @@ use backup; * @covers \restore_module_structure_step */ class backup_restore_activity_test extends \advanced_testcase { + /** + * Load the backup and restore classes. + */ + public static function setUpBeforeClass(): void { + global $CFG; + require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); + require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); + } /** * Test that duplicating a page preserves the lang setting. diff --git a/enrol/meta/tests/plugin_test.php b/enrol/meta/tests/plugin_test.php index 070f806b662..754c2622fd4 100644 --- a/enrol/meta/tests/plugin_test.php +++ b/enrol/meta/tests/plugin_test.php @@ -736,12 +736,15 @@ class plugin_test extends \advanced_testcase { * Test that a new group with the name of the course is created. */ public function test_enrol_meta_create_new_group() { - global $DB; + global $DB, $CFG; $this->resetAfterTest(); // Create two courses. $course = $this->getDataGenerator()->create_course(array('fullname' => 'Mathematics')); $course2 = $this->getDataGenerator()->create_course(array('fullname' => 'Physics')); $metacourse = $this->getDataGenerator()->create_course(array('fullname' => 'All sciences')); + + require_once($CFG->dirroot.'/enrol/meta/locallib.php'); + // Run the function. $groupid = enrol_meta_create_new_group($metacourse->id, $course->id); // Check the results. diff --git a/lib/grade/tests/grade_grade_test.php b/lib/grade/tests/grade_grade_test.php index fac84ffc3c5..ab6b768cfbe 100644 --- a/lib/grade/tests/grade_grade_test.php +++ b/lib/grade/tests/grade_grade_test.php @@ -126,7 +126,12 @@ class grade_grade_test extends \grade_base_testcase { $this->assertEquals(40, \grade_grade::standardise_score(50, 30, 80, 0, 100)); } - public function test_grade_grade_set_locked() { + /** + * Tests grade_grade::set_locked() + * + * @covers \grade_grade::set_locked + */ + public function test_grade_grade_set_locked(): void { // Skip this test because set_locked() arguments have been modified, rendering these tests // useless until they are re-written. Note this comes from MDL-32323 (2012!). $this->markTestSkipped('Useless set_locked() tests until they are re-written'); diff --git a/lib/tests/accesslib_test.php b/lib/tests/accesslib_test.php index 60aa99c2063..dd210c7c264 100644 --- a/lib/tests/accesslib_test.php +++ b/lib/tests/accesslib_test.php @@ -3515,9 +3515,9 @@ class accesslib_test extends advanced_testcase { // Test context_helper::reset_caches() method. context_helper::reset_caches(); - $this->assertEquals(0, context_inspection::test_context_cache_size()); + $this->assertEquals(0, context_inspection::check_context_cache_size()); context_course::instance($SITE->id); - $this->assertEquals(1, context_inspection::test_context_cache_size()); + $this->assertEquals(1, context_inspection::check_context_cache_size()); // Test context preloading. @@ -3536,14 +3536,15 @@ class accesslib_test extends advanced_testcase { context_helper::preload_from_record($record); $this->assertEquals(new stdClass(), $record); } - $this->assertEquals(count($records), context_inspection::test_context_cache_size()); + $this->assertEquals(count($records), context_inspection::check_context_cache_size()); unset($records); unset($columns); context_helper::reset_caches(); context_helper::preload_course($SITE->id); $numfrontpagemodules = $DB->count_records('course_modules', array('course' => $SITE->id)); - $this->assertEquals(3 + $numfrontpagemodules, context_inspection::test_context_cache_size()); // Depends on number of default blocks. + $this->assertEquals(3 + $numfrontpagemodules, + context_inspection::check_context_cache_size()); // Depends on number of default blocks. // Test assign_capability(), unassign_capability() functions. @@ -3967,13 +3968,13 @@ class accesslib_test extends advanced_testcase { $lastcourse = array_pop($testcourses); $this->assertTrue($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$lastcourse))); $coursecontext = context_course::instance($lastcourse); - $this->assertEquals(1, context_inspection::test_context_cache_size()); + $this->assertEquals(1, context_inspection::check_context_cache_size()); $this->assertNotEquals(CONTEXT_COURSE, $coursecontext->instanceid); $DB->delete_records('cache_flags', array()); context_helper::delete_instance(CONTEXT_COURSE, $lastcourse); $dirty = get_cache_flags('accesslib/dirtycontexts', time()-2); $this->assertFalse(isset($dirty[$coursecontext->path])); - $this->assertEquals(0, context_inspection::test_context_cache_size()); + $this->assertEquals(0, context_inspection::check_context_cache_size()); $this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$lastcourse))); context_course::instance($lastcourse); @@ -4028,20 +4029,21 @@ class accesslib_test extends advanced_testcase { for ($i=0; $iassertEquals(CONTEXT_CACHE_MAX_SIZE, context_inspection::test_context_cache_size()); + $this->assertEquals(CONTEXT_CACHE_MAX_SIZE, context_inspection::check_context_cache_size()); } else if ($i == CONTEXT_CACHE_MAX_SIZE) { // Once the limit is reached roughly 1/3 of records should be removed from cache. - $this->assertEquals((int)ceil(CONTEXT_CACHE_MAX_SIZE * (2/3) + 101), context_inspection::test_context_cache_size()); + $this->assertEquals((int)ceil(CONTEXT_CACHE_MAX_SIZE * (2 / 3) + 101), + context_inspection::check_context_cache_size()); } } // We keep the first 100 cached. - $prevsize = context_inspection::test_context_cache_size(); + $prevsize = context_inspection::check_context_cache_size(); for ($i=0; $i<100; $i++) { context_user::instance($testusers[$i]); - $this->assertEquals($prevsize, context_inspection::test_context_cache_size()); + $this->assertEquals($prevsize, context_inspection::check_context_cache_size()); } context_user::instance($testusers[102]); - $this->assertEquals($prevsize+1, context_inspection::test_context_cache_size()); + $this->assertEquals($prevsize + 1, context_inspection::check_context_cache_size()); unset($testusers); @@ -5283,7 +5285,12 @@ class accesslib_test extends advanced_testcase { * Context caching fixture */ abstract class context_inspection extends \core\context_helper { - public static function test_context_cache_size() { + /** + * Return the cached contexts count for testing purposes. + * + * @return int + */ + public static function check_context_cache_size() { return self::$cache_count; } } diff --git a/lib/tests/completionlib_test.php b/lib/tests/completionlib_test.php index fb0904bbb2d..cf99198f9cf 100644 --- a/lib/tests/completionlib_test.php +++ b/lib/tests/completionlib_test.php @@ -1690,7 +1690,8 @@ class completionlib_test extends advanced_testcase { * @covers \aggregate_completions */ public function test_aggregate_completions() { - global $DB; + global $DB, $CFG; + require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php'); $this->resetAfterTest(true); $time = time(); diff --git a/lib/tests/hook/manager_test.php b/lib/tests/hook/manager_test.php index de1e29be6ed..a91c8bcc52a 100644 --- a/lib/tests/hook/manager_test.php +++ b/lib/tests/hook/manager_test.php @@ -149,6 +149,9 @@ final class manager_test extends \advanced_testcase { * Test hook dispatching, that is callback execution. */ public function test_dispatch_with_invalid(): void { + require_once(__DIR__ . '/../fixtures/hook/hook.php'); + require_once(__DIR__ . '/../fixtures/hook/callbacks.php'); + // Missing callbacks is ignored. $componentfiles = [ 'test_plugin1' => __DIR__ . '/../fixtures/hook/hooks1_missing.php', diff --git a/lib/tests/myprofilelib_test.php b/lib/tests/myprofilelib_test.php index fd7344f484b..1452b6100f5 100644 --- a/lib/tests/myprofilelib_test.php +++ b/lib/tests/myprofilelib_test.php @@ -16,11 +16,6 @@ namespace core; -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once($CFG->dirroot . '/lib/myprofilelib.php'); - /** * Tests for myprofilelib apis. * @@ -44,6 +39,16 @@ class myprofilelib_test extends \advanced_testcase { * @var \core_user\output\myprofile\tree The navigation tree. */ private $tree; + + /** + * Load required test libraries + */ + public static function setUpBeforeClass(): void { + global $CFG; + require_once($CFG->dirroot . '/lib/myprofilelib.php'); + require_once($CFG->dirroot . '/user/profile/lib.php'); + } + public function setUp(): void { // Set the $PAGE->url value so core_myprofile_navigation() doesn't complain. global $PAGE; diff --git a/lib/tests/task/logging_test.php b/lib/tests/task/logging_test.php index 9a7b056a765..cb3752735db 100644 --- a/lib/tests/task/logging_test.php +++ b/lib/tests/task/logging_test.php @@ -485,7 +485,7 @@ class logging_test extends \advanced_testcase { // Note PHPUnit does not support mocking static functions. $CFG->task_log_class = logging_test_mocked_logger::class; - logging_test_mocked_logger::test_reset(); + logging_test_mocked_logger::reset_test(); return $CFG->task_log_class; } @@ -533,7 +533,7 @@ class logging_test_mocked_logger implements task_logger { /** * Reset the test class. */ - public static function test_reset() { + public static function reset_test() { self::$isconfigured = true; self::$storelogfortask = []; self::$haslogreport = true; diff --git a/lib/tests/task/manager_test.php b/lib/tests/task/manager_test.php index 5156a33b52d..90d2c82f75b 100644 --- a/lib/tests/task/manager_test.php +++ b/lib/tests/task/manager_test.php @@ -16,6 +16,14 @@ namespace core\task; +defined('MOODLE_INTERNAL') || die(); + +// We need to keep this here because there is a provider +// needing \core\task\adhoc_test_task and cannot move it +// to setUpBeforeClass() or similar. Whenever we allow to +// autoload fixtures, this can be removed. +require_once(__DIR__ . '/../fixtures/task_fixtures.php'); + /** * This file contains the unit tests for the task manager. * @@ -26,10 +34,6 @@ namespace core\task; * @covers \core\task\manager */ final class manager_test extends \advanced_testcase { - public static function setUpBeforeClass(): void { - require_once(__DIR__ . '/../fixtures/task_fixtures.php'); - } - /** * Data provider for test_get_candidate_adhoc_tasks. * diff --git a/mod/lesson/tests/generator_test.php b/mod/lesson/tests/generator_test.php index b1cff3f3e80..5f22f261cb4 100644 --- a/mod/lesson/tests/generator_test.php +++ b/mod/lesson/tests/generator_test.php @@ -226,7 +226,9 @@ class generator_test extends \advanced_testcase { * @param string $type Type of page to test: LESSON_PAGE_CLUSTER, LESSON_PAGE_ENDOFCLUSTER or LESSON_PAGE_ENDOFBRANCH. */ public function test_create_cluster_pages(string $type): void { - global $DB; + global $CFG, $DB; + require_once($CFG->dirroot . '/mod/lesson/locallib.php'); + require_once($CFG->dirroot . '/mod/lesson/pagetypes/cluster.php'); $this->resetAfterTest(); $this->setAdminUser(); diff --git a/mod/lti/service/gradebookservices/tests/gradebookservices_test.php b/mod/lti/service/gradebookservices/tests/gradebookservices_test.php index 3ac62226fdd..15846ae7382 100644 --- a/mod/lti/service/gradebookservices/tests/gradebookservices_test.php +++ b/mod/lti/service/gradebookservices/tests/gradebookservices_test.php @@ -28,6 +28,13 @@ use ltiservice_gradebookservices\local\service\gradebookservices; * @coversDefaultClass \mod_lti\service\gradebookservices\local\gradebookservices */ class gradebookservices_test extends \advanced_testcase { + /** + * Load the necessary libs for the tests. + */ + public static function setUpBeforeClass(): void { + global $CFG; + require_once($CFG->dirroot . '/mod/lti/locallib.php'); + } /** * @covers ::instance_added @@ -37,9 +44,6 @@ class gradebookservices_test extends \advanced_testcase { * that can be retrieved using the gradebook service API. */ public function test_lti_add_coupled_lineitem() { - global $CFG; - require_once($CFG->dirroot . '/mod/lti/locallib.php'); - $this->resetAfterTest(); $this->setAdminUser(); @@ -75,9 +79,6 @@ class gradebookservices_test extends \advanced_testcase { * that can be retrieved using the gradebook service API. */ public function test_lti_add_coupled_lineitem_default_subreview() { - global $CFG; - require_once($CFG->dirroot . '/mod/lti/locallib.php'); - $this->resetAfterTest(); $this->setAdminUser(); @@ -128,9 +129,6 @@ class gradebookservices_test extends \advanced_testcase { * the line items should be actually passed. */ public function test_get_launch_parameters_coupled() { - global $CFG; - require_once($CFG->dirroot . '/mod/lti/locallib.php'); - $this->resetAfterTest(); $this->setAdminUser(); @@ -162,9 +160,6 @@ class gradebookservices_test extends \advanced_testcase { * launch is submission review. */ public function test_get_launch_parameters_coupled_subreview_override() { - global $CFG; - require_once($CFG->dirroot . '/mod/lti/locallib.php'); - $this->resetAfterTest(); $this->setAdminUser(); @@ -193,9 +188,6 @@ class gradebookservices_test extends \advanced_testcase { * launch is submission review. */ public function test_get_launch_parameters_coupled_subreview_override_default() { - global $CFG; - require_once($CFG->dirroot . '/mod/lti/locallib.php'); - $this->resetAfterTest(); $this->setAdminUser(); @@ -224,9 +216,6 @@ class gradebookservices_test extends \advanced_testcase { * if there is a single line item attached to that lti instance. */ public function test_get_launch_parameters_decoupled() { - global $CFG; - require_once($CFG->dirroot . '/mod/lti/locallib.php'); - $this->resetAfterTest(); $this->setAdminUser(); diff --git a/mod/lti/tests/privacy/provider_test.php b/mod/lti/tests/privacy/provider_test.php index 9a4732bdc4c..ddb36629e2b 100644 --- a/mod/lti/tests/privacy/provider_test.php +++ b/mod/lti/tests/privacy/provider_test.php @@ -30,6 +30,10 @@ use mod_lti\privacy\provider; defined('MOODLE_INTERNAL') || die(); +global $CFG; + +require_once($CFG->dirroot . '/mod/lti/locallib.php'); + /** * Privacy provider tests class. * diff --git a/mod/quiz/tests/backup/restore_39_test.php b/mod/quiz/tests/backup/restore_39_test.php index 91be76fa7c9..4a0c4e0746b 100644 --- a/mod/quiz/tests/backup/restore_39_test.php +++ b/mod/quiz/tests/backup/restore_39_test.php @@ -31,7 +31,9 @@ use restore_controller; final class restore_39_test extends advanced_testcase { public function test_restore_random_question_39(): void { - global $DB, $USER; + global $DB, $CFG, $USER; + + require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); $this->resetAfterTest(); $this->setAdminUser(); diff --git a/question/type/calculated/tests/question_test.php b/question/type/calculated/tests/question_test.php index 44e73c8487f..7cd5545ec4b 100644 --- a/question/type/calculated/tests/question_test.php +++ b/question/type/calculated/tests/question_test.php @@ -269,8 +269,6 @@ class question_test extends \advanced_testcase { $this->resetAfterTest(); - $qtype = new qtype_calculated(); - // Create a question. $q = \test_question_maker::get_question_data('calculated', 'mult'); $q->id = 99; @@ -284,6 +282,7 @@ class question_test extends \advanced_testcase { $units[] = $unit; $DB->insert_records("question_numerical_units", $units); + $qtype = new qtype_calculated(); $qtypeobj = question_bank::get_qtype($qtype->name()); $fakedata = ["a" => "5.7", "b" => "3.3"];