MDL-35332: Use fast hash when generating hashes during tests
[moodle.git] / lib / testing / generator / data_generator.php
CommitLineData
7e7cfe7a
PS
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 generator.
19 *
20 * @package core
6b219869 21 * @category test
7e7cfe7a
PS
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27/**
6b219869 28 * Data generator class for unit tests and other tools that need to create fake test sites.
7e7cfe7a
PS
29 *
30 * @package core
6b219869 31 * @category test
7e7cfe7a
PS
32 * @copyright 2012 Petr Skoda {@link http://skodak.org}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
5c3c2c81 35class testing_data_generator {
7e7cfe7a
PS
36 protected $usercounter = 0;
37 protected $categorycount = 0;
4729332b 38 protected $cohortcount = 0;
7e7cfe7a
PS
39 protected $coursecount = 0;
40 protected $scalecount = 0;
41 protected $groupcount = 0;
42 protected $groupingcount = 0;
43
a9a0cb69
MN
44 /**
45 * @var int keep track of how many forum discussions have been created.
46 */
47 protected $forumdiscussioncount = 0;
48
49 /**
50 * @var int keep track of how many forum posts have been created.
51 */
52 protected $forumpostcount = 0;
53
7e7cfe7a
PS
54 /** @var array list of plugin generators */
55 protected $generators = array();
56
57 /** @var array lis of common last names */
58 public $lastnames = array(
59 'Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Miller', 'Davis', 'García', 'Rodríguez', 'Wilson',
60 'Müller', 'Schmidt', 'Schneider', 'Fischer', 'Meyer', 'Weber', 'Schulz', 'Wagner', 'Becker', 'Hoffmann',
61 'Novák', 'Svoboda', 'Novotný', 'Dvořák', 'Černý', 'Procházková', 'Kučerová', 'Veselá', 'Horáková', 'Němcová',
62 'Смирнов', 'Иванов', 'Кузнецов', 'Соколов', 'Попов', 'Лебедева', 'Козлова', 'Новикова', 'Морозова', 'Петрова',
63 '王', '李', '张', '刘', '陈', '楊', '黃', '趙', '吳', '周',
64 '佐藤', '鈴木', '高橋', '田中', '渡辺', '伊藤', '山本', '中村', '小林', '斎藤',
65 );
66
67 /** @var array lis of common first names */
68 public $firstnames = array(
69 'Jacob', 'Ethan', 'Michael', 'Jayden', 'William', 'Isabella', 'Sophia', 'Emma', 'Olivia', 'Ava',
70 'Lukas', 'Leon', 'Luca', 'Timm', 'Paul', 'Leonie', 'Leah', 'Lena', 'Hanna', 'Laura',
71 'Jakub', 'Jan', 'Tomáš', 'Lukáš', 'Matěj', 'Tereza', 'Eliška', 'Anna', 'Adéla', 'Karolína',
72 'Даниил', 'Максим', 'Артем', 'Иван', 'Александр', 'София', 'Анастасия', 'Дарья', 'Мария', 'Полина',
73 '伟', '伟', '芳', '伟', '秀英', '秀英', '娜', '秀英', '伟', '敏',
74 '翔', '大翔', '拓海', '翔太', '颯太', '陽菜', 'さくら', '美咲', '葵', '美羽',
75 );
76
77 public $loremipsum = <<<EOD
78Lorem 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.
79Temporibus 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.
80Vivamus 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.
81Integer 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.
82In 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.
83EOD;
84
85 /**
86 * To be called from data reset code only,
87 * do not use in tests.
88 * @return void
89 */
90 public function reset() {
91 $this->usercounter = 0;
92 $this->categorycount = 0;
93 $this->coursecount = 0;
94 $this->scalecount = 0;
a9a0cb69
MN
95 $this->forumdiscussioncount = 0;
96 $this->forumpostcount = 0;
7e7cfe7a 97
5c3c2c81 98 foreach ($this->generators as $generator) {
7e7cfe7a
PS
99 $generator->reset();
100 }
101 }
102
103 /**
104 * Return generator for given plugin
105 * @param string $component
106 * @return mixed plugin data generator
107 */
108 public function get_plugin_generator($component) {
109 list($type, $plugin) = normalize_component($component);
110
111 if ($type !== 'mod' and $type !== 'block') {
112 throw new coding_exception("Plugin type $type does not support generators yet");
113 }
114
115 $dir = get_plugin_directory($type, $plugin);
116
117 if (!isset($this->generators[$type.'_'.$plugin])) {
118 $lib = "$dir/tests/generator/lib.php";
119 if (!include_once($lib)) {
120 throw new coding_exception("Plugin $component does not support data generator, missing tests/generator/lib");
121 }
122 $classname = $type.'_'.$plugin.'_generator';
123 $this->generators[$type.'_'.$plugin] = new $classname($this);
124 }
125
126 return $this->generators[$type.'_'.$plugin];
127 }
128
129 /**
130 * Create a test user
131 * @param array|stdClass $record
132 * @param array $options
133 * @return stdClass user record
134 */
135 public function create_user($record=null, array $options=null) {
136 global $DB, $CFG;
137
138 $this->usercounter++;
139 $i = $this->usercounter;
140
141 $record = (array)$record;
142
143 if (!isset($record['auth'])) {
144 $record['auth'] = 'manual';
145 }
146
147 if (!isset($record['firstname']) and !isset($record['lastname'])) {
148 $country = rand(0, 5);
149 $firstname = rand(0, 4);
150 $lastname = rand(0, 4);
151 $female = rand(0, 1);
152 $record['firstname'] = $this->firstnames[($country*10) + $firstname + ($female*5)];
153 $record['lastname'] = $this->lastnames[($country*10) + $lastname + ($female*5)];
154
155 } else if (!isset($record['firstname'])) {
156 $record['firstname'] = 'Firstname'.$i;
157
158 } else if (!isset($record['lastname'])) {
159 $record['lastname'] = 'Lastname'.$i;
160 }
161
162 if (!isset($record['idnumber'])) {
163 $record['idnumber'] = '';
164 }
165
166 if (!isset($record['mnethostid'])) {
167 $record['mnethostid'] = $CFG->mnet_localhost_id;
168 }
169
170 if (!isset($record['username'])) {
fe67134e
PS
171 $record['username'] = 'username'.$i;
172 $j = 2;
7e7cfe7a 173 while ($DB->record_exists('user', array('username'=>$record['username'], 'mnethostid'=>$record['mnethostid']))) {
fe67134e
PS
174 $record['username'] = 'username'.$i.'_'.$j;
175 $j++;
7e7cfe7a
PS
176 }
177 }
178
179 if (!isset($record['password'])) {
180 $record['password'] = 'lala';
181 }
182
183 if (!isset($record['email'])) {
184 $record['email'] = $record['username'].'@example.com';
185 }
186
187 if (!isset($record['confirmed'])) {
188 $record['confirmed'] = 1;
189 }
190
191 if (!isset($record['lang'])) {
192 $record['lang'] = 'en';
193 }
194
195 if (!isset($record['maildisplay'])) {
196 $record['maildisplay'] = 1;
197 }
198
199 if (!isset($record['deleted'])) {
200 $record['deleted'] = 0;
201 }
202
203 $record['timecreated'] = time();
204 $record['timemodified'] = $record['timecreated'];
205 $record['lastip'] = '0.0.0.0';
206
5c4ffeb8
SC
207 // Use fast hash during testing.
208 $record['password'] = hash_internal_user_password($record['password'], true);
7e7cfe7a
PS
209
210 if ($record['deleted']) {
211 $delname = $record['email'].'.'.time();
212 while ($DB->record_exists('user', array('username'=>$delname))) {
213 $delname++;
214 }
215 $record['idnumber'] = '';
216 $record['email'] = md5($record['username']);
217 $record['username'] = $delname;
218 $record['picture'] = 0;
219 }
220
221 $userid = $DB->insert_record('user', $record);
222
223 if (!$record['deleted']) {
224 context_user::instance($userid);
225 }
226
227 return $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
228 }
229
230 /**
231 * Create a test course category
232 * @param array|stdClass $record
233 * @param array $options
234 * @return stdClass course category record
235 */
4729332b 236 public function create_category($record=null, array $options=null) {
7e7cfe7a
PS
237 global $DB, $CFG;
238 require_once("$CFG->dirroot/course/lib.php");
239
240 $this->categorycount++;
241 $i = $this->categorycount;
242
243 $record = (array)$record;
244
245 if (!isset($record['name'])) {
246 $record['name'] = 'Course category '.$i;
247 }
248
249 if (!isset($record['idnumber'])) {
250 $record['idnumber'] = '';
251 }
252
253 if (!isset($record['description'])) {
254 $record['description'] = "Test course category $i\n$this->loremipsum";
255 }
256
257 if (!isset($record['descriptionformat'])) {
4a38e659 258 $record['descriptionformat'] = FORMAT_MOODLE;
7e7cfe7a
PS
259 }
260
261 if (!isset($record['parent'])) {
4a38e659 262 $record['parent'] = 0;
7e7cfe7a
PS
263 }
264
265 if (empty($record['parent'])) {
266 $parent = new stdClass();
267 $parent->path = '';
268 $parent->depth = 0;
269 } else {
270 $parent = $DB->get_record('course_categories', array('id'=>$record['parent']), '*', MUST_EXIST);
271 }
272 $record['depth'] = $parent->depth+1;
273
274 $record['sortorder'] = 0;
275 $record['timemodified'] = time();
276 $record['timecreated'] = $record['timemodified'];
277
278 $catid = $DB->insert_record('course_categories', $record);
279 $path = $parent->path . '/' . $catid;
280 $DB->set_field('course_categories', 'path', $path, array('id'=>$catid));
281 context_coursecat::instance($catid);
282
283 fix_course_sortorder();
284
285 return $DB->get_record('course_categories', array('id'=>$catid), '*', MUST_EXIST);
286 }
287
4729332b
PS
288 /**
289 * Create test cohort.
290 * @param array|stdClass $record
291 * @param array $options
292 * @return stdClass cohort record
293 */
294 public function create_cohort($record=null, array $options=null) {
295 global $DB, $CFG;
296 require_once("$CFG->dirroot/cohort/lib.php");
297
298 $this->cohortcount++;
299 $i = $this->cohortcount;
300
301 $record = (array)$record;
302
303 if (!isset($record['contextid'])) {
304 $record['contextid'] = context_system::instance()->id;
305 }
306
307 if (!isset($record['name'])) {
308 $record['name'] = 'Cohort '.$i;
309 }
310
311 if (!isset($record['idnumber'])) {
312 $record['idnumber'] = '';
313 }
314
315 if (!isset($record['description'])) {
316 $record['description'] = "Test cohort $i\n$this->loremipsum";
317 }
318
319 if (!isset($record['descriptionformat'])) {
320 $record['descriptionformat'] = FORMAT_MOODLE;
321 }
322
323 if (!isset($record['component'])) {
324 $record['component'] = '';
325 }
326
327 $id = cohort_add_cohort((object)$record);
328
329 return $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
330 }
331
7e7cfe7a
PS
332 /**
333 * Create a test course
334 * @param array|stdClass $record
335 * @param array $options with keys:
336 * 'createsections'=>bool precreate all sections
337 * @return stdClass course record
338 */
4729332b 339 public function create_course($record=null, array $options=null) {
7e7cfe7a
PS
340 global $DB, $CFG;
341 require_once("$CFG->dirroot/course/lib.php");
342
343 $this->coursecount++;
344 $i = $this->coursecount;
345
346 $record = (array)$record;
347
348 if (!isset($record['fullname'])) {
349 $record['fullname'] = 'Test course '.$i;
350 }
351
352 if (!isset($record['shortname'])) {
353 $record['shortname'] = 'tc_'.$i;
354 }
355
356 if (!isset($record['idnumber'])) {
357 $record['idnumber'] = '';
358 }
359
360 if (!isset($record['format'])) {
361 $record['format'] = 'topics';
362 }
363
364 if (!isset($record['newsitems'])) {
365 $record['newsitems'] = 0;
366 }
367
368 if (!isset($record['numsections'])) {
369 $record['numsections'] = 5;
370 }
371
4a38e659
PS
372 if (!isset($record['summary'])) {
373 $record['summary'] = "Test course $i\n$this->loremipsum";
7e7cfe7a
PS
374 }
375
4a38e659
PS
376 if (!isset($record['summaryformat'])) {
377 $record['summaryformat'] = FORMAT_MOODLE;
7e7cfe7a
PS
378 }
379
380 if (!isset($record['category'])) {
381 $record['category'] = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
382 }
383
384 $course = create_course((object)$record);
385 context_course::instance($course->id);
7e7cfe7a 386 if (!empty($options['createsections'])) {
384c3510
MG
387 if (isset($course->numsections)) {
388 course_create_sections_if_missing($course, range(0, $course->numsections));
389 } else {
390 course_create_sections_if_missing($course, 0);
7e7cfe7a
PS
391 }
392 }
393
394 return $course;
395 }
396
397 /**
398 * Create course section if does not exist yet
384c3510 399 * @param array|stdClass $record must contain 'course' and 'section' attributes
7e7cfe7a
PS
400 * @param array|null $options
401 * @return stdClass
402 * @throws coding_exception
403 */
404 public function create_course_section($record = null, array $options = null) {
405 global $DB;
406
407 $record = (array)$record;
408
409 if (empty($record['course'])) {
5c3c2c81 410 throw new coding_exception('course must be present in testing_data_generator::create_course_section() $record');
7e7cfe7a
PS
411 }
412
413 if (!isset($record['section'])) {
5c3c2c81 414 throw new coding_exception('section must be present in testing_data_generator::create_course_section() $record');
7e7cfe7a
PS
415 }
416
b46be6ad
MG
417 course_create_sections_if_missing($record['course'], $record['section']);
418 return get_fast_modinfo($record['course'])->get_section_info($record['section']);
7e7cfe7a
PS
419 }
420
421 /**
422 * Create a test block
423 * @param string $blockname
424 * @param array|stdClass $record
425 * @param array $options
426 * @return stdClass block instance record
427 */
428 public function create_block($blockname, $record=null, array $options=null) {
429 $generator = $this->get_plugin_generator('block_'.$blockname);
430 return $generator->create_instance($record, $options);
431 }
432
433 /**
434 * Create a test module
435 * @param string $modulename
436 * @param array|stdClass $record
437 * @param array $options
438 * @return stdClass activity record
439 */
440 public function create_module($modulename, $record=null, array $options=null) {
441 $generator = $this->get_plugin_generator('mod_'.$modulename);
442 return $generator->create_instance($record, $options);
443 }
444
445 /**
446 * Create a test group for the specified course
447 *
448 * $record should be either an array or a stdClass containing infomation about the group to create.
449 * At the very least it needs to contain courseid.
450 * Default values are added for name, description, and descriptionformat if they are not present.
451 *
6b219869
DM
452 * This function calls groups_create_group() to create the group within the database.
453 * @see groups_create_group
7e7cfe7a
PS
454 * @param array|stdClass $record
455 * @return stdClass group record
456 */
457 public function create_group($record) {
458 global $DB, $CFG;
459
460 require_once($CFG->dirroot . '/group/lib.php');
461
462 $this->groupcount++;
463 $i = $this->groupcount;
464
465 $record = (array)$record;
466
467 if (empty($record['courseid'])) {
5c3c2c81 468 throw new coding_exception('courseid must be present in testing_data_generator::create_group() $record');
7e7cfe7a
PS
469 }
470
471 if (!isset($record['name'])) {
472 $record['name'] = 'group-' . $i;
473 }
474
475 if (!isset($record['description'])) {
476 $record['description'] = "Test Group $i\n{$this->loremipsum}";
477 }
478
479 if (!isset($record['descriptionformat'])) {
480 $record['descriptionformat'] = FORMAT_MOODLE;
481 }
482
483 $id = groups_create_group((object)$record);
484
485 return $DB->get_record('groups', array('id'=>$id));
486 }
487
87bb583c
DM
488 /**
489 * Create a test group member
490 * @param array|stdClass $record
491 * @throws coding_exception
492 * @return boolean
493 */
494 public function create_group_member($record) {
495 global $DB, $CFG;
496
497 require_once($CFG->dirroot . '/group/lib.php');
498
499 $record = (array)$record;
500
501 if (empty($record['userid'])) {
502 throw new coding_exception('user must be present in testing_util::create_group_member() $record');
503 }
504
505 if (!isset($record['groupid'])) {
506 throw new coding_exception('group must be present in testing_util::create_group_member() $record');
507 }
508
509 if (!isset($record['component'])) {
510 $record['component'] = null;
511 }
512 if (!isset($record['itemid'])) {
513 $record['itemid'] = 0;
514 }
515
516 return groups_add_member($record['groupid'], $record['userid'], $record['component'], $record['itemid']);
517 }
518
7e7cfe7a
PS
519 /**
520 * Create a test grouping for the specified course
521 *
522 * $record should be either an array or a stdClass containing infomation about the grouping to create.
523 * At the very least it needs to contain courseid.
524 * Default values are added for name, description, and descriptionformat if they are not present.
525 *
6b219869
DM
526 * This function calls groups_create_grouping() to create the grouping within the database.
527 * @see groups_create_grouping
7e7cfe7a
PS
528 * @param array|stdClass $record
529 * @return stdClass grouping record
530 */
531 public function create_grouping($record) {
532 global $DB, $CFG;
533
534 require_once($CFG->dirroot . '/group/lib.php');
535
536 $this->groupingcount++;
537 $i = $this->groupingcount;
538
539 $record = (array)$record;
540
541 if (empty($record['courseid'])) {
5c3c2c81 542 throw new coding_exception('courseid must be present in testing_data_generator::create_grouping() $record');
7e7cfe7a
PS
543 }
544
545 if (!isset($record['name'])) {
546 $record['name'] = 'grouping-' . $i;
547 }
548
549 if (!isset($record['description'])) {
550 $record['description'] = "Test Grouping $i\n{$this->loremipsum}";
551 }
552
553 if (!isset($record['descriptionformat'])) {
554 $record['descriptionformat'] = FORMAT_MOODLE;
555 }
556
557 $id = groups_create_grouping((object)$record);
558
559 return $DB->get_record('groupings', array('id'=>$id));
560 }
561
87bb583c
DM
562 /**
563 * Create a test grouping group
564 * @param array|stdClass $record
565 * @throws coding_exception
566 * @return boolean
567 */
568 public function create_grouping_group($record) {
569 global $DB, $CFG;
570
571 require_once($CFG->dirroot . '/group/lib.php');
572
573 $record = (array)$record;
574
575 if (empty($record['groupingid'])) {
576 throw new coding_exception('grouping must be present in testing::create_grouping_group() $record');
577 }
578
579 if (!isset($record['groupid'])) {
580 throw new coding_exception('group must be present in testing_util::create_grouping_group() $record');
581 }
582
583 return groups_assign_grouping($record['groupingid'], $record['groupid']);
584 }
585
7e7cfe7a
PS
586 /**
587 * Create a test scale
588 * @param array|stdClass $record
589 * @param array $options
590 * @return stdClass block instance record
591 */
592 public function create_scale($record=null, array $options=null) {
593 global $DB;
594
595 $this->scalecount++;
596 $i = $this->scalecount;
597
598 $record = (array)$record;
599
600 if (!isset($record['name'])) {
601 $record['name'] = 'Test scale '.$i;
602 }
603
604 if (!isset($record['scale'])) {
605 $record['scale'] = 'A,B,C,D,F';
606 }
607
608 if (!isset($record['courseid'])) {
609 $record['courseid'] = 0;
610 }
611
612 if (!isset($record['userid'])) {
613 $record['userid'] = 0;
614 }
615
616 if (!isset($record['description'])) {
617 $record['description'] = 'Test scale description '.$i;
618 }
619
620 if (!isset($record['descriptionformat'])) {
621 $record['descriptionformat'] = FORMAT_MOODLE;
622 }
623
624 $record['timemodified'] = time();
625
626 if (isset($record['id'])) {
627 $DB->import_record('scale', $record);
628 $DB->get_manager()->reset_sequence('scale');
629 $id = $record['id'];
630 } else {
631 $id = $DB->insert_record('scale', $record);
632 }
633
634 return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST);
635 }
4f5789ea
PS
636
637 /**
638 * Simplified enrolment of user to course using default options.
639 *
640 * It is strongly recommended to use only this method for 'manual' and 'self' plugins only!!!
641 *
642 * @param int $userid
643 * @param int $courseid
644 * @param int $roleid optional role id, use only with manual plugin
645 * @param string $enrol name of enrol plugin,
646 * there must be exactly one instance in course,
647 * it must support enrol_user() method.
648 * @return bool success
649 */
650 public function enrol_user($userid, $courseid, $roleid = null, $enrol = 'manual') {
651 global $DB;
652
653 if (!$plugin = enrol_get_plugin($enrol)) {
654 return false;
655 }
656
657 $instances = $DB->get_records('enrol', array('courseid'=>$courseid, 'enrol'=>$enrol));
658 if (count($instances) != 1) {
659 return false;
660 }
661 $instance = reset($instances);
662
663 if (is_null($roleid) and $instance->roleid) {
664 $roleid = $instance->roleid;
665 }
666
667 $plugin->enrol_user($instance, $userid, $roleid);
668
669 return true;
670 }
a9a0cb69
MN
671
672 /**
673 * Function to create a dummy discussion.
674 *
675 * @param array|stdClass $record
676 * @return stdClass the discussion object
677 */
678 public function create_forum_discussion($record = null) {
679 global $DB;
680
681 // Increment the forum discussion count.
682 $this->forumdiscussioncount++;
683
684 $record = (array) $record;
685
686 if (!isset($record['course'])) {
687 throw new coding_exception('course must be present in phpunit_util::create_forum_discussion() $record');
688 }
689
690 if (!isset($record['forum'])) {
691 throw new coding_exception('forum must be present in phpunit_util::create_forum_discussion() $record');
692 }
693
694 if (!isset($record['userid'])) {
695 throw new coding_exception('userid must be present in phpunit_util::create_forum_discussion() $record');
696 }
697
698 if (!isset($record['name'])) {
699 $record['name'] = "Discussion " . $this->forumdiscussioncount;
700 }
701
702 if (!isset($record['subject'])) {
703 $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount;
704 }
705
706 if (!isset($record['message'])) {
707 $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount);
708 }
709
710 if (!isset($record['messageformat'])) {
711 $record['messageformat'] = editors_get_preferred_format();
712 }
713
714 if (!isset($record['messagetrust'])) {
715 $record['messagetrust'] = "";
716 }
717
718 if (!isset($record['assessed'])) {
719 $record['assessed'] = '1';
720 }
721
722 if (!isset($record['groupid'])) {
723 $record['groupid'] = "-1";
724 }
725
726 if (!isset($record['timestart'])) {
727 $record['timestart'] = "0";
728 }
729
730 if (!isset($record['timeend'])) {
731 $record['timeend'] = "0";
732 }
733
734 if (!isset($record['mailnow'])) {
735 $record['mailnow'] = "0";
736 }
737
738 $record = (object) $record;
739
740 // Add the discussion.
741 $record->id = forum_add_discussion($record, null, null, $record->userid);
742
743 return $record;
744 }
745
746 /**
747 * Function to create a dummy post.
748 *
749 * @param array|stdClass $record
750 * @return stdClass the post object
751 */
752 public function create_forum_post($record = null) {
753 global $DB;
754
755 // Increment the forum post count.
756 $this->forumpostcount++;
757
758 // Variable to store time.
759 $time = time() + $this->forumpostcount;
760
761 $record = (array) $record;
762
763 if (!isset($record['discussion'])) {
764 throw new coding_exception('discussion must be present in phpunit_util::create_forum_post() $record');
765 }
766
767 if (!isset($record['userid'])) {
768 throw new coding_exception('userid must be present in phpunit_util::create_forum_post() $record');
769 }
770
771 if (!isset($record['parent'])) {
772 $record['parent'] = 0;
773 }
774
775 if (!isset($record['subject'])) {
776 $record['subject'] = 'Forum post subject ' . $this->forumpostcount;
777 }
778
779 if (!isset($record['message'])) {
780 $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount);
781 }
782
783 if (!isset($record['created'])) {
784 $record['created'] = $time;
785 }
786
787 if (!isset($record['modified'])) {
788 $record['modified'] = $time;
789 }
790
791 $record = (object) $record;
792
793 // Add the post.
794 $record->id = $DB->insert_record('forum_posts', $record);
795
796 // Update the last post.
797 forum_discussion_update_last_post($record->discussion);
798
799 return $record;
800 }
7e7cfe7a 801}
6b219869
DM
802
803/**
804 * Deprecated in favour of testing_data_generator
805 *
806 * @deprecated since Moodle 2.5 MDL-37457 - please do not use this function any more.
807 * @todo MDL-37517 This will be deleted in Moodle 2.7
808 * @see testing_data_generator
809 * @package core
810 * @category test
811 * @copyright 2012 David Monllaó
812 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
813 */
814class phpunit_data_generator extends testing_data_generator {
815
816 /**
817 * Dumb constructor to throw the deprecated notification
818 */
819 public function __construct() {
820 debugging('Class phpunit_data_generator is deprecated, please use class testing_module_generator instead', DEBUG_DEVELOPER);
821 }
822}