mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
Merge branch 'MDL-38863_master' of git://github.com/dmonllao/moodle
This commit is contained in:
commit
0b92270b19
@ -79,6 +79,34 @@ Feature: Set up contextual data for tests
|
||||
And I follow "Course 1"
|
||||
Then I should see "Topic 1"
|
||||
|
||||
Scenario: Add role assigns
|
||||
Given the following "users" exists:
|
||||
| username | firstname | lastname | email |
|
||||
| user1 | User | 1 | user1@moodlemoodle.com |
|
||||
| user2 | User | 2 | user2@moodlemoodle.com |
|
||||
| user3 | User | 3 | user3@moodlemoodle.com |
|
||||
And the following "categories" exists:
|
||||
| name | category | idnumber |
|
||||
| Cat 1 | 0 | CAT1 |
|
||||
And the following "courses" exists:
|
||||
| fullname | shortname | category |
|
||||
| Course 1 | C1 | CAT1 |
|
||||
And the following "role assigns" exists:
|
||||
| user | role | contextlevel | reference |
|
||||
| user1 | manager | System | |
|
||||
| user2 | editingteacher | Category | CAT1 |
|
||||
| user3 | editingteacher | Course | C1 |
|
||||
When I log in as "user1"
|
||||
Then I should see "Front page settings"
|
||||
And I log out
|
||||
And I log in as "user2"
|
||||
And I follow "Course 1"
|
||||
And I should see "Turn editing on"
|
||||
And I log out
|
||||
And I log in as "user3"
|
||||
And I follow "Course 1"
|
||||
And I should see "Turn editing on"
|
||||
|
||||
@javascript
|
||||
Scenario: Add relations between users and groups
|
||||
Given the following "users" exists:
|
||||
|
@ -712,6 +712,34 @@ EOD;
|
||||
$plugin->enrol_user($instance, $userid, $roleid, $timestart, $timeend, $status);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns the specified role to a user in the context.
|
||||
*
|
||||
* @param int $roleid
|
||||
* @param int $userid
|
||||
* @param int $contextid Defaults to the system context
|
||||
* @return int new/existing id of the assignment
|
||||
*/
|
||||
public function role_assign($roleid, $userid, $contextid = false) {
|
||||
|
||||
// Default to the system context.
|
||||
if (!$contextid) {
|
||||
$context = context_system::instance();
|
||||
$contextid = $context->id;
|
||||
}
|
||||
|
||||
if (empty($roleid)) {
|
||||
throw new coding_exception('roleid must be present in testing_data_generator::role_assign() arguments');
|
||||
}
|
||||
|
||||
if (empty($userid)) {
|
||||
throw new coding_exception('userid must be present in testing_data_generator::role_assign() arguments');
|
||||
}
|
||||
|
||||
return role_assign($roleid, $userid, $contextid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,10 +94,15 @@ class behat_data_generators extends behat_base {
|
||||
|
||||
),
|
||||
'system role assigns' => array(
|
||||
'datagenerator' => 'role_assign',
|
||||
'datagenerator' => 'system_role_assign',
|
||||
'required' => array('user', 'role'),
|
||||
'switchids' => array('user' => 'userid', 'role' => 'roleid')
|
||||
),
|
||||
'role assigns' => array(
|
||||
'datagenerator' => 'role_assign',
|
||||
'required' => array('user', 'role', 'contextlevel', 'reference'),
|
||||
'switchids' => array('user' => 'userid', 'role' => 'roleid')
|
||||
),
|
||||
'group members' => array(
|
||||
'datagenerator' => 'group_member',
|
||||
'required' => array('user', 'group'),
|
||||
@ -238,12 +243,17 @@ class behat_data_generators extends behat_base {
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a role to a user at system level.
|
||||
* Assigns a role to a user at system context
|
||||
*
|
||||
* Used by "system role assigns" can be deleted when
|
||||
* system role assign will be deprecated in favour of
|
||||
* "role assigns"
|
||||
*
|
||||
* @throws Exception
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
protected function process_role_assign($data) {
|
||||
protected function process_system_role_assign($data) {
|
||||
|
||||
if (empty($data['roleid'])) {
|
||||
throw new Exception('\'system role assigns\' requires the field \'role\' to be specified');
|
||||
@ -254,7 +264,39 @@ class behat_data_generators extends behat_base {
|
||||
}
|
||||
|
||||
$context = context_system::instance();
|
||||
role_assign($data['roleid'], $data['userid'], $context->id);
|
||||
|
||||
$this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a role to a user at the specified context
|
||||
*
|
||||
* @throws Exception
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
protected function process_role_assign($data) {
|
||||
|
||||
if (empty($data['roleid'])) {
|
||||
throw new Exception('\'role assigns\' requires the field \'role\' to be specified');
|
||||
}
|
||||
|
||||
if (!isset($data['userid'])) {
|
||||
throw new Exception('\'role assigns\' requires the field \'user\' to be specified');
|
||||
}
|
||||
|
||||
if (empty($data['contextlevel'])) {
|
||||
throw new Exception('\'role assigns\' requires the field \'contextlevel\' to be specified');
|
||||
}
|
||||
|
||||
if (!isset($data['reference'])) {
|
||||
throw new Exception('\'role assigns\' requires the field \'reference\' to be specified');
|
||||
}
|
||||
|
||||
// Getting the context id.
|
||||
$context = $this->get_context($data['contextlevel'], $data['reference']);
|
||||
|
||||
$this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -353,4 +395,67 @@ class behat_data_generators extends behat_base {
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the internal context id from the context reference.
|
||||
*
|
||||
* The context reference changes depending on the context
|
||||
* level, it can be the system, a user, a category, a course or
|
||||
* a module.
|
||||
*
|
||||
* @throws Exception
|
||||
* @param string $levelname The context level string introduced by the test writer
|
||||
* @param string $contextref The context reference introduced by the test writer
|
||||
* @return context
|
||||
*/
|
||||
protected function get_context($levelname, $contextref) {
|
||||
global $DB;
|
||||
|
||||
// Getting context levels and names (we will be using the English ones as it is the test site language).
|
||||
$contextlevels = context_helper::get_all_levels();
|
||||
$contextnames = array();
|
||||
foreach ($contextlevels as $level => $classname) {
|
||||
$contextnames[context_helper::get_level_name($level)] = $level;
|
||||
}
|
||||
|
||||
if (empty($contextnames[$levelname])) {
|
||||
throw new Exception('The specified "' . $levelname . '" context level does not exist');
|
||||
}
|
||||
$contextlevel = $contextnames[$levelname];
|
||||
|
||||
// Return it, we don't need to look for other internal ids.
|
||||
if ($contextlevel == CONTEXT_SYSTEM) {
|
||||
return context_system::instance();
|
||||
}
|
||||
|
||||
switch ($contextlevel) {
|
||||
|
||||
case CONTEXT_USER:
|
||||
$instanceid = $DB->get_field('user', 'id', array('username' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_COURSECAT:
|
||||
$instanceid = $DB->get_field('course_categories', 'id', array('idnumber' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_COURSE:
|
||||
$instanceid = $DB->get_field('course', 'id', array('shortname' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_MODULE:
|
||||
$instanceid = $DB->get_field('course_modules', 'id', array('idnumber' => $contextref));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
$contextclass = $contextlevels[$contextlevel];
|
||||
if (!$context = $contextclass::instance($instanceid, IGNORE_MISSING)) {
|
||||
throw new Exception('The specified "' . $contextref . '" context reference does not exist');
|
||||
}
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user