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 mod_data;
18
 
19
use file_archive;
20
use stdClass;
21
use zip_archive;
22
 
23
/**
24
 * Preset tests class for mod_data.
25
 *
26
 * @package    mod_data
27
 * @category   test
28
 * @copyright  2022 Sara Arjona <sara@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @coversDefaultClass \mod_data\preset
31
 */
32
class preset_test extends \advanced_testcase {
33
 
34
    /**
35
     * Test for static create_from_plugin method.
36
     *
37
     * @covers ::create_from_plugin
38
     */
11 efrain 39
    public function test_create_from_plugin(): void {
1 efrain 40
        $this->resetAfterTest();
41
        $this->setAdminUser();
42
 
43
        // Check create_from_plugin is working as expected when an existing plugin is given.
44
        $pluginname = 'imagegallery';
45
        $result = preset::create_from_plugin(null, $pluginname);
46
        $this->assertTrue($result->isplugin);
47
        $this->assertEquals(get_string('modulename', "datapreset_$pluginname"), $result->name);
48
        $this->assertEquals($pluginname, $result->shortname);
49
        $this->assertEquals(get_string('modulename_help', "datapreset_$pluginname"), $result->description);
50
        $this->assertEmpty($result->get_userid());
51
        $this->assertEmpty($result->storedfile);
52
        $this->assertNull($result->get_path());
53
 
54
        // Check create_from_plugin is working as expected when an unexisting plugin is given.
55
        $pluginname = 'unexisting';
56
        $result = preset::create_from_plugin(null, $pluginname);
57
        $this->assertNull($result);
58
    }
59
 
60
    /**
61
     * Test for static create_from_storedfile method.
62
     *
63
     * @covers ::create_from_storedfile
64
     */
11 efrain 65
    public function test_create_from_storedfile(): void {
1 efrain 66
        global $USER;
67
 
68
        $this->resetAfterTest();
69
        $this->setAdminUser();
70
 
71
        // Create a course and a database activity.
72
        $course = $this->getDataGenerator()->create_course();
73
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
74
        $manager = manager::create_from_instance($activity);
75
 
76
        // Create a saved preset.
77
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
78
        $record = (object) [
79
            'name' => 'Testing preset name',
80
            'description' => 'Testing preset description',
81
        ];
82
        $plugingenerator->create_preset($activity, $record);
83
        $savedpresets = $manager->get_available_saved_presets();
84
        $savedpreset = reset($savedpresets);
85
 
86
        // Check create_from_storedfile is working as expected with a valid preset file.
87
        $result = preset::create_from_storedfile($manager, $savedpreset->storedfile);
88
        $this->assertFalse($result->isplugin);
89
        $this->assertEquals($record->name, $result->name);
90
        $this->assertEquals($record->name, $result->shortname);
91
        $this->assertEquals($record->description, $result->description);
92
        $this->assertEquals($savedpreset->storedfile->get_userid(), $result->get_userid());
93
        $this->assertNotEmpty($result->storedfile);
94
        $this->assertEquals('/' . $record->name . '/', $result->get_path());
95
 
96
        // Check create_from_storedfile is not creating a preset object when an invalid file is given.
97
        $draftid = file_get_unused_draft_itemid();
98
        $filerecord = [
99
            'component' => 'user',
100
            'filearea' => 'draft',
101
            'contextid' => \context_user::instance($USER->id)->id,
102
            'itemid' => $draftid,
103
            'filename' => 'preset.xml',
104
            'filepath' => '/'
105
        ];
106
        $fs = get_file_storage();
107
        $file = $fs->create_file_from_string($filerecord, 'This is the file content');
108
        $result = preset::create_from_storedfile($manager, $file);
109
        $this->assertNull($result);
110
    }
111
 
112
    /**
113
     * Test for static create_from_instance method.
114
     *
115
     * @covers ::create_from_instance
116
     */
11 efrain 117
    public function test_create_from_instance(): void {
1 efrain 118
        $this->resetAfterTest();
119
        $this->setAdminUser();
120
 
121
        // Create a course and a database activity.
122
        $course = $this->getDataGenerator()->create_course();
123
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
124
        $manager = manager::create_from_instance($activity);
125
 
126
        // Create a saved preset.
127
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
128
        $record = (object) [
129
            'name' => 'Testing preset name',
130
            'description' => 'Testing preset description',
131
        ];
132
        $plugingenerator->create_preset($activity, $record);
133
        $savedpresets = $manager->get_available_saved_presets();
134
        $savedpreset = reset($savedpresets);
135
 
136
        // Check create_from_instance is working as expected when a preset with this name exists.
137
        $result = preset::create_from_instance($manager, $record->name, $record->description);
138
        $this->assertFalse($result->isplugin);
139
        $this->assertEquals($record->name, $result->name);
140
        $this->assertEquals($record->name, $result->shortname);
141
        $this->assertEquals($record->description, $result->description);
142
        $this->assertEquals($savedpreset->storedfile->get_userid(), $result->get_userid());
143
        $this->assertNotEmpty($result->storedfile);
144
        $this->assertEquals('/' . $record->name . '/', $result->get_path());
145
 
146
        // Check create_from_instance is working as expected when there is no preset with the given name.
147
        $presetname = 'Unexisting preset';
148
        $presetdescription = 'This is the description for the unexisting preset';
149
        $result = preset::create_from_instance($manager, $presetname, $presetdescription);
150
        $this->assertFalse($result->isplugin);
151
        $this->assertEquals($presetname, $result->name);
152
        $this->assertEquals($presetname, $result->shortname);
153
        $this->assertEquals($presetdescription, $result->description);
154
        $this->assertEmpty($result->get_userid());
155
        $this->assertEmpty($result->storedfile);
156
        $this->assertEquals('/' . $presetname . '/', $result->get_path());
157
    }
158
 
159
    /**
160
     * Test for static create_from_fullname method.
161
     *
162
     * @covers ::create_from_fullname
163
     */
11 efrain 164
    public function test_create_from_fullname(): void {
1 efrain 165
        $this->resetAfterTest();
166
        $this->setAdminUser();
167
 
168
        // Create a course and a database activity.
169
        $course = $this->getDataGenerator()->create_course();
170
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
171
        $manager = manager::create_from_instance($activity);
172
 
173
        // Create a saved preset.
174
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
175
        $record = (object) [
176
            'name' => 'Testing preset name',
177
            'description' => 'Testing preset description',
178
        ];
179
        $savedpreset = $plugingenerator->create_preset($activity, $record);
180
 
181
        // Check instantiate from plugin.
182
        $pluginname = 'imagegallery';
183
        $fullname = '0/imagegallery';
184
        $result = preset::create_from_fullname($manager, $fullname);
185
        $this->assertTrue($result->isplugin);
186
        $this->assertEquals(get_string('modulename', "datapreset_$pluginname"), $result->name);
187
        $this->assertEquals($pluginname, $result->shortname);
188
        $this->assertEquals(get_string('modulename_help', "datapreset_$pluginname"), $result->description);
189
        $this->assertEmpty($result->get_userid());
190
        $this->assertEmpty($result->storedfile);
191
        $this->assertNull($result->get_path());
192
 
193
        // Check instantiate from user preset
194
        // Check create_from_instance is working as expected when a preset with this name exists.
195
        $fullname = $savedpreset->get_userid() . '/' . $savedpreset->name;
196
        $result = preset::create_from_fullname($manager, $fullname);
197
        $this->assertFalse($result->isplugin);
198
        $this->assertEquals($savedpreset->name, $result->name);
199
        $this->assertEquals($savedpreset->shortname, $result->shortname);
200
        $this->assertEquals($savedpreset->description, $savedpreset->description);
201
        $this->assertEquals($savedpreset->storedfile->get_userid(), $result->get_userid());
202
        $this->assertNotEmpty($result->storedfile);
203
        $this->assertEquals('/' . $savedpreset->name . '/', $result->get_path());
204
    }
205
 
206
    /**
207
     * Test for the save a preset method when the preset hasn't been saved before.
208
     *
209
     * @covers ::save
210
     */
11 efrain 211
    public function test_save_new_preset(): void {
1 efrain 212
        $this->resetAfterTest();
213
        $this->setAdminUser();
214
 
215
        // Save should return false when trying to save a plugin preset.
216
        $preset = preset::create_from_plugin(null, 'imagegallery');
217
        $result = $preset->save();
218
        $this->assertFalse($result);
219
 
220
        // Create a course and a database activity.
221
        $course = $this->getDataGenerator()->create_course();
222
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
223
        $manager = manager::create_from_instance($activity);
224
 
225
        // Add a field to the activity.
226
        $fieldrecord = new stdClass();
227
        $fieldrecord->name = 'field-1';
228
        $fieldrecord->type = 'text';
229
        $datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
230
        $datagenerator->create_field($fieldrecord, $activity);
231
 
232
        // Create a saved preset.
233
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
234
        $record = (object) [
235
            'name' => 'Testing preset name',
236
            'description' => 'Testing preset description',
237
        ];
238
        $plugingenerator->create_preset($activity, $record);
239
 
240
        // Save should return false when trying to save an existing saved preset.
241
        $preset = preset::create_from_instance($manager, $record->name, $record->description);
242
        $result = $preset->save();
243
        $this->assertFalse($result);
244
 
245
        // The preset should be saved when it's new and there is no any other having the same name.
246
        $savedpresets = $manager->get_available_saved_presets();
247
        $this->assertCount(1, $savedpresets);
248
        $presetname = 'New preset';
249
        $presetdescription = 'This is the description for the new preset';
250
        $preset = preset::create_from_instance($manager, $presetname, $presetdescription);
251
        $result = $preset->save();
252
        $this->assertTrue($result);
253
        // Check the preset has been created.
254
        $savedpresets = $manager->get_available_saved_presets();
255
        $this->assertCount(2, $savedpresets);
256
        $savedpresetsnames = array_map(function($preset) {
257
            return $preset->name;
258
        }, $savedpresets);
259
        $this->assertContains($presetname, $savedpresetsnames);
260
    }
261
 
262
    /**
263
     * Test for the save a preset method when is an existing preset that has been saved before.
264
     *
265
     * @covers ::save
266
     */
11 efrain 267
    public function test_save_existing_preset(): void {
1 efrain 268
        $this->resetAfterTest();
269
        $this->setAdminUser();
270
 
271
        // Create a course and a database activity.
272
        $course = $this->getDataGenerator()->create_course();
273
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
274
        $manager = manager::create_from_instance($activity);
275
 
276
        // Add a field to the activity.
277
        $fieldrecord = new stdClass();
278
        $fieldrecord->name = 'field-1';
279
        $fieldrecord->type = 'text';
280
        $datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
281
        $datagenerator->create_field($fieldrecord, $activity);
282
 
283
        // Create a saved preset.
284
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
285
        $record = (object) [
286
            'name' => 'Testing preset name',
287
            'description' => 'Testing preset description',
288
        ];
289
        $oldpresetname = $record->name;
290
        $plugingenerator->create_preset($activity, $record);
291
 
292
        // Save should return false when trying to save an existing preset.
293
        $preset = preset::create_from_instance($manager, $record->name, $record->description);
294
        $result = $preset->save();
295
        $this->assertFalse($result);
296
        // Check no new preset has been created.
297
        $this->assertCount(1, $manager->get_available_saved_presets());
298
 
299
        // Save should overwrite existing preset if name or description have changed.
300
        $preset->name = 'New preset name';
301
        $preset->description = 'New preset description';
302
        $result = $preset->save();
303
        $this->assertTrue($result);
304
        // Check the preset files have been renamed.
305
        $presetfiles = array_merge(array_values(manager::TEMPLATES_LIST), ['preset.xml', '.']);
306
        foreach ($presetfiles as $templatefile) {
307
            $file = preset::get_file($preset->get_path(), $templatefile);
308
            $this->assertNotNull($file);
309
        }
310
        // Check old preset files have been removed.
311
        $oldpath = "{$oldpresetname}";
312
        foreach ($presetfiles as $templatefile) {
313
            $file = preset::get_file($oldpath, $templatefile);
314
            $this->assertNull($file);
315
        }
316
 
317
        // Check no new preset has been created.
318
        $savedpresets = $manager->get_available_saved_presets();
319
        $this->assertCount(1, $savedpresets);
320
        // Check the preset has the expected values.
321
        $savedpreset = reset($savedpresets);
322
        $this->assertEquals($preset->name, $savedpreset->name);
323
        $this->assertEquals($preset->description, $savedpreset->description);
324
        $this->assertNotEmpty($preset->storedfile);
325
        // Check the storedfile has been updated properly.
326
        $this->assertEquals($preset->name, trim($savedpreset->storedfile->get_filepath(), '/'));
327
 
328
        // Create another saved preset with empty description.
329
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
330
        $record = (object) [
331
            'name' => 'Testing preset 2',
332
        ];
333
        $plugingenerator->create_preset($activity, $record);
334
        $this->assertCount(2, $manager->get_available_saved_presets());
335
        // Description should be saved too when it was empty in the original preset and a new value is assigned to it.
336
        $preset = preset::create_from_instance($manager, $record->name);
337
        $preset->description = 'New preset description';
338
        $result = $preset->save();
339
        $this->assertTrue($result);
340
        $savedpresets = $manager->get_available_saved_presets();
341
        $this->assertCount(2, $savedpresets);
342
        foreach ($savedpresets as $savedpreset) {
343
            if ($savedpreset->name == $record->name) {
344
                $this->assertEquals($preset->description, $savedpreset->description);
345
            }
346
        }
347
    }
348
 
349
    /**
350
     * Test for the export a preset method.
351
     *
352
     * @covers ::export
353
     */
11 efrain 354
    public function test_export(): void {
1 efrain 355
        $this->resetAfterTest();
356
        $this->setAdminUser();
357
 
358
        // Export should return empty string when trying to export a plugin preset.
359
        $preset = preset::create_from_plugin(null, 'imagegallery');
360
        $result = $preset->export();
361
        $this->assertEmpty($result);
362
 
363
        // Create a course and a database activity.
364
        $course = $this->getDataGenerator()->create_course();
365
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
366
        $manager = manager::create_from_instance($activity);
367
 
368
        // Add a field to the activity.
369
        $fieldrecord = new stdClass();
370
        $fieldrecord->name = 'field-1';
371
        $fieldrecord->type = 'text';
372
        $datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
373
        $datagenerator->create_field($fieldrecord, $activity);
374
 
375
        // For now, default templates are not created automatically. This will be changed in MDL-75234.
376
        foreach (manager::TEMPLATES_LIST as $templatename => $notused) {
377
            data_generate_default_template($activity, $templatename);
378
        }
379
 
380
        $preset = preset::create_from_instance($manager, $activity->name);
381
        $result = $preset->export();
382
        $presetfilenames = array_merge(array_values(manager::TEMPLATES_LIST), ['preset.xml']);
383
 
384
        $ziparchive = new zip_archive();
385
        $ziparchive->open($result, file_archive::OPEN);
386
        $files = $ziparchive->list_files();
387
        foreach ($files as $file) {
388
            $this->assertContains($file->pathname, $presetfilenames);
389
 
390
            // Check the file is not empty (except CSS, JS and listtemplateheader/footer files which are empty by default).
391
            $extension = pathinfo($file->pathname, PATHINFO_EXTENSION);
392
            $ishtmlorxmlfile = in_array($extension, ['html', 'xml']);
393
 
394
            $expectedemptyfiles = array_intersect_key(manager::TEMPLATES_LIST, array_flip([
395
                'listtemplateheader',
396
                'listtemplatefooter',
397
                'rsstitletemplate',
398
            ]));
399
 
400
            if ($ishtmlorxmlfile && !in_array($file->pathname, $expectedemptyfiles)) {
401
                $this->assertGreaterThan(0, $file->size);
402
            } else {
403
                $this->assertEquals(0, $file->size);
404
            }
405
        }
406
        $ziparchive->close();
407
    }
408
 
409
    /**
410
     * Test for get_userid().
411
     *
412
     * @covers ::get_userid
413
     */
11 efrain 414
    public function test_get_userid(): void {
1 efrain 415
        $this->resetAfterTest();
416
 
417
        $course = $this->getDataGenerator()->create_course();
418
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
419
        $user = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
420
        $this->setUser($user);
421
 
422
        // Check userid is null for plugin preset.
423
        $manager = manager::create_from_instance($activity);
424
        $pluginpresets = $manager->get_available_plugin_presets();
425
        $pluginpreset = reset($pluginpresets);
426
        $this->assertNull($pluginpreset->get_userid());
427
 
428
        // Check userid meets the user that has created the preset when it's a saved preset.
429
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
430
        $savedpreset = (object) [
431
            'name' => 'Preset created by teacher',
432
        ];
433
        $plugingenerator->create_preset($activity, $savedpreset);
434
        $savedpresets = $manager->get_available_saved_presets();
435
        $savedpreset = reset($savedpresets);
436
        $this->assertEquals($user->id, $savedpreset->get_userid());
437
 
438
        // Check userid is null when preset hasn't any file associated.
439
        $preset = preset::create_from_instance($manager, 'Unexisting preset');
440
        $this->assertNull($preset->get_userid());
441
    }
442
 
443
    /**
444
     * Test for get_path().
445
     *
446
     * @covers ::get_path
447
     */
11 efrain 448
    public function test_get_path(): void {
1 efrain 449
        $this->resetAfterTest();
450
        $this->setAdminUser();
451
 
452
        $course = $this->getDataGenerator()->create_course();
453
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
454
 
455
        // Check path is null for plugin preset.
456
        $manager = manager::create_from_instance($activity);
457
        $pluginpresets = $manager->get_available_plugin_presets();
458
        $pluginpreset = reset($pluginpresets);
459
        $this->assertNull($pluginpreset->get_path());
460
 
461
        // Check path meets expected value when it's a saved preset.
462
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
463
        $savedpreset = (object) [
464
            'name' => 'Saved preset',
465
        ];
466
        $plugingenerator->create_preset($activity, $savedpreset);
467
        $savedpresets = $manager->get_available_saved_presets();
468
        $savedpreset = reset($savedpresets);
469
        $this->assertEquals("/{$savedpreset->name}/", $savedpreset->get_path());
470
 
471
        // Check path is /presetname/ when preset hasn't any file associated.
472
        $presetname = 'Unexisting preset';
473
        $preset = preset::create_from_instance($manager, $presetname);
474
        $this->assertEquals("/{$presetname}/", $preset->get_path());
475
    }
476
 
477
    /**
478
     * Test for is_directory_a_preset().
479
     *
480
     * @dataProvider is_directory_a_preset_provider
481
     * @covers ::is_directory_a_preset
482
     * @param string $directory
483
     * @param bool $expected
484
     */
485
    public function test_is_directory_a_preset(string $directory, bool $expected): void {
486
        $this->resetAfterTest();
487
        $this->setAdminUser();
488
 
489
        $result = preset::is_directory_a_preset($directory);
490
        $this->assertEquals($expected, $result);
491
    }
492
 
493
    /**
494
     * Data provider for test_is_directory_a_preset().
495
     *
496
     * @return array
497
     */
498
    public function is_directory_a_preset_provider(): array {
499
        global $CFG;
500
 
501
        return [
502
            'Valid preset directory' => [
503
                'directory' => $CFG->dirroot . '/mod/data/preset/imagegallery',
504
                'expected' => true,
505
            ],
506
            'Invalid preset directory' => [
507
                'directory' => $CFG->dirroot . '/mod/data/field/checkbox',
508
                'expected' => false,
509
            ],
510
            'Unexisting preset directory' => [
511
                'directory' => $CFG->dirroot . 'unexistingdirectory',
512
                'expected' => false,
513
            ],
514
        ];
515
    }
516
 
517
    /**
518
     * Test for get_name_from_plugin().
519
     *
520
     * @covers ::get_name_from_plugin
521
     */
11 efrain 522
    public function test_get_name_from_plugin(): void {
1 efrain 523
        $this->resetAfterTest();
524
        $this->setAdminUser();
525
 
526
        // The expected name for plugins with modulename in lang is this value.
527
        $name = preset::get_name_from_plugin('imagegallery');
528
        $this->assertEquals('Image gallery', $name);
529
 
530
        // However, if the plugin doesn't exist or the modulename is not defined, the preset shortname will be returned.
531
        $presetshortname = 'nonexistingpreset';
532
        $name = preset::get_name_from_plugin($presetshortname);
533
        $this->assertEquals($presetshortname, $name);
534
    }
535
 
536
    /**
537
     * Test for get_description_from_plugin().
538
     *
539
     * @covers ::get_description_from_plugin
540
     */
11 efrain 541
    public function test_get_description_from_plugin(): void {
1 efrain 542
        $this->resetAfterTest();
543
        $this->setAdminUser();
544
 
545
        // The expected name for plugins with modulename in lang is this value.
546
        $description = preset::get_description_from_plugin('imagegallery');
547
        $this->assertEquals('Use this preset to collect images.', $description);
548
 
549
        // However, if the plugin doesn't exist or the modulename is not defined, empty string will be returned.
550
        $presetshortname = 'nonexistingpreset';
551
        $description = preset::get_description_from_plugin($presetshortname);
552
        $this->assertEmpty($description);
553
    }
554
 
555
    /**
556
     * Test for generate_preset_xml().
557
     *
558
     * @covers ::generate_preset_xml
559
     * @dataProvider generate_preset_xml_provider
560
     * @param array $params activity config settings
561
     * @param string|null $description preset description
562
     */
11 efrain 563
    public function test_generate_preset_xml(array $params, ?string $description): void {
1 efrain 564
        $this->resetAfterTest();
565
        $this->setAdminUser();
566
 
567
        // Make accessible the method.
568
        $reflection = new \ReflectionClass(preset::class);
569
        $method = $reflection->getMethod('generate_preset_xml');
570
 
571
        // The method should return empty string when trying to generate preset.xml for a plugin preset.
572
        $preset = preset::create_from_plugin(null, 'imagegallery');
573
        $result = $method->invokeArgs($preset, []);
574
        $this->assertEmpty($result);
575
 
576
        // Create a course and a database activity.
577
        $course = $this->getDataGenerator()->create_course();
578
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, array_merge(['course' => $course], $params));
579
 
580
        // Add a field to the activity.
581
        $fieldrecord = new stdClass();
582
        $fieldrecord->name = 'field-1';
583
        $fieldrecord->type = 'text';
584
        $datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
585
        $datagenerator->create_field($fieldrecord, $activity);
586
 
587
        $manager = manager::create_from_instance($activity);
588
        $preset = preset::create_from_instance($manager, $activity->name, $description);
589
 
590
        // Call the generate_preset_xml method.
591
        $result = $method->invokeArgs($preset, []);
592
        // Check is a valid XML.
593
        $parsedxml = simplexml_load_string($result);
594
        // Check the description has the expected value.
595
        $this->assertEquals($description, strval($parsedxml->description));
596
        // Check settings have the expected values.
597
        foreach ($params as $paramname => $paramvalue) {
598
            $this->assertEquals($paramvalue, strval($parsedxml->settings->{$paramname}));
599
        }
600
        // Check field have the expected values.
601
        $this->assertEquals($fieldrecord->name, strval($parsedxml->field->name));
602
        $this->assertEquals($fieldrecord->type, strval($parsedxml->field->type));
603
    }
