Skip to content

Commit

Permalink
MDL-59395 core_user: added unit tests for new participation functions
Browse files Browse the repository at this point in the history
Part of MDL-59290.
  • Loading branch information
mdjnelson committed Jul 5, 2017
1 parent bc47b70 commit ba6645d
Showing 1 changed file with 179 additions and 0 deletions.
179 changes: 179 additions & 0 deletions user/tests/userlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,4 +694,183 @@ public function test_user_get_user_details_missing_fields() {
self::assertSame('50', $got['timezone']);
self::assertSame('0', $got['mailformat']);
}

/**
* Test returning the total number of participants.
*/
public function test_user_get_total_participants() {
global $DB;

$this->resetAfterTest();

// Create a course.
$course = self::getDataGenerator()->create_course();

// Create a teacher.
$teacher = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);

// Create a bunch of students.
$student1 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);
$student2 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);
$student3 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);

// Create a group.
$group = self::getDataGenerator()->create_group(array('courseid' => $course->id));

// Enrol the students.
self::getDataGenerator()->enrol_user($student1->id, $course->id);
self::getDataGenerator()->enrol_user($student2->id, $course->id);
self::getDataGenerator()->enrol_user($student3->id, $course->id);

// Enrol the teacher.
$roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
self::getDataGenerator()->enrol_user($teacher->id, $course->id, $roleids['editingteacher']);

// Add the teacher and two of the students to the group.
groups_add_member($group->id, $teacher->id);
groups_add_member($group->id, $student1->id);
groups_add_member($group->id, $student2->id);

// Set it so the teacher and two of the students have accessed the courses within the last day,
// but only one of the students is in the group.
$accesssince = time() - DAYSECS;
$lastaccess = new stdClass();
$lastaccess->userid = $teacher->id;
$lastaccess->courseid = $course->id;
$lastaccess->timeaccess = time() - DAYSECS;
$DB->insert_record('user_lastaccess', $lastaccess);

$lastaccess->userid = $student1->id;
$DB->insert_record('user_lastaccess', $lastaccess);

$lastaccess->userid = $student3->id;
$DB->insert_record('user_lastaccess', $lastaccess);

// Now, when we perform the following search we should only return 1 user. A student who belongs to
// the group and has the name 'searchforthis' and has also accessed the course in the last day.
$count = user_get_total_participants($course->id, $group->id, $accesssince + 1, $roleids['student'], 'searchforthis');

$this->assertEquals(1, $count);
}

/**
* Test returning the number of participants on the front page.
*/
public function test_user_get_total_participants_on_front_page() {
$this->resetAfterTest();

// Set it so that only 3 users have accessed the site within the last day.
$accesssince = time() - DAYSECS;

// Create a bunch of users.
$user1 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis', 'lastaccess' => $accesssince]);
$user2 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis', 'lastaccess' => $accesssince]);
$user3 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);
$user4 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis', 'lastaccess' => $accesssince]);

// Create a group.
$group = self::getDataGenerator()->create_group(array('courseid' => SITEID));

// Add 3 of the users to a group.
groups_add_member($group->id, $user1->id);
groups_add_member($group->id, $user2->id);
groups_add_member($group->id, $user3->id);

// Now, when we perform the following search we should only return 2 users. Users who belong to
// the group and have the name 'searchforthis' and have also accessed the site in the last day.
$count = user_get_total_participants(SITEID, $group->id, $accesssince + 1, 0, 'searchforthis');

$this->assertEquals(2, $count);
}

/**
* Test returning the participants.
*/
public function test_user_get_participants() {
global $DB;

$this->resetAfterTest();

// Create a course.
$course = self::getDataGenerator()->create_course();

// Create a teacher.
$teacher = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);

// Create a bunch of students.
$student1 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);
$student2 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);
$student3 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);

// Create a group.
$group = self::getDataGenerator()->create_group(array('courseid' => $course->id));

// Enrol the students.
self::getDataGenerator()->enrol_user($student1->id, $course->id);
self::getDataGenerator()->enrol_user($student2->id, $course->id);
self::getDataGenerator()->enrol_user($student3->id, $course->id);

// Enrol the teacher.
$roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
self::getDataGenerator()->enrol_user($teacher->id, $course->id, $roleids['editingteacher']);

// Add the teacher and two of the students to the group.
groups_add_member($group->id, $teacher->id);
groups_add_member($group->id, $student1->id);
groups_add_member($group->id, $student2->id);

// Set it so the teacher and two of the students have accessed the course within the last day, but only one of
// the students is in the group.
$accesssince = time() - DAYSECS;
$lastaccess = new stdClass();
$lastaccess->userid = $teacher->id;
$lastaccess->courseid = $course->id;
$lastaccess->timeaccess = time() - DAYSECS;
$DB->insert_record('user_lastaccess', $lastaccess);

$lastaccess->userid = $student1->id;
$DB->insert_record('user_lastaccess', $lastaccess);

$lastaccess->userid = $student3->id;
$DB->insert_record('user_lastaccess', $lastaccess);

// Now, when we perform the following search we should only return 1 user. A student who belongs to
// the group and has the name 'searchforthis' and has also accessed the course in the last day.
$userset = user_get_participants($course->id, $group->id, $accesssince + 1, $roleids['student'], 'searchforthis');

$this->assertEquals(1, sizeof($userset));
$this->assertEquals($student1->id, $userset->current()->id);
}

/**
* Test returning the participants on the front page.
*/
public function test_user_get_participants_on_front_page() {
$this->resetAfterTest();

// Set it so that only 3 users have accessed the site within the last day.
$accesssince = time() - DAYSECS;

// Create a bunch of users.
$user1 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis', 'lastaccess' => $accesssince]);
$user2 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis', 'lastaccess' => $accesssince]);
$user3 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis']);
$user4 = self::getDataGenerator()->create_user(['firstname' => 'searchforthis', 'lastaccess' => $accesssince]);

// Create a group.
$group = self::getDataGenerator()->create_group(array('courseid' => SITEID));

// Add 3 of the users to a group.
groups_add_member($group->id, $user1->id);
groups_add_member($group->id, $user2->id);
groups_add_member($group->id, $user3->id);

// Now, when we perform the following search we should only return 2 users. Users who belong to
// the group and have the name 'searchforthis' and have also accessed the site in the last day.
$userset = user_get_participants(SITEID, $group->id, $accesssince + 1, 0, 'searchforthis');

$this->assertEquals($user1->id, $userset->current()->id);
$userset->next();
$this->assertEquals($user2->id, $userset->current()->id);
}
}

0 comments on commit ba6645d

Please sign in to comment.