This commit is contained in:
Huong Nguyen 2024-12-16 10:07:57 +07:00
commit 860bb57497
No known key found for this signature in database
GPG Key ID: 40D88AB693A3E72A
4 changed files with 156 additions and 31 deletions

View File

@ -0,0 +1,7 @@
issueNumber: MDL-83703
notes:
core:
- message: >
Support for `subplugins.php` files has been removed. All subplugin
metadata must be created in a `subplugins.json` file.
type: removed

View File

@ -1,28 +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/>.
/**
* Definition of MFA sub-plugins (factors).
*
* @package tool_mfa
* @author Mikhail Golenkov <golenkovm@gmail.com>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$subplugins = (array) json_decode(file_get_contents($CFG->dirroot."/admin/tool/mfa/db/subplugins.json"))->plugintypes;

View File

@ -754,9 +754,10 @@ $cache = ' . var_export($cache, true) . ';
error_log("$ownerdir/db/subplugins.json is invalid ($jsonerror)");
}
} else if (file_exists("$ownerdir/db/subplugins.php")) {
error_log('Use of subplugins.php has been deprecated. ' .
"Please update your '$ownerdir' plugin to provide a subplugins.json file instead.");
include("$ownerdir/db/subplugins.php");
throw new coding_exception(
'Use of subplugins.php has been deprecated and is no longer supported. ' .
"Please update your '$ownerdir' plugin to provide a subplugins.json file instead.",
);
}
foreach ($subplugins as $subtype => $dir) {

View File

@ -16,6 +16,7 @@
namespace core;
use core\exception\coding_exception;
use DirectoryIterator;
use ReflectionClass;
use ReflectionProperty;
@ -36,6 +37,7 @@ final class component_test extends \advanced_testcase {
parent::tearDown();
component::reset();
ini_set('error_log', null);
}
/**
@ -1382,4 +1384,147 @@ final class component_test extends \advanced_testcase {
}
}
}
/**
* Test that fetching of subtype data throws an exception when a subplugins.php is present without a json equivalent.
*/
public function test_fetch_subtypes_php_only(): void {
$vfileroot = \org\bovigo\vfs\vfsStream::setup('root', null, [
'plugintype' => [
'exampleplugin' => [
'db' => [
'subplugins.php' => '',
],
],
],
]);
$this->expectException(coding_exception::class);
$this->expectExceptionMessageMatches('/Use of subplugins.php has been deprecated and is no longer supported/');
$pluginroot = $vfileroot->getChild('plugintype/exampleplugin');
$rcm = new \ReflectionMethod(\core\component::class, 'fetch_subtypes');
$rcm->invoke(null, $pluginroot->url());
}
/**
* Test that fetching of subtype data does not throw an exception when a subplugins.php is present
* with a json file equivalent.
*
* Note: The content of the php file is irrelevant and we no longer use it anyway.
*/
public function test_fetch_subtypes_php_and_json(): void {
global $CFG;
$this->resetAfterTest();
$vfileroot = \org\bovigo\vfs\vfsStream::setup('root', null, [
'plugintype' => [
'exampleplugin' => [
'db' => [
'subplugins.json' => json_encode([
'plugintypes' => [
'exampleplugina' => 'plugintype/exampleplugin/apples',
],
]),
'subplugins.php' => '',
],
'apples' => [],
],
],
]);
$CFG->dirroot = $vfileroot->url();
$pluginroot = $vfileroot->getChild('plugintype/exampleplugin');
$rcm = new \ReflectionMethod(\core\component::class, 'fetch_subtypes');
$subplugins = $rcm->invoke(null, $pluginroot->url());
$this->assertEquals([
'exampleplugina' => $pluginroot->getChild('apples')->url(),
], $subplugins);
}
/**
* Test that fetching of subtype data throws appropriate exceptions when a subplugins.php is present
* with a json file equivalent.
*
* Note: The content of the php file is irrelevant and we no longer use it anyway.
*
* @dataProvider invalid_subplugins_json_provider
* @param string[] $expectedwarnings Errors to expect in the exception message
* @param array[] $plugintypesdir The contents of the subplugins.json file
*/
public function test_fetch_subtypes_json_invalid_values(
array $expectedwarnings,
array $plugintypesdir,
): void {
global $CFG;
$this->resetAfterTest();
$vfileroot = \org\bovigo\vfs\vfsStream::setup('root', null, [
'plugintype' => [
'exampleplugin' => [
'db' => [
'subplugins.json' => json_encode([
'plugintypes' => $plugintypesdir,
]),
'subplugins.php' => '',
],
'apples' => [],
],
],
]);
$CFG->dirroot = $vfileroot->url();
$pluginroot = $vfileroot->getChild('plugintype/exampleplugin');
$logdir = make_request_directory();
$logfile = "{$logdir}/error.log";
ini_set('error_log', $logfile);
$rcm = new \ReflectionMethod(\core\component::class, 'fetch_subtypes');
$rcm->invoke(null, $pluginroot->url());
$warnings = file_get_contents($logfile);
foreach ($expectedwarnings as $expectedwarning) {
$this->assertMatchesRegularExpression($expectedwarning, $warnings);
}
}
/**
* Data provider for invalid subplugins.json files.
*
* @return array
*/
public static function invalid_subplugins_json_provider(): array {
return [
'Invalid characters in subtype name' => [
'expectedwarnings' => [
"/Invalid subtype .*APPLES.*detected.*invalid characters present/",
],
'plugintypesdir' => [
'APPLES' => 'plugintype/exampleplugin/APPLES',
],
],
'Subplugin which duplicates a core subsystem' => [
'expectedwarnings' => [
"/Invalid subtype .*editor.*detected.*duplicates core subsystem/",
],
'plugintypesdir' => [
'editor' => 'plugintype/exampleplugin/apples',
],
],
'Subplugin directory does not exist' => [
'expectedwarnings' => [
"/Invalid subtype directory/",
],
'plugintypesdir' => [
'exampleapples' => 'plugintype/exampleplugin/pears',
],
],
];
}
}