Skip to content
Permalink
Browse files

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
  • Loading branch information...
ankitagarwal committed Jun 14, 2013
1 parent 3a68f79 commit 95a3bb4dc87dd45948f4517dc40de466e4f20e21
Showing with 47 additions and 1 deletion.
  1. +47 −1 enrol/tests/externallib_test.php
@@ -199,21 +199,67 @@ public function test_assign_roles() {
$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)));
// Check the role has been assigned.
$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');
$categories = core_role_external::assign_roles(
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
*/

0 comments on commit 95a3bb4

Please sign in to comment.
You can’t perform that action at this time.