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