Merge branch 'MDL-38582' of git://github.com/timhunt/moodle
[moodle.git] / lib / testing / tests / generator_test.php
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/>.
17 /**
18  * Data generators tests
19  *
20  * @package    core
21  * @category   test
22  * @copyright  2012 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Test data generator
31  *
32  * @package    core
33  * @category   test
34  * @copyright  2012 Petr Skoda {@link http://skodak.org}
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class core_test_generator_testcase extends advanced_testcase {
38     public function test_get_plugin_generator_good_case() {
39         $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
40         $this->assertInstanceOf('core_question_generator', $generator);
41     }
43     public function test_get_plugin_generator_sloppy_name() {
44         $generator = $this->getDataGenerator()->get_plugin_generator('quiz');
45         $this->assertDebuggingCalled('Please specify the component you want a generator for as ' .
46                     'mod_quiz, not quiz.', DEBUG_DEVELOPER);
47         $this->assertInstanceOf('mod_quiz_generator', $generator);
48     }
50     public function test_get_plugin_generator_no_component_dir() {
51         $this->setExpectedException('coding_exception', 'Component core_completion does not support ' .
52                     'generators yet. Missing tests/generator/lib.php.');
53         $generator = $this->getDataGenerator()->get_plugin_generator('core_completion');
54     }
56     public function test_create() {
57         global $DB;
59         $this->resetAfterTest(true);
60         $generator = $this->getDataGenerator();
62         $count = $DB->count_records('user');
63         $user = $generator->create_user();
64         $this->assertEquals($count+1, $DB->count_records('user'));
65         $this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME));
66         $this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL));
67         $user = $generator->create_user(array('firstname'=>'Žluťoučký', 'lastname'=>'Koníček'));
68         $this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME));
69         $this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL));
71         $count = $DB->count_records('course_categories');
72         $category = $generator->create_category();
73         $this->assertEquals($count+1, $DB->count_records('course_categories'));
74         $this->assertRegExp('/^Course category \d/', $category->name);
75         $this->assertSame('', $category->idnumber);
76         $this->assertRegExp('/^Test course category \d/', $category->description);
77         $this->assertSame(FORMAT_MOODLE, $category->descriptionformat);
79         $count = $DB->count_records('cohort');
80         $cohort = $generator->create_cohort();
81         $this->assertEquals($count+1, $DB->count_records('cohort'));
82         $this->assertEquals(context_system::instance()->id, $cohort->contextid);
83         $this->assertRegExp('/^Cohort \d/', $cohort->name);
84         $this->assertSame('', $cohort->idnumber);
85         $this->assertRegExp('/^Test cohort \d/', $cohort->description);
86         $this->assertSame(FORMAT_MOODLE, $cohort->descriptionformat);
87         $this->assertSame('', $cohort->component);
88         $this->assertLessThanOrEqual(time(), $cohort->timecreated);
89         $this->assertSame($cohort->timecreated, $cohort->timemodified);
91         $count = $DB->count_records('course');
92         $course = $generator->create_course();
93         $this->assertEquals($count+1, $DB->count_records('course'));
94         $this->assertRegExp('/^Test course \d/', $course->fullname);
95         $this->assertRegExp('/^tc_\d/', $course->shortname);
96         $this->assertSame('', $course->idnumber);
97         $this->assertSame('topics', $course->format);
98         $this->assertEquals(0, $course->newsitems);
99         $this->assertEquals(5, $course->numsections);
100         $this->assertRegExp('/^Test course \d/', $course->summary);
101         $this->assertSame(FORMAT_MOODLE, $course->summaryformat);
103         $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3));
104         $this->assertEquals($course->id, $section->course);
106         $scale = $generator->create_scale();
107         $this->assertNotEmpty($scale);
108     }
110     public function test_create_module() {
111         global $CFG, $SITE;
112         if (!file_exists("$CFG->dirroot/mod/page/")) {
113             $this->markTestSkipped('Can not find standard Page module');
114         }
116         $this->resetAfterTest(true);
117         $generator = $this->getDataGenerator();
119         $page = $generator->create_module('page', array('course'=>$SITE->id));
120         $this->assertNotEmpty($page);
121         $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
122         $this->assertEquals(0, $cm->sectionnum);
124         $page = $generator->create_module('page', array('course'=>$SITE->id), array('section'=>3));
125         $this->assertNotEmpty($page);
126         $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
127         $this->assertEquals(3, $cm->sectionnum);
128     }
130     public function test_create_block() {
131         global $CFG;
132         if (!file_exists("$CFG->dirroot/blocks/online_users/")) {
133             $this->markTestSkipped('Can not find standard Online users block');
134         }
136         $this->resetAfterTest(true);
137         $generator = $this->getDataGenerator();
139         $page = $generator->create_block('online_users');
140         $this->assertNotEmpty($page);
141     }
143     public function test_enrol_user() {
144         global $DB;
146         $this->resetAfterTest();
148         $selfplugin = enrol_get_plugin('self');
149         $this->assertNotEmpty($selfplugin);
151         $manualplugin = enrol_get_plugin('manual');
152         $this->assertNotEmpty($manualplugin);
154         // Prepare some data.
156         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
157         $this->assertNotEmpty($studentrole);
158         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
159         $this->assertNotEmpty($teacherrole);
161         $course1 = $this->getDataGenerator()->create_course();
162         $course2 = $this->getDataGenerator()->create_course();
163         $course3 = $this->getDataGenerator()->create_course();
165         $context1 = context_course::instance($course1->id);
166         $context2 = context_course::instance($course2->id);
167         $context3 = context_course::instance($course3->id);
169         $user1 = $this->getDataGenerator()->create_user();
170         $user2 = $this->getDataGenerator()->create_user();
171         $user3 = $this->getDataGenerator()->create_user();
173         $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
174         $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
175         $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
176         $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
178         $this->assertEquals($studentrole->id, $instance1->roleid);
179         $this->assertEquals($studentrole->id, $instance2->roleid);
180         $this->assertEquals($studentrole->id, $instance3->roleid);
182         $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual')));
183         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
184         $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
185         $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
186         $maninstance3->roleid = $teacherrole->id;
187         $DB->update_record('enrol', $maninstance3, array('id'=>$maninstance3->id));
189         $this->assertEquals($studentrole->id, $maninstance1->roleid);
190         $this->assertEquals($studentrole->id, $maninstance2->roleid);
191         $this->assertEquals($teacherrole->id, $maninstance3->roleid);
193         $result = $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
194         $this->assertTrue($result);
195         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id)));
196         $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));
198         $result = $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $teacherrole->id, 'manual');
199         $this->assertTrue($result);
200         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user1->id)));
201         $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
203         $result = $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 0, 'manual');
204         $this->assertTrue($result);
205         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id)));
206         $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id)));
209         $result = $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
210         $this->assertTrue($result);
211         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
212         $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));
215         $selfplugin->add_instance($course2, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id));
216         $result = $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
217         $this->assertFalse($result);
219         $DB->delete_records('enrol', array('enrol'=>'self', 'courseid'=>$course3->id));
220         $result = $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
221         $this->assertFalse($result);
223     }