/** * Test assign_roles */ public function test_assign_roles() { global $USER; $this->resetAfterTest(true); $course = self::getDataGenerator()->create_course(); // Set the required capabilities by the external function. $context = context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/role:assign', $context->id); $this->assignUserCapability('moodle/course:view', $context->id, $roleid); // Add manager role to $USER. // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'. role_assign(1, $USER->id, context_system::instance()->id); // Check the teacher role has not been assigned to $USER. $users = get_role_users(3, $context); $this->assertEquals(count($users), 0); // 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->expectException('moodle_exception'); $categories = core_role_external::assign_roles(array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)); }
/** * Manual role assignments to users * * @param array $assignments An array of manual role assignment * @since Moodle 2.0 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. * @todo MDL-31194 This will be deleted in Moodle 2.5. * @see core_role_external::assign_roles() */ public static function role_assign($assignments) { return core_role_external::assign_roles($assignments); }