604
 
605
    /**
606
     * Data provider for generate_preset_xml().
607
     *
608
     * @return array
609
     */
610
    public function generate_preset_xml_provider(): array {
611
        return [
612
            'Generate preset.xml with the default params and empty description' => [
613
                'params' => [],
614
                'description' => null,
615
            ],
616
            'Generate preset.xml with a description but the default params' => [
617
                'params' => [],
618
                'description' => 'This is a description',
619
            ],
620
            'Generate preset.xml with empty description but changing some params' => [
621
                'params' => [
622
                    'requiredentries' => 2,
623
                    'approval' => 1,
624
                ],
625
                'description' => null,
626
            ],
627
            'Generate preset.xml with a description and changing some params' => [
628
                'params' => [
629
                    'maxentries' => 5,
630
                    'manageapproved' => 0,
631
                ],
632
                'description' => 'This is a description',
633
            ],
634
        ];
635
    }
636
 
637
    /**
638
     * Test for get_file().
639
     *
640
     * @covers ::get_file
641
     */
11 efrain 642
    public function test_get_file(): void {
1 efrain 643
        $this->resetAfterTest();
644
        $this->setAdminUser();
645
 
646
        // Create a course and a database activity.
647
        $course = $this->getDataGenerator()->create_course();
648
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
649
        $manager = manager::create_from_instance($activity);
650
 
651
        $presetname = 'Saved preset';
652
        // Check file doesn't exist if the preset hasn't been saved yet.
653
        $preset = preset::create_from_instance($manager, $presetname);
654
        $file = preset::get_file($preset->get_path(), 'preset.xml');
655
        $this->assertNull($file);
656
 
657
        // Check file is not empty when there is a saved preset with this name.
658
        $preset->save();
659
        $file = preset::get_file($preset->get_path(), 'preset.xml');
660
        $this->assertNotNull($file);
661
        $this->assertStringContainsString($presetname, $file->get_filepath());
662
        $this->assertEquals('preset.xml', $file->get_filename());
663
 
664
        // Check invalid preset file name doesn't exist.
665
        $file = preset::get_file($preset->get_path(), 'unexistingpreset.xml');
666
        $this->assertNull($file);
667
    }
