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