diff --git a/admin/tool/admin_presets/classes/local/action/load.php b/admin/tool/admin_presets/classes/local/action/load.php index 76f41a52e69..6bdfa948ee8 100644 --- a/admin/tool/admin_presets/classes/local/action/load.php +++ b/admin/tool/admin_presets/classes/local/action/load.php @@ -94,6 +94,24 @@ class load extends base { * 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(); @@ -101,7 +119,12 @@ class load extends base { // Preset data. if (!$preset = $DB->get_record('adminpresets', ['id' => $data->id])) { - throw new moodle_exception('errornopreset', 'core_adminpresets'); + if ($raiseexception) { + throw new moodle_exception('errornopreset', 'core_adminpresets'); + } else { + $this->outputs = get_string('errornopreset', 'core_adminpresets'); + return; + } } // Print preset basic data. @@ -125,22 +148,23 @@ class load extends base { $applieddata->settings = $applied; $applieddata->beforeapplying = true; $application->appliedchanges = $applieddata; - if (empty($applied)) { - // Display a warning when no settings will be applied. - $applieddata->message = get_string('nosettingswillbeapplied', 'tool_admin_presets'); + 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); + // 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 { diff --git a/admin/tool/admin_presets/tests/local/action/load_test.php b/admin/tool/admin_presets/tests/local/action/load_test.php index ac081e54f41..459203899ed 100644 --- a/admin/tool/admin_presets/tests/local/action/load_test.php +++ b/admin/tool/admin_presets/tests/local/action/load_test.php @@ -51,6 +51,33 @@ class load_test extends \advanced_testcase { $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. * diff --git a/lang/en/adminpresets.php b/lang/en/adminpresets.php index 6bfd529f01a..2b3af85c57a 100644 --- a/lang/en/adminpresets.php +++ b/lang/en/adminpresets.php @@ -27,7 +27,7 @@ $string['disabledwithvalue'] = 'Disabled ({$a})'; $string['enabled'] = 'Enabled'; $string['errordeleting'] = 'Error deleting from database.'; $string['errorinserting'] = 'Error inserting into database.'; -$string['errornopreset'] = 'It doesn\'t exists a preset with that name.'; +$string['errornopreset'] = 'It doesn\'t exist a preset with that name.'; $string['fullpreset'] = 'Full'; $string['fullpresetdescription'] = 'All the Starter features plus External (LTI) tool, SCORM, Workshop, Analytics, Badges, Competencies, Learning plans and lots more.'; $string['markedasadvanced'] = 'marked as advanced';