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
/**
18
 * Data generators for acceptance testing.
19
 *
20
 * @package   core
21
 * @category  test
22
 * @copyright 2012 David Monllaó
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
 
31
/**
32
 * Behat data generator class for core entities.
33
 *
34
 * @package   core
35
 * @category  test
36
 * @copyright 2012 David Monllaó
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class behat_core_generator extends behat_generator_base {
40
 
41
    protected function get_creatable_entities(): array {
42
        $entities = [
43
            'users' => [
44
                'singular' => 'user',
45
                'datagenerator' => 'user',
46
                'required' => ['username'],
47
            ],
48
            'categories' => [
49
                'singular' => 'category',
50
                'datagenerator' => 'category',
51
                'required' => ['idnumber'],
52
                'switchids' => ['category' => 'parent'],
53
            ],
54
            'courses' => [
55
                'singular' => 'course',
56
                'datagenerator' => 'course',
57
                'required' => ['shortname'],
58
                'switchids' => ['category' => 'category'],
59
            ],
60
            'groups' => [
61
                'singular' => 'group',
62
                'datagenerator' => 'group',
63
                'required' => ['idnumber', 'course'],
64
                'switchids' => ['course' => 'courseid'],
65
            ],
66
            'groupings' => [
67
                'singular' => 'grouping',
68
                'datagenerator' => 'grouping',
69
                'required' => ['idnumber', 'course'],
70
                'switchids' => ['course' => 'courseid'],
71
            ],
72
            'course enrolments' => [
73
                'singular' => 'course enrolment',
74
                'datagenerator' => 'enrol_user',
75
                'required' => ['user', 'course', 'role'],
76
                'switchids' => ['user' => 'userid', 'course' => 'courseid', 'role' => 'roleid'],
77
            ],
78
            'custom field categories' => [
79
                'singular' => 'custom field category',
80
                'datagenerator' => 'custom_field_category',
81
                'required' => ['name', 'component', 'area', 'itemid'],
82
                'switchids' => [],
83
            ],
84
            'custom fields' => [
85
                'singular' => 'custom field',
86
                'datagenerator' => 'custom_field',
87
                'required' => ['name', 'category', 'type', 'shortname'],
88
                'switchids' => [],
89
            ],
90
            'custom profile field categories' => [
91
                'singular' => 'custom profile field category',
92
                'datagenerator' => 'custom_profile_field_category',
93
                'required' => ['name'],
94
                'switchids' => [],
95
            ],
96
            'custom profile fields' => [
97
                'singular' => 'custom profile field',
98
                'datagenerator' => 'custom_profile_field',
99
                'required' => ['datatype', 'shortname', 'name'],
100
                'switchids' => [],
101
            ],
102
            'permission overrides' => [
103
                'singular' => 'permission override',
104
                'datagenerator' => 'permission_override',
105
                'required' => ['capability', 'permission', 'role', 'contextlevel', 'reference'],
106
                'switchids' => ['role' => 'roleid'],
107
            ],
108
            'system role assigns' => [
109
                'singular' => 'system role assignment',
110
                'datagenerator' => 'system_role_assign',
111
                'required' => ['user', 'role'],
112
                'switchids' => ['user' => 'userid', 'role' => 'roleid'],
113
            ],
114
            'role assigns' => [
115
                'singular' => 'role assignment',
116
                'datagenerator' => 'role_assign',
117
                'required' => ['user', 'role', 'contextlevel', 'reference'],
118
                'switchids' => ['user' => 'userid', 'role' => 'roleid'],
119
            ],
120
            'activities' => [
121
                'singular' => 'activity',
122
                'datagenerator' => 'activity',
123
                'required' => ['activity', 'course'],
124
                'switchids' => ['course' => 'course', 'gradecategory' => 'gradecat', 'grouping' => 'groupingid'],
125
            ],
126
            'blocks' => [
127
                'singular' => 'block',
128
                'datagenerator' => 'block_instance',
129
                'required' => ['blockname', 'contextlevel', 'reference'],
130
            ],
131
            'group members' => [
132
                'singular' => 'group member',
133
                'datagenerator' => 'group_member',
134
                'required' => ['user', 'group'],
135
                'switchids' => ['user' => 'userid', 'group' => 'groupid'],
136
            ],
137
            'grouping groups' => [
138
                'singular' => 'grouping group',
139
                'datagenerator' => 'grouping_group',
140
                'required' => ['grouping', 'group'],
141
                'switchids' => ['grouping' => 'groupingid', 'group' => 'groupid'],
142
            ],
143
            'cohorts' => [
144
                'singular' => 'cohort',
145
                'datagenerator' => 'cohort',
146
                'required' => ['idnumber'],
147
            ],
148
            'cohort members' => [
149
                'singular' => 'cohort member',
150
                'datagenerator' => 'cohort_member',
151
                'required' => ['user', 'cohort'],
152
                'switchids' => ['user' => 'userid', 'cohort' => 'cohortid'],
153
            ],
154
            'roles' => [
155
                'singular' => 'role',
156
                'datagenerator' => 'role',
157
                'required' => ['shortname'],
158
            ],
159
            'role capabilities' => [
160
                'singular' => 'role capability',
161
                'datagenerator' => 'role_capability',
162
                'required' => ['role'],
163
                'switchids' => ['role' => 'roleid'],
164
            ],
165
            'grade categories' => [
166
                'singular' => 'grade category',
167
                'datagenerator' => 'grade_category',
168
                'required' => ['fullname', 'course'],
169
                'switchids' => ['course' => 'courseid', 'gradecategory' => 'parent'],
170
            ],
171
            'grade grades' => [
172
                'singular' => 'grade grade',
173
                'datagenerator' => 'grade_grade',
174
                'required' => ['gradeitem'],
175
                'switchids' => ['user' => 'userid', 'gradeitem' => 'itemid'],
176
            ],
177
            'grade items' => [
178
                'singular' => 'grade item',
179
                'datagenerator' => 'grade_item',
180
                'required' => ['course'],
181
                'switchids' => [
182
                    'scale' => 'scaleid',
183
                    'outcome' => 'outcomeid',
184
                    'course' => 'courseid',
185
                    'gradecategory' => 'categoryid',
186
                ],
187
            ],
188
            'grade outcomes' => [
189
                'singular' => 'grade outcome',
190
                'datagenerator' => 'grade_outcome',
191
                'required' => ['shortname', 'scale'],
192
                'switchids' => ['course' => 'courseid', 'gradecategory' => 'categoryid', 'scale' => 'scaleid'],
193
            ],
194
            'scales' => [
195
                'singular' => 'scale',
196
                'datagenerator' => 'scale',
197
                'required' => ['name', 'scale'],
198
                'switchids' => ['course' => 'courseid'],
199
            ],
200
            'question categories' => [
201
                'singular' => 'question category',
202
                'datagenerator' => 'question_category',
203
                'required' => ['name', 'contextlevel', 'reference'],
204
                'switchids' => ['questioncategory' => 'parent'],
205
            ],
206
            'questions' => [
207
                'singular' => 'question',
208
                'datagenerator' => 'question',
209
                'required' => ['qtype', 'questioncategory', 'name'],
210
                'switchids' => ['questioncategory' => 'category', 'user' => 'createdby'],
211
            ],
212
            'tags' => [
213
                'singular' => 'tag',
214
                'datagenerator' => 'tag',
215
                'required' => ['name'],
216
            ],
217
            'events' => [
218
                'singular' => 'event',
219
                'datagenerator' => 'event',
220
                'required' => ['name', 'eventtype'],
221
                'switchids' => [
222
                    'user' => 'userid',
223
                    'course' => 'courseid',
224
                    'category' => 'categoryid',
225
                ],
226
            ],
227
            'message contacts' => [
228
                'singular' => 'message contact',
229
                'datagenerator' => 'message_contacts',
230
                'required' => ['user', 'contact'],
231
                'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
232
            ],
233
            'private messages' => [
234
                'singular' => 'private message',
235
                'datagenerator' => 'private_messages',
236
                'required' => ['user', 'contact', 'message'],
237
                'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
238
            ],
239
            'favourite conversations' => [
240
                'singular' => 'favourite conversation',
241
                'datagenerator' => 'favourite_conversations',
242
                'required' => ['user', 'contact'],
243
                'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
244
            ],
245
            'group messages' => [
246
                'singular' => 'group message',
247
                'datagenerator' => 'group_messages',
248
                'required' => ['user', 'group', 'message'],
249
                'switchids' => ['user' => 'userid', 'group' => 'groupid'],
250
            ],
251
            'muted group conversations' => [
252
                'singular' => 'muted group conversation',
253
                'datagenerator' => 'mute_group_conversations',
254
                'required' => ['user', 'group', 'course'],
255
                'switchids' => ['user' => 'userid', 'group' => 'groupid', 'course' => 'courseid'],
256
            ],
257
            'muted private conversations' => [
258
                'singular' => 'muted private conversation',
259
                'datagenerator' => 'mute_private_conversations',
260
                'required' => ['user', 'contact'],
261
                'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
262
            ],
263
            'language customisations' => [
264
                'singular' => 'language customisation',
265
                'datagenerator' => 'customlang',
266
                'required' => ['component', 'stringid', 'value'],
267
            ],
268
            'language packs' => [
269
                'singular' => 'language pack',
270
                'datagenerator' => 'langpack',
271
                'required' => ['language'],
272
            ],
273
            'analytics models' => [
274
                'singular' => 'analytics model',
275
                'datagenerator' => 'analytics_model',
276
                'required' => ['target', 'indicators', 'timesplitting', 'enabled'],
277
            ],
278
            'user preferences' => [
279
                'singular' => 'user preference',
280
                'datagenerator' => 'user_preferences',
281
                'required' => array('user', 'preference', 'value'),
282
                'switchids' => array('user' => 'userid'),
283
            ],
284
            'contentbank contents' => [
285
                'singular' => 'contentbank content',
286
                'datagenerator' => 'contentbank_content',
287
                'required' => array('contextlevel', 'reference', 'contenttype', 'user', 'contentname'),
288
                'switchids' => array('user' => 'userid')
289
            ],
290
            'user private files' => [
291
                'singular' => 'user private file',
292
                'datagenerator' => 'user_private_files',
11 efrain 293
                'required' => ['user', 'filepath'],
1 efrain 294
                'switchids' => ['user' => 'userid']
295
            ],
296
            'badge external backpacks' => [
297
                'singular' => 'badge external backpack',
298
                'datagenerator' => 'badge_external_backpack',
299
                'required' => ['backpackapiurl', 'backpackweburl', 'apiversion']
300
            ],
301
            'setup backpacks connected' => [
302
                'singular' => 'setup backpack connected',
303
                'datagenerator' => 'setup_backpack_connected',
304
                'required' => ['user', 'externalbackpack'],
305
                'switchids' => ['user' => 'userid', 'externalbackpack' => 'externalbackpackid']
306
            ],
307
            'last access times' => [
308
                'singular' => 'last access time',
309
                'datagenerator' => 'last_access_times',
310
                'required' => ['user', 'course', 'lastaccess'],
311
                'switchids' => ['user' => 'userid', 'course' => 'courseid'],
312
            ],
313
            'notifications' => [
314
                'singular' => 'notification',
315
                'datagenerator' => 'notification',
316
                'required' => ['subject', 'userfrom', 'userto'],
317
                'switchids' => ['userfrom' => 'userfromid', 'userto' => 'usertoid'],
318
            ],
1441 ariadna 319
            'stored progress bars' => [
320
                'singular' => 'stored progress bar',
321
                'datagenerator' => 'stored_progress_bar',
322
                'required' => ['idnumber'],
323
            ],
1 efrain 324
        ];
325
 
326
        return $entities;
327
    }
328
 
329
    /**
330
     * Get the grade item id using a name.
331
     *
332
     * @param string $name
333
     * @return int The grade item id
334
     */
335
    protected function get_gradeitem_id(string $name): int {
336
        global $DB;
337
 
338
        if (!$id = $DB->get_field('grade_items', 'id', ['itemname' => $name])) {
339
            throw new Exception('The specified grade item with name "' . $name . '" could not be found.');
340
        }
341
 
342
        return $id;
343
    }
344
 
345
    /**
346
     * Remove any empty custom fields, to avoid errors when creating the course.
347
     *
348
     * @param array $data
349
     * @return array
350
     */
351
    protected function preprocess_course($data) {
352
        foreach ($data as $fieldname => $value) {
353
            if ($value === '' && strpos($fieldname, 'customfield_') === 0) {
354
                unset($data[$fieldname]);
355
            }
356
        }
357
        return $data;
358
    }
359
 
360
    /**
361
     * If password is not set it uses the username.
362
     *
363
     * @param array $data
364
     * @return array
365
     */
366
    protected function preprocess_user($data) {
367
        if (!isset($data['password'])) {
368
            $data['password'] = $data['username'];
369
        }
370
        return $data;
371
    }
372
 
373
    /**
374
     * If contextlevel and reference are specified for cohort, transform them to the contextid.
375
     *
376
     * @param array $data
377
     * @return array
378
     */
379
    protected function preprocess_cohort($data) {
380
        if (isset($data['contextlevel'])) {
381
            if (!isset($data['reference'])) {
382
                throw new Exception('If field contextlevel is specified, field reference must also be present');
383
            }
384
            $context = $this->get_context($data['contextlevel'], $data['reference']);
385
            unset($data['contextlevel']);
386
            unset($data['reference']);
387
            $data['contextid'] = $context->id;
388
        }
389
        return $data;
390
    }
391
 
392
    /**
393
     * Preprocesses the creation of a grade item. Converts gradetype text to a number.
394
     *
395
     * @param array $data
396
     * @return array
397
     */
398
    protected function preprocess_grade_item($data) {
399
        global $CFG;
400
        require_once("$CFG->libdir/grade/constants.php");
401
 
402
        if (isset($data['gradetype'])) {
403
            $data['gradetype'] = constant("GRADE_TYPE_" . strtoupper($data['gradetype']));
404
        }
405
 
406
        if (!empty($data['category']) && !empty($data['courseid'])) {
407
            $cat = grade_category::fetch(array('fullname' => $data['category'], 'courseid' => $data['courseid']));
408
            if (!$cat) {
409
                throw new Exception('Could not resolve category with name "' . $data['category'] . '"');
410
            }
411
            unset($data['category']);
412
            $data['categoryid'] = $cat->id;
413
        }
414
 
415
        // We need to ensure that all these attributes coming from data are not-localised floats.
416
        $attrs = [
417
            'grademax',
418
            'grademin',
419
            'gradepass',
420
            'multfactor',
421
            'plusfactor',
422
            'aggregationcoef',
423
            'aggregationcoef2',
424
        ];
425
        foreach ($attrs as $attr) {
426
            if (array_key_exists($attr, $data)) {
427
                $data[$attr] = unformat_float($data[$attr]);
428
            }
429
        }
430
 
431
        return $data;
432
    }
433
 
434
    /**
435
     * Adapter to modules generator.
436
     *
437
     * @throws Exception Custom exception for test writers
438
     * @param array $data
439
     * @return void
440
     */
441
    protected function process_activity($data) {
442
        global $DB, $CFG;
443
 
444
        // The the_following_exists() method checks that the field exists.
445
        $activityname = $data['activity'];
446
        unset($data['activity']);
447
 
448
        // Convert scale name into scale id (negative number indicates using scale).
449
        if (isset($data['grade']) && strlen($data['grade']) && !is_number($data['grade'])) {
450
            $data['grade'] = - $this->get_scale_id($data['grade']);
451
            require_once("$CFG->libdir/grade/constants.php");
452
 
453
            if (!isset($data['gradetype'])) {
454
                $data['gradetype'] = GRADE_TYPE_SCALE;
455
            }
456
        }
457
 
458
        if (!array_key_exists('idnumber', $data)) {
459
            $data['idnumber'] = $data['name'];
460
            if (strlen($data['name']) > 100) {
461
                throw new Exception(
462
                    "Activity '{$activityname}' cannot be used as the default idnumber. " .
463
                    "The idnumber has a max length of 100 chars. " .
464
                    "Please manually specify an idnumber."
465
                );
466
            }
467
        }
468
 
469
        // We split $data in the activity $record and the course module $options.
470
        $cmoptions = array();
471
        $cmcolumns = $DB->get_columns('course_modules');
472
        foreach ($cmcolumns as $key => $value) {
473
            if (isset($data[$key])) {
474
                $cmoptions[$key] = $data[$key];
475
            }
476
        }
477
 
478
        $this->datagenerator->create_module($activityname, $data, $cmoptions);
479
    }
480
 
481
    /**
482
     * Add a block to a page.
483
     *
484
     * @param array $data should mostly match the fields of the block_instances table.
485
     *     The block type is specified by blockname.
486
     *     The parentcontextid is set from contextlevel and reference.
487
     *     Missing values are filled in by testing_block_generator::prepare_record.
488
     *     $data is passed to create_block as both $record and $options. Normally
489
     *     the keys are different, so this is a way to let people set values in either place.
490
     */
491
    protected function process_block_instance($data) {
492
 
493
        if (empty($data['blockname'])) {
494
            throw new Exception('\'blocks\' requires the field \'block\' type to be specified');
495
        }
496
 
497
        if (empty($data['contextlevel'])) {
498
            throw new Exception('\'blocks\' requires the field \'contextlevel\' to be specified');
499
        }
500
 
501
        if (!isset($data['reference'])) {
502
            throw new Exception('\'blocks\' requires the field \'reference\' to be specified');
503
        }
504
 
505
        $context = $this->get_context($data['contextlevel'], $data['reference']);
506
        $data['parentcontextid'] = $context->id;
507
 
508
        // Pass $data as both $record and $options. I think that is unlikely to
509
        // cause problems since the relevant key names are different.
510
        // $options is not used in most blocks I have seen, but where it is, it is necessary.
511
        $this->datagenerator->create_block($data['blockname'], $data, $data);
512
    }
513
 
514
    /**
515
     * Creates language customisation.
516
     *
517
     * @throws Exception
518
     * @throws dml_exception
519
     * @param array $data
520
     * @return void
521
     */
522
    protected function process_customlang($data) {
523
        global $CFG, $DB, $USER;
524
 
525
        require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/customlang/locallib.php');
526
        require_once($CFG->libdir . '/adminlib.php');
527
 
528
        if (empty($data['component'])) {
529
            throw new Exception('\'customlang\' requires the field \'component\' type to be specified');
530
        }
531
 
532
        if (empty($data['stringid'])) {
533
            throw new Exception('\'customlang\' requires the field \'stringid\' to be specified');
534
        }
535
 
536
        if (!isset($data['value'])) {
537
            throw new Exception('\'customlang\' requires the field \'value\' to be specified');
538
        }
539
 
540
        $now = time();
541
 
542
        tool_customlang_utils::checkout($USER->lang);
543
 
544
        $record = $DB->get_record_sql("SELECT s.*
545
                                         FROM {tool_customlang} s
546
                                         JOIN {tool_customlang_components} c ON s.componentid = c.id
547
                                        WHERE c.name = ? AND s.lang = ? AND s.stringid = ?",
548
                array($data['component'], $USER->lang, $data['stringid']));
549
 
550
        if (empty($data['value']) && !is_null($record->local)) {
551
            $record->local = null;
552
            $record->modified = 1;
553
            $record->outdated = 0;
554
            $record->timecustomized = null;
555
            $DB->update_record('tool_customlang', $record);
556
            tool_customlang_utils::checkin($USER->lang);
557
        }
558
 
559
        if (!empty($data['value']) && $data['value'] != $record->local) {
560
            $record->local = $data['value'];
561
            $record->modified = 1;
562
            $record->outdated = 0;
563
            $record->timecustomized = $now;
564
            $DB->update_record('tool_customlang', $record);
565
            tool_customlang_utils::checkin($USER->lang);
566
        }
567
    }
568
    /**
569
     * Imports a langpack.
570
     *
571
     * @param array $data
572
     */
573
    protected function process_langpack($data) {
574
        $controller = new \tool_langimport\controller();
575
        $controller->install_languagepacks($data['language']);
576
        get_string_manager()->reset_caches();
577
    }
578
 
579
    /**
580
     * Adapter to enrol_user() data generator.
581
     *
582
     * @throws Exception
583
     * @param array $data
584
     * @return void
585
     */
586
    protected function process_enrol_user($data) {
587
        global $SITE;
588
 
589
        if (empty($data['roleid'])) {
590
            throw new Exception('\'course enrolments\' requires the field \'role\' to be specified');
591
        }
592
 
593
        if (!isset($data['userid'])) {
594
            throw new Exception('\'course enrolments\' requires the field \'user\' to be specified');
595
        }
596
 
597
        if (!isset($data['courseid'])) {
598
            throw new Exception('\'course enrolments\' requires the field \'course\' to be specified');
599
        }
600
 
601
        if (!isset($data['enrol'])) {
602
            $data['enrol'] = 'manual';
603
        }
604
 
605
        if (!isset($data['timestart'])) {
606
            $data['timestart'] = 0;
607
        }
608
 
609
        if (!isset($data['timeend'])) {
610
            $data['timeend'] = 0;
611
        }
612
 
613
        if (!isset($data['status'])) {
614
            $data['status'] = null;
615
        } else {
616
            $status = strtolower($data['status']);
617
            switch ($status) {
618
                case 'active':
619
                    $data['status'] = ENROL_USER_ACTIVE;
620
                    break;
621
                case 'suspended':
622
                    $data['status'] = ENROL_USER_SUSPENDED;
623
                    break;
624
            }
625
        }
626
 
627
        // If the provided course shortname is the site shortname we consider it a system role assign.
628
        if ($data['courseid'] == $SITE->id) {
629
            // Frontpage course assign.
630
            $context = context_course::instance($data['courseid']);
631
            role_assign($data['roleid'], $data['userid'], $context->id);
632
 
633
        } else {
634
            // Course assign.
635
            $this->datagenerator->enrol_user($data['userid'], $data['courseid'], $data['roleid'], $data['enrol'],
636
                    $data['timestart'], $data['timeend'], $data['status']);
637
        }
638
 
639
    }
640
 
641
    /**
642
     * Allows/denies a capability at the specified context
643
     *
644
     * @throws Exception
645
     * @param array $data
646
     * @return void
647
     */
648
    protected function process_permission_override($data) {
649
 
650
        // Will throw an exception if it does not exist.
651
        $context = $this->get_context($data['contextlevel'], $data['reference']);
652
 
653
        switch ($data['permission']) {
654
            case get_string('allow', 'role'):
655
                $permission = CAP_ALLOW;
656
                break;
657
            case get_string('prevent', 'role'):
658
                $permission = CAP_PREVENT;
659
                break;
660
            case get_string('prohibit', 'role'):
661
                $permission = CAP_PROHIBIT;
662
                break;
663
            default:
664
                throw new Exception('The \'' . $data['permission'] . '\' permission does not exist');
665
                break;
666
        }
667
 
668
        if (is_null(get_capability_info($data['capability']))) {
669
            throw new Exception('The \'' . $data['capability'] . '\' capability does not exist');
670
        }
671
 
672
        role_change_permission($data['roleid'], $context, $data['capability'], $permission);
673
    }
674
 
675
    /**
676
     * Assigns a role to a user at system context
677
     *
678
     * Used by "system role assigns" can be deleted when
679
     * system role assign will be deprecated in favour of
680
     * "role assigns"
681
     *
682
     * @throws Exception
683
     * @param array $data
684
     * @return void
685
     */
686
    protected function process_system_role_assign($data) {
687
 
688
        if (empty($data['roleid'])) {
689
            throw new Exception('\'system role assigns\' requires the field \'role\' to be specified');
690
        }
691
 
692
        if (!isset($data['userid'])) {
693
            throw new Exception('\'system role assigns\' requires the field \'user\' to be specified');
694
        }
695
 
696
        $context = context_system::instance();
697
 
698
        $this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
699
    }
700
 
701
    /**
702
     * Assigns a role to a user at the specified context
703
     *
704
     * @throws Exception
705
     * @param array $data
706
     * @return void
707
     */
708
    protected function process_role_assign($data) {
709
 
710
        if (empty($data['roleid'])) {
711
            throw new Exception('\'role assigns\' requires the field \'role\' to be specified');
712
        }
713
 
714
        if (!isset($data['userid'])) {
715
            throw new Exception('\'role assigns\' requires the field \'user\' to be specified');
716
        }
717
 
718
        if (empty($data['contextlevel'])) {
719
            throw new Exception('\'role assigns\' requires the field \'contextlevel\' to be specified');
720
        }
721
 
722
        if (!isset($data['reference'])) {
723
            throw new Exception('\'role assigns\' requires the field \'reference\' to be specified');
724
        }
725
 
726
        // Getting the context id.
727
        $context = $this->get_context($data['contextlevel'], $data['reference']);
728
 
729
        $this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
730
    }
731
 
732
    /**
733
     * Creates a role.
734
     *
735
     * @param array $data
736
     * @return void
737
     */
738
    protected function process_role($data) {
739
 
740
        // We require the user to fill the role shortname.
741
        if (empty($data['shortname'])) {
742
            throw new Exception('\'role\' requires the field \'shortname\' to be specified');
743
        }
744
 
745
        $this->datagenerator->create_role($data);
746
    }
747
 
748
    /**
749
     * Assign capabilities to a role.
750
     *
751
     * @param array $data
752
     */
753
    protected function process_role_capability($data): void {
754
        // We require the user to fill the role shortname.
755
        if (empty($data['roleid'])) {
756
            throw new Exception('\'role capability\' requires the field \'roleid\' to be specified');
757
        }
758
 
759
        $roleid = $data['roleid'];
760
        unset($data['roleid']);
761
 
762
        $this->datagenerator->create_role_capability($roleid, $data, \context_system::instance());
763
    }
764
 
765
    /**
766
     * Adds members to cohorts
767
     *
768
     * @param array $data
769
     * @return void
770
     */
771
    protected function process_cohort_member($data) {
772
        cohort_add_member($data['cohortid'], $data['userid']);
773
    }
774
 
775
    /**
776
     * Create a question category.
777
     *
778
     * @param array $data the row of data from the behat script.
779
     */
780
    protected function process_question_category($data) {
781
        global $DB;
782
 
783
        $context = $this->get_context($data['contextlevel'], $data['reference']);
784
 
785
        // The way this class works, we have already looked up the given parent category
786
        // name and found a matching category. However, it is possible, particularly
787
        // for the 'top' category, for there to be several categories with the
788
        // same name. So far one will have been picked at random, but we need
789
        // the one from the right context. So, if we have the wrong category, try again.
790
        // (Just fixing it here, rather than getting it right first time, is a bit
791
        // of a bodge, but in general this class assumes that names are unique,
792
        // and normally they are, so this was the easiest fix.)
793
        if (!empty($data['parent'])) {
794
            $foundparent = $DB->get_record('question_categories', ['id' => $data['parent']], '*', MUST_EXIST);
795
            if ($foundparent->contextid != $context->id) {
796
                $rightparentid = $DB->get_field('question_categories', 'id',
797
                        ['contextid' => $context->id, 'name' => $foundparent->name]);
798
                if (!$rightparentid) {
799
                    throw new Exception('The specified question category with name "' . $foundparent->name .
800
                            '" does not exist in context "' . $context->get_context_name() . '"."');
801
                }
802
                $data['parent'] = $rightparentid;
803
            }
804
        }
805
 
806
        $data['contextid'] = $context->id;
807
        /** @var core_question_generator $qgenerator */
808
        $qgenerator = $this->datagenerator->get_plugin_generator('core_question');
809
        $qgenerator->create_question_category($data);
810
    }
811
 
812
    /**
813
     * Create a question.
814
     *
815
     * Creating questions relies on the question/type/.../tests/helper.php mechanism.
816
     * We start with test_question_maker::get_question_form_data($data['qtype'], $data['template'])
817
     * and then overlay the values from any other fields of $data that are set.
818
     *
819
     * There is a special case that allows you to set qtype to 'missingtype'.
820
     * This creates an example of broken question, such as you might get if you
821
     * install a question type, create some questions of that type, and then
822
     * uninstall the question type (which is prevented through the UI but can
823
     * still happen). This special lets tests verify that these questions are
824
     * handled OK.
825
     *
826
     * @param array $data the row of data from the behat script.
827
     */
