MDL-39152 phpunit: improve tests for assign_roles()
core_role_external::assign_roles() now support alternative paramaters instead of contextid. Test working of those. Also test the parameters validation a bit, since these are no longer automatically tested being optional now
This commit is contained in:
@@ -199,7 +199,7 @@ class core_role_external_testcase extends externallib_advanced_testcase {
|
||||
$users = get_role_users(3, $context);
|
||||
$this->assertEquals(count($users), 0);
|
||||
|
||||
// Call the external function. Assign teacher role to $USER.
|
||||
// Call the external function. Assign teacher role to $USER with contextid.
|
||||
core_role_external::assign_roles(array(
|
||||
array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
|
||||
|
||||
@@ -207,6 +207,17 @@ class core_role_external_testcase extends externallib_advanced_testcase {
|
||||
$users = get_role_users(3, $context);
|
||||
$this->assertEquals(count($users), 1);
|
||||
|
||||
// Unassign role.
|
||||
role_unassign(3, $USER->id, $context->id);
|
||||
$users = get_role_users(3, $context);
|
||||
$this->assertEquals(count($users), 0);
|
||||
|
||||
// Call the external function. Assign teacher role to $USER.
|
||||
core_role_external::assign_roles(array(
|
||||
array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id)));
|
||||
$users = get_role_users(3, $context);
|
||||
$this->assertEquals(count($users), 1);
|
||||
|
||||
// Call without required capability.
|
||||
$this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
|
||||
$this->setExpectedException('moodle_exception');
|
||||
@@ -214,6 +225,41 @@ class core_role_external_testcase extends externallib_advanced_testcase {
|
||||
array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test assign_roles() parameter validation
|
||||
*/
|
||||
public function test_assign_roles_params() {
|
||||
global $USER;
|
||||
|
||||
// Call without correct context details.
|
||||
$this->setExpectedException('invalid_parameter_exception');
|
||||
core_role_external::assign_roles(array('roleid' => 3, 'userid' => $USER->id));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test assign_roles() parameter validation
|
||||
*/
|
||||
public function test_assign_roles_params2() {
|
||||
global $USER;
|
||||
|
||||
// Call without correct context details.
|
||||
$this->setExpectedException('invalid_parameter_exception');
|
||||
core_role_external::assign_roles(array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test assign_roles() parameter validation
|
||||
*/
|
||||
public function test_assign_roles_params3() {
|
||||
global $USER;
|
||||
|
||||
// Call without correct context details.
|
||||
$this->resetAfterTest(true);
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$this->setExpectedException('invalid_parameter_exception');
|
||||
core_role_external::assign_roles(array('roleid' => 3, 'userid' => $USER->id, 'instanceid' => $course->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test unassign_roles
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user