MDL-82287 core: Deprecate long-deprecated functions

These were originally believed to be so widely used that we could never
migrate away from them but it seems we have!
This commit is contained in:
Andrew Nicols 2024-06-25 21:21:00 +08:00
parent 4101dc6dab
commit 77f9238cf4
No known key found for this signature in database
GPG key ID: 6D1E3157C8CFBF14
9 changed files with 159 additions and 395 deletions

View file

@ -4045,26 +4045,6 @@ class accesslib_test extends advanced_testcase {
context_user::instance($testusers[102]);
$this->assertEquals($prevsize + 1, context_inspection::check_context_cache_size());
unset($testusers);
// Test basic test of legacy functions.
// Note: watch out, the fake site might be pretty borked already.
$this->assertEquals(get_system_context(), context_system::instance());
$this->assertDebuggingCalled('get_system_context() is deprecated, please use context_system::instance() instead.', DEBUG_DEVELOPER);
foreach ($DB->get_records('context') as $contextid => $record) {
$context = context::instance_by_id($contextid);
$this->assertEquals($context, get_context_instance($record->contextlevel, $record->instanceid));
$this->assertDebuggingCalled('get_context_instance() is deprecated, please use context_xxxx::instance() instead.', DEBUG_DEVELOPER);
}
// Make sure a debugging is thrown.
get_context_instance($record->contextlevel, $record->instanceid);
$this->assertDebuggingCalled('get_context_instance() is deprecated, please use context_xxxx::instance() instead.', DEBUG_DEVELOPER);
get_system_context();
$this->assertDebuggingCalled('get_system_context() is deprecated, please use context_system::instance() instead.', DEBUG_DEVELOPER);
}
/**

View file

@ -99,11 +99,16 @@ final class component_test extends advanced_testcase {
$subsystems = core_component::get_core_subsystems();
$this->assertSame($subsystems, get_core_subsystems(true));
$this->assertDebuggingCalled();
$this->resetDebugging();
$realsubsystems = get_core_subsystems();
$this->assertDebuggingCalled();
$this->assertdebuggingcalledcount(2);
$this->resetDebugging();
$this->assertSame($realsubsystems, get_core_subsystems(false));
$this->assertDebuggingCalled();
$this->assertdebuggingcalledcount(2);
$this->resetDebugging();
$this->assertEquals(count($subsystems), count($realsubsystems));
@ -138,10 +143,16 @@ final class component_test extends advanced_testcase {
$plugintypes = core_component::get_plugin_types();
$this->assertSame($plugintypes, get_plugin_types());
$this->assertDebuggingCalled();
$this->resetDebugging();
$this->assertSame($plugintypes, get_plugin_types(true));
$this->assertDebuggingCalled();
$this->resetDebugging();
$realplugintypes = get_plugin_types(false);
$this->assertDebuggingCalled();
$this->assertdebuggingcalledcount(2);
$this->resetDebugging();
foreach ($plugintypes as $plugintype => $fulldir) {
$this->assertSame($fulldir, $CFG->dirroot . '/' . $realplugintypes[$plugintype]);
@ -175,6 +186,8 @@ final class component_test extends advanced_testcase {
foreach ($plugintypes as $plugintype => $fulldir) {
$plugins = core_component::get_plugin_list($plugintype);
$this->assertSame($plugins, get_plugin_list($plugintype));
$this->assertDebuggingCalled();
$this->resetDebugging();
}
}
@ -199,6 +212,8 @@ final class component_test extends advanced_testcase {
core_component::get_plugin_directory($plugintype, $pluginname),
get_plugin_directory($plugintype, $pluginname),
);
$this->assertDebuggingCalled();
$this->resetDebugging();
}
}
}
@ -308,92 +323,83 @@ final class component_test extends advanced_testcase {
);
}
public function test_normalize_component(): void {
// Moodle core.
$this->assertSame(['core', null], core_component::normalize_component('core'));
$this->assertSame(['core', null], core_component::normalize_component('moodle'));
$this->assertSame(['core', null], core_component::normalize_component(''));
// Moodle core subsystems.
$this->assertSame(['core', 'admin'], core_component::normalize_component('admin'));
$this->assertSame(['core', 'admin'], core_component::normalize_component('core_admin'));
$this->assertSame(['core', 'admin'], core_component::normalize_component('moodle_admin'));
// Activity modules and their subplugins.
$this->assertSame(['mod', 'workshop'], core_component::normalize_component('workshop'));
$this->assertSame(['mod', 'workshop'], core_component::normalize_component('mod_workshop'));
$this->assertSame(['workshopform', 'accumulative'], core_component::normalize_component('workshopform_accumulative'));
$this->assertSame(['mod', 'quiz'], core_component::normalize_component('quiz'));
$this->assertSame(['quiz', 'grading'], core_component::normalize_component('quiz_grading'));
$this->assertSame(['mod', 'data'], core_component::normalize_component('data'));
$this->assertSame(['datafield', 'checkbox'], core_component::normalize_component('datafield_checkbox'));
// Other plugin types.
$this->assertSame(['auth', 'mnet'], core_component::normalize_component('auth_mnet'));
$this->assertSame(['enrol', 'self'], core_component::normalize_component('enrol_self'));
$this->assertSame(['block', 'html'], core_component::normalize_component('block_html'));
$this->assertSame(['block', 'mnet_hosts'], core_component::normalize_component('block_mnet_hosts'));
$this->assertSame(['local', 'amos'], core_component::normalize_component('local_amos'));
$this->assertSame(['local', 'admin'], core_component::normalize_component('local_admin'));
// Unknown words without underscore are supposed to be activity modules.
/**
* Test \core_component::normalize_component function.
*
* @dataProvider normalise_component_provider
* @param array $expected
* @param string $args
*/
public function test_normalize_component(array $expected, string $args): void {
$this->assertSame(
['mod', 'whoonearthwouldcomewithsuchastupidnameofcomponent'],
core_component::normalize_component('whoonearthwouldcomewithsuchastupidnameofcomponent')
);
// Module names can not contain underscores, this must be a subplugin.
$this->assertSame(
['whoonearth', 'wouldcomewithsuchastupidnameofcomponent'],
core_component::normalize_component('whoonearth_wouldcomewithsuchastupidnameofcomponent')
);
$this->assertSame(
['whoonearth', 'would_come_withsuchastupidnameofcomponent'],
core_component::normalize_component('whoonearth_would_come_withsuchastupidnameofcomponent')
$expected,
core_component::normalize_component($args),
);
}
public function test_deprecated_normalize_component(): void {
// Moodle core.
$this->assertSame(['core', null], normalize_component('core'));
$this->assertSame(['core', null], normalize_component(''));
$this->assertSame(['core', null], normalize_component('moodle'));
// Moodle core subsystems.
$this->assertSame(['core', 'admin'], normalize_component('admin'));
$this->assertSame(['core', 'admin'], normalize_component('core_admin'));
$this->assertSame(['core', 'admin'], normalize_component('moodle_admin'));
// Activity modules and their subplugins.
$this->assertSame(['mod', 'workshop'], normalize_component('workshop'));
$this->assertSame(['mod', 'workshop'], normalize_component('mod_workshop'));
$this->assertSame(['workshopform', 'accumulative'], normalize_component('workshopform_accumulative'));
$this->assertSame(['mod', 'quiz'], normalize_component('quiz'));
$this->assertSame(['quiz', 'grading'], normalize_component('quiz_grading'));
$this->assertSame(['mod', 'data'], normalize_component('data'));
$this->assertSame(['datafield', 'checkbox'], normalize_component('datafield_checkbox'));
// Other plugin types.
$this->assertSame(['auth', 'mnet'], normalize_component('auth_mnet'));
$this->assertSame(['enrol', 'self'], normalize_component('enrol_self'));
$this->assertSame(['block', 'html'], normalize_component('block_html'));
$this->assertSame(['block', 'mnet_hosts'], normalize_component('block_mnet_hosts'));
$this->assertSame(['local', 'amos'], normalize_component('local_amos'));
$this->assertSame(['local', 'admin'], normalize_component('local_admin'));
// Unknown words without underscore are supposed to be activity modules.
/**
* Test the deprecated normalize_component function.
*
* @dataProvider normalise_component_provider
* @param array $expected
* @param string $args
*/
public function test_deprecated_normalize_component(array $expected, string $args): void {
$this->assertSame(
['mod', 'whoonearthwouldcomewithsuchastupidnameofcomponent'],
normalize_component('whoonearthwouldcomewithsuchastupidnameofcomponent')
);
// Module names can not contain underscores, this must be a subplugin.
$this->assertSame(
['whoonearth', 'wouldcomewithsuchastupidnameofcomponent'],
normalize_component('whoonearth_wouldcomewithsuchastupidnameofcomponent')
);
$this->assertSame(
['whoonearth', 'would_come_withsuchastupidnameofcomponent'],
normalize_component('whoonearth_would_come_withsuchastupidnameofcomponent')
$expected,
normalize_component($args),
);
$this->assertDebuggingCalled();
}
/**
* Data provider for the normalize_component function.
*/
public static function normalise_component_provider(): array {
return [
// Moodle core.
[['core', null], 'core'],
[['core', null], ''],
[['core', null], 'moodle'],
// Moodle core subsystems.
[['core', 'admin'], 'admin'],
[['core', 'admin'], 'core_admin'],
[['core', 'admin'], 'moodle_admin'],
// Activity modules and their subplugins.
[['mod', 'workshop'], 'workshop'],
[['mod', 'workshop'], 'mod_workshop'],
[['workshopform', 'accumulative'], 'workshopform_accumulative'],
[['mod', 'quiz'], 'quiz'],
[['quiz', 'grading'], 'quiz_grading'],
[['mod', 'data'], 'data'],
[['datafield', 'checkbox'], 'datafield_checkbox'],
// Other plugin types.
[['auth', 'mnet'], 'auth_mnet'],
[['enrol', 'self'], 'enrol_self'],
[['block', 'html'], 'block_html'],
[['block', 'mnet_hosts'], 'block_mnet_hosts'],
[['local', 'amos'], 'local_amos'],
[['local', 'admin'], 'local_admin'],
// Unknown words without underscore are supposed to be activity modules.
[
['mod', 'whoonearthwouldcomewithsuchastupidnameofcomponent'],
'whoonearthwouldcomewithsuchastupidnameofcomponent',
],
// Module names can not contain underscores, this must be a subplugin.
[
['whoonearth', 'wouldcomewithsuchastupidnameofcomponent'],
'whoonearth_wouldcomewithsuchastupidnameofcomponent',
],
[
['whoonearth', 'would_come_withsuchastupidnameofcomponent'],
'whoonearth_would_come_withsuchastupidnameofcomponent',
],
];
}
public function test_get_component_directory(): void {
@ -484,12 +490,16 @@ final class component_test extends advanced_testcase {
$plugins = core_component::get_plugin_list($plugintype);
foreach ($plugins as $pluginname => $plugindir) {
$this->assertSame($plugindir, get_component_directory(($plugintype . '_' . $pluginname)));
$this->assertDebuggingCalled();
$this->resetDebugging();
}
}
$subsystems = core_component::get_core_subsystems();
foreach ($subsystems as $subsystem => $fulldir) {
$this->assertSame($fulldir, get_component_directory(('core_' . $subsystem)));
$this->assertDebuggingCalled();
$this->resetDebugging();
}
}

View file

@ -1,51 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/csslib.php');
/**
* CSS optimiser test class.
*
* @package core
* @category test
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class csslib_test extends \advanced_testcase {
/**
* Test that css_is_colour function throws an exception.
*/
public function test_css_is_colour(): void {
$this->expectException('coding_exception');
$this->expectExceptionMessage('css_is_colour() can not be used anymore.');
css_is_colour();
}
/**
* Test that css_is_width function throws an exception.
*/
public function test_css_is_width(): void {
$this->expectException('coding_exception');
$this->expectExceptionMessage('css_is_width() can not be used anymore.');
css_is_width();
}
}

