MDL-32149 scale generator and workshop tests
[moodle.git] / lib / phpunit / generatorlib.php
blobf01893e6e1e6ed55d7c2147449c9046588764474
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.
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/>.
17 /**
18 * PHPUnit data generator class
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Data generator for unit tests
31 class phpunit_data_generator {
32 protected $usercounter = 0;
33 protected $categorycount = 0;
34 protected $coursecount = 0;
35 protected $blockcount = 0;
36 protected $modulecount = 0;
37 protected $scalecount = 0;
39 /**
40 * To be called from data reset code only,
41 * do not use in tests.
42 * @return void
44 public function reset() {
45 $this->usercounter = 0;
46 $this->categorycount = 0;
47 $this->coursecount = 0;
48 $this->blockcount = 0;
49 $this->modulecount = 0;
50 $this->scalecount = 0;
53 /**
54 * Create a test user
55 * @param array|stdClass $record
56 * @param array $options
57 * @return stdClass user record
59 public function create_user($record=null, array $options=null) {
60 global $DB, $CFG;
62 $this->usercounter++;
63 $i = $this->usercounter;
65 $record = (array)$record;
67 if (!isset($record['auth'])) {
68 $record['auth'] = 'manual';
71 if (!isset($record['firstname'])) {
72 $record['firstname'] = 'Firstname'.$i;
75 if (!isset($record['lastname'])) {
76 $record['lastname'] = 'Lastname'.$i;
79 if (!isset($record['idnumber'])) {
80 $record['idnumber'] = '';
83 if (!isset($record['username'])) {
84 $record['username'] = 'username'.$i;
87 if (!isset($record['password'])) {
88 $record['password'] = 'lala';
91 if (!isset($record['email'])) {
92 $record['email'] = $record['username'].'@example.com';
95 if (!isset($record['confirmed'])) {
96 $record['confirmed'] = 1;
99 if (!isset($record['mnethostid'])) {
100 $record['mnethostid'] = $CFG->mnet_localhost_id;
103 if (!isset($record['lang'])) {
104 $record['lang'] = 'en';
107 if (!isset($record['maildisplay'])) {
108 $record['maildisplay'] = 1;
111 if (!isset($record['deleted'])) {
112 $record['deleted'] = 0;
115 $record['timecreated'] = time();
116 $record['timemodified'] = $record['timecreated'];
117 $record['lastip'] = '0.0.0.0';
119 $record['password'] = hash_internal_user_password($record['password']);
121 if ($record['deleted']) {
122 $delname = $record['email'].'.'.time();
123 while ($DB->record_exists('user', array('username'=>$delname))) {
124 $delname++;
126 $record['idnumber'] = '';
127 $record['email'] = md5($record['username']);
128 $record['username'] = $delname;
131 $userid = $DB->insert_record('user', $record);
132 if (!$record['deleted']) {
133 context_user::instance($userid);
136 return $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
140 * Create a test course category
141 * @param array|stdClass $record
142 * @param array $options
143 * @return stdClass course category record
145 function create_category($record=null, array $options=null) {
146 global $DB, $CFG;
147 require_once("$CFG->dirroot/course/lib.php");
149 $this->categorycount++;
150 $i = $this->categorycount;
152 $record = (array)$record;
154 if (!isset($record['name'])) {
155 $record['name'] = 'Course category '.$i;
158 if (!isset($record['idnumber'])) {
159 $record['idnumber'] = '';
162 if (!isset($record['description'])) {
163 $record['description'] = 'Test course category '.$i;
166 if (!isset($record['descriptionformat'])) {
167 $record['description'] = FORMAT_MOODLE;
170 if (!isset($record['parent'])) {
171 $record['descriptionformat'] = 0;
174 if ($record['parent'] == 0) {
175 $parent = new stdClass();
176 $parent->path = '';
177 $parent->depth = 0;
178 } else {
179 $parent = $DB->get_record('course_categories', array('id'=>$record['parent']), '*', MUST_EXIST);
181 $record['depth'] = $parent->depth+1;
183 $record['sortorder'] = 0;
184 $record['timemodified'] = time();
185 $record['timecreated'] = $record['timemodified'];
187 $catid = $DB->insert_record('course_categories', $record);
188 $path = $parent->path . '/' . $catid;
189 $DB->set_field('course_categories', 'path', $path, array('id'=>$catid));
190 context_coursecat::instance($catid);
192 fix_course_sortorder();
194 return $DB->get_record('course_categories', array('id'=>$catid), '*', MUST_EXIST);
198 * Create a test course
199 * @param array|stdClass $record
200 * @param array $options with keys:
201 * 'createsections'=>bool precreate all sections
202 * @return stdClass course record
204 function create_course($record=null, array $options=null) {
205 global $DB, $CFG;
206 require_once("$CFG->dirroot/course/lib.php");
208 $this->coursecount++;
209 $i = $this->coursecount;
211 $record = (array)$record;
213 if (!isset($record['fullname'])) {
214 $record['fullname'] = 'Test course '.$i;
217 if (!isset($record['shortname'])) {
218 $record['shortname'] = 'tc_'.$i;
221 if (!isset($record['idnumber'])) {
222 $record['idnumber'] = '';
225 if (!isset($record['format'])) {
226 $record['format'] = 'topics';
229 if (!isset($record['newsitems'])) {
230 $record['newsitems'] = 0;
233 if (!isset($record['numsections'])) {
234 $record['numsections'] = 5;
237 if (!isset($record['description'])) {
238 $record['description'] = 'Test course '.$i;
241 if (!isset($record['descriptionformat'])) {
242 $record['description'] = FORMAT_MOODLE;
245 if (!isset($record['category'])) {
246 $record['category'] = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
249 $course = create_course((object)$record);
250 context_course::instance($course->id);
252 if (!empty($options['createsections'])) {
253 for($i=1; $i<$record['numsections']; $i++) {
254 self::create_course_section(array('course'=>$course->id, 'section'=>$i));
258 return $course;
262 * Create course section if does not exist yet
263 * @param mixed $record
264 * @param array|null $options
265 * @return stdClass
266 * @throws coding_exception
268 public function create_course_section($record = null, array $options = null) {
269 global $DB;
271 $record = (array)$record;
273 if (empty($record['course'])) {
274 throw new coding_exception('course must be present in phpunit_util::create_course_section() $record');
277 if (!isset($record['section'])) {
278 throw new coding_exception('section must be present in phpunit_util::create_course_section() $record');
281 if (!isset($record['name'])) {
282 $record['name'] = '';
285 if (!isset($record['summary'])) {
286 $record['summary'] = '';
289 if (!isset($record['summaryformat'])) {
290 $record['summaryformat'] = FORMAT_MOODLE;
293 if ($section = $DB->get_record('course_sections', array('course'=>$record['course'], 'section'=>$record['section']))) {
294 return $section;
297 $section = new stdClass();
298 $section->course = $record['course'];
299 $section->section = $record['section'];
300 $section->name = $record['name'];
301 $section->summary = $record['summary'];
302 $section->summaryformat = $record['summaryformat'];
303 $id = $DB->insert_record('course_sections', $section);
305 return $DB->get_record('course_sections', array('id'=>$id));
309 * Create a test block
310 * @param string $blockname
311 * @param array|stdClass $record
312 * @param array $options
313 * @return stdClass block instance record
315 public function create_block($blockname, $record=null, array $options=null) {
316 global $DB;
318 $this->blockcount++;
319 $i = $this->blockcount;
321 $record = (array)$record;
323 $record['blockname'] = $blockname;
325 //TODO: use block callbacks
327 if (!isset($record['parentcontextid'])) {
328 $record['parentcontextid'] = context_system::instance()->id;
331 if (!isset($record['showinsubcontexts'])) {
332 $record['showinsubcontexts'] = 1;
335 if (!isset($record['pagetypepattern'])) {
336 $record['pagetypepattern'] = '';
339 if (!isset($record['subpagepattern'])) {
340 $record['subpagepattern'] = '';
343 if (!isset($record['defaultweight'])) {
344 $record['defaultweight'] = '';
347 $biid = $DB->insert_record('block_instances', $record);
348 context_block::instance($biid);
350 return $DB->get_record('block_instances', array('id'=>$biid), '*', MUST_EXIST);
354 * Create a test module
355 * @param string $modulename
356 * @param array|stdClass $record
357 * @param array $options
358 * @return stdClass activity record
360 public function create_module($modulename, $record=null, array $options=null) {
361 global $DB, $CFG;
362 require_once("$CFG->dirroot/course/lib.php");
364 $this->modulecount++;
365 $i = $this->modulecount;
367 $record = (array)$record;
368 $options = (array)$options;
370 if (!isset($record['name'])) {
371 $record['name'] = get_string('pluginname', $modulename).' '.$i;
374 if (!isset($record['intro'])) {
375 $record['intro'] = 'Test module '.$i;
378 if (!isset($record['introformat'])) {
379 $record['introformat'] = FORMAT_MOODLE;
382 if (!isset($options['section'])) {
383 $options['section'] = 1;
386 //TODO: use module callbacks
388 if ($modulename === 'page') {
389 if (!isset($record['content'])) {
390 $record['content'] = 'Test page content';
392 if (!isset($record['contentformat'])) {
393 $record['contentformat'] = FORMAT_MOODLE;
396 } else {
397 error('TODO: only mod_page is supported in data generator for now');
400 $id = $DB->insert_record($modulename, $record);
402 $cm = new stdClass();
403 $cm->course = $record['course'];
404 $cm->module = $DB->get_field('modules', 'id', array('name'=>$modulename));
405 $cm->section = $options['section'];
406 $cm->instance = $id;
407 $cm->id = $DB->insert_record('course_modules', $cm);
409 $cm->coursemodule = $cm->id;
410 add_mod_to_section($cm);
412 context_module::instance($cm->id);
414 $instance = $DB->get_record($modulename, array('id'=>$id), '*', MUST_EXIST);
415 $instance->cmid = $cm->id;
417 return $instance;
421 * Create a test scale
422 * @param array|stdClass $record
423 * @param array $options
424 * @return stdClass block instance record
426 public function create_scale($record=null, array $options=null) {
427 global $DB;
429 $this->scalecount++;
430 $i = $this->scalecount;
432 $record = (array)$record;
434 if (!isset($record['name'])) {
435 $record['name'] = 'Test scale '.$i;
438 if (!isset($record['scale'])) {
439 $record['scale'] = 'A,B,C,D,F';
442 if (!isset($record['courseid'])) {
443 $record['courseid'] = 0;
446 if (!isset($record['userid'])) {
447 $record['userid'] = 0;
450 if (!isset($record['description'])) {
451 $record['description'] = 'Test scale description '.$i;
454 if (!isset($record['descriptionformat'])) {
455 $record['descriptionformat'] = FORMAT_MOODLE;
458 $record['timemodified'] = time();
460 if (isset($record['id'])) {
461 $DB->import_record('scale', $record);
462 $DB->get_manager()->reset_sequence('scale');
463 $id = $record['id'];
464 } else {
465 $id = $DB->insert_record('scale', $record);
468 return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST);