MDL-82110 core: Remove debugging for unmigrated hooks in unit tests

This commit is contained in:
Andrew Nicols 2024-08-15 13:10:58 +08:00
parent 6cd55074c7
commit 4e4a6c6580
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
3 changed files with 49 additions and 9 deletions

View File

@ -59,8 +59,13 @@ final class manager implements
/**
* Constructor can be used only from factory methods.
*
* @param bool $phpunit Whether this is a PHPUnit instantiated instance
*/
private function __construct() {
private function __construct(
/** @var bool Whether this is a PHPUnit instantiated instance */
private bool $phpunit = false,
) {
}
/**
@ -89,11 +94,28 @@ final class manager implements
if (!PHPUNIT_TEST) {
throw new \coding_exception('Invalid call of manager::phpunit_get_instance() outside of tests');
}
$instance = new self();
$instance = new self(
phpunit: true,
);
$instance->load_callbacks($componentfiles);
return $instance;
}
/**
* Whether to warn when an unmigrated legacy hook is found.
*
* @return bool
*/
public function warn_on_unmigrated_legacy_hooks(): bool {
if ($this->phpunit) {
// This is an empty instance used in PHPUnit tests.
// It does not know of any migrated hooks.
return false;
}
return true;
}
/**
* Override hook callbacks for testing purposes.
*

View File

@ -7206,11 +7206,12 @@ function get_plugins_with_function($function, $file = 'lib.php', $include = true
foreach ($pluginfunctions as $plugintype => $plugins) {
foreach ($plugins as $plugin => $unusedfunction) {
$component = $plugintype . '_' . $plugin;
if ($hooks = di::get(hook\manager::class)->get_hooks_deprecating_plugin_callback($plugincallback)) {
if (di::get(hook\manager::class)->is_deprecating_hook_present($component, $plugincallback)) {
$hookmanager = di::get(hook\manager::class);
if ($hooks = $hookmanager->get_hooks_deprecating_plugin_callback($plugincallback)) {
if ($hookmanager->is_deprecating_hook_present($component, $plugincallback)) {
// Ignore the old callback, it is there only for older Moodle versions.
unset($pluginfunctions[$plugintype][$plugin]);
} else {
} else if ($hookmanager->warn_on_unmigrated_legacy_hooks()) {
$hookmessage = count($hooks) == 1 ? reset($hooks) : 'one of ' . implode(', ', $hooks);
debugging(
"Callback $plugincallback in $component component should be migrated to new " .
@ -7451,7 +7452,7 @@ function component_callback($component, $function, array $params = array(), $def
// Do not call the old lib.php callback,
// it is there for compatibility with older Moodle versions only.
return null;
} else {
} else if ($hookmanager->warn_on_unmigrated_legacy_hooks()) {
$hookmessage = count($hooks) == 1 ? reset($hooks) : 'one of ' . implode(', ', $hooks);
debugging(
"Callback $function in $component component should be migrated to new hook callback for $hookmessage",
@ -7553,7 +7554,7 @@ function component_class_callback($classname, $methodname, array $params, $defau
// Do not call the old class callback,
// it is there for compatibility with older Moodle versions only.
return null;
} else {
} else if ($hookmanager->warn_on_unmigrated_legacy_hooks()) {
$hookmessage = count($hooks) == 1 ? reset($hooks) : 'one of ' . implode(', ', $hooks);
debugging("Callback $callback in $component component should be migrated to new hook callback for $hookmessage",
DEBUG_DEVELOPER);

View File

@ -348,9 +348,18 @@ final class manager_test extends \advanced_testcase {
'Called deprecated callback',
component_callback('fake_hooktest', 'old_callback', [], null, true)
);
$this->assertDebuggingNotCalled();
// Forcefully modify the PHPUnit flag on the manager to ensure the debugging message is output.
$manager = di::get(manager::class);
$rp = new \ReflectionProperty($manager, 'phpunit');
$rp->setValue($manager, false);
component_callback('fake_hooktest', 'old_callback', [], null, true);
$this->assertDebuggingCalled(
'Callback old_callback in fake_hooktest component should be migrated to new hook ' .
'callback for fake_hooktest\hook\hook_replacing_callback'
'callback for fake_hooktest\hook\hook_replacing_callback',
);
}
@ -421,9 +430,17 @@ final class manager_test extends \advanced_testcase {
'Called deprecated class callback',
component_class_callback('fake_hooktest\callbacks', 'old_class_callback', [], null, true)
);
$this->assertDebuggingNotCalled();
// Forcefully modify the PHPUnit flag on the manager to ensure the debugging message is output.
$manager = di::get(manager::class);
$rp = new \ReflectionProperty($manager, 'phpunit');
$rp->setValue($manager, false);
component_class_callback('fake_hooktest\callbacks', 'old_class_callback', [], null, true);
$this->assertDebuggingCalled(
'Callback callbacks::old_class_callback in fake_hooktest component should be migrated to new hook ' .
'callback for fake_hooktest\hook\hook_replacing_class_callback'
'callback for fake_hooktest\hook\hook_replacing_class_callback',
);
}