Proyectos de Subversion Moodle

Rev

Rev 11 | | 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 mod_data;
18
 
19
use mod_data\local\importer\preset_existing_importer;
20
use mod_data\local\importer\preset_importer;
21
 
22
/**
23
 * Preset importer tests class for mod_data.
24
 *
25
 * @package    mod_data
26
 * @category   test
27
 * @copyright  2022 Amaia Anabitarte <amaia@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @coversDefaultClass \mod_data\local\importer\preset_importer
30
 */
1441 ariadna 31
final class preset_importer_test extends \advanced_testcase {
1 efrain 32
    /**
33
     * Data provider for build providers for test_needs_mapping and test_set_affected_fields.
34
     *
35
     * @return array[]
36
     */
1441 ariadna 37
    public static function preset_importer_provider(): array {
1 efrain 38
        // Image gallery preset is: ['title' => 'text', 'description' => 'textarea', 'image' => 'picture'];
39
 
40
        $titlefield = new \stdClass();
41
        $titlefield->name = 'title';
42
        $titlefield->type = 'text';
43
 
44
        $descfield = new \stdClass();
45
        $descfield->name = 'description';
46
        $descfield->type = 'textarea';
47
 
48
        $imagefield = new \stdClass();
49
        $imagefield->name = 'image';
50
        $imagefield->type = 'picture';
51
 
52
        $difffield = new \stdClass();
53
        $difffield->name = 'title';
54
        $difffield->type = 'textarea';
55
 
56
        $newfield = new \stdClass();
57
        $newfield->name = 'number';
58
        $newfield->type = 'number';
59
 
60
        return [
61
            'Empty database / Empty importer' => [
62
                'currentfields' => [],
63
                'newfields' => [],
64
                'pluginname' => '',
65
            ],
66
            'Empty database / Importer with fields' => [
67
                'currentfields' => [],
68
                'newfields' => [$titlefield, $descfield, $imagefield],
69
                'pluginname' => 'imagegallery',
70
            ],
71
            'Database with fields / Empty importer' => [
72
                'currentfields' => [$titlefield, $descfield, $imagefield],
73
                'newfields' => [],
74
                'pluginname' => '',
75
            ],
76
            'Same fields' => [
77
                'currentfields' => [$titlefield, $descfield, $imagefield],
78
                'newfields' => [$titlefield, $descfield, $imagefield],
79
                'pluginname' => 'imagegallery',
80
            ],
81
            'Fields to create' => [
82
                'currentfields' => [$titlefield, $descfield],
83
                'newfields' => [$titlefield, $descfield, $imagefield],
84
                'pluginname' => 'imagegallery',
85
            ],
86
            'Fields to remove' => [
87
                'currentfields' => [$titlefield, $descfield, $imagefield, $difffield],
88
                'newfields' => [$titlefield, $descfield, $imagefield],
89
                'pluginname' => 'imagegallery',
90
            ],
91
            'Fields to update' => [
92
                'currentfields' => [$difffield, $descfield, $imagefield],
93
                'newfields' => [$titlefield, $descfield, $imagefield],
94
                'pluginname' => 'imagegallery',
95
            ],
96
            'Fields to create, remove and update' => [
97
                'currentfields' => [$titlefield, $descfield, $imagefield, $difffield],
98
                'newfields' => [$titlefield, $descfield, $newfield],
99
                'pluginname' => '',
100
            ],
101
        ];
102
    }
103
 
104
    /**
105
     * Data provider for needs_mapping().
106
     *
107
     * @return array[]
108
     */
1441 ariadna 109
    public static function needs_mapping_provider(): array {
110
        $basedprovider = static::preset_importer_provider();
1 efrain 111
 
1441 ariadna 112
        $basedprovider['Empty database / Empty importer']['expectedresult'] = false;
113
        $basedprovider['Empty database / Importer with fields']['expectedresult'] = false;
114
        $basedprovider['Database with fields / Empty importer']['expectedresult'] = true;
115
        $basedprovider['Same fields']['expectedresult'] = false;
116
        $basedprovider['Fields to create']['expectedresult'] = true;
117
        $basedprovider['Fields to remove']['expectedresult'] = true;
118
        $basedprovider['Fields to update']['expectedresult'] = true;
119
        $basedprovider['Fields to create, remove and update']['expectedresult'] = true;
1 efrain 120
 
121
        return $basedprovider;
122
    }
123
 
124
    /**
125
     * Test for needs_mapping method.
126
     *
127
     * @dataProvider needs_mapping_provider
128
     * @covers ::needs_mapping
129
     *
130
     * @param array $currentfields Fields of the current activity.
131
     * @param array $newfields Fields to be imported.
132
     * @param string $pluginname The plugin preset to be imported.
133
     * @param bool $expectedresult Expected exception.
134
     */
135
    public function test_needs_mapping(
136
        array $currentfields,
137
        array $newfields,
138
        string $pluginname,
139
        bool $expectedresult
11 efrain 140
    ): void {
1 efrain 141
 
142
        global $USER;
143
 
144
        $this->resetAfterTest();
145
        $this->setAdminUser();
146
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
147
 
148
        // Create a course and a database activity.
149
        $course = $this->getDataGenerator()->create_course();
150
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
151
        // Add current fields to the activity.
152
        foreach ($currentfields as $field) {
153
            $plugingenerator->create_field($field, $activity);
154
        }
155
        $manager = manager::create_from_instance($activity);
156
 
157
        $presetactivity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
158
        // Add current fields to the activity.
159
        foreach ($newfields as $field) {
160
            $plugingenerator->create_field($field, $presetactivity);
161
        }
162
 
163
        $record = (object) [
164
            'name' => 'Testing preset name',
165
            'description' => 'Testing preset description',
166
        ];
167
        $saved = $plugingenerator->create_preset($presetactivity, $record);
168
        $savedimporter = new preset_existing_importer($manager, $USER->id . '/Testing preset name');
169
        $this->assertEquals($savedimporter->needs_mapping(), $expectedresult);
170
 
171
        // Create presets and importers.
172
        if ($pluginname) {
173
            $plugin = preset::create_from_plugin(null, $pluginname);
174
            $pluginimporter = new preset_existing_importer($manager, '/' . $pluginname);
175
            $this->assertEquals($pluginimporter->needs_mapping(), $expectedresult);
176
        }
177
    }
178
 
179
    /**
180
     * Data provider for test_set_affected_fields().
181
     *
182
     * @return array[]
183
     */
1441 ariadna 184
    public static function set_affected_provider(): array {
185
        $basedprovider = static::preset_importer_provider();
1 efrain 186
 
187
        $basedprovider['Empty database / Empty importer']['fieldstocreate'] = 0;
188
        $basedprovider['Empty database / Empty importer']['fieldstoremove'] = 0;
189
        $basedprovider['Empty database / Empty importer']['fieldstoupdate'] = 0;
190
 
191
        $basedprovider['Empty database / Importer with fields']['fieldstocreate'] = 3;
192
        $basedprovider['Empty database / Importer with fields']['fieldstoremove'] = 0;
193
        $basedprovider['Empty database / Importer with fields']['fieldstoupdate'] = 0;
194
 
195
        $basedprovider['Database with fields / Empty importer']['fieldstocreate'] = 0;
196
        $basedprovider['Database with fields / Empty importer']['fieldstoremove'] = 3;
197
        $basedprovider['Database with fields / Empty importer']['fieldstoupdate'] = 0;
198
 
199
        $basedprovider['Same fields']['fieldstocreate'] = 0;
200
        $basedprovider['Same fields']['fieldstoremove'] = 0;
201
        $basedprovider['Same fields']['fieldstoupdate'] = 3;
202
 
203
        $basedprovider['Fields to create']['fieldstocreate'] = 1;
204
        $basedprovider['Fields to create']['fieldstoremove'] = 0;
205
        $basedprovider['Fields to create']['fieldstoupdate'] = 2;
206
 
207
        $basedprovider['Fields to remove']['fieldstocreate'] = 0;
208
        $basedprovider['Fields to remove']['fieldstoremove'] = 1;
209
        $basedprovider['Fields to remove']['fieldstoupdate'] = 3;
210
 
211
        $basedprovider['Fields to update']['fieldstocreate'] = 1;
212
        $basedprovider['Fields to update']['fieldstoremove'] = 1;
213
        $basedprovider['Fields to update']['fieldstoupdate'] = 2;
214
 
215
        $basedprovider['Fields to create, remove and update']['fieldstocreate'] = 1;
216
        $basedprovider['Fields to create, remove and update']['fieldstoremove'] = 2;
217
        $basedprovider['Fields to create, remove and update']['fieldstoupdate'] = 2;
218
 
219
        return $basedprovider;
220
    }
221
 
222
    /**
223
     * Test for set_affected_fields method.
224
     *
225
     * @dataProvider set_affected_provider
226
     * @covers ::set_affected_fields
227
     *
228
     * @param array $currentfields Fields of the current activity.
229
     * @param array $newfields Fields to be imported.
230
     * @param string $pluginname The plugin preset to be imported.
231
     * @param int $fieldstocreate Expected number of fields on $fieldstocreate.
232
     * @param int $fieldstoremove Expected number of fields on $fieldstoremove.
233
     * @param int $fieldstoupdate Expected number of fields on $fieldstoupdate.
234
     */
235
    public function test_set_affected_fields(
236
        array $currentfields,
237
        array $newfields,
238
        string $pluginname,
239
        int $fieldstocreate,
240
        int $fieldstoremove,
241
        int $fieldstoupdate
11 efrain 242
    ): void {
1 efrain 243
        global $USER;
244
 
245
        $this->resetAfterTest();
246
        $this->setAdminUser();
247
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
248
 
249
        // Create a course and a database activity.
250
        $course = $this->getDataGenerator()->create_course();
251
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
252
        // Add current fields to the activity.
253
        foreach ($currentfields as $field) {
254
            $plugingenerator->create_field($field, $activity);
255
        }
256
        $manager = manager::create_from_instance($activity);
257
 
258
        $presetactivity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
259
        // Add current fields to the activity.
260
        foreach ($newfields as $field) {
261
            $plugingenerator->create_field($field, $presetactivity);
262
        }
263
 
264
        $record = (object) [
265
            'name' => 'Testing preset name',
266
            'description' => 'Testing preset description',
267
        ];
268
        $saved = $plugingenerator->create_preset($presetactivity, $record);
269
        $savedimporter = new preset_existing_importer($manager, $USER->id . '/Testing preset name');
270
        $this->assertEquals(count($savedimporter->fieldstoremove), $fieldstoremove);
271
        $this->assertEquals(count($savedimporter->fieldstocreate), $fieldstocreate);
272
        $this->assertEquals(count($savedimporter->fieldstoupdate), $fieldstoupdate);
273
 
274
        // Create presets and importers.
275
        if ($pluginname) {
276
            $plugin = preset::create_from_plugin(null, $pluginname);
277
            $pluginimporter = new preset_existing_importer($manager, '/' . $pluginname);
278
            $this->assertEquals(count($pluginimporter->fieldstoremove), $fieldstoremove);
279
            $this->assertEquals(count($pluginimporter->fieldstocreate), $fieldstocreate);
280
            $this->assertEquals(count($pluginimporter->fieldstoupdate), $fieldstoupdate);
281
        }
282
    }
283
 
284
    /**
285
     * Test for get_mapping_information method.
286
     *
287
     * @dataProvider set_affected_provider
288
     * @covers ::get_mapping_information
289
     *
290
     * @param array $currentfields Fields of the current activity.
291
     * @param array $newfields Fields to be imported.
292
     * @param string $pluginname The plugin preset to be imported.
293
     * @param int $fieldstocreate Expected number of fields on $fieldstocreate.
294
     * @param int $fieldstoremove Expected number of fields on $fieldstoremove.
295
     * @param int $fieldstoupdate Expected number of fields on $fieldstoupdate.
296
     */
297
    public function test_get_mapping_information(
298
        array $currentfields,
299
        array $newfields,
300
        string $pluginname,
301
        int $fieldstocreate,
302
        int $fieldstoremove,
303
        int $fieldstoupdate
11 efrain 304
    ): void {
1 efrain 305
        global $USER;
306
 
1441 ariadna 307
        // xdebug_break();
1 efrain 308
        $this->resetAfterTest();
309
        $this->setAdminUser();
310
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
311
 
312
        // Create a course and a database activity.
313
        $course = $this->getDataGenerator()->create_course();
314
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
315
        // Add current fields to the activity.
316
        foreach ($currentfields as $field) {
317
            $plugingenerator->create_field($field, $activity);
318
        }
319
        $manager = manager::create_from_instance($activity);
320
 
321
        $presetactivity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
322
        // Add current fields to the activity.
323
        foreach ($newfields as $field) {
324
            $plugingenerator->create_field($field, $presetactivity);
325
        }
326
 
327
        $record = (object) [
328
            'name' => 'Testing preset name',
329
            'description' => 'Testing preset description',
330
        ];
331
        $saved = $plugingenerator->create_preset($presetactivity, $record);
332
        $savedimporter = new preset_existing_importer($manager, $USER->id . '/Testing preset name');
333
        $information = $savedimporter->get_mapping_information();
334
        $this->assertEquals($savedimporter->needs_mapping(), $information['needsmapping']);
335
        $this->assertEquals(count($savedimporter->fieldstoremove), $fieldstoremove);
336
        $this->assertEquals(count($savedimporter->fieldstocreate), $fieldstocreate);
337
        $this->assertEquals(count($savedimporter->fieldstoupdate), $fieldstoupdate);
338
 
339
        // Create presets and importers.
340
        if ($pluginname) {
341
            $plugin = preset::create_from_plugin(null, $pluginname);
342
            $pluginimporter = new preset_existing_importer($manager, '/' . $pluginname);
343
            $information = $pluginimporter->get_mapping_information();
344
            $this->assertEquals($pluginimporter->needs_mapping(), $information['needsmapping']);
345
            $this->assertEquals(count($pluginimporter->fieldstoremove), $fieldstoremove);
346
            $this->assertEquals(count($pluginimporter->fieldstocreate), $fieldstocreate);
347
            $this->assertEquals(count($pluginimporter->fieldstoupdate), $fieldstoupdate);
348
        }
349
    }
350
 
351
    /**
352
     * Data provider for get_field_names().
353
     *
354
     * @return array[]
355
     */
1441 ariadna 356
    public static function get_field_names_provider(): array {
1 efrain 357
        return [
358
            'Empty list' => [
359
                'fields' => [],
360
                'expected' => '',
361
            ],
362
            'List with one field' => [
363
                'fields' => ['fieldname' => 'text'],
364
                'expected' => 'fieldname',
365
            ],
366
            'List of fields with same type' => [
367
                'fields' => ['textfield' => 'text', 'other' => 'text'],
368
                'expected' => 'textfield, other',
369
            ],
370
            'List of fields with different type' => [
371
                'fields' => ['textfield' => 'text', 'number' => 'number'],
372
                'expected' => 'textfield, number',
373
            ],
374
        ];
375
    }
376
 
377
    /**
378
     * Test for get_field_names method.
379
     *
380
     * @dataProvider get_field_names_provider
381
     * @covers ::get_field_names
382
     *
383
     * @param array $fields List of fields to get the names from.
384
     * @param string $expected The list of field names expected.
385
     */
11 efrain 386
    public function test_get_field_names(array $fields, string $expected): void {
1 efrain 387
        global $USER;
388
 
389
        $this->resetAfterTest();
390
        $this->setAdminUser();
391
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
392
 
393
        // Create a course and a database activity.
394
        $course = $this->getDataGenerator()->create_course();
395
        $presetactivity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
396
        foreach ($fields as $fieldname => $fieldtype) {
397
            $newfield = new \stdClass();
398
            $newfield->name = $fieldname;
399
            $newfield->type = $fieldtype;
400
 
401
            $createdfield = $plugingenerator->create_field($newfield, $presetactivity);
402
        }
403
        $manager = manager::create_from_instance($presetactivity);
404
 
405
        $record = (object) [
406
            'name' => 'Testing preset name',
407
            'description' => 'Testing preset description',
408
        ];
409
        $saved = $plugingenerator->create_preset($presetactivity, $record);
410
        $savedimporter = new preset_existing_importer($manager, $USER->id . '/Testing preset name');
411
        $this->assertEquals($expected, $savedimporter->get_field_names($manager->get_field_records()));
412
    }
413
 
414
    /**
415
     * Test for create_from_plugin_or_directory creation static method.
416
     *
417
     * @covers ::create_from_plugin_or_directory
418
     *
419
     */
11 efrain 420
    public function test_create_from_plugin_or_directory(): void {
1 efrain 421
 
422
        global $USER;
423
 
424
        $this->resetAfterTest();
425
        $this->setAdminUser();
426
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
427
 
428
        // Create a course and a database activity.
429
        $course = $this->getDataGenerator()->create_course();
430
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
431
        $manager = manager::create_from_instance($activity);
432
 
433
        $presetactivity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
434
 
435
        $record = (object) [
436
            'name' => 'Testing preset name',
437
            'description' => 'Testing preset description',
438
        ];
439
        $saved = $plugingenerator->create_preset($presetactivity, $record);
440
 
441
        // A plugin preset returns an instance of preset_existing_importer.
442
        $preset = preset_importer::create_from_plugin_or_directory($manager, '/imagegallery');
443
        $this->assertInstanceOf('\mod_data\local\importer\preset_existing_importer', $preset);
444
 
445
        // A saved preset returns an instance of preset_existing_importer.
446
        $preset = preset_importer::create_from_plugin_or_directory($manager, $USER->id . '/Testing preset name');
447
        $this->assertInstanceOf('\mod_data\local\importer\preset_existing_importer', $preset);
448
 
449
        // An empty preset name throws an exception.
450
        $this->expectException('moodle_exception');
451
        try {
452
            preset_importer::create_from_plugin_or_directory($manager, '');
453
        } finally {
454
            // A non-existing preset name throws an exception.
455
            $this->expectException('moodle_exception');
456
            preset_importer::create_from_plugin_or_directory($manager, $USER->id . '/Non-existing');
457
        }
458
    }
459
}