[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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_plugin_generator_no_component_dir() { 51 $this->setExpectedException('coding_exception', 'Component core_completion does not support ' . 52 'generators yet. Missing tests/generator/lib.php.'); 53 $generator = $this->getDataGenerator()->get_plugin_generator('core_completion'); 54 } 55 56 public function test_create() { 57 global $DB; 58 59 $this->resetAfterTest(true); 60 $generator = $this->getDataGenerator(); 61 62 $count = $DB->count_records('user'); 63 $user = $generator->create_user(); 64 $this->assertEquals($count+1, $DB->count_records('user')); 65 $this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME)); 66 $this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL)); 67 $user = $generator->create_user(array('firstname'=>'Žluťoučký', 'lastname'=>'Koníček')); 68 $this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME)); 69 $this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL)); 70 71 $count = $DB->count_records('course_categories'); 72 $category = $generator->create_category(); 73 $this->assertEquals($count+1, $DB->count_records('course_categories')); 74 $this->assertRegExp('/^Course category \d/', $category->name); 75 $this->assertSame('', $category->idnumber); 76 $this->assertRegExp('/^Test course category \d/', $category->description); 77 $this->assertSame(FORMAT_MOODLE, $category->descriptionformat); 78 79 $count = $DB->count_records('cohort'); 80 $cohort = $generator->create_cohort(); 81 $this->assertEquals($count+1, $DB->count_records('cohort')); 82 $this->assertEquals(context_system::instance()->id, $cohort->contextid); 83 $this->assertRegExp('/^Cohort \d/', $cohort->name); 84 $this->assertSame('', $cohort->idnumber); 85 $this->assertRegExp('/^Test cohort \d/', $cohort->description); 86 $this->assertSame(FORMAT_MOODLE, $cohort->descriptionformat); 87 $this->assertSame('', $cohort->component); 88 $this->assertLessThanOrEqual(time(), $cohort->timecreated); 89 $this->assertSame($cohort->timecreated, $cohort->timemodified); 90 91 $count = $DB->count_records('course'); 92 $course = $generator->create_course(); 93 $this->assertEquals($count+1, $DB->count_records('course')); 94 $this->assertRegExp('/^Test course \d/', $course->fullname); 95 $this->assertRegExp('/^tc_\d/', $course->shortname); 96 $this->assertSame('', $course->idnumber); 97 $this->assertSame('topics', $course->format); 98 $this->assertEquals(0, $course->newsitems); 99 $this->assertEquals(5, $course->numsections); 100 $this->assertRegExp('/^Test course \d/', $course->summary); 101 $this->assertSame(FORMAT_MOODLE, $course->summaryformat); 102 103 $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3)); 104 $this->assertEquals($course->id, $section->course); 105 106 $scale = $generator->create_scale(); 107 $this->assertNotEmpty($scale); 108 } 109 110 public function test_create_module() { 111 global $CFG, $SITE, $DB; 112 if (!file_exists("$CFG->dirroot/mod/page/")) { 113 $this->markTestSkipped('Can not find standard Page module'); 114 } 115 116 $this->resetAfterTest(true); 117 $generator = $this->getDataGenerator(); 118 119 $page = $generator->create_module('page', array('course'=>$SITE->id)); 120 $this->assertNotEmpty($page); 121 $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true); 122 $this->assertEquals(0, $cm->sectionnum); 123 124 $page = $generator->create_module('page', array('course'=>$SITE->id), array('section'=>3)); 125 $this->assertNotEmpty($page); 126 $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true); 127 $this->assertEquals(3, $cm->sectionnum); 128 129 // Prepare environment to generate modules with all possible options. 130 131 // Enable advanced functionality. 132 $CFG->enablecompletion = 1; 133 $CFG->enableavailability = 1; 134 $CFG->enableoutcomes = 1; 135 require_once($CFG->libdir.'/gradelib.php'); 136 require_once($CFG->libdir.'/completionlib.php'); 137 require_once($CFG->dirroot.'/rating/lib.php'); 138 139 // Create a course with enabled completion. 140 $course = $generator->create_course(array('enablecompletion' => true)); 141 142 // Create new grading category in this course. 143 $grade_category = new grade_category(); 144 $grade_category->courseid = $course->id; 145 $grade_category->fullname = 'Grade category'; 146 $grade_category->insert(); 147 148 // Create group and grouping. 149 $group = $generator->create_group(array('courseid' => $course->id)); 150 $grouping = $generator->create_grouping(array('courseid' => $course->id)); 151 $generator->create_grouping_group(array('groupid' => $group->id, 'groupingid' => $grouping->id)); 152 153 // Prepare arrays with properties that we can both use for creating modules and asserting the data in created modules. 154 155 // General properties. 156 $optionsgeneral = array( 157 'visible' => 0, // Note: 'visibleold' will always be set to the same value as 'visible'. 158 'section' => 3, // Note: section will be created if does not exist. 159 // Module supports FEATURE_IDNUMBER. 160 'cmidnumber' => 'IDNUM', // Note: alternatively can have key 'idnumber'. 161 // Module supports FEATURE_GROUPS; 162 'groupmode' => SEPARATEGROUPS, // Note: will be reset to 0 if course groupmodeforce is set. 163 // Module supports FEATURE_GROUPINGS. 164 'groupingid' => $grouping->id, 165 ); 166 167 // In case completion is enabled on site and for course every module can have manual completion. 168 $featurecompletionmanual = array( 169 'completion' => COMPLETION_TRACKING_MANUAL, // "Students can manually mark activity as completed." 170 'completionexpected' => time() + 7 * DAYSECS, 171 ); 172 173 // Automatic completion is possible if module supports FEATURE_COMPLETION_TRACKS_VIEWS or FEATURE_GRADE_HAS_GRADE. 174 // Note: completionusegrade is stored in DB and can be found in cm_info as 'completiongradeitemnumber' - either NULL or 0. 175 // Note: module can have more autocompletion rules as defined in moodleform_mod::add_completion_rules(). 176 $featurecompletionautomatic = array( 177 'completion' => COMPLETION_TRACKING_AUTOMATIC, // "Show activity as complete when conditions are met." 178 'completionview' => 1, // "Student must view this activity to complete it" 179 'completionusegrade' => 1, // "Student must receive a grade to complete this activity" 180 ); 181 182 // Module supports FEATURE_RATE: 183 $featurerate = array( 184 'assessed' => RATING_AGGREGATE_AVERAGE, // "Aggregate type" 185 'scale' => 100, // Either max grade or negative number for scale id. 186 'ratingtime' => 1, // "Restrict ratings to items with dates in this range". 187 'assesstimestart' => time() - DAYSECS, // Note: Will be ignored if neither 'assessed' nor 'ratingtime' is set. 188 'assesstimefinish' => time() + DAYSECS, // Note: Will be ignored if neither 'assessed' nor 'ratingtime' is set. 189 ); 190 191 // Module supports FEATURE_GRADE_HAS_GRADE: 192 $featuregrade = array( 193 'grade' => 10, 194 'gradecat' => $grade_category->id, // Note: if $CFG->enableoutcomes is set, this can be set to -1 to automatically create new grade category. 195 ); 196 197 // Now let's create several modules with different options. 198 $m1 = $generator->create_module('assign', 199 array('course' => $course->id) + 200 $optionsgeneral); 201 $m2 = $generator->create_module('data', 202 array('course' => $course->id) + 203 $featurecompletionmanual + 204 $featurerate); 205 $m3 = $generator->create_module('assign', 206 array('course' => $course->id) + 207 $featurecompletionautomatic + 208 $featuregrade); 209 210 // We need id of the grading item for the second module to create availability dependency in the 3rd module. 211 $gradingitem = grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id)); 212 213 // Now prepare option to create the 4th module with an availability condition. 214 $optionsavailability = array( 215 'availability' => '{"op":"&","showc":[true],"c":[' . 216 '{"type":"date","d":">=","t":' . (time() - WEEKSECS) . '}]}', 217 ); 218 219 // Create module with conditional availability. 220 $m4 = $generator->create_module('assign', 221 array('course' => $course->id) + 222 $optionsavailability 223 ); 224 225 // Verifying that everything is generated correctly. 226 $modinfo = get_fast_modinfo($course->id); 227 $cm1 = $modinfo->cms[$m1->cmid]; 228 $this->assertEquals($optionsgeneral['visible'], $cm1->visible); 229 $this->assertEquals($optionsgeneral['section'], $cm1->sectionnum); // Note difference in key. 230 $this->assertEquals($optionsgeneral['cmidnumber'], $cm1->idnumber); // Note difference in key. 231 $this->assertEquals($optionsgeneral['groupmode'], $cm1->groupmode); 232 $this->assertEquals($optionsgeneral['groupingid'], $cm1->groupingid); 233 234 $cm2 = $modinfo->cms[$m2->cmid]; 235 $this->assertEquals($featurecompletionmanual['completion'], $cm2->completion); 236 $this->assertEquals($featurecompletionmanual['completionexpected'], $cm2->completionexpected); 237 $this->assertEquals(null, $cm2->completiongradeitemnumber); 238 // Rating info is stored in the module's table (in our test {data}). 239 $data = $DB->get_record('data', array('id' => $m2->id)); 240 $this->assertEquals($featurerate['assessed'], $data->assessed); 241 $this->assertEquals($featurerate['scale'], $data->scale); 242 $this->assertEquals($featurerate['assesstimestart'], $data->assesstimestart); 243 $this->assertEquals($featurerate['assesstimefinish'], $data->assesstimefinish); 244 // No validation for 'ratingtime'. It is only used in to enable/disable assesstime* when adding module. 245 246 $cm3 = $modinfo->cms[$m3->cmid]; 247 $this->assertEquals($featurecompletionautomatic['completion'], $cm3->completion); 248 $this->assertEquals($featurecompletionautomatic['completionview'], $cm3->completionview); 249 $this->assertEquals(0, $cm3->completiongradeitemnumber); // Zero instead of default null since 'completionusegrade' was set. 250 $gradingitem = grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id)); 251 $this->assertEquals(0, $gradingitem->grademin); 252 $this->assertEquals($featuregrade['grade'], $gradingitem->grademax); 253 $this->assertEquals($featuregrade['gradecat'], $gradingitem->categoryid); 254 255 $cm4 = $modinfo->cms[$m4->cmid]; 256 $this->assertEquals($optionsavailability['availability'], $cm4->availability); 257 } 258 259 public function test_create_block() { 260 global $CFG; 261 if (!file_exists("$CFG->dirroot/blocks/online_users/")) { 262 $this->markTestSkipped('Can not find standard Online users block'); 263 } 264 265 $this->resetAfterTest(true); 266 $generator = $this->getDataGenerator(); 267 268 $page = $generator->create_block('online_users'); 269 $this->assertNotEmpty($page); 270 } 271 272 public function test_enrol_user() { 273 global $DB; 274 275 $this->resetAfterTest(); 276 277 $selfplugin = enrol_get_plugin('self'); 278 $this->assertNotEmpty($selfplugin); 279 280 $manualplugin = enrol_get_plugin('manual'); 281 $this->assertNotEmpty($manualplugin); 282 283 // Prepare some data. 284 285 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 286 $this->assertNotEmpty($studentrole); 287 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 288 $this->assertNotEmpty($teacherrole); 289 290 $course1 = $this->getDataGenerator()->create_course(); 291 $course2 = $this->getDataGenerator()->create_course(); 292 $course3 = $this->getDataGenerator()->create_course(); 293 294 $context1 = context_course::instance($course1->id); 295 $context2 = context_course::instance($course2->id); 296 $context3 = context_course::instance($course3->id); 297 298 $user1 = $this->getDataGenerator()->create_user(); 299 $user2 = $this->getDataGenerator()->create_user(); 300 $user3 = $this->getDataGenerator()->create_user(); 301 302 $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self'))); 303 $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST); 304 $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST); 305 $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST); 306 307 $this->assertEquals($studentrole->id, $instance1->roleid); 308 $this->assertEquals($studentrole->id, $instance2->roleid); 309 $this->assertEquals($studentrole->id, $instance3->roleid); 310 311 $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual'))); 312 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); 313 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST); 314 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST); 315 $maninstance3->roleid = $teacherrole->id; 316 $DB->update_record('enrol', $maninstance3, array('id'=>$maninstance3->id)); 317 318 $this->assertEquals($studentrole->id, $maninstance1->roleid); 319 $this->assertEquals($studentrole->id, $maninstance2->roleid); 320 $this->assertEquals($teacherrole->id, $maninstance3->roleid); 321 322 $result = $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 323 $this->assertTrue($result); 324 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id))); 325 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id))); 326 327 $result = $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $teacherrole->id, 'manual'); 328 $this->assertTrue($result); 329 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user1->id))); 330 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id))); 331 332 $result = $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 0, 'manual'); 333 $this->assertTrue($result); 334 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id))); 335 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id))); 336 337 $result = $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self'); 338 $this->assertTrue($result); 339 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id))); 340 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id))); 341 342 $selfplugin->add_instance($course2, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id)); 343 $result = $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self'); 344 $this->assertFalse($result); 345 346 $DB->delete_records('enrol', array('enrol'=>'self', 'courseid'=>$course3->id)); 347 $result = $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self'); 348 $this->assertFalse($result); 349 } 350 351 public function test_create_grade_category() { 352 global $DB, $CFG; 353 require_once $CFG->libdir . '/grade/constants.php'; 354 355 $this->resetAfterTest(true); 356 $generator = $this->getDataGenerator(); 357 $course = $generator->create_course(); 358 359 // Generate category and make sure number of records in DB table increases. 360 // Note we only count grade cats with depth > 1 because the course grade category 361 // is lazily created. 362 $count = $DB->count_records_select('grade_categories', 'depth <> 1'); 363 $gradecategory = $generator->create_grade_category(array('courseid'=>$course->id)); 364 $this->assertEquals($count+1, $DB->count_records_select('grade_categories', 'depth <> 1')); 365 $this->assertEquals(2, $gradecategory->depth); 366 $this->assertEquals($course->id, $gradecategory->courseid); 367 $this->assertEquals('Grade category 1', $gradecategory->fullname); 368 369 // Generate category and make sure aggregation is set. 370 $gradecategory = $generator->create_grade_category( 371 array('courseid' => $course->id, 'aggregation' => GRADE_AGGREGATE_MEDIAN)); 372 $this->assertEquals(GRADE_AGGREGATE_MEDIAN, $gradecategory->aggregation); 373 374 // Generate category and make sure parent is set. 375 $gradecategory2 = $generator->create_grade_category( 376 array('courseid' => $course->id, 377 'parent' => $gradecategory->id)); 378 $this->assertEquals($gradecategory->id, $gradecategory2->parent); 379 } 380 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |