Example #1
0
 /**
  * Tests the filter_users (bulk checking) function. Also tests the SQL
  * variant get_user_list_sql.
  */
 public function test_filter_users()
 {
     global $DB;
     $this->resetAfterTest();
     // Erase static cache before test.
     condition::wipe_static_cache();
     // Make a test course and some users.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course();
     $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
     $teacher = $generator->create_user();
     $generator->enrol_user($teacher->id, $course->id, $roleids['editingteacher']);
     $allusers = array($teacher->id => $teacher);
     $students = array();
     for ($i = 0; $i < 3; $i++) {
         $student = $generator->create_user();
         $students[$i] = $student;
         $generator->enrol_user($student->id, $course->id, $roleids['student']);
         $allusers[$student->id] = $student;
     }
     $info = new \core_availability\mock_info($course);
     // Make test groups.
     $group1 = $generator->create_group(array('courseid' => $course->id));
     $group2 = $generator->create_group(array('courseid' => $course->id));
     // Assign students to groups as follows (teacher is not in a group):
     // 0: no groups.
     // 1: in group 1.
     // 2: in group 2.
     groups_add_member($group1, $students[1]);
     groups_add_member($group2, $students[2]);
     // Test 'any group' condition.
     $checker = new \core_availability\capability_checker($info->get_context());
     $cond = new condition((object) array());
     $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
     ksort($result);
     $expected = array($teacher->id, $students[1]->id, $students[2]->id);
     $this->assertEquals($expected, $result);
     // Test it with get_user_list_sql.
     list($sql, $params) = $cond->get_user_list_sql(false, $info, true);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals($expected, $result);
     // Test NOT version (note that teacher can still access because AAG works
     // both ways).
     $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
     ksort($result);
     $expected = array($teacher->id, $students[0]->id);
     $this->assertEquals($expected, $result);
     // Test with get_user_list_sql.
     list($sql, $params) = $cond->get_user_list_sql(true, $info, true);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals($expected, $result);
     // Test specific group.
     $cond = new condition((object) array('id' => (int) $group1->id));
     $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
     ksort($result);
     $expected = array($teacher->id, $students[1]->id);
     $this->assertEquals($expected, $result);
     list($sql, $params) = $cond->get_user_list_sql(false, $info, true);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals($expected, $result);
     $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
     ksort($result);
     $expected = array($teacher->id, $students[0]->id, $students[2]->id);
     $this->assertEquals($expected, $result);
     list($sql, $params) = $cond->get_user_list_sql(true, $info, true);
     $result = $DB->get_fieldset_sql($sql, $params);
     sort($result);
     $this->assertEquals($expected, $result);
 }
Example #2
0
 /**
  * Tests the filter_users (bulk checking) function.
  */
 public function test_filter_users()
 {
     global $DB, $CFG;
     $this->resetAfterTest();
     $CFG->enableavailability = true;
     // Erase static cache before test.
     condition::wipe_static_cache();
     // Make a test course and some users.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course();
     $student1 = $generator->create_user(array('institution' => 'Unseen University'));
     $student2 = $generator->create_user(array('institution' => 'Hogwarts'));
     $student3 = $generator->create_user(array('institution' => 'Unseen University'));
     $allusers = array();
     foreach (array($student1, $student2, $student3) as $student) {
         $generator->enrol_user($student->id, $course->id);
         $allusers[$student->id] = $student;
     }
     $this->set_field($student1->id, 'poison dart');
     $this->set_field($student2->id, 'poison dart');
     $info = new \core_availability\mock_info($course);
     $checker = new \core_availability\capability_checker($info->get_context());
     // Test standard field condition (positive and negative).
     $cond = new condition((object) array('sf' => 'institution', 'op' => 'contains', 'v' => 'Unseen'));
     $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
     ksort($result);
     $this->assertEquals(array($student1->id, $student3->id), $result);
     $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
     ksort($result);
     $this->assertEquals(array($student2->id), $result);
     // Test custom field condition.
     $cond = new condition((object) array('cf' => 'frogtype', 'op' => 'contains', 'v' => 'poison'));
     $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
     ksort($result);
     $this->assertEquals(array($student1->id, $student2->id), $result);
     $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
     ksort($result);
     $this->assertEquals(array($student3->id), $result);
 }
Example #3
0
 /**
  * Tests the filter_users function.
  */
 public function test_filter_users()
 {
     $info = new \core_availability\mock_info();
     $checker = new capability_checker($info->get_context());
     // Don't need to create real users in database, just use these ids.
     $users = array(1 => null, 2 => null, 3 => null);
     // Test basic tree with one condition that doesn't filter.
     $structure = tree::get_root_json(array(self::mock(array())));
     $tree = new tree($structure);
     $result = $tree->filter_user_list($users, false, $info, $checker);
     ksort($result);
     $this->assertEquals(array(1, 2, 3), array_keys($result));
     // Now a tree with one condition that filters.
     $structure = tree::get_root_json(array(self::mock(array('filter' => array(2, 3)))));
     $tree = new tree($structure);
     $result = $tree->filter_user_list($users, false, $info, $checker);
     ksort($result);
     $this->assertEquals(array(2, 3), array_keys($result));
     // Tree with two conditions that both filter (|).
     $structure = tree::get_root_json(array(self::mock(array('filter' => array(3))), self::mock(array('filter' => array(1)))), tree::OP_OR);
     $tree = new tree($structure);
     $result = $tree->filter_user_list($users, false, $info, $checker);
     ksort($result);
     $this->assertEquals(array(1, 3), array_keys($result));
     // Tree with two condition that both filter (&).
     $structure = tree::get_root_json(array(self::mock(array('filter' => array(2, 3))), self::mock(array('filter' => array(1, 2)))));
     $tree = new tree($structure);
     $result = $tree->filter_user_list($users, false, $info, $checker);
     ksort($result);
     $this->assertEquals(array(2), array_keys($result));
     // Tree with child tree with NOT condition.
     $structure = tree::get_root_json(array(tree::get_nested_json(array(self::mock(array('filter' => array(1)))), tree::OP_NOT_AND)));
     $tree = new tree($structure);
     $result = $tree->filter_user_list($users, false, $info, $checker);
     ksort($result);
     $this->assertEquals(array(2, 3), array_keys($result));
 }
 /**
  * Tests the filter_users (bulk checking) function.
  */
 public function test_filter_users()
 {
     global $DB, $CFG;
     $this->resetAfterTest();
     $CFG->enableavailability = true;
     // Erase static cache before test.
     condition::wipe_static_cache();
     // Make a test course and some users.
     $generator = $this->getDataGenerator();
     $course = $generator->create_course();
     $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
     $teacher = $generator->create_user();
     $generator->enrol_user($teacher->id, $course->id, $roleids['teacher']);
     $allusers = array($teacher->id => $teacher);
     $students = array();
     for ($i = 0; $i < 3; $i++) {
         $student = $generator->create_user();
         $students[$i] = $student;
         $generator->enrol_user($student->id, $course->id, $roleids['student']);
         $allusers[$student->id] = $student;
     }
     $info = new \core_availability\mock_info($course);
     $checker = new \core_availability\capability_checker($info->get_context());
     // Make test groups.
     $group1 = $generator->create_group(array('courseid' => $course->id));
     $group2 = $generator->create_group(array('courseid' => $course->id));
     $grouping1 = $generator->create_grouping(array('courseid' => $course->id));
     $grouping2 = $generator->create_grouping(array('courseid' => $course->id));
     groups_assign_grouping($grouping1->id, $group1->id);
     groups_assign_grouping($grouping2->id, $group2->id);
     // Make page in grouping 2.
     $pagegen = $generator->get_plugin_generator('mod_page');
     $page = $pagegen->create_instance(array('course' => $course->id, 'groupingid' => $grouping2->id, 'availability' => '{"op":"|","show":true,"c":[{"type":"grouping","activity":true}]}'));
     // Assign students to groups as follows (teacher is not in a group):
     // 0: no groups.
     // 1: in group 1/grouping 1.
     // 2: in group 2/grouping 2.
     groups_add_member($group1, $students[1]);
     groups_add_member($group2, $students[2]);
     // Test specific grouping.
     $cond = new condition((object) array('id' => (int) $grouping1->id));
     $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
     ksort($result);
     $this->assertEquals(array($teacher->id, $students[1]->id), $result);
     $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
     ksort($result);
     $this->assertEquals(array($teacher->id, $students[0]->id, $students[2]->id), $result);
     // Test course-module grouping.
     $modinfo = get_fast_modinfo($course);
     $cm = $modinfo->get_cm($page->cmid);
     $info = new \core_availability\info_module($cm);
     $result = array_keys($info->filter_user_list($allusers, $course));
     $this->assertEquals(array($teacher->id, $students[2]->id), $result);
 }