Merge branch 'MDL-72907-master' of https://github.com/sarjona/moodle

This commit is contained in:
Shamim Rezaie 2021-11-17 02:02:51 +11:00
commit 8f65f24221
3 changed files with 170 additions and 15 deletions

View File

@ -142,6 +142,8 @@ if (($action == 'edit') || ($action == 'new')) {
$success = true;
if (!$repoid = $type->create()) {
$success = false;
} else {
add_to_config_log('repository_visibility', '', (int)$visible, $plugin);
}
$data = data_submitted();
}
@ -184,14 +186,14 @@ if (($action == 'edit') || ($action == 'new')) {
print_error('confirmsesskeybad', '', $baseurl);
}
$class = \core_plugin_manager::resolve_plugininfo_class('repository');
$class::enable_plugin($repository, true);
$class::enable_plugin($repository, 1);
$return = true;
} else if ($action == 'hide') {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
}
$class = \core_plugin_manager::resolve_plugininfo_class('repository');
$class::enable_plugin($repository, false);
$class::enable_plugin($repository, 0);
$return = true;
} else if ($action == 'delete') {
$repositorytype = repository::get_type_by_typename($repository);
@ -202,6 +204,8 @@ if (($action == 'edit') || ($action == 'new')) {
}
if ($repositorytype->delete($downloadcontents)) {
// Include this information into config changes table.
add_to_config_log('repository_visibility', $repositorytype->get_visible(), '', $repository);
core_plugin_manager::reset_caches();
redirect($baseurl);
} else {

View File

@ -26,11 +26,22 @@ namespace core\plugininfo;
use moodle_url, part_of_admin_tree, admin_externalpage;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
/**
* Class for repositories
*/
class repository extends base {
/** @var int Repository state, when it's enabled and visible. */
public const REPOSITORY_ON = 1;
/** @var int Repository state, when it's enabled but hidden. */
public const REPOSITORY_OFF = 0;
/** @var int Repository state, when it's disabled. */
public const REPOSITORY_DISABLED = -1;
/**
* Finds all enabled plugins, the result may include missing plugins.
* @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
@ -40,27 +51,50 @@ class repository extends base {
return $DB->get_records_menu('repository', null, 'type ASC', 'type, type AS val');
}
/**
* Enable or disable a plugin.
* When possible, the change will be stored into the config_log table, to let admins check when/who has modified it.
*
* @param string $pluginname The plugin name to enable/disable.
* @param int $enabled Whether the pluginname should be enabled and visible (1), enabled but hidden (0) or disabled (-1).
*
* @return bool Whether $pluginname has been updated or not.
*/
public static function enable_plugin(string $pluginname, int $enabled): bool {
global $DB;
$haschanged = false;
$repositorytype = \repository::get_type_by_typename($pluginname);
if (empty($repositorytype)) {
throw new \moodle_exception('invalidplugin', 'repository', '', $pluginname);
}
// The visibility will be only changed if the repository exists in database. Otherwise, this method won't do anything.
// Enabled repositories exist in 'repository' table.
// Visible = REPOSITORY_ON ==> Enabled and visible.
// Visible = REPOSITORY_OFF ==> Enabled but hidden.
// Disabled repositories don't exist in 'repository' table.
if ($plugin = $DB->get_record('repository', ['type' => $pluginname])) {
// The plugin is enabled.
$oldvalue = $plugin->visible;
// Only set visibility if it's different from the current value.
if ($oldvalue != $enabled) {
$haschanged = true;
$repositorytype->update_visibility($enabled);
// Include this information into config changes table.
add_to_config_log('repository_visibility', $oldvalue, $enabled, $pluginname);
\core_plugin_manager::reset_caches();
$repositorytype = \repository::get_type_by_typename($pluginname);
if ($enabled == self::REPOSITORY_DISABLED) {
$haschanged = $repositorytype->delete();
$enabled = '';
} else if ($oldvalue != $enabled) {
$haschanged = $repositorytype->update_visibility($enabled);
}
} else {
// Not all repositories have their own 'repository' record created. Disabled repositories don't have one.
// Make changes only when we want to enable repository.
$oldvalue = '';
if ($enabled == self::REPOSITORY_ON || $enabled == self::REPOSITORY_OFF) {
$type = new \repository_type($pluginname, [], $enabled);
if (!$haschanged = $type->create()) {
throw new \moodle_exception('invalidplugin', 'repository', '', $pluginname);
}
}
}
if ($haschanged) {
// Include this information into config changes table.
add_to_config_log('repository_visibility', $oldvalue, $enabled, $pluginname);
\core_plugin_manager::reset_caches();
}
return $haschanged;

View File

@ -0,0 +1,117 @@
<?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/>.
/**
* Unit tests for repository plugin manager class.
*
* @package core
* @copyright 2021 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types = 1);
namespace core\plugininfo;
/**
* Tests of the repository plugin manager.
*/
class repository_test extends \advanced_testcase {
/**
* Test the enable_plugin function to check that it enables and disables repository plugins properly.
*
* @dataProvider enable_plugin_provider
* @param string $pluginname Repository to enable.
* @param int|null $initialvisibility Initialvalue for visibility field.
* @param int $newstatus New enabled status for the plugin.
* @param bool $result Wether the repository is part of enabled plugin list or not.
*/
public function test_enable_plugin(string $pluginname, ?int $initialvisibility, int $newstatus, bool $result): void {
global $DB;
$this->resetAfterTest();
$DB->set_field('repository', 'visible', $initialvisibility, ['type' => $pluginname]);
repository::enable_plugin($pluginname, $newstatus);
$enableplugins = repository::get_enabled_plugins();
$this->assertSame($result, in_array($pluginname, $enableplugins));
}
/**
* Data provider for the load_disk_version tests for testing with invalid supported fields.
*
* @return array
*/
public function enable_plugin_provider(): array {
return [
'Disable an enable and visible repository' => [
'pluginname' => 'contentbank',
'initialvisibility' => repository::REPOSITORY_ON,
'newstatus' => repository::REPOSITORY_DISABLED,
'result' => false,
],
'Disable an enable and hidden repository' => [
'pluginname' => 'contentbank',
'initialvisibility' => repository::REPOSITORY_OFF,
'newstatus' => repository::REPOSITORY_DISABLED,
'result' => false,
],
'Disable a disabled repository' => [
'pluginname' => 'coursefiles',
'initialvisibility' => null,
'newstatus' => repository::REPOSITORY_DISABLED,
'result' => false
],
'Enable an enable and visible repository' => [
'pluginname' => 'contentbank',
'initialvisibility' => repository::REPOSITORY_ON,
'newstatus' => repository::REPOSITORY_ON,
'result' => true,
],
'Enable an enable and hidden repository' => [
'pluginname' => 'contentbank',
'initialvisibility' => repository::REPOSITORY_OFF,
'newstatus' => repository::REPOSITORY_ON,
'result' => true,
],
'Enable a disabled repository' => [
'pluginname' => 'coursefiles',
'initialvisibility' => null,
'newstatus' => repository::REPOSITORY_ON,
'result' => true,
],
'Enable but hide an enable and visible repository' => [
'pluginname' => 'contentbank',
'initialvisibility' => repository::REPOSITORY_ON,
'newstatus' => repository::REPOSITORY_OFF,
'result' => true,
],
'Enable but hide an enable and hidden repository' => [
'pluginname' => 'contentbank',
'initialvisibility' => repository::REPOSITORY_OFF,
'newstatus' => repository::REPOSITORY_OFF,
'result' => true,
],
'Enable but hide a disabled repository' => [
'pluginname' => 'coursefiles',
'initialvisibility' => null,
'newstatus' => repository::REPOSITORY_OFF,
'result' => true,
],
];
}
}