668
 
669
    /**
670
     * Test for can_manage().
671
     *
672
     * @covers ::can_manage
673
     */
11 efrain 674
    public function test_can_manage(): void {
1 efrain 675
        $this->resetAfterTest();
676
 
677
        // Create course, database activity and users.
678
        $course = $this->getDataGenerator()->create_course();
679
        $data = $this->getDataGenerator()->create_module('data', ['course' => $course->id]);
680
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
681
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
682
        $manager = manager::create_from_instance($data);
683
 
684
        $preset1name = 'Admin preset';
685
        $preset2name = 'Teacher preset';
686
 
687
        // Create a saved preset by admin.
688
        $this->setAdminUser();
689
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
690
        $record = (object) [
691
            'name' => $preset1name,
692
            'description' => 'Testing preset description',
693
        ];
694
        $adminpreset = $plugingenerator->create_preset($data, $record);
695
 
696
        // Create a saved preset by teacher.
697
        $this->setUser($teacher);
698
        $record = (object) [
699
            'name' => $preset2name,
700
            'description' => 'Testing preset description',
701
        ];
702
        $teacherpreset = $plugingenerator->create_preset($data, $record);
703
 
704
        // Plugins can't be deleted.
705
        $pluginpresets = manager::get_available_plugin_presets();
706
        $pluginpreset = reset($pluginpresets);
707
        $this->assertFalse($pluginpreset->can_manage());
708
 
709
        // Admin can delete all saved presets.
710
        $this->setAdminUser();
711
        $this->assertTrue($adminpreset->can_manage());
712
        $this->assertTrue($teacherpreset->can_manage());
713
 
714
        // Teacher can delete their own preset only.
715
        $this->setUser($teacher);
716
        $this->assertFalse($adminpreset->can_manage());
717
        $this->assertTrue($teacherpreset->can_manage());
718
 
719
        // Student can't delete any of the presets.
720
        $this->setUser($student);
721
        $this->assertFalse($adminpreset->can_manage());
722
        $this->assertFalse($teacherpreset->can_manage());
723
    }
