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 |
namespace core;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Test data generator
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @category phpunit
|
|
|
24 |
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
* @covers \core\testing_data_generator
|
|
|
27 |
*/
|
|
|
28 |
class testing_generator_test extends \advanced_testcase {
|
11 |
efrain |
29 |
public function test_get_plugin_generator_good_case(): void {
|
1 |
efrain |
30 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
31 |
$this->assertInstanceOf('core_question_generator', $generator);
|
|
|
32 |
}
|
|
|
33 |
|
11 |
efrain |
34 |
public function test_get_plugin_generator_sloppy_name(): void {
|
1 |
efrain |
35 |
$generator = $this->getDataGenerator()->get_plugin_generator('quiz');
|
|
|
36 |
$this->assertDebuggingCalled('Please specify the component you want a generator for as ' .
|
|
|
37 |
'mod_quiz, not quiz.', DEBUG_DEVELOPER);
|
|
|
38 |
$this->assertInstanceOf('mod_quiz_generator', $generator);
|
|
|
39 |
}
|
|
|
40 |
|
11 |
efrain |
41 |
public function test_get_default_generator(): void {
|
1 |
efrain |
42 |
$generator = $this->getDataGenerator()->get_plugin_generator('block_somethingthatdoesnotexist');
|
|
|
43 |
$this->assertInstanceOf('default_block_generator', $generator);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Test plugin generator, with no component directory.
|
|
|
48 |
*/
|
11 |
efrain |
49 |
public function test_get_plugin_generator_no_component_dir(): void {
|
1 |
efrain |
50 |
$this->expectException(\coding_exception::class);
|
|
|
51 |
$this->expectExceptionMessage('Component core_cohort does not support generators yet. Missing tests/generator/lib.php.');
|
|
|
52 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_cohort');
|
|
|
53 |
}
|
|
|
54 |
|
11 |
efrain |
55 |
public function test_create_user(): void {
|
1 |
efrain |
56 |
global $DB, $CFG;
|
|
|
57 |
require_once($CFG->dirroot.'/user/lib.php');
|
|
|
58 |
|
|
|
59 |
$this->resetAfterTest(true);
|
|
|
60 |
$generator = $this->getDataGenerator();
|
|
|
61 |
|
|
|
62 |
$count = $DB->count_records('user');
|
|
|
63 |
$this->setCurrentTimeStart();
|
|
|
64 |
$user = $generator->create_user();
|
|
|
65 |
$this->assertEquals($count + 1, $DB->count_records('user'));
|
|
|
66 |
$this->assertSame($user->username, \core_user::clean_field($user->username, 'username'));
|
|
|
67 |
$this->assertSame($user->email, \core_user::clean_field($user->email, 'email'));
|
|
|
68 |
$this->assertNotEmpty($user->firstnamephonetic);
|
|
|
69 |
$this->assertNotEmpty($user->lastnamephonetic);
|
|
|
70 |
$this->assertNotEmpty($user->alternatename);
|
|
|
71 |
$this->assertNotEmpty($user->middlename);
|
|
|
72 |
$this->assertSame('manual', $user->auth);
|
|
|
73 |
$this->assertSame('en', $user->lang);
|
|
|
74 |
$this->assertSame('1', $user->confirmed);
|
|
|
75 |
$this->assertSame('0', $user->deleted);
|
|
|
76 |
$this->assertTimeCurrent($user->timecreated);
|
|
|
77 |
$this->assertSame($user->timecreated, $user->timemodified);
|
|
|
78 |
$this->assertSame('0.0.0.0', $user->lastip);
|
|
|
79 |
|
|
|
80 |
$record = array(
|
|
|
81 |
'auth' => 'email',
|
|
|
82 |
'firstname' => 'Žluťoučký',
|
|
|
83 |
'lastname' => 'Koníček',
|
|
|
84 |
'firstnamephonetic' => 'Zhlutyoucky',
|
|
|
85 |
'lastnamephonetic' => 'Koniiczek',
|
|
|
86 |
'middlename' => 'Hopper',
|
|
|
87 |
'alternatename' => 'horse',
|
|
|
88 |
'idnumber' => 'abc1',
|
|
|
89 |
'mnethostid' => (string)$CFG->mnet_localhost_id,
|
|
|
90 |
'username' => 'konic666',
|
|
|
91 |
'password' => 'password1',
|
|
|
92 |
'email' => 'email@example.com',
|
|
|
93 |
'confirmed' => '1',
|
|
|
94 |
'maildisplay' => '1',
|
|
|
95 |
'mailformat' => '0',
|
|
|
96 |
'maildigest' => '1',
|
|
|
97 |
'autosubscribe' => '0',
|
|
|
98 |
'trackforums' => '0',
|
|
|
99 |
'deleted' => '0',
|
|
|
100 |
'timecreated' => '666',
|
|
|
101 |
);
|
|
|
102 |
$user = $generator->create_user($record);
|
|
|
103 |
$this->assertEquals($count + 2, $DB->count_records('user'));
|
|
|
104 |
foreach ($record as $k => $v) {
|
|
|
105 |
if ($k === 'password') {
|
|
|
106 |
$this->assertTrue(password_verify($v, $user->password));
|
|
|
107 |
} else {
|
|
|
108 |
$this->assertSame($v, $user->{$k});
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$record = array(
|
|
|
113 |
'firstname' => 'Some',
|
|
|
114 |
'lastname' => 'User',
|
|
|
115 |
'idnumber' => 'def',
|
|
|
116 |
'username' => 'user666',
|
|
|
117 |
'email' => 'email666@example.com',
|
|
|
118 |
'deleted' => '1',
|
|
|
119 |
);
|
|
|
120 |
$user = $generator->create_user($record);
|
|
|
121 |
$this->assertEquals($count + 3, $DB->count_records('user'));
|
|
|
122 |
$this->assertSame('', $user->idnumber);
|
|
|
123 |
$this->assertSame(md5($record['username']), $user->email);
|
|
|
124 |
$this->assertEquals(1, $user->deleted);
|
|
|
125 |
|
|
|
126 |
// Test generating user with interests.
|
|
|
127 |
$user = $generator->create_user(array('interests' => 'Cats, Dogs'));
|
|
|
128 |
$userdetails = user_get_user_details($user);
|
|
|
129 |
$this->assertSame('Cats, Dogs', $userdetails['interests']);
|
|
|
130 |
}
|
|
|
131 |
|
11 |
efrain |
132 |
public function test_create(): void {
|
1 |
efrain |
133 |
global $DB;
|
|
|
134 |
|
|
|
135 |
$this->resetAfterTest(true);
|
|
|
136 |
$generator = $this->getDataGenerator();
|
|
|
137 |
|
|
|
138 |
$count = $DB->count_records('course_categories');
|
|
|
139 |
$category = $generator->create_category();
|
|
|
140 |
$this->assertEquals($count+1, $DB->count_records('course_categories'));
|
|
|
141 |
$this->assertMatchesRegularExpression('/^Course category \d/', $category->name);
|
|
|
142 |
$this->assertSame('', $category->idnumber);
|
|
|
143 |
$this->assertMatchesRegularExpression('/^Test course category \d/', $category->description);
|
|
|
144 |
$this->assertSame(FORMAT_MOODLE, $category->descriptionformat);
|
|
|
145 |
|
|
|
146 |
$count = $DB->count_records('cohort');
|
|
|
147 |
$cohort = $generator->create_cohort();
|
|
|
148 |
$this->assertEquals($count+1, $DB->count_records('cohort'));
|
|
|
149 |
$this->assertEquals(\context_system::instance()->id, $cohort->contextid);
|
|
|
150 |
$this->assertMatchesRegularExpression('/^Cohort \d/', $cohort->name);
|
|
|
151 |
$this->assertSame('', $cohort->idnumber);
|
|
|
152 |
$this->assertMatchesRegularExpression("/^Description for '{$cohort->name}' \\n/", $cohort->description);
|
|
|
153 |
$this->assertSame(FORMAT_MOODLE, $cohort->descriptionformat);
|
|
|
154 |
$this->assertSame('', $cohort->component);
|
|
|
155 |
$this->assertLessThanOrEqual(time(), $cohort->timecreated);
|
|
|
156 |
$this->assertSame($cohort->timecreated, $cohort->timemodified);
|
|
|
157 |
|
|
|
158 |
$count = $DB->count_records('course');
|
|
|
159 |
$course = $generator->create_course();
|
|
|
160 |
$this->assertEquals($count+1, $DB->count_records('course'));
|
|
|
161 |
$this->assertMatchesRegularExpression('/^Test course \d/', $course->fullname);
|
|
|
162 |
$this->assertMatchesRegularExpression('/^tc_\d/', $course->shortname);
|
|
|
163 |
$this->assertSame('', $course->idnumber);
|
|
|
164 |
$this->assertSame('topics', $course->format);
|
|
|
165 |
$this->assertEquals(0, $course->newsitems);
|
|
|
166 |
$this->assertEquals(5, course_get_format($course)->get_last_section_number());
|
|
|
167 |
$this->assertMatchesRegularExpression('/^Test course \d/', $course->summary);
|
|
|
168 |
$this->assertSame(FORMAT_MOODLE, $course->summaryformat);
|
|
|
169 |
|
|
|
170 |
$section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3));
|
|
|
171 |
$this->assertEquals($course->id, $section->course);
|
|
|
172 |
$this->assertNull($section->name);
|
|
|
173 |
|
|
|
174 |
$course = $generator->create_course(array('tags' => 'Cat, Dog'));
|
|
|
175 |
$this->assertEquals(array('Cat', 'Dog'), array_values(\core_tag_tag::get_item_tags_array('core', 'course', $course->id)));
|
|
|
176 |
|
|
|
177 |
$scale = $generator->create_scale();
|
|
|
178 |
$this->assertNotEmpty($scale);
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Test case for the `test_create_course_initsections` method.
|
|
|
183 |
*
|
|
|
184 |
* This method tests the behavior of creating a course with initialized sections.
|
|
|
185 |
* It checks that the sections are renamed to "Section x" when `initsections` is true,
|
|
|
186 |
* and that the sections are not renamed when `initsections` is false.
|
|
|
187 |
*
|
|
|
188 |
* @covers ::create_course
|
|
|
189 |
*/
|
|
|
190 |
public function test_create_course_initsections(): void {
|
|
|
191 |
global $DB;
|
|
|
192 |
|
|
|
193 |
$this->resetAfterTest(true);
|
|
|
194 |
$generator = $this->getDataGenerator();
|
|
|
195 |
|
|
|
196 |
// Check that the sections are renamed to "Section x" when initsections is true.
|
|
|
197 |
$course = $generator->create_course(['initsections' => 1]);
|
|
|
198 |
$sections = course_get_format($course)->get_sections();
|
|
|
199 |
foreach ($sections as $section) {
|
|
|
200 |
if ($section->sectionnum > 0) {
|
|
|
201 |
$this->assertEquals(get_string('section') . ' ' . $section->sectionnum, $section->name);
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
// Check that the sections are not renamed when initsections is false.
|
|
|
206 |
$course = $generator->create_course(['initsections' => 0]);
|
|
|
207 |
$sections = course_get_format($course)->get_sections();
|
|
|
208 |
foreach ($sections as $section) {
|
|
|
209 |
if ($section->sectionnum > 0) {
|
|
|
210 |
$this->assertNull($section->name);
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
|
11 |
efrain |
215 |
public function test_create_module(): void {
|
1 |
efrain |
216 |
global $CFG, $SITE, $DB;
|
|
|
217 |
|
|
|
218 |
$this->setAdminUser();
|
|
|
219 |
|
|
|
220 |
if (!file_exists("$CFG->dirroot/mod/page/")) {
|
|
|
221 |
$this->markTestSkipped('Can not find standard Page module');
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
$this->resetAfterTest(true);
|
|
|
225 |
$generator = $this->getDataGenerator();
|
|
|
226 |
|
|
|
227 |
$page = $generator->create_module('page', array('course'=>$SITE->id));
|
|
|
228 |
$this->assertNotEmpty($page);
|
|
|
229 |
$cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
|
|
|
230 |
$this->assertEquals(0, $cm->sectionnum);
|
|
|
231 |
|
|
|
232 |
$page = $generator->create_module('page', array('course'=>$SITE->id), array('section'=>3));
|
|
|
233 |
$this->assertNotEmpty($page);
|
|
|
234 |
$cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
|
|
|
235 |
$this->assertEquals(3, $cm->sectionnum);
|
|
|
236 |
|
|
|
237 |
$page = $generator->create_module('page', array('course' => $SITE->id, 'tags' => 'Cat, Dog'));
|
|
|
238 |
$this->assertEquals(array('Cat', 'Dog'),
|
|
|
239 |
array_values(\core_tag_tag::get_item_tags_array('core', 'course_modules', $page->cmid)));
|
|
|
240 |
|
|
|
241 |
// Prepare environment to generate modules with all possible options.
|
|
|
242 |
|
|
|
243 |
// Enable advanced functionality.
|
|
|
244 |
$CFG->enablecompletion = 1;
|
|
|
245 |
$CFG->enableavailability = 1;
|
|
|
246 |
$CFG->enableoutcomes = 1;
|
|
|
247 |
require_once($CFG->libdir.'/gradelib.php');
|
|
|
248 |
require_once($CFG->libdir.'/completionlib.php');
|
|
|
249 |
require_once($CFG->dirroot.'/rating/lib.php');
|
|
|
250 |
|
|
|
251 |
// Create a course with enabled completion.
|
|
|
252 |
$course = $generator->create_course(array('enablecompletion' => true));
|
|
|
253 |
|
|
|
254 |
// Create new grading category in this course.
|
|
|
255 |
$grade_category = new \grade_category();
|
|
|
256 |
$grade_category->courseid = $course->id;
|
|
|
257 |
$grade_category->fullname = 'Grade category';
|
|
|
258 |
$grade_category->insert();
|
|
|
259 |
|
|
|
260 |
// Create group and grouping.
|
|
|
261 |
$group = $generator->create_group(array('courseid' => $course->id));
|
|
|
262 |
$grouping = $generator->create_grouping(array('courseid' => $course->id));
|
|
|
263 |
$generator->create_grouping_group(array('groupid' => $group->id, 'groupingid' => $grouping->id));
|
|
|
264 |
|
|
|
265 |
// Prepare arrays with properties that we can both use for creating modules and asserting the data in created modules.
|
|
|
266 |
|
|
|
267 |
// General properties.
|
|
|
268 |
$optionsgeneral = array(
|
|
|
269 |
'visible' => 0, // Note: 'visibleold' will always be set to the same value as 'visible'.
|
|
|
270 |
'section' => 3, // Note: section will be created if does not exist.
|
|
|
271 |
// Module supports FEATURE_IDNUMBER.
|
|
|
272 |
'cmidnumber' => 'IDNUM', // Note: alternatively can have key 'idnumber'.
|
|
|
273 |
// Module supports FEATURE_GROUPS;
|
|
|
274 |
'groupmode' => SEPARATEGROUPS, // Note: will be reset to 0 if course groupmodeforce is set.
|
|
|
275 |
// Module supports FEATURE_GROUPINGS.
|
|
|
276 |
'groupingid' => $grouping->id,
|
|
|
277 |
);
|
|
|
278 |
|
|
|
279 |
// In case completion is enabled on site and for course every module can have manual completion.
|
|
|
280 |
$featurecompletionmanual = array(
|
|
|
281 |
'completion' => COMPLETION_TRACKING_MANUAL, // "Students can manually mark activity as completed."
|
|
|
282 |
'completionexpected' => time() + 7 * DAYSECS,
|
|
|
283 |
);
|
|
|
284 |
|
|
|
285 |
// Automatic completion is possible if module supports FEATURE_COMPLETION_TRACKS_VIEWS or FEATURE_GRADE_HAS_GRADE.
|
|
|
286 |
// Note: completionusegrade is stored in DB and can be found in cm_info as 'completiongradeitemnumber' - either NULL or 0.
|
|
|
287 |
// Note: module can have more autocompletion rules as defined in moodleform_mod::add_completion_rules().
|
|
|
288 |
$featurecompletionautomatic = array(
|
|
|
289 |
'completion' => COMPLETION_TRACKING_AUTOMATIC, // "Show activity as complete when conditions are met."
|
|
|
290 |
'completionview' => 1, // "Student must view this activity to complete it"
|
|
|
291 |
'completionusegrade' => 1, // "Student must receive a grade to complete this activity"
|
|
|
292 |
'completionpassgrade' => 1, // "Student must receive a passing grade to complete this activity"
|
|
|
293 |
);
|
|
|
294 |
|
|
|
295 |
// Module supports FEATURE_RATE:
|
|
|
296 |
$featurerate = array(
|
|
|
297 |
'assessed' => RATING_AGGREGATE_AVERAGE, // "Aggregate type"
|
|
|
298 |
'scale' => 100, // Either max grade or negative number for scale id.
|
|
|
299 |
'ratingtime' => 1, // "Restrict ratings to items with dates in this range".
|
|
|
300 |
'assesstimestart' => time() - DAYSECS, // Note: Will be ignored if neither 'assessed' nor 'ratingtime' is set.
|
|
|
301 |
'assesstimefinish' => time() + DAYSECS, // Note: Will be ignored if neither 'assessed' nor 'ratingtime' is set.
|
|
|
302 |
);
|
|
|
303 |
|
|
|
304 |
// Module supports FEATURE_GRADE_HAS_GRADE:
|
|
|
305 |
$featuregrade = array(
|
|
|
306 |
'grade' => 10,
|
|
|
307 |
'gradecat' => $grade_category->id, // Note: if $CFG->enableoutcomes is set, this can be set to -1 to automatically create new grade category.
|
|
|
308 |
);
|
|
|
309 |
|
|
|
310 |
// Now let's create several modules with different options.
|
|
|
311 |
$m1 = $generator->create_module('assign',
|
|
|
312 |
array('course' => $course->id) +
|
|
|
313 |
$optionsgeneral);
|
|
|
314 |
$m2 = $generator->create_module('data',
|
|
|
315 |
array('course' => $course->id) +
|
|
|
316 |
$featurecompletionmanual +
|
|
|
317 |
$featurerate);
|
|
|
318 |
$m3 = $generator->create_module('assign',
|
|
|
319 |
array('course' => $course->id) +
|
|
|
320 |
$featurecompletionautomatic +
|
|
|
321 |
$featuregrade);
|
|
|
322 |
|
|
|
323 |
// We need id of the grading item for the second module to create availability dependency in the 3rd module.
|
|
|
324 |
$gradingitem = \grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id));
|
|
|
325 |
|
|
|
326 |
// Now prepare option to create the 4th module with an availability condition.
|
|
|
327 |
$optionsavailability = array(
|
|
|
328 |
'availability' => '{"op":"&","showc":[true],"c":[' .
|
|
|
329 |
'{"type":"date","d":">=","t":' . (time() - WEEKSECS) . '}]}',
|
|
|
330 |
);
|
|
|
331 |
|
|
|
332 |
// Create module with conditional availability.
|
|
|
333 |
$m4 = $generator->create_module('assign',
|
|
|
334 |
array('course' => $course->id) +
|
|
|
335 |
$optionsavailability
|
|
|
336 |
);
|
|
|
337 |
|
|
|
338 |
// Verifying that everything is generated correctly.
|
|
|
339 |
$modinfo = get_fast_modinfo($course->id);
|
|
|
340 |
$cm1 = $modinfo->cms[$m1->cmid];
|
|
|
341 |
$this->assertEquals($optionsgeneral['visible'], $cm1->visible);
|
|
|
342 |
$this->assertEquals($optionsgeneral['section'], $cm1->sectionnum); // Note difference in key.
|
|
|
343 |
$this->assertEquals($optionsgeneral['cmidnumber'], $cm1->idnumber); // Note difference in key.
|
|
|
344 |
$this->assertEquals($optionsgeneral['groupmode'], $cm1->groupmode);
|
|
|
345 |
$this->assertEquals($optionsgeneral['groupingid'], $cm1->groupingid);
|
|
|
346 |
|
|
|
347 |
$cm2 = $modinfo->cms[$m2->cmid];
|
|
|
348 |
$this->assertEquals($featurecompletionmanual['completion'], $cm2->completion);
|
|
|
349 |
$this->assertEquals($featurecompletionmanual['completionexpected'], $cm2->completionexpected);
|
|
|
350 |
$this->assertEquals(null, $cm2->completiongradeitemnumber);
|
|
|
351 |
// Rating info is stored in the module's table (in our test {data}).
|
|
|
352 |
$data = $DB->get_record('data', array('id' => $m2->id));
|
|
|
353 |
$this->assertEquals($featurerate['assessed'], $data->assessed);
|
|
|
354 |
$this->assertEquals($featurerate['scale'], $data->scale);
|
|
|
355 |
$this->assertEquals($featurerate['assesstimestart'], $data->assesstimestart);
|
|
|
356 |
$this->assertEquals($featurerate['assesstimefinish'], $data->assesstimefinish);
|
|
|
357 |
// No validation for 'ratingtime'. It is only used in to enable/disable assesstime* when adding module.
|
|
|
358 |
|
|
|
359 |
$cm3 = $modinfo->cms[$m3->cmid];
|
|
|
360 |
$this->assertEquals($featurecompletionautomatic['completion'], $cm3->completion);
|
|
|
361 |
$this->assertEquals($featurecompletionautomatic['completionview'], $cm3->completionview);
|
|
|
362 |
$this->assertEquals($featurecompletionautomatic['completionpassgrade'], $cm3->completionpassgrade);
|
|
|
363 |
$this->assertEquals(0, $cm3->completiongradeitemnumber); // Zero instead of default null since 'completionusegrade' was set.
|
|
|
364 |
$gradingitem = \grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id));
|
|
|
365 |
$this->assertEquals(0, $gradingitem->grademin);
|
|
|
366 |
$this->assertEquals($featuregrade['grade'], $gradingitem->grademax);
|
|
|
367 |
$this->assertEquals($featuregrade['gradecat'], $gradingitem->categoryid);
|
|
|
368 |
|
|
|
369 |
$cm4 = $modinfo->cms[$m4->cmid];
|
|
|
370 |
$this->assertEquals($optionsavailability['availability'], $cm4->availability);
|
|
|
371 |
}
|
|
|
372 |
|
11 |
efrain |
373 |
public function test_create_block(): void {
|
1 |
efrain |
374 |
global $CFG;
|
|
|
375 |
if (!file_exists("$CFG->dirroot/blocks/online_users/")) {
|
|
|
376 |
$this->markTestSkipped('Can not find standard Online users block');
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
$this->resetAfterTest(true);
|
|
|
380 |
$generator = $this->getDataGenerator();
|
|
|
381 |
|
|
|
382 |
$page = $generator->create_block('online_users');
|
|
|
383 |
$this->assertNotEmpty($page);
|
|
|
384 |
}
|
|
|
385 |
|
11 |
efrain |
386 |
public function test_enrol_user(): void {
|
1 |
efrain |
387 |
global $DB;
|
|
|
388 |
|
|
|
389 |
$this->resetAfterTest();
|
|
|
390 |
|
|
|
391 |
$selfplugin = enrol_get_plugin('self');
|
|
|
392 |
$this->assertNotEmpty($selfplugin);
|
|
|
393 |
|
|
|
394 |
$manualplugin = enrol_get_plugin('manual');
|
|
|
395 |
$this->assertNotEmpty($manualplugin);
|
|
|
396 |
|
|
|
397 |
// Prepare some data.
|
|
|
398 |
|
|
|
399 |
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
|
|
|
400 |
$this->assertNotEmpty($studentrole);
|
|
|
401 |
$teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
|
|
|
402 |
$this->assertNotEmpty($teacherrole);
|
|
|
403 |
|
|
|
404 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
405 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
406 |
$course3 = $this->getDataGenerator()->create_course();
|
|
|
407 |
|
|
|
408 |
$context1 = \context_course::instance($course1->id);
|
|
|
409 |
$context2 = \context_course::instance($course2->id);
|
|
|
410 |
$context3 = \context_course::instance($course3->id);
|
|
|
411 |
|
|
|
412 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
413 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
414 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
415 |
$user4 = $this->getDataGenerator()->create_user();
|
|
|
416 |
|
|
|
417 |
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
|
|
|
418 |
$instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
|
|
419 |
$instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
|
|
420 |
$instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
|
|
421 |
|
|
|
422 |
$this->assertEquals($studentrole->id, $instance1->roleid);
|
|
|
423 |
$this->assertEquals($studentrole->id, $instance2->roleid);
|
|
|
424 |
$this->assertEquals($studentrole->id, $instance3->roleid);
|
|
|
425 |
|
|
|
426 |
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual')));
|
|
|
427 |
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
428 |
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
429 |
$maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
430 |
$maninstance3->roleid = $teacherrole->id;
|
|
|
431 |
$DB->update_record('enrol', $maninstance3, array('id'=>$maninstance3->id));
|
|
|
432 |
|
|
|
433 |
$this->assertEquals($studentrole->id, $maninstance1->roleid);
|
|
|
434 |
$this->assertEquals($studentrole->id, $maninstance2->roleid);
|
|
|
435 |
$this->assertEquals($teacherrole->id, $maninstance3->roleid);
|
|
|
436 |
|
|
|
437 |
$result = $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
|
|
|
438 |
$this->assertTrue($result);
|
|
|
439 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id)));
|
|
|
440 |
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));
|
|
|
441 |
|
|
|
442 |
$result = $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $teacherrole->id, 'manual');
|
|
|
443 |
$this->assertTrue($result);
|
|
|
444 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user1->id)));
|
|
|
445 |
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
|
|
|
446 |
|
|
|
447 |
$result = $this->getDataGenerator()->enrol_user($user4->id, $course2->id, 'teacher', 'manual');
|
|
|
448 |
$this->assertTrue($result);
|
|
|
449 |
$this->assertTrue($DB->record_exists('user_enrolments',
|
|
|
450 |
array('enrolid' => $maninstance2->id, 'userid' => $user4->id)));
|
|
|
451 |
$this->assertTrue($DB->record_exists('role_assignments',
|
|
|
452 |
array('contextid' => $context2->id, 'userid' => $user4->id, 'roleid' => $teacherrole->id)));
|
|
|
453 |
|
|
|
454 |
$result = $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 0, 'manual');
|
|
|
455 |
$this->assertTrue($result);
|
|
|
456 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id)));
|
|
|
457 |
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id)));
|
|
|
458 |
|
|
|
459 |
$result = $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
|
|
|
460 |
$this->assertTrue($result);
|
|
|
461 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
|
|
|
462 |
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));
|
|
|
463 |
|
|
|
464 |
$selfplugin->add_instance($course2, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id));
|
|
|
465 |
$result = $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
|
|
|
466 |
$this->assertFalse($result);
|
|
|
467 |
|
|
|
468 |
$DB->delete_records('enrol', array('enrol'=>'self', 'courseid'=>$course3->id));
|
|
|
469 |
$result = $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
|
|
|
470 |
$this->assertFalse($result);
|
|
|
471 |
}
|
|
|
472 |
|
11 |
efrain |
473 |
public function test_create_grade_category(): void {
|
1 |
efrain |
474 |
global $DB, $CFG;
|
|
|
475 |
require_once $CFG->libdir . '/grade/constants.php';
|
|
|
476 |
|
|
|
477 |
$this->resetAfterTest(true);
|
|
|
478 |
$generator = $this->getDataGenerator();
|
|
|
479 |
$course = $generator->create_course();
|
|
|
480 |
|
|
|
481 |
// Generate category and make sure number of records in DB table increases.
|
|
|
482 |
// Note we only count grade cats with depth > 1 because the course grade category
|
|
|
483 |
// is lazily created.
|
|
|
484 |
$count = $DB->count_records_select('grade_categories', 'depth <> 1');
|
|
|
485 |
$gradecategory = $generator->create_grade_category(array('courseid'=>$course->id));
|
|
|
486 |
$this->assertEquals($count+1, $DB->count_records_select('grade_categories', 'depth <> 1'));
|
|
|
487 |
$this->assertEquals(2, $gradecategory->depth);
|
|
|
488 |
$this->assertEquals($course->id, $gradecategory->courseid);
|
|
|
489 |
$this->assertEquals('Grade category 1', $gradecategory->fullname);
|
|
|
490 |
|
|
|
491 |
// Generate category and make sure aggregation is set.
|
|
|
492 |
$gradecategory = $generator->create_grade_category(
|
|
|
493 |
array('courseid' => $course->id, 'aggregation' => GRADE_AGGREGATE_MEDIAN));
|
|
|
494 |
$this->assertEquals(GRADE_AGGREGATE_MEDIAN, $gradecategory->aggregation);
|
|
|
495 |
|
|
|
496 |
// Generate category and make sure parent is set.
|
|
|
497 |
$gradecategory2 = $generator->create_grade_category(
|
|
|
498 |
array('courseid' => $course->id,
|
|
|
499 |
'parent' => $gradecategory->id));
|
|
|
500 |
$this->assertEquals($gradecategory->id, $gradecategory2->parent);
|
|
|
501 |
}
|
|
|
502 |
|
11 |
efrain |
503 |
public function test_create_custom_profile_field_category(): void {
|
1 |
efrain |
504 |
global $DB;
|
|
|
505 |
|
|
|
506 |
$this->resetAfterTest();
|
|
|
507 |
$generator = $this->getDataGenerator();
|
|
|
508 |
|
|
|
509 |
// Insert first category without specified sortorder.
|
|
|
510 |
$result = $generator->create_custom_profile_field_category(['name' => 'Frogs']);
|
|
|
511 |
$record = $DB->get_record('user_info_category', ['name' => 'Frogs']);
|
|
|
512 |
$this->assertEquals(1, $record->sortorder);
|
|
|
513 |
|
|
|
514 |
// Also check the return value.
|
|
|
515 |
$this->assertEquals(1, $result->sortorder);
|
|
|
516 |
$this->assertEquals('Frogs', $result->name);
|
|
|
517 |
$this->assertEquals($record->id, $result->id);
|
|
|
518 |
|
|
|
519 |
// Insert next category without specified sortorder.
|
|
|
520 |
$generator->create_custom_profile_field_category(['name' => 'Zombies']);
|
|
|
521 |
$record = $DB->get_record('user_info_category', ['name' => 'Zombies']);
|
|
|
522 |
$this->assertEquals(2, $record->sortorder);
|
|
|
523 |
|
|
|
524 |
// Insert category with specified sortorder.
|
|
|
525 |
$generator->create_custom_profile_field_category(['name' => 'Toads', 'sortorder' => 9]);
|
|
|
526 |
$record = $DB->get_record('user_info_category', ['name' => 'Toads']);
|
|
|
527 |
$this->assertEquals(9, $record->sortorder);
|
|
|
528 |
|
|
|
529 |
// Insert another with unspecified sortorder.
|
|
|
530 |
$generator->create_custom_profile_field_category(['name' => 'Werewolves']);
|
|
|
531 |
$record = $DB->get_record('user_info_category', ['name' => 'Werewolves']);
|
|
|
532 |
$this->assertEquals(10, $record->sortorder);
|
|
|
533 |
}
|
|
|
534 |
|
11 |
efrain |
535 |
public function test_create_custom_profile_field(): void {
|
1 |
efrain |
536 |
global $DB;
|
|
|
537 |
|
|
|
538 |
$this->resetAfterTest();
|
|
|
539 |
$generator = $this->getDataGenerator();
|
|
|
540 |
|
|
|
541 |
// Insert minimal field without specified category.
|
|
|
542 |
$field1 = $generator->create_custom_profile_field(
|
|
|
543 |
['datatype' => 'text', 'shortname' => 'colour', 'name' => 'Colour']);
|
|
|
544 |
$record = $DB->get_record('user_info_field', ['shortname' => 'colour']);
|
|
|
545 |
|
|
|
546 |
// Check specified values.
|
|
|
547 |
$this->assertEquals('Colour', $record->name);
|
|
|
548 |
$this->assertEquals('text', $record->datatype);
|
|
|
549 |
|
|
|
550 |
// Check sortorder (first in category).
|
|
|
551 |
$this->assertEquals(1, $record->sortorder);
|
|
|
552 |
|
|
|
553 |
// Check shared defaults for most datatypes.
|
|
|
554 |
$this->assertEquals('', $record->description);
|
|
|
555 |
$this->assertEquals(0, $record->descriptionformat);
|
|
|
556 |
$this->assertEquals(0, $record->required);
|
|
|
557 |
$this->assertEquals(0, $record->locked);
|
|
|
558 |
$this->assertEquals(PROFILE_VISIBLE_ALL, $record->visible);
|
|
|
559 |
$this->assertEquals(0, $record->forceunique);
|
|
|
560 |
$this->assertEquals(0, $record->signup);
|
|
|
561 |
$this->assertEquals('', $record->defaultdata);
|
|
|
562 |
$this->assertEquals(0, $record->defaultdataformat);
|
|
|
563 |
|
|
|
564 |
// Check specific defaults for text datatype.
|
|
|
565 |
$this->assertEquals(30, $record->param1);
|
|
|
566 |
$this->assertEquals(2048, $record->param2);
|
|
|
567 |
|
|
|
568 |
// Check the returned value matches the database data.
|
|
|
569 |
$this->assertEquals($record, $field1);
|
|
|
570 |
|
|
|
571 |
// The category should relate to a new 'testing' category.
|
|
|
572 |
$catrecord = $DB->get_record('user_info_category', ['id' => $record->categoryid]);
|
|
|
573 |
$this->assertEquals('Testing', $catrecord->name);
|
|
|
574 |
$this->assertEquals(1, $catrecord->sortorder);
|
|
|
575 |
|
|
|
576 |
// Create another field, this time supplying values for a few of the fields.
|
|
|
577 |
$generator->create_custom_profile_field(
|
|
|
578 |
['datatype' => 'text', 'shortname' => 'brightness', 'name' => 'Brightness',
|
|
|
579 |
'required' => 1, 'forceunique' => 1]);
|
|
|
580 |
$record = $DB->get_record('user_info_field', ['shortname' => 'brightness']);
|
|
|
581 |
|
|
|
582 |
// Same testing category, next sortorder.
|
|
|
583 |
$this->assertEquals($catrecord->id, $record->categoryid);
|
|
|
584 |
$this->assertEquals(2, $record->sortorder);
|
|
|
585 |
|
|
|
586 |
// Check modified fields.
|
|
|
587 |
$this->assertEquals(1, $record->required);
|
|
|
588 |
$this->assertEquals(1, $record->forceunique);
|
|
|
589 |
|
|
|
590 |
// Create a field in specified category by id or name...
|
|
|
591 |
$category = $generator->create_custom_profile_field_category(['name' => 'Amphibians']);
|
|
|
592 |
$field3 = $generator->create_custom_profile_field(
|
|
|
593 |
['datatype' => 'text', 'shortname' => 'frog', 'name' => 'Frog',
|
|
|
594 |
'categoryid' => $category->id]);
|
|
|
595 |
$this->assertEquals($category->id, $field3->categoryid);
|
|
|
596 |
$this->assertEquals(1, $field3->sortorder);
|
|
|
597 |
$field4 = $generator->create_custom_profile_field(
|
|
|
598 |
['datatype' => 'text', 'shortname' => 'toad', 'name' => 'Toad',
|
|
|
599 |
'category' => 'Amphibians', 'sortorder' => 4]);
|
|
|
600 |
$this->assertEquals($category->id, $field4->categoryid);
|
|
|
601 |
$this->assertEquals(4, $field4->sortorder);
|
|
|
602 |
|
|
|
603 |
// Check defaults for menu, datetime, and checkbox.
|
|
|
604 |
$field5 = $generator->create_custom_profile_field(
|
|
|
605 |
['datatype' => 'menu', 'shortname' => 'cuisine', 'name' => 'Cuisine']);
|
|
|
606 |
$this->assertEquals("Yes\nNo", $field5->param1);
|
|
|
607 |
$this->assertEquals('No', $field5->defaultdata);
|
|
|
608 |
$field6 = $generator->create_custom_profile_field(
|
|
|
609 |
['datatype' => 'datetime', 'shortname' => 'epoch', 'name' => 'Epoch']);
|
|
|
610 |
$this->assertEquals(2010, $field6->param1);
|
|
|
611 |
$this->assertEquals(2015, $field6->param2);
|
|
|
612 |
$this->assertEquals(1, $field6->param3);
|
|
|
613 |
$field7 = $generator->create_custom_profile_field(
|
|
|
614 |
['datatype' => 'checkbox', 'shortname' => 'areyousure', 'name' => 'Are you sure?']);
|
|
|
615 |
$this->assertEquals(0, $field7->defaultdata);
|
|
|
616 |
|
|
|
617 |
// Check setting options for menu using \n work as expected.
|
|
|
618 |
$field8 = $generator->create_custom_profile_field([
|
|
|
619 |
'datatype' => 'menu', 'shortname' => 'cuisine', 'name' => 'Cuisine', 'param1' => 'French\nChinese\nLebanese',
|
|
|
620 |
'defaultdata' => 'Chinese'
|
|
|
621 |
]);
|
|
|
622 |
$this->assertEquals("French\nChinese\nLebanese", $field8->param1);
|
|
|
623 |
}
|
|
|
624 |
}
|