first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,44 @@
<?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/>.
namespace tool_admin_presets\event;
use core\event\base;
/**
* Admin tool presets event class deleted.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_deleted extends base {
public static function get_name(): string {
return get_string('eventpresetdeleted', 'tool_admin_presets');
}
public function get_description(): string {
return "User {$this->userid} has deleted the preset with id {$this->objectid}.";
}
protected function init(): void {
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'adminpresets';
}
}
@@ -0,0 +1,49 @@
<?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/>.
namespace tool_admin_presets\event;
use core\event\base;
/**
* Admin tool presets event class downloaded.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_downloaded extends base {
public static function get_name(): string {
return get_string('eventpresetdownloaded', 'tool_admin_presets');
}
public function get_description(): string {
return "User {$this->userid} has downloaded the preset with id {$this->objectid}.";
}
public function get_url(): \moodle_url {
return new \moodle_url('/admin/tool/admin_presets/index.php',
['action' => 'export', 'mode' => 'download_xml', 'id' => $this->objectid, 'sesskey' => sesskey()]);
}
protected function init(): void {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'adminpresets';
}
}
@@ -0,0 +1,49 @@
<?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/>.
namespace tool_admin_presets\event;
use core\event\base;
/**
* Admin tool presets event class exported.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_exported extends base {
public static function get_name(): string {
return get_string('eventpresetexported', 'tool_admin_presets');
}
public function get_description(): string {
return "User {$this->userid} has exported the preset with id {$this->objectid}.";
}
public function get_url(): \moodle_url {
return new \moodle_url('/admin/tool/admin_presets/index.php',
['action' => 'load', 'mode' => 'preview', 'id' => $this->objectid]);
}
protected function init(): void {
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'adminpresets';
}
}
@@ -0,0 +1,49 @@
<?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/>.
namespace tool_admin_presets\event;
use core\event\base;
/**
* Admin tool presets event class imported.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_imported extends base {
public static function get_name(): string {
return get_string('eventpresetimported', 'tool_admin_presets');
}
public function get_description(): string {
return "User {$this->userid} has imported the preset with id {$this->objectid}.";
}
public function get_url(): \moodle_url {
return new \moodle_url('/admin/tool/admin_presets/index.php',
['action' => 'load', 'mode' => 'preview', 'id' => $this->objectid]);
}
protected function init(): void {
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'adminpresets';
}
}
@@ -0,0 +1,49 @@
<?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/>.
namespace tool_admin_presets\event;
use core\event\base;
/**
* Admin tool presets event class loaded.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_loaded extends base {
public static function get_name(): string {
return get_string('eventpresetloaded', 'tool_admin_presets');
}
public function get_description(): string {
return "User {$this->userid} has loaded the preset with id {$this->objectid}.";
}
public function get_url(): \moodle_url {
return new \moodle_url('/admin/tool/admin_presets/index.php',
['action' => 'load', 'mode' => 'preview', 'id' => $this->objectid]);
}
protected function init(): void {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'adminpresets';
}
}
@@ -0,0 +1,49 @@
<?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/>.
namespace tool_admin_presets\event;
use core\event\base;
/**
* Admin tool presets event class previewed.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_previewed extends base {
public static function get_name(): string {
return get_string('eventpresetpreviewed', 'tool_admin_presets');
}
public function get_description(): string {
return "User {$this->userid} has previewed the preset with id {$this->objectid}.";
}
public function get_url(): \moodle_url {
return new \moodle_url('/admin/tool/admin_presets/index.php',
['action' => 'load', 'mode' => 'preview', 'id' => $this->objectid]);
}
protected function init(): void {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'adminpresets';
}
}
@@ -0,0 +1,44 @@
<?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/>.
namespace tool_admin_presets\event;
use core\event\base;
/**
* Admin tool presets event class reverted.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_reverted extends base {
public static function get_name(): string {
return get_string('eventpresetreverted', 'tool_admin_presets');
}
public function get_description(): string {
return "User {$this->userid} has reverted the preset with id {$this->objectid}.";
}
protected function init(): void {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'adminpresets';
}
}
@@ -0,0 +1,48 @@
<?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/>.
namespace tool_admin_presets\event;
use core\event\base;
/**
* Admin tool presets event class listed.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class presets_listed extends base {
public static function get_name(): string {
return get_string('eventpresetslisted', 'tool_admin_presets');
}
public function get_description(): string {
return "User {$this->userid} listed the system presets.";
}
public function get_url(): \moodle_url {
return new \moodle_url('/admin/tool/admin_presets/index.php');
}
protected function init(): void {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'adminpresets';
}
}
@@ -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/>.
namespace tool_admin_presets\form;
defined('MOODLE_INTERNAL') || die();
use moodleform;
global $CFG;
require_once($CFG->dirroot . '/lib/formslib.php');
/**
* Form for loading continue button.
*
* @package tool_admin_presets
* @copyright 2021 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class continue_form extends moodleform {
public function definition(): void {
$this->add_action_buttons(false, get_string('continue'));
}
}
@@ -0,0 +1,61 @@
<?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/>.
namespace tool_admin_presets\form;
use moodleform;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/lib/formslib.php');
/**
* Form for exporting settings.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class export_form extends moodleform {
public function definition(): void {
global $USER;
$mform = &$this->_form;
// Preset attributes.
$mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="60"');
$mform->addRule('name', null, 'required', null, 'client');
$mform->setType('name', PARAM_TEXT);
$mform->addElement('editor', 'comments', get_string('description'));
$mform->setType('comments', PARAM_CLEANHTML);
$mform->addElement('text', 'author',
get_string('author', 'tool_admin_presets'), 'maxlength="254" size="60"');
$mform->setType('author', PARAM_TEXT);
$mform->setDefault('author', $USER->firstname . ' ' . $USER->lastname);
$mform->addElement('checkbox', 'includesensiblesettings',
get_string('includesensiblesettings', 'tool_admin_presets'));
$mform->setDefault('includesensiblesettings', 0);
$mform->addHelpButton('includesensiblesettings', 'includesensiblesettings', 'tool_admin_presets');
$this->add_action_buttons(true, get_string('actionexportbutton', 'tool_admin_presets'));
}
}
@@ -0,0 +1,51 @@
<?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/>.
namespace tool_admin_presets\form;
defined('MOODLE_INTERNAL') || die();
use moodleform;
global $CFG;
require_once($CFG->dirroot . '/lib/formslib.php');
/**
* Form for importting settings.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_form extends moodleform {
public function definition(): void {
$mform = &$this->_form;
// Rename preset input.
$mform->addElement('text', 'name',
get_string('renamepreset', 'tool_admin_presets'), 'maxlength="254" size="40"');
$mform->setType('name', PARAM_TEXT);
// File upload.
$mform->addElement('filepicker', 'xmlfile', get_string('selectfile', 'tool_admin_presets'), null,
['accepted_types' => ['.xml']]);
$mform->addRule('xmlfile', null, 'required');
$this->add_action_buttons(true, get_string('import', 'tool_admin_presets'));
}
}
@@ -0,0 +1,47 @@
<?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/>.
namespace tool_admin_presets\form;
defined('MOODLE_INTERNAL') || die();
use moodleform;
global $CFG;
require_once($CFG->dirroot . '/lib/formslib.php');
/**
* Form for loading settings.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class load_form extends moodleform {
public function definition(): void {
$mform = &$this->_form;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$html = '<div class="alert alert-info">' . get_string('applypresetdescription', 'tool_admin_presets') . '</div>';
$mform->addElement('html', $html);
$this->add_action_buttons(true, get_string('loadselected', 'tool_admin_presets'));
}
}
@@ -0,0 +1,209 @@
<?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/>.
namespace tool_admin_presets\local\action;
use context_system;
use moodle_url;
use core_adminpresets\manager;
use tool_admin_presets\output\presets_list;
use tool_admin_presets\output\export_import;
/**
* Admin tool presets main controller class.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class base {
/** @var array Array map for the events. **/
protected static $eventsactionsmap = [
'base' => 'presets_listed',
'delete' => 'preset_deleted',
'export' => 'preset_exported',
'import' => 'preset_imported',
'preview' => 'preset_previewed',
'load' => 'preset_loaded',
'rollback' => 'preset_reverted',
'download_xml' => 'preset_downloaded'
];
/** @var string The main action (delete, export, import, load...). **/
protected $action;
/** @var string The mode (show, execute...). **/
protected $mode;
/** @var int Admin preset identifier. **/
protected $id;
/** @var int The output content to display in the page. **/
protected $outputs;
/** @var \moodleform The moodle form to display in the page. **/
protected $moodleform;
/** @var manager The manager helper class instance. **/
protected $manager;
/**
* Loads common class attributes.
*/
public function __construct() {
$this->manager = new manager();
$this->action = optional_param('action', 'base', PARAM_ALPHA);
$this->mode = optional_param('mode', 'show', PARAM_ALPHAEXT);
$this->id = optional_param('id', false, PARAM_INT);
}
/**
* Method to list the presets available on the system
*
* It allows users to access the different preset
* actions (preview, load, download, delete and rollback)
*/
public function show(): void {
global $DB, $OUTPUT;
$options = new export_import();
$this->outputs = $OUTPUT->render($options);
$presets = $DB->get_records('adminpresets');
$list = new presets_list($presets, true);
$this->outputs .= $OUTPUT->render($list);
}
/**
* Main display method
*
* Prints the block header and the common block outputs, the
* selected action outputs, his form and the footer
*
* $outputs value depends on $mode and $action selected
*/
public function display(): void {
global $OUTPUT;
$this->display_header();
// Other outputs.
if (!empty($this->outputs)) {
echo $this->outputs;
}
// Form.
if ($this->moodleform) {
$this->moodleform->display();
}
// Footer.
echo $OUTPUT->footer();
}
/**
* Displays the header
*/
protected function display_header(): void {
global $PAGE, $OUTPUT, $SITE;
// Strings.
$titlestr = get_string('pluginname', 'tool_admin_presets');
// Header.
$PAGE->set_title($titlestr);
$PAGE->set_heading($SITE->fullname);
$title = $this->get_title();
$text = $this->get_explanatory_description();
// Only add it to the navbar if it's different to the plugin name (to avoid duplicates in the navbar).
if ($title != get_string('pluginname', 'tool_admin_presets')) {
$PAGE->navbar->add($title);
}
if ($node = $PAGE->settingsnav->find('tool_admin_presets', \navigation_node::TYPE_SETTING)) {
$node->make_active();
}
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
if ($text) {
echo $OUTPUT->box($text);
}
}
/**
* Get page title for this action.
*
* @return string The page title to display into the page.
*/
protected function get_title(): string {
if ($this->action == 'base') {
return get_string('pluginname', 'tool_admin_presets');
}
return get_string($this->action . $this->mode, 'tool_admin_presets');
}
/**
* Get explanatory description to be displayed below the heading. It's optional and might change depending on the
* action and the mode.
*
* @return string|null The explanatory description for the current action and mode.
*/
protected function get_explanatory_description(): ?string {
$text = null;
if ($this->action == 'base') {
$text = get_string('basedescription', 'tool_admin_presets');
}
return $text;
}
/**
* Trigger an event based on the current action.
*
* @return void
*/
public function log(): void {
// The only read action we store is list presets and preview.
$islist = ($this->action == 'base' && $this->mode == 'show');
$ispreview = ($this->action == 'load' && $this->mode == 'show');
if ($this->mode != 'show' || $islist || $ispreview) {
$action = $this->action;
if ($ispreview) {
$action = 'preview';
}
if ($this->mode != 'execute' && $this->mode != 'show') {
$action = $this->mode;
}
if (array_key_exists($action, self::$eventsactionsmap)) {
$eventnamespace = '\\tool_admin_presets\\event\\' . self::$eventsactionsmap[$action];
$eventdata = [
'context' => context_system::instance(),
'objectid' => $this->id
];
$event = $eventnamespace::create($eventdata);
$event->trigger();
}
}
}
}
@@ -0,0 +1,78 @@
<?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/>.
namespace tool_admin_presets\local\action;
use core_adminpresets\manager;
use moodle_exception;
/**
* This class extends base class and handles delete function.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete extends base {
/**
* Shows a confirm box
*/
public function show(): void {
global $DB, $OUTPUT;
// Check the preset exists (cannot delete the pre-installed core "Starter" and "Full" presets).
$presetdata = $DB->get_record('adminpresets', ['id' => $this->id, 'iscore' => manager::NONCORE_PRESET], 'name');
if ($presetdata) {
$deletetext = get_string('deletepreset', 'tool_admin_presets', $presetdata->name);
$params = ['action' => $this->action, 'mode' => 'execute', 'id' => $this->id, 'sesskey' => sesskey()];
$confirmurl = new \moodle_url('/admin/tool/admin_presets/index.php', $params);
$cancelurl = new \moodle_url('/admin/tool/admin_presets/index.php');
// If the preset was applied add a warning text.
if ($DB->get_records('adminpresets_app', ['adminpresetid' => $this->id])) {
$deletetext .= '<p><strong>' .
get_string("deletepreviouslyapplied", "tool_admin_presets") . '</strong></p>';
}
$displayoptions = [
'confirmtitle' => get_string('deletepresettitle', 'tool_admin_presets', $presetdata->name),
'continuestr' => get_string('delete')
];
$this->outputs = $OUTPUT->confirm($deletetext, $confirmurl, $cancelurl, $displayoptions);
} else {
throw new moodle_exception('errordeleting', 'core_adminpresets');
}
}
/**
* Delete the DB preset
*/
public function execute(): void {
require_sesskey();
$this->manager->delete_preset($this->id);
// Trigger the as it is usually triggered after execute finishes.
$this->log();
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
redirect($url);
}
}
@@ -0,0 +1,101 @@
<?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/>.
namespace tool_admin_presets\local\action;
defined('MOODLE_INTERNAL') || die();
use tool_admin_presets\form\export_form;
use moodle_exception;
global $CFG;
require_once($CFG->dirroot . '/lib/filelib.php');
require_once($CFG->dirroot . '/backup/util/xml/xml_writer.class.php');
require_once($CFG->dirroot . '/backup/util/xml/output/xml_output.class.php');
require_once($CFG->dirroot . '/backup/util/xml/output/memory_xml_output.class.php');
/**
* This class extends base class and handles export function.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class export extends base {
/**
* Shows the initial form to export/save admin settings.
*
* Loads the database configuration and prints
* the settings in a hierarchical table
*/
public function show(): void {
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'export', 'mode' => 'execute']);
$this->moodleform = new export_form($url);
}
/**
* Stores a preset into the DB.
*/
public function execute(): void {
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'export', 'mode' => 'execute']);
$this->moodleform = new export_form($url);
if ($data = $this->moodleform->get_data()) {
list($presetid, $settingsfound, $pluginsfound) = $this->manager->export_preset($data);
// Store it here for logging and other future id-oriented stuff.
$this->id = $presetid;
// If there are no settings nor plugins, an error should be raised.
if (!$settingsfound && !$pluginsfound) {
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'export']);
redirect($url, get_string('novalidsettingsselected', 'tool_admin_presets'));
}
}
// Trigger the as it is usually triggered after execute finishes.
$this->log();
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
redirect($url);
}
/**
* To download system presets.
*
* @return void preset file
* @throws dml_exception
* @throws moodle_exception
* @throws xml_output_exception
* @throws xml_writer_exception
*/
public function download_xml(): void {
require_sesskey();
list($xmlstr, $filename) = $this->manager->download_preset($this->id);
// Trigger the as it is usually triggered after execute finishes.
$this->log();
send_file($xmlstr, $filename, 0, 0, true, true);
}
protected function get_explanatory_description(): ?string {
return get_string('exportdescription', 'tool_admin_presets');
}
}
@@ -0,0 +1,88 @@
<?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/>.
namespace tool_admin_presets\local\action;
use tool_admin_presets\form\import_form;
/**
* This class extends base class and handles import function.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import extends base {
/**
* Displays the import moodleform
*/
public function show(): void {
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'import', 'mode' => 'execute']);
$this->moodleform = new import_form($url);
}
/**
* Imports the xmlfile into DB
*/
public function execute(): void {
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'import', 'mode' => 'execute']);
$this->moodleform = new import_form($url);
if ($this->moodleform->is_cancelled()) {
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
redirect($url);
}
if ($data = $this->moodleform->get_data()) {
// Getting the file.
$xmlcontent = $this->moodleform->get_file_content('xmlfile');
list($xml, $preset, $settingsfound, $pluginsfound) = $this->manager->import_preset($xmlcontent, $data->name);
if (!$xml) {
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'import']);
redirect($url, get_string('wrongfile', 'tool_admin_presets'));
}
// Store it here for logging and other future id-oriented stuff.
if (!is_null($preset)) {
$this->id = $preset->id;
}
// If there are no valid or selected settings, raise an error.
if (!$settingsfound && !$pluginsfound) {
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'import']);
redirect($url, get_string('novalidsettings', 'tool_admin_presets'));
}
// Trigger it after execute finishes.
$this->log();
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'id' => $preset->id]);
redirect($url);
}
}
protected function get_explanatory_description(): ?string {
$text = null;
if ($this->mode == 'show') {
$text = get_string('importdescription', 'tool_admin_presets');
}
return $text;
}
}
@@ -0,0 +1,176 @@
<?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/>.
namespace tool_admin_presets\local\action;
use moodle_exception;
use stdClass;
use tool_admin_presets\form\continue_form;
use tool_admin_presets\form\load_form;
use tool_admin_presets\output\presets_list;
/**
* This class extends base class and handles load function.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class load extends base {
/**
* Executes the settings load into the system
*/
public function execute(): void {
global $OUTPUT;
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'mode' => 'execute']);
$this->moodleform = new load_form($url);
if ($this->moodleform->is_cancelled()) {
redirect(new \moodle_url('/admin/tool/admin_presets/index.php?action=base'));
}
if ($this->moodleform->is_submitted() && $this->moodleform->is_validated() && ($this->moodleform->get_data())) {
// Apply preset settings and set plugins visibility.
[$applied, $skipped] = $this->manager->apply_preset($this->id);
if (empty($applied)) {
$message = [
'message' => get_string('nothingloaded', 'tool_admin_presets'),
'closebutton' => true,
'announce' => true,
];
} else {
$message = [
'message' => get_string('settingsappliednotification', 'tool_admin_presets'),
'closebutton' => true,
'announce' => true,
];
}
$application = new stdClass();
$applieddata = new stdClass();
$applieddata->show = !empty($applied);
$applieddata->message = $message;
$applieddata->heading = get_string('settingsapplied', 'tool_admin_presets');
$applieddata->caption = get_string('settingsapplied', 'tool_admin_presets');
$applieddata->settings = $applied;
$application->appliedchanges = $applieddata;
$skippeddata = new stdClass();
$skippeddata->show = !empty($skipped);
$skippeddata->heading = get_string('settingsnotapplied', 'tool_admin_presets');
$skippeddata->caption = get_string('settingsnotapplicable', 'tool_admin_presets');
$skippeddata->settings = $skipped;
$application->skippedchanges = $skippeddata;
$this->outputs = $OUTPUT->render_from_template('tool_admin_presets/settings_application', $application);
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
$this->moodleform = new continue_form($url);
}
}
/**
* Displays the select preset settings to select what to import.
* Loads the preset data and displays a settings tree.
*
* It checks the Moodle version and it only allows users to import
* the preset available settings.
*/
public function show(): void {
$this->display_preset(true);
}
/**
* Displays a preset information (name, description, settings different from the current configuration...).
*/
public function preview(): void {
$this->display_preset(false, false);
}
/**
* Method to prepare the information to preview/load the preset.
*
* @param bool $displayform Whether the form should be displayed in the page or not.
* @param bool $raiseexception Whether the exception should be raised or not when the preset doesn't exist. When it's set
* to false, a message is displayed, instead of raising the exception.
*/
protected function display_preset(bool $displayform = true, bool $raiseexception = true) {
global $DB, $OUTPUT;
$data = new stdClass();
$data->id = $this->id;
// Preset data.
if (!$preset = $DB->get_record('adminpresets', ['id' => $data->id])) {
if ($raiseexception) {
throw new moodle_exception('errornopreset', 'core_adminpresets');
} else {
$this->outputs = get_string('errornopreset', 'core_adminpresets');
return;
}
}
// Print preset basic data.
$list = new presets_list([$preset]);
$this->outputs = $OUTPUT->render($list);
// Simulate preset application to display settings and plugins that will change.
[$applied] = $this->manager->apply_preset($this->id, true);
// Order the applied array by the visiblename column.
if (!empty($applied)) {
$visiblenamecolumn = array_column($applied, 'visiblename');
array_multisort($visiblenamecolumn, SORT_ASC, $applied);
}
$application = new stdClass();
$applieddata = new stdClass();
$applieddata->show = !empty($applied);
$applieddata->heading = get_string('settingstobeapplied', 'tool_admin_presets');
$applieddata->caption = get_string('settingsapplied', 'tool_admin_presets');
$applieddata->settings = $applied;
$applieddata->beforeapplying = true;
$application->appliedchanges = $applieddata;
if ($displayform) {
if (empty($applied)) {
// Display a warning when no settings will be applied.
$applieddata->message = get_string('nosettingswillbeapplied', 'tool_admin_presets');
// Only display the Continue button.
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
$this->moodleform = new continue_form($url);
} else {
// Display the form to apply the preset.
$url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'mode' => 'execute']);
$this->moodleform = new load_form($url);
$this->moodleform->set_data($data);
}
}
$this->outputs .= $OUTPUT->render_from_template('tool_admin_presets/settings_application', $application);
}
protected function get_explanatory_description(): ?string {
$text = null;
if ($this->mode == 'show') {
$text = get_string('loaddescription', 'tool_admin_presets');
}
return $text;
}
}
@@ -0,0 +1,124 @@
<?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/>.
namespace tool_admin_presets\local\action;
use stdClass;
use tool_admin_presets\form\continue_form;
/**
* This class extends base class and handles rollback function.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rollback extends base {
/**
* Displays the different previous applications of the preset
*/
public function show(): void {
global $DB, $OUTPUT;
// Preset data.
$preset = $DB->get_record('adminpresets', ['id' => $this->id]);
// Applications data.
$context = new stdClass();
$applications = $DB->get_records('adminpresets_app', ['adminpresetid' => $this->id], 'time DESC');
$context->noapplications = !empty($applications);
$context->applications = [];
foreach ($applications as $application) {
$format = get_string('strftimedatetime', 'langconfig');
$user = $DB->get_record('user', ['id' => $application->userid]);
$rollbacklink = new \moodle_url(
'/admin/tool/admin_presets/index.php',
['action' => 'rollback', 'mode' => 'execute', 'id' => $application->id, 'sesskey' => sesskey()]
);
$context->applications[] = [
'timeapplied' => \core_date::strftime($format, (int)$application->time),
'user' => fullname($user),
'action' => $rollbacklink->out(false),
];
}
$this->outputs .= '<br/>' . $OUTPUT->heading(get_string('presetname', 'tool_admin_presets') . ': ' . $preset->name, 3);
$this->outputs = $OUTPUT->render_from_template('tool_admin_presets/preset_applications_list', $context);
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
$this->moodleform = new continue_form($url);
}
/**
* Executes the application rollback
*
* Each setting value is checked against the config_log->value
*/
public function execute(): void {
global $OUTPUT;
require_sesskey();
list($presetapp, $rollback, $failures) = $this->manager->revert_preset($this->id);
if (!is_null($presetapp)) {
// Change $this->id to point to the preset.
$this->id = $presetapp->adminpresetid;
}
$appliedchanges = new stdClass();
$appliedchanges->show = !empty($rollback);
$appliedchanges->caption = get_string('rollbackresults', 'tool_admin_presets');
$appliedchanges->settings = $rollback;
$skippedchanges = new stdClass();
$skippedchanges->show = !empty($failures);
$skippedchanges->caption = get_string('rollbackfailures', 'tool_admin_presets');
$skippedchanges->settings = $failures;
$data = new stdClass();
$data->appliedchanges = $appliedchanges;
$data->skippedchanges = $skippedchanges;
$data->beforeapplying = true;
$this->outputs = $OUTPUT->render_from_template('tool_admin_presets/settings_rollback', $data);
$url = new \moodle_url('/admin/tool/admin_presets/index.php');
$this->moodleform = new continue_form($url);
}
protected function get_title(): string {
global $DB;
$title = '';
if ($preset = $DB->get_record('adminpresets', ['id' => $this->id])) {
$title = get_string($this->action . $this->mode, 'tool_admin_presets', $preset->name);
}
return $title;
}
protected function get_explanatory_description(): ?string {
$text = null;
if ($this->mode == 'show') {
$text = get_string('rollbackdescription', 'tool_admin_presets');
}
return $text;
}
}
@@ -0,0 +1,54 @@
<?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/>.
/**
* tool_admin_presets export and import option renderer
*
* @package tool_admin_presets
* @copyright 2021 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_admin_presets\output;
use renderable;
use templatable;
use renderer_base;
use stdClass;
/**
* Class containing data for export and import template
*
* @copyright 2021 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class export_import implements renderable, templatable {
/**
* Export the data.
*
* @param renderer_base $output
* @return stdClass
*/
public function export_for_template(renderer_base $output): stdClass {
$context = new stdClass();
$exportlink = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'export']);
$exportbutton = new \single_button($exportlink, get_string('actionexport', 'tool_admin_presets'), 'get');
$context->export = $exportbutton->export_for_template($output);
$importlink = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'import']);
$importbutton = new \single_button($importlink, get_string('actionimport', 'tool_admin_presets'), 'get');
$context->import = $importbutton->export_for_template($output);
return $context;
}
}
@@ -0,0 +1,139 @@
<?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/>.
/**
* tool_admin_presets specific renderers
*
* @package tool_admin_presets
* @copyright 2021 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_admin_presets\output;
use core_adminpresets\manager;
use renderable;
use templatable;
use renderer_base;
use stdClass;
/**
* Class containing data for admin_presets tool
*
* @copyright 2021 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class presets_list implements renderable, templatable {
/**
* @var stdClass[] Array of admin presets.
*/
private $presets;
/**
* @var bool Wether the action menu is visible.
*/
private $showactions;
/**
* Construct this renderable.
*
* @param stdClass[] $presets Array of existing admin presets.
* @param bool $showactions Whether actions should be displayed or not.
*/
public function __construct(array $presets, bool $showactions = false) {
$this->presets = $presets;
$this->showactions = $showactions;
}
/**
* Export the data.
*
* @param renderer_base $output
* @return stdClass
*/
public function export_for_template(renderer_base $output): stdClass {
global $DB;
$context = new stdClass();
$context->presets = [];
foreach ($this->presets as $preset) {
if ($preset->timeimported) {
$timeimportedstring = userdate($preset->timeimported);
} else {
$timeimportedstring = '';
}
$data = [
'name' => format_text($preset->name, FORMAT_PLAIN),
'description' => format_text($preset->comments, FORMAT_HTML),
'release' => format_text($preset->moodlerelease, FORMAT_PLAIN),
'author' => format_text($preset->author, FORMAT_PLAIN),
'site' => format_text(clean_text($preset->site, PARAM_URL), FORMAT_PLAIN),
'timecreated' => userdate($preset->timecreated),
'timeimported' => $timeimportedstring
];
if ($this->showactions) {
// Preset actions.
$actionsmenu = new \action_menu();
$actionsmenu->set_menu_trigger(get_string('actions'));
$actionsmenu->set_owner_selector('preset-actions-' . $preset->id);
$loadlink = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'id' => $preset->id]);
$actionsmenu->add(new \action_menu_link_secondary(
$loadlink, new \pix_icon('t/play', ''),
get_string('applyaction', 'tool_admin_presets')
));
$downloadlink = new \moodle_url('/admin/tool/admin_presets/index.php',
['action' => 'export', 'mode' => 'download_xml', 'sesskey' => sesskey(), 'id' => $preset->id]
);
$actionsmenu->add(new \action_menu_link_secondary(
$downloadlink,
new \pix_icon('t/download', ''),
get_string('download')
));
// Delete button won't be displayed for the pre-installed core "Starter" and "Full" presets.
if ($preset->iscore == manager::NONCORE_PRESET) {
$deletelink = new \moodle_url('/admin/tool/admin_presets/index.php',
['action' => 'delete', 'id' => $preset->id]
);
$actionsmenu->add(new \action_menu_link_secondary(
$deletelink,
new \pix_icon('i/delete', ''),
get_string('delete')
));
}
// Look for preset applications.
if ($DB->get_records('adminpresets_app', ['adminpresetid' => $preset->id])) {
$params = ['action' => 'rollback', 'id' => $preset->id];
$rollbacklink = new \moodle_url('/admin/tool/admin_presets/index.php', $params);
$actionsmenu->add(new \action_menu_link_secondary(
$rollbacklink,
new \pix_icon('i/reload', ''),
get_string('showhistory', 'tool_admin_presets')
));
}
$data['actions'] = $actionsmenu->export_for_template($output);
}
$context->presets[] = $data;
}
$context->nopresets = empty($context->presets);
$context->showactions = $this->showactions;
return $context;
}
}
@@ -0,0 +1,37 @@
<?php
// This file is part of The Course Module Navigation Block
//
// 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/>.
namespace tool_admin_presets\privacy;
/**
* Admin tool presets this file handle privacy provider.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+66
View File
@@ -0,0 +1,66 @@
<?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/>.
/**
* Admin tool presets plugin to load some settings.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
global $CFG, $PAGE;
$action = optional_param('action', 'base', PARAM_ALPHA);
$mode = optional_param('mode', 'show', PARAM_ALPHAEXT);
require_login();
if (!$context = context_system::instance()) {
throw new moodle_exception('wrongcontext', 'error');
}
require_capability('moodle/site:config', $context);
// Loads the required action class and form.
$fileclassname = $action;
$classname = 'tool_admin_presets\\local\\action\\'.$action;
if (!class_exists($classname)) {
throw new moodle_exception('falseaction', 'tool_admin_presets', $action);
}
$url = new moodle_url('/admin/tool/admin_presets/index.php');
$url->param('action', $action);
$url->param('mode', $mode);
$PAGE->set_url($url);
$PAGE->set_pagelayout('admin');
$PAGE->set_context($context);
$PAGE->set_primary_active_tab('siteadminnode');
// Executes the required action.
$instance = new $classname();
if (!method_exists($instance, $mode)) {
throw new moodle_exception('falsemode', 'tool_admin_presets', $mode);
}
// Executes the required method and displays output.
$instance->$mode();
$instance->log();
$instance->display();
@@ -0,0 +1,95 @@
<?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/>.
/**
* Admin tool presets plugin to load some settings.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['actionexport'] = 'Create preset';
$string['actionexportbutton'] = 'Create preset';
$string['actionimport'] = 'Import preset';
$string['actualvalue'] = 'Actual value';
$string['applyaction'] = 'Review settings and apply';
$string['applypresetdescription'] = 'If you change your mind after applying this preset, you can undo the setting changes via \'Show version history\' in the preset actions menu.';
$string['author'] = 'Author';
$string['basedescription'] = 'Presets allow you to easily switch between different site admin configurations. After selecting a preset, you can turn on more features any time as required.';
$string['created'] = 'Created';
$string['currentvalue'] = 'Current value';
$string['deletepreset'] = 'Are you sure you want to delete the site admin preset {$a}?';
$string['deletepreviouslyapplied'] = 'This preset has been previously applied. Deleting a preset removes it from your site completely. You will not be able to revert your settings to how they were before applying this preset.';
$string['deletepresettitle'] = 'Delete {$a} preset?';
$string['deleteshow'] = 'Delete site admin preset';
$string['eventpresetdeleted'] = 'Preset deleted';
$string['eventpresetdownloaded'] = 'Preset downloaded';
$string['eventpresetexported'] = 'Preset created';
$string['eventpresetimported'] = 'Preset imported';
$string['eventpresetloaded'] = 'Preset applied';
$string['eventpresetpreviewed'] = 'Preset previewed';
$string['eventpresetreverted'] = 'Preset restored';
$string['eventpresetslisted'] = 'Presets have been listed';
$string['exportdescription'] = 'Save all your current site admin settings as a preset to share or reuse.';
$string['exportshow'] = 'Create site admin preset';
$string['falseaction'] = 'Action not supported in this version.';
$string['falsemode'] = 'Mode not supported in this version.';
$string['import'] = 'Import';
$string['imported'] = 'Imported';
$string['importdescription'] = 'Import site admin settings as a preset to apply to your site.';
$string['importexecute'] = 'Import site admin preset';
$string['importshow'] = 'Import site admin preset';
$string['includesensiblesettings'] = 'Include settings with passwords';
$string['includesensiblesettings_help'] = 'Settings with passwords contain sensitive information specific to your site. Only include these settings if you are creating a preset to reuse on your site.';
$string['loaddescription'] = 'Review the setting changes before applying this preset.';
$string['loadexecute'] = 'Site admin preset applied';
$string['loadpreview'] = 'Preview site admin preset';
$string['loadselected'] = 'Apply';
$string['loadshow'] = 'Apply site admin preset';
$string['newvalue'] = 'New value';
$string['nopresets'] = 'You don\'t have any site admin preset.';
$string['nosettingswillbeapplied'] = 'These settings are the same as the current settings; there are no changes to apply.';
$string['nothingloaded'] = 'No setting changes have been made because the settings in the preset are the same as on your site.';
$string['novalidsettings'] = 'No valid settings';
$string['novalidsettingsselected'] = 'No valid settings selected';
$string['oldvalue'] = 'Old value';
$string['pluginname'] = 'Site admin presets';
$string['presetapplicationslisttable'] = 'Site admin preset applications table';
$string['presetslisttable'] = 'Site admin presets table';
$string['presetmoodlerelease'] = 'Moodle release';
$string['presetname'] = 'Preset name';
$string['privacy:metadata'] = 'The Site admin presets tool does not store any personal data.';
$string['renamepreset'] = 'Name (optional)';
$string['rollback'] = 'Restore this version';
$string['rollbackdescription'] = 'Use the \'Restore this version\' link to revert to the settings just before the preset was applied.';
$string['rollbackexecute'] = 'Restored version from the site admin preset {$a}';
$string['rollbackfailures'] = 'The following settings cannot be reverted, as the value was changed after applying the preset.';
$string['rollbackresults'] = 'Settings successfully restored';
$string['rollbackshow'] = '{$a} preset version history';
$string['selectfile'] = 'Select file';
$string['settingname'] = 'Setting name';
$string['settingsapplied'] = 'Setting changes';
$string['settingsappliednotification'] = 'Review the following setting changes which have been applied.
<br/>If you change your mind, you can undo the setting changes via \'Show version history\' in the preset actions menu.';
$string['settingsnotapplicable'] = 'Settings not applicable to this Moodle version';
$string['settingsnotapplied'] = 'Unchanged settings';
$string['settingstobeapplied'] = 'Setting changes';
$string['showhistory'] = 'Show version history';
$string['site'] = 'Site';
$string['timeapplied'] = 'Date';
$string['wrongfile'] = 'Wrong file';
+32
View File
@@ -0,0 +1,32 @@
<?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/>.
/**
* Admin tool presets plugin to load some settings.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if ($hassiteconfig) {
$ADMIN->add('root', new admin_externalpage('tool_admin_presets',
get_string('pluginname', 'tool_admin_presets'),
new moodle_url('/admin/tool/admin_presets/index.php')));
}
+15
View File
@@ -0,0 +1,15 @@
.admin_presets_error {
color: red;
text-align: center;
}
.admin_presets_success {
color: green;
text-align: center;
}
.adminpreset_appdescription {
font-size: small;
margin-top: 40px;
margin-bottom: 20px;
}
@@ -0,0 +1,46 @@
{{!
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/>.
}}
{{!
@template tool_admin_presets/export_import
Template for export and import options.
Example context (json):
{
"export":
{
"id": "button1",
"method": "get",
"url": "http://example.org/export",
"label": "Create preset",
"actions": []
},
"import":
{
"id": "button2",
"method": "get",
"url": "http://example.org/import",
"label": "Import preset",
"actions": []
}
}
}}
<div class="mt-2 mb-1 float-right">
{{#export}}{{> core/single_button}}{{/export}}
{{#import}}{{> core/single_button}}{{/import}}
</div>
@@ -0,0 +1,53 @@
{{!
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/>.
}}
{{!
@template tool_admin_presets/local/tables/actual_value_settings_table
Template for actual setting value table.
Example context (json):
{
"caption": "Applied settings",
"settings": [
{
"plugin": "quiz",
"visiblename": "Decimal places in grades",
"visiblevalue": "2"
}
]
}
}}
<table class="generaltable boxaligncenter admin_presets_skipped">
<caption class="accesshide">{{{caption}}}</caption>
<thead>
<tr>
<th scope="col">{{#str}}settingname, tool_admin_presets{{/str}}</th>
<th scope="col">{{#str}}plugin{{/str}}</th>
<th scope="col">{{#str}}actualvalue, tool_admin_presets{{/str}}</th>
</tr>
</thead>
<tbody>
{{#settings}}
<tr>
<td>{{{visiblename}}}</td>
<td>{{{plugin}}}</td>
<td>{{{visiblevalue}}}</td>
</tr>
{{/settings}}
</tbody>
</table>
@@ -0,0 +1,66 @@
{{!
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/>.
}}
{{!
@template tool_admin_presets/local/tables/old_and_new_value_settings_table
Template for setting comparison table, showing old and new values.
Context variables required for this template:
* beforeapplying - Whether the preset has been already applied or not.
Example context (json):
{
"caption": "Applied settings",
"settings": [
{
"plugin": "quiz",
"visiblename": "Decimal places in grades",
"oldvisiblevalue": "0",
"visiblevalue": "2"
}
]
}
}}
<table class="generaltable boxaligncenter admin_presets_applied">
<caption class="accesshide">{{{caption}}}</caption>
<thead>
<tr>
<th scope="col" class="w-50">{{#str}}settingname, tool_admin_presets{{/str}}</th>
<th scope="col" class="w-25">{{#str}}plugin{{/str}}</th>
<th scope="col">
{{^beforeapplying}}
{{#str}}oldvalue, tool_admin_presets{{/str}}
{{/beforeapplying}}
{{#beforeapplying}}
{{#str}}currentvalue, tool_admin_presets{{/str}}
{{/beforeapplying}}
</th>
<th scope="col">{{#str}}newvalue, tool_admin_presets{{/str}}</th>
</tr>
</thead>
<tbody>
{{#settings}}
<tr>
<td>{{{visiblename}}}</td>
<td>{{{plugin}}}</td>
<td>{{{oldvisiblevalue}}}</td>
<td>{{{visiblevalue}}}</td>
</tr>
{{/settings}}
</tbody>
</table>
@@ -0,0 +1,64 @@
{{!
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/>.
}}
{{!
@template tool_admin_presets/preset_applications_list
Template for preset applications list.
Example context (json):
{
"noapplications": 0,
"applications": {
"show": 1,
"caption": "Applied settings",
"settings": [
{
"timeapplied": "Thursday, 9 September 2021, 3:18 PM",
"user": "Joseba Cilarte",
"action": "http://example.com/admin/tool/admin_presets/index.php?action=rollback&mode=execute&id=43"
}
]
}
}
}}
{{^noapplications}}
<div id="id_nopresets" class="box py-3 generalbox">
<p id="{{uniqid}}">{{#str}}nopresets, tool_admin_presets{{/str}}</p>
</div>
{{/noapplications}}
{{#noapplications}}
<table class="generaltable boxaligncenter">
<caption class="accesshide">{{#str}}presetapplicationslisttable, tool_admin_presets{{/str}}</caption>
<thead>
<tr>
<th scope="col">{{#str}}timeapplied, tool_admin_presets{{/str}}</th>
<th scope="col">{{#str}}user{{/str}}</th>
<th scope="col">{{#str}}actions{{/str}}</th>
</tr>
</thead>
<tbody>
{{#applications}}
<tr>
<td>{{{timeapplied}}}</td>
<td>{{{user}}}</td>
<td><a href="{{action}}">{{#str}}rollback, tool_admin_presets{{/str}}</a></td>
</tr>
{{/applications}}
</tbody>
</table>
{{/noapplications}}
@@ -0,0 +1,79 @@
{{!
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/>.
}}
{{!
@template tool_admin_presets/presets_list
Template for presets list.
Context variables required for this template:
* nopresets - Whether presets list is empty or not.
* showactions - Whether the actions should be displayed or not.
* presets - Presets to display in the list, with information such as name, description or author.
Example context (json):
{
"nopresets": 0,
"showactions": 1
}
}}
{{#nopresets}}
<div id="id_nopresets" class="box py-3 generalbox">
<p id="{{uniqid}}">{{#str}}nopresets, tool_admin_presets{{/str}}</p>
</div>
{{/nopresets}}
{{^nopresets}}
<table class="generaltable boxaligncenter mb-5">
<caption class="accesshide">{{#str}}presetslisttable, tool_admin_presets{{/str}}</caption>
<thead>
<tr>
<th scope="col">{{#str}}name{{/str}}</th>
<th scope="col">{{#str}}description{{/str}}</th>
{{^showactions}}
<th scope="col">{{#str}}presetmoodlerelease, tool_admin_presets{{/str}}</th>
<th scope="col">{{#str}}author, tool_admin_presets{{/str}}</th>
<th scope="col">{{#str}}site, tool_admin_presets{{/str}}</th>
<th scope="col">{{#str}}created, tool_admin_presets{{/str}}</th>
<th scope="col">{{#str}}imported, tool_admin_presets{{/str}}</th>
{{/showactions}}
{{#showactions}}
<th scope="col" aria-label="{{#str}}actions{{/str}}"></th>
{{/showactions}}
</tr>
</thead>
<tbody>
{{#presets}}
<tr>
<td>{{{name}}}</td>
<td>{{{description}}}</td>
{{^showactions}}
<td>{{{release}}}</td>
<td>{{{author}}}</td>
<td>{{{site}}}</td>
<td>{{{timecreated}}}</td>
<td>{{{timeimported}}}</td>
{{/showactions}}
{{#showactions}}
<td>
{{#actions}}{{> core/action_menu}}{{/actions}}
</td>
{{/showactions}}
</tr>
{{/presets}}
</tbody>
</table>
{{/nopresets}}
@@ -0,0 +1,59 @@
{{!
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/>.
}}
{{!
@template tool_admin_presets/settings_application
Template for preset loading result.
Example context (json):
{
"nopresets": 0,
"showactions": 1
}
}}
{{#appliedchanges.show}}
{{#appliedchanges.message}}
{{> core/notification_success}}
{{/appliedchanges.message}}
{{#appliedchanges.heading}}
<h3>{{{.}}}</h3>
{{/appliedchanges.heading}}
{{#appliedchanges}}{{> tool_admin_presets/local/tables/old_and_new_value_settings_table}}{{/appliedchanges}}
{{/appliedchanges.show}}
{{^appliedchanges.show}}
{{#appliedchanges}}
{{> core/notification_warning}}
{{/appliedchanges}}
{{/appliedchanges.show}}
{{#skippedchanges.show}}
{{#skippedchanges.heading}}
<h3>
<a data-toggle="collapse" href="#collapseSkippedChanges" role="button" aria-expanded="false" aria-controls="collapseSkippedChanges">
{{{.}}}
</a>
</h3>
{{/skippedchanges.heading}}
{{#skippedchanges}}
<div class="collapse" id="collapseSkippedChanges">
{{> tool_admin_presets/local/tables/actual_value_settings_table}}
</div>
{{/skippedchanges}}
{{/skippedchanges.show}}
@@ -0,0 +1,36 @@
{{!
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/>.
}}
{{!
@template tool_admin_presets/settings_rollback
Template for rolling preset back result.
Example context (json):
{
"nopresets": 0,
"showactions": 1
}
}}
{{#appliedchanges.show}}
<div class="alert alert-success">{{#str}}rollbackresults, tool_admin_presets{{/str}}</div>
{{#appliedchanges}}{{> tool_admin_presets/local/tables/old_and_new_value_settings_table}}{{/appliedchanges}}
{{/appliedchanges.show}}
{{#skippedchanges.show}}
<div class="alert alert-info">{{#str}}rollbackfailures, tool_admin_presets{{/str}}</div>
{{#skippedchanges}}{{> tool_admin_presets/local/tables/old_and_new_value_settings_table}}{{/skippedchanges}}
{{/skippedchanges.show}}
@@ -0,0 +1,154 @@
@tool @tool_admin_presets @javascript
Feature: I can apply presets
Background:
Given I log in as "admin"
Scenario: Default settings are equal to Full preset
Given I navigate to "Site admin presets" in site administration
And I should see "Full"
And I open the action menu in "Full" "table_row"
When I choose "Review settings and apply" in the open action menu
Then I should not see "Setting changes"
Scenario: Applying Starter Moodle preset changes status and settings
# Checking the settings before applying Full Moodle preset (we're only testing one of each type).
Given I navigate to "Plugins > Activity modules > Manage activities" in site administration
And "Disable Database" "icon" should exist in the "Database" "table_row"
And I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
And "Hide" "icon" should exist in the "Restriction by grouping" "table_row"
And I navigate to "Plugins > Blocks > Manage blocks" in site administration
And "Disable Logged in user" "icon" should exist in the "Logged in user" "table_row"
And I navigate to "Plugins > Course formats > Manage course formats" in site administration
And "Disable" "icon" should exist in the "Social" "table_row"
And I navigate to "Plugins > Question behaviours > Manage question behaviours" in site administration
And "Enabled" "icon" should exist in the "Immediate feedback with CBM" "table_row"
And I navigate to "Plugins > Question types > Manage question types" in site administration
And "Enabled" "icon" should exist in the "Calculated multichoice" "table_row"
When I navigate to "Site admin presets" in site administration
And I should see "Starter"
And I open the action menu in "Starter" "table_row"
And I choose "Review settings and apply" in the open action menu
And I should see "Setting changes"
# Checking all the settings to be applied for the Starter (if will help to identify possible regressions).
And I should see "Activities" in the "Setting changes" "table"
And I should see "Activity chooser tabs" in the "Setting changes" "table"
And I should see "Adaptive mode (no penalties)" in the "Setting changes" "table"
And I should see "Analytics" in the "Setting changes" "table"
And I should see "Blog menu" in the "Setting changes" "table"
And I should see "Blog tags" in the "Setting changes" "table"
And I should see "Calculated" in the "Setting changes" "table"
And I should see "Calculated multichoice" in the "Setting changes" "table"
And I should see "Calculated simple" in the "Setting changes" "table"
And I should see "Database" in the "Setting changes" "table"
And "Database" row "Current value" column of "Setting changes" table should contain "Enabled"
And "Database" row "New value" column of "Setting changes" table should contain "Disabled"
And I should see "Cohort sync" in the "Setting changes" "table"
And I should see "Comments" in the "Setting changes" "table"
And I should see "Course completion status" in the "Setting changes" "table"
And I should see "Courses" in the "Setting changes" "table"
And I should see "Database" in the "Setting changes" "table"
And I should see "Deferred feedback with CBM" in the "Setting changes" "table"
And I should see "Drag and drop markers" in the "Setting changes" "table"
And I should see "Drag and drop onto image" in the "Setting changes" "table"
And I should see "Embedded answers (Cloze)" in the "Setting changes" "table"
And I should see "Enable badges" in the "Setting changes" "table"
And "Enable badges" row "Current value" column of "Setting changes" table should contain "Yes"
And "Enable badges" row "New value" column of "Setting changes" table should contain "No"
And I should see "Enable blogs" in the "Setting changes" "table"
And I should see "Enable comments" in the "Setting changes" "table"
And I should see "Enable competencies" in the "core_competency" "table_row"
And I should see "Enable notes" in the "Setting changes" "table"
And I should see "Enable tags functionality" in the "Setting changes" "table"
And I should see "External tool" in the "Setting changes" "table"
And I should see "Flickr" in the "Setting changes" "table"
And I should see "Global search" in the "Setting changes" "table"
And I should see "Guest access" in the "Setting changes" "table"
And I should see "Guest login button" in the "Setting changes" "table"
And I should see "IMS content package" in the "Setting changes" "table"
And I should see "Immediate feedback with CBM" in the "Setting changes" "table"
And I should see "Latest badges" in the "Setting changes" "table"
And I should see "Learning plans" in the "Setting changes" "table"
And I should see "Lesson" in the "Setting changes" "table"
And I should see "Logged in user" in the "Setting changes" "table"
And I should see "Login" in the "Setting changes" "table"
And I should see "Main menu" in the "Setting changes" "table"
And I should see "Maximum number of attachments" in the "Setting changes" "table"
And "Maximum number of attachments" row "Current value" column of "Setting changes" table should contain "9"
And "Maximum number of attachments" row "New value" column of "Setting changes" table should contain "3"
And I should see "Mentees" in the "Setting changes" "table"
And I should see "Network servers" in the "Setting changes" "table"
And I should see "Numerical" in the "Setting changes" "table"
And I should see "Online users" in the "Setting changes" "table"
And I should see "Private files" in the "Setting changes" "table"
And I should see "Random short-answer matching" in the "Setting changes" "table"
And I should see "Recent blog entries" in the "Setting changes" "table"
And I should see "Recently accessed courses" in the "Setting changes" "table"
And I should see "Restriction by grouping" in the "Setting changes" "table"
And I should see "Restriction by profile" in the "Setting changes" "table"
And I should see "SCORM package" in the "Setting changes" "table"
And I should see "Search forums" in the "Setting changes" "table"
And I should see "Section links" in the "Setting changes" "table"
And I should see "Server files" in the "Setting changes" "table"
And I should see "Show data retention summary" in the "Setting changes" "table"
And I should see "Social activities" in the "Setting changes" "table"
And I should see "Social" in the "Setting changes" "table"
And I should see "Starred courses" in the "Setting changes" "table"
And I should see "Lesson" in the "Setting changes" "table"
And I should see "Tags" in the "Setting changes" "table"
And I should see "URL downloader" in the "Setting changes" "table"
And I should see "Wiki" in the "Setting changes" "table"
And I should see "Wikimedia" in the "Setting changes" "table"
And I should see "Workshop" in the "Setting changes" "table"
# Check filters and data formats strings are displayed properly.
And I should see "Activity names auto-linking" in the "Setting changes" "table"
And I should see "Javascript Object Notation (.json)" in the "Setting changes" "table"
# Apply the Starter preset.
And I click on "Apply" "button"
And I navigate to "Advanced features" in site administration
And the field "Enable badges" matches value "0"
And the field "Enable competencies" matches value "0"
And I navigate to "Plugins > Activity modules > Manage activities" in site administration
And "Disable Database" "icon" should not exist in the "Database" "table_row"
And I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
And "Hide" "icon" should not exist in the "Restriction by grouping" "table_row"
And I navigate to "Plugins > Blocks > Manage blocks" in site administration
And "Disable Logged in user" "icon" should not exist in the "Logged in user" "table_row"
And I navigate to "Plugins > Course formats > Manage course formats" in site administration
And "Disable" "icon" should not exist in the "Social" "table_row"
And I navigate to "Plugins > Question behaviours > Manage question behaviours" in site administration
And "Enabled" "icon" should not exist in the "Immediate feedback with CBM" "table_row"
And I navigate to "Plugins > Question types > Manage question types" in site administration
And "Enabled" "icon" should not exist in the "Calculated multichoice" "table_row"
Scenario: Re-applying Starter Moodle preset does not display setting changes
# Apply Starter preset.
Given I navigate to "Site admin presets" in site administration
And I open the action menu in "Starter" "table_row"
And I choose "Review settings and apply" in the open action menu
And I click on "Apply" "button"
And I click on "Continue" "button"
# When the Starter preset it's applied again, no changes should be displayed.
When I open the action menu in "Starter" "table_row"
And I choose "Review settings and apply" in the open action menu
Then I should not see "Setting changes"
Scenario: Applied exported settings
Given I navigate to "Site admin presets" in site administration
And I click on "Create preset" "button"
And I set the field "Name" to "Current"
And I click on "Create preset" "button"
And I should see "Current"
And I open the action menu in "Current" "table_row"
When I choose "Review settings and apply" in the open action menu
And I should not see "Setting changes"
And I click on "Continue" "button"
And the following config values are set as admin:
| enabled | 0 | core_competency |
And I open the action menu in "Current" "table_row"
And I choose "Review settings and apply" in the open action menu
Then I should see "Setting changes"
And I should see "Enable competencies" in the "core_competency" "table_row"
And I click on "Apply" "button"
And I navigate to "Advanced features" in site administration
And the field "Enable competencies" matches value "1"
@@ -0,0 +1,130 @@
<?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/>.
/**
* Steps definitions related with admin presets.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Sylvain Revenu | Pimenko
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
require_once(__DIR__ . '/../../../../../lib/behat/behat_field_manager.php');
use Behat\Mink\Exception\ExpectationException as ExpectationException;
/**
* Steps definitions related with admin presets.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Sylvain Revenu | Pimenko
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_admin_presets extends behat_base {
/**
* Downloads the file from a specific link on the page and checks the size is in a given range.
*
* Only works if the link has an href attribute. Javascript downloads are
* not supported. Currently, the href must be an absolute URL.
*
* The range includes the endpoints. That is, a 10 byte file in considered to
* be between "5" and "10" bytes, and between "10" and "20" bytes.
*
* @Then /^following "(?P<link_string>[^"]*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should download between "(?P<min_bytes>\d+)" and "(?P<max_bytes>\d+)" bytes$/
* @param string $link the text of the link.
* @param string $selectortype The type of what we look for
* @param string $nodeelement Element we look in
* @param string $nodeselectortype The type of selector where we look in
* @param int $minexpectedsize the minimum expected file size in bytes.
* @param int $maxexpectedsize the maximum expected file size in bytes.
* @return void
* @throws ExpectationException
*/
final public function following_in_the_should_download_between_and_bytes(string $link, string $selectortype,
string $nodeelement, string $nodeselectortype, int $minexpectedsize, int $maxexpectedsize): void {
// If the minimum is greater than the maximum then swap the values.
if ((int) $minexpectedsize > (int) $maxexpectedsize) {
list($minexpectedsize, $maxexpectedsize) = [$maxexpectedsize, $minexpectedsize];
}
$exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
// It will stop spinning once file is downloaded or time out.
$result = $this->spin(
function($context, $args) {
return $this->download_file_from_link_within_node($args['selectortype'], $args['link'],
$args['nodeselectortype'], $args['nodeelement']);
},
[
'selectortype' => $selectortype,
'link' => $link,
'nodeselectortype' => $nodeselectortype,
'nodeelement' => $nodeelement
],
behat_base::get_extended_timeout(),
$exception
);
// Check download size.
$actualsize = (int) strlen($result);
if ($actualsize < $minexpectedsize || $actualsize > $maxexpectedsize) {
throw new ExpectationException('Downloaded data was ' . $actualsize .
' bytes, expecting between ' . $minexpectedsize . ' and ' .
$maxexpectedsize, $this->getSession());
}
}
/**
* Given the text of a link, download the linked file and return the contents.
*
* This is a helper method used by {@see following_in_the_should_download_between_and_bytes()}
*
* @param string $selectortype The type of what we look for
* @param string $link the text of the link.
* @param string $nodeselectortype The type of selector where we look in
* @param string $nodeelement Element we look in
* @return string the content of the downloaded file.
*/
final public function download_file_from_link_within_node(string $selectortype, string $link,
string $nodeselectortype, string $nodeelement): string {
// Find the link from ur specific node.
$linknode = $this->get_node_in_container($selectortype, $link, $nodeselectortype, $nodeelement);
$this->ensure_node_is_visible($linknode);
// Get the href and check it.
$url = $linknode->getAttribute('href');
if (!$url) {
throw new ExpectationException('Download link does not have href attribute',
$this->getSession());
}
if (!preg_match('~^https?://~', $url)) {
throw new ExpectationException('Download link not an absolute URL: ' . $url,
$this->getSession());
}
// Download the URL and check the size.
$session = $this->getSession()->getCookie('MoodleSession');
return download_file_content($url, ['Cookie' => 'MoodleSession=' . $session]);
}
}
@@ -0,0 +1,51 @@
@tool @tool_admin_presets @javascript
Feature: Admin preset deletion
Background: Create a preset to delete
Given I log in as "admin"
And the following "tool_admin_presets > preset" exist:
| name |
| Custom preset |
And I navigate to "Site admin presets" in site administration
Scenario: Core preset settings can't be deleted
Given I should see "Starter"
And I should see "Full"
And I should see "Custom preset"
When I open the action menu in "Custom preset" "table_row"
And I should see "Delete"
And I press the escape key
And I open the action menu in "Full" "table_row"
Then I should not see "Delete"
And I press the escape key
And I open the action menu in "Starter" "table_row"
And I should not see "Delete"
Scenario: Custom preset settings can be deleted
Given I should see "Custom preset"
And I open the action menu in "Custom preset" "table_row"
When I choose "Delete" in the open action menu
And I should see "Are you sure you want to delete the site admin preset Custom preset?"
And I should not see "This preset has been previously applied"
And I click on "Cancel" "button"
And I should see "Presets allow you to easily switch between different site admin configurations."
And "Custom preset" "table_row" should exist
And I open the action menu in "Custom preset" "table_row"
And I choose "Delete" in the open action menu
And I should not see "This preset has been previously applied"
And I click on "Delete" "button"
And I should see "Presets allow you to easily switch between different site admin configurations."
Then "Custom preset" "table_row" should not exist
Scenario: Delete preset that has been applied
Given I open the action menu in "Custom preset" "table_row"
And I choose "Review settings and apply" in the open action menu
And I click on "Apply" "button"
And I navigate to "Site admin presets" in site administration
When I open the action menu in "Custom preset" "table_row"
And I choose "Delete" in the open action menu
And I should see "Are you sure you want to delete the site admin preset Custom preset?"
Then I should see "This preset has been previously applied"
And I click on "Delete" "button"
And I should see "Presets allow you to easily switch between different site admin configurations"
And "Custom preset" "table_row" should not exist
@@ -0,0 +1,22 @@
@tool @tool_admin_presets
Feature: I can download a preset
Background:
Given the following "tool_admin_presets > preset" exist:
| name |
| Custom preset |
Scenario: Custom preset settings can be downloaded
Given I log in as "admin"
And I navigate to "Site admin presets" in site administration
When I open the action menu in "Custom preset" "table_row"
Then following "Download" in the "Custom preset" "table_row" should download a file that:
| Has mimetype | text/xml |
| Contains text in xml element | Custom preset |
Scenario: Core preset settings can be downloaded
Given I log in as "admin"
And I navigate to "Site admin presets" in site administration
When I open the action menu in "Starter" "table_row"
Then following "Download" in the "Starter" "table_row" should download a file that:
| Has mimetype | text/xml |
| Contains text in xml element | Starter |
@@ -0,0 +1,33 @@
@tool @tool_admin_presets @javascript
Feature: I can add a new preset with current settings
Background:
Given I log in as "admin"
And I navigate to "Site admin presets" in site administration
Scenario: Export settings with an existing name
Given I should see "Starter"
And I click on "Create preset" "button"
And I set the field "Name" to "Starter"
And I set the field "Description" to "Non-core starter preset"
When I click on "Create preset" "button"
Then the following should exist in the "Site admin presets table" table:
| Name | Description |
| Starter | Moodle with all of the most popular features, including Assignment, Feedback, Forum, H5P, Quiz and Completion tracking. |
| Full | All the Starter features plus External (LTI) tool, SCORM, Workshop, Analytics, Badges, Competencies, Learning plans and lots more. |
| Starter | Non-core starter preset |
Scenario: Export current settings
Given I click on "Create preset" "button"
And I set the field "Name" to "Current"
And I click on "Create preset" "button"
And I should see "Current"
And I open the action menu in "Current" "table_row"
When I choose "Review settings and apply" in the open action menu
And I should not see "Setting changes"
And I click on "Continue" "button"
And the following config values are set as admin:
| enableportfolios | 1 |
And I open the action menu in "Current" "table_row"
And I choose "Review settings and apply" in the open action menu
Then I should see "Setting changes"
@@ -0,0 +1,77 @@
@tool @tool_admin_presets @_file_upload @javascript
Feature: I can upload a preset file
Background: Go to the Import settings page
Given I log in as "admin"
And I navigate to "Site admin presets" in site administration
And I click on "Import preset" "link_or_button"
Scenario: Import settings and plugins from a valid XML file
Given I should see "Import site admin preset"
And I click on "Import" "button"
And I should see "You must supply a value here"
And I upload "admin/presets/tests/fixtures/import_settings_plugins.xml" file to "Select file" filemanager
And I click on "Import" "button"
And I should see "Setting changes"
And I should see "Imported preset"
And I should see "Enable portfolios" in the "core" "table_row"
And I should see "900" in the "mod_lesson" "table_row"
When I click on "Apply" "button"
And I should see "Setting changes"
And I should see "Unchanged settings"
And I click on "Continue" "button"
Then I should see "Imported preset" in the "Site admin presets table" "table"
And I navigate to "Advanced features" in site administration
And the following fields match these values:
| Enable portfolios | 1 |
And I navigate to "Plugins > Activity modules > Lesson" in site administration
And the following fields match these values:
| Popup window width | 900 |
Scenario: Rename imported settings
Given I should see "Import site admin preset"
And I set the field "Name" to "Renamed preset"
And I upload "admin/presets/tests/fixtures/import_settings_plugins.xml" file to "Select file" filemanager
And I click on "Import" "button"
And I should not see "Imported preset"
And I should see "Renamed preset"
When I click on "Apply" "button"
And I click on "Continue" "button"
Then I should not see "Imported preset" in the "Site admin presets table" "table"
And I should see "Renamed preset" in the "Site admin presets table" "table"
Scenario: Import settings from an invalid XML file
Given I set the field "Name" to "Renamed preset"
And I upload "admin/presets/tests/fixtures/invalid_xml_file.xml" file to "Select file" filemanager
When I click on "Import" "button"
Then I should see "Wrong file"
And I should not see "Setting changes"
And I navigate to "Site admin presets" in site administration
And I should not see "Renamed preset"
Scenario: Import unexisting settings category
Given I set the field "Name" to "Renamed preset"
And I upload "admin/presets/tests/fixtures/unexisting_category.xml" file to "Select file" filemanager
When I click on "Import" "button"
Then I should see "No valid settings"
And I should not see "Setting changes"
And I navigate to "Site admin presets" in site administration
And I should not see "Renamed preset"
Scenario: Import one unexisting setting
Given the following config values are set as admin:
| debug | 0 |
| debugdisplay | 0 |
And I set the field "Name" to "Renamed preset"
And I upload "admin/presets/tests/fixtures/import_settings_with_unexisting_setting.xml" file to "Select file" filemanager
When I click on "Import" "button"
And I should see "Setting changes"
And I should see "Enable portfolios" in the "core" "table_row"
And I should not see "No valid settings"
And I click on "Apply" "button"
And I should see "Setting changes"
And I click on "Continue" "button"
Then I should see "Renamed preset" in the "Site admin presets table" "table"
And I navigate to "Advanced features" in site administration
And the following fields match these values:
| Enable portfolios | 1 |
@@ -0,0 +1,52 @@
@tool @tool_admin_presets @javascript
Feature: I can revert changes after a load
Background: Apply Starter Moodle to revert it
Given I log in as "admin"
And I navigate to "Site admin presets" in site administration
And I open the action menu in "Starter" "table_row"
And I choose "Review settings and apply" in the open action menu
And I should see "Setting changes"
And I click on "Apply" "button"
And I click on "Continue" "button"
Scenario: Presets that haven't been applied can't be reverted
Given I open the action menu in "Full" "table_row"
Then I should not see "Show version history"
Scenario: Presets that have been applied can be reverted
# Checking applied settings before reverting them.
Given I navigate to "Advanced features" in site administration
And the field "Enable badges" matches value "0"
And the field "Enable competencies" matches value "0"
And I navigate to "Plugins > Activity modules > Manage activities" in site administration
And "Disable Database" "icon" should not exist in the "Database" "table_row"
And I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
And "Hide" "icon" should not exist in the "Restriction by grouping" "table_row"
And I navigate to "Plugins > Blocks > Manage blocks" in site administration
And "Disable Logged in user" "icon" should not exist in the "Logged in user" "table_row"
And I navigate to "Plugins > Course formats > Manage course formats" in site administration
And "Disable" "icon" should not exist in the "Social" "table_row"
And I navigate to "Plugins > Question behaviours > Manage question behaviours" in site administration
And "Enabled" "icon" should not exist in the "Immediate feedback with CBM" "table_row"
And I navigate to "Plugins > Question types > Manage question types" in site administration
And "Enabled" "icon" should not exist in the "Calculated multichoice" "table_row"
And I navigate to "Site admin presets" in site administration
And I open the action menu in "Starter" "table_row"
And I choose "Show version history" in the open action menu
When I click on "Restore this version" "link"
And I navigate to "Advanced features" in site administration
Then the field "Enable badges" matches value "1"
And the field "Enable competencies" matches value "1"
And I navigate to "Plugins > Activity modules > Manage activities" in site administration
And "Disable Database" "icon" should exist in the "Database" "table_row"
And I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
And "Hide" "icon" should exist in the "Restriction by grouping" "table_row"
And I navigate to "Plugins > Blocks > Manage blocks" in site administration
And "Disable Logged in user" "icon" should exist in the "Logged in user" "table_row"
And I navigate to "Plugins > Course formats > Manage course formats" in site administration
And "Disable" "icon" should exist in the "Social" "table_row"
And I navigate to "Plugins > Question behaviours > Manage question behaviours" in site administration
And "Enabled" "icon" should exist in the "Immediate feedback with CBM" "table_row"
And I navigate to "Plugins > Question types > Manage question types" in site administration
And "Enabled" "icon" should exist in the "Calculated multichoice" "table_row"
@@ -0,0 +1,59 @@
<?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/>.
namespace tool_admin_presets\event;
/**
* Tests for the preset_deleted event class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \tool_admin_presets\event\preset_deleted
*/
class preset_deleted_test extends \advanced_testcase {
/**
* Test preset_deleted event.
*/
public function test_preset_deleted_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$params = [
'context' => \context_system::instance(),
'objectid' => $presetid,
];
$event = preset_deleted::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\tool_admin_presets\event\preset_deleted', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
$this->assertEquals($presetid, $event->objectid);
}
}
@@ -0,0 +1,59 @@
<?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/>.
namespace tool_admin_presets\event;
/**
* Tests for the preset_downloaded event class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \tool_admin_presets\event\preset_downloaded
*/
class preset_downloaded_test extends \advanced_testcase {
/**
* Test preset_downloaded event.
*/
public function test_preset_downloaded_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$params = [
'context' => \context_system::instance(),
'objectid' => $presetid,
];
$event = preset_downloaded::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\tool_admin_presets\event\preset_downloaded', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
$this->assertEquals($presetid, $event->objectid);
}
}
@@ -0,0 +1,59 @@
<?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/>.
namespace tool_admin_presets\event;
/**
* Tests for the preset_exported event class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \tool_admin_presets\event\preset_exported
*/
class preset_exported_test extends \advanced_testcase {
/**
* Test preset_exported event.
*/
public function test_preset_exported_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$params = [
'context' => \context_system::instance(),
'objectid' => $presetid,
];
$event = preset_exported::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\tool_admin_presets\event\preset_exported', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
$this->assertEquals($presetid, $event->objectid);
}
}
@@ -0,0 +1,59 @@
<?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/>.
namespace tool_admin_presets\event;
/**
* Tests for the preset_imported event class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \tool_admin_presets\event\preset_imported
*/
class preset_imported_test extends \advanced_testcase {
/**
* Test preset_imported event.
*/
public function test_preset_imported_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$params = [
'context' => \context_system::instance(),
'objectid' => $presetid,
];
$event = preset_imported::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\tool_admin_presets\event\preset_imported', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
$this->assertEquals($presetid, $event->objectid);
}
}
@@ -0,0 +1,59 @@
<?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/>.
namespace tool_admin_presets\event;
/**
* Tests for the preset_loaded event class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \tool_admin_presets\event\preset_loaded
*/
class preset_loaded_test extends \advanced_testcase {
/**
* Test preset_loaded event.
*/
public function test_preset_loaded_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$params = [
'context' => \context_system::instance(),
'objectid' => $presetid,
];
$event = preset_loaded::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\tool_admin_presets\event\preset_loaded', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
$this->assertEquals($presetid, $event->objectid);
}
}
@@ -0,0 +1,59 @@
<?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/>.
namespace tool_admin_presets\event;
/**
* Tests for the preset_previewed event class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \tool_admin_presets\event\preset_previewed
*/
class preset_previewed_test extends \advanced_testcase {
/**
* Test preset_previewed event.
*/
public function test_preset_previewed_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$params = [
'context' => \context_system::instance(),
'objectid' => $presetid,
];
$event = preset_previewed::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\tool_admin_presets\event\preset_previewed', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
$this->assertEquals($presetid, $event->objectid);
}
}
@@ -0,0 +1,59 @@
<?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/>.
namespace tool_admin_presets\event;
/**
* Tests for the preset_reverted event class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \tool_admin_presets\event\preset_reverted
*/
class preset_reverted_test extends \advanced_testcase {
/**
* Test preset_reverted event.
*/
public function test_preset_reverted_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$params = [
'context' => \context_system::instance(),
'objectid' => $presetid,
];
$event = preset_reverted::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\tool_admin_presets\event\preset_reverted', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
$this->assertEquals($presetid, $event->objectid);
}
}
@@ -0,0 +1,59 @@
<?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/>.
namespace tool_admin_presets\event;
/**
* Tests for the presets_listed event class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \tool_admin_presets\event\presets_listed
*/
class presets_listed_test extends \advanced_testcase {
/**
* Test presets_listed event.
*/
public function test_presets_listed_event(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$params = [
'context' => \context_system::instance(),
'objectid' => $presetid,
];
$event = presets_listed::create($params);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\tool_admin_presets\event\presets_listed', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
$this->assertEquals($presetid, $event->objectid);
}
}
@@ -0,0 +1,41 @@
<?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/>.
/**
* Admin presets entity generators.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Amaia Anabitarte (amaia@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_tool_admin_presets_generator extends behat_generator_base {
/**
* Get a list of the entities that can be created.
* @return array entity name => information about how to generate.
*/
protected function get_creatable_entities(): array {
return [
'presets' => [
'singular' => 'preset',
'datagenerator' => 'preset',
'required' => [],
],
];
}
}
@@ -0,0 +1,32 @@
<?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/>.
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/admin/presets/tests/generator/lib.php');
/**
* Data generator the admin_presets tool.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_admin_presets_generator extends \core_adminpresets_generator {
}
@@ -0,0 +1,146 @@
<?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/>.
namespace tool_admin_presets\local\action;
/**
* Tests for the base class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \tool_admin_presets\local\action\base
*/
class base_test extends \advanced_testcase {
/**
* Test the behaviour of log() method.
*
* @covers ::log
* @dataProvider log_provider
*
* @param string $action Action to log.
* @param string $mode Mode to log.
* @param string|null $expectedclassname The expected classname or null if no event is expected.
*/
public function test_base_log(string $action, string $mode, ?string $expectedclassname): void {
$this->resetAfterTest();
$this->setAdminUser();
// Initialise the parameters and create the class.
if (!empty($mode)) {
$_POST['mode'] = $mode;
}
if (!empty($action)) {
$_POST['action'] = $action;
}
$base = new base();
// Redirect events (to capture them) and call to the log method.
$sink = $this->redirectEvents();
$base->log();
$events = $sink->get_events();
$sink->close();
$event = reset($events);
// Validate event data.
if (is_null($expectedclassname)) {
$this->assertFalse($event);
} else {
$this->assertInstanceOf('\\tool_admin_presets\\event\\' . $expectedclassname, $event);
}
}
/**
* Data provider for test_base_log().
*
* @return array
*/
public function log_provider(): array {
return [
// Action = base.
'action=base and mode = show' => [
'action' => 'base',
'mode' => 'show',
'expectedclassname' => 'presets_listed',
],
'action=base and mode = execute' => [
'action' => 'base',
'mode' => 'execute',
'expectedclassname' => 'presets_listed',
],
// Action = delete.
'action=delete and mode = show' => [
'action' => 'delete',
'mode' => 'show',
'expectedclassname' => null,
],
'action=delete and mode = execute' => [
'action' => 'delete',
'mode' => 'execute',
'expectedclassname' => 'preset_deleted',
],
'mode = delete and action = base' => [
'action' => 'base',
'mode' => 'delete',
'expectedclassname' => 'preset_deleted',
],
// Action = export.
'action=export and mode = show' => [
'action' => 'export',
'mode' => 'show',
'expectedclassname' => null,
],
'action=export and mode = execute' => [
'action' => 'export',
'mode' => 'execute',
'expectedclassname' => 'preset_exported',
],
'mode = export and action = download_xml' => [
'action' => 'export',
'mode' => 'download_xml',
'expectedclassname' => 'preset_downloaded',
],
// Action = load.
'action=load and mode = show' => [
'action' => 'load',
'mode' => 'show',
'expectedclassname' => 'preset_previewed',
],
'action=load and mode = execute' => [
'action' => 'load',
'mode' => 'execute',
'expectedclassname' => 'preset_loaded',
],
// Unexisting action/method.
'Unexisting action' => [
'action' => 'unexisting',
'mode' => 'show',
'expectedclassname' => null,
],
'Unexisting mode' => [
'action' => 'delete',
'mode' => 'unexisting',
'expectedclassname' => null,
],
];
}
}
@@ -0,0 +1,150 @@
<?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/>.
namespace tool_admin_presets\local\action;
/**
* Tests for the delete class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \tool_admin_presets\local\action\delete
*/
class delete_test extends \advanced_testcase {
/**
* Test the behaviour of execute() method.
*
* @covers ::execute
*/
public function test_delete_execute(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create some presets.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid1 = $generator->create_preset(['name' => 'Preset 1', 'applypreset' => true]);
$presetid2 = $generator->create_preset(['name' => 'Preset 2']);
$currentpresets = $DB->count_records('adminpresets');
$currentitems = $DB->count_records('adminpresets_it');
$currentadvitems = $DB->count_records('adminpresets_it_a');
$currentplugins = $DB->count_records('adminpresets_plug');
// Only preset1 has been applied.
$this->assertCount(1, $DB->get_records('adminpresets_app'));
// Only the preset1 settings that have changed: enablebadges, mediawidth and maxanswers.
$this->assertCount(3, $DB->get_records('adminpresets_app_it'));
// Only the preset1 advanced settings that have changed: maxanswers_adv.
$this->assertCount(1, $DB->get_records('adminpresets_app_it_a'));
// Only the preset1 plugins that have changed: enrol_guest and mod_glossary.
$this->assertCount(2, $DB->get_records('adminpresets_app_plug'));
// Initialise the parameters and create the delete class.
$_POST['action'] = 'delete';
$_POST['mode'] = 'execute';
$_POST['id'] = $presetid1;
$_POST['sesskey'] = sesskey();
$action = new delete();
$sink = $this->redirectEvents();
try {
$action->execute();
} catch (\exception $e) {
// If delete action was successfull, redirect should be called so we will encounter an
// 'unsupported redirect error' moodle_exception.
$this->assertInstanceOf(\moodle_exception::class, $e);
} finally {
// Check the preset data has been removed.
$presets = $DB->get_records('adminpresets');
$this->assertCount($currentpresets - 1, $presets);
$preset = reset($presets);
$this->assertArrayHasKey($presetid2, $presets);
// Check preset items.
$this->assertCount($currentitems - 4, $DB->get_records('adminpresets_it'));
$this->assertCount(0, $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid1]));
// Check preset advanced items.
$this->assertCount($currentadvitems - 1, $DB->get_records('adminpresets_it_a'));
// Check preset plugins.
$this->assertCount($currentplugins - 3, $DB->get_records('adminpresets_plug'));
$this->assertCount(0, $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid1]));
// Check preset applied tables are empty now.
$this->assertCount(0, $DB->get_records('adminpresets_app'));
$this->assertCount(0, $DB->get_records('adminpresets_app_it'));
$this->assertCount(0, $DB->get_records('adminpresets_app_it_a'));
$this->assertCount(0, $DB->get_records('adminpresets_app_plug'));
// Check the delete event has been raised.
$events = $sink->get_events();
$sink->close();
$event = reset($events);
$this->assertInstanceOf('\\tool_admin_presets\\event\\preset_deleted', $event);
}
}
/**
* Test the behaviour of execute() method when the preset id doesn't exist.
*
* @covers ::execute
*/
public function test_delete_execute_unexisting_preset(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create some presets.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset(['name' => 'Preset 1']);
// Initialise the parameters and create the delete class.
$_POST['action'] = 'delete';
$_POST['mode'] = 'execute';
$_POST['id'] = $presetid * 2; // Unexisting preset identifier.
$_POST['sesskey'] = sesskey();
$action = new delete();
$this->expectException(\moodle_exception::class);
$action->execute();
}
/**
* Test the behaviour of show() method when the preset id doesn't exist.
*
* @covers ::show
*/
public function test_delete_show_unexisting_preset(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create some presets.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset(['name' => 'Preset 1']);
// Initialise the parameters and create the delete class.
$_POST['action'] = 'delete';
$_POST['mode'] = 'show';
$_POST['id'] = $presetid * 2; // Unexisting preset identifier.
$action = new delete();
$this->expectException(\moodle_exception::class);
$action->show();
}
}
@@ -0,0 +1,177 @@
<?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/>.
namespace tool_admin_presets\local\action;
use core_adminpresets\manager;
/**
* Tests for the export class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \tool_admin_presets\local\action\export
*/
class export_test extends \advanced_testcase {
/**
* Test the behaviour of execute() method.
* @covers ::execute
* @dataProvider export_execute_provider
*
* @param bool $includesensible Whether the sensible settings should be exported too or not.
* @param string $presetname Preset name.
*/
public function test_export_execute(bool $includesensible = false, string $presetname = 'Export 1'): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Get current presets and items.
$currentpresets = $DB->count_records('adminpresets');
$currentadvitems = $DB->count_records('adminpresets_it_a');
// Initialise some settings (to compare their values have been exported as expected).
set_config('recaptchapublickey', 'abcde');
set_config('enablebadges', '0');
set_config('mediawidth', '900', 'mod_lesson');
set_config('maxanswers', '2', 'mod_lesson');
set_config('maxanswers_adv', '0', 'mod_lesson');
set_config('defaultfeedback', '0', 'mod_lesson');
set_config('defaultfeedback_adv', '1', 'mod_lesson');
// Get the data we are submitting for the form and mock submitting it.
$formdata = [
'name' => $presetname,
'comments' => ['text' => 'This is a presets for testing export'],
'author' => 'Super-Girl',
'includesensiblesettings' => $includesensible,
'admin_presets_submit' => 'Save changes',
];
\tool_admin_presets\form\export_form::mock_submit($formdata);
// Initialise the parameters and create the export class.
$_POST['action'] = 'export';
$_POST['mode'] = 'execute';
$_POST['sesskey'] = sesskey();
$action = new export();
$sink = $this->redirectEvents();
try {
$action->execute();
} catch (\exception $e) {
// If export action was successfull, redirect should be called so we will encounter an
// 'unsupported redirect error' moodle_exception.
$this->assertInstanceOf(\moodle_exception::class, $e);
} finally {
// Check the preset record has been created.
$presets = $DB->get_records('adminpresets');
$this->assertCount($currentpresets + 1, $presets);
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->access_protected($action, 'id');
$this->assertArrayHasKey($presetid, $presets);
$preset = $presets[$presetid];
$this->assertEquals($presetname, $preset->name);
$this->assertEquals(manager::NONCORE_PRESET, $preset->iscore);
// Check the items, advanced attributes and plugins have been created.
$this->assertGreaterThan(0, $DB->count_records('adminpresets_it', ['adminpresetid' => $presetid]));
$this->assertGreaterThan($currentadvitems, $DB->count_records('adminpresets_it_a'));
$this->assertGreaterThan(0, $DB->count_records('adminpresets_plug', ['adminpresetid' => $presetid]));
// Check settings have been created with the expected values.
$params = ['adminpresetid' => $presetid, 'plugin' => 'none', 'name' => 'enablebadges'];
$setting = $DB->get_record('adminpresets_it', $params);
$this->assertEquals('0', $setting->value);
$params = ['adminpresetid' => $presetid, 'plugin' => 'mod_lesson', 'name' => 'mediawidth'];
$setting = $DB->get_record('adminpresets_it', $params);
$this->assertEquals('900', $setting->value);
$params = ['adminpresetid' => $presetid, 'plugin' => 'mod_lesson', 'name' => 'maxanswers'];
$setting = $DB->get_record('adminpresets_it', $params);
$this->assertEquals('2', $setting->value);
$params = ['itemid' => $setting->id, 'name' => 'maxanswers_adv'];
$setting = $DB->get_record('adminpresets_it_a', $params);
$this->assertEquals('0', $setting->value);
$params = ['adminpresetid' => $presetid, 'plugin' => 'mod_lesson', 'name' => 'defaultfeedback'];
$setting = $DB->get_record('adminpresets_it', $params);
$this->assertEquals('0', $setting->value);
$params = ['itemid' => $setting->id, 'name' => 'defaultfeedback_adv'];
$setting = $DB->get_record('adminpresets_it_a', $params);
$this->assertEquals('1', $setting->value);
// Check plugins have been created with the expected values.
$manager = \core_plugin_manager::instance();
$plugintype = 'enrol';
$plugins = $manager->get_present_plugins($plugintype);
$enabledplugins = $manager->get_enabled_plugins($plugintype);
foreach ($plugins as $pluginname => $unused) {
$params = ['adminpresetid' => $presetid, 'plugin' => $plugintype, 'name' => $pluginname];
$plugin = $DB->get_record('adminpresets_plug', $params);
$enabled = (!empty($enabledplugins) && array_key_exists($pluginname, $enabledplugins));
$this->assertEquals($enabled, (bool) $plugin->enabled);
}
// Check whether sensible settings have been exported or not.
$params = ['adminpresetid' => $presetid, 'plugin' => 'none', 'name' => 'recaptchapublickey'];
$recaptchasetting = $DB->get_record('adminpresets_it', $params);
$params = ['adminpresetid' => $presetid, 'plugin' => 'none', 'name' => 'cronremotepassword'];
$cronsetting = $DB->get_record('adminpresets_it', $params);
if ($includesensible) {
$this->assertEquals('abcde', $recaptchasetting->value);
$this->assertNotFalse($cronsetting);
} else {
$this->assertFalse($recaptchasetting);
$this->assertFalse($cronsetting);
}
// Check the export event has been raised.
$events = $sink->get_events();
$sink->close();
$event = reset($events);
$this->assertInstanceOf('\\tool_admin_presets\\event\\preset_exported', $event);
}
}
/**
* Data provider for test_export_execute().
*
* @return array
*/
public function export_execute_provider(): array {
return [
'Export settings and plugins, excluding sensible' => [
'includesensible' => false,
],
'Export settings and plugins, including sensible' => [
'includesensible' => true,
],
'Export settings and plugins, with Starter name (it should not be marked as core)' => [
'includesensible' => false,
'presetname' => 'Starter',
],
'Export settings and plugins, with Full name (it should not be marked as core)' => [
'includesensible' => false,
'presetname' => 'Full',
],
];
}
}
@@ -0,0 +1,252 @@
<?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/>.
namespace tool_admin_presets\local\action;
use core_adminpresets\manager;
/**
* Tests for the import class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \tool_admin_presets\local\action\import
*/
class import_test extends \advanced_testcase {
/**
* Test the behaviour of execute() method.
*
* @dataProvider import_execute_provider
* @covers ::execute
*
* @param string $filecontents File content to import.
* @param bool $expectedpreset Whether the preset should be created or not.
* @param bool $expectedsettings Whether settings will be created or not.
* @param bool $expectedplugins Whether plugins will be created or not.
* @param bool $expecteddebugging Whether debugging message will be thrown or not.
* @param string|null $expectedexception Expected exception class (if that's the case).
* @param string|null $expectedpresetname Expected preset name.
*/
public function test_import_execute(string $filecontents, bool $expectedpreset, bool $expectedsettings = false,
bool $expectedplugins = false, bool $expecteddebugging = false, string $expectedexception = null,
string $expectedpresetname = 'Imported preset'): void {
global $DB, $USER;
$this->resetAfterTest();
$this->setAdminUser();
$currentpresets = $DB->count_records('adminpresets');
$currentitems = $DB->count_records('adminpresets_it');
$currentadvitems = $DB->count_records('adminpresets_it_a');
// Create draft file to import.
$draftid = file_get_unused_draft_itemid();
$filerecord = [
'component' => 'user',
'filearea' => 'draft',
'contextid' => \context_user::instance($USER->id)->id, 'itemid' => $draftid,
'filename' => 'export.xml', 'filepath' => '/'
];
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, $filecontents);
// Get the data we are submitting for the form and mock submitting it.
$formdata = [
'xmlfile' => $draftid,
'name' => '',
'admin_presets_submit' => 'Save changes',
'sesskey' => sesskey(),
];
\tool_admin_presets\form\import_form::mock_submit($formdata);
// Initialise the parameters and create the import class.
$_POST['action'] = 'import';
$_POST['mode'] = 'execute';
$action = new import();
$sink = $this->redirectEvents();
try {
$action->execute();
} catch (\exception $e) {
// If import action was successfull, redirect should be called so we will encounter an
// 'unsupported redirect error' moodle_exception.
if ($expectedexception) {
$this->assertInstanceOf($expectedexception, $e);
} else {
$this->assertInstanceOf(\moodle_exception::class, $e);
}
} finally {
if ($expecteddebugging) {
$this->assertDebuggingCalled();
}
if ($expectedpreset) {
// Check the preset record has been created.
$presets = $DB->get_records('adminpresets');
$this->assertCount($currentpresets + 1, $presets);
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->access_protected($action, 'id');
$this->assertArrayHasKey($presetid, $presets);
$preset = $presets[$presetid];
$this->assertEquals($expectedpresetname, $preset->name);
$this->assertEquals('http://demo.moodle', $preset->site);
$this->assertEquals('Ada Lovelace', $preset->author);
$this->assertEquals(manager::NONCORE_PRESET, $preset->iscore);
if ($expectedsettings) {
// Check the items have been created.
$items = $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid]);
$this->assertCount(4, $items);
$presetitems = [
'none' => [
'enablebadges' => 0,
'enableportfolios' => 1,
'allowemojipicker' => 1,
],
'mod_lesson' => [
'mediawidth' => 900,
'maxanswers' => 2,
],
];
foreach ($items as $item) {
$this->assertArrayHasKey($item->name, $presetitems[$item->plugin]);
$this->assertEquals($presetitems[$item->plugin][$item->name], $item->value);
}
// Check the advanced attributes have been created.
$advitems = $DB->get_records('adminpresets_it_a');
$this->assertCount($currentadvitems + 1, $advitems);
$advitemfound = false;
foreach ($advitems as $advitem) {
if ($advitem->name == 'maxanswers_adv') {
$this->assertEmpty($advitem->value);
$advitemfound = true;
}
}
$this->assertTrue($advitemfound);
}
if ($expectedplugins) {
// Check the plugins have been created.
$plugins = $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid]);
$this->assertCount(6, $plugins);
$presetplugins = [
'atto' => [
'html' => 1,
],
'block' => [
'html' => 0,
'activity_modules' => 1,
],
'mod' => [
'chat' => 0,
'data' => 0,
'lesson' => 1,
],
];
foreach ($plugins as $plugin) {
$this->assertArrayHasKey($plugin->name, $presetplugins[$plugin->plugin]);
$this->assertEquals($presetplugins[$plugin->plugin][$plugin->name], $plugin->enabled);
}
}
} else {
// Check the preset nor the items are not created.
$this->assertCount($currentpresets, $DB->get_records('adminpresets'));
$this->assertCount($currentitems, $DB->get_records('adminpresets_it'));
$this->assertCount($currentadvitems, $DB->get_records('adminpresets_it_a'));
}
// Check the export event has been raised.
$events = $sink->get_events();
$sink->close();
$event = reset($events);
if ($expectedpreset) {
// If preset has been created, an event should be raised.
$this->assertInstanceOf('\\tool_admin_presets\\event\\preset_imported', $event);
} else {
$this->assertFalse($event);
}
}
}
/**
* Data provider for test_import_execute().
*
* @return array
*/
public function import_execute_provider(): array {
$fixturesfolder = __DIR__ . '/../../../../../presets/tests/fixtures/';
return [
'Import settings from an empty file' => [
'filecontents' => '',
'expectedpreset' => false,
],
'Import settings and plugins from a valid XML file' => [
'filecontents' => file_get_contents($fixturesfolder . 'import_settings_plugins.xml'),
'expectedpreset' => true,
'expectedsettings' => true,
'expectedplugins' => true,
],
'Import only settings from a valid XML file' => [
'filecontents' => file_get_contents($fixturesfolder . 'import_settings.xml'),
'expectedpreset' => true,
'expectedsettings' => true,
'expectedplugins' => false,
],
'Import settings and plugins from a valid XML file with Starter name, which will be marked as non-core' => [
'filecontents' => file_get_contents($fixturesfolder . 'import_starter_name.xml'),
'expectedpreset' => true,
'expectedsettings' => true,
'expectedplugins' => true,
'expecteddebugging' => false,
'expectedexception' => null,
'expectedpresetname' => 'Starter',
],
'Import settings from an invalid XML file' => [
'filecontents' => file_get_contents($fixturesfolder . 'invalid_xml_file.xml'),
'expectedpreset' => false,
'expectedsettings' => false,
'expectedplugins' => false,
'expecteddebugging' => false,
'expectedexception' => \Exception::class,
],
'Import unexisting settings category' => [
'filecontents' => file_get_contents($fixturesfolder . 'unexisting_category.xml'),
'expectedpreset' => false,
'expectedsettings' => false,
'expectedplugins' => false,
],
'Import unexisting setting' => [
'filecontents' => file_get_contents($fixturesfolder . 'unexisting_setting.xml'),
'expectedpreset' => false,
'expectedsettings' => false,
'expectedplugins' => false,
'expecteddebugging' => true,
],
'Import valid settings with one unexisting setting too' => [
'filecontents' => file_get_contents($fixturesfolder . 'import_settings_with_unexisting_setting.xml'),
'expectedpreset' => true,
'expectedsettings' => false,
'expectedplugins' => false,
'expecteddebugging' => true,
],
];
}
}
@@ -0,0 +1,166 @@
<?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/>.
namespace tool_admin_presets\local\action;
/**
* Tests for the load class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \tool_admin_presets\local\action\load
*/
class load_test extends \advanced_testcase {
/**
* Test the behaviour of show() method when the preset id doesn't exist.
*
* @covers ::show
*/
public function test_load_show_unexisting_preset(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create some presets.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
// Initialise the parameters and create the load class.
$_POST['action'] = 'load';
$_POST['mode'] = 'view';
$_POST['id'] = $presetid * 2; // Unexisting preset identifier.
$action = new load();
$this->expectException(\moodle_exception::class);
$action->show();
}
/**
* Test the behaviour of preview() method when the preset id doesn't exist.
*
* @covers ::preview
*/
public function test_load_preview_unexisting_preset(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create some presets.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
// Initialise the parameters and create the load class.
$_POST['action'] = 'load';
$_POST['mode'] = 'preview';
$_POST['id'] = $presetid * 2; // Unexisting preset identifier.
$action = new load();
$action->preview();
$outputs = $generator->access_protected($action, 'outputs');
// In that case, no exception should be raised and the text of no preset found should be displayed.
$this->assertEquals(get_string('errornopreset', 'core_adminpresets'), $outputs);
}
/**
* Test the behaviour of execute() method.
*
* @covers ::execute
*/
public function test_load_execute(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
$currentpresets = $DB->count_records('adminpresets');
$currentitems = $DB->count_records('adminpresets_it');
$currentadvitems = $DB->count_records('adminpresets_it_a');
$currentplugins = $DB->count_records('adminpresets_plug');
$currentapppresets = $DB->count_records('adminpresets_app');
$currentappitems = $DB->count_records('adminpresets_app_it');
$currentappadvitems = $DB->count_records('adminpresets_app_it_a');
$currentappplugins = $DB->count_records('adminpresets_app_plug');
// Set the config values (to confirm they change after applying the preset).
set_config('enablebadges', 1);
set_config('allowemojipicker', 1);
set_config('mediawidth', '640', 'mod_lesson');
set_config('maxanswers', '5', 'mod_lesson');
set_config('maxanswers_adv', '1', 'mod_lesson');
set_config('enablecompletion', 1);
set_config('usecomments', 0);
// Get the data we are submitting for the form and mock submitting it.
$formdata = [
'id' => $presetid,
'admin_presets_submit' => 'Load selected settings',
];
\tool_admin_presets\form\load_form::mock_submit($formdata);
// Initialise the parameters.
$_POST['action'] = 'load';
$_POST['mode'] = 'execute';
$_POST['id'] = $presetid;
$_POST['sesskey'] = sesskey();
// Create the load class and execute it.
$action = new load();
$action->execute();
// Check the preset applied has been added to database.
$this->assertCount($currentapppresets + 1, $DB->get_records('adminpresets_app'));
// Applied items: enablebadges@none, mediawitdh@mod_lesson and maxanswers@@mod_lesson.
$this->assertCount($currentappitems + 3, $DB->get_records('adminpresets_app_it'));
// Applied advanced items: maxanswers_adv@mod_lesson.
$this->assertCount($currentappadvitems + 1, $DB->get_records('adminpresets_app_it_a'));
// Applied plugins: enrol_guest and mod_glossary.
$this->assertCount($currentappplugins + 2, $DB->get_records('adminpresets_app_plug'));
// Check no new preset has been created.
$this->assertCount($currentpresets, $DB->get_records('adminpresets'));
$this->assertCount($currentitems, $DB->get_records('adminpresets_it'));
$this->assertCount($currentadvitems, $DB->get_records('adminpresets_it_a'));
$this->assertCount($currentplugins, $DB->get_records('adminpresets_plug'));
// Check the setting values have changed accordingly with the ones defined in the preset.
$this->assertEquals(0, get_config('core', 'enablebadges'));
$this->assertEquals(900, get_config('mod_lesson', 'mediawidth'));
$this->assertEquals(2, get_config('mod_lesson', 'maxanswers'));
$this->assertEquals(0, get_config('mod_lesson', 'maxanswers_adv'));
// These settings will never change.
$this->assertEquals(1, get_config('core', 'allowemojipicker'));
$this->assertEquals(1, get_config('core', 'enablecompletion'));
$this->assertEquals(0, get_config('core', 'usecomments'));
// Check the plugins visibility have changed accordingly with the ones defined in the preset.
$enabledplugins = \core\plugininfo\enrol::get_enabled_plugins();
$this->assertArrayNotHasKey('guest', $enabledplugins);
$this->assertArrayHasKey('manual', $enabledplugins);
$enabledplugins = \core\plugininfo\mod::get_enabled_plugins();
$this->assertArrayNotHasKey('glossary', $enabledplugins);
$this->assertArrayHasKey('assign', $enabledplugins);
$enabledplugins = \core\plugininfo\qtype::get_enabled_plugins();
$this->assertArrayHasKey('truefalse', $enabledplugins);
}
}
@@ -0,0 +1,148 @@
<?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/>.
namespace tool_admin_presets\local\action;
/**
* Tests for the rollback class.
*
* @package tool_admin_presets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \tool_admin_presets\local\action\rollback
*/
class rollback_test extends \advanced_testcase {
/**
* Test the behaviour of execute() method.
*
* @covers ::execute
*/
public function test_rollback_execute(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Set the config values (to confirm they change after applying the preset).
set_config('enablebadges', 1);
set_config('allowemojipicker', 1);
set_config('mediawidth', '640', 'mod_lesson');
set_config('maxanswers', '5', 'mod_lesson');
set_config('maxanswers_adv', '1', 'mod_lesson');
set_config('enablecompletion', 1);
set_config('usecomments', 0);
// Create a preset and apply it.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset(['applypreset' => true]);
$presetappid = $DB->get_field('adminpresets_app', 'id', ['adminpresetid' => $presetid]);
$currentpresets = $DB->count_records('adminpresets');
$currentitems = $DB->count_records('adminpresets_it');
$currentadvitems = $DB->count_records('adminpresets_it_a');
$currentplugins = $DB->count_records('adminpresets_plug');
$this->assertCount(1, $DB->get_records('adminpresets_app'));
$this->assertCount(3, $DB->get_records('adminpresets_app_it'));
$this->assertCount(1, $DB->get_records('adminpresets_app_it_a'));
$this->assertCount(2, $DB->get_records('adminpresets_app_plug'));
// Check the setttings have changed accordingly after applying the preset.
$this->assertEquals(0, get_config('core', 'enablebadges'));
$this->assertEquals(900, get_config('mod_lesson', 'mediawidth'));
$this->assertEquals(2, get_config('mod_lesson', 'maxanswers'));
$this->assertEquals(1, get_config('core', 'allowemojipicker'));
$this->assertEquals(1, get_config('core', 'enablecompletion'));
$this->assertEquals(0, get_config('core', 'usecomments'));
// Check the plugins visibility have changed accordingly with the ones defined in the preset.
$enabledplugins = \core\plugininfo\enrol::get_enabled_plugins();
$this->assertArrayNotHasKey('guest', $enabledplugins);
$enabledplugins = \core\plugininfo\mod::get_enabled_plugins();
$this->assertArrayNotHasKey('glossary', $enabledplugins);
$enabledplugins = \core\plugininfo\qtype::get_enabled_plugins();
$this->assertArrayHasKey('truefalse', $enabledplugins);
// Initialise the parameters.
$_POST['action'] = 'rollback';
$_POST['mode'] = 'execute';
$_POST['id'] = $presetappid;
$_POST['sesskey'] = sesskey();
// Create the rollback class and execute it.
$action = new rollback();
$action->execute();
// Check the preset applied has been reverted (so the records in _appXX tables have been removed).
$this->assertCount(0, $DB->get_records('adminpresets_app'));
$this->assertCount(0, $DB->get_records('adminpresets_app_it'));
$this->assertCount(0, $DB->get_records('adminpresets_app_it_a'));
$this->assertCount(0, $DB->get_records('adminpresets_app_plug'));
// Check the preset data hasn't changed.
$this->assertCount($currentpresets, $DB->get_records('adminpresets'));
$this->assertCount($currentitems, $DB->get_records('adminpresets_it'));
$this->assertCount($currentadvitems, $DB->get_records('adminpresets_it_a'));
$this->assertCount($currentplugins, $DB->get_records('adminpresets_plug'));
// Check the setting values have been reverted accordingly.
$this->assertEquals(1, get_config('core', 'enablebadges'));
$this->assertEquals(640, get_config('mod_lesson', 'mediawidth'));
$this->assertEquals(5, get_config('mod_lesson', 'maxanswers'));
$this->assertEquals(1, get_config('mod_lesson', 'maxanswers_adv'));
// These settings won't change, regardless if they are posted to the form.
$this->assertEquals(1, get_config('core', 'allowemojipicker'));
$this->assertEquals(1, get_config('core', 'enablecompletion'));
$this->assertEquals(0, get_config('core', 'usecomments'));
// Check the plugins visibility have been reverted accordingly.
$enabledplugins = \core\plugininfo\enrol::get_enabled_plugins();
$this->assertArrayHasKey('guest', $enabledplugins);
$enabledplugins = \core\plugininfo\mod::get_enabled_plugins();
$this->assertArrayHasKey('glossary', $enabledplugins);
// This plugin won't change (because it had the same value than before the preset was applied).
$enabledplugins = \core\plugininfo\qtype::get_enabled_plugins();
$this->assertArrayHasKey('truefalse', $enabledplugins);
}
/**
* Test the behaviour of execute() method when the preset applied id doesn't exist.
*
* @covers ::execute
*/
public function test_rollback_execute_unexisting_presetapp(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset and apply it.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset(['applypreset' => true]);
$presetappid = $DB->get_field('adminpresets_app', 'id', ['adminpresetid' => $presetid]);
// Initialise the parameters.
$_POST['action'] = 'rollback';
$_POST['mode'] = 'execute';
$_POST['id'] = $presetappid * 2; // Unexisting presetapp identifier.
$_POST['sesskey'] = sesskey();
// Create the rollback class and execute it.
$action = new rollback();
$this->expectException(\moodle_exception::class);
$action->execute();
}
}
+30
View File
@@ -0,0 +1,30 @@
<?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/>.
/**
* Admin tool presets plugin to load some settings.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'tool_admin_presets';
$plugin->version = 2024042200;
$plugin->requires = 2024041600;