828
    protected function process_question($data) {
829
        global $DB;
830
 
831
        if (array_key_exists('questiontext', $data)) {
832
            $data['questiontext'] = array(
833
                    'text'   => $data['questiontext'],
834
                    'format' => FORMAT_HTML,
835
            );
836
        }
837
 
838
        if (array_key_exists('generalfeedback', $data)) {
839
            $data['generalfeedback'] = array(
840
                    'text'   => $data['generalfeedback'],
841
                    'format' => FORMAT_HTML,
842
            );
843
        }
844
 
845
        $which = null;
846
        if (!empty($data['template'])) {
847
            $which = $data['template'];
848
        }
849
 
850
        $missingtypespecialcase = false;
851
        if ($data['qtype'] === 'missingtype') {
852
            $data['qtype'] = 'essay'; // Actual type uses here does not matter. We just need any question.
853
            $missingtypespecialcase = true;
854
        }
855
 
856
        /** @var core_question_generator $qgenerator */
857
        $qgenerator = $this->datagenerator->get_plugin_generator('core_question');
858
        $questiondata = $qgenerator
859
            ->create_question($data['qtype'], $which, $data);
860
 
861
        if ($missingtypespecialcase) {
862
            $DB->set_field('question', 'qtype', 'unknownqtype', ['id' => $questiondata->id]);
863
        }
864
    }
865
 
866
    /**
867
     * Adds user to contacts
868
     *
869
     * @param array $data
870
     * @return void
871
     */
872
    protected function process_message_contacts($data) {
873
        \core_message\api::add_contact($data['userid'], $data['contactid']);
874
    }
875
 
876
    /**
877
     * Send a new message from user to contact in a private conversation
878
     *
879
     * @param array $data
880
     * @return void
881
     */
882
    protected function process_private_messages(array $data) {
883
        if (empty($data['format'])) {
884
            $data['format'] = 'FORMAT_PLAIN';
885
        }
886
 
887
        if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
888
            $conversation = \core_message\api::create_conversation(
889
                    \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
890
                    [$data['userid'], $data['contactid']]
891
            );
892
            $conversationid = $conversation->id;
893
        }
894
        \core_message\api::send_message_to_conversation(
895
                $data['userid'],
896
                $conversationid,
897
                $data['message'],
898
                constant($data['format'])
899
        );
900
    }
901
 
902
    /**
903
     * Send a new message from user to a group conversation
904
     *
905
     * @param array $data
906
     * @return void
907
     */
908
    protected function process_group_messages(array $data) {
909
        global $DB;
910
 
911
        if (empty($data['format'])) {
912
            $data['format'] = 'FORMAT_PLAIN';
913
        }
914
 
915
        $group = $DB->get_record('groups', ['id' => $data['groupid']]);
916
        $coursecontext = context_course::instance($group->courseid);
917
        if (!$conversation = \core_message\api::get_conversation_by_area('core_group', 'groups', $data['groupid'],
918
                $coursecontext->id)) {
919
            $members = $DB->get_records_menu('groups_members', ['groupid' => $data['groupid']], '', 'userid, id');
920
            $conversation = \core_message\api::create_conversation(
921
                    \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
922
                    array_keys($members),
923
                    $group->name,
924
                    \core_message\api::MESSAGE_CONVERSATION_ENABLED,
925
                    'core_group',
926
                    'groups',
927
                    $group->id,
928
                    $coursecontext->id);
929
        }
930
        \core_message\api::send_message_to_conversation(
931
                $data['userid'],
932
                $conversation->id,
933
                $data['message'],
934
                constant($data['format'])
935
        );
936
    }
937
 
938
    /**
939
     * Mark a private conversation as favourite for user
940
     *
941
     * @param array $data
942
     * @return void
943
     */
944
    protected function process_favourite_conversations(array $data) {
945
        if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
946
            $conversation = \core_message\api::create_conversation(
947
                    \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
948
                    [$data['userid'], $data['contactid']]
949
            );
950
            $conversationid = $conversation->id;
951
        }
952
        \core_message\api::set_favourite_conversation($conversationid, $data['userid']);
953
    }
954
 
955
    /**
956
     * Mute an existing group conversation for user
957
     *
958
     * @param array $data
959
     * @return void
960
     */
961
    protected function process_mute_group_conversations(array $data) {
962
        if (groups_is_member($data['groupid'], $data['userid'])) {
963
            $context = context_course::instance($data['courseid']);
964
            $conversation = \core_message\api::get_conversation_by_area(
965
                    'core_group',
966
                    'groups',
967
                    $data['groupid'],
968
                    $context->id
969
            );
970
            if ($conversation) {
971
                \core_message\api::mute_conversation($data['userid'], $conversation->id);
972
            }
973
        }
974
    }
975
 
976
    /**
977
     * Mute a private conversation for user
978
     *
979
     * @param array $data
980
     * @return void
981
     */
982
    protected function process_mute_private_conversations(array $data) {
983
        if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
984
            $conversation = \core_message\api::create_conversation(
985
                    \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
986
                    [$data['userid'], $data['contactid']]
987
            );
988
            $conversationid = $conversation->id;
989
        }
990
        \core_message\api::mute_conversation($data['userid'], $conversationid);
991
    }
992
 
993
    /**
994
     * Transform indicators string into array.
995
     *
996
     * @param array $data
997
     * @return array
998
     */
999
    protected function preprocess_analytics_model($data) {
1000
        $data['indicators'] = explode(',', $data['indicators']);
1001
        return $data;
1002
    }
1003
 
1004
    /**
1005
     * Creates an analytics model
1006
     *
1007
     * @param array $data target
1008
     * @return void
1009
     */
