Commit c44bbe35 authored by David Mudrák's avatar David Mudrák
Browse files

MDL-49329 admin: Improve loading of available updates info

The actual loading of available updates info objects is moved back to
the plugin manager class. As we can now mockup the manager in unit
tests, this allows us to bypass the real \core\update\checker and have
unit tests for \core\plugininfo\base::available_updates().
parent 2d488c8f
......@@ -878,6 +878,35 @@ class core_plugin_manager {
return false;
}
/**
* Returns list of available updates for the given component.
*
* This method should be considered as internal API and is supposed to be
* called by {@link \core\plugininfo\base::available_updates()} only
* to lazy load the data once they are first requested.
*
* @param string $component frankenstyle name of the plugin
* @return null|array array of \core\update\info objects or null
*/
public function load_available_updates_for_plugin($component) {
global $CFG;
$provider = \core\update\checker::instance();
if (!$provider->enabled() or during_initial_install()) {
return null;
}
if (isset($CFG->updateminmaturity)) {
$minmaturity = $CFG->updateminmaturity;
} else {
// This can happen during the very first upgrade to 2.3.
$minmaturity = MATURITY_STABLE;
}
return $provider->get_update_info($component, array('minmaturity' => $minmaturity));
}
/**
* Check to see if the given plugin folder can be removed by the web server process.
*
......
......@@ -405,33 +405,6 @@ abstract class base {
return isset($enabled[$this->name]);
}
/**
* Populates the property {@link $availableupdates}
*
* This is supposed to be called by {@link self::available_updates()} only
* to lazy load the data once they are first requested.
*/
protected function load_available_updates() {
global $CFG;
$provider = \core\update\checker::instance();
if (!$provider->enabled() or during_initial_install()) {
$this->availableupdates = array();
return;
}
if (isset($CFG->updateminmaturity)) {
$minmaturity = $CFG->updateminmaturity;
} else {
// This can happen during the very first upgrade to 2.3.
$minmaturity = MATURITY_STABLE;
}
$this->availableupdates = $provider->get_update_info($this->component,
array('minmaturity' => $minmaturity));
}
/**
* If there are updates for this plugin available, returns them.
*
......@@ -439,16 +412,20 @@ abstract class base {
* is available. Returns null if there is no update available or if the update
* availability is unknown.
*
* Populates the property {@link $availableupdates} on first call (lazy
* loading).
*
* @return array|null
*/
public function available_updates() {
if ($this->availableupdates === null) {
// Lazy load the information about available updates.
$this->load_available_updates();
$this->availableupdates = $this->pluginman->load_available_updates_for_plugin($this->component);
}
if (empty($this->availableupdates) or !is_array($this->availableupdates)) {
$this->availableupdates = array();
return null;
}
......
......@@ -35,4 +35,40 @@ class testable_core_plugin_manager extends core_plugin_manager {
/** @var testable_core_plugin_manager holds the singleton instance */
protected static $singletoninstance;
/**
* Mockup implementation of loading available updates info.
*
* This testable implementation does not actually use
* {@link \core\update\checker}. Instead, it provides hard-coded list of
* fictional available updates for some standard plugin.
*
* @param string $component
* @return array|null array of \core\update\info objects or null
*/
public function load_available_updates_for_plugin($component) {
if ($component === 'mod_forum') {
$updates = array();
$updates[] = new \core\update\info($component, array(
'version' => '2002073008',
'release' => 'Forum 0.1',
'maturity' => MATURITY_ALPHA,
'url' => 'https://en.wikipedia.org/wiki/Moodle',
'download' => 'https://moodle.org/plugins/pluginversion.php?id=1',
'downloadmd5' => md5('I can not think of anything funny to type here'),
));
$updates[] = new \core\update\info($component, array(
'version' => '2999122400',
'release' => 'Forum NG',
'maturity' => MATURITY_BETA,
));
return $updates;
}
return null;
}
}
......@@ -263,4 +263,29 @@ class core_plugin_manager_testcase extends advanced_testcase {
}
}
}
public function test_plugin_available_updates() {
$pluginman = testable_core_plugin_manager::instance();
foreach ($pluginman->get_plugins() as $type => $infos) {
foreach ($infos as $name => $info) {
$updates = $info->available_updates();
if ($info->component != 'mod_forum') {
$this->assertNull($updates);
} else {
$this->assertEquals(1, count($updates));
$update = array_shift($updates);
$this->assertInstanceOf('\core\update\info', $update);
$this->assertEquals('mod_forum', $update->component);
$this->assertEquals('2999122400', $update->version);
}
}
}
}
public function test_some_plugins_updatable() {
$pluginman = testable_core_plugin_manager::instance();
$this->assertTrue($pluginman->some_plugins_updatable());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment