mirrored from git://git.moodle.org/moodle.git
/
data_generator.php
1186 lines (971 loc) · 42.8 KB
/
data_generator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Data generator.
*
* @package core
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Data generator class for unit tests and other tools that need to create fake test sites.
*
* @package core
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testing_data_generator {
/** @var int The number of grade categories created */
protected $gradecategorycounter = 0;
/** @var int The number of grade items created */
protected $gradeitemcounter = 0;
/** @var int The number of grade outcomes created */
protected $gradeoutcomecounter = 0;
protected $usercounter = 0;
protected $categorycount = 0;
protected $cohortcount = 0;
protected $coursecount = 0;
protected $scalecount = 0;
protected $groupcount = 0;
protected $groupingcount = 0;
protected $rolecount = 0;
protected $tagcount = 0;
/** @var array list of plugin generators */
protected $generators = array();
/** @var array lis of common last names */
public $lastnames = array(
'Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Miller', 'Davis', 'García', 'Rodríguez', 'Wilson',
'Müller', 'Schmidt', 'Schneider', 'Fischer', 'Meyer', 'Weber', 'Schulz', 'Wagner', 'Becker', 'Hoffmann',
'Novák', 'Svoboda', 'Novotný', 'Dvořák', 'Černý', 'Procházková', 'Kučerová', 'Veselá', 'Horáková', 'Němcová',
'Смирнов', 'Иванов', 'Кузнецов', 'Соколов', 'Попов', 'Лебедева', 'Козлова', 'Новикова', 'Морозова', 'Петрова',
'王', '李', '张', '刘', '陈', '楊', '黃', '趙', '吳', '周',
'佐藤', '鈴木', '高橋', '田中', '渡辺', '伊藤', '山本', '中村', '小林', '斎藤',
);
/** @var array lis of common first names */
public $firstnames = array(
'Jacob', 'Ethan', 'Michael', 'Jayden', 'William', 'Isabella', 'Sophia', 'Emma', 'Olivia', 'Ava',
'Lukas', 'Leon', 'Luca', 'Timm', 'Paul', 'Leonie', 'Leah', 'Lena', 'Hanna', 'Laura',
'Jakub', 'Jan', 'Tomáš', 'Lukáš', 'Matěj', 'Tereza', 'Eliška', 'Anna', 'Adéla', 'Karolína',
'Даниил', 'Максим', 'Артем', 'Иван', 'Александр', 'София', 'Анастасия', 'Дарья', 'Мария', 'Полина',
'伟', '伟', '芳', '伟', '秀英', '秀英', '娜', '秀英', '伟', '敏',
'翔', '大翔', '拓海', '翔太', '颯太', '陽菜', 'さくら', '美咲', '葵', '美羽',
);
public $loremipsum = <<<EOD
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla non arcu lacinia neque faucibus fringilla. Vivamus porttitor turpis ac leo. Integer in sapien. Nullam eget nisl. Aliquam erat volutpat. Cras elementum. Mauris suscipit, ligula sit amet pharetra semper, nibh ante cursus purus, vel sagittis velit mauris vel metus. Integer malesuada. Nullam lectus justo, vulputate eget mollis sed, tempor sed magna. Mauris elementum mauris vitae tortor. Aliquam erat volutpat.
Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Pellentesque ipsum. Cras pede libero, dapibus nec, pretium sit amet, tempor quis. Aliquam ante. Proin in tellus sit amet nibh dignissim sagittis. Vivamus porttitor turpis ac leo. Duis bibendum, lectus ut viverra rhoncus, dolor nunc faucibus libero, eget facilisis enim ipsum id lacus. In sem justo, commodo ut, suscipit at, pharetra vitae, orci. Aliquam erat volutpat. Nulla est.
Vivamus luctus egestas leo. Aenean fermentum risus id tortor. Mauris dictum facilisis augue. Aliquam erat volutpat. Aliquam ornare wisi eu metus. Aliquam id dolor. Duis condimentum augue id magna semper rutrum. Donec iaculis gravida nulla. Pellentesque ipsum. Etiam dictum tincidunt diam. Quisque tincidunt scelerisque libero. Etiam egestas wisi a erat.
Integer lacinia. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris tincidunt sem sed arcu. Nullam feugiat, turpis at pulvinar vulputate, erat libero tristique tellus, nec bibendum odio risus sit amet ante. Aliquam id dolor. Maecenas sollicitudin. Et harum quidem rerum facilis est et expedita distinctio. Mauris suscipit, ligula sit amet pharetra semper, nibh ante cursus purus, vel sagittis velit mauris vel metus. Nullam dapibus fermentum ipsum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Pellentesque sapien. Duis risus. Mauris elementum mauris vitae tortor. Suspendisse nisl. Integer rutrum, orci vestibulum ullamcorper ultricies, lacus quam ultricies odio, vitae placerat pede sem sit amet enim.
In laoreet, magna id viverra tincidunt, sem odio bibendum justo, vel imperdiet sapien wisi sed libero. Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo. Nullam justo enim, consectetuer nec, ullamcorper ac, vestibulum in, elit. Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? Maecenas lorem. Etiam posuere lacus quis dolor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Curabitur ligula sapien, pulvinar a vestibulum quis, facilisis vel sapien. Nam sed tellus id magna elementum tincidunt. Suspendisse nisl. Vivamus luctus egestas leo. Nulla non arcu lacinia neque faucibus fringilla. Etiam dui sem, fermentum vitae, sagittis id, malesuada in, quam. Etiam dictum tincidunt diam. Etiam commodo dui eget wisi. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo. Duis ante orci, molestie vitae vehicula venenatis, tincidunt ac pede. Pellentesque sapien.
EOD;
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->usercounter = 0;
$this->categorycount = 0;
$this->coursecount = 0;
$this->scalecount = 0;
foreach ($this->generators as $generator) {
$generator->reset();
}
}
/**
* Return generator for given plugin or component.
* @param string $component the component name, e.g. 'mod_forum' or 'core_question'.
* @return component_generator_base or rather an instance of the appropriate subclass.
*/
public function get_plugin_generator($component) {
list($type, $plugin) = core_component::normalize_component($component);
$cleancomponent = $type . '_' . $plugin;
if ($cleancomponent != $component) {
debugging("Please specify the component you want a generator for as " .
"{$cleancomponent}, not {$component}.", DEBUG_DEVELOPER);
$component = $cleancomponent;
}
if (isset($this->generators[$component])) {
return $this->generators[$component];
}
$dir = core_component::get_component_directory($component);
$lib = $dir . '/tests/generator/lib.php';
if (!$dir || !is_readable($lib)) {
throw new coding_exception("Component {$component} does not support " .
"generators yet. Missing tests/generator/lib.php.");
}
include_once($lib);
$classname = $component . '_generator';
if (!class_exists($classname)) {
throw new coding_exception("Component {$component} does not support " .
"data generators yet. Class {$classname} not found.");
}
$this->generators[$component] = new $classname($this);
return $this->generators[$component];
}
/**
* Create a test user
* @param array|stdClass $record
* @param array $options
* @return stdClass user record
*/
public function create_user($record=null, array $options=null) {
global $DB, $CFG;
$this->usercounter++;
$i = $this->usercounter;
$record = (array)$record;
if (!isset($record['auth'])) {
$record['auth'] = 'manual';
}
if (!isset($record['firstname']) and !isset($record['lastname'])) {
$country = rand(0, 5);
$firstname = rand(0, 4);
$lastname = rand(0, 4);
$female = rand(0, 1);
$record['firstname'] = $this->firstnames[($country*10) + $firstname + ($female*5)];
$record['lastname'] = $this->lastnames[($country*10) + $lastname + ($female*5)];
} else if (!isset($record['firstname'])) {
$record['firstname'] = 'Firstname'.$i;
} else if (!isset($record['lastname'])) {
$record['lastname'] = 'Lastname'.$i;
}
if (!isset($record['firstnamephonetic'])) {
$firstnamephonetic = rand(0, 59);
$record['firstnamephonetic'] = $this->firstnames[$firstnamephonetic];
}
if (!isset($record['lastnamephonetic'])) {
$lastnamephonetic = rand(0, 59);
$record['lastnamephonetic'] = $this->lastnames[$lastnamephonetic];
}
if (!isset($record['middlename'])) {
$middlename = rand(0, 59);
$record['middlename'] = $this->firstnames[$middlename];
}
if (!isset($record['alternatename'])) {
$alternatename = rand(0, 59);
$record['alternatename'] = $this->firstnames[$alternatename];
}
if (!isset($record['idnumber'])) {
$record['idnumber'] = '';
}
if (!isset($record['mnethostid'])) {
$record['mnethostid'] = $CFG->mnet_localhost_id;
}
if (!isset($record['username'])) {
$record['username'] = 'username'.$i;
$j = 2;
while ($DB->record_exists('user', array('username'=>$record['username'], 'mnethostid'=>$record['mnethostid']))) {
$record['username'] = 'username'.$i.'_'.$j;
$j++;
}
}
if (isset($record['password'])) {
$record['password'] = hash_internal_user_password($record['password']);
} else {
// The auth plugin may not fully support this,
// but it is still better/faster than hashing random stuff.
$record['password'] = AUTH_PASSWORD_NOT_CACHED;
}
if (!isset($record['email'])) {
$record['email'] = $record['username'].'@example.com';
}
if (!isset($record['confirmed'])) {
$record['confirmed'] = 1;
}
if (!isset($record['lang'])) {
$record['lang'] = 'en';
}
if (!isset($record['maildisplay'])) {
$record['maildisplay'] = $CFG->defaultpreference_maildisplay;
}
if (!isset($record['mailformat'])) {
$record['mailformat'] = $CFG->defaultpreference_mailformat;
}
if (!isset($record['maildigest'])) {
$record['maildigest'] = $CFG->defaultpreference_maildigest;
}
if (!isset($record['autosubscribe'])) {
$record['autosubscribe'] = $CFG->defaultpreference_autosubscribe;
}
if (!isset($record['trackforums'])) {
$record['trackforums'] = $CFG->defaultpreference_trackforums;
}
if (!isset($record['deleted'])) {
$record['deleted'] = 0;
}
if (!isset($record['timecreated'])) {
$record['timecreated'] = time();
}
$record['timemodified'] = $record['timecreated'];
$record['lastip'] = '0.0.0.0';
if ($record['deleted']) {
$delname = $record['email'].'.'.time();
while ($DB->record_exists('user', array('username'=>$delname))) {
$delname++;
}
$record['idnumber'] = '';
$record['email'] = md5($record['username']);
$record['username'] = $delname;
$record['picture'] = 0;
}
$userid = $DB->insert_record('user', $record);
if (!$record['deleted']) {
context_user::instance($userid);
}
$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
if (!$record['deleted'] && isset($record['interests'])) {
require_once($CFG->dirroot . '/user/editlib.php');
if (!is_array($record['interests'])) {
$record['interests'] = preg_split('/\s*,\s*/', trim($record['interests']), -1, PREG_SPLIT_NO_EMPTY);
}
useredit_update_interests($user, $record['interests']);
}
return $user;
}
/**
* Create a test course category
* @param array|stdClass $record
* @param array $options
* @return coursecat course category record
*/
public function create_category($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->libdir/coursecatlib.php");
$this->categorycount++;
$i = $this->categorycount;
$record = (array)$record;
if (!isset($record['name'])) {
$record['name'] = 'Course category '.$i;
}
if (!isset($record['description'])) {
$record['description'] = "Test course category $i\n$this->loremipsum";
}
if (!isset($record['idnumber'])) {
$record['idnumber'] = '';
}
return coursecat::create($record);
}
/**
* Create test cohort.
* @param array|stdClass $record
* @param array $options
* @return stdClass cohort record
*/
public function create_cohort($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/cohort/lib.php");
$this->cohortcount++;
$i = $this->cohortcount;
$record = (array)$record;
if (!isset($record['contextid'])) {
$record['contextid'] = context_system::instance()->id;
}
if (!isset($record['name'])) {
$record['name'] = 'Cohort '.$i;
}
if (!isset($record['idnumber'])) {
$record['idnumber'] = '';
}
if (!isset($record['description'])) {
$record['description'] = "Test cohort $i\n$this->loremipsum";
}
if (!isset($record['descriptionformat'])) {
$record['descriptionformat'] = FORMAT_MOODLE;
}
if (!isset($record['visible'])) {
$record['visible'] = 1;
}
if (!isset($record['component'])) {
$record['component'] = '';
}
$id = cohort_add_cohort((object)$record);
return $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
}
/**
* Create a test course
* @param array|stdClass $record
* @param array $options with keys:
* 'createsections'=>bool precreate all sections
* @return stdClass course record
*/
public function create_course($record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
$this->coursecount++;
$i = $this->coursecount;
$record = (array)$record;
if (!isset($record['fullname'])) {
$record['fullname'] = 'Test course '.$i;
}
if (!isset($record['shortname'])) {
$record['shortname'] = 'tc_'.$i;
}
if (!isset($record['idnumber'])) {
$record['idnumber'] = '';
}
if (!isset($record['format'])) {
$record['format'] = 'topics';
}
if (!isset($record['newsitems'])) {
$record['newsitems'] = 0;
}
if (!isset($record['numsections'])) {
$record['numsections'] = 5;
}
if (!isset($record['summary'])) {
$record['summary'] = "Test course $i\n$this->loremipsum";
}
if (!isset($record['summaryformat'])) {
$record['summaryformat'] = FORMAT_MOODLE;
}
if (!isset($record['category'])) {
$record['category'] = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
}
if (!isset($record['startdate'])) {
$record['startdate'] = usergetmidnight(time());
}
if (isset($record['tags']) && !is_array($record['tags'])) {
$record['tags'] = preg_split('/\s*,\s*/', trim($record['tags']), -1, PREG_SPLIT_NO_EMPTY);
}
if (!empty($options['createsections']) && empty($record['numsections'])) {
// Since Moodle 3.3 function create_course() automatically creates sections if numsections is specified.
// For BC if 'createsections' is given but 'numsections' is not, assume the default value from config.
$record['numsections'] = get_config('moodlecourse', 'numsections');
}
$course = create_course((object)$record);
context_course::instance($course->id);
return $course;
}
/**
* Create course section if does not exist yet
* @param array|stdClass $record must contain 'course' and 'section' attributes
* @param array|null $options
* @return stdClass
* @throws coding_exception
*/
public function create_course_section($record = null, array $options = null) {
global $DB;
$record = (array)$record;
if (empty($record['course'])) {
throw new coding_exception('course must be present in testing_data_generator::create_course_section() $record');
}
if (!isset($record['section'])) {
throw new coding_exception('section must be present in testing_data_generator::create_course_section() $record');
}
course_create_sections_if_missing($record['course'], $record['section']);
return get_fast_modinfo($record['course'])->get_section_info($record['section']);
}
/**
* Create a test block.
*
* The $record passed in becomes the basis for the new row added to the
* block_instances table. You only need to supply the values of interest.
* Any missing values have sensible defaults filled in, and ->blockname will be set based on $blockname.
*
* The $options array provides additional data, not directly related to what
* will be inserted in the block_instance table, which may affect the block
* that is created. The meanings of any data passed here depends on the particular
* type of block being created.
*
* @param string $blockname the type of block to create. E.g. 'html'.
* @param array|stdClass $record forms the basis for the entry to be inserted in the block_instances table.
* @param array $options further, block-specific options to control how the block is created.
* @return stdClass new block_instance record.
*/
public function create_block($blockname, $record=null, array $options=array()) {
$generator = $this->get_plugin_generator('block_'.$blockname);
return $generator->create_instance($record, $options);
}
/**
* Create a test activity module.
*
* The $record should contain the same data that you would call from
* ->get_data() when the mod_[type]_mod_form is submitted, except that you
* only need to supply values of interest. The only required value is
* 'course'. Any missing values will have a sensible default supplied.
*
* The $options array provides additional data, not directly related to what
* would come back from the module edit settings form, which may affect the activity
* that is created. The meanings of any data passed here depends on the particular
* type of activity being created.
*
* @param string $modulename the type of activity to create. E.g. 'forum' or 'quiz'.
* @param array|stdClass $record data, as if from the module edit settings form.
* @param array $options additional data that may affect how the module is created.
* @return stdClass activity record new new record that was just inserted in the table
* like 'forum' or 'quiz', with a ->cmid field added.
*/
public function create_module($modulename, $record=null, array $options=null) {
$generator = $this->get_plugin_generator('mod_'.$modulename);
return $generator->create_instance($record, $options);
}
/**
* Create a test group for the specified course
*
* $record should be either an array or a stdClass containing infomation about the group to create.
* At the very least it needs to contain courseid.
* Default values are added for name, description, and descriptionformat if they are not present.
*
* This function calls groups_create_group() to create the group within the database.
* @see groups_create_group
* @param array|stdClass $record
* @return stdClass group record
*/
public function create_group($record) {
global $DB, $CFG;
require_once($CFG->dirroot . '/group/lib.php');
$this->groupcount++;
$i = $this->groupcount;
$record = (array)$record;
if (empty($record['courseid'])) {
throw new coding_exception('courseid must be present in testing_data_generator::create_group() $record');
}
if (!isset($record['name'])) {
$record['name'] = 'group-' . $i;
}
if (!isset($record['description'])) {
$record['description'] = "Test Group $i\n{$this->loremipsum}";
}
if (!isset($record['descriptionformat'])) {
$record['descriptionformat'] = FORMAT_MOODLE;
}
$id = groups_create_group((object)$record);
return $DB->get_record('groups', array('id'=>$id));
}
/**
* Create a test group member
* @param array|stdClass $record
* @throws coding_exception
* @return boolean
*/
public function create_group_member($record) {
global $DB, $CFG;
require_once($CFG->dirroot . '/group/lib.php');
$record = (array)$record;
if (empty($record['userid'])) {
throw new coding_exception('user must be present in testing_util::create_group_member() $record');
}
if (!isset($record['groupid'])) {
throw new coding_exception('group must be present in testing_util::create_group_member() $record');
}
if (!isset($record['component'])) {
$record['component'] = null;
}
if (!isset($record['itemid'])) {
$record['itemid'] = 0;
}
return groups_add_member($record['groupid'], $record['userid'], $record['component'], $record['itemid']);
}
/**
* Create a test grouping for the specified course
*
* $record should be either an array or a stdClass containing infomation about the grouping to create.
* At the very least it needs to contain courseid.
* Default values are added for name, description, and descriptionformat if they are not present.
*
* This function calls groups_create_grouping() to create the grouping within the database.
* @see groups_create_grouping
* @param array|stdClass $record
* @return stdClass grouping record
*/
public function create_grouping($record) {
global $DB, $CFG;
require_once($CFG->dirroot . '/group/lib.php');
$this->groupingcount++;
$i = $this->groupingcount;
$record = (array)$record;
if (empty($record['courseid'])) {
throw new coding_exception('courseid must be present in testing_data_generator::create_grouping() $record');
}
if (!isset($record['name'])) {
$record['name'] = 'grouping-' . $i;
}
if (!isset($record['description'])) {
$record['description'] = "Test Grouping $i\n{$this->loremipsum}";
}
if (!isset($record['descriptionformat'])) {
$record['descriptionformat'] = FORMAT_MOODLE;
}
$id = groups_create_grouping((object)$record);
return $DB->get_record('groupings', array('id'=>$id));
}
/**
* Create a test grouping group
* @param array|stdClass $record
* @throws coding_exception
* @return boolean
*/
public function create_grouping_group($record) {
global $DB, $CFG;
require_once($CFG->dirroot . '/group/lib.php');
$record = (array)$record;
if (empty($record['groupingid'])) {
throw new coding_exception('grouping must be present in testing::create_grouping_group() $record');
}
if (!isset($record['groupid'])) {
throw new coding_exception('group must be present in testing_util::create_grouping_group() $record');
}
return groups_assign_grouping($record['groupingid'], $record['groupid']);
}
/**
* Create an instance of a repository.
*
* @param string type of repository to create an instance for.
* @param array|stdClass $record data to use to up set the instance.
* @param array $options options
* @return stdClass repository instance record
* @since Moodle 2.5.1
*/
public function create_repository($type, $record=null, array $options = null) {
$generator = $this->get_plugin_generator('repository_'.$type);
return $generator->create_instance($record, $options);
}
/**
* Create an instance of a repository.
*
* @param string type of repository to create an instance for.
* @param array|stdClass $record data to use to up set the instance.
* @param array $options options
* @return repository_type object
* @since Moodle 2.5.1
*/
public function create_repository_type($type, $record=null, array $options = null) {
$generator = $this->get_plugin_generator('repository_'.$type);
return $generator->create_type($record, $options);
}
/**
* Create a test scale
* @param array|stdClass $record
* @param array $options
* @return stdClass block instance record
*/
public function create_scale($record=null, array $options=null) {
global $DB;
$this->scalecount++;
$i = $this->scalecount;
$record = (array)$record;
if (!isset($record['name'])) {
$record['name'] = 'Test scale '.$i;
}
if (!isset($record['scale'])) {
$record['scale'] = 'A,B,C,D,F';
}
if (!isset($record['courseid'])) {
$record['courseid'] = 0;
}
if (!isset($record['userid'])) {
$record['userid'] = 0;
}
if (!isset($record['description'])) {
$record['description'] = 'Test scale description '.$i;
}
if (!isset($record['descriptionformat'])) {
$record['descriptionformat'] = FORMAT_MOODLE;
}
$record['timemodified'] = time();
if (isset($record['id'])) {
$DB->import_record('scale', $record);
$DB->get_manager()->reset_sequence('scale');
$id = $record['id'];
} else {
$id = $DB->insert_record('scale', $record);
}
return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST);
}
/**
* Creates a new role in the system.
*
* You can fill $record with the role 'name',
* 'shortname', 'description' and 'archetype'.
*
* If an archetype is specified it's capabilities,
* context where the role can be assigned and
* all other properties are copied from the archetype;
* if no archetype is specified it will create an
* empty role.
*
* @param array|stdClass $record
* @return int The new role id
*/
public function create_role($record=null) {
global $DB;
$this->rolecount++;
$i = $this->rolecount;
$record = (array)$record;
if (empty($record['shortname'])) {
$record['shortname'] = 'role-' . $i;
}
if (empty($record['name'])) {
$record['name'] = 'Test role ' . $i;
}
if (empty($record['description'])) {
$record['description'] = 'Test role ' . $i . ' description';
}
if (empty($record['archetype'])) {
$record['archetype'] = '';
} else {
$archetypes = get_role_archetypes();
if (empty($archetypes[$record['archetype']])) {
throw new coding_exception('\'role\' requires the field \'archetype\' to specify a ' .
'valid archetype shortname (editingteacher, student...)');
}
}
// Creates the role.
if (!$newroleid = create_role($record['name'], $record['shortname'], $record['description'], $record['archetype'])) {
throw new coding_exception('There was an error creating \'' . $record['shortname'] . '\' role');
}
// If no archetype was specified we allow it to be added to all contexts,
// otherwise we allow it in the archetype contexts.
if (!$record['archetype']) {
$contextlevels = array_keys(context_helper::get_all_levels());
} else {
// Copying from the archetype default rol.
$archetyperoleid = $DB->get_field(
'role',
'id',
array('shortname' => $record['archetype'], 'archetype' => $record['archetype'])
);
$contextlevels = get_role_contextlevels($archetyperoleid);
}
set_role_contextlevels($newroleid, $contextlevels);
if ($record['archetype']) {
// We copy all the roles the archetype can assign, override, switch to and view.
if ($record['archetype']) {
$types = array('assign', 'override', 'switch', 'view');
foreach ($types as $type) {
$rolestocopy = get_default_role_archetype_allows($type, $record['archetype']);
foreach ($rolestocopy as $tocopy) {
$functionname = "core_role_set_{$type}_allowed";
$functionname($newroleid, $tocopy);
}
}
}
// Copying the archetype capabilities.
$sourcerole = $DB->get_record('role', array('id' => $archetyperoleid));
role_cap_duplicate($sourcerole, $newroleid);
}
return $newroleid;
}
/**
* Create a tag.
*
* @param array|stdClass $record
* @return stdClass the tag record
*/
public function create_tag($record = null) {
global $DB, $USER;
$this->tagcount++;
$i = $this->tagcount;
$record = (array) $record;
if (!isset($record['userid'])) {
$record['userid'] = $USER->id;
}
if (!isset($record['rawname'])) {
if (isset($record['name'])) {
$record['rawname'] = $record['name'];
} else {
$record['rawname'] = 'Tag name ' . $i;
}
}
// Attribute 'name' should be a lowercase version of 'rawname', if not set.
if (!isset($record['name'])) {
$record['name'] = core_text::strtolower($record['rawname']);
} else {
$record['name'] = core_text::strtolower($record['name']);
}
if (!isset($record['tagcollid'])) {
$record['tagcollid'] = core_tag_collection::get_default();
}
if (!isset($record['description'])) {
$record['description'] = 'Tag description';
}
if (!isset($record['descriptionformat'])) {
$record['descriptionformat'] = FORMAT_MOODLE;
}
if (!isset($record['flag'])) {
$record['flag'] = 0;
}
if (!isset($record['timemodified'])) {
$record['timemodified'] = time();
}
$id = $DB->insert_record('tag', $record);
return $DB->get_record('tag', array('id' => $id), '*', MUST_EXIST);
}
/**
* Helper method which combines $defaults with the values specified in $record.
* If $record is an object, it is converted to an array.
* Then, for each key that is in $defaults, but not in $record, the value
* from $defaults is copied.
* @param array $defaults the default value for each field with
* @param array|stdClass $record
* @return array updated $record.
*/
public function combine_defaults_and_record(array $defaults, $record) {
$record = (array) $record;
foreach ($defaults as $key => $defaults) {
if (!array_key_exists($key, $record)) {
$record[$key] = $defaults;
}
}
return $record;
}
/**
* Simplified enrolment of user to course using default options.
*
* It is strongly recommended to use only this method for 'manual' and 'self' plugins only!!!
*
* @param int $userid
* @param int $courseid
* @param int|string $roleidorshortname optional role id or role shortname, use only with manual plugin
* @param string $enrol name of enrol plugin,
* there must be exactly one instance in course,
* it must support enrol_user() method.
* @param int $timestart (optional) 0 means unknown
* @param int $timeend (optional) 0 means forever
* @param int $status (optional) default to ENROL_USER_ACTIVE for new enrolments
* @return bool success
*/
public function enrol_user($userid, $courseid, $roleidorshortname = null, $enrol = 'manual',
$timestart = 0, $timeend = 0, $status = null) {
global $DB;
// If role is specified by shortname, convert it into an id.
if (!is_numeric($roleidorshortname) && is_string($roleidorshortname)) {
$roleid = $DB->get_field('role', 'id', array('shortname' => $roleidorshortname), MUST_EXIST);
} else {
$roleid = $roleidorshortname;
}
if (!$plugin = enrol_get_plugin($enrol)) {
return false;
}
$instances = $DB->get_records('enrol', array('courseid'=>$courseid, 'enrol'=>$enrol));
if (count($instances) != 1) {
return false;
}
$instance = reset($instances);
if (is_null($roleid) and $instance->roleid) {
$roleid = $instance->roleid;
}
$plugin->enrol_user($instance, $userid, $roleid, $timestart, $timeend, $status);
return true;
}
/**
* Assigns the specified role to a user in the context.
*
* @param int $roleid
* @param int $userid
* @param int $contextid Defaults to the system context
* @return int new/existing id of the assignment
*/
public function role_assign($roleid, $userid, $contextid = false) {
// Default to the system context.
if (!$contextid) {
$context = context_system::instance();
$contextid = $context->id;
}
if (empty($roleid)) {
throw new coding_exception('roleid must be present in testing_data_generator::role_assign() arguments');
}
if (empty($userid)) {
throw new coding_exception('userid must be present in testing_data_generator::role_assign() arguments');
}
return role_assign($roleid, $userid, $contextid);
}
/**
* Create a grade_category.
*
* @param array|stdClass $record
* @return stdClass the grade category record
*/
public function create_grade_category($record = null) {
global $CFG;
$this->gradecategorycounter++;
$record = (array)$record;
if (empty($record['courseid'])) {
throw new coding_exception('courseid must be present in testing::create_grade_category() $record');
}
if (!isset($record['fullname'])) {
$record['fullname'] = 'Grade category ' . $this->gradecategorycounter;
}
// For gradelib classes.
require_once($CFG->libdir . '/gradelib.php');
// Create new grading category in this course.
$gradecategory = new grade_category(array('courseid' => $record['courseid']), false);
$gradecategory->apply_default_settings();
grade_category::set_properties($gradecategory, $record);
$gradecategory->apply_forced_settings();
$gradecategory->insert();
// This creates a default grade item for the category
$gradeitem = $gradecategory->load_grade_item();
$gradecategory->update_from_db();
return $gradecategory->get_record_data();