1 |
efrain |
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 delete 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\delete
|
|
|
27 |
*/
|
|
|
28 |
class delete_test extends \advanced_testcase {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Test the behaviour of execute() method.
|
|
|
32 |
*
|
|
|
33 |
* @covers ::execute
|
|
|
34 |
*/
|
|
|
35 |
public function test_delete_execute(): void {
|
|
|
36 |
global $DB;
|
|
|
37 |
|
|
|
38 |
$this->resetAfterTest();
|
|
|
39 |
$this->setAdminUser();
|
|
|
40 |
|
|
|
41 |
// Create some presets.
|
|
|
42 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
|
|
|
43 |
$presetid1 = $generator->create_preset(['name' => 'Preset 1', 'applypreset' => true]);
|
|
|
44 |
$presetid2 = $generator->create_preset(['name' => 'Preset 2']);
|
|
|
45 |
|
|
|
46 |
$currentpresets = $DB->count_records('adminpresets');
|
|
|
47 |
$currentitems = $DB->count_records('adminpresets_it');
|
|
|
48 |
$currentadvitems = $DB->count_records('adminpresets_it_a');
|
|
|
49 |
$currentplugins = $DB->count_records('adminpresets_plug');
|
|
|
50 |
|
|
|
51 |
// Only preset1 has been applied.
|
|
|
52 |
$this->assertCount(1, $DB->get_records('adminpresets_app'));
|
|
|
53 |
// Only the preset1 settings that have changed: enablebadges, mediawidth and maxanswers.
|
|
|
54 |
$this->assertCount(3, $DB->get_records('adminpresets_app_it'));
|
|
|
55 |
// Only the preset1 advanced settings that have changed: maxanswers_adv.
|
|
|
56 |
$this->assertCount(1, $DB->get_records('adminpresets_app_it_a'));
|
|
|
57 |
// Only the preset1 plugins that have changed: enrol_guest and mod_glossary.
|
|
|
58 |
$this->assertCount(2, $DB->get_records('adminpresets_app_plug'));
|
|
|
59 |
|
|
|
60 |
// Initialise the parameters and create the delete class.
|
|
|
61 |
$_POST['action'] = 'delete';
|
|
|
62 |
$_POST['mode'] = 'execute';
|
|
|
63 |
$_POST['id'] = $presetid1;
|
|
|
64 |
$_POST['sesskey'] = sesskey();
|
|
|
65 |
|
|
|
66 |
$action = new delete();
|
|
|
67 |
$sink = $this->redirectEvents();
|
|
|
68 |
try {
|
|
|
69 |
$action->execute();
|
|
|
70 |
} catch (\exception $e) {
|
|
|
71 |
// If delete action was successfull, redirect should be called so we will encounter an
|
|
|
72 |
// 'unsupported redirect error' moodle_exception.
|
|
|
73 |
$this->assertInstanceOf(\moodle_exception::class, $e);
|
|
|
74 |
} finally {
|
|
|
75 |
// Check the preset data has been removed.
|
|
|
76 |
$presets = $DB->get_records('adminpresets');
|
|
|
77 |
$this->assertCount($currentpresets - 1, $presets);
|
|
|
78 |
$preset = reset($presets);
|
|
|
79 |
$this->assertArrayHasKey($presetid2, $presets);
|
|
|
80 |
// Check preset items.
|
|
|
81 |
$this->assertCount($currentitems - 4, $DB->get_records('adminpresets_it'));
|
|
|
82 |
$this->assertCount(0, $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid1]));
|
|
|
83 |
// Check preset advanced items.
|
|
|
84 |
$this->assertCount($currentadvitems - 1, $DB->get_records('adminpresets_it_a'));
|
|
|
85 |
// Check preset plugins.
|
|
|
86 |
$this->assertCount($currentplugins - 3, $DB->get_records('adminpresets_plug'));
|
|
|
87 |
$this->assertCount(0, $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid1]));
|
|
|
88 |
// Check preset applied tables are empty now.
|
|
|
89 |
$this->assertCount(0, $DB->get_records('adminpresets_app'));
|
|
|
90 |
$this->assertCount(0, $DB->get_records('adminpresets_app_it'));
|
|
|
91 |
$this->assertCount(0, $DB->get_records('adminpresets_app_it_a'));
|
|
|
92 |
$this->assertCount(0, $DB->get_records('adminpresets_app_plug'));
|
|
|
93 |
|
|
|
94 |
// Check the delete event has been raised.
|
|
|
95 |
$events = $sink->get_events();
|
|
|
96 |
$sink->close();
|
|
|
97 |
$event = reset($events);
|
|
|
98 |
$this->assertInstanceOf('\\tool_admin_presets\\event\\preset_deleted', $event);
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Test the behaviour of execute() method when the preset id doesn't exist.
|
|
|
104 |
*
|
|
|
105 |
* @covers ::execute
|
|
|
106 |
*/
|
|
|
107 |
public function test_delete_execute_unexisting_preset(): void {
|
|
|
108 |
|
|
|
109 |
$this->resetAfterTest();
|
|
|
110 |
$this->setAdminUser();
|
|
|
111 |
|
|
|
112 |
// Create some presets.
|
|
|
113 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
|
|
|
114 |
$presetid = $generator->create_preset(['name' => 'Preset 1']);
|
|
|
115 |
|
|
|
116 |
// Initialise the parameters and create the delete class.
|
|
|
117 |
$_POST['action'] = 'delete';
|
|
|
118 |
$_POST['mode'] = 'execute';
|
|
|
119 |
$_POST['id'] = $presetid * 2; // Unexisting preset identifier.
|
|
|
120 |
$_POST['sesskey'] = sesskey();
|
|
|
121 |
|
|
|
122 |
$action = new delete();
|
|
|
123 |
$this->expectException(\moodle_exception::class);
|
|
|
124 |
$action->execute();
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Test the behaviour of show() method when the preset id doesn't exist.
|
|
|
129 |
*
|
|
|
130 |
* @covers ::show
|
|
|
131 |
*/
|
|
|
132 |
public function test_delete_show_unexisting_preset(): void {
|
|
|
133 |
|
|
|
134 |
$this->resetAfterTest();
|
|
|
135 |
$this->setAdminUser();
|
|
|
136 |
|
|
|
137 |
// Create some presets.
|
|
|
138 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
|
|
|
139 |
$presetid = $generator->create_preset(['name' => 'Preset 1']);
|
|
|
140 |
|
|
|
141 |
// Initialise the parameters and create the delete class.
|
|
|
142 |
$_POST['action'] = 'delete';
|
|
|
143 |
$_POST['mode'] = 'show';
|
|
|
144 |
$_POST['id'] = $presetid * 2; // Unexisting preset identifier.
|
|
|
145 |
|
|
|
146 |
$action = new delete();
|
|
|
147 |
$this->expectException(\moodle_exception::class);
|
|
|
148 |
$action->show();
|
|
|
149 |
}
|
|
|
150 |
}
|