Пример #1
0
 /**
  * Tests getting user list SQL. This is a different test from the above because
  * there is some additional code in this function so more variants need testing.
  */
 public function test_get_user_list_sql()
 {
     global $DB, $CFG;
     $this->resetAfterTest();
     $CFG->enableavailability = true;
     // Erase static cache before test.
     condition::wipe_static_cache();
     // For testing, make another info field with default value.
     $DB->insert_record('user_info_field', array('shortname' => 'tonguestyle', 'name' => 'Tongue style', 'categoryid' => 1, 'datatype' => 'text', 'defaultdata' => 'Slimy'));
     $otherprofilefield = $DB->get_record('user_info_field', array('shortname' => 'tonguestyle'));
     // 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'));
     $student4 = $generator->create_user(array('institution' => '0'));
     $allusers = array();
     foreach (array($student1, $student2, $student3, $student4) 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');
     $this->set_field($student3->id, 'Rough', $otherprofilefield->id);
     $this->info = new \core_availability\mock_info($course);
     // Test standard field condition (positive).
     $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_CONTAINS, 'v' => 'Univ'));
     $this->assert_user_list_sql_results(array($student1->id, $student3->id));
     // Now try it negative.
     $this->assert_user_list_sql_results(array($student2->id, $student4->id), true);
     // Try all the other condition types.
     $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_DOES_NOT_CONTAIN, 'v' => 's'));
     $this->assert_user_list_sql_results(array($student4->id));
     $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_IS_EQUAL_TO, 'v' => 'Hogwarts'));
     $this->assert_user_list_sql_results(array($student2->id));
     $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_STARTS_WITH, 'v' => 'U'));
     $this->assert_user_list_sql_results(array($student1->id, $student3->id));
     $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_ENDS_WITH, 'v' => 'rts'));
     $this->assert_user_list_sql_results(array($student2->id));
     $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_IS_EMPTY));
     $this->assert_user_list_sql_results(array($student4->id));
     $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_IS_NOT_EMPTY));
     $this->assert_user_list_sql_results(array($student1->id, $student2->id, $student3->id));
     // Try with a custom field condition that doesn't have a default.
     $this->cond = new condition((object) array('cf' => 'frogtype', 'op' => condition::OP_CONTAINS, 'v' => 'poison'));
     $this->assert_user_list_sql_results(array($student1->id, $student2->id));
     $this->cond = new condition((object) array('cf' => 'frogtype', 'op' => condition::OP_IS_EMPTY));
     $this->assert_user_list_sql_results(array($student3->id, $student4->id));
     // Try with one that does have a default.
     $this->cond = new condition((object) array('cf' => 'tonguestyle', 'op' => condition::OP_STARTS_WITH, 'v' => 'Sli'));
     $this->assert_user_list_sql_results(array($student1->id, $student2->id, $student4->id));
     $this->cond = new condition((object) array('cf' => 'tonguestyle', 'op' => condition::OP_IS_EMPTY));
     $this->assert_user_list_sql_results(array());
 }
Пример #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);
 }