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
+24
View File
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<PRESET>
<NAME>Imported preset</NAME>
<COMMENTS>&lt;p dir="ltr" style="text-align:left;"&gt;Porfolios enabled, Emojipicker enabled, Lesson media width set to 900 and maxanswers set to 2 and disabled advanced&lt;/p&gt;</COMMENTS>
<PRESET_DATE>1631615985</PRESET_DATE>
<SITE_URL>http://demo.moodle</SITE_URL>
<AUTHOR>Ada Lovelace</AUTHOR>
<MOODLE_VERSION>2021091100</MOODLE_VERSION>
<MOODLE_RELEASE>4.0dev (Build: 20210911)</MOODLE_RELEASE>
<ADMIN_SETTINGS>
<MOD_LESSON>
<SETTINGS>
<MAXANSWERS maxanswers_adv="">2</MAXANSWERS>
<MEDIAWIDTH>900</MEDIAWIDTH>
</SETTINGS>
</MOD_LESSON>
<NONE>
<SETTINGS>
<ALLOWEMOJIPICKER>1</ALLOWEMOJIPICKER>
<ENABLEPORTFOLIOS>1</ENABLEPORTFOLIOS>
</SETTINGS>
</NONE>
</ADMIN_SETTINGS>
</PRESET>
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<PRESET>
<NAME>Imported preset</NAME>
<COMMENTS>&lt;p dir="ltr" style="text-align:left;"&gt;Porfolios enabled, Emojipicker enabled, Lesson media width set to 900 and maxanswers set to 2 and disabled advanced. Plugins: disabled block_html, mod_database and mod_chat plugins and enabled atto_html, block_activity_modules and mod_lesson.&lt;/p&gt;</COMMENTS>
<PRESET_DATE>1631615985</PRESET_DATE>
<SITE_URL>http://demo.moodle</SITE_URL>
<AUTHOR>Ada Lovelace</AUTHOR>
<MOODLE_VERSION>2021091100</MOODLE_VERSION>
<MOODLE_RELEASE>4.0dev (Build: 20210911)</MOODLE_RELEASE>
<ADMIN_SETTINGS>
<MOD_LESSON>
<SETTINGS>
<MAXANSWERS maxanswers_adv="">2</MAXANSWERS>
<MEDIAWIDTH>900</MEDIAWIDTH>
</SETTINGS>
</MOD_LESSON>
<NONE>
<SETTINGS>
<ALLOWEMOJIPICKER>1</ALLOWEMOJIPICKER>
<ENABLEPORTFOLIOS>1</ENABLEPORTFOLIOS>
</SETTINGS>
</NONE>
</ADMIN_SETTINGS>
<PLUGINS>
<ATTO>
<HTML>1</HTML>
</ATTO>
<BLOCK>
<HTML>0</HTML>
<ACTIVITY_MODULES>1</ACTIVITY_MODULES>
</BLOCK>
<MOD>
<CHAT>0</CHAT>
<DATA>0</DATA>
<LESSON>1</LESSON>
</MOD>
</PLUGINS>
</PRESET>
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<PRESET>
<NAME>Imported preset</NAME>
<COMMENTS>&lt;p dir="ltr" style="text-align:left;"&gt;Porfolios enabled, Emojipicker enabled, Lesson media width set to 900 and maxanswers set to 2 and disabled advanced&lt;/p&gt;</COMMENTS>
<PRESET_DATE>1631615985</PRESET_DATE>
<SITE_URL>http://demo.moodle</SITE_URL>
<AUTHOR>Ada Lovelace</AUTHOR>
<MOODLE_VERSION>2021091100</MOODLE_VERSION>
<MOODLE_RELEASE>4.0dev (Build: 20210911)</MOODLE_RELEASE>
<ADMIN_SETTINGS>
<MOD_LESSON>
<SETTINGS>
<MAXANSWERS maxanswers_adv="">2</MAXANSWERS>
<MEDIAWIDTH>900</MEDIAWIDTH>
</SETTINGS>
</MOD_LESSON>
<NONE>
<SETTINGS>
<UNEXISTINGSETTING>0</UNEXISTINGSETTING>
<ALLOWEMOJIPICKER>1</ALLOWEMOJIPICKER>
<ENABLEPORTFOLIOS>1</ENABLEPORTFOLIOS>
</SETTINGS>
</NONE>
</ADMIN_SETTINGS>
</PRESET>
+38
View File
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<PRESET>
<NAME>Starter</NAME>
<COMMENTS>&lt;p dir="ltr" style="text-align:left;"&gt;Badges disabled, Emojipicker enabled, Lesson media width set to 900 and maxanswers set to 2 and disabled advanced. Plugins: disabled block_html, mod_database and mod_chat plugins and enabled atto_html and block_activity_modules.&lt;/p&gt;</COMMENTS>
<PRESET_DATE>1631615985</PRESET_DATE>
<SITE_URL>http://demo.moodle</SITE_URL>
<AUTHOR>Ada Lovelace</AUTHOR>
<MOODLE_VERSION>2021091100</MOODLE_VERSION>
<MOODLE_RELEASE>4.0dev (Build: 20210911)</MOODLE_RELEASE>
<ADMIN_SETTINGS>
<MOD_LESSON>
<SETTINGS>
<MAXANSWERS maxanswers_adv="">2</MAXANSWERS>
<MEDIAWIDTH>900</MEDIAWIDTH>
</SETTINGS>
</MOD_LESSON>
<NONE>
<SETTINGS>
<ALLOWEMOJIPICKER>1</ALLOWEMOJIPICKER>
<ENABLEBADGES>0</ENABLEBADGES>
</SETTINGS>
</NONE>
</ADMIN_SETTINGS>
<PLUGINS>
<ATTO>
<HTML>1</HTML>
</ATTO>
<BLOCK>
<HTML>0</HTML>
<ACTIVITY_MODULES>1</ACTIVITY_MODULES>
</BLOCK>
<MOD>
<CHAT>0</CHAT>
<DATA>0</DATA>
<LESSON>1</LESSON>
</MOD>
</PLUGINS>
</PRESET>
+23
View File
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<PRESET>
<NAME>Imported preset - Invalid XML file</NAME>
<COMMENTS></COMMENTS>
<PRESET_DATE>1631615985</PRESET_DATE>
<SITE_URL>http://demo.moodle</SITE_URL>
<AUTHOR>Ada Lovelace</AUTHOR>
<MOODLE_VERSION>2021091100</MOODLE_VERSION>
<MOODLE_RELEASE>4.0dev (Build: 20210911)</MOODLE_RELEASE>
<ADMIN_SETTINGS>
<MOD_LESSON>
<SETTINGS>
<MAXANSWERS maxanswers_adv="">2</MAXANSWERS>
<MEDIAWIDTH>900</MEDIAWIDTH>
</SETTINGS>
</MOD_LESSON>
<NONE>
<SETTINGS>
<ALLOWEMOJIPICKER>1</ALLOWEMOJIPICKER>
<ENABLEPORTFOLIOS>1</ENABLEPORTFOLIOS>
</SETTINGS>
</NONE>
</PRESET>
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<PRESET>
<NAME>Imported preset with unexisting category</NAME>
<COMMENTS></COMMENTS>
<PRESET_DATE>1631615985</PRESET_DATE>
<SITE_URL>http://demo.moodle</SITE_URL>
<AUTHOR>Ada Lovelace</AUTHOR>
<MOODLE_VERSION>2021091100</MOODLE_VERSION>
<MOODLE_RELEASE>4.0dev (Build: 20210911)</MOODLE_RELEASE>
<ADMIN_SETTINGS>
<NONE>
<UNEXISTINGCATEGORY>
<ENABLEPORTFOLIOS>1</ENABLEPORTFOLIOS>
</UNEXISTINGCATEGORY>
</NONE>
</ADMIN_SETTINGS>
</PRESET>
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<PRESET>
<NAME>Imported preset with unexisting setting</NAME>
<COMMENTS></COMMENTS>
<PRESET_DATE>1631615985</PRESET_DATE>
<SITE_URL>http://demo.moodle</SITE_URL>
<AUTHOR>Ada Lovelace</AUTHOR>
<MOODLE_VERSION>2021091100</MOODLE_VERSION>
<MOODLE_RELEASE>4.0dev (Build: 20210911)</MOODLE_RELEASE>
<ADMIN_SETTINGS>
<NONE>
<SETTINGS>
<UNEXISTINGSETTING>0</UNEXISTINGSETTING>
</SETTINGS>
</NONE>
</ADMIN_SETTINGS>
</PRESET>
+252
View File
@@ -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/>.
defined('MOODLE_INTERNAL') || die();
use core_adminpresets\local\setting\adminpresets_setting;
use core_adminpresets\manager;
use core_adminpresets\helper;
global $CFG;
require_once($CFG->libdir . '/adminlib.php');
/**
* Data generator for adminpresets component.
*
* @package core_adminpresets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_adminpresets_generator extends \component_generator_base {
/**
* Create a preset. This preset will have only 3 settings and 3 plugins.
* Settings:
* - none.enablebadges = 0
* - none.allowemojipicker = 1
* - mod_lesson.mediawidth = 900
* - mod_lesson.maxanswers = 2 with advanced disabled.
* Plugins:
* - enrol_guest = 0
* - mod_glossary = 0
* - qtype_truefalse = 1
*
* @param array $data Preset data. Supported values:
* - name. To define the preset name.
* - comments. To change the comments field.
* - author. To set the author.
* - applypreset. Whether the preset should be applied too or not.
* @return int Identifier of the preset created.
*/
public function create_preset(array $data = []): int {
global $DB, $USER, $CFG;
if (!isset($data['name'])) {
$data['name'] = 'Preset default name';
}
if (!isset($data['comments'])) {
$data['comments'] = 'Preset default comment';
}
if (!isset($data['author'])) {
$data['author'] = 'Default author';
}
if (!isset($data['iscore'])) {
$data['iscore'] = manager::NONCORE_PRESET;
}
// Validate iscore value.
$allowed = [manager::NONCORE_PRESET, manager::STARTER_PRESET, manager::FULL_PRESET];
if (!in_array($data['iscore'], $allowed)) {
$data['iscore'] = manager::NONCORE_PRESET;
}
$preset = [
'userid' => $USER->id,
'name' => $data['name'],
'comments' => $data['comments'],
'site' => $CFG->wwwroot,
'author' => $data['author'],
'moodleversion' => $CFG->version,
'moodlerelease' => $CFG->release,
'timecreated' => time(),
'timeimported' => 0,
'iscore' => $data['iscore'],
];
$presetid = $DB->insert_record('adminpresets', $preset);
$preset['id'] = $presetid;
// Setting: enablebadges = 0.
helper::add_item($presetid, 'enablebadges', '0');
// Setting: allowemojipicker = 1.
helper::add_item($presetid, 'allowemojipicker', '1');
// Setting: mediawidth = 900.
helper::add_item($presetid, 'mediawidth', '900', 'mod_lesson');
// Setting: maxanswers = 2 (with advanced disabled).
helper::add_item($presetid, 'maxanswers', '2', 'mod_lesson', 'maxanswers_adv', 0);
// Plugin: enrol_guest = 0.
helper::add_plugin($presetid, 'enrol', 'guest', 0);
// Plugin: mod_glossary = 0.
helper::add_plugin($presetid, 'mod', 'glossary', 0);
// Plugin: qtype_truefalse.
helper::add_plugin($presetid, 'qtype', 'truefalse', 1);
// Check if the preset should be created as applied preset too, to fill in the rest of the tables.
$applypreset = isset($data['applypreset']) && $data['applypreset'];
if ($applypreset) {
$presetapp = [
'adminpresetid' => $presetid,
'userid' => $USER->id,
'time' => time(),
];
$appid = $DB->insert_record('adminpresets_app', $presetapp);
$this->apply_setting($appid, 'enablebadges', '1', '0');
// The allowemojipicker setting shouldn't be applied because the value matches the current one.
$this->apply_setting($appid, 'mediawidth', '640', '900', 'mod_lesson');
$this->apply_setting($appid, 'maxanswers', '5', '2', 'mod_lesson');
$this->apply_setting($appid, 'maxanswers_adv', '1', '0', 'mod_lesson', 'maxanswers');
$this->apply_plugin($appid, 'enrol', 'guest', 1, 0);
$this->apply_plugin($appid, 'mod', 'glossary', 1, 0);
// The qtype_truefalse plugin shouldn't be applied because the value matches the current one.
}
return $presetid;
}
/**
* Helper method to create an applied setting item.
*
* @param int $appid The applied preset identifier.
* @param string $name The setting name.
* @param string $oldvalue The setting old value.
* @param string $newvalue The setting new value.
* @param string|null $plugin The setting plugin (or null if none).
* @param string|null $itemname Whether it should be treated as advanced item or not.
*
* @return bool|int true or new id.
*/
private function apply_setting(int $appid, string $name, string $oldvalue, string $newvalue, ?string $plugin = null,
?string $itemname = null) {
global $DB;
set_config($name, $newvalue, $plugin);
$configlogid = $this->add_to_config_log($name, $oldvalue, $newvalue, $plugin);
$presetappitem = [
'adminpresetapplyid' => $appid,
'configlogid' => $configlogid,
];
$table = 'adminpresets_app_it';
if (!is_null($itemname)) {
$table = 'adminpresets_app_it_a';
$presetappitem['itemname'] = $itemname;
}
$appitemid = $DB->insert_record($table, $presetappitem);
return $appitemid;
}
/**
* Helper method to create an applied plugin.
*
* @param int $appid The applied preset identifier.
* @param string $plugin The plugin type.
* @param string $name The plugin name.
* @param int $oldvalue The setting old value.
* @param int $newvalue The setting new value.
*
* @return bool|int true or new id.
*/
private function apply_plugin(int $appid, string $plugin, string $name, int $oldvalue, int $newvalue) {
global $DB;
// Change plugin visibility.
$pluginclass = \core_plugin_manager::resolve_plugininfo_class($plugin);
$pluginclass::enable_plugin($name, $newvalue);
// Create entry in applied plugins table.
$presetappplug = [
'adminpresetapplyid' => $appid,
'plugin' => $plugin,
'name' => $name,
'value' => $newvalue,
'oldvalue' => $oldvalue,
];
$appplugid = $DB->insert_record('adminpresets_app_plug', $presetappplug);
return $appplugid;
}
/**
* Helper method to add entry in config_log.
*
* @param string $name The setting name.
* @param string $oldvalue The setting old value.
* @param string $value The setting new value.
* @param string|null $plugin The setting plugin (or null if the setting doesn't belong to any plugin).
* @return int The id of the config_log entry created.
*/
private function add_to_config_log(string $name, string $oldvalue, string $value, ?string $plugin = null): int {
global $DB, $USER;
$log = new stdClass();
$log->userid = $USER->id;
$log->timemodified = time();
$log->name = $name;
$log->oldvalue = $oldvalue;
$log->value = $value;
$log->plugin = $plugin;
$id = $DB->insert_record('config_log', $log);
return $id;
}
/**
* Helper method to access to a protected property.
*
* @param string|object $object The class.
* @param string $property The private/protected property in $object to access.
* @return mixed The current value of the property.
*/
public function access_protected($object, string $property) {
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
return $property->getValue($object);
}
/**
* Given a tree category and setting name, it gets the adminpresets_setting class.
*
* @param string $category Tree category name where the setting is located.
* @param string $settingname Setting name to get the class.
* @return adminpresets_setting
*/
public function get_admin_preset_setting(string $category, string $settingname): adminpresets_setting {
$adminroot = admin_get_root();
// Set method accessibility.
$method = new ReflectionMethod(manager::class, 'get_setting');
// Get the proper adminpresets_setting instance.
$settingpage = $adminroot->locate($category);
$settingdata = $settingpage->settings->$settingname;
return $method->invokeArgs(new manager(), [$settingdata, '']);
}
}
+236
View File
@@ -0,0 +1,236 @@
<?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 core_adminpresets;
/**
* Tests for the data generator.
*
* @package core_adminpresets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass core_adminpresets_generator
*/
class generator_test extends \advanced_testcase {
/**
* Test the behaviour of create_preset() method.
*
* @covers ::create_preset
* @dataProvider create_preset_provider
*
* @param string|null $name Preset name field.
* @param string|null $comments Preset comments field.
* @param string|null $author Preset author field.
* @param bool $applypreset Whether the preset should be applied or not.
* @param int|null $iscore Whether the preset is a core preset or not.
* @param int|null $iscoreresult Expected iscore value for the result preset.
*/
public function test_create_preset(?string $name = null, ?string $comments = null, ?string $author = null,
bool $applypreset = false, ?int $iscore = null, ?int $iscoreresult = null): void {
global $CFG, $DB;
$this->resetAfterTest();
$data = [];
if (isset($name)) {
$data['name'] = $name;
} else {
// Set the default value used in the generator.
$name = 'Preset default name';
}
if (isset($comments)) {
// Set the default value used in the generator.
$data['comments'] = $comments;
} else {
// Set the default value used in the generator.
$comments = 'Preset default comment';
}
if (isset($author)) {
$data['author'] = $author;
} else {
$author = 'Default author';
}
if ($applypreset) {
$data['applypreset'] = $applypreset;
}
if (isset($iscore)) {
$data['iscore'] = $iscore;
}
if (!isset($iscoreresult)) {
$iscoreresult = manager::NONCORE_PRESET;
}
// Create a preset.
$presetid = $this->getDataGenerator()->get_plugin_generator('core_adminpresets')->create_preset($data);
// Check the preset data.
$preset = $DB->get_record('adminpresets', ['id' => $presetid]);
$this->assertEquals($name, $preset->name);
$this->assertEquals($comments, $preset->comments);
$this->assertEquals($author, $preset->author);
$this->assertEquals($iscoreresult, $preset->iscore);
$this->assertEquals($CFG->version, $preset->moodleversion);
$this->assertEquals($CFG->release, $preset->moodlerelease);
$this->assertEquals($CFG->wwwroot, $preset->site);
// Check the settings.
$settings = $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid]);
$this->assertCount(4, $settings);
// These are the settings created in the generator. Check the results match them.
$expectedsettings = [
'enablebadges' => 0,
'allowemojipicker' => 1,
'mediawidth' => 900,
'maxanswers' => 2,
];
foreach ($settings as $setting) {
$this->assertArrayHasKey($setting->name, $expectedsettings);
$this->assertEquals($expectedsettings[$setting->name], $setting->value);
}
// Check the advanced settings (should be only one).
$settingsid = array_keys($settings);
list($insql, $inparams) = $DB->get_in_or_equal($settingsid);
$advsettings = $DB->get_records_select('adminpresets_it_a', 'itemid ' . $insql, $inparams);
$this->assertCount(1, $advsettings);
$advsetting = reset($advsettings);
$this->assertEquals('maxanswers_adv', $advsetting->name);
$this->assertEquals(0, $advsetting->value);
// Check the plugins.
$plugins = $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid]);
$this->assertCount(3, $plugins);
// These are the plugins created in the generator. Check the results match them.
$expectedplugins = [
'enrol' => [
'guest' => 0,
],
'mod' => [
'glossary' => 0,
],
'qtype' => [
'truefalse' => 1,
],
];
foreach ($plugins as $plugin) {
$this->assertArrayHasKey($plugin->plugin, $expectedplugins);
$this->assertArrayHasKey($plugin->name, $expectedplugins[$plugin->plugin]);
$this->assertEquals($expectedplugins[$plugin->plugin][$plugin->name], $plugin->enabled);
}
if ($applypreset) {
// Verify that the preset has been applied.
$apps = $DB->get_records('adminpresets_app', ['adminpresetid' => $presetid]);
$this->assertCount(1, $apps);
$app = reset($apps);
// Check the applied settings.
$appsettings = $DB->get_records('adminpresets_app_it', ['adminpresetapplyid' => $app->id]);
$this->assertCount(3, $appsettings);
// These are the settings created in the generator (all but the allowemojipicker because it hasn't changed).
$expectedappsettings = $expectedsettings;
unset($expectedappsettings['allowemojipicker']);
// Check the results match the expected settings applied.
foreach ($appsettings as $appsetting) {
$configlog = $DB->get_record('config_log', ['id' => $appsetting->configlogid]);
$this->assertArrayHasKey($configlog->name, $expectedappsettings);
$this->assertEquals($expectedappsettings[$configlog->name], $configlog->value);
}
$appsettings = $DB->get_records('adminpresets_app_it_a', ['adminpresetapplyid' => $app->id]);
$this->assertCount(1, $appsettings);
$appsetting = reset($appsettings);
$configlog = $DB->get_record('config_log', ['id' => $appsetting->configlogid]);
$this->assertEquals('maxanswers_adv', $configlog->name);
$this->assertEquals(0, $configlog->value);
// Check the applied plugins.
$appplugins = $DB->get_records('adminpresets_app_plug', ['adminpresetapplyid' => $app->id]);
$this->assertCount(2, $appplugins);
// These are the plugins created in the generator (all but the qtype_truefalse because it hasn't changed).
$expectedappplugins = $expectedplugins;
unset($expectedappplugins['qtype']);
// Check the results match the expected plugins applied.
foreach ($appplugins as $appplugin) {
$this->assertArrayHasKey($appplugin->plugin, $expectedappplugins);
$this->assertArrayHasKey($appplugin->name, $expectedappplugins[$appplugin->plugin]);
$this->assertEquals($expectedappplugins[$appplugin->plugin][$appplugin->name], $appplugin->value);
}
}
}
/**
* Data provider for test_create_preset().
*
* @return array
*/
public function create_preset_provider(): array {
return [
'Default values' => [
],
'Name not empty' => [
'name' => 'Preset xaxi name',
],
'Comment not empty' => [
'name' => null,
'comments' => 'This is a different comment',
],
'Author not empty' => [
'name' => null,
'comments' => null,
'author' => 'Ada Lovelace',
],
'No default values for all the fields' => [
'name' => 'Preset with a super-nice name',
'comments' => 'This is a comment different from the previous one',
'author' => 'Alejandro Sanz',
],
'Apply preset' => [
'name' => null,
'comments' => null,
'author' => null,
'applypreset' => true,
],
'Starter preset' => [
'name' => 'Starter',
'comments' => null,
'author' => null,
'applypreset' => false,
'iscore' => manager::STARTER_PRESET,
'iscoreresult' => manager::STARTER_PRESET,
],
'Full preset' => [
'name' => 'Full',
'comments' => null,
'author' => null,
'applypreset' => false,
'iscore' => manager::FULL_PRESET,
'iscoreresult' => manager::FULL_PRESET,
],
'Invalid iscore' => [
'name' => 'Invalid iscore value',
'comments' => null,
'author' => null,
'applypreset' => false,
'iscore' => -1,
'iscoreresult' => manager::NONCORE_PRESET,
],
];
}
}
+387
View File
@@ -0,0 +1,387 @@
<?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 core_adminpresets;
/**
* Tests for the helper class.
*
* @package core_adminpresets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass helper
*/
class helper_test extends \advanced_testcase {
/**
* Test the behaviour of create_preset() method.
*
* @covers ::create_preset
* @dataProvider create_preset_provider
*
* @param string|null $name Preset name field.
* @param string|null $comments Preset comments field.
* @param int|null $iscore Preset iscore field.
* @param int|null $iscoreresult Expected iscore value for the result preset.
*/
public function test_create_preset(?string $name = null, ?string $comments = null, ?int $iscore = null,
?int $iscoreresult = null): void {
global $CFG, $DB, $USER;
$this->resetAfterTest();
$data = [];
if (isset($name)) {
$data['name'] = $name;
}
if (isset($comments)) {
$data['comments'] = $comments;
}
if (isset($iscore)) {
$data['iscore'] = $iscore;
}
if (!isset($iscoreresult)) {
$iscoreresult = manager::NONCORE_PRESET;
}
// Create a preset.
$presetid = helper::create_preset($data);
// Check the preset data.
$preset = $DB->get_record('adminpresets', ['id' => $presetid]);
$this->assertEquals($name, $preset->name);
$this->assertEquals($comments, $preset->comments);
$this->assertEquals(fullname($USER), $preset->author);
$this->assertEquals($iscoreresult, $preset->iscore);
$this->assertEquals($CFG->version, $preset->moodleversion);
$this->assertEquals($CFG->release, $preset->moodlerelease);
$this->assertEquals($CFG->wwwroot, $preset->site);
// Check the preset is empty and hasn't settings or plugins.
$settings = $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid]);
$this->assertCount(0, $settings);
$plugins = $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid]);
$this->assertCount(0, $plugins);
}
/**
* Data provider for test_create_preset().
*
* @return array
*/
public function create_preset_provider(): array {
return [
'Default values' => [
],
'Name not empty' => [
'name' => 'Preset xaxi name',
],
'Comments not empty' => [
'name' => null,
'comments' => 'This is a different comment',
],
'Name and comments not empty' => [
'name' => 'Preset with a super-nice name',
'comments' => 'This is a comment different from the previous one',
],
'Starter preset' => [
'name' => 'Starter',
'comments' => null,
'iscore' => manager::STARTER_PRESET,
'iscoreresult' => manager::STARTER_PRESET,
],
'Full preset' => [
'name' => 'Full',
'comments' => null,
'iscore' => manager::FULL_PRESET,
'iscoreresult' => manager::FULL_PRESET,
],
'Invalid iscore' => [
'name' => 'Invalid iscore value',
'comments' => null,
'iscore' => -1,
'iscoreresult' => manager::NONCORE_PRESET,
],
];
}
/**
* Test the behaviour of add_item() method.
*
* @covers ::add_item
* @dataProvider add_item_provider
*
* @param string $name Item name.
* @param string $value Item value.
* @param string|null $plugin Item plugin.
* @param string|null $advname If the item is an advanced setting, the name of the advanced setting should be specified here.
* @param string|null $advvalue If the item is an advanced setting, the value of the advanced setting should be specified here.
*/
public function test_add_item(string $name, string $value, ?string $plugin = 'none', ?string $advname = null,
?string $advvalue = null): void {
global $DB;
$this->resetAfterTest();
// Create a preset.
$presetid = helper::create_preset([]);
$this->assertEquals(1, $DB->count_records('adminpresets', ['id' => $presetid]));
// Add items.
$itemid = helper::add_item($presetid, $name, $value, $plugin, $advname, $advvalue);
// Check settings have been created.
$settings = $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid]);
$this->assertCount(1, $settings);
$setting = reset($settings);
$this->assertEquals($itemid, $setting->id);
$this->assertEquals($name, $setting->name);
$this->assertEquals($value, $setting->value);
$this->assertEquals($plugin, $setting->plugin);
if ($advname) {
// Check settings have been created.
$advsettings = $DB->get_records('adminpresets_it_a', ['itemid' => $itemid]);
$this->assertCount(1, $advsettings);
$advsetting = reset($advsettings);
$this->assertEquals($advname, $advsetting->name);
$this->assertEquals($advvalue, $advsetting->value);
} else {
// Check no advanced items have been created.
$this->assertEquals(0, $DB->count_records('adminpresets_it_a', ['itemid' => $itemid]));
}
// Check no plugins have been created.
$this->assertEquals(0, $DB->count_records('adminpresets_plug', ['adminpresetid' => $presetid]));
}
/**
* Data provider for test_add_item().
*
* @return array
*/
public function add_item_provider(): array {
return [
'Setting without plugin' => [
'name' => 'settingname',
'value' => 'thisisthevalue',
],
'Setting with plugin' => [
'name' => 'settingname',
'value' => 'thisisthevalue',
'plugin' => 'pluginname',
],
'Setting with advanced item' => [
'name' => 'settingname',
'value' => 'thevalue',
'plugin' => 'pluginname',
'advname' => 'advsettingname',
'advvalue' => 'advsettingvalue',
],
];
}
/**
* Test the behaviour of add_plugin() method.
*
* @covers ::add_plugin
* @dataProvider add_plugin_provider
*
* @param string $type Plugin type.
* @param string $name Plugin name.
* @param mixed $enabled Whether the plugin will be enabled or not.
*/
public function test_add_plugin(string $type, string $name, $enabled = 0): void {
global $DB;
$this->resetAfterTest();
// Create a preset.
$presetid = helper::create_preset([]);
$this->assertEquals(1, $DB->count_records('adminpresets', ['id' => $presetid]));
// Add plugin.
$pluginid = helper::add_plugin($presetid, $type, $name, $enabled);
// Check plugin has been created.
$pluggins = $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid]);
$this->assertCount(1, $pluggins);
$plugin = reset($pluggins);
$this->assertEquals($pluginid, $plugin->id);
$this->assertEquals($type, $plugin->plugin);
$this->assertEquals($name, $plugin->name);
$this->assertEquals((int) $enabled, $plugin->enabled);
// Check no settings have been created.
$this->assertEquals(0, $DB->count_records('adminpresets_it', ['adminpresetid' => $presetid]));
}
/**
* Data provider for test_add_plugin().
*
* @return array
*/
public function add_plugin_provider(): array {
return [
'Plugin: enabled (using int)' => [
'type' => 'plugintype',
'name' => 'pluginname',
'enabled' => 1,
],
'Plugin: enabled (using bool)' => [
'type' => 'plugintype',
'name' => 'pluginname',
'enabled' => true,
],
'Plugin: disabled (using int)' => [
'type' => 'plugintype',
'name' => 'pluginname',
'enabled' => 0,
],
'Plugin: disabled (using bool)' => [
'type' => 'plugintype',
'name' => 'pluginname',
'enabled' => false,
],
'Plugin: negative int value' => [
'type' => 'plugintype',
'name' => 'pluginname',
'enabled' => -9999,
],
];
}
/**
* Test the behaviour of change_default_preset() method.
*
* @covers ::change_default_preset
* @dataProvider change_default_preset_provider
*
* @param string $preset The preset name to apply or the path to the XML to be imported and applied.
* @param array|null $settings A few settings to check (with their expected values).
* @param array|null $plugins A few module plugins to check (with their expected values for the visibility).
*/
public function test_change_default_preset(string $preset, ?array $settings = null, ?array $plugins = null): void {
$this->resetAfterTest();
$this->setAdminUser();
// We need to change some of the default values; otherwise, the full preset won't be applied, because all the settings
// and plugins are the same.
set_config('enableanalytics', '0');
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$generator->create_preset(['name' => 'Preset 1']);
$presetid = helper::change_default_preset($preset);
if (empty($settings) && empty($plugins)) {
// The preset hasn't been applied.
$this->assertNull($presetid);
} else {
// The preset has been applied. Check the settings and plugins are the expected.
$this->assertNotEmpty($presetid);
// Check the setting values have changed accordingly with the ones defined in the preset.
foreach ($settings as $settingname => $settingvalue) {
$this->assertEquals($settingvalue, get_config('core', $settingname));
}
// Check the plugins visibility have changed accordingly with the ones defined in the preset.
$enabledplugins = \core\plugininfo\mod::get_enabled_plugins();
foreach ($plugins as $pluginname => $pluginvalue) {
if ($pluginvalue) {
$this->assertArrayHasKey($pluginname, $enabledplugins);
} else {
$this->assertArrayNotHasKey($pluginname, $enabledplugins);
}
}
}
}
/**
* Data provider for test_change_default_preset().
*
* @return array
*/
public function change_default_preset_provider(): array {
return [
'Starter preset' => [
'preset' => 'starter',
'settings' => [
'enablebadges' => 0,
'enableportfolios' => 0,
],
'plugins' => [
'assign' => 1,
'chat' => 0,
'data' => 0,
'lesson' => 0,
],
],
'Full preset' => [
'preset' => 'full',
'settings' => [
'enablebadges' => 1,
'enableportfolios' => 0,
],
'plugins' => [
'assign' => 1,
'book' => 1,
'data' => 1,
'lesson' => 1,
],
],
'Preset 1, created manually' => [
'preset' => 'Preset 1',
'settings' => [
'enablebadges' => 0,
'allowemojipicker' => 1,
],
'plugins' => [
'assign' => 1,
'glossary' => 0,
],
],
'Unexisting preset name' => [
'preset' => 'unexisting',
],
'Valid XML file' => [
'preset' => __DIR__ . '/fixtures/import_settings_plugins.xml',
'settings' => [
'allowemojipicker' => 1,
'enableportfolios' => 1,
],
'plugins' => [
'assign' => 1,
'chat' => 0,
'data' => 0,
'lesson' => 1,
],
],
'Invalid XML file' => [
'preset' => __DIR__ . '/fixtures/invalid_xml_file.xml',
],
'Unexisting XML file' => [
'preset' => __DIR__ . '/fixtures/unexisting.xml',
],
];
}
}
@@ -0,0 +1,92 @@
<?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 core_adminpresets\local\setting;
/**
* Tests for the adminpresets_admin_setting_bloglevel class.
*
* @package core_adminpresets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_adminpresets\local\setting\adminpresets_admin_setting_bloglevel
*/
class adminpresets_admin_setting_bloglevel_test extends \advanced_testcase {
/**
* Test the behaviour of save_value() method.
*
* @covers ::save_value
* @dataProvider save_value_provider
*
* @param int $settingvalue Setting value to be saved.
* @param bool $expectedsaved Whether the setting will be saved or not.
*/
public function test_save_value(int $settingvalue, bool $expectedsaved): void {
global $DB;
$this->resetAfterTest();
// Login as admin, to access all the settings.
$this->setAdminUser();
// Set the config values (to confirm they change after applying the preset).
set_config('bloglevel', BLOG_SITE_LEVEL); // All site users can see all blog entries.
// Get the setting and save the value.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$setting = $generator->get_admin_preset_setting('blog', 'bloglevel');
$result = $setting->save_value(false, $settingvalue);
// Check the result is the expected (saved when it has a different value and ignored when the value is the same).
if ($expectedsaved) {
$this->assertCount(1, $DB->get_records('config_log', ['id' => $result]));
// Specific from the save_value in adminpresets_admin_setting_bloglevel.
if ($settingvalue != 0) {
$this->assertTrue((bool) $DB->get_field('block', 'visible', ['name' => 'blog_menu']));
} else {
$this->assertFalse((bool) $DB->get_field('block', 'visible', ['name' => 'blog_menu']));
}
} else {
$this->assertFalse($result);
}
$this->assertEquals($settingvalue, get_config('core', 'bloglevel'));
}
/**
* Data provider for test_save_value().
*
* @return array
*/
public function save_value_provider(): array {
return [
'Save the bloglevel and set blog_menu block visibility to true' => [
'setttingvalue' => BLOG_USER_LEVEL,
'expectedsaved' => true,
],
'Same value to bloglevel, so it will not be saved' => [
'setttingvalue' => BLOG_SITE_LEVEL,
'expectedsaved' => false,
],
'Save the bloglevel and set blog_menu block visibility to false' => [
'setttingvalue' => 0,
'expectedsaved' => true,
],
];
}
}
@@ -0,0 +1,94 @@
<?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 core_adminpresets\local\setting;
/**
* Tests for the adminpresets_admin_setting_sitesettext class.
*
* @package core_adminpresets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_adminpresets\local\setting\adminpresets_admin_setting_sitesettext
*/
class adminpresets_admin_setting_sitesettext_test extends \advanced_testcase {
/**
* Test the behaviour of save_value() method.
*
* @covers ::save_value
* @dataProvider save_value_provider
*
* @param string $settingname Setting name to save.
* @param string $settingvalue Setting value to be saved.
* @param bool $expectedsaved Whether the setting will be saved or not.
*/
public function test_save_value(string $settingname, string $settingvalue, bool $expectedsaved): void {
global $DB;
$this->resetAfterTest();
// Login as admin, to access all the settings.
$this->setAdminUser();
// Get the setting and save the value.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$setting = $generator->get_admin_preset_setting('frontpagesettings', $settingname);
$result = $setting->save_value(false, $settingvalue);
// Check the result is the expected (saved when it has a different value and ignored when the value is the same).
if ($expectedsaved) {
$this->assertCount(1, $DB->get_records('config_log', ['id' => $result]));
// Specific from the save_value in adminpresets_admin_setting_sitesettext.
$sitecourse = $DB->get_record('course', ['id' => 1]);
$this->assertEquals($settingvalue, $sitecourse->{$settingname});
} else {
$this->assertFalse($result);
}
}
/**
* Data provider for test_save_value().
*
* @return array
*/
public function save_value_provider(): array {
return [
'Fullname: different value' => [
'settingname' => 'fullname',
'setttingvalue' => 'New site fullname',
'expectedsaved' => true,
],
'Fullname: same value' => [
'settingname' => 'fullname',
'setttingvalue' => 'PHPUnit test site',
'expectedsaved' => false,
],
'Summary: different value' => [
'settingname' => 'summary',
'setttingvalue' => 'This is a new site summary.',
'expectedsaved' => true,
],
'Summary: same value' => [
'settingname' => 'summary',
'setttingvalue' => '',
'expectedsaved' => false,
],
];
}
}
@@ -0,0 +1,198 @@
<?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 core_adminpresets\local\setting;
/**
* Tests for the adminpresets_setting class.
*
* @package core_adminpresets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_adminpresets\local\setting\adminpresets_setting
*/
class adminpresets_setting_test extends \advanced_testcase {
/**
* Test the behaviour of save_value() method.
*
* @covers ::save_value
* @dataProvider save_value_provider
*
* @param string $category Admin tree where the setting belongs.
* @param string $settingplugin Plugin where the setting belongs.
* @param string $settingname Setting name.
* @param string $settingvalue Setting value to be saved.
* @param bool $expectedsaved Whether the setting will be saved or not.
*/
public function test_save_value(string $category, string $settingplugin, string $settingname, string $settingvalue,
bool $expectedsaved): void {
global $DB;
$this->resetAfterTest();
// Login as admin, to access all the settings.
$this->setAdminUser();
// Set the config values (to confirm they change after applying the preset).
set_config('enablebadges', 1);
set_config('mediawidth', '640', 'mod_lesson');
// The expected setting name in the admin tree is $plugin.$name when plugin is not core.
if ($settingplugin !== 'core') {
$name = $settingplugin . $settingname;
} else {
$name = $settingname;
}
// Get the setting and save the value.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$setting = $generator->get_admin_preset_setting($category, $name);
$result = $setting->save_value(false, $settingvalue);
// Check the result is the expected (saved when it has a different value and ignored when the value is the same).
if ($expectedsaved) {
$this->assertCount(1, $DB->get_records('config_log', ['id' => $result]));
} else {
$this->assertFalse($result);
}
$this->assertEquals($settingvalue, get_config($settingplugin, $settingname));
}
/**
* Data provider for test_save_value().
*
* @return array
*/
public function save_value_provider(): array {
return [
'Core setting with the same value is not saved' => [
'category' => 'optionalsubsystems',
'settingplugin' => 'core',
'settingname' => 'enablebadges',
'setttingvalue' => '1',
'expectedsaved' => false,
],
'Core setting with a different value is saved' => [
'category' => 'optionalsubsystems',
'settingplugin' => 'core',
'settingname' => 'enablebadges',
'setttingvalue' => '0',
'expectedsaved' => true,
],
'Plugin setting with the same value is not saved' => [
'category' => 'modsettinglesson',
'settingplugin' => 'mod_lesson',
'settingname' => 'mediawidth',
'setttingvalue' => '640',
'expectedsaved' => false,
],
'Plugin setting with different value is saved' => [
'category' => 'modsettinglesson',
'settingplugin' => 'mod_lesson',
'settingname' => 'mediawidth',
'setttingvalue' => '900',
'expectedsaved' => true,
],
];
}
/**
* Test the behaviour of save_attributes_values() method.
*
* @covers ::save_attributes_values
* @dataProvider save_attributes_values_provider
*
* @param string $category Admin tree where the setting belongs.
* @param string $settingplugin Plugin where the setting belongs.
* @param string $settingname Setting name.
* @param string|null $advsettingname Advanced setting name.
* @param string $advsettingvalue Advanced setting value to be saved.
* @param bool $expectedsaved Whether the setting will be saved or not.
*/
public function test_save_attributes_values(string $category, string $settingplugin, string $settingname,
?string $advsettingname, string $advsettingvalue, bool $expectedsaved): void {
global $DB;
$this->resetAfterTest();
// Login as admin, to access all the settings.
$this->setAdminUser();
// Set the config values (to confirm they change after applying the preset).
set_config('maxanswers_adv', '1', 'mod_lesson');
// The expected setting name in the admin tree is $plugin.$name when plugin is not core.
if ($settingplugin !== 'core') {
$name = $settingplugin . $settingname;
} else {
$name = $settingname;
}
// Get the setting and save the value.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$setting = $generator->get_admin_preset_setting($category, $name);
if ($advsettingname) {
$setting->set_attribute_value($advsettingname, $advsettingvalue);
}
$result = $setting->save_attributes_values();
// Check the result is the expected (saved when it has a different value and ignored when the value is the same).
if ($expectedsaved) {
$this->assertCount(1, $result);
$configlog = reset($result);
$this->assertCount(1, $DB->get_records('config_log', ['id' => $configlog]));
} else {
$this->assertFalse($result);
}
if ($advsettingname) {
$this->assertEquals($advsettingvalue, get_config($settingplugin, $advsettingname));
}
}
/**
* Data provider for test_save_attributes_values().
*
* @return array
*/
public function save_attributes_values_provider(): array {
return [
'Plugin setting with the same value is not saved' => [
'category' => 'modsettinglesson',
'settingplugin' => 'mod_lesson',
'settingname' => 'maxanswers',
'advsettingname' => 'maxanswers_adv',
'advsetttingvalue' => '1',
'expectedsaved' => false,
],
'Plugin setting with different value is saved' => [
'category' => 'modsettinglesson',
'settingplugin' => 'mod_lesson',
'settingname' => 'maxanswers',
'advsettingname' => 'maxanswers_adv',
'advsetttingvalue' => '0',
'expectedsaved' => true,
],
'Plugin setting without advanced attributes are not saved' => [
'category' => 'modsettinglesson',
'settingplugin' => 'mod_lesson',
'settingname' => 'maxanswers',
'advsettingname' => null,
'advsetttingvalue' => '0',
'expectedsaved' => false,
],
];
}
}
+798
View File
@@ -0,0 +1,798 @@
<?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 core_adminpresets;
use moodle_exception;
use stdClass;
/**
* Tests for the manager class.
*
* @package core_adminpresets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_adminpresets\manager
*/
class manager_test extends \advanced_testcase {
/**
* Include required libraries.
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once($CFG->libdir.'/adminlib.php');
}
/**
* Test the behaviour of protected get_site_settings method.
*
* @covers ::get_site_settings
* @covers ::get_settings
*/
public function test_manager_get_site_settings(): void {
global $DB;
$this->resetAfterTest();
// Login as admin, to access all the settings.
$this->setAdminUser();
$manager = new manager();
$result = $manager->get_site_settings();
// Check fullname is set into the none category.
$this->assertInstanceOf(
'\core_adminpresets\local\setting\adminpresets_admin_setting_sitesettext',
$result['none']['fullname']
);
$this->assertEquals('PHPUnit test site', $result['none']['fullname']->get_value());
// Check some of the config setting is present (they should be stored in the "none" category).
$this->assertInstanceOf(
'\core_adminpresets\local\setting\adminpresets_admin_setting_configcheckbox',
$result['none']['enablecompletion']
);
$this->assertEquals(1, $result['none']['enablecompletion']->get_value());
// Check some of the plugin config settings is present.
$this->assertInstanceOf(
'\core_adminpresets\local\setting\adminpresets_admin_setting_configtext',
$result['folder']['maxsizetodownload']
);
$this->assertEquals(0, $result['folder']['maxsizetodownload']->get_value());
// Set some of these values.
$sitecourse = new stdClass();
$sitecourse->id = 1;
$sitecourse->fullname = 'New site fullname';
$DB->update_record('course', $sitecourse);
set_config('enablecompletion', 0);
set_config('maxsizetodownload', 101, 'folder');
// Check the new values are returned properly.
$result = $manager->get_site_settings();
// Site fullname.
$this->assertInstanceOf(
'\core_adminpresets\local\setting\adminpresets_admin_setting_sitesettext',
$result['none']['fullname']
);
$this->assertEquals($sitecourse->fullname, $result['none']['fullname']->get_value());
// Config setting.
$this->assertInstanceOf(
'\core_adminpresets\local\setting\adminpresets_admin_setting_configcheckbox',
$result['none']['enablecompletion']
);
$this->assertEquals(0, $result['none']['enablecompletion']->get_value());
// Plugin config settting.
$this->assertInstanceOf(
'\core_adminpresets\local\setting\adminpresets_admin_setting_configtext',
$result['folder']['maxsizetodownload']
);
$this->assertEquals(101, $result['folder']['maxsizetodownload']->get_value());
}
/**
* Test the behaviour of protected get_setting method.
*
* @covers ::get_setting
* @covers ::get_settings_class
*/
public function test_manager_get_setting(): void {
$this->resetAfterTest();
// Login as admin, to access all the settings.
$this->setAdminUser();
$adminroot = admin_get_root();
// Check the adminpresets_xxxxx class is created properly when it exists.
$settingpage = $adminroot->locate('optionalsubsystems');
$settingdata = $settingpage->settings->enablebadges;
$manager = new manager();
$result = $manager->get_setting($settingdata, '');
$this->assertInstanceOf('\core_adminpresets\local\setting\adminpresets_admin_setting_configcheckbox', $result);
$this->assertNotEquals('core_adminpresets\local\setting\adminpresets_setting', get_class($result));
// Check the mapped class is returned when no specific class exists and it exists in the mappings array.
$settingpage = $adminroot->locate('h5psettings');
$settingdata = $settingpage->settings->h5plibraryhandler;;
$result = $manager->get_setting($settingdata, '');
$this->assertInstanceOf('\core_adminpresets\local\setting\adminpresets_admin_setting_configselect', $result);
$this->assertNotEquals(
'core_adminpresets\local\setting\adminpresets_admin_settings_h5plib_handler_select',
get_class($result)
);
// Check the mapped class is returned when no specific class exists and it exists in the mappings array.
$settingpage = $adminroot->locate('modsettingquiz');
$settingdata = $settingpage->settings->quizbrowsersecurity;;
$result = $manager->get_setting($settingdata, '');
$this->assertInstanceOf(\mod_quiz\adminpresets\adminpresets_browser_security_setting::class, $result);
$this->assertNotEquals(\core_adminpresets\local\setting\adminpresets_setting::class, get_class($result));
// Check the adminpresets_setting class is returned when no specific class exists.
$settingpage = $adminroot->locate('managecustomfields');
$settingdata = $settingpage->settings->customfieldsui;;
$result = $manager->get_setting($settingdata, '');
$this->assertInstanceOf('\core_adminpresets\local\setting\adminpresets_setting', $result);
$this->assertEquals('core_adminpresets\local\setting\adminpresets_setting', get_class($result));
}
/**
* Test the behaviour of apply_preset() method when the given presetid doesn't exist.
*
* @covers ::apply_preset
*/
public function test_apply_preset_unexisting_preset(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create some presets.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
// Unexisting preset identifier.
$unexistingid = $presetid * 2;
$manager = new manager();
$this->expectException(\moodle_exception::class);
$manager->apply_preset($unexistingid);
}
/**
* Test the behaviour of apply_preset() method.
*
* @covers ::apply_preset
*/
public function test_apply_preset(): 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);
// Call the apply_preset method.
$manager = new manager();
$manager->apply_preset($presetid);
// 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);
// Check the presetid has been also stored in the lastpresetapplied config setting.
$this->assertEquals($presetid, get_config('adminpresets', 'lastpresetapplied'));
// Call apply_preset as a simulation, so it shouldn't be applied and lastpresetapplied should still be $presetid.
$presetid2 = $generator->create_preset();
$manager->apply_preset($presetid2, true);
$this->assertEquals($presetid, get_config('adminpresets', 'lastpresetapplied'));
}
/**
* Test the behaviour of export_preset() method.
*
* @covers ::export_preset
* @dataProvider export_preset_provider
*
* @param bool $includesensible Whether the sensible settings should be exported too or not.
* @param string $presetname Preset name.
*/
public function test_export_preset(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');
// Prepare the data to export preset.
$data = [
'name' => $presetname,
'comments' => ['text' => 'This is a presets for testing export'],
'author' => 'Super-Girl',
'includesensiblesettings' => $includesensible,
];
// Call the method to be tested.
$manager = new manager();
list($presetid, $settingsfound, $pluginsfound) = $manager->export_preset((object) $data);
// Check the preset record has been created.
$presets = $DB->get_records('adminpresets');
$this->assertCount($currentpresets + 1, $presets);
$this->assertArrayHasKey($presetid, $presets);
$preset = $presets[$presetid];
$this->assertEquals($presetname, $preset->name);
$this->assertEquals(manager::NONCORE_PRESET, $preset->iscore);
// Check the preset includes settings and plugins.
$this->assertTrue($settingsfound);
$this->assertTrue($pluginsfound);
// 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);
}
}
/**
* Data provider for test_export_preset().
*
* @return array
*/
public function export_preset_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',
],
];
}
/**
* Test the behaviour of download_preset() method, when the given presetid doesn't exist.
*
* @covers ::download_preset
*/
public function test_download_unexisting_preset(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create some presets.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$presetid = $generator->create_preset();
// Unexisting preset identifier.
$unexistingid = $presetid * 2;
$manager = new manager();
$this->expectException(\moodle_exception::class);
$manager->download_preset($unexistingid);
}
/**
* Test the behaviour of import_preset() method.
*
* @dataProvider import_preset_provider
* @covers ::import_preset
*
* @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_preset(string $filecontents, bool $expectedpreset, bool $expectedsettings = false,
bool $expectedplugins = false, bool $expecteddebugging = false, string $expectedexception = null,
string $expectedpresetname = 'Imported preset'): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$currentpresets = $DB->count_records('adminpresets');
$currentitems = $DB->count_records('adminpresets_it');
$currentadvitems = $DB->count_records('adminpresets_it_a');
// Call the method to be tested.
$manager = new manager();
try {
list($xml, $preset, $settingsfound, $pluginsfound) = $manager->import_preset($filecontents);
} catch (\exception $e) {
if ($expectedexception) {
$this->assertInstanceOf($expectedexception, $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');
$this->assertArrayHasKey($preset->id, $presets);
$preset = $presets[$preset->id];
$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' => $preset->id]);
$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' => $preset->id]);
$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'));
}
}
}
/**
* Data provider for test_import_preset().
*
* @return array
*/
public function import_preset_provider(): array {
return [
'Import settings from an empty file' => [
'filecontents' => '',
'expectedpreset' => false,
],
'Import settings and plugins from a valid XML file' => [
'filecontents' => file_get_contents(__DIR__ . '/fixtures/import_settings_plugins.xml'),
'expectedpreset' => true,
'expectedsettings' => true,
'expectedplugins' => true,
],
'Import only settings from a valid XML file' => [
'filecontents' => file_get_contents(__DIR__ . '/fixtures/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(__DIR__ . '/fixtures/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(__DIR__ . '/fixtures/invalid_xml_file.xml'),
'expectedpreset' => false,
'expectedsettings' => false,
'expectedplugins' => false,
'expecteddebugging' => false,
'expectedexception' => \Exception::class,
],
'Import unexisting settings category' => [
'filecontents' => file_get_contents(__DIR__ . '/fixtures/unexisting_category.xml'),
'expectedpreset' => false,
'expectedsettings' => false,
'expectedplugins' => false,
],
'Import unexisting setting' => [
'filecontents' => file_get_contents(__DIR__ . '/fixtures/unexisting_setting.xml'),
'expectedpreset' => false,
'expectedsettings' => false,
'expectedplugins' => false,
'expecteddebugging' => true,
],
'Import valid settings with one unexisting setting too' => [
'filecontents' => file_get_contents(__DIR__ . '/fixtures/import_settings_with_unexisting_setting.xml'),
'expectedpreset' => true,
'expectedsettings' => false,
'expectedplugins' => false,
'expecteddebugging' => true,
],
];
}
/**
* Test the behaviour of delete_preset() method when the preset id doesn't exist.
*
* @covers ::delete_preset
*/
public function test_delete_preset_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']);
// Unexisting preset identifier.
$unexistingid = $presetid * 2;
$manager = new manager();
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Error deleting from database');
$manager->delete_preset($unexistingid);
}
/**
* Test trying to delete the core/pre-defined presets
*
* @covers ::delete_preset
*/
public function test_delete_preset_core(): void {
global $DB;
$this->resetAfterTest();
$starterpreset = $DB->get_record('adminpresets', ['iscore' => manager::STARTER_PRESET]);
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Error deleting from database');
(new manager())->delete_preset($starterpreset->id);
}
/**
* Test the behaviour of delete_preset() method.
*
* @covers ::delete_preset
*/
public function test_delete_preset(): 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'));
// Call the method to be tested.
$manager = new manager();
$manager->delete_preset($presetid1);
// 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'));
}
/**
* Test the behaviour of revert_preset() method when the preset applied id doesn't exist.
*
* @covers ::revert_preset
*/
public function test_revert_preset_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]);
// Unexisting applied preset identifier.
$unexistingid = $presetappid * 2;
$manager = new manager();
$this->expectException(\moodle_exception::class);
$manager->revert_preset($unexistingid);
}
/**
* Test the behaviour of revert_preset() method.
*
* @covers ::revert_preset
*/
public function test_revert_preset(): 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);
// Call the method to be tested.
$manager = new manager();
list($presetapp, $rollback, $failures) = $manager->revert_preset($presetappid);
// Check the preset applied has been reverted (so the records in _appXX tables have been removed).
$this->assertNotEmpty($presetapp);
$this->assertNotEmpty($rollback);
$this->assertEmpty($failures);
$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);
}
}
@@ -0,0 +1,170 @@
<?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 core_adminpresets\privacy;
use context_system;
use context_user;
use core_privacy\local\metadata\collection;
use core_privacy\tests\provider_testcase;
/**
* Tests for the privacy provider class.
*
* @package core_adminpresets
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_adminpresets\privacy\provider
*/
class privacy_provider_test extends provider_testcase {
/**
* Test for provider::get_metadata().
* @covers ::get_metadata
*/
public function test_get_metadata(): void {
$collection = new collection('core_adminpresets');
$newcollection = provider::get_metadata($collection);
$itemcollection = $newcollection->get_collection();
$this->assertCount(2, $itemcollection);
// The expected metadata fields are covered by test_metadata_provider() in privacy/tests/provider_test.php.
}
/**
* Test for provider::get_contexts_for_userid() doesn't return any context.
* @covers ::get_contexts_for_userid
*/
public function test_get_contexts_for_userid(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$generator->create_preset();
$contextlist = provider::get_contexts_for_userid($USER->id);
$this->assertEmpty($contextlist);
}
/**
* Test for provider::get_users_in_context() doesn't return any user.
* @covers ::get_users_in_context
*/
public function test_get_users_in_context(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$generator->create_preset();
$usercontext = context_user::instance($USER->id);
$userlist = new \core_privacy\local\request\userlist($usercontext, 'core_adminpresets');
\core_message\privacy\provider::get_users_in_context($userlist);
$this->assertEmpty($userlist->get_userids());
}
/**
* Test for provider::export_user_data().
* @covers ::export_user_data
*/
public function test_export_user_data(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$generator->create_preset();
// Check data is not exported in user context.
$usercontext = context_user::instance($USER->id);
$this->export_context_data_for_user($USER->id, $usercontext, 'core_adminpresets');
$writer = \core_privacy\local\request\writer::with_context($usercontext);
$this->assertEmpty($writer->get_data([get_string('siteadminpresetspluginname', 'core_adminpresets')]));
$this->assertEmpty($writer->get_all_metadata([]));
$this->assertEmpty($writer->get_files([]));
// Check data is not exported in system context either.
$systemcontext = context_system::instance();
$this->export_context_data_for_user($USER->id, $systemcontext, 'core_adminpresets');
$writer = \core_privacy\local\request\writer::with_context($systemcontext);
$this->assertEmpty($writer->get_data([get_string('siteadminpresetspluginname', 'core_adminpresets')]));
$this->assertEmpty($writer->get_all_metadata([]));
$this->assertEmpty($writer->get_files([]));
}
/**
* Test for provider::delete_data_for_all_users_in_context().
* @covers ::delete_data_for_all_users_in_context
*/
public function test_delete_data_for_all_users_in_context(): void {
global $DB, $USER;
$this->resetAfterTest();
$this->setAdminUser();
$currentpresets = $DB->count_records('adminpresets');
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$generator->create_preset();
$this->assertEquals($currentpresets + 1, $DB->count_records('adminpresets'));
$usercontext = context_user::instance($USER->id);
provider::delete_data_for_all_users_in_context($usercontext);
// Confirm the presets haven't been removed.
$this->assertEquals($currentpresets + 1, $DB->count_records('adminpresets'));
}
/**
* Test for provider::delete_data_for_user().
* @covers ::delete_data_for_user
*/
public function test_delete_data_for_user(): void {
global $DB, $USER;
$this->resetAfterTest();
$this->setAdminUser();
$currentpresets = $DB->count_records('adminpresets');
// Create a preset.
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$generator->create_preset();
$this->assertEquals($currentpresets + 1, $DB->count_records('adminpresets'));
$usercontext = context_user::instance($USER->id);
$contextlist = new \core_privacy\local\request\approved_contextlist($USER, 'core_adminpresets', [$usercontext->id]);
provider::delete_data_for_user($contextlist);
// Confirm the presets haven't been removed.
$this->assertEquals($currentpresets + 1, $DB->count_records('adminpresets'));
}
}