mirror of
https://github.com/moodle/moodle.git
synced 2025-05-11 18:56:09 +02:00
MDL-49329 admin: Make core_plugin_manager better suited for unit testing
We can now override the plugin manager's methods in the testable subclass while still keeping the singleton behaviour of it. The change makes use of late static binding.
This commit is contained in:
parent
30d8bc5f66
commit
361feecdcf
lib
@ -86,10 +86,10 @@ class core_plugin_manager {
|
||||
* @return core_plugin_manager the singleton instance
|
||||
*/
|
||||
public static function instance() {
|
||||
if (is_null(self::$singletoninstance)) {
|
||||
self::$singletoninstance = new self();
|
||||
if (is_null(static::$singletoninstance)) {
|
||||
static::$singletoninstance = new static();
|
||||
}
|
||||
return self::$singletoninstance;
|
||||
return static::$singletoninstance;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,15 +98,15 @@ class core_plugin_manager {
|
||||
*/
|
||||
public static function reset_caches($phpunitreset = false) {
|
||||
if ($phpunitreset) {
|
||||
self::$singletoninstance = null;
|
||||
static::$singletoninstance = null;
|
||||
} else {
|
||||
if (self::$singletoninstance) {
|
||||
self::$singletoninstance->pluginsinfo = null;
|
||||
self::$singletoninstance->subpluginsinfo = null;
|
||||
self::$singletoninstance->installedplugins = null;
|
||||
self::$singletoninstance->enabledplugins = null;
|
||||
self::$singletoninstance->presentplugins = null;
|
||||
self::$singletoninstance->plugintypes = null;
|
||||
if (static::$singletoninstance) {
|
||||
static::$singletoninstance->pluginsinfo = null;
|
||||
static::$singletoninstance->subpluginsinfo = null;
|
||||
static::$singletoninstance->installedplugins = null;
|
||||
static::$singletoninstance->enabledplugins = null;
|
||||
static::$singletoninstance->presentplugins = null;
|
||||
static::$singletoninstance->plugintypes = null;
|
||||
}
|
||||
}
|
||||
$cache = cache::make('core', 'plugin_manager');
|
||||
@ -238,7 +238,7 @@ class core_plugin_manager {
|
||||
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
foreach ($plugintypes as $plugintype => $fulldir) {
|
||||
$plugininfoclass = self::resolve_plugininfo_class($plugintype);
|
||||
$plugininfoclass = static::resolve_plugininfo_class($plugintype);
|
||||
if (class_exists($plugininfoclass)) {
|
||||
$enabled = $plugininfoclass::get_enabled_plugins();
|
||||
if (!is_array($enabled)) {
|
||||
@ -374,13 +374,13 @@ class core_plugin_manager {
|
||||
|
||||
if (!isset($types[$type])) {
|
||||
// Orphaned subplugins!
|
||||
$plugintypeclass = self::resolve_plugininfo_class($type);
|
||||
$plugintypeclass = static::resolve_plugininfo_class($type);
|
||||
$this->pluginsinfo[$type] = $plugintypeclass::get_plugins($type, null, $plugintypeclass);
|
||||
return $this->pluginsinfo[$type];
|
||||
}
|
||||
|
||||
/** @var \core\plugininfo\base $plugintypeclass */
|
||||
$plugintypeclass = self::resolve_plugininfo_class($type);
|
||||
$plugintypeclass = static::resolve_plugininfo_class($type);
|
||||
$plugins = $plugintypeclass::get_plugins($type, $types[$type], $plugintypeclass);
|
||||
$this->pluginsinfo[$type] = $plugins;
|
||||
|
||||
@ -1297,7 +1297,7 @@ class core_plugin_manager {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($pluginfo->get_status() === self::PLUGIN_STATUS_NEW) {
|
||||
if ($pluginfo->get_status() === static::PLUGIN_STATUS_NEW) {
|
||||
// The plugin is not installed. It should be either installed or removed from the disk.
|
||||
// Relying on this temporary state may be tricky.
|
||||
return false;
|
||||
|
38
lib/tests/fixtures/testable_plugin_manager.php
vendored
Normal file
38
lib/tests/fixtures/testable_plugin_manager.php
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Provides testable_core_plugin_manager class.
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2015 David Mudrak <david@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Testable variant of the core_plugin_manager
|
||||
*
|
||||
* @copyright 2015 David Mudrak <david@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class testable_core_plugin_manager extends core_plugin_manager {
|
||||
|
||||
/** @var testable_core_plugin_manager holds the singleton instance */
|
||||
protected static $singletoninstance;
|
||||
}
|
@ -25,21 +25,31 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot.'/lib/tests/fixtures/testable_plugin_manager.php');
|
||||
|
||||
/**
|
||||
* Tests of the basic API of the plugin manager.
|
||||
*/
|
||||
class core_plugin_manager_testcase extends advanced_testcase {
|
||||
|
||||
public function test_instance() {
|
||||
$pluginman = core_plugin_manager::instance();
|
||||
$this->assertInstanceOf('core_plugin_manager', $pluginman);
|
||||
$pluginman1 = core_plugin_manager::instance();
|
||||
$this->assertInstanceOf('core_plugin_manager', $pluginman1);
|
||||
$pluginman2 = core_plugin_manager::instance();
|
||||
$this->assertSame($pluginman, $pluginman2);
|
||||
$this->assertSame($pluginman1, $pluginman2);
|
||||
$pluginman3 = testable_core_plugin_manager::instance();
|
||||
$this->assertInstanceOf('core_plugin_manager', $pluginman3);
|
||||
$this->assertInstanceOf('testable_core_plugin_manager', $pluginman3);
|
||||
$pluginman4 = testable_core_plugin_manager::instance();
|
||||
$this->assertSame($pluginman3, $pluginman4);
|
||||
$this->assertNotSame($pluginman1, $pluginman3);
|
||||
}
|
||||
|
||||
public function test_reset_caches() {
|
||||
// Make sure there are no warnings or errors.
|
||||
core_plugin_manager::reset_caches();
|
||||
testable_core_plugin_manager::reset_caches();
|
||||
}
|
||||
|
||||
public function test_get_plugin_types() {
|
||||
@ -95,12 +105,23 @@ class core_plugin_manager_testcase extends advanced_testcase {
|
||||
}
|
||||
|
||||
public function test_get_plugins() {
|
||||
$plugininfos = core_plugin_manager::instance()->get_plugins();
|
||||
foreach ($plugininfos as $type => $infos) {
|
||||
$plugininfos1 = core_plugin_manager::instance()->get_plugins();
|
||||
foreach ($plugininfos1 as $type => $infos) {
|
||||
foreach ($infos as $name => $info) {
|
||||
$this->assertInstanceOf('\core\plugininfo\base', $info);
|
||||
}
|
||||
}
|
||||
|
||||
// The testable variant of the manager holds its own tree of the
|
||||
// plugininfo objects.
|
||||
$plugininfos2 = testable_core_plugin_manager::instance()->get_plugins();
|
||||
$this->assertNotSame($plugininfos1['mod']['forum'], $plugininfos2['mod']['forum']);
|
||||
|
||||
// Singletons of each manager class share the same tree.
|
||||
$plugininfos3 = core_plugin_manager::instance()->get_plugins();
|
||||
$this->assertSame($plugininfos1['mod']['forum'], $plugininfos3['mod']['forum']);
|
||||
$plugininfos4 = testable_core_plugin_manager::instance()->get_plugins();
|
||||
$this->assertSame($plugininfos2['mod']['forum'], $plugininfos4['mod']['forum']);
|
||||
}
|
||||
|
||||
public function test_get_plugins_of_type() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user