MDL-79843 core: test covering hook events for deprecated plugins

Verifies that listeners are excluded if they are from a deprecated
plugin type.
This commit is contained in:
Jake Dallimore
2025-01-28 14:42:20 +08:00
parent 44b16855e6
commit 26fcb89fd2
2 changed files with 48 additions and 1 deletions
+47
View File
@@ -17,6 +17,7 @@
namespace core\hook;
use core\di;
use core\tests\fake_plugins_test_trait;
/**
* Hooks tests.
@@ -28,6 +29,9 @@ use core\di;
* @covers \core\hook\manager
*/
final class manager_test extends \advanced_testcase {
use fake_plugins_test_trait;
/**
* Test public factory method to get hook manager.
*/
@@ -481,6 +485,49 @@ final class manager_test extends \advanced_testcase {
$this->assertDebuggingNotCalled();
}
/**
* Test verifying that callbacks for deprecated plugins are not returned and hook dispatching won't call into these plugins.
*
* @runInSeparateProcess
* @return void
*/
public function test_get_callbacks_for_hook_deprecated_plugintype(): void {
$this->resetAfterTest();
// Inject the fixture 'fake' plugin type into component sources, which includes a single 'fake_fullfeatured' plugin.
// This 'fake_fullfeatured' plugin is an available plugin at this stage (not yet deprecated).
$this->add_full_mocked_plugintype(
plugintype: 'fake',
path: 'lib/tests/fixtures/fakeplugins/fake',
);
// Force reset the static instance cache \core\hook\manager::$instance so that a fresh instance is instantiated, ensuring
// the component lists are re-run and the hook manager can see the injected mock plugin and it's callbacks.
// Note: we can't use \core\hook\manager::phpunit_get_instance() because that doesn't load in component callbacks from disk.
$hookmanrc = new \ReflectionClass(\core\hook\manager::class);
$hookmanrc->setStaticPropertyValue('instance', null);
$manager = \core\hook\manager::get_instance();
// Get all registered callbacks for the hook listened to by the mock plugin (after_course_created).
$listeners = $manager->get_callbacks_for_hook(\core_course\hook\after_course_created::class);
$componentswithcallbacks = array_column($listeners, 'component');
// Verify the available mock plugin is returned as a listener.
$this->assertContains('fake_fullfeatured', $componentswithcallbacks);
// Deprecate the 'fake' plugin type.
$this->deprecate_full_mocked_plugintype('fake');
// Force a fresh plugin manager instance, again to ensure the up-to-date component lists are used.
$hookmanrc->setStaticPropertyValue('instance', null);
$manager = \core\hook\manager::get_instance();
// And verify the plugin is now not returned as a listener, since it's deprecated.
$listeners = $manager->get_callbacks_for_hook(\core_course\hook\after_course_created::class);
$componentswithcallbacks = array_column($listeners, 'component');
$this->assertNotContains('fake_fullfeatured', $componentswithcallbacks);
}
/**
* Normalise the sort order of callbacks to help with asserts.
*