Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
use core_adminpresets\manager;
20
 
21
/**
22
 * Tests for the import class.
23
 *
24
 * @package    tool_admin_presets
25
 * @category   test
26
 * @copyright  2021 Sara Arjona (sara@moodle.com)
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @coversDefaultClass \tool_admin_presets\local\action\import
29
 */
1441 ariadna 30
final class import_test extends \advanced_testcase {
1 efrain 31
 
32
    /**
33
     * Test the behaviour of execute() method.
34
     *
35
     * @dataProvider import_execute_provider
36
     * @covers ::execute
37
     *
38
     * @param string $filecontents File content to import.
39
     * @param bool $expectedpreset Whether the preset should be created or not.
40
     * @param bool $expectedsettings Whether settings will be created or not.
41
     * @param bool $expectedplugins Whether plugins will be created or not.
42
     * @param bool $expecteddebugging Whether debugging message will be thrown or not.
43
     * @param string|null $expectedexception Expected exception class (if that's the case).
44
     * @param string|null $expectedpresetname Expected preset name.
45
     */
46
    public function test_import_execute(string $filecontents, bool $expectedpreset, bool $expectedsettings = false,
1441 ariadna 47
            bool $expectedplugins = false, bool $expecteddebugging = false, ?string $expectedexception = null,
1 efrain 48
            string $expectedpresetname = 'Imported preset'): void {
49
        global $DB, $USER;
50
 
51
        $this->resetAfterTest();
52
        $this->setAdminUser();
53
 
54
        $currentpresets = $DB->count_records('adminpresets');
55
        $currentitems = $DB->count_records('adminpresets_it');
56
        $currentadvitems = $DB->count_records('adminpresets_it_a');
57
 
58
        // Create draft file to import.
59
        $draftid = file_get_unused_draft_itemid();
60
        $filerecord = [
61
            'component' => 'user',
62
            'filearea' => 'draft',
63
            'contextid' => \context_user::instance($USER->id)->id, 'itemid' => $draftid,
64
            'filename' => 'export.xml', 'filepath' => '/'
65
        ];
66
        $fs = get_file_storage();
67
        $fs->create_file_from_string($filerecord, $filecontents);
68
        // Get the data we are submitting for the form and mock submitting it.
69
        $formdata = [
70
            'xmlfile' => $draftid,
71
            'name' => '',
72
            'admin_presets_submit' => 'Save changes',
73
            'sesskey' => sesskey(),
74
        ];
75
        \tool_admin_presets\form\import_form::mock_submit($formdata);
76
 
77
        // Initialise the parameters and create the import class.
78
        $_POST['action'] = 'import';
79
        $_POST['mode'] = 'execute';
80
 
81
        $action = new import();
82
        $sink = $this->redirectEvents();
83
        try {
1441 ariadna 84
            // Suppress warnings and load XML.
85
            $invokable = self::get_invokable();
86
            set_error_handler($invokable, E_WARNING);
1 efrain 87
            $action->execute();
88
        } catch (\exception $e) {
89
            // If import action was successfull, redirect should be called so we will encounter an
90
            // 'unsupported redirect error' moodle_exception.
91
            if ($expectedexception) {
92
                $this->assertInstanceOf($expectedexception, $e);
93
            } else {
94
                $this->assertInstanceOf(\moodle_exception::class, $e);
95
            }
1441 ariadna 96
            restore_error_handler();
1 efrain 97
        } finally {
98
            if ($expecteddebugging) {
99
                $this->assertDebuggingCalled();
100
            }
101
 
102
            if ($expectedpreset) {
103
                // Check the preset record has been created.
104
                $presets = $DB->get_records('adminpresets');
105
                $this->assertCount($currentpresets + 1, $presets);
106
                $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
107
                $presetid = $generator->access_protected($action, 'id');
108
                $this->assertArrayHasKey($presetid, $presets);
109
                $preset = $presets[$presetid];
110
                $this->assertEquals($expectedpresetname, $preset->name);
111
                $this->assertEquals('http://demo.moodle', $preset->site);
112
                $this->assertEquals('Ada Lovelace', $preset->author);
113
                $this->assertEquals(manager::NONCORE_PRESET, $preset->iscore);
114
 
115
                if ($expectedsettings) {
116
                    // Check the items have been created.
117
                    $items = $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid]);
118
                    $this->assertCount(4, $items);
119
                    $presetitems = [
120
                        'none' => [
121
                            'enablebadges' => 0,
122
                            'enableportfolios' => 1,
123
                            'allowemojipicker' => 1,
124
                        ],
125
                        'mod_lesson' => [
126
                            'mediawidth' => 900,
127
                            'maxanswers' => 2,
128
                        ],
129
                    ];
130
                    foreach ($items as $item) {
131
                        $this->assertArrayHasKey($item->name, $presetitems[$item->plugin]);
132
                        $this->assertEquals($presetitems[$item->plugin][$item->name], $item->value);
133
                    }
134
 
135
                    // Check the advanced attributes have been created.
136
                    $advitems = $DB->get_records('adminpresets_it_a');
137
                    $this->assertCount($currentadvitems + 1, $advitems);
138
                    $advitemfound = false;
139
                    foreach ($advitems as $advitem) {
140
                        if ($advitem->name == 'maxanswers_adv') {
141
                            $this->assertEmpty($advitem->value);
142
                            $advitemfound = true;
143
                        }
144
                    }
145
                    $this->assertTrue($advitemfound);
146
                }
147
 
148
                if ($expectedplugins) {
149
                    // Check the plugins have been created.
150
                    $plugins = $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid]);
151
                    $this->assertCount(6, $plugins);
152
                    $presetplugins = [
153
                        'atto' => [
154
                            'html' => 1,
155
                        ],
156
                        'block' => [
157
                            'html' => 0,
158
                            'activity_modules' => 1,
159
                        ],
160
                        'mod' => [
1441 ariadna 161
                            'page' => 0,
1 efrain 162
                            'data' => 0,
163
                            'lesson' => 1,
164
                        ],
165
                    ];
166
                    foreach ($plugins as $plugin) {
167
                        $this->assertArrayHasKey($plugin->name, $presetplugins[$plugin->plugin]);
168
                        $this->assertEquals($presetplugins[$plugin->plugin][$plugin->name], $plugin->enabled);
169
                    }
170
 
171
                }
