Skip to content

Commit

Permalink
MDL-39676 improve get_role_users() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed May 25, 2013
1 parent 73f560c commit 5ce443a
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/tests/accesslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,8 +1222,7 @@ public function test_get_default_enrol_roles() {
* @return void
*/
public function test_get_role_users() {
global $DB, $CFG;
require_once("$CFG->dirroot/group/lib.php");
global $DB;

$this->resetAfterTest();

Expand All @@ -1238,37 +1237,51 @@ public function test_get_role_users() {
$otherrename = (object)array('roleid'=>$otherid, 'name'=>'Ostatní', 'contextid'=>$coursecontext->id);
$DB->insert_record('role_names', $otherrename);

$user1 = $this->getDataGenerator()->create_user();
$user1 = $this->getDataGenerator()->create_user(array('firstname'=>'John', 'lastname'=>'Smith'));
role_assign($teacherrole->id, $user1->id, $coursecontext->id);
$user2 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user(array('firstname'=>'Jan', 'lastname'=>'Kovar'));
role_assign($teacherrole->id, $user2->id, $systemcontext->id);
$user3 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user3->id, $course->id, $teacherrole->id);
$user4 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user4->id, $course->id, $studentrole->id);

$group = $this->getDataGenerator()->create_group(array('courseid'=>$course->id));
groups_add_member($group->id, $user3->id);
groups_add_member($group, $user3);

$users = get_role_users($teacherrole->id, $coursecontext);
$this->assertEquals(array($user1->id, $user3->id), array_keys($users), '', 0, 10, true);
$user = $users[$user1->id];
$this->assertEquals($teacherrole->id, $user->roleid);
$this->assertEquals($teacherrole->name, $user->rolename);
$this->assertEquals($teacherrole->shortname, $user->roleshortname);
$this->assertEquals($teacherrename->name, $user->rolecoursealias);
$this->assertCount(2, $users);
$this->assertArrayHasKey($user1->id, $users);
$this->assertEquals($users[$user1->id]->id, $user1->id);
$this->assertEquals($users[$user1->id]->roleid, $teacherrole->id);
$this->assertEquals($users[$user1->id]->rolename, $teacherrole->name);
$this->assertEquals($users[$user1->id]->roleshortname, $teacherrole->shortname);
$this->assertEquals($users[$user1->id]->rolecoursealias, $teacherrename->name);
$this->assertArrayHasKey($user3->id, $users);
$this->assertEquals($users[$user3->id]->id, $user3->id);
$this->assertEquals($users[$user3->id]->roleid, $teacherrole->id);
$this->assertEquals($users[$user3->id]->rolename, $teacherrole->name);
$this->assertEquals($users[$user3->id]->roleshortname, $teacherrole->shortname);
$this->assertEquals($users[$user3->id]->rolecoursealias, $teacherrename->name);

$users = get_role_users($teacherrole->id, $coursecontext, true);
$this->assertEquals(array($user1->id, $user2->id, $user3->id), array_keys($users), '', 0, 10, true);
$this->assertCount(3, $users);

$users = get_role_users($teacherrole->id, $coursecontext, false, '', null, false);
$this->assertEquals(array($user3->id), array_keys($users), '', 0, 10, true);
$users = get_role_users($teacherrole->id, $coursecontext, true, '', null, null, '', 2, 1);
$this->assertCount(1, $users);

$users = get_role_users($teacherrole->id, $coursecontext, false, '', null, null);
$users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id, u.email, u.idnumber', 'u.idnumber');
$this->assertCount(2, $users);
$this->assertArrayHasKey($user1->id, $users);
$this->assertArrayHasKey($user3->id, $users);

$users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id, u.email, u.idnumber', 'u.idnumber', null, $group->id);
$this->assertCount(1, $users);
$this->assertArrayHasKey($user3->id, $users);

$users = get_role_users($teacherrole->id, $coursecontext, false, 'u.id, u.email, u.idnumber', 'u.idnumber', true, $group->id, 0, 10, 'u.deleted = 0');
$this->assertEquals(array($user3->id), array_keys($users), '', 0, 10, true);
$users = get_role_users($teacherrole->id, $coursecontext, true, 'u.id, u.email, u.idnumber, u.firstname', 'u.idnumber', null, '', '', '', 'u.firstname = :xfirstname', array('xfirstname'=>'John'));
$this->assertCount(1, $users);
$this->assertArrayHasKey($user1->id, $users);
}

/**
Expand Down

0 comments on commit 5ce443a

Please sign in to comment.