forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webservice MDL-21658 add moodle_role_assign() unit test function
- Loading branch information
Showing
1 changed file
with
59 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,8 @@ function setUp() { | |
'moodle_user_create_users' => false, | ||
'moodle_course_create_courses' => false, | ||
'moodle_user_delete_users' => false, | ||
'moodle_user_update_users' => false | ||
'moodle_user_update_users' => false, | ||
'moodle_role_assign' => false | ||
); | ||
|
||
//performance testing: number of time the web service are run | ||
|
@@ -797,13 +798,12 @@ function moodle_user_update_users($client) { | |
$user2->email = '[email protected]'; | ||
$users = array($user1, $user2); | ||
|
||
//update the user | ||
//delete the users by webservice | ||
//update the users by web service | ||
$function = 'moodle_user_update_users'; | ||
$params = array('users' => $users); | ||
$client->call($function, $params); | ||
|
||
//compare user | ||
//compare DB user with the test data | ||
$dbuser1 = $DB->get_record('user', array('username' => $user1->username)); | ||
$this->assertEqual($dbuser1->firstname, $user1->firstname); | ||
$this->assertEqual($dbuser1->password, | ||
|
@@ -836,7 +836,6 @@ function moodle_user_update_users($client) { | |
$this->assertEqual($customfields[$customfieldname2], | ||
$user1->customfields[1]['value']); | ||
|
||
//retrieve user2 from the DB and check values | ||
$dbuser2 = $DB->get_record('user', array('username' => $user2->username)); | ||
$this->assertEqual($dbuser2->firstname, $user2->firstname); | ||
$this->assertEqual($dbuser2->password, | ||
|
@@ -861,4 +860,59 @@ function moodle_user_update_users($client) { | |
} | ||
} | ||
|
||
function moodle_role_assign($client) { | ||
global $DB, $CFG; | ||
|
||
$searchusers = $DB->get_records_list('user', 'username', | ||
array('veryimprobabletestusername2')); | ||
$searchroles = $DB->get_records_list('role', 'shortname', | ||
array('role1thatshouldnotexist', 'role2thatshouldnotexist')); | ||
|
||
if (empty($searchusers) and empty($searchroles)) { | ||
|
||
//create a temp user | ||
$user = new stdClass(); | ||
$user->username = 'veryimprobabletestusername2'; | ||
$user->password = 'testpassword2'; | ||
$user->firstname = 'testfirstname2'; | ||
$user->lastname = 'testlastname2'; | ||
$user->email = '[email protected]'; | ||
require_once($CFG->dirroot."/user/lib.php"); | ||
$user->id = user_create_user($user); | ||
|
||
//create two roles | ||
$role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', ''); | ||
$role2->id = create_role('role2thatshouldnotexist', 'role2thatshouldnotexist', ''); | ||
|
||
//assign user to role by webservice | ||
$context = get_system_context(); | ||
$assignments = array( | ||
array('roleid' => $role1->id, 'userid' => $user->id, 'contextid' => $context->id), | ||
array('roleid' => $role2->id, 'userid' => $user->id, 'contextid' => $context->id) | ||
); | ||
|
||
$function = 'moodle_role_assign'; | ||
$params = array('assignments' => $assignments); | ||
$client->call($function, $params); | ||
|
||
//check that the assignment work | ||
$roles = get_user_roles($context, $user->id, false); | ||
foreach ($roles as $role) { | ||
$this->assertTrue(($role->roleid == $role1->id) or ($role->roleid == $role2->id) ); | ||
} | ||
|
||
//unassign roles from user | ||
role_unassign($role1->id, $user->id, $context->id, '', NULL); | ||
role_unassign($role2->id, $user->id, $context->id, '', NULL); | ||
|
||
//delete user from DB | ||
$DB->delete_records('user', array('id' => $user->id)); | ||
|
||
//delete the two role from DB | ||
delete_role($role1->id); | ||
delete_role($role2->id); | ||
} | ||
} | ||
|
||
|
||
} |