From 5ce443a1b16f218f05a3f8da5e990b92ffc35265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 25 May 2013 10:09:42 +0200 Subject: [PATCH] MDL-39676 improve get_role_users() unit tests --- lib/tests/accesslib_test.php | 47 +++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/tests/accesslib_test.php b/lib/tests/accesslib_test.php index f0e9ae18bed96..847724d754fea 100644 --- a/lib/tests/accesslib_test.php +++ b/lib/tests/accesslib_test.php @@ -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(); @@ -1238,9 +1237,9 @@ 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); @@ -1248,27 +1247,41 @@ public function test_get_role_users() { $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); } /**