diff --git a/lib/classes/plugin_manager.php b/lib/classes/plugin_manager.php index 757b21985e1..09096d8729b 100644 --- a/lib/classes/plugin_manager.php +++ b/lib/classes/plugin_manager.php @@ -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. * diff --git a/lib/classes/plugininfo/base.php b/lib/classes/plugininfo/base.php index fae917a6153..cb80f29b253 100644 --- a/lib/classes/plugininfo/base.php +++ b/lib/classes/plugininfo/base.php @@ -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; } diff --git a/lib/tests/fixtures/testable_plugin_manager.php b/lib/tests/fixtures/testable_plugin_manager.php index 847bff43ab9..26975b80e53 100644 --- a/lib/tests/fixtures/testable_plugin_manager.php +++ b/lib/tests/fixtures/testable_plugin_manager.php @@ -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; + } } diff --git a/lib/tests/plugin_manager_test.php b/lib/tests/plugin_manager_test.php index 3926e3de607..b17e3d75cdc 100644 --- a/lib/tests/plugin_manager_test.php +++ b/lib/tests/plugin_manager_test.php @@ -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()); + } }