See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310]
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 generators tests 19 * 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 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 29 /** 30 * Test data generator 31 * 32 * @package core 33 * @category phpunit 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 } 42 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 } 49 50 public function test_get_default_generator() { 51 $generator = $this->getDataGenerator()->get_plugin_generator('block_somethingthatdoesnotexist'); 52 $this->assertInstanceOf('default_block_generator', $generator); 53 } 54 55 /** 56 * Test plugin generator, with no component directory. 57 * 58 * @expectedException coding_exception 59 * @expectedExceptionMessage Component core_completion does not support generators yet. Missing tests/generator/lib.php. 60 */ 61 public function test_get_plugin_generator_no_component_dir() { 62 $generator = $this->getDataGenerator()->get_plugin_generator('core_completion'); 63 } 64 65 public function test_create_user() { 66 global $DB, $CFG; 67 require_once($CFG->dirroot.'/user/lib.php'); 68 69 $this->resetAfterTest(true); 70 $generator = $this->getDataGenerator(); 71 72 $count = $DB->count_records('user'); 73 $this->setCurrentTimeStart(); 74 $user = $generator->create_user(); 75 $this->assertEquals($count + 1, $DB->count_records('user')); 76 $this->assertSame($user->username, core_user::clean_field($user->username, 'username')); 77 $this->assertSame($user->email, core_user::clean_field($user->email, 'email')); 78 $this->assertNotEmpty($user->firstnamephonetic); 79 $this->assertNotEmpty($user->lastnamephonetic); 80 $this->assertNotEmpty($user->alternatename); 81 $this->assertNotEmpty($user->middlename); 82 $this->assertSame('manual', $user->auth); 83 $this->assertSame('en', $user->lang); 84 $this->assertSame('1', $user->confirmed); 85 $this->assertSame('0', $user->deleted); 86 $this->assertTimeCurrent($user->timecreated); 87 $this->assertSame($user->timecreated, $user->timemodified); 88 $this->assertSame('0.0.0.0', $user->lastip); 89 90 $record = array( 91 'auth' => 'email', 92 'firstname' => 'Žluťoučký', 93 'lastname' => 'Koníček', 94 'firstnamephonetic' => 'Zhlutyoucky', 95 'lastnamephonetic' => 'Koniiczek', 96 'middlename' => 'Hopper', 97 'alternatename' => 'horse', 98 'idnumber' => 'abc1', 99 'mnethostid' => (string)$CFG->mnet_localhost_id, 100 'username' => 'konic666', 101 'password' => 'password1', 102 'email' => 'email@example.com', 103 'confirmed' => '1', 104 'maildisplay' => '1', 105 'mailformat' => '0', 106 'maildigest' => '1', 107 'autosubscribe' => '0', 108 'trackforums' => '0', 109 'deleted' => '0', 110 'timecreated' => '666', 111 ); 112 $user = $generator->create_user($record); 113 $this->assertEquals($count + 2, $DB->count_records('user')); 114 foreach ($record as $k => $v) { 115 if ($k === 'password') { 116 $this->assertTrue(password_verify($v, $user->password)); 117 } else { 118 $this->assertSame($v, $user->{$k}); 119 } 120 } 121 122 $record = array( 123 'firstname' => 'Some', 124 'lastname' => 'User', 125 'idnumber' => 'def', 126 'username' => 'user666', 127 'email' => 'email666@example.com', 128 'deleted' => '1', 129 ); 130 $user = $generator->create_user($record); 131 $this->assertEquals($count + 3, $DB->count_records('user')); 132 $this->assertSame('', $user->idnumber); 133 $this->assertSame(md5($record['username']), $user->email); 134 $this->assertEquals(1, $user->deleted); 135 136 // Test generating user with interests. 137 $user = $generator->create_user(array('interests' => 'Cats, Dogs')); 138 $userdetails = user_get_user_details($user); 139 $this->assertSame('Cats, Dogs', $userdetails['interests']); 140 } 141 142 public function test_create() { 143 global $DB; 144 145 $this->resetAfterTest(true); 146 $generator = $this->getDataGenerator(); 147 148 $count = $DB->count_records('course_categories'); 149 $category = $generator->create_category(); 150 $this->assertEquals($count+1, $DB->count_records('course_categories')); 151 $this->assertRegExp('/^Course category \d/', $category->name); 152 $this->assertSame('', $category->idnumber); 153 $this->assertRegExp('/^Test course category \d/', $category->description); 154 $this->assertSame(FORMAT_MOODLE, $category->descriptionformat); 155 156 $count = $DB->count_records('cohort'); 157 $cohort = $generator->create_cohort(); 158 $this->assertEquals($count+1, $DB->count_records('cohort')); 159 $this->assertEquals(context_system::instance()->id, $cohort->contextid); 160 $this->assertRegExp('/^Cohort \d/', $cohort->name); 161 $this->assertSame('', $cohort->idnumber); 162 $this->assertRegExp("/^Description for '{$cohort->name}' \\n/", $cohort->description); 163 $this->assertSame(FORMAT_MOODLE, $cohort->descriptionformat); 164 $this->assertSame('', $cohort->component); 165 $this->assertLessThanOrEqual(time(), $cohort->timecreated); 166 $this->assertSame($cohort->timecreated, $cohort->timemodified); 167 168 $count = $DB->count_records('course'); 169 $course = $generator->create_course(); 170 $this->assertEquals($count+1, $DB->count_records('course')); 171 $this->assertRegExp('/^Test course \d/', $course->fullname); 172 $this->assertRegExp('/^tc_\d/', $course->shortname); 173 $this->assertSame('', $course->idnumber); 174 $this->assertSame('topics', $course->format); 175 $this->assertEquals(0, $course->newsitems); 176 $this->assertEquals(5, course_get_format($course)->get_last_section_number()); 177 $this->assertRegExp('/^Test course \d/', $course->summary); 178 $this->assertSame(FORMAT_MOODLE, $course->summaryformat); 179 180 $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3)); 181 $this->assertEquals($course->id, $section->course); 182 183 $course = $generator->create_course(array('tags' => 'Cat, Dog')); 184 $this->assertEquals(array('Cat', 'Dog'), array_values(core_tag_tag::get_item_tags_array('core', 'course', $course->id))); 185 186 $scale = $generator->create_scale(); 187 $this->assertNotEmpty($scale); 188 } 189 190 public function test_create_module() { 191 global $CFG, $SITE, $DB; 192 193 $this->setAdminUser(); 194 195 if (!file_exists("$CFG->dirroot/mod/page/")) { 196 $this->markTestSkipped('Can not find standard Page module'); 197 } 198 199 $this->resetAfterTest(true); 200 $generator = $this->getDataGenerator(); 201 202 $page = $generator->create_module('page', array('course'=>$SITE->id)); 203 $this->assertNotEmpty($page); 204 $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true); 205 $this->assertEquals(0, $cm->sectionnum); 206 207 $page = $generator->create_module('page', array('course'=>$SITE->id), array('section'=>3)); 208 $this->assertNotEmpty($page); 209 $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true); 210 $this->assertEquals(3, $cm->sectionnum); 211 212 $page = $generator->create_module('page', array('course' => $SITE->id, 'tags' => 'Cat, Dog')); 213 $this->assertEquals(array('Cat', 'Dog'), 214 array_values(core_tag_tag::get_item_tags_array('core', 'course_modules', $page->cmid))); 215 216 // Prepare environment to generate modules with all possible options. 217 218 // Enable advanced functionality. 219 $CFG->enablecompletion = 1; 220 $CFG->enableavailability = 1; 221 $CFG->enableoutcomes = 1; 222 require_once($CFG->libdir.'/gradelib.php'); 223 require_once($CFG->libdir.'/completionlib.php'); 224 require_once($CFG->dirroot.'/rating/lib.php'); 225 226 // Create a course with enabled completion. 227 $course = $generator->create_course(array('enablecompletion' => true)); 228 229 // Create new grading category in this course. 230 $grade_category = new grade_category(); 231 $grade_category->courseid = $course->id; 232 $grade_category->fullname = 'Grade category'; 233 $grade_category->insert(); 234 235 // Create group and grouping. 236 $group = $generator->create_group(array('courseid' => $course->id)); 237 $grouping = $generator->create_grouping(array('courseid' => $course->id)); 238 $generator->create_grouping_group(array('groupid' => $group->id, 'groupingid' => $grouping->id)); 239 240 // Prepare arrays with properties that we can both use for creating modules and asserting the data in created modules. 241 242 // General properties. 243 $optionsgeneral = array( 244 'visible' => 0, // Note: 'visibleold' will always be set to the same value as 'visible'. 245 'section' => 3, // Note: section will be created if does not exist. 246 // Module supports FEATURE_IDNUMBER. 247 'cmidnumber' => 'IDNUM', // Note: alternatively can have key 'idnumber'. 248 // Module supports FEATURE_GROUPS; 249 'groupmode' => SEPARATEGROUPS, // Note: will be reset to 0 if course groupmodeforce is set. 250 // Module supports FEATURE_GROUPINGS. 251 'groupingid' => $grouping->id, 252 ); 253 254 // In case completion is enabled on site and for course every module can have manual completion. 255 $featurecompletionmanual = array( 256 'completion' => COMPLETION_TRACKING_MANUAL, // "Students can manually mark activity as completed." 257 'completionexpected' => time() + 7 * DAYSECS, 258 ); 259 260 // Automatic completion is possible if module supports FEATURE_COMPLETION_TRACKS_VIEWS or FEATURE_GRADE_HAS_GRADE. 261 // Note: completionusegrade is stored in DB and can be found in cm_info as 'completiongradeitemnumber' - either NULL or 0. 262 // Note: module can have more autocompletion rules as defined in moodleform_mod::add_completion_rules(). 263 $featurecompletionautomatic = array( 264 'completion' => COMPLETION_TRACKING_AUTOMATIC, // "Show activity as complete when conditions are met." 265 'completionview' => 1, // "Student must view this activity to complete it" 266 'completionusegrade' => 1, // "Student must receive a grade to complete this activity" 267 ); 268 269 // Module supports FEATURE_RATE: 270 $featurerate = array( 271 'assessed' => RATING_AGGREGATE_AVERAGE, // "Aggregate type" 272 'scale' => 100, // Either max grade or negative number for scale id. 273 'ratingtime' => 1, // "Restrict ratings to items with dates in this range". 274 'assesstimestart' => time() - DAYSECS, // Note: Will be ignored if neither 'assessed' nor 'ratingtime' is set. 275 'assesstimefinish' => time() + DAYSECS, // Note: Will be ignored if neither 'assessed' nor 'ratingtime' is set. 276 ); 277 278 // Module supports FEATURE_GRADE_HAS_GRADE: 279 $featuregrade = array( 280 'grade' => 10, 281 'gradecat' => $grade_category->id, // Note: if $CFG->enableoutcomes is set, this can be set to -1 to automatically create new grade category. 282 ); 283 284 // Now let's create several modules with different options. 285 $m1 = $generator->create_module('assign', 286 array('course' => $course->id) + 287 $optionsgeneral); 288 $m2 = $generator->create_module('data', 289 array('course' => $course->id) + 290 $featurecompletionmanual + 291 $featurerate); 292 $m3 = $generator->create_module('assign', 293 array('course' => $course->id) + 294 $featurecompletionautomatic + 295 $featuregrade); 296 297 // We need id of the grading item for the second module to create availability dependency in the 3rd module. 298 $gradingitem = grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id)); 299 300 // Now prepare option to create the 4th module with an availability condition. 301 $optionsavailability = array( 302 'availability' => '{"op":"&","showc":[true],"c":[' . 303 '{"type":"date","d":">=","t":' . (time() - WEEKSECS) . '}]}', 304 ); 305 306 // Create module with conditional availability. 307 $m4 = $generator->create_module('assign', 308 array('course' => $course->id) + 309 $optionsavailability 310 ); 311 312 // Verifying that everything is generated correctly. 313 $modinfo = get_fast_modinfo($course->id); 314 $cm1 = $modinfo->cms[$m1->cmid]; 315 $this->assertEquals($optionsgeneral['visible'], $cm1->visible); 316 $this->assertEquals($optionsgeneral['section'], $cm1->sectionnum); // Note difference in key. 317 $this->assertEquals($optionsgeneral['cmidnumber'], $cm1->idnumber); // Note difference in key. 318 $this->assertEquals($optionsgeneral['groupmode'], $cm1->groupmode); 319 $this->assertEquals($optionsgeneral['groupingid'], $cm1->groupingid); 320 321 $cm2 = $modinfo->cms[$m2->cmid]; 322 $this->assertEquals($featurecompletionmanual['completion'], $cm2->completion); 323 $this->assertEquals($featurecompletionmanual['completionexpected'], $cm2->completionexpected); 324 $this->assertEquals(null, $cm2->completiongradeitemnumber); 325 // Rating info is stored in the module's table (in our test {data}). 326 $data = $DB->get_record('data', array('id' => $m2->id)); 327 $this->assertEquals($featurerate['assessed'], $data->assessed); 328 $this->assertEquals($featurerate['scale'], $data->scale); 329 $this->assertEquals($featurerate['assesstimestart'], $data->assesstimestart); 330 $this->assertEquals($featurerate['assesstimefinish'], $data->assesstimefinish); 331 // No validation for 'ratingtime'. It is only used in to enable/disable assesstime* when adding module. 332 333 $cm3 = $modinfo->cms[$m3->cmid]; 334 $this->assertEquals($featurecompletionautomatic['completion'], $cm3->completion); 335 $this->assertEquals($featurecompletionautomatic['completionview'], $cm3->completionview); 336 $this->assertEquals(0, $cm3->completiongradeitemnumber); // Zero instead of default null since 'completionusegrade' was set. 337 $gradingitem = grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id)); 338 $this->assertEquals(0, $gradingitem->grademin); 339 $this->assertEquals($featuregrade['grade'], $gradingitem->grademax); 340 $this->assertEquals($featuregrade['gradecat'], $gradingitem->categoryid); 341 342 $cm4 = $modinfo->cms[$m4->cmid]; 343 $this->assertEquals($optionsavailability['availability'], $cm4->availability); 344 } 345 346 public function test_create_block() { 347 global $CFG; 348 if (!file_exists("$CFG->dirroot/blocks/online_users/")) { 349 $this->markTestSkipped('Can not find standard Online users block'); 350 } 351 352 $this->resetAfterTest(true); 353 $generator = $this->getDataGenerator(); 354 355 $page = $generator->create_block('online_users'); 356 $this->assertNotEmpty($page); 357 } 358 359 public function test_enrol_user() { 360 global $DB; 361 362 $this->resetAfterTest(); 363 364 $selfplugin = enrol_get_plugin('self'); 365 $this->assertNotEmpty($selfplugin); 366 367 $manualplugin = enrol_get_plugin('manual'); 368 $this->assertNotEmpty($manualplugin); 369 370 // Prepare some data. 371 372 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 373 $this->assertNotEmpty($studentrole); 374 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 375 $this->assertNotEmpty($teacherrole); 376 377 $course1 = $this->getDataGenerator()->create_course(); 378 $course2 = $this->getDataGenerator()->create_course(); 379 $course3 = $this->getDataGenerator()->create_course(); 380 381 $context1 = context_course::instance($course1->id); 382 $context2 = context_course::instance($course2->id); 383 $context3 = context_course::instance($course3->id); 384 385 $user1 = $this->getDataGenerator()->create_user(); 386 $user2 = $this->getDataGenerator()->create_user(); 387 $user3 = $this->getDataGenerator()->create_user(); 388 $user4 = $this->getDataGenerator()->create_user(); 389 390 $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self'))); 391 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST); 392 $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST); 393 $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST); 394 395 $this->assertEquals($studentrole->id, $instance1->roleid); 396 $this->assertEquals($studentrole->id, $instance2->roleid); 397 $this->assertEquals($studentrole->id, $instance3->roleid); 398 399 $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual'))); 400 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 401 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 402 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 403 $maninstance3->roleid = $teacherrole->id; 404 $DB->update_record('enrol', $maninstance3, array('id'=>$maninstance3->id)); 405 406 $this->assertEquals($studentrole->id, $maninstance1->roleid); 407 $this->assertEquals($studentrole->id, $maninstance2->roleid); 408 $this->assertEquals($teacherrole->id, $maninstance3->roleid); 409 410 $result = $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 411 $this->assertTrue($result); 412 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id))); 413 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id))); 414 415 $result = $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $teacherrole->id, 'manual'); 416 $this->assertTrue($result); 417 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user1->id))); 418 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id))); 419 420 $result = $this->getDataGenerator()->enrol_user($user4->id, $course2->id, 'teacher', 'manual'); 421 $this->assertTrue($result); 422 $this->assertTrue($DB->record_exists('user_enrolments', 423 array('enrolid' => $maninstance2->id, 'userid' => $user4->id))); 424 $this->assertTrue($DB->record_exists('role_assignments', 425 array('contextid' => $context2->id, 'userid' => $user4->id, 'roleid' => $teacherrole->id))); 426 427 $result = $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 0, 'manual'); 428 $this->assertTrue($result); 429 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id))); 430 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id))); 431 432 $result = $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self'); 433 $this->assertTrue($result); 434 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id))); 435 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id))); 436 437 $selfplugin->add_instance($course2, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id)); 438 $result = $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self'); 439 $this->assertFalse($result); 440 441 $DB->delete_records('enrol', array('enrol'=>'self', 'courseid'=>$course3->id)); 442 $result = $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self'); 443 $this->assertFalse($result); 444 } 445 446 public function test_create_grade_category() { 447 global $DB, $CFG; 448 require_once $CFG->libdir . '/grade/constants.php'; 449 450 $this->resetAfterTest(true); 451 $generator = $this->getDataGenerator(); 452 $course = $generator->create_course(); 453 454 // Generate category and make sure number of records in DB table increases. 455 // Note we only count grade cats with depth > 1 because the course grade category 456 // is lazily created. 457 $count = $DB->count_records_select('grade_categories', 'depth <> 1'); 458 $gradecategory = $generator->create_grade_category(array('courseid'=>$course->id)); 459 $this->assertEquals($count+1, $DB->count_records_select('grade_categories', 'depth <> 1')); 460 $this->assertEquals(2, $gradecategory->depth); 461 $this->assertEquals($course->id, $gradecategory->courseid); 462 $this->assertEquals('Grade category 1', $gradecategory->fullname); 463 464 // Generate category and make sure aggregation is set. 465 $gradecategory = $generator->create_grade_category( 466 array('courseid' => $course->id, 'aggregation' => GRADE_AGGREGATE_MEDIAN)); 467 $this->assertEquals(GRADE_AGGREGATE_MEDIAN, $gradecategory->aggregation); 468 469 // Generate category and make sure parent is set. 470 $gradecategory2 = $generator->create_grade_category( 471 array('courseid' => $course->id, 472 'parent' => $gradecategory->id)); 473 $this->assertEquals($gradecategory->id, $gradecategory2->parent); 474 } 475 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body