From 995c5489b7361b992bf29140a7fad35cdae3bdb0 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Thu, 25 Jul 2024 12:26:46 +0800 Subject: [PATCH] MDL-79843 core_adminpresets: skip deprecated plugin types during apply Presets are stored in the DB and may contain config for plugins which have since been deprecated. This just makes sure these are skipped when trying to apply the preset. --- admin/presets/classes/manager.php | 5 ++++ admin/presets/tests/manager_test.php | 37 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/admin/presets/classes/manager.php b/admin/presets/classes/manager.php index 5b353ed0258..6719ab77d2c 100644 --- a/admin/presets/classes/manager.php +++ b/admin/presets/classes/manager.php @@ -1133,6 +1133,11 @@ class manager { 'visiblevalue' => $visiblevalue, ]; + if ($plugininfo && $plugininfo->is_deprecated()) { + $skipped[] = $data; + continue; + } + if ($pluginclass == '\core\plugininfo\orphaned') { $skipped[] = $data; continue; diff --git a/admin/presets/tests/manager_test.php b/admin/presets/tests/manager_test.php index 2d97144c004..60389369c3d 100644 --- a/admin/presets/tests/manager_test.php +++ b/admin/presets/tests/manager_test.php @@ -809,4 +809,41 @@ final class manager_test extends \advanced_testcase { $enabledplugins = \core\plugininfo\qtype::get_enabled_plugins(); $this->assertArrayHasKey('truefalse', $enabledplugins); } + + /** + * Test apply_preset for a deprecated plugin type. + * + * @runInSeparateProcess + * + * @covers ::apply_preset + * @return void + */ + public function test_apply_preset_deprecated_plugintype(): void { + global $CFG; + $this->resetAfterTest(); + + // Create a preset. + $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets'); + $presetid = $generator->create_preset(); + + // Add a mock plugin into the preset. This plugin will be mocked and deprecated below. + helper::add_plugin($presetid, 'fake', 'fullfeatured', true); + + // Inject the mock plugin type + plugin, and deprecate it. + $this->add_mocked_plugintype('fake', "$CFG->libdir/tests/fixtures/fakeplugins/fake", true); + $this->add_mocked_plugin('fake', 'fullfeatured', $CFG->libdir . '/tests/fixtures/fakeplugins/fake/fullfeatured'); + + // Pre-flight check to ensure that the plugin is deprecated. + $pluginman = \core\plugin_manager::instance(); + $plugininfo = $pluginman->get_plugin_info('fake_fullfeatured'); + // Expected, unit-test-only debugging, since \core\plugin_manager isn't aware of the mock plugininfo class for this type. + $this->assertDebuggingCalled(); + $this->assertTrue($plugininfo->is_deprecated()); + + // Finally, check apply_preset method, verifying the deprecated plugin is skipped. + $manager = new manager(); + [$applied, $skipped] = $manager->apply_preset($presetid); + $this->assertdebuggingcalledcount(2); // Expected unit-test-only debugging, as above. + $this->assertContains('fake', array_column($skipped, 'plugin')); + } }