See Release Notes
Long Term Support Release
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace tool_admin_presets\local\action; 18 19 /** 20 * Tests for the rollback class. 21 * 22 * @package tool_admin_presets 23 * @category test 24 * @copyright 2021 Sara Arjona (sara@moodle.com) 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 * @coversDefaultClass \tool_admin_presets\local\action\rollback 27 */ 28 class rollback_test extends \advanced_testcase { 29 30 /** 31 * Test the behaviour of execute() method. 32 * 33 * @covers ::execute 34 */ 35 public function test_rollback_execute(): void { 36 global $DB; 37 38 $this->resetAfterTest(); 39 $this->setAdminUser(); 40 41 // Set the config values (to confirm they change after applying the preset). 42 set_config('enablebadges', 1); 43 set_config('allowemojipicker', 1); 44 set_config('mediawidth', '640', 'mod_lesson'); 45 set_config('maxanswers', '5', 'mod_lesson'); 46 set_config('maxanswers_adv', '1', 'mod_lesson'); 47 set_config('enablecompletion', 1); 48 set_config('usecomments', 0); 49 50 // Create a preset and apply it. 51 $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets'); 52 $presetid = $generator->create_preset(['applypreset' => true]); 53 $presetappid = $DB->get_field('adminpresets_app', 'id', ['adminpresetid' => $presetid]); 54 55 $currentpresets = $DB->count_records('adminpresets'); 56 $currentitems = $DB->count_records('adminpresets_it'); 57 $currentadvitems = $DB->count_records('adminpresets_it_a'); 58 $currentplugins = $DB->count_records('adminpresets_plug'); 59 $this->assertCount(1, $DB->get_records('adminpresets_app')); 60 $this->assertCount(3, $DB->get_records('adminpresets_app_it')); 61 $this->assertCount(1, $DB->get_records('adminpresets_app_it_a')); 62 $this->assertCount(2, $DB->get_records('adminpresets_app_plug')); 63 64 // Check the setttings have changed accordingly after applying the preset. 65 $this->assertEquals(0, get_config('core', 'enablebadges')); 66 $this->assertEquals(900, get_config('mod_lesson', 'mediawidth')); 67 $this->assertEquals(2, get_config('mod_lesson', 'maxanswers')); 68 $this->assertEquals(1, get_config('core', 'allowemojipicker')); 69 $this->assertEquals(1, get_config('core', 'enablecompletion')); 70 $this->assertEquals(0, get_config('core', 'usecomments')); 71 72 // Check the plugins visibility have changed accordingly with the ones defined in the preset. 73 $enabledplugins = \core\plugininfo\enrol::get_enabled_plugins(); 74 $this->assertArrayNotHasKey('guest', $enabledplugins); 75 $enabledplugins = \core\plugininfo\mod::get_enabled_plugins(); 76 $this->assertArrayNotHasKey('glossary', $enabledplugins); 77 $enabledplugins = \core\plugininfo\qtype::get_enabled_plugins(); 78 $this->assertArrayHasKey('truefalse', $enabledplugins); 79 80 // Initialise the parameters. 81 $_POST['action'] = 'rollback'; 82 $_POST['mode'] = 'execute'; 83 $_POST['id'] = $presetappid; 84 $_POST['sesskey'] = sesskey(); 85 86 // Create the rollback class and execute it. 87 $action = new rollback(); 88 $action->execute(); 89 90 // Check the preset applied has been reverted (so the records in _appXX tables have been removed). 91 $this->assertCount(0, $DB->get_records('adminpresets_app')); 92 $this->assertCount(0, $DB->get_records('adminpresets_app_it')); 93 $this->assertCount(0, $DB->get_records('adminpresets_app_it_a')); 94 $this->assertCount(0, $DB->get_records('adminpresets_app_plug')); 95 // Check the preset data hasn't changed. 96 $this->assertCount($currentpresets, $DB->get_records('adminpresets')); 97 $this->assertCount($currentitems, $DB->get_records('adminpresets_it')); 98 $this->assertCount($currentadvitems, $DB->get_records('adminpresets_it_a')); 99 $this->assertCount($currentplugins, $DB->get_records('adminpresets_plug')); 100 101 // Check the setting values have been reverted accordingly. 102 $this->assertEquals(1, get_config('core', 'enablebadges')); 103 $this->assertEquals(640, get_config('mod_lesson', 'mediawidth')); 104 $this->assertEquals(5, get_config('mod_lesson', 'maxanswers')); 105 $this->assertEquals(1, get_config('mod_lesson', 'maxanswers_adv')); 106 // These settings won't change, regardless if they are posted to the form. 107 $this->assertEquals(1, get_config('core', 'allowemojipicker')); 108 $this->assertEquals(1, get_config('core', 'enablecompletion')); 109 $this->assertEquals(0, get_config('core', 'usecomments')); 110 111 // Check the plugins visibility have been reverted accordingly. 112 $enabledplugins = \core\plugininfo\enrol::get_enabled_plugins(); 113 $this->assertArrayHasKey('guest', $enabledplugins); 114 $enabledplugins = \core\plugininfo\mod::get_enabled_plugins(); 115 $this->assertArrayHasKey('glossary', $enabledplugins); 116 // This plugin won't change (because it had the same value than before the preset was applied). 117 $enabledplugins = \core\plugininfo\qtype::get_enabled_plugins(); 118 $this->assertArrayHasKey('truefalse', $enabledplugins); 119 } 120 121 /** 122 * Test the behaviour of execute() method when the preset applied id doesn't exist. 123 * 124 * @covers ::execute 125 */ 126 public function test_rollback_execute_unexisting_presetapp(): void { 127 global $DB; 128 129 $this->resetAfterTest(); 130 $this->setAdminUser(); 131 132 // Create a preset and apply it. 133 $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets'); 134 $presetid = $generator->create_preset(['applypreset' => true]); 135 $presetappid = $DB->get_field('adminpresets_app', 'id', ['adminpresetid' => $presetid]); 136 137 // Initialise the parameters. 138 $_POST['action'] = 'rollback'; 139 $_POST['mode'] = 'execute'; 140 $_POST['id'] = $presetappid * 2; // Unexisting presetapp identifier. 141 $_POST['sesskey'] = sesskey(); 142 143 // Create the rollback class and execute it. 144 $action = new rollback(); 145 $this->expectException(\moodle_exception::class); 146 $action->execute(); 147 } 148 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body