mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
MDL-79697 core: Coding style updates
This commit is contained in:
parent
db556a4ce8
commit
846892ab07
@ -22,22 +22,24 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
// Constants used in version.php files, these must exist when core_component executes.
|
||||
|
||||
// We make use of error_log as debugging is not always available.
|
||||
// phpcs:disable moodle.PHP.ForbiddenFunctions.FoundWithAlternative
|
||||
// We make use of empty if statements to make complex decisions clearer.
|
||||
// phpcs:disable Generic.CodeAnalysis.EmptyStatement.DetectedIf
|
||||
|
||||
/** Software maturity level - internals can be tested using white box techniques. */
|
||||
define('MATURITY_ALPHA', 50);
|
||||
define('MATURITY_ALPHA', 50);
|
||||
/** Software maturity level - feature complete, ready for preview and testing. */
|
||||
define('MATURITY_BETA', 100);
|
||||
define('MATURITY_BETA', 100);
|
||||
/** Software maturity level - tested, will be released unless there are fatal bugs. */
|
||||
define('MATURITY_RC', 150);
|
||||
define('MATURITY_RC', 150);
|
||||
/** Software maturity level - ready for production deployment. */
|
||||
define('MATURITY_STABLE', 200);
|
||||
define('MATURITY_STABLE', 200);
|
||||
/** Any version - special value that can be used in $plugin->dependencies in version.php files. */
|
||||
define('ANY_VERSION', 'any');
|
||||
|
||||
|
||||
/**
|
||||
* Collection of components related methods.
|
||||
*/
|
||||
@ -58,7 +60,7 @@ class core_component {
|
||||
'yui' => true,
|
||||
];
|
||||
/** @var array list plugin types that support subplugins, do not add more here unless absolutely necessary */
|
||||
protected static $supportsubplugins = array('mod', 'editor', 'tool', 'local');
|
||||
protected static $supportsubplugins = ['mod', 'editor', 'tool', 'local'];
|
||||
|
||||
/** @var object JSON source of the component data */
|
||||
protected static $componentsource = null;
|
||||
@ -83,13 +85,13 @@ class core_component {
|
||||
/** @var int|float core version. */
|
||||
protected static $version = null;
|
||||
/** @var array list of the files to map. */
|
||||
protected static $filestomap = array('lib.php', 'settings.php');
|
||||
protected static $filestomap = ['lib.php', 'settings.php'];
|
||||
/** @var array associative array of PSR-0 namespaces and corresponding paths. */
|
||||
protected static $psr0namespaces = array(
|
||||
protected static $psr0namespaces = [
|
||||
'Horde' => 'lib/horde/framework/Horde',
|
||||
'Mustache' => 'lib/mustache/src/Mustache',
|
||||
'CFPropertyList' => 'lib/plist/classes/CFPropertyList',
|
||||
);
|
||||
];
|
||||
/** @var array<string|array<string>> associative array of PRS-4 namespaces and corresponding paths. */
|
||||
protected static $psr4namespaces = [
|
||||
'MaxMind' => 'lib/maxmind/MaxMind',
|
||||
@ -192,7 +194,7 @@ class core_component {
|
||||
|
||||
// Iterate through each PSR-0 namespace prefix.
|
||||
foreach (self::$psr0namespaces as $prefix => $path) {
|
||||
$file = self::get_class_file($class, $prefix, $path, array('\\', '_'));
|
||||
$file = self::get_class_file($class, $prefix, $path, ['\\', '_']);
|
||||
if (!empty($file) && file_exists($file)) {
|
||||
return $file;
|
||||
}
|
||||
@ -246,7 +248,7 @@ class core_component {
|
||||
return;
|
||||
}
|
||||
|
||||
if (defined('IGNORE_COMPONENT_CACHE') and IGNORE_COMPONENT_CACHE) {
|
||||
if (defined('IGNORE_COMPONENT_CACHE') && IGNORE_COMPONENT_CACHE) {
|
||||
self::fill_all_caches();
|
||||
return;
|
||||
}
|
||||
@ -264,7 +266,7 @@ class core_component {
|
||||
}
|
||||
return;
|
||||
}
|
||||
$cache = array();
|
||||
$cache = [];
|
||||
include($cachefile);
|
||||
self::$plugintypes = $cache['plugintypes'];
|
||||
self::$plugins = $cache['plugins'];
|
||||
@ -279,18 +281,20 @@ class core_component {
|
||||
}
|
||||
|
||||
if (!is_writable(dirname($cachefile))) {
|
||||
die('Can not create alternative component cache file defined in $CFG->alternative_component_cache, can not continue');
|
||||
die(
|
||||
'Can not create alternative component cache file defined in ' .
|
||||
'$CFG->alternative_component_cache, can not continue'
|
||||
);
|
||||
}
|
||||
|
||||
// Lets try to create the file, it might be in some writable directory or a local cache dir.
|
||||
|
||||
} else {
|
||||
// Note: $CFG->cachedir MUST be shared by all servers in a cluster,
|
||||
// use $CFG->alternative_component_cache if you do not like it.
|
||||
// use $CFG->alternative_component_cache if you do not like it.
|
||||
$cachefile = "$CFG->cachedir/core_component.php";
|
||||
}
|
||||
|
||||
if (!CACHE_DISABLE_ALL and !self::is_developer()) {
|
||||
if (!CACHE_DISABLE_ALL && !self::is_developer()) {
|
||||
// 1/ Use the cache only outside of install and upgrade.
|
||||
// 2/ Let developers add/remove classes in developer mode.
|
||||
if (is_readable($cachefile)) {
|
||||
@ -304,6 +308,7 @@ class core_component {
|
||||
// Outdated cache. We trigger an error log to track an eventual repetitive failure of float comparison.
|
||||
error_log('Resetting core_component cache after core upgrade to version ' . self::fetch_core_version());
|
||||
} else if ($cache['plugintypes']['mod'] !== "$CFG->dirroot/mod") {
|
||||
// phpcs:ignore moodle.Commenting.InlineComment.NotCapital
|
||||
// $CFG->dirroot was changed.
|
||||
} else {
|
||||
// The cache looks ok, let's use it.
|
||||
@ -319,7 +324,7 @@ class core_component {
|
||||
return;
|
||||
}
|
||||
// Note: we do not verify $CFG->admin here intentionally,
|
||||
// they must visit admin/index.php after any change.
|
||||
// they must visit admin/index.php after any change.
|
||||
}
|
||||
}
|
||||
|
||||
@ -345,13 +350,13 @@ class core_component {
|
||||
mkdir($cachedir, $dirpermissions, true);
|
||||
}
|
||||
|
||||
if ($fp = @fopen($cachefile.'.tmp', 'xb')) {
|
||||
if ($fp = @fopen($cachefile . '.tmp', 'xb')) {
|
||||
fwrite($fp, $content);
|
||||
fclose($fp);
|
||||
@rename($cachefile.'.tmp', $cachefile);
|
||||
@rename($cachefile . '.tmp', $cachefile);
|
||||
@chmod($cachefile, $filepermissions);
|
||||
}
|
||||
@unlink($cachefile.'.tmp'); // Just in case anything fails (race condition).
|
||||
@unlink($cachefile . '.tmp'); // Just in case anything fails (race condition).
|
||||
self::invalidate_opcode_php_cache($cachefile);
|
||||
}
|
||||
}
|
||||
@ -374,7 +379,7 @@ class core_component {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($debug & E_ALL and $debug & E_STRICT) {
|
||||
if ($debug & E_ALL && $debug & E_STRICT) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -393,7 +398,7 @@ class core_component {
|
||||
self::fill_all_caches();
|
||||
}
|
||||
|
||||
$cache = array(
|
||||
$cache = [
|
||||
'subsystems' => self::$subsystems,
|
||||
'plugintypes' => self::$plugintypes,
|
||||
'plugins' => self::$plugins,
|
||||
@ -404,10 +409,10 @@ class core_component {
|
||||
'classmaprenames' => self::$classmaprenames,
|
||||
'filemap' => self::$filemap,
|
||||
'version' => self::$version,
|
||||
);
|
||||
];
|
||||
|
||||
return '<?php
|
||||
$cache = '.var_export($cache, true).';
|
||||
$cache = ' . var_export($cache, true) . ';
|
||||
';
|
||||
}
|
||||
|
||||
@ -417,9 +422,9 @@ $cache = '.var_export($cache, true).';
|
||||
protected static function fill_all_caches() {
|
||||
self::$subsystems = self::fetch_subsystems();
|
||||
|
||||
list(self::$plugintypes, self::$parents, self::$subplugins) = self::fetch_plugintypes();
|
||||
[self::$plugintypes, self::$parents, self::$subplugins] = self::fetch_plugintypes();
|
||||
|
||||
self::$plugins = array();
|
||||
self::$plugins = [];
|
||||
foreach (self::$plugintypes as $type => $fulldir) {
|
||||
self::$plugins[$type] = self::fetch_plugins($type, $fulldir);
|
||||
}
|
||||
@ -499,13 +504,13 @@ $cache = '.var_export($cache, true).';
|
||||
$types[$plugintype] = "{$CFG->dirroot}/{$path}";
|
||||
}
|
||||
|
||||
$parents = array();
|
||||
$subplugins = array();
|
||||
$parents = [];
|
||||
$subplugins = [];
|
||||
|
||||
if (!empty($CFG->themedir) and is_dir($CFG->themedir) ) {
|
||||
if (!empty($CFG->themedir) && is_dir($CFG->themedir)) {
|
||||
$types['theme'] = $CFG->themedir;
|
||||
} else {
|
||||
$types['theme'] = $CFG->dirroot.'/theme';
|
||||
$types['theme'] = $CFG->dirroot . '/theme';
|
||||
}
|
||||
|
||||
foreach (self::$supportsubplugins as $type) {
|
||||
@ -519,20 +524,20 @@ $cache = '.var_export($cache, true).';
|
||||
if (!$subtypes) {
|
||||
continue;
|
||||
}
|
||||
$subplugins[$type.'_'.$plugin] = array();
|
||||
foreach($subtypes as $subtype => $subdir) {
|
||||
$subplugins[$type . '_' . $plugin] = [];
|
||||
foreach ($subtypes as $subtype => $subdir) {
|
||||
if (isset($types[$subtype])) {
|
||||
error_log("Invalid subtype '$subtype', duplicate detected.");
|
||||
continue;
|
||||
}
|
||||
$types[$subtype] = $subdir;
|
||||
$parents[$subtype] = $type.'_'.$plugin;
|
||||
$subplugins[$type.'_'.$plugin][$subtype] = array_keys(self::fetch_plugins($subtype, $subdir));
|
||||
$parents[$subtype] = $type . '_' . $plugin;
|
||||
$subplugins[$type . '_' . $plugin][$subtype] = array_keys(self::fetch_plugins($subtype, $subdir));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Local is always last!
|
||||
$types['local'] = $CFG->dirroot.'/local';
|
||||
$types['local'] = $CFG->dirroot . '/local';
|
||||
|
||||
if (in_array('local', self::$supportsubplugins)) {
|
||||
$type = 'local';
|
||||
@ -542,20 +547,20 @@ $cache = '.var_export($cache, true).';
|
||||
if (!$subtypes) {
|
||||
continue;
|
||||
}
|
||||
$subplugins[$type.'_'.$plugin] = array();
|
||||
foreach($subtypes as $subtype => $subdir) {
|
||||
$subplugins[$type . '_' . $plugin] = [];
|
||||
foreach ($subtypes as $subtype => $subdir) {
|
||||
if (isset($types[$subtype])) {
|
||||
error_log("Invalid subtype '$subtype', duplicate detected.");
|
||||
continue;
|
||||
}
|
||||
$types[$subtype] = $subdir;
|
||||
$parents[$subtype] = $type.'_'.$plugin;
|
||||
$subplugins[$type.'_'.$plugin][$subtype] = array_keys(self::fetch_plugins($subtype, $subdir));
|
||||
$parents[$subtype] = $type . '_' . $plugin;
|
||||
$subplugins[$type . '_' . $plugin][$subtype] = array_keys(self::fetch_plugins($subtype, $subdir));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array($types, $parents, $subplugins);
|
||||
return [$types, $parents, $subplugins];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -579,8 +584,8 @@ $cache = '.var_export($cache, true).';
|
||||
protected static function fetch_subtypes($ownerdir) {
|
||||
global $CFG;
|
||||
|
||||
$types = array();
|
||||
$subplugins = array();
|
||||
$types = [];
|
||||
$subplugins = [];
|
||||
if (file_exists("$ownerdir/db/subplugins.json")) {
|
||||
$subplugins = [];
|
||||
$subpluginsjson = json_decode(file_get_contents("$ownerdir/db/subplugins.json"));
|
||||
@ -609,7 +614,7 @@ $cache = '.var_export($cache, true).';
|
||||
error_log("Invalid subtype '$subtype'' detected in '$ownerdir', duplicates core subsystem.");
|
||||
continue;
|
||||
}
|
||||
if ($CFG->admin !== 'admin' and strpos($dir, 'admin/') === 0) {
|
||||
if ($CFG->admin !== 'admin' && strpos($dir, 'admin/') === 0) {
|
||||
$dir = preg_replace('|^admin/|', "$CFG->admin/", $dir);
|
||||
}
|
||||
if (!is_dir("$CFG->dirroot/$dir")) {
|
||||
@ -633,13 +638,13 @@ $cache = '.var_export($cache, true).';
|
||||
|
||||
$fulldirs = (array)$fulldir;
|
||||
if ($plugintype === 'theme') {
|
||||
if (realpath($fulldir) !== realpath($CFG->dirroot.'/theme')) {
|
||||
if (realpath($fulldir) !== realpath($CFG->dirroot . '/theme')) {
|
||||
// Include themes in standard location too.
|
||||
array_unshift($fulldirs, $CFG->dirroot.'/theme');
|
||||
array_unshift($fulldirs, $CFG->dirroot . '/theme');
|
||||
}
|
||||
}
|
||||
|
||||
$result = array();
|
||||
$result = [];
|
||||
|
||||
foreach ($fulldirs as $fulldir) {
|
||||
if (!is_dir($fulldir)) {
|
||||
@ -647,11 +652,11 @@ $cache = '.var_export($cache, true).';
|
||||
}
|
||||
$items = new \DirectoryIterator($fulldir);
|
||||
foreach ($items as $item) {
|
||||
if ($item->isDot() or !$item->isDir()) {
|
||||
if ($item->isDot() || !$item->isDir()) {
|
||||
continue;
|
||||
}
|
||||
$pluginname = $item->getFilename();
|
||||
if ($plugintype === 'auth' and $pluginname === 'db') {
|
||||
if ($plugintype === 'auth' && $pluginname === 'db') {
|
||||
// Special exception for this wrong plugin name.
|
||||
} else if (isset(self::$ignoreddirs[$pluginname])) {
|
||||
continue;
|
||||
@ -660,7 +665,7 @@ $cache = '.var_export($cache, true).';
|
||||
// Always ignore plugins with problematic names here.
|
||||
continue;
|
||||
}
|
||||
$result[$pluginname] = $fulldir.'/'.$pluginname;
|
||||
$result[$pluginname] = $fulldir . '/' . $pluginname;
|
||||
unset($item);
|
||||
}
|
||||
unset($items);
|
||||
@ -676,7 +681,7 @@ $cache = '.var_export($cache, true).';
|
||||
protected static function fill_classmap_cache() {
|
||||
global $CFG;
|
||||
|
||||
self::$classmap = array();
|
||||
self::$classmap = [];
|
||||
|
||||
self::load_classes('core', "$CFG->dirroot/lib/classes");
|
||||
|
||||
@ -684,12 +689,12 @@ $cache = '.var_export($cache, true).';
|
||||
if (!$fulldir) {
|
||||
continue;
|
||||
}
|
||||
self::load_classes('core_'.$subsystem, "$fulldir/classes");
|
||||
self::load_classes('core_' . $subsystem, "$fulldir/classes");
|
||||
}
|
||||
|
||||
foreach (self::$plugins as $plugintype => $plugins) {
|
||||
foreach ($plugins as $pluginname => $fulldir) {
|
||||
self::load_classes($plugintype.'_'.$pluginname, "$fulldir/classes");
|
||||
self::load_classes($plugintype . '_' . $pluginname, "$fulldir/classes");
|
||||
}
|
||||
}
|
||||
ksort(self::$classmap);
|
||||
@ -704,15 +709,15 @@ $cache = '.var_export($cache, true).';
|
||||
protected static function fill_filemap_cache() {
|
||||
global $CFG;
|
||||
|
||||
self::$filemap = array();
|
||||
self::$filemap = [];
|
||||
|
||||
foreach (self::$filestomap as $file) {
|
||||
if (!isset(self::$filemap[$file])) {
|
||||
self::$filemap[$file] = array();
|
||||
self::$filemap[$file] = [];
|
||||
}
|
||||
foreach (self::$plugins as $plugintype => $plugins) {
|
||||
if (!isset(self::$filemap[$file][$plugintype])) {
|
||||
self::$filemap[$file][$plugintype] = array();
|
||||
self::$filemap[$file][$plugintype] = [];
|
||||
}
|
||||
foreach ($plugins as $pluginname => $fulldir) {
|
||||
if (file_exists("$fulldir/$file")) {
|
||||
@ -748,7 +753,7 @@ $cache = '.var_export($cache, true).';
|
||||
}
|
||||
if ($item->isDir()) {
|
||||
$dirname = $item->getFilename();
|
||||
self::load_classes($component, "$fulldir/$dirname", $namespace.'\\'.$dirname);
|
||||
self::load_classes($component, "$fulldir/$dirname", $namespace . '\\' . $dirname);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -761,10 +766,10 @@ $cache = '.var_export($cache, true).';
|
||||
}
|
||||
if ($namespace === '') {
|
||||
// Legacy long frankenstyle class name.
|
||||
self::$classmap[$component.'_'.$classname] = "$fulldir/$filename";
|
||||
self::$classmap[$component . '_' . $classname] = "$fulldir/$filename";
|
||||
}
|
||||
// New namespaced classes.
|
||||
self::$classmap[$component.$namespace.'\\'.$classname] = "$fulldir/$filename";
|
||||
self::$classmap[$component . $namespace . '\\' . $classname] = "$fulldir/$filename";
|
||||
}
|
||||
unset($item);
|
||||
unset($items);
|
||||
@ -826,7 +831,7 @@ $cache = '.var_export($cache, true).';
|
||||
self::init();
|
||||
|
||||
if (!isset(self::$plugins[$plugintype])) {
|
||||
return array();
|
||||
return [];
|
||||
}
|
||||
return self::$plugins[$plugintype];
|
||||
}
|
||||
@ -854,7 +859,7 @@ $cache = '.var_export($cache, true).';
|
||||
$suffix = '';
|
||||
}
|
||||
|
||||
$pluginclasses = array();
|
||||
$pluginclasses = [];
|
||||
$plugins = self::get_plugin_list($plugintype);
|
||||
foreach ($plugins as $plugin => $fulldir) {
|
||||
// Try class in frankenstyle namespace.
|
||||
@ -874,7 +879,7 @@ $cache = '.var_export($cache, true).';
|
||||
}
|
||||
|
||||
// Fall back to old file location and class name.
|
||||
if ($file and file_exists("$fulldir/$file")) {
|
||||
if ($file && file_exists("$fulldir/$file")) {
|
||||
include_once("$fulldir/$file");
|
||||
if (class_exists($classname, false)) {
|
||||
$pluginclasses[$plugintype . '_' . $plugin] = $classname;
|
||||
@ -898,7 +903,7 @@ $cache = '.var_export($cache, true).';
|
||||
*/
|
||||
public static function get_plugin_list_with_file($plugintype, $file, $include = false) {
|
||||
global $CFG; // Necessary in case it is referenced by included PHP scripts.
|
||||
$pluginfiles = array();
|
||||
$pluginfiles = [];
|
||||
|
||||
if (isset(self::$filemap[$file])) {
|
||||
// If the file was supposed to be mapped, then it should have been set in the array.
|
||||
@ -939,16 +944,14 @@ $cache = '.var_export($cache, true).';
|
||||
*/
|
||||
public static function get_component_classes_in_namespace($component = null, $namespace = '') {
|
||||
|
||||
$classes = array();
|
||||
$classes = [];
|
||||
|
||||
// Only look for components if a component name is set or a namespace is set.
|
||||
if (isset($component) || !empty($namespace)) {
|
||||
|
||||
// If a component parameter value is set we only want to look in that component.
|
||||
// Otherwise we want to check all components.
|
||||
$component = (isset($component)) ? self::normalize_componentname($component) : '\w+';
|
||||
if ($namespace) {
|
||||
|
||||
// We will add them later.
|
||||
$namespace = trim($namespace, '\\');
|
||||
|
||||
@ -1040,7 +1043,7 @@ $cache = '.var_export($cache, true).';
|
||||
* @return string
|
||||
*/
|
||||
public static function normalize_componentname($componentname) {
|
||||
list($plugintype, $pluginname) = self::normalize_component($componentname);
|
||||
[$plugintype, $pluginname] = self::normalize_component($componentname);
|
||||
if ($plugintype === 'core' && is_null($pluginname)) {
|
||||
return $plugintype;
|
||||
}
|
||||
@ -1056,8 +1059,8 @@ $cache = '.var_export($cache, true).';
|
||||
* @return array two-items list of [(string)type, (string|null)name]
|
||||
*/
|
||||
public static function normalize_component($component) {
|
||||
if ($component === 'moodle' or $component === 'core' or $component === '') {
|
||||
return array('core', null);
|
||||
if ($component === 'moodle' || $component === 'core' || $component === '') {
|
||||
return ['core', null];
|
||||
}
|
||||
|
||||
if (strpos($component, '_') === false) {
|
||||
@ -1070,16 +1073,15 @@ $cache = '.var_export($cache, true).';
|
||||
$type = 'mod';
|
||||
$plugin = $component;
|
||||
}
|
||||
|
||||
} else {
|
||||
list($type, $plugin) = explode('_', $component, 2);
|
||||
[$type, $plugin] = explode('_', $component, 2);
|
||||
if ($type === 'moodle') {
|
||||
$type = 'core';
|
||||
}
|
||||
// Any unknown type must be a subplugin.
|
||||
}
|
||||
|
||||
return array($type, $plugin);
|
||||
return [$type, $plugin];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1091,7 +1093,7 @@ $cache = '.var_export($cache, true).';
|
||||
public static function get_component_directory($component) {
|
||||
global $CFG;
|
||||
|
||||
list($type, $plugin) = self::normalize_component($component);
|
||||
[$type, $plugin] = self::normalize_component($component);
|
||||
|
||||
if ($type === 'core') {
|
||||
if ($plugin === null) {
|
||||
@ -1110,7 +1112,7 @@ $cache = '.var_export($cache, true).';
|
||||
public static function get_plugin_types_with_subplugins() {
|
||||
self::init();
|
||||
|
||||
$return = array();
|
||||
$return = [];
|
||||
foreach (self::$supportsubplugins as $type) {
|
||||
$return[$type] = self::$plugintypes[$type];
|
||||
}
|
||||
@ -1166,12 +1168,12 @@ $cache = '.var_export($cache, true).';
|
||||
*
|
||||
* @return array as (string)plugintype_pluginname => (int)version
|
||||
*/
|
||||
public static function get_all_versions() : array {
|
||||
public static function get_all_versions(): array {
|
||||
global $CFG;
|
||||
|
||||
self::init();
|
||||
|
||||
$versions = array();
|
||||
$versions = [];
|
||||
|
||||
// Main version first.
|
||||
$versions['core'] = self::fetch_core_version();
|
||||
@ -1179,15 +1181,15 @@ $cache = '.var_export($cache, true).';
|
||||
// The problem here is tha the component cache might be stable,
|
||||
// we want this to work also on frontpage without resetting the component cache.
|
||||
$usecache = false;
|
||||
if (CACHE_DISABLE_ALL or (defined('IGNORE_COMPONENT_CACHE') and IGNORE_COMPONENT_CACHE)) {
|
||||
if (CACHE_DISABLE_ALL || (defined('IGNORE_COMPONENT_CACHE') && IGNORE_COMPONENT_CACHE)) {
|
||||
$usecache = true;
|
||||
}
|
||||
|
||||
// Now all plugins.
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
$plugintypes = self::get_plugin_types();
|
||||
foreach ($plugintypes as $type => $typedir) {
|
||||
if ($usecache) {
|
||||
$plugs = core_component::get_plugin_list($type);
|
||||
$plugs = self::get_plugin_list($type);
|
||||
} else {
|
||||
$plugs = self::fetch_plugins($type, $typedir);
|
||||
}
|
||||
@ -1195,8 +1197,8 @@ $cache = '.var_export($cache, true).';
|
||||
$plugin = new stdClass();
|
||||
$plugin->version = null;
|
||||
$module = $plugin;
|
||||
include($fullplug.'/version.php');
|
||||
$versions[$type.'_'.$plug] = $plugin->version;
|
||||
include($fullplug . '/version.php');
|
||||
$versions[$type . '_' . $plug] = $plugin->version;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1211,7 +1213,7 @@ $cache = '.var_export($cache, true).';
|
||||
* @param array|null $components optional component directory => hash array to use. Only used in PHPUnit.
|
||||
* @return string sha1 hash.
|
||||
*/
|
||||
public static function get_all_component_hash(?array $components = null) : string {
|
||||
public static function get_all_component_hash(?array $components = null): string {
|
||||
$tohash = $components ?? self::get_all_directory_hashes();
|
||||
return sha1(serialize($tohash));
|
||||
}
|
||||
@ -1222,7 +1224,7 @@ $cache = '.var_export($cache, true).';
|
||||
* @param array|null $directories optional component directory array to hash. Only used in PHPUnit.
|
||||
* @return array of directory => hash.
|
||||
*/
|
||||
public static function get_all_directory_hashes(?array $directories = null) : array {
|
||||
public static function get_all_directory_hashes(?array $directories = null): array {
|
||||
global $CFG;
|
||||
|
||||
self::init();
|
||||
@ -1236,7 +1238,7 @@ $cache = '.var_export($cache, true).';
|
||||
|
||||
if (empty($directories)) {
|
||||
$directories = [
|
||||
$CFG->libdir . '/db'
|
||||
$CFG->libdir . '/db',
|
||||
];
|
||||
// For all components, get the directory of the /db directory.
|
||||
$plugintypes = self::get_plugin_types();
|
||||
@ -1323,7 +1325,7 @@ $cache = '.var_export($cache, true).';
|
||||
protected static function fill_classmap_renames_cache() {
|
||||
global $CFG;
|
||||
|
||||
self::$classmaprenames = array();
|
||||
self::$classmaprenames = [];
|
||||
|
||||
self::load_renamed_classes("$CFG->dirroot/lib/");
|
||||
|
||||
@ -1382,7 +1384,7 @@ $cache = '.var_export($cache, true).';
|
||||
*
|
||||
* @return array an associative array of components and their corresponding paths.
|
||||
*/
|
||||
public static function get_component_list() : array {
|
||||
public static function get_component_list(): array {
|
||||
$components = [];
|
||||
// Get all plugins.
|
||||
foreach (self::get_plugin_types() as $plugintype => $typedir) {
|
||||
@ -1410,7 +1412,7 @@ $cache = '.var_export($cache, true).';
|
||||
* ]
|
||||
* @return array the list of frankenstyle component names.
|
||||
*/
|
||||
public static function get_component_names() : array {
|
||||
public static function get_component_names(): array {
|
||||
$componentnames = [];
|
||||
// Get all plugins.
|
||||
foreach (self::get_plugin_types() as $plugintype => $typedir) {
|
||||
@ -1445,7 +1447,7 @@ $cache = '.var_export($cache, true).';
|
||||
* @return bool True if the plugin has a monologo icon
|
||||
*/
|
||||
public static function has_monologo_icon(string $plugintype, string $pluginname): bool {
|
||||
$plugindir = core_component::get_plugin_directory($plugintype, $pluginname);
|
||||
$plugindir = self::get_plugin_directory($plugintype, $pluginname);
|
||||
if ($plugindir === null) {
|
||||
return false;
|
||||
}
|
||||
|
@ -14,6 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// phpcs:disable moodle.PHPUnit.TestCaseNames.MissingNS
|
||||
|
||||
/**
|
||||
* core_component related tests.
|
||||
*
|
||||
@ -25,7 +27,6 @@
|
||||
* @covers \core_component
|
||||
*/
|
||||
class component_test extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* To be changed if number of subsystems increases/decreases,
|
||||
* this is defined here to annoy devs that try to add more without any thinking,
|
||||
@ -33,36 +34,43 @@ class component_test extends advanced_testcase {
|
||||
*/
|
||||
const SUBSYSTEMCOUNT = 77;
|
||||
|
||||
public function test_get_core_subsystems() {
|
||||
public function test_get_core_subsystems(): void {
|
||||
global $CFG;
|
||||
|
||||
$subsystems = core_component::get_core_subsystems();
|
||||
|
||||
$this->assertCount(self::SUBSYSTEMCOUNT, $subsystems, 'Oh, somebody added or removed a core subsystem, think twice before doing that!');
|
||||
$this->assertCount(
|
||||
self::SUBSYSTEMCOUNT,
|
||||
$subsystems,
|
||||
'Oh, somebody added or removed a core subsystem, think twice before doing that!',
|
||||
);
|
||||
|
||||
// Make sure all paths are full/null, exist and are inside dirroot.
|
||||
foreach ($subsystems as $subsystem => $fulldir) {
|
||||
$this->assertFalse(strpos($subsystem, '_'), 'Core subsystems must be one work without underscores');
|
||||
if ($fulldir === null) {
|
||||
if ($subsystem === 'filepicker' or $subsystem === 'help') {
|
||||
if ($subsystem === 'filepicker' || $subsystem === 'help') { // phpcs:ignore
|
||||
// Arrgghh, let's not introduce more subsystems for no real reason...
|
||||
} else {
|
||||
// Lang strings.
|
||||
$this->assertFileExists("$CFG->dirroot/lang/en/$subsystem.php", 'Core subsystems without fulldir are usually used for lang strings.');
|
||||
$this->assertFileExists(
|
||||
"$CFG->dirroot/lang/en/$subsystem.php",
|
||||
'Core subsystems without fulldir are usually used for lang strings.',
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
$this->assertFileExists($fulldir);
|
||||
// Check that base uses realpath() separators and "/" in the subdirs.
|
||||
$this->assertStringStartsWith($CFG->dirroot.'/', $fulldir);
|
||||
$reldir = substr($fulldir, strlen($CFG->dirroot)+1);
|
||||
$this->assertStringStartsWith($CFG->dirroot . '/', $fulldir);
|
||||
$reldir = substr($fulldir, strlen($CFG->dirroot) + 1);
|
||||
$this->assertFalse(strpos($reldir, '\\'));
|
||||
}
|
||||
|
||||
// Make sure all core language files are also subsystems!
|
||||
$items = new DirectoryIterator("$CFG->dirroot/lang/en");
|
||||
foreach ($items as $item) {
|
||||
if ($item->isDot() or $item->isDir()) {
|
||||
if ($item->isDot() || $item->isDir()) {
|
||||
continue;
|
||||
}
|
||||
$file = $item->getFilename();
|
||||
@ -74,15 +82,18 @@ class component_test extends advanced_testcase {
|
||||
if (substr($file, -4) !== '.php') {
|
||||
continue;
|
||||
}
|
||||
$file = substr($file, 0, strlen($file)-4);
|
||||
$this->assertArrayHasKey($file, $subsystems, 'All core lang files should be subsystems, think twice before adding anything!');
|
||||
$file = substr($file, 0, strlen($file) - 4);
|
||||
$this->assertArrayHasKey(
|
||||
$file,
|
||||
$subsystems,
|
||||
'All core lang files should be subsystems, think twice before adding anything!',
|
||||
);
|
||||
}
|
||||
unset($item);
|
||||
unset($items);
|
||||
|
||||
}
|
||||
|
||||
public function test_deprecated_get_core_subsystems() {
|
||||
public function test_deprecated_get_core_subsystems(): void {
|
||||
global $CFG;
|
||||
|
||||
$subsystems = core_component::get_core_subsystems();
|
||||
@ -102,14 +113,17 @@ class component_test extends advanced_testcase {
|
||||
$this->assertNull($realsubsystems[$subsystem]);
|
||||
continue;
|
||||
}
|
||||
$this->assertSame($fulldir, $CFG->dirroot.'/'.$realsubsystems[$subsystem]);
|
||||
$this->assertSame($fulldir, $CFG->dirroot . '/' . $realsubsystems[$subsystem]);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_get_plugin_types() {
|
||||
public function test_get_plugin_types(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->assertTrue(empty($CFG->themedir), 'Non-empty $CFG->themedir is not covered by any tests yet, you need to disable it.');
|
||||
$this->assertTrue(
|
||||
empty($CFG->themedir),
|
||||
'Non-empty $CFG->themedir is not covered by any tests yet, you need to disable it.',
|
||||
);
|
||||
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
|
||||
@ -118,7 +132,7 @@ class component_test extends advanced_testcase {
|
||||
}
|
||||
}
|
||||
|
||||
public function test_deprecated_get_plugin_types() {
|
||||
public function test_deprecated_get_plugin_types(): void {
|
||||
global $CFG;
|
||||
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
@ -130,11 +144,11 @@ class component_test extends advanced_testcase {
|
||||
$this->assertDebuggingCalled();
|
||||
|
||||
foreach ($plugintypes as $plugintype => $fulldir) {
|
||||
$this->assertSame($fulldir, $CFG->dirroot.'/'.$realplugintypes[$plugintype]);
|
||||
$this->assertSame($fulldir, $CFG->dirroot . '/' . $realplugintypes[$plugintype]);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_get_plugin_list() {
|
||||
public function test_get_plugin_list(): void {
|
||||
global $CFG;
|
||||
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
@ -146,7 +160,7 @@ class component_test extends advanced_testcase {
|
||||
}
|
||||
if ($plugintype !== 'auth') {
|
||||
// Let's crosscheck it with independent implementation (auth/db is an exception).
|
||||
$reldir = substr($fulldir, strlen($CFG->dirroot)+1);
|
||||
$reldir = substr($fulldir, strlen($CFG->dirroot) + 1);
|
||||
$dirs = get_list_of_plugins($reldir);
|
||||
$dirs = array_values($dirs);
|
||||
$this->assertDebuggingCalled();
|
||||
@ -155,7 +169,7 @@ class component_test extends advanced_testcase {
|
||||
}
|
||||
}
|
||||
|
||||
public function test_deprecated_get_plugin_list() {
|
||||
public function test_deprecated_get_plugin_list(): void {
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
|
||||
foreach ($plugintypes as $plugintype => $fulldir) {
|
||||
@ -164,7 +178,7 @@ class component_test extends advanced_testcase {
|
||||
}
|
||||
}
|
||||
|
||||
public function test_get_plugin_directory() {
|
||||
public function test_get_plugin_directory(): void {
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
|
||||
foreach ($plugintypes as $plugintype => $fulldir) {
|
||||
@ -175,18 +189,21 @@ class component_test extends advanced_testcase {
|
||||
}
|
||||
}
|
||||
|
||||
public function test_deprecated_get_plugin_directory() {
|
||||
public function test_deprecated_get_plugin_directory(): void {
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
|
||||
foreach ($plugintypes as $plugintype => $fulldir) {
|
||||
$plugins = core_component::get_plugin_list($plugintype);
|
||||
foreach ($plugins as $pluginname => $plugindir) {
|
||||
$this->assertSame(core_component::get_plugin_directory($plugintype, $pluginname), get_plugin_directory($plugintype, $pluginname));
|
||||
$this->assertSame(
|
||||
core_component::get_plugin_directory($plugintype, $pluginname),
|
||||
get_plugin_directory($plugintype, $pluginname),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function test_get_subsystem_directory() {
|
||||
public function test_get_subsystem_directory(): void {
|
||||
$subsystems = core_component::get_core_subsystems();
|
||||
foreach ($subsystems as $subsystem => $fulldir) {
|
||||
$this->assertSame($fulldir, core_component::get_subsystem_directory($subsystem));
|
||||
@ -210,7 +227,7 @@ class component_test extends advanced_testcase {
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function is_valid_plugin_name_provider(): array {
|
||||
public static function is_valid_plugin_name_provider(): array {
|
||||
return [
|
||||
[['mod', 'example1'], true],
|
||||
[['mod', 'feedback360'], true],
|
||||
@ -247,7 +264,7 @@ class component_test extends advanced_testcase {
|
||||
];
|
||||
}
|
||||
|
||||
public function test_normalize_componentname() {
|
||||
public function test_normalize_componentname(): void {
|
||||
// Moodle core.
|
||||
$this->assertSame('core', core_component::normalize_componentname('core'));
|
||||
$this->assertSame('core', core_component::normalize_componentname('moodle'));
|
||||
@ -276,122 +293,140 @@ class component_test extends advanced_testcase {
|
||||
$this->assertSame('local_admin', core_component::normalize_componentname('local_admin'));
|
||||
|
||||
// Unknown words without underscore are supposed to be activity modules.
|
||||
$this->assertSame('mod_whoonearthwouldcomewithsuchastupidnameofcomponent',
|
||||
core_component::normalize_componentname('whoonearthwouldcomewithsuchastupidnameofcomponent'));
|
||||
$this->assertSame(
|
||||
'mod_whoonearthwouldcomewithsuchastupidnameofcomponent',
|
||||
core_component::normalize_componentname('whoonearthwouldcomewithsuchastupidnameofcomponent')
|
||||
);
|
||||
// Module names can not contain underscores, this must be a subplugin.
|
||||
$this->assertSame('whoonearth_wouldcomewithsuchastupidnameofcomponent',
|
||||
core_component::normalize_componentname('whoonearth_wouldcomewithsuchastupidnameofcomponent'));
|
||||
$this->assertSame('whoonearth_would_come_withsuchastupidnameofcomponent',
|
||||
core_component::normalize_componentname('whoonearth_would_come_withsuchastupidnameofcomponent'));
|
||||
$this->assertSame(
|
||||
'whoonearth_wouldcomewithsuchastupidnameofcomponent',
|
||||
core_component::normalize_componentname('whoonearth_wouldcomewithsuchastupidnameofcomponent')
|
||||
);
|
||||
$this->assertSame(
|
||||
'whoonearth_would_come_withsuchastupidnameofcomponent',
|
||||
core_component::normalize_componentname('whoonearth_would_come_withsuchastupidnameofcomponent')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_normalize_component() {
|
||||
public function test_normalize_component(): void {
|
||||
// Moodle core.
|
||||
$this->assertSame(array('core', null), core_component::normalize_component('core'));
|
||||
$this->assertSame(array('core', null), core_component::normalize_component('moodle'));
|
||||
$this->assertSame(array('core', null), core_component::normalize_component(''));
|
||||
$this->assertSame(['core', null], core_component::normalize_component('core'));
|
||||
$this->assertSame(['core', null], core_component::normalize_component('moodle'));
|
||||
$this->assertSame(['core', null], core_component::normalize_component(''));
|
||||
|
||||
// Moodle core subsystems.
|
||||
$this->assertSame(array('core', 'admin'), core_component::normalize_component('admin'));
|
||||
$this->assertSame(array('core', 'admin'), core_component::normalize_component('core_admin'));
|
||||
$this->assertSame(array('core', 'admin'), core_component::normalize_component('moodle_admin'));
|
||||
$this->assertSame(['core', 'admin'], core_component::normalize_component('admin'));
|
||||
$this->assertSame(['core', 'admin'], core_component::normalize_component('core_admin'));
|
||||
$this->assertSame(['core', 'admin'], core_component::normalize_component('moodle_admin'));
|
||||
|
||||
// Activity modules and their subplugins.
|
||||
$this->assertSame(array('mod', 'workshop'), core_component::normalize_component('workshop'));
|
||||
$this->assertSame(array('mod', 'workshop'), core_component::normalize_component('mod_workshop'));
|
||||
$this->assertSame(array('workshopform', 'accumulative'), core_component::normalize_component('workshopform_accumulative'));
|
||||
$this->assertSame(array('mod', 'quiz'), core_component::normalize_component('quiz'));
|
||||
$this->assertSame(array('quiz', 'grading'), core_component::normalize_component('quiz_grading'));
|
||||
$this->assertSame(array('mod', 'data'), core_component::normalize_component('data'));
|
||||
$this->assertSame(array('datafield', 'checkbox'), core_component::normalize_component('datafield_checkbox'));
|
||||
$this->assertSame(['mod', 'workshop'], core_component::normalize_component('workshop'));
|
||||
$this->assertSame(['mod', 'workshop'], core_component::normalize_component('mod_workshop'));
|
||||
$this->assertSame(['workshopform', 'accumulative'], core_component::normalize_component('workshopform_accumulative'));
|
||||
$this->assertSame(['mod', 'quiz'], core_component::normalize_component('quiz'));
|
||||
$this->assertSame(['quiz', 'grading'], core_component::normalize_component('quiz_grading'));
|
||||
$this->assertSame(['mod', 'data'], core_component::normalize_component('data'));
|
||||
$this->assertSame(['datafield', 'checkbox'], core_component::normalize_component('datafield_checkbox'));
|
||||
|
||||
// Other plugin types.
|
||||
$this->assertSame(array('auth', 'mnet'), core_component::normalize_component('auth_mnet'));
|
||||
$this->assertSame(array('enrol', 'self'), core_component::normalize_component('enrol_self'));
|
||||
$this->assertSame(array('block', 'html'), core_component::normalize_component('block_html'));
|
||||
$this->assertSame(array('block', 'mnet_hosts'), core_component::normalize_component('block_mnet_hosts'));
|
||||
$this->assertSame(array('local', 'amos'), core_component::normalize_component('local_amos'));
|
||||
$this->assertSame(array('local', 'admin'), core_component::normalize_component('local_admin'));
|
||||
$this->assertSame(['auth', 'mnet'], core_component::normalize_component('auth_mnet'));
|
||||
$this->assertSame(['enrol', 'self'], core_component::normalize_component('enrol_self'));
|
||||
$this->assertSame(['block', 'html'], core_component::normalize_component('block_html'));
|
||||
$this->assertSame(['block', 'mnet_hosts'], core_component::normalize_component('block_mnet_hosts'));
|
||||
$this->assertSame(['local', 'amos'], core_component::normalize_component('local_amos'));
|
||||
$this->assertSame(['local', 'admin'], core_component::normalize_component('local_admin'));
|
||||
|
||||
// Unknown words without underscore are supposed to be activity modules.
|
||||
$this->assertSame(array('mod', 'whoonearthwouldcomewithsuchastupidnameofcomponent'),
|
||||
core_component::normalize_component('whoonearthwouldcomewithsuchastupidnameofcomponent'));
|
||||
$this->assertSame(
|
||||
['mod', 'whoonearthwouldcomewithsuchastupidnameofcomponent'],
|
||||
core_component::normalize_component('whoonearthwouldcomewithsuchastupidnameofcomponent')
|
||||
);
|
||||
// Module names can not contain underscores, this must be a subplugin.
|
||||
$this->assertSame(array('whoonearth', 'wouldcomewithsuchastupidnameofcomponent'),
|
||||
core_component::normalize_component('whoonearth_wouldcomewithsuchastupidnameofcomponent'));
|
||||
$this->assertSame(array('whoonearth', 'would_come_withsuchastupidnameofcomponent'),
|
||||
core_component::normalize_component('whoonearth_would_come_withsuchastupidnameofcomponent'));
|
||||
$this->assertSame(
|
||||
['whoonearth', 'wouldcomewithsuchastupidnameofcomponent'],
|
||||
core_component::normalize_component('whoonearth_wouldcomewithsuchastupidnameofcomponent')
|
||||
);
|
||||
$this->assertSame(
|
||||
['whoonearth', 'would_come_withsuchastupidnameofcomponent'],
|
||||
core_component::normalize_component('whoonearth_would_come_withsuchastupidnameofcomponent')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_deprecated_normalize_component() {
|
||||
public function test_deprecated_normalize_component(): void {
|
||||
// Moodle core.
|
||||
$this->assertSame(array('core', null), normalize_component('core'));
|
||||
$this->assertSame(array('core', null), normalize_component(''));
|
||||
$this->assertSame(array('core', null), normalize_component('moodle'));
|
||||
$this->assertSame(['core', null], normalize_component('core'));
|
||||
$this->assertSame(['core', null], normalize_component(''));
|
||||
$this->assertSame(['core', null], normalize_component('moodle'));
|
||||
|
||||
// Moodle core subsystems.
|
||||
$this->assertSame(array('core', 'admin'), normalize_component('admin'));
|
||||
$this->assertSame(array('core', 'admin'), normalize_component('core_admin'));
|
||||
$this->assertSame(array('core', 'admin'), normalize_component('moodle_admin'));
|
||||
$this->assertSame(['core', 'admin'], normalize_component('admin'));
|
||||
$this->assertSame(['core', 'admin'], normalize_component('core_admin'));
|
||||
$this->assertSame(['core', 'admin'], normalize_component('moodle_admin'));
|
||||
|
||||
// Activity modules and their subplugins.
|
||||
$this->assertSame(array('mod', 'workshop'), normalize_component('workshop'));
|
||||
$this->assertSame(array('mod', 'workshop'), normalize_component('mod_workshop'));
|
||||
$this->assertSame(array('workshopform', 'accumulative'), normalize_component('workshopform_accumulative'));
|
||||
$this->assertSame(array('mod', 'quiz'), normalize_component('quiz'));
|
||||
$this->assertSame(array('quiz', 'grading'), normalize_component('quiz_grading'));
|
||||
$this->assertSame(array('mod', 'data'), normalize_component('data'));
|
||||
$this->assertSame(array('datafield', 'checkbox'), normalize_component('datafield_checkbox'));
|
||||
$this->assertSame(['mod', 'workshop'], normalize_component('workshop'));
|
||||
$this->assertSame(['mod', 'workshop'], normalize_component('mod_workshop'));
|
||||
$this->assertSame(['workshopform', 'accumulative'], normalize_component('workshopform_accumulative'));
|
||||
$this->assertSame(['mod', 'quiz'], normalize_component('quiz'));
|
||||
$this->assertSame(['quiz', 'grading'], normalize_component('quiz_grading'));
|
||||
$this->assertSame(['mod', 'data'], normalize_component('data'));
|
||||
$this->assertSame(['datafield', 'checkbox'], normalize_component('datafield_checkbox'));
|
||||
|
||||
// Other plugin types.
|
||||
$this->assertSame(array('auth', 'mnet'), normalize_component('auth_mnet'));
|
||||
$this->assertSame(array('enrol', 'self'), normalize_component('enrol_self'));
|
||||
$this->assertSame(array('block', 'html'), normalize_component('block_html'));
|
||||
$this->assertSame(array('block', 'mnet_hosts'), normalize_component('block_mnet_hosts'));
|
||||
$this->assertSame(array('local', 'amos'), normalize_component('local_amos'));
|
||||
$this->assertSame(array('local', 'admin'), normalize_component('local_admin'));
|
||||
$this->assertSame(['auth', 'mnet'], normalize_component('auth_mnet'));
|
||||
$this->assertSame(['enrol', 'self'], normalize_component('enrol_self'));
|
||||
$this->assertSame(['block', 'html'], normalize_component('block_html'));
|
||||
$this->assertSame(['block', 'mnet_hosts'], normalize_component('block_mnet_hosts'));
|
||||
$this->assertSame(['local', 'amos'], normalize_component('local_amos'));
|
||||
$this->assertSame(['local', 'admin'], normalize_component('local_admin'));
|
||||
|
||||
// Unknown words without underscore are supposed to be activity modules.
|
||||
$this->assertSame(array('mod', 'whoonearthwouldcomewithsuchastupidnameofcomponent'),
|
||||
normalize_component('whoonearthwouldcomewithsuchastupidnameofcomponent'));
|
||||
$this->assertSame(
|
||||
['mod', 'whoonearthwouldcomewithsuchastupidnameofcomponent'],
|
||||
normalize_component('whoonearthwouldcomewithsuchastupidnameofcomponent')
|
||||
);
|
||||
// Module names can not contain underscores, this must be a subplugin.
|
||||
$this->assertSame(array('whoonearth', 'wouldcomewithsuchastupidnameofcomponent'),
|
||||
normalize_component('whoonearth_wouldcomewithsuchastupidnameofcomponent'));
|
||||
$this->assertSame(array('whoonearth', 'would_come_withsuchastupidnameofcomponent'),
|
||||
normalize_component('whoonearth_would_come_withsuchastupidnameofcomponent'));
|
||||
$this->assertSame(
|
||||
['whoonearth', 'wouldcomewithsuchastupidnameofcomponent'],
|
||||
normalize_component('whoonearth_wouldcomewithsuchastupidnameofcomponent')
|
||||
);
|
||||
$this->assertSame(
|
||||
['whoonearth', 'would_come_withsuchastupidnameofcomponent'],
|
||||
normalize_component('whoonearth_would_come_withsuchastupidnameofcomponent')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_get_component_directory() {
|
||||
public function test_get_component_directory(): void {
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
foreach ($plugintypes as $plugintype => $fulldir) {
|
||||
$plugins = core_component::get_plugin_list($plugintype);
|
||||
foreach ($plugins as $pluginname => $plugindir) {
|
||||
$this->assertSame($plugindir, core_component::get_component_directory(($plugintype.'_'.$pluginname)));
|
||||
$this->assertSame($plugindir, core_component::get_component_directory(($plugintype . '_' . $pluginname)));
|
||||
}
|
||||
}
|
||||
|
||||
$subsystems = core_component::get_core_subsystems();
|
||||
foreach ($subsystems as $subsystem => $fulldir) {
|
||||
$this->assertSame($fulldir, core_component::get_component_directory(('core_'.$subsystem)));
|
||||
$this->assertSame($fulldir, core_component::get_component_directory(('core_' . $subsystem)));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_deprecated_get_component_directory() {
|
||||
public function test_deprecated_get_component_directory(): void {
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
foreach ($plugintypes as $plugintype => $fulldir) {
|
||||
$plugins = core_component::get_plugin_list($plugintype);
|
||||
foreach ($plugins as $pluginname => $plugindir) {
|
||||
$this->assertSame($plugindir, get_component_directory(($plugintype.'_'.$pluginname)));
|
||||
$this->assertSame($plugindir, get_component_directory(($plugintype . '_' . $pluginname)));
|
||||
}
|
||||
}
|
||||
|
||||
$subsystems = core_component::get_core_subsystems();
|
||||
foreach ($subsystems as $subsystem => $fulldir) {
|
||||
$this->assertSame($fulldir, get_component_directory(('core_'.$subsystem)));
|
||||
$this->assertSame($fulldir, get_component_directory(('core_' . $subsystem)));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_get_subtype_parent() {
|
||||
public function test_get_subtype_parent(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->assertNull(core_component::get_subtype_parent('mod'));
|
||||
@ -403,14 +438,14 @@ class component_test extends advanced_testcase {
|
||||
$this->assertNull(core_component::get_subtype_parent('assignxxxxx'));
|
||||
}
|
||||
|
||||
public function test_get_subplugins() {
|
||||
public function test_get_subplugins(): void {
|
||||
global $CFG;
|
||||
|
||||
// Any plugin with more subtypes is ok here.
|
||||
$this->assertFileExists("$CFG->dirroot/mod/assign/db/subplugins.json");
|
||||
|
||||
$subplugins = core_component::get_subplugins('mod_assign');
|
||||
$this->assertSame(array('assignsubmission', 'assignfeedback'), array_keys($subplugins));
|
||||
$this->assertSame(['assignsubmission', 'assignfeedback'], array_keys($subplugins));
|
||||
|
||||
$subs = core_component::get_plugin_list('assignsubmission');
|
||||
$feeds = core_component::get_plugin_list('assignfeedback');
|
||||
@ -427,29 +462,28 @@ class component_test extends advanced_testcase {
|
||||
$this->assertNull(core_component::get_subplugins('xxxx_yyyy'));
|
||||
}
|
||||
|
||||
public function test_get_plugin_types_with_subplugins() {
|
||||
public function test_get_plugin_types_with_subplugins(): void {
|
||||
global $CFG;
|
||||
|
||||
$types = core_component::get_plugin_types_with_subplugins();
|
||||
|
||||
// Hardcode it here to detect if anybody hacks the code to include more subplugin types.
|
||||
$expected = array(
|
||||
$expected = [
|
||||
'mod' => "$CFG->dirroot/mod",
|
||||
'editor' => "$CFG->dirroot/lib/editor",
|
||||
'tool' => "$CFG->dirroot/$CFG->admin/tool",
|
||||
'local' => "$CFG->dirroot/local",
|
||||
);
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $types);
|
||||
|
||||
}
|
||||
|
||||
public function test_get_plugin_list_with_file() {
|
||||
public function test_get_plugin_list_with_file(): void {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// No extra reset here because core_component reset automatically.
|
||||
|
||||
$expected = array();
|
||||
$expected = [];
|
||||
$reports = core_component::get_plugin_list('report');
|
||||
foreach ($reports as $name => $fulldir) {
|
||||
if (file_exists("$fulldir/lib.php")) {
|
||||
@ -471,10 +505,10 @@ class component_test extends advanced_testcase {
|
||||
|
||||
// Test missing.
|
||||
$list = core_component::get_plugin_list_with_file('report', 'idontexist.php', true);
|
||||
$this->assertEquals(array(), array_keys($list));
|
||||
$this->assertEquals([], array_keys($list));
|
||||
}
|
||||
|
||||
public function test_get_component_classes_in_namespace() {
|
||||
public function test_get_component_classes_in_namespace(): void {
|
||||
|
||||
// Unexisting.
|
||||
$this->assertCount(0, core_component::get_component_classes_in_namespace('core_unexistingcomponent', 'something'));
|
||||
@ -513,7 +547,8 @@ class component_test extends advanced_testcase {
|
||||
// (We don't assert exact amounts here as the count of `output` classes will change depending on plugins installed).
|
||||
$this->assertGreaterThan(
|
||||
count(\core_component::get_component_classes_in_namespace('core', 'output')),
|
||||
count(\core_component::get_component_classes_in_namespace(null, 'output')));
|
||||
count(\core_component::get_component_classes_in_namespace(null, 'output'))
|
||||
);
|
||||
|
||||
// Without either a component or namespace it returns an empty array.
|
||||
$this->assertEmpty(\core_component::get_component_classes_in_namespace());
|
||||
@ -524,7 +559,7 @@ class component_test extends advanced_testcase {
|
||||
/**
|
||||
* Data provider for classloader test
|
||||
*/
|
||||
public function classloader_provider() {
|
||||
public static function classloader_provider(): array {
|
||||
global $CFG;
|
||||
|
||||
// As part of these tests, we Check that there are no unexpected problems with overlapping PSR namespaces.
|
||||
@ -535,11 +570,11 @@ class component_test extends advanced_testcase {
|
||||
|
||||
$psr0 = [
|
||||
'psr0' => 'lib/tests/fixtures/component/psr0',
|
||||
'overlap' => 'lib/tests/fixtures/component/overlap'
|
||||
'overlap' => 'lib/tests/fixtures/component/overlap',
|
||||
];
|
||||
$psr4 = [
|
||||
'psr4' => 'lib/tests/fixtures/component/psr4',
|
||||
'overlap' => 'lib/tests/fixtures/component/overlap'
|
||||
'overlap' => 'lib/tests/fixtures/component/overlap',
|
||||
];
|
||||
return [
|
||||
'PSR-0 Classloading - Root' => [
|
||||
@ -603,7 +638,7 @@ class component_test extends advanced_testcase {
|
||||
* @param string $includedfiles The file expected to be loaded.
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function test_classloader($psr0, $psr4, $classname, $includedfiles) {
|
||||
public function test_classloader($psr0, $psr4, $classname, $includedfiles): void {
|
||||
$psr0namespaces = new ReflectionProperty('core_component', 'psr0namespaces');
|
||||
$psr0namespaces->setAccessible(true);
|
||||
$psr0namespaces->setValue(null, $psr0);
|
||||
@ -624,7 +659,7 @@ class component_test extends advanced_testcase {
|
||||
/**
|
||||
* Data provider for psr_classloader test
|
||||
*/
|
||||
public function psr_classloader_provider() {
|
||||
public static function psr_classloader_provider(): array {
|
||||
global $CFG;
|
||||
|
||||
// As part of these tests, we Check that there are no unexpected problems with overlapping PSR namespaces.
|
||||
@ -636,11 +671,11 @@ class component_test extends advanced_testcase {
|
||||
|
||||
$psr0 = [
|
||||
'psr0' => 'lib/tests/fixtures/component/psr0',
|
||||
'overlap' => 'lib/tests/fixtures/component/overlap'
|
||||
'overlap' => 'lib/tests/fixtures/component/overlap',
|
||||
];
|
||||
$psr4 = [
|
||||
'psr4' => 'lib/tests/fixtures/component/psr4',
|
||||
'overlap' => 'lib/tests/fixtures/component/overlap'
|
||||
'overlap' => 'lib/tests/fixtures/component/overlap',
|
||||
];
|
||||
return [
|
||||
'PSR-0 Classloading - Root' => [
|
||||
@ -661,10 +696,10 @@ class component_test extends advanced_testcase {
|
||||
'classname' => 'psr0\\subnamespace\\slashes',
|
||||
'file' => "{$directory}psr0/subnamespace/slashes.php",
|
||||
],
|
||||
'PSR-0 Classloading - non-existant file' => [
|
||||
'PSR-0 Classloading - non-existent file' => [
|
||||
'psr0' => $psr0,
|
||||
'psr4' => $psr4,
|
||||
'classname' => 'psr0_subnamespace_nonexistant_file',
|
||||
'classname' => 'psr0_subnamespace_nonexistent_file',
|
||||
'file' => false,
|
||||
],
|
||||
'PSR-4 Classloading - Root' => [
|
||||
@ -685,10 +720,10 @@ class component_test extends advanced_testcase {
|
||||
'classname' => 'psr4\\subnamespace\\underscore_example',
|
||||
'file' => "{$directory}psr4/subnamespace/underscore_example.php",
|
||||
],
|
||||
'PSR-4 Classloading - non-existant file' => [
|
||||
'PSR-4 Classloading - non-existent file' => [
|
||||
'psr0' => $psr0,
|
||||
'psr4' => $psr4,
|
||||
'classname' => 'psr4\\subnamespace\\nonexistant',
|
||||
'classname' => 'psr4\\subnamespace\\nonexistent',
|
||||
'file' => false,
|
||||
],
|
||||
'Overlap - Ensure no unexpected problems with PSR-4 when overlapping namespaces.' => [
|
||||
@ -738,21 +773,20 @@ class component_test extends advanced_testcase {
|
||||
* @param string|bool $file The expected file corresponding to the class or false for nonexistant.
|
||||
* @runInSeparateProcess
|
||||
*/
|
||||
public function test_psr_classloader($psr0, $psr4, $classname, $file) {
|
||||
public function test_psr_classloader($psr0, $psr4, $classname, $file): void {
|
||||
$psr0namespaces = new ReflectionProperty('core_component', 'psr0namespaces');
|
||||
$psr0namespaces->setAccessible(true);
|
||||
$psr0namespaces->setValue(null, $psr0);
|
||||
|
||||
$psr4namespaces = new ReflectionProperty('core_component', 'psr4namespaces');
|
||||
$psr4namespaces->setAccessible(true);
|
||||
$oldpsr4namespaces = $psr4namespaces->getValue(null);
|
||||
$psr4namespaces->setValue(null, $psr4);
|
||||
|
||||
$component = new ReflectionClass('core_component');
|
||||
$psrclassloader = $component->getMethod('psr_classloader');
|
||||
$psrclassloader->setAccessible(true);
|
||||
|
||||
$returnvalue = $psrclassloader->invokeArgs(null, array($classname));
|
||||
$returnvalue = $psrclassloader->invokeArgs(null, [$classname]);
|
||||
// Normalise to forward slashes for testing comparison.
|
||||
if ($returnvalue) {
|
||||
$returnvalue = str_replace('\\', '/', $returnvalue);
|
||||
@ -763,7 +797,7 @@ class component_test extends advanced_testcase {
|
||||
/**
|
||||
* Data provider for get_class_file test
|
||||
*/
|
||||
public function get_class_file_provider() {
|
||||
public static function get_class_file_provider(): array {
|
||||
global $CFG;
|
||||
|
||||
return [
|
||||
@ -789,7 +823,7 @@ class component_test extends advanced_testcase {
|
||||
'result' => $CFG->dirroot . "/test/src/Multiple/Namespaces.php",
|
||||
],
|
||||
'Getting a file with multiple namespaces (non-existent)' => [
|
||||
'classname' => 'Nonexistant\\Namespace\\Test',
|
||||
'classname' => 'Nonexistent\\Namespace\\Test',
|
||||
'prefix' => "Test",
|
||||
'path' => 'test/src',
|
||||
'separators' => ['\\'],
|
||||
@ -808,19 +842,19 @@ class component_test extends advanced_testcase {
|
||||
* @param string[] $separators The characters that should be used for separating.
|
||||
* @param string|bool $result The expected result to be returned from get_class_file.
|
||||
*/
|
||||
public function test_get_class_file($classname, $prefix, $path, $separators, $result) {
|
||||
public function test_get_class_file($classname, $prefix, $path, $separators, $result): void {
|
||||
$component = new ReflectionClass('core_component');
|
||||
$psrclassloader = $component->getMethod('get_class_file');
|
||||
$psrclassloader->setAccessible(true);
|
||||
|
||||
$file = $psrclassloader->invokeArgs(null, array($classname, $prefix, $path, $separators));
|
||||
$file = $psrclassloader->invokeArgs(null, [$classname, $prefix, $path, $separators]);
|
||||
$this->assertEquals($result, $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm the get_component_list method contains an entry for every component.
|
||||
*/
|
||||
public function test_get_component_list_contains_all_components() {
|
||||
public function test_get_component_list_contains_all_components(): void {
|
||||
global $CFG;
|
||||
$componentslist = \core_component::get_component_list();
|
||||
|
||||
@ -834,7 +868,7 @@ class component_test extends advanced_testcase {
|
||||
$this->assertArrayHasKey($plugintype, $componentslist);
|
||||
}
|
||||
|
||||
// And finally, one for 'core'.
|
||||
// And one for 'core'.
|
||||
$this->assertArrayHasKey('core', $componentslist);
|
||||
|
||||
// Check a few of the known plugin types to confirm their presence at their respective type index.
|
||||
@ -846,7 +880,7 @@ class component_test extends advanced_testcase {
|
||||
/**
|
||||
* Test the get_component_names() method.
|
||||
*/
|
||||
public function test_get_component_names() {
|
||||
public function test_get_component_names(): void {
|
||||
global $CFG;
|
||||
$componentnames = \core_component::get_component_names();
|
||||
|
||||
@ -873,7 +907,7 @@ class component_test extends advanced_testcase {
|
||||
/**
|
||||
* Basic tests for APIs related functions in the core_component class.
|
||||
*/
|
||||
public function test_apis_methods() {
|
||||
public function test_apis_methods(): void {
|
||||
$apis = core_component::get_core_apis();
|
||||
$this->assertIsArray($apis);
|
||||
|
||||
@ -894,7 +928,7 @@ class component_test extends advanced_testcase {
|
||||
* to validate it (3rd part libraries needed). Plus the schema doesn't allow to validate things
|
||||
* like uniqueness or sorting. We are going to do all that here.
|
||||
*/
|
||||
public function test_apis_json_validation() {
|
||||
public function test_apis_json_validation(): void {
|
||||
$apis = $sortedapis = core_component::get_core_apis();
|
||||
ksort($sortedapis); // We'll need this later.
|
||||
|
||||
@ -940,9 +974,6 @@ class component_test extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test for monologo icons check in plugins.
|
||||
*
|
||||
* @covers core_component::has_monologo_icon
|
||||
* @return void
|
||||
*/
|
||||
public function test_has_monologo_icon(): void {
|
||||
// The Forum activity plugin has monologo icons.
|
||||
@ -958,7 +989,7 @@ class component_test extends advanced_testcase {
|
||||
*
|
||||
* @covers \core_component::get_all_directory_hashes
|
||||
*/
|
||||
public function test_get_db_directories_hash() {
|
||||
public function test_get_db_directories_hash(): void {
|
||||
$initial = \core_component::get_all_component_hash();
|
||||
|
||||
$dir = make_request_directory();
|
||||
|
Loading…
x
Reference in New Issue
Block a user