Exemplo n.º 1
0
 /**
  * Tests the filter_user_list() and get_user_list_sql() functions.
  */
 public function test_filter_user_list()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/course/lib.php';
     $this->resetAfterTest();
     $CFG->enableavailability = true;
     // Create a course with 2 sections and 2 pages and 3 users.
     // Availability is set up initially on the 'page/section 2' items.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course(array('numsections' => 2), array('createsections' => true));
     $u1 = $generator->create_user();
     $u2 = $generator->create_user();
     $u3 = $generator->create_user();
     $allusers = array($u1->id => $u1, $u2->id => $u2, $u3->id => $u3);
     $generator->enrol_user($u1->id, $course->id);
     $generator->enrol_user($u2->id, $course->id);
     $generator->enrol_user($u3->id, $course->id);
     $pagegen = $generator->get_plugin_generator('mod_page');
     $page = $pagegen->create_instance(array('course' => $course));
     $page2 = $pagegen->create_instance(array('course' => $course, 'availability' => '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u2->id . ',' . $u3->id . ']}]}'));
     $modinfo = get_fast_modinfo($course);
     $section = $modinfo->get_section_info(1);
     $section2 = $modinfo->get_section_info(2);
     $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u1->id . ',' . $u2->id . ']}]}', array('id' => $section2->id));
     moveto_module($modinfo->get_cm($page2->cmid), $section2);
     // With no restrictions, returns full list.
     $info = new info_module($modinfo->get_cm($page->cmid));
     $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers)));
     $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
     // Set an availability restriction in database for section 1.
     // For the section we set it so it doesn't support filters; for the
     // module we have a filter.
     $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}', array('id' => $section->id));
     $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u3->id . ']}]}', array('id' => $page->cmid));
     rebuild_course_cache($course->id, true);
     $modinfo = get_fast_modinfo($course);
     // Now it should work (for the module).
     $info = new info_module($modinfo->get_cm($page->cmid));
     $expected = array($u3->id);
     $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
     list($sql, $params) = $info->get_user_list_sql();
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals($expected, $result);
     $info = new info_section($modinfo->get_section_info(1));
     $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers)));
     $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
     // With availability disabled, module returns full list too.
     $CFG->enableavailability = false;
     $info = new info_module($modinfo->get_cm($page->cmid));
     $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers)));
     $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
     // Check the other section...
     $CFG->enableavailability = true;
     $info = new info_section($modinfo->get_section_info(2));
     $expected = array($u1->id, $u2->id);
     $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
     list($sql, $params) = $info->get_user_list_sql(true);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals($expected, $result);
     // And the module in that section - which has combined the section and
     // module restrictions.
     $info = new info_module($modinfo->get_cm($page2->cmid));
     $expected = array($u2->id);
     $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
     list($sql, $params) = $info->get_user_list_sql(true);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals($expected, $result);
 }
Exemplo n.º 2
0
 /**
  * Tests the filter_users() function.
  */
 public function test_filter_users()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/course/lib.php';
     $this->resetAfterTest();
     $CFG->enableavailability = true;
     // Create a course with 2 sections and 2 pages and 3 users.
     // Availability is set up initially on the 'page/section 2' items.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course(array('numsections' => 2), array('createsections' => true));
     $u1 = $generator->create_user();
     $u2 = $generator->create_user();
     $u3 = $generator->create_user();
     $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'), MUST_EXIST);
     $allusers = array($u1->id => $u1, $u2->id => $u2, $u3->id => $u3);
     $generator->enrol_user($u1->id, $course->id, $studentroleid);
     $generator->enrol_user($u2->id, $course->id, $studentroleid);
     $generator->enrol_user($u3->id, $course->id, $studentroleid);
     // Page 2 allows access to users 2 and 3, while section 2 allows access
     // to users 1 and 2.
     $pagegen = $generator->get_plugin_generator('mod_page');
     $page = $pagegen->create_instance(array('course' => $course));
     $page2 = $pagegen->create_instance(array('course' => $course, 'availability' => '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u2->id . ',' . $u3->id . ']}]}'));
     $modinfo = get_fast_modinfo($course);
     $section = $modinfo->get_section_info(1);
     $section2 = $modinfo->get_section_info(2);
     $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u1->id . ',' . $u2->id . ']}]}', array('id' => $section2->id));
     moveto_module($modinfo->get_cm($page2->cmid), $section2);
     // With no restrictions, returns full list.
     $info = new info_module($modinfo->get_cm($page->cmid));
     $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers)));
     // Set an availability restriction in database for section 1.
     // For the section we set it so it doesn't support filters; for the
     // module we have a filter.
     $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}', array('id' => $section->id));
     $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u3->id . ']}]}', array('id' => $page->cmid));
     rebuild_course_cache($course->id, true);
     $modinfo = get_fast_modinfo($course);
     // Now it should work (for the module).
     $info = new info_module($modinfo->get_cm($page->cmid));
     $this->assertEquals(array($u3->id), array_keys($info->filter_user_list($allusers)));
     $info = new info_section($modinfo->get_section_info(1));
     $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers)));
     // With availability disabled, module returns full list too.
     $CFG->enableavailability = false;
     $info = new info_module($modinfo->get_cm($page->cmid));
     $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers)));
     // Check the other section...
     $CFG->enableavailability = true;
     $info = new info_section($modinfo->get_section_info(2));
     $this->assertEquals(array($u1->id, $u2->id), array_keys($info->filter_user_list($allusers)));
     // And the module in that section - which has combined the section and
     // module restrictions.
     $info = new info_module($modinfo->get_cm($page2->cmid));
     $this->assertEquals(array($u2->id), array_keys($info->filter_user_list($allusers)));
     // If the students have viewhiddenactivities, they get past the module
     // restriction.
     role_change_permission($studentroleid, context_module::instance($page2->cmid), 'moodle/course:viewhiddenactivities', CAP_ALLOW);
     $expected = array($u1->id, $u2->id);
     $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
     // If they have viewhiddensections, they also get past the section
     // restriction.
     role_change_permission($studentroleid, context_course::instance($course->id), 'moodle/course:viewhiddensections', CAP_ALLOW);
     $expected = array($u1->id, $u2->id, $u3->id);
     $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
 }