View file

@ -573,56 +573,6 @@ class base_test extends \advanced_testcase {
$this->assertSame($event::LEVEL_TEACHING, $event->edulevel);
}
public function test_legacy(): void {
global $DB, $CFG;
$this->resetAfterTest(true);
$observers = array(
array(
'eventname' => '\core_tests\event\unittest_executed',
'callback' => '\core_tests\event\unittest_observer::observe_one',
),
array(
'eventname' => '*',
'callback' => '\core_tests\event\unittest_observer::observe_all',
'includefile' => null,
'internal' => 1,
'priority' => 9999,
),
);
$DB->delete_records('log', array());
$this->expectException(\coding_exception::class);
events_update_definition('unittest');
$DB->delete_records_select('events_handlers', "component <> 'unittest'");
$this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
$this->assertEquals(3, $DB->count_records('events_handlers'));
set_config('loglifetime', 60*60*24*5);
\core\event\manager::phpunit_replace_observers($observers);
\core_tests\event\unittest_observer::reset();
$event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
$event1->trigger();
$event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>6, 'xx'=>11)));
$event2->nest = true;
$event2->trigger();
$this->assertSame(
array('observe_all-5', 'observe_one-5', 'observe_all-nesting-6', 'observe_one-6', 'observe_all-666', 'observe_one-666'),
\core_tests\event\unittest_observer::$info);
$this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
$this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
$logs = $DB->get_records('log', array(), 'id ASC');
$this->assertCount(0, $logs);
}
public function test_restore_event(): void {
$event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
$data1 = $event1->get_data();