Skip to content

Commit

Permalink
MDL-38863 behat: Role assign generator
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Jul 23, 2013
1 parent ffc3f53 commit 72ddc05
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 4 deletions.
28 changes: 28 additions & 0 deletions admin/tool/behat/tests/behat/data_generators.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions lib/testing/generator/data_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,34 @@ public function enrol_user($userid, $courseid, $roleid = null, $enrol = 'manual'
$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);
}

}

/**
Expand Down
113 changes: 109 additions & 4 deletions lib/tests/behat/behat_data_generators.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -238,12 +243,17 @@ protected function process_enrol_user($data) {
}

/**
* 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');
Expand All @@ -254,7 +264,39 @@ protected function process_role_assign($data) {
}

$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);
}

/**
Expand Down Expand Up @@ -353,4 +395,67 @@ protected function get_grouping_id($idnumber) {
}
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;
}

}

0 comments on commit 72ddc05

Please sign in to comment.