[ 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 * Unit tests for info and subclasses. 19 * 20 * @package core_availability 21 * @copyright 2014 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use core_availability\info; 28 use core_availability\info_module; 29 use core_availability\info_section; 30 31 /** 32 * Unit tests for info and subclasses. 33 * 34 * @package core_availability 35 * @copyright 2014 The Open University 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class info_testcase extends \advanced_testcase { 39 public function setUp() { 40 // Load the mock condition so that it can be used. 41 require_once (__DIR__ . '/fixtures/mock_condition.php'); 42 } 43 44 /** 45 * Tests the info_module class (is_available, get_full_information). 46 */ 47 public function test_info_module() { 48 global $DB; 49 50 // Create a course and pages. 51 $this->setAdminUser(); 52 $this->resetAfterTest(); 53 $generator = $this->getDataGenerator(); 54 $course = $generator->create_course(); 55 $rec = array('course' => $course); 56 $page1 = $generator->get_plugin_generator('mod_page')->create_instance($rec); 57 $page2 = $generator->get_plugin_generator('mod_page')->create_instance($rec); 58 $page3 = $generator->get_plugin_generator('mod_page')->create_instance($rec); 59 $page4 = $generator->get_plugin_generator('mod_page')->create_instance($rec); 60 61 // Set up the availability option for the pages to mock options. 62 $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[' . 63 '{"type":"mock","a":false,"m":"grandmaster flash"}]}', array('id' => $page1->cmid)); 64 $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[' . 65 '{"type":"mock","a":true,"m":"the furious five"}]}', array('id' => $page2->cmid)); 66 67 // Third page is invalid. (Fourth has no availability settings.) 68 $DB->set_field('course_modules', 'availability', '{{{', array('id' => $page3->cmid)); 69 70 $modinfo = get_fast_modinfo($course); 71 $cm1 = $modinfo->get_cm($page1->cmid); 72 $cm2 = $modinfo->get_cm($page2->cmid); 73 $cm3 = $modinfo->get_cm($page3->cmid); 74 $cm4 = $modinfo->get_cm($page4->cmid); 75 76 // Do availability and full information checks. 77 $info = new info_module($cm1); 78 $information = ''; 79 $this->assertFalse($info->is_available($information)); 80 $this->assertEquals('SA: grandmaster flash', $information); 81 $this->assertEquals('SA: [FULL]grandmaster flash', $info->get_full_information()); 82 $info = new info_module($cm2); 83 $this->assertTrue($info->is_available($information)); 84 $this->assertEquals('', $information); 85 $this->assertEquals('SA: [FULL]the furious five', $info->get_full_information()); 86 87 // Check invalid one. 88 $info = new info_module($cm3); 89 $this->assertFalse($info->is_available($information)); 90 $debugging = phpunit_util::get_debugging_messages(); 91 phpunit_util::reset_debugging(); 92 $this->assertEquals(1, count($debugging)); 93 $this->assertContains('Invalid availability', $debugging[0]->message); 94 95 // Check empty one. 96 $info = new info_module($cm4); 97 $this->assertTrue($info->is_available($information)); 98 $this->assertEquals('', $information); 99 $this->assertEquals('', $info->get_full_information()); 100 } 101 102 /** 103 * Tests the info_section class (is_available, get_full_information). 104 */ 105 public function test_info_section() { 106 global $DB; 107 108 // Create a course. 109 $this->setAdminUser(); 110 $this->resetAfterTest(); 111 $generator = $this->getDataGenerator(); 112 $course = $generator->create_course( 113 array('numsections' => 4), array('createsections' => true)); 114 115 // Set up the availability option for the sections to mock options. 116 $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' . 117 '{"type":"mock","a":false,"m":"public"}]}', 118 array('course' => $course->id, 'section' => 1)); 119 $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' . 120 '{"type":"mock","a":true,"m":"enemy"}]}', 121 array('course' => $course->id, 'section' => 2)); 122 123 // Third section is invalid. (Fourth has no availability setting.) 124 $DB->set_field('course_sections', 'availability', '{{{', 125 array('course' => $course->id, 'section' => 3)); 126 127 $modinfo = get_fast_modinfo($course); 128 $sections = $modinfo->get_section_info_all(); 129 130 // Do availability and full information checks. 131 $info = new info_section($sections[1]); 132 $information = ''; 133 $this->assertFalse($info->is_available($information)); 134 $this->assertEquals('SA: public', $information); 135 $this->assertEquals('SA: [FULL]public', $info->get_full_information()); 136 $info = new info_section($sections[2]); 137 $this->assertTrue($info->is_available($information)); 138 $this->assertEquals('', $information); 139 $this->assertEquals('SA: [FULL]enemy', $info->get_full_information()); 140 141 // Check invalid one. 142 $info = new info_section($sections[3]); 143 $this->assertFalse($info->is_available($information)); 144 $debugging = phpunit_util::get_debugging_messages(); 145 phpunit_util::reset_debugging(); 146 $this->assertEquals(1, count($debugging)); 147 $this->assertContains('Invalid availability', $debugging[0]->message); 148 149 // Check empty one. 150 $info = new info_section($sections[4]); 151 $this->assertTrue($info->is_available($information)); 152 $this->assertEquals('', $information); 153 $this->assertEquals('', $info->get_full_information()); 154 } 155 156 /** 157 * Tests the is_user_visible() static function in info_module. 158 */ 159 public function test_is_user_visible() { 160 global $CFG, $DB; 161 require_once($CFG->dirroot . '/course/lib.php'); 162 $this->resetAfterTest(); 163 164 // Create a course and some pages: 165 // 0. Invisible due to visible=0. 166 // 1. Availability restriction (mock, set to fail). 167 // 2. Availability restriction on section (mock, set to fail). 168 // 3. Actually visible. 169 $generator = $this->getDataGenerator(); 170 $course = $generator->create_course( 171 array('numsections' => 1), array('createsections' => true)); 172 $rec = array('course' => $course, ); 173 $pages = array(); 174 $pagegen = $generator->get_plugin_generator('mod_page'); 175 $pages[0] = $pagegen->create_instance($rec, array('visible' => 0)); 176 $pages[1] = $pagegen->create_instance($rec); 177 $pages[2] = $pagegen->create_instance($rec); 178 $pages[3] = $pagegen->create_instance($rec); 179 $modinfo = get_fast_modinfo($course); 180 $section = $modinfo->get_section_info(1); 181 $cm = $modinfo->get_cm($pages[2]->cmid); 182 moveto_module($cm, $section); 183 184 // Set the availability restrictions in database. The enableavailability 185 // setting is off so these do not take effect yet. 186 $notavailable = '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}'; 187 $DB->set_field('course_sections', 'availability', 188 $notavailable, array('id' => $section->id)); 189 $DB->set_field('course_modules', 'availability', 190 $notavailable, array('id' => $pages[1]->cmid)); 191 get_fast_modinfo($course, 0, true); 192 193 // Set up 4 users - a teacher and student plus somebody who isn't even 194 // on the course. Also going to use admin user and a spare student to 195 // avoid cache problems. 196 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id'); 197 $teacher = $generator->create_user(); 198 $student = $generator->create_user(); 199 $student2 = $generator->create_user(); 200 $other = $generator->create_user(); 201 $admin = $DB->get_record('user', array('username' => 'admin')); 202 $generator->enrol_user($teacher->id, $course->id, $roleids['teacher']); 203 $generator->enrol_user($student->id, $course->id, $roleids['student']); 204 $generator->enrol_user($student2->id, $course->id, $roleids['student']); 205 206 // Basic case when availability disabled, for visible item. 207 $this->assertTrue(info_module::is_user_visible($pages[3]->cmid, $student->id, false)); 208 209 // Specifying as an object should not make any queries. 210 $cm = $DB->get_record('course_modules', array('id' => $pages[3]->cmid)); 211 $beforequeries = $DB->perf_get_queries(); 212 $this->assertTrue(info_module::is_user_visible($cm, $student->id, false)); 213 $this->assertEquals($beforequeries, $DB->perf_get_queries()); 214 215 // Specifying as cm_info for correct user should not make any more queries 216 // if we have already obtained dynamic data. 217 $modinfo = get_fast_modinfo($course, $student->id); 218 $cminfo = $modinfo->get_cm($cm->id); 219 // This will obtain dynamic data. 220 $name = $cminfo->name; 221 $beforequeries = $DB->perf_get_queries(); 222 $this->assertTrue(info_module::is_user_visible($cminfo, $student->id, false)); 223 $this->assertEquals($beforequeries, $DB->perf_get_queries()); 224 225 // Function does not care if you are in the course (unless $checkcourse). 226 $this->assertTrue(info_module::is_user_visible($cm, $other->id, false)); 227 228 // With $checkcourse, check for enrolled, not enrolled, and admin user. 229 $this->assertTrue(info_module::is_user_visible($cm, $student->id, true)); 230 $this->assertFalse(info_module::is_user_visible($cm, $other->id, true)); 231 $this->assertTrue(info_module::is_user_visible($cm, $admin->id, true)); 232 233 // With availability off, the student can access all except the 234 // visible=0 one. 235 $this->assertFalse(info_module::is_user_visible($pages[0]->cmid, $student->id, false)); 236 $this->assertTrue(info_module::is_user_visible($pages[1]->cmid, $student->id, false)); 237 $this->assertTrue(info_module::is_user_visible($pages[2]->cmid, $student->id, false)); 238 239 // Teacher and admin can even access the visible=0 one. 240 $this->assertTrue(info_module::is_user_visible($pages[0]->cmid, $teacher->id, false)); 241 $this->assertTrue(info_module::is_user_visible($pages[0]->cmid, $admin->id, false)); 242 243 // Now enable availability (and clear cache). 244 $CFG->enableavailability = true; 245 get_fast_modinfo($course, 0, true); 246 247 // Student cannot access the activity restricted by its own or by the 248 // section's availability. 249 $this->assertFalse(info_module::is_user_visible($pages[1]->cmid, $student->id, false)); 250 $this->assertFalse(info_module::is_user_visible($pages[2]->cmid, $student->id, false)); 251 } 252 253 /** 254 * Tests the convert_legacy_fields function used in restore. 255 */ 256 public function test_convert_legacy_fields() { 257 // Check with no availability conditions first. 258 $rec = (object)array('availablefrom' => 0, 'availableuntil' => 0, 259 'groupingid' => 7, 'showavailability' => 1); 260 $this->assertNull(info::convert_legacy_fields($rec, false)); 261 262 // Check same list for a section. 263 $this->assertEquals( 264 '{"op":"&","showc":[false],"c":[{"type":"grouping","id":7}]}', 265 info::convert_legacy_fields($rec, true)); 266 267 // Check groupmembersonly with grouping. 268 $rec->groupmembersonly = 1; 269 $this->assertEquals( 270 '{"op":"&","showc":[false],"c":[{"type":"grouping","id":7}]}', 271 info::convert_legacy_fields($rec, false)); 272 273 // Check groupmembersonly without grouping. 274 $rec->groupingid = 0; 275 $this->assertEquals( 276 '{"op":"&","showc":[false],"c":[{"type":"group"}]}', 277 info::convert_legacy_fields($rec, false)); 278 279 // Check start date. 280 $rec->groupmembersonly = 0; 281 $rec->availablefrom = 123; 282 $this->assertEquals( 283 '{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":123}]}', 284 info::convert_legacy_fields($rec, false)); 285 286 // Start date with show = false. 287 $rec->showavailability = 0; 288 $this->assertEquals( 289 '{"op":"&","showc":[false],"c":[{"type":"date","d":">=","t":123}]}', 290 info::convert_legacy_fields($rec, false)); 291 292 // End date. 293 $rec->showavailability = 1; 294 $rec->availablefrom = 0; 295 $rec->availableuntil = 456; 296 $this->assertEquals( 297 '{"op":"&","showc":[false],"c":[{"type":"date","d":"<","t":456}]}', 298 info::convert_legacy_fields($rec, false)); 299 300 // All together now. 301 $rec->groupingid = 7; 302 $rec->groupmembersonly = 1; 303 $rec->availablefrom = 123; 304 $this->assertEquals( 305 '{"op":"&","showc":[false,true,false],"c":[' . 306 '{"type":"grouping","id":7},' . 307 '{"type":"date","d":">=","t":123},' . 308 '{"type":"date","d":"<","t":456}' . 309 ']}', 310 info::convert_legacy_fields($rec, false)); 311 $this->assertEquals( 312 '{"op":"&","showc":[false,true,false],"c":[' . 313 '{"type":"grouping","id":7},' . 314 '{"type":"date","d":">=","t":123},' . 315 '{"type":"date","d":"<","t":456}' . 316 ']}', 317 info::convert_legacy_fields($rec, false, true)); 318 } 319 320 /** 321 * Tests the add_legacy_availability_condition function used in restore. 322 */ 323 public function test_add_legacy_availability_condition() { 324 // Completion condition tests. 325 $rec = (object)array('sourcecmid' => 7, 'requiredcompletion' => 1); 326 // No previous availability, show = true. 327 $this->assertEquals( 328 '{"op":"&","showc":[true],"c":[{"type":"completion","cm":7,"e":1}]}', 329 info::add_legacy_availability_condition(null, $rec, true)); 330 // No previous availability, show = false. 331 $this->assertEquals( 332 '{"op":"&","showc":[false],"c":[{"type":"completion","cm":7,"e":1}]}', 333 info::add_legacy_availability_condition(null, $rec, false)); 334 335 // Existing availability. 336 $before = '{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":70}]}'; 337 $this->assertEquals( 338 '{"op":"&","showc":[true,true],"c":['. 339 '{"type":"date","d":">=","t":70},' . 340 '{"type":"completion","cm":7,"e":1}' . 341 ']}', 342 info::add_legacy_availability_condition($before, $rec, true)); 343 344 // Grade condition tests. 345 $rec = (object)array('gradeitemid' => 3, 'grademin' => 7, 'grademax' => null); 346 $this->assertEquals( 347 '{"op":"&","showc":[true],"c":[{"type":"grade","id":3,"min":7.00000}]}', 348 info::add_legacy_availability_condition(null, $rec, true)); 349 $rec->grademax = 8; 350 $this->assertEquals( 351 '{"op":"&","showc":[true],"c":[{"type":"grade","id":3,"min":7.00000,"max":8.00000}]}', 352 info::add_legacy_availability_condition(null, $rec, true)); 353 unset($rec->grademax); 354 unset($rec->grademin); 355 $this->assertEquals( 356 '{"op":"&","showc":[true],"c":[{"type":"grade","id":3}]}', 357 info::add_legacy_availability_condition(null, $rec, true)); 358 359 // Note: There is no need to test the grade condition with show 360 // true/false and existing availability, because this uses the same 361 // function. 362 } 363 364 /** 365 * Tests the add_legacy_availability_field_condition function used in restore. 366 */ 367 public function test_add_legacy_availability_field_condition() { 368 // User field, normal operator. 369 $rec = (object)array('userfield' => 'email', 'shortname' => null, 370 'operator' => 'contains', 'value' => '@'); 371 $this->assertEquals( 372 '{"op":"&","showc":[true],"c":[' . 373 '{"type":"profile","op":"contains","sf":"email","v":"@"}]}', 374 info::add_legacy_availability_field_condition(null, $rec, true)); 375 376 // User field, non-value operator. 377 $rec = (object)array('userfield' => 'email', 'shortname' => null, 378 'operator' => 'isempty', 'value' => ''); 379 $this->assertEquals( 380 '{"op":"&","showc":[true],"c":[' . 381 '{"type":"profile","op":"isempty","sf":"email"}]}', 382 info::add_legacy_availability_field_condition(null, $rec, true)); 383 384 // Custom field. 385 $rec = (object)array('userfield' => null, 'shortname' => 'frogtype', 386 'operator' => 'isempty', 'value' => ''); 387 $this->assertEquals( 388 '{"op":"&","showc":[true],"c":[' . 389 '{"type":"profile","op":"isempty","cf":"frogtype"}]}', 390 info::add_legacy_availability_field_condition(null, $rec, true)); 391 } 392 393 /** 394 * Tests the filter_user_list() and get_user_list_sql() functions. 395 */ 396 public function test_filter_user_list() { 397 global $CFG, $DB; 398 require_once($CFG->dirroot . '/course/lib.php'); 399 $this->resetAfterTest(); 400 $CFG->enableavailability = true; 401 402 // Create a course with 2 sections and 2 pages and 3 users. 403 // Availability is set up initially on the 'page/section 2' items. 404 $generator = $this->getDataGenerator(); 405 $course = $generator->create_course( 406 array('numsections' => 2), array('createsections' => true)); 407 $u1 = $generator->create_user(); 408 $u2 = $generator->create_user(); 409 $u3 = $generator->create_user(); 410 $allusers = array($u1->id => $u1, $u2->id => $u2, $u3->id => $u3); 411 $generator->enrol_user($u1->id, $course->id); 412 $generator->enrol_user($u2->id, $course->id); 413 $generator->enrol_user($u3->id, $course->id); 414 415 $pagegen = $generator->get_plugin_generator('mod_page'); 416 $page = $pagegen->create_instance(array('course' => $course)); 417 $page2 = $pagegen->create_instance(array('course' => $course, 418 'availability' => '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . 419 $u2->id . ',' . $u3->id . ']}]}')); 420 $modinfo = get_fast_modinfo($course); 421 $section = $modinfo->get_section_info(1); 422 $section2 = $modinfo->get_section_info(2); 423 $DB->set_field('course_sections', 'availability', 424 '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u1->id . ',' . $u2->id .']}]}', 425 array('id' => $section2->id)); 426 moveto_module($modinfo->get_cm($page2->cmid), $section2); 427 428 // With no restrictions, returns full list. 429 $info = new info_module($modinfo->get_cm($page->cmid)); 430 $this->assertEquals(array($u1->id, $u2->id, $u3->id), 431 array_keys($info->filter_user_list($allusers))); 432 $this->assertEquals(array('', array()), $info->get_user_list_sql(true)); 433 434 // Set an availability restriction in database for section 1. 435 // For the section we set it so it doesn't support filters; for the 436 // module we have a filter. 437 $DB->set_field('course_sections', 'availability', 438 '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}', 439 array('id' => $section->id)); 440 $DB->set_field('course_modules', 'availability', 441 '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u3->id .']}]}', 442 array('id' => $page->cmid)); 443 rebuild_course_cache($course->id, true); 444 $modinfo = get_fast_modinfo($course); 445 446 // Now it should work (for the module). 447 $info = new info_module($modinfo->get_cm($page->cmid)); 448 $expected = array($u3->id); 449 $this->assertEquals($expected, 450 array_keys($info->filter_user_list($allusers))); 451 list ($sql, $params) = $info->get_user_list_sql(); 452 $result = $DB->get_fieldset_sql($sql, $params); 453 sort($result); 454 $this->assertEquals($expected, $result); 455 $info = new info_section($modinfo->get_section_info(1)); 456 $this->assertEquals(array($u1->id, $u2->id, $u3->id), 457 array_keys($info->filter_user_list($allusers))); 458 $this->assertEquals(array('', array()), $info->get_user_list_sql(true)); 459 460 // With availability disabled, module returns full list too. 461 $CFG->enableavailability = false; 462 $info = new info_module($modinfo->get_cm($page->cmid)); 463 $this->assertEquals(array($u1->id, $u2->id, $u3->id), 464 array_keys($info->filter_user_list($allusers))); 465 $this->assertEquals(array('', array()), $info->get_user_list_sql(true)); 466 467 // Check the other section... 468 $CFG->enableavailability = true; 469 $info = new info_section($modinfo->get_section_info(2)); 470 $expected = array($u1->id, $u2->id); 471 $this->assertEquals($expected, array_keys($info->filter_user_list($allusers))); 472 list ($sql, $params) = $info->get_user_list_sql(true); 473 $result = $DB->get_fieldset_sql($sql, $params); 474 sort($result); 475 $this->assertEquals($expected, $result); 476 477 // And the module in that section - which has combined the section and 478 // module restrictions. 479 $info = new info_module($modinfo->get_cm($page2->cmid)); 480 $expected = array($u2->id); 481 $this->assertEquals($expected, array_keys($info->filter_user_list($allusers))); 482 list ($sql, $params) = $info->get_user_list_sql(true); 483 $result = $DB->get_fieldset_sql($sql, $params); 484 sort($result); 485 $this->assertEquals($expected, $result); 486 } 487 }
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 |