2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Provides testable_core_plugin_manager class.
22 * @copyright 2015 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once(__DIR__
.'/testable_update_api.php');
31 * Testable variant of the core_plugin_manager
33 * @copyright 2015 David Mudrak <david@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class testable_core_plugin_manager
extends core_plugin_manager
{
38 /** @var testable_core_plugin_manager holds the singleton instance */
39 protected static $singletoninstance;
42 * Allows us to inject items directly into the plugins info tree.
44 * Do not forget to call our reset_caches() after using this method to force a new
47 * @param string $type plugin type
48 * @param string $name plugin name
49 * @param \core\plugininfo\base $plugininfo plugin info class
51 public function inject_testable_plugininfo($type, $name, \core\plugininfo\base
$plugininfo) {
53 // Let the parent initialize the ->pluginsinfo tree.
54 parent
::get_plugins();
56 // Inject the additional plugin info.
57 $this->pluginsinfo
[$type][$name] = $plugininfo;
61 * Returns testable subclass of the client.
63 * @return \core\update\testable_api
65 protected function get_update_api_client() {
66 return \core\update\testable_api
::client();
70 * Mockup implementation of loading available updates info.
72 * This testable implementation does not actually use
73 * {@link \core\update\checker}. Instead, it provides hard-coded list of
74 * fictional available updates for our foo_bar plugin.
76 * Note there is a difference in the behaviour as the actual update API
77 * does not return info of lower version than requested. To mock up well,
78 * make sure the injected foo_bar testable plugin info has version lower
79 * than the lowest one returned here.
81 * @param string $component
82 * @return array|null array of \core\update\info objects or null
84 public function load_available_updates_for_plugin($component) {
86 if ($component === 'foo_bar') {
89 $updates[] = new \core\update\
info($component, array(
90 'version' => '2015093000',
91 'release' => 'Foo bar 15.09.30 beta',
92 'maturity' => MATURITY_BETA
,
95 $updates[] = new \core\update\
info($component, array(
96 'version' => '2015100400',
97 'release' => 'Foo bar 15.10.04',
98 'maturity' => MATURITY_STABLE
,
101 $updates[] = new \core\update\
info($component, array(
102 'version' => '2015100500',
103 'release' => 'Foo bar 15.10.05 beta',
104 'maturity' => MATURITY_BETA
,
114 * Adds fake plugin information from record.
116 * @param testable_plugininfo_base $record
119 public function add_fake_plugin_info($record): void
{
120 $this->load_present_plugins();
122 $this->presentplugins
[$record->type
][$record->name
] = $record;