724
 
725
    /**
726
     * Test for delete().
727
     *
728
     * @covers ::delete
729
     */
11 efrain 730
    public function test_delete(): void {
1 efrain 731
        $this->resetAfterTest();
732
 
733
        // Create course, database activity and users.
734
        $course = $this->getDataGenerator()->create_course();
735
        $data = $this->getDataGenerator()->create_module('data', ['course' => $course->id]);
736
        $manager = manager::create_from_instance($data);
737
        $presetname = 'Admin preset';
738
 
739
        // Create a saved preset by admin.
740
        $this->setAdminUser();
741
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
742
        $record = (object) [
743
            'name' => $presetname,
744
            'description' => 'Testing preset description',
745
        ];
746
        $adminpreset = $plugingenerator->create_preset($data, $record);
747
        $initialpresets = $manager->get_available_presets();
748
 
749
        // Plugins can't be deleted.
750
        $pluginpresets = manager::get_available_plugin_presets();
751
        $pluginpreset = reset($pluginpresets);
752
        $result = $pluginpreset->delete();
753
        $currentpluginpresets = manager::get_available_plugin_presets();
754
        $this->assertEquals(count($pluginpresets), count($currentpluginpresets));
755
 
756
        $result = $adminpreset->delete();
757
        $this->assertTrue($result);
758
 
759
        // After deleting the preset, there is no file linked.
760
        $adminpreset = preset::create_from_instance($manager, $presetname);
761
        $this->assertEmpty($adminpreset->storedfile);
762
 
763
        // Check the preset has been deleted.
764
        $currentpresets = $manager->get_available_presets();
765
        $this->assertEquals(count($initialpresets) - 1, count($currentpresets));
766
 
767
        // The behavior of trying to delete a preset twice.
768
        $result = $adminpreset->delete();
769
        $this->assertFalse($result);
770
 
771
        // Check the preset has not been deleted.
772
        $currentpresets = $manager->get_available_presets();
773
        $this->assertEquals(count($initialpresets) - 1, count($currentpresets));
774
 
775
        $emptypreset = preset::create_from_instance($manager, $presetname);
776
        // The behavior of deleting an empty preset.
777
        $result = $emptypreset->delete();
778
        $this->assertFalse($result);
779
 
780
        // Check the preset has not been deleted.
781
        $currentpresets = $manager->get_available_presets();
782
        $this->assertEquals(count($initialpresets) - 1, count($currentpresets));
783
    }