172
            } else {
173
                // Check the preset nor the items are not created.
174
                $this->assertCount($currentpresets, $DB->get_records('adminpresets'));
175
                $this->assertCount($currentitems, $DB->get_records('adminpresets_it'));
176
                $this->assertCount($currentadvitems, $DB->get_records('adminpresets_it_a'));
177
            }
178
 
179
            // Check the export event has been raised.
180
            $events = $sink->get_events();
181
            $sink->close();
182
            $event = reset($events);
183
            if ($expectedpreset) {
184
                // If preset has been created, an event should be raised.
185
                $this->assertInstanceOf('\\tool_admin_presets\\event\\preset_imported', $event);
186
            } else {
187
                $this->assertFalse($event);
188
            }
189
        }
190
    }
191
 
192
    /**
193
     * Data provider for test_import_execute().
194
     *
195
     * @return array
196
     */
1441 ariadna 197
    public static function import_execute_provider(): array {
1 efrain 198
        $fixturesfolder = __DIR__ . '/../../../../../presets/tests/fixtures/';
199
 
200
        return [
201
            'Import settings from an empty file' => [
202
                'filecontents' => '',
203
                'expectedpreset' => false,
204
            ],
205
            'Import settings and plugins from a valid XML file' => [
206
                'filecontents' => file_get_contents($fixturesfolder . 'import_settings_plugins.xml'),
207
                'expectedpreset' => true,
208
                'expectedsettings' => true,
209
                'expectedplugins' => true,
210
            ],
211
            'Import only settings from a valid XML file' => [
212
                'filecontents' => file_get_contents($fixturesfolder . 'import_settings.xml'),
213
                'expectedpreset' => true,
214
                'expectedsettings' => true,
215
                'expectedplugins' => false,
216
            ],
217
            'Import settings and plugins from a valid XML file with Starter name, which will be marked as non-core' => [
218
                'filecontents' => file_get_contents($fixturesfolder . 'import_starter_name.xml'),
219
                'expectedpreset' => true,
220
                'expectedsettings' => true,
221
                'expectedplugins' => true,
222
                'expecteddebugging' => false,
223
                'expectedexception' => null,
224
                'expectedpresetname' => 'Starter',
225
            ],
226
            'Import settings from an invalid XML file' => [
227
                'filecontents' => file_get_contents($fixturesfolder . 'invalid_xml_file.xml'),
228
                'expectedpreset' => false,
229
                'expectedsettings' => false,
230
                'expectedplugins' => false,
231
                'expecteddebugging' => false,
232
                'expectedexception' => \Exception::class,
233
            ],
234
            'Import unexisting settings category' => [
235
                'filecontents' => file_get_contents($fixturesfolder . 'unexisting_category.xml'),
236
                'expectedpreset' => false,
237
                'expectedsettings' => false,
238
                'expectedplugins' => false,
239
            ],
240
            'Import unexisting setting' => [
241
                'filecontents' => file_get_contents($fixturesfolder . 'unexisting_setting.xml'),
242
                'expectedpreset' => false,
243
                'expectedsettings' => false,
244
                'expectedplugins' => false,
245
                'expecteddebugging' => true,
246
            ],
247
            'Import valid settings with one unexisting setting too' => [
248
                'filecontents' => file_get_contents($fixturesfolder . 'import_settings_with_unexisting_setting.xml'),
249
                'expectedpreset' => true,
250
                'expectedsettings' => false,
251
                'expectedplugins' => false,
252
                'expecteddebugging' => true,
253
            ],
254
        ];
255
    }
256
}