MDL-81698 phpunit: Apply various fixes towards 1by1 execution

The changes here are heterogeneous:
- Include stuff that is not available (other test has included it).
  Sometimes local to a unit test, others in setupBeforeClass() or
  globally, ... depends on every case.
- Rename some tests (namespaces, test name, ...) towards getting it
  running.
- Amend small bits here and there.

Important note: I've left any "cosmetic" warning out from the
changes, only a few errors (like long array syntax) have been fixed.
This commit is contained in:
Eloy Lafuente (stronk7) 2024-04-26 13:34:29 +02:00
parent 80f521eed0
commit b2a485e244
No known key found for this signature in database
GPG Key ID: 53487A05E6228820
18 changed files with 96 additions and 49 deletions

View File

@ -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.
*

View File

@ -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(

View File

@ -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();

View File

@ -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');
/**

View File

@ -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.

View File

@ -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.

View File

@ -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');

View File

@ -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; $i<CONTEXT_CACHE_MAX_SIZE + 100; $i++) {
context_user::instance($testusers[$i]);
if ($i == CONTEXT_CACHE_MAX_SIZE - 1) {
$this->assertEquals(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;
}
}

View File

@ -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();

View File

@ -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',

View File

@ -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;

View File

@ -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;

View File

@ -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.
*

View File

@ -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();

View File

@ -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();

View File

@ -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.
*

View File

@ -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();

View File

@ -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"];