784
 
785
    /**
786
     * Test for the get_fields method.
787
     *
788
     * @covers ::get_fields
789
     */
11 efrain 790
    public function test_get_fields(): void {
1 efrain 791
        $this->resetAfterTest();
792
        $this->setAdminUser();
793
 
794
        // Create a course and a database activity.
795
        $course = $this->getDataGenerator()->create_course();
796
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
797
        $manager = manager::create_from_instance($activity);
798
 
799
        // Add a field to the activity.
800
        $fieldrecord = new stdClass();
801
        $fieldrecord->name = 'field-1';
802
        $fieldrecord->type = 'text';
803
        $datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
804
        $datagenerator->create_field($fieldrecord, $activity);
805
 
806
        // Create a saved preset.
807
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
808
        $record = (object) [
809
            'name' => 'Testing preset name',
810
            'description' => 'Testing preset description',
811
        ];
812
        $preset = $plugingenerator->create_preset($activity, $record);
813
 
814
        // Check regular fields.
815
        $fields = $preset->get_fields();
816
        $this->assertCount(1, $fields);
817
        $this->assertArrayHasKey('field-1', $fields);
818
        $field = $fields['field-1'];
819
        $this->assertEquals('text', $field->type);
820
        $this->assertEquals('field-1', $field->get_name());
821
        $this->assertEquals(false, $field->get_preview());
822
 
823
        // Check preview fields.
824
        $savedpresets = $manager->get_available_saved_presets();
825
        $preset = reset($savedpresets);
826
        $fields = $preset->get_fields(true);
827
        $this->assertCount(1, $fields);
828
        $this->assertArrayHasKey('field-1', $fields);
829
        $field = $fields['field-1'];
830
        $this->assertEquals('text', $field->type);
831
        $this->assertEquals('field-1', $field->get_name());
832
        $this->assertEquals(true, $field->get_preview());
833
    }