1010
    protected function process_analytics_model($data) {
1011
        \core_analytics\manager::create_declared_model($data);
1012
    }
1013
 
1014
    /**
1015
     * Set a preference value for user
1016
     *
1017
     * @param array $data
1018
     * @return void
1019
     */
1020
    protected function process_user_preferences(array $data) {
1021
        set_user_preference($data['preference'], $data['value'], $data['userid']);
1022
    }
1023
 
1024
    /**
1025
     * Create content in the given context's content bank.
1026
     *
1027
     * @param array $data
1028
     * @return void
1029
     */
1030
    protected function process_contentbank_content(array $data) {
1031
        global $CFG;
1032
 
1033
        if (empty($data['contextlevel'])) {
1034
            throw new Exception('contentbank_content requires the field contextlevel to be specified');
1035
        }
1036
 
1037
        if (!isset($data['reference'])) {
1038
            throw new Exception('contentbank_content requires the field reference to be specified');
1039
        }
1040
 
1041
        if (empty($data['contenttype'])) {
1042
            throw new Exception('contentbank_content requires the field contenttype to be specified');
1043
        }
1044
 
1045
        $contenttypeclass = "\\".$data['contenttype']."\\contenttype";
1046
        if (class_exists($contenttypeclass)) {
1047
            $context = $this->get_context($data['contextlevel'], $data['reference']);
1048
            $contenttype = new $contenttypeclass($context);
1049
            $record = new stdClass();
1050
            $record->usercreated = $data['userid'];
1051
            $record->name = $data['contentname'];
1052
            if (isset($data['visibility'])) {
1053
                $record->visibility = $data['visibility'];
1054
            }
1055
            $content = $contenttype->create_content($record);
1056
 
1057
            if (!empty($data['filepath'])) {
1058
                $filename = basename($data['filepath']);
1059
                $fs = get_file_storage();
11 efrain 1060
                $filerecord = [
1 efrain 1061
                    'component' => 'contentbank',
1062
                    'filearea' => 'public',
1063
                    'contextid' => $context->id,
1064
                    'userid' => $data['userid'],
1065
                    'itemid' => $content->get_id(),
1066
                    'filename' => $filename,
11 efrain 1067
                    'filepath' => '/',
1068
                ];
1 efrain 1069
                $fs->create_file_from_pathname($filerecord, $CFG->dirroot . $data['filepath']);
1070
            }
1071
        } else {
1072
            throw new Exception('The specified "' . $data['contenttype'] . '" contenttype does not exist');
1073
        }
1074
    }
1075
 
1076
    /**
1077
     * Create content in the given user's private files.
1078
     *
1079
     * @param array $data
1080
     * @return void
1081
     */
1082
    protected function process_user_private_files(array $data) {
1083
        global $CFG;
1084
 
1085
        $userid = $data['userid'];
1086
        $fs = get_file_storage();
1087
        $filepath = "{$CFG->dirroot}/{$data['filepath']}";
1088
 
1089
        if (!file_exists($filepath)) {
1090
            throw new coding_exception("File '{$filepath}' does not exist");
1091
        }
1092
        $filerecord = [
1093
            'userid' => $userid,
1094
            'contextid' => context_user::instance($userid)->id,
1095
            'component' => 'user',
1096
            'filearea' => 'private',
1097
            'itemid' => 0,
1098
            'filepath'  => '/',
1099
            'filename'  => basename($filepath),
1100
        ];
1101
        $fs->create_file_from_pathname($filerecord, $filepath);
1102
    }
1103
 
1104
    /**
1105
     * Create a exetrnal backpack.
1106
     *
1107
     * @param array $data
1108
     */
1109
    protected function process_badge_external_backpack(array $data) {
1110
        global $DB;
1111
        $DB->insert_record('badge_external_backpack', $data, true);
1112
    }
1113
 
1114
    /**
1115
     * Setup a backpack connected for user.
1116
     *
1117
     * @param array $data
1118
     * @throws dml_exception
1119
     */
1120
    protected function process_setup_backpack_connected(array $data) {
1121
        global $DB;
1122
 
1123
        if (empty($data['userid'])) {
1124
            throw new Exception('\'setup backpack connected\' requires the field \'user\' to be specified');
1125
        }
1126
        if (empty($data['externalbackpackid'])) {
1127
            throw new Exception('\'setup backpack connected\' requires the field \'externalbackpack\' to be specified');
1128
        }
1129
        // Dummy badge_backpack_oauth2 data.
1130
        $timenow = time();
1131
        $backpackoauth2 = new stdClass();
1132
        $backpackoauth2->usermodified = $data['userid'];
1133
        $backpackoauth2->timecreated = $timenow;
1134
        $backpackoauth2->timemodified = $timenow;
1135
        $backpackoauth2->userid = $data['userid'];
1136
        $backpackoauth2->issuerid = 1;
1137
        $backpackoauth2->externalbackpackid = $data['externalbackpackid'];
1138
        $backpackoauth2->token = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
1139
        $backpackoauth2->refreshtoken = '0123456789abcdefghijk';
1140
        $backpackoauth2->expires = $timenow + 3600;
1141
        $backpackoauth2->scope = 'https://purl.imsglobal.org/spec/ob/v2p1/scope/assertion.create';
1142
        $backpackoauth2->scope .= ' https://purl.imsglobal.org/spec/ob/v2p1/scope/assertion.readonly offline_access';
1143
        $DB->insert_record('badge_backpack_oauth2', $backpackoauth2);
1144
 
1145
        // Dummy badge_backpack data.
1146
        $backpack = new stdClass();
1147
        $backpack->userid = $data['userid'];
1148
        $backpack->email = 'student@behat.moodle';
1149
        $backpack->backpackuid = 0;
1150
        $backpack->autosync = 0;
1151
        $backpack->password = '';
1152
        $backpack->externalbackpackid = $data['externalbackpackid'];
1153
        $DB->insert_record('badge_backpack', $backpack);
1154
    }
