This commit is contained in:
Ilya Tregubov 2024-02-07 13:16:06 +08:00
commit 9ad941c565
3 changed files with 25 additions and 5 deletions

View File

@ -761,8 +761,8 @@ class manager {
public function delete_preset(int $presetid): void {
global $DB;
// Check the preset exists.
$preset = $DB->get_record('adminpresets', ['id' => $presetid]);
// Check the preset exists (cannot delete the pre-installed core "Starter" and "Full" presets).
$preset = $DB->get_record('adminpresets', ['id' => $presetid, 'iscore' => self::NONCORE_PRESET]);
if (!$preset) {
throw new moodle_exception('errordeleting', 'core_adminpresets');
}

View File

@ -16,6 +16,7 @@
namespace core_adminpresets;
use moodle_exception;
use stdClass;
/**
@ -599,10 +600,28 @@ class manager_test extends \advanced_testcase {
$manager = new manager();
$this->expectException(\moodle_exception::class);
$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.
*

View File

@ -16,6 +16,7 @@
namespace tool_admin_presets\local\action;
use core_adminpresets\manager;
use moodle_exception;
/**
@ -34,8 +35,8 @@ class delete extends base {
public function show(): void {
global $DB, $OUTPUT;
// Getting the preset name.
$presetdata = $DB->get_record('adminpresets', ['id' => $this->id], 'name');
// 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);