MDL-73953 core_adminpresets: Marking core presets on DB

This commit is contained in:
Amaia Anabitarte 2022-03-01 17:43:07 +01:00
parent 77ca86bf87
commit c0196fb096
12 changed files with 159 additions and 14 deletions

View file

@ -32,7 +32,7 @@ class helper {
* - name. To define the preset name.
* - comments. To change the comments field.
* - author. To update the author field.
* - iscore. Whether the preset is a core preset or not.
* - iscore. Whether the preset is a core preset or not. Valid values on \core_adminpresets\manager class.
* @return int The identifier of the preset created.
*/
public static function create_preset(array $data): int {
@ -41,7 +41,13 @@ class helper {
$name = array_key_exists('name', $data) ? $data['name'] : '';
$comments = array_key_exists('comments', $data) ? $data['comments'] : '';
$author = array_key_exists('author', $data) ? $data['author'] : fullname($USER);
$iscore = array_key_exists('iscore', $data) ? $data['iscore'] : 0;
$iscore = array_key_exists('iscore', $data) ? $data['iscore'] : manager::NONCORE_PRESET;
// Validate iscore value.
$allowed = [manager::NONCORE_PRESET, manager::STARTER_PRESET, manager::FULL_PRESET];
if (!in_array($iscore, $allowed)) {
$iscore = manager::NONCORE_PRESET;
}
$preset = [
'userid' => $USER->id,
@ -168,7 +174,7 @@ class helper {
$data = [
'name' => get_string('starterpreset', 'core_adminpresets'),
'comments' => get_string('starterpresetdescription', 'core_adminpresets'),
'iscore' => 1,
'iscore' => manager::STARTER_PRESET,
];
$presetid = static::create_preset($data);
@ -279,7 +285,7 @@ class helper {
$data = [
'name' => get_string('fullpreset', 'core_adminpresets'),
'comments' => get_string('fullpresetdescription', 'core_adminpresets'),
'iscore' => 1,
'iscore' => manager::FULL_PRESET,
];
$presetid = static::create_preset($data);

View file

@ -124,6 +124,15 @@ class manager {
'moodlerelease' => 'MOODLE_RELEASE'
];
/** @var int Non-core preset */
public const NONCORE_PRESET = 0;
/** @var int Starter preset */
public const STARTER_PRESET = 1;
/** @var int Full preset */
public const FULL_PRESET = 2;
/**
* Gets the system settings
*

View file

@ -64,6 +64,14 @@ class core_adminpresets_generator extends \component_generator_base {
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,
@ -75,6 +83,7 @@ class core_adminpresets_generator extends \component_generator_base {
'moodlerelease' => $CFG->release,
'timecreated' => time(),
'timeimported' => 0,
'iscore' => $data['iscore'],
];
$presetid = $DB->insert_record('adminpresets', $preset);

View file

@ -37,9 +37,11 @@ class generator_test extends \advanced_testcase {
* @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): void {
bool $applypreset = false, ?int $iscore = null, ?int $iscoreresult = null): void {
global $CFG, $DB;
$this->resetAfterTest();
@ -66,6 +68,12 @@ class generator_test extends \advanced_testcase {
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);
@ -76,6 +84,7 @@ class generator_test extends \advanced_testcase {
$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);
@ -197,7 +206,31 @@ class generator_test extends \advanced_testcase {
'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,
],
];
}
}

View file

@ -35,8 +35,11 @@ class helper_test extends \advanced_testcase {
*
* @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): void {
public function test_create_preset(?string $name = null, ?string $comments = null, ?int $iscore = null,
?int $iscoreresult = null): void {
global $CFG, $DB, $USER;
$this->resetAfterTest();
@ -48,6 +51,12 @@ class helper_test extends \advanced_testcase {
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);
@ -58,6 +67,7 @@ class helper_test extends \advanced_testcase {
$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);
@ -89,6 +99,24 @@ class helper_test extends \advanced_testcase {
'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,
],
];
}

View file

@ -293,7 +293,7 @@ class manager_test extends \advanced_testcase {
$this->assertArrayHasKey($presetid, $presets);
$preset = $presets[$presetid];
$this->assertEquals($presetname, $preset->name);
$this->assertEquals(0, $preset->iscore);
$this->assertEquals(manager::NONCORE_PRESET, $preset->iscore);
// Check the preset includes settings and plugins.
$this->assertTrue($settingsfound);
@ -448,7 +448,7 @@ class manager_test extends \advanced_testcase {
$this->assertEquals($expectedpresetname, $preset->name);
$this->assertEquals('http://demo.moodle', $preset->site);
$this->assertEquals('Ada Lovelace', $preset->author);
$this->assertEquals(0, $preset->iscore);
$this->assertEquals(manager::NONCORE_PRESET, $preset->iscore);
if ($expectedsettings) {
// Check the items have been created.