MDL-81456 core: Run tests modifying core_component in a separate process

This commit is contained in:
Andrew Nicols
2024-04-08 09:36:21 +08:00
parent fa2e11105a
commit d7e1f2cef7
2 changed files with 69 additions and 44 deletions
+62
View File
@@ -768,4 +768,66 @@ abstract class advanced_testcase extends base_testcase {
return $clock;
}
/**
* Add a mocked plugintype to Moodle.
*
* A new plugintype name must be provided with a path to the plugintype's root.
*
* Please note that tests calling this method must be run in separate isolation mode.
* Please avoid using this if at all possible.
*
* @param string $plugintype The name of the plugintype
* @param string $path The path to the plugintype's root
*/
protected function add_mocked_plugintype(
string $plugintype,
string $path,
): void {
require_phpunit_isolation();
$mockedcomponent = new \ReflectionClass(\core_component::class);
$plugintypes = $mockedcomponent->getStaticPropertyValue('plugintypes');
if (array_key_exists($plugintype, $plugintypes)) {
throw new \coding_exception("The plugintype '{$plugintype}' already exists.");
}
$plugintypes[$plugintype] = $path;
$mockedcomponent->setStaticPropertyValue('plugintypes', $plugintypes);
$this->resetDebugging();
}
/**
* Add a mocked plugin to Moodle.
*
* A new plugin name must be provided with a path to the plugin's root.
* The plugin type must already exist (or have been mocked separately).
*
* Please note that tests calling this method must be run in separate isolation mode.
* Please avoid using this if at all possible.
*
* @param string $plugintype The name of the plugintype
* @param string $pluginname The name of the plugin
* @param string $path The path to the plugin's root
*/
protected function add_mocked_plugin(
string $plugintype,
string $pluginname,
string $path,
): void {
require_phpunit_isolation();
$mockedcomponent = new \ReflectionClass(\core_component::class);
$plugins = $mockedcomponent->getStaticPropertyValue('plugins');
if (!array_key_exists($plugintype, $plugins)) {
$plugins[$plugintype] = [];
}
$plugins[$plugintype][$pluginname] = $path;
$mockedcomponent->setStaticPropertyValue('plugins', $plugins);
$this->resetDebugging();
}
}