1155
 
1156
    /**
1157
     * Creates notifications to specific user.
1158
     *
1159
     * @param array $data
1160
     * @return void
1161
     */
1162
    protected function process_notification(array $data) {
1163
        global $DB;
1164
 
1165
        $notification = new stdClass();
1166
        $notification->useridfrom = $data['userfromid'];
1167
        $notification->useridto = $data['usertoid'];
1168
        $notification->subject = $data['subject'];
1169
        $notification->fullmessage = $data['subject'] . ' description';
1170
        $notification->smallmessage = $data['subject'] . ' description';
1171
        $notification->fullmessagehtml = $data['subject'] . ' description';
1172
 
1173
        if ($data['timecreated'] !== 'null') {
1174
            $notification->timecreated = $data['timecreated'];
1175
        }
1176
 
1177
        if ($data['timeread'] !== 'null') {
1178
            $notification->timeread = $data['timeread'];
1179
        }
1180
 
1181
        if (!empty($data)) {
1182
            $popupnotification = new stdClass();
1183
            $popupnotification->notificationid = $DB->insert_record('notifications', $notification);
1184
            $DB->insert_record('message_popup_notifications', $popupnotification);
1185
        }
1186
 
1187
    }
1188
 
1189
    /**
1190
     * Creates user last access data within given courses.
1191
     *
1192
     * @param array $data
1193
     * @return void
1194
     */
1195
    protected function process_last_access_times(array $data) {
1196
        global $DB;
1197
 
1198
        if (!isset($data['userid'])) {
1441 ariadna 1199
            throw new Exception('\'last access times\' requires the field \'user\' to be specified');
1 efrain 1200
        }
1201
 
1202
        if (!isset($data['courseid'])) {
1441 ariadna 1203
            throw new Exception('\'last access times\' requires the field \'course\' to be specified');
1 efrain 1204
        }
1205
 
1206
        if (!isset($data['lastaccess'])) {
1441 ariadna 1207
            throw new Exception('\'last access times\' requires the field \'lastaccess\' to be specified');
1 efrain 1208
        }
1209
 
1210
        $userdata = [];
1211
        $userdata['old'] = $DB->get_record('user', ['id' => $data['userid']], 'firstaccess, lastaccess, lastlogin, currentlogin');
1212
        $userdata['new'] = [
1213
            'firstaccess' => $userdata['old']->firstaccess,
1214
            'lastaccess' => $userdata['old']->lastaccess,
1215
            'lastlogin' => $userdata['old']->lastlogin,
1216
            'currentlogin' => $userdata['old']->currentlogin,
1217
        ];
1218
 
1219
        // Check for lastaccess data for this course.
1220
        $lastaccessdata = [
1221
            'userid' => $data['userid'],
1222
            'courseid' => $data['courseid'],
1223
        ];
1224
 
1225
        $lastaccessid = $DB->get_field('user_lastaccess', 'id', $lastaccessdata);
1226
 
1227
        $dbdata = (object) $lastaccessdata;
1228
        $dbdata->timeaccess = $data['lastaccess'];
1229
 
1230
        // Set the course last access time.
1231
        if ($lastaccessid) {
1232
            $dbdata->id = $lastaccessid;
1233
            $DB->update_record('user_lastaccess', $dbdata);
1234
        } else {
1235
            $DB->insert_record('user_lastaccess', $dbdata);
1236
        }
1237
 
1238
        // Store changes to other user access times as needed.
1239
 
1240
        // Update first access if this is the user's first login, or this access is earlier than their current first access.
1241
        if (empty($userdata['new']['firstaccess']) ||
1242
                $userdata['new']['firstaccess'] > $data['lastaccess']) {
1243
            $userdata['new']['firstaccess'] = $data['lastaccess'];
1244
        }
1245
 
1246
        // Update last access if it is the user's most recent access.
1247
        if (empty($userdata['new']['lastaccess']) ||
1248
                $userdata['new']['lastaccess'] < $data['lastaccess']) {
1249
            $userdata['new']['lastaccess'] = $data['lastaccess'];
1250
        }
1251
 
1252
        // Update last and current login if it is the user's most recent access.
1253
        if (empty($userdata['new']['lastlogin']) ||
1254
                $userdata['new']['lastlogin'] < $data['lastaccess']) {
1255
            $userdata['new']['lastlogin'] = $data['lastaccess'];
1256
            $userdata['new']['currentlogin'] = $data['lastaccess'];
1257
        }
1258
 
1259
        $updatedata = [];
1260
 
1261
        if ($userdata['new']['firstaccess'] != $userdata['old']->firstaccess) {
1262
            $updatedata['firstaccess'] = $userdata['new']['firstaccess'];
1263
        }
1264
 
1265
        if ($userdata['new']['lastaccess'] != $userdata['old']->lastaccess) {
1266
            $updatedata['lastaccess'] = $userdata['new']['lastaccess'];
1267
        }
1268
 
1269
        if ($userdata['new']['lastlogin'] != $userdata['old']->lastlogin) {
1270
            $updatedata['lastlogin'] = $userdata['new']['lastlogin'];
1271
        }
1272
 
1273
        if ($userdata['new']['currentlogin'] != $userdata['old']->currentlogin) {
1274
            $updatedata['currentlogin'] = $userdata['new']['currentlogin'];
1275
        }
1276
 
1277
        // Only update user access data if there have been any changes.
1278
        if (!empty($updatedata)) {
1279
            $updatedata['id'] = $data['userid'];
1280
            $updatedata = (object) $updatedata;
1281
            $DB->update_record('user', $updatedata);
1282
        }
1283
    }
1284
}