834
 
835
    /**
836
     * Test for the get_sample_entries method.
837
     *
838
     * @covers ::get_sample_entries
839
     */
11 efrain 840
    public function test_get_sample_entries(): void {
1 efrain 841
        $this->resetAfterTest();
842
 
843
        $user = $this->getDataGenerator()->create_user();
844
        $this->setUser($user);
845
 
846
        // Create a course and a database activity.
847
        $course = $this->getDataGenerator()->create_course();
848
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
849
        $manager = manager::create_from_instance($activity);
850
 
851
        // Add a field to the activity.
852
        $fieldrecord = new stdClass();
853
        $fieldrecord->name = 'field-1';
854
        $fieldrecord->type = 'text';
855
        $datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
856
        $datagenerator->create_field($fieldrecord, $activity);
857
 
858
        // Create a saved preset.
859
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
860
        $record = (object) [
861
            'name' => 'Testing preset name',
862
            'description' => 'Testing preset description',
863
        ];
864
        $preset = $plugingenerator->create_preset($activity, $record);
865
 
866
        $entries = $preset->get_sample_entries(3);
867
        $this->assertCount(3, $entries);
868
        foreach ($entries as $entry) {
869
            $this->assertEquals($user->id, $entry->userid);
870
            $this->assertEquals($user->email, $entry->email);
871
            $this->assertEquals($user->firstname, $entry->firstname);
872
            $this->assertEquals($user->lastname, $entry->lastname);
873
            $this->assertEquals($activity->id, $entry->dataid);
874
            $this->assertEquals(0, $entry->groupid);
875
            $this->assertEquals(1, $entry->approved);
876
        }
877
    }
878
 
879
    /**
880
     * Test for the get_template_content method.
881
     *
882
     * @covers ::get_template_content
883
     */
11 efrain 884
    public function test_get_template_content(): void {
1 efrain 885
        $this->resetAfterTest();
886
 
887
        $user = $this->getDataGenerator()->create_user();
888
        $this->setUser($user);
889
        $course = $this->getDataGenerator()->create_course();
890
 
891
        // Module data with templates.
892
        $templates = [
893
            'singletemplate' => 'Single template content',
894
            'listtemplate' => 'List template content',
895
            'listtemplateheader' => 'List template content header',
896
            'listtemplatefooter' => 'List template content footer',
897
            'addtemplate' => 'Add template content',
898
            'rsstemplate' => 'RSS template content',
899
            'rsstitletemplate' => 'RSS title template content',
900
            'csstemplate' => 'CSS template content',
901
            'jstemplate' => 'JS template content',
902
            'asearchtemplate' => 'Advanced search template content',
903
        ];
904
        $params = array_merge(['course' => $course], $templates);
905
 
906
        // Create a database activity.
907
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, $params);
908
        $manager = manager::create_from_instance($activity);
909
 
910
        // Create a saved preset.
911
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
912
        $record = (object) [
913
            'name' => 'Testing preset name',
914
            'description' => 'Testing preset description',
915
        ];
916
        $preset = $plugingenerator->create_preset($activity, $record);
917
 
918
        // Test user preset templates.
919
        foreach ($templates as $templatename => $templatecontent) {
920
            $content = $preset->get_template_content($templatename);
921
            $this->assertEquals($templatecontent, $content);
922
        }
923
 
924
        // Test plugin preset content.
925
        $pluginname = 'imagegallery';
926
        $preset = preset::create_from_plugin($manager, $pluginname);
927
        foreach (manager::TEMPLATES_LIST as $templatename => $templatefile) {
928
            // Get real file contents.
929
            $path = $manager->path . '/preset/' . $pluginname . '/' . $templatefile;
930
            $templatecontent = file_get_contents($path);
931
            $content = $preset->get_template_content($templatename);
932
            $this->assertEquals($templatecontent, $content);
933
        }
934
    }
935
 
936
    /**
937
     * Test for the get_fullname method.
938
     *
939
     * @covers ::get_fullname
940
     */
11 efrain 941
    public function test_get_fullname(): void {
1 efrain 942
        $this->resetAfterTest();
943
 
944
        $user = $this->getDataGenerator()->create_user();
945
        $this->setUser($user);
946
        $course = $this->getDataGenerator()->create_course();
947
 
948
        // Create a database activity.
949
        $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
950
        $manager = manager::create_from_instance($activity);
951
 
952
        // Create a saved preset.
953
        $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
954
        $record = (object) [
955
            'name' => 'Testing preset name',
956
            'description' => 'Testing preset description',
957
        ];
958
        $preset = $plugingenerator->create_preset($activity, $record);
959
 
960
        // Test user preset templates.
961
        $this->assertEquals("{$user->id}/Testing preset name", $preset->get_fullname());
962
 
963
        // Test plugin preset content.
964
        $pluginname = 'imagegallery';
965
        $preset = preset::create_from_plugin($manager, $pluginname);
966
        $this->assertEquals("0/imagegallery", $preset->get_fullname());
967
    }
968
}