Merge branch 'MDL-44108_master' of git://github.com/dmonllao/moodle

This commit is contained in:
Damyon Wiese 2014-02-24 15:42:32 +08:00
commit dea614e493
3 changed files with 131 additions and 3 deletions

View File

@ -111,22 +111,32 @@ Feature: Set up contextual data for tests
Then I should see "Topic 1"
Scenario: Add role assigns
Given the following "users" exists:
Given the following "roles" exists:
| name | shortname | description | archetype |
| Custom editing teacher | custom1 | My custom role 1 | editingteacher |
| Custom student | custom2 | | |
And 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 |
| user4 | User | 4 | user4@moodlemoodle.com |
| user5 | User | 5 | user5@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 "course enrolments" exists:
| user | course | role |
| user4 | C1 | custom1 |
And the following "role assigns" exists:
| user | role | contextlevel | reference |
| user1 | manager | System | |
| user2 | editingteacher | Category | CAT1 |
| user3 | editingteacher | Course | C1 |
| user5 | custom2 | System | |
When I log in as "user1"
Then I should see "Front page settings"
And I log out
@ -137,6 +147,16 @@ Feature: Set up contextual data for tests
And I log in as "user3"
And I follow "Course 1"
And I should see "Turn editing on"
And I log out
And I log in as "user4"
And I follow "Course 1"
And I should see "Turn editing on"
And I log out
And I log in as "user5"
And I should see "You are logged in as"
And I follow "Course 1"
And I should see "You can not enrol yourself in this course."
Scenario: Add modules
Given the following "courses" exists:

View File

@ -41,6 +41,7 @@ class testing_data_generator {
protected $scalecount = 0;
protected $groupcount = 0;
protected $groupingcount = 0;
protected $rolecount = 0;
/** @var array list of plugin generators */
protected $generators = array();
@ -656,6 +657,93 @@ EOD;
return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST);
}
/**
* Creates a new role in the system.
*
* You can fill $record with the role 'name',
* 'shortname', 'description' and 'archetype'.
*
* If an archetype is specified it's capabilities,
* context where the role can be assigned and
* all other properties are copied from the archetype;
* if no archetype is specified it will create an
* empty role.
*
* @param array|stdClass $record
* @return int The new role id
*/
public function create_role($record=null) {
global $DB;
$this->rolecount++;
$i = $this->rolecount;
$record = (array)$record;
if (empty($record['shortname'])) {
$record['shortname'] = 'role-' . $i;
}
if (empty($record['name'])) {
$record['name'] = 'Test role ' . $i;
}
if (empty($record['description'])) {
$record['description'] = 'Test role ' . $i . ' description';
}
if (empty($record['archetype'])) {
$record['archetype'] = '';
} else {
$archetypes = get_role_archetypes();
if (empty($archetypes[$record['archetype']])) {
throw new coding_exception('\'role\' requires the field \'archetype\' to specify a ' .
'valid archetype shortname (editingteacher, student...)');
}
}
// Creates the role.
if (!$newroleid = create_role($record['name'], $record['shortname'], $record['description'], $record['archetype'])) {
throw new coding_exception('There was an error creating \'' . $record['shortname'] . '\' role');
}
// If no archetype was specified we allow it to be added to all contexts,
// otherwise we allow it in the archetype contexts.
if (!$record['archetype']) {
$contextlevels = array_keys(context_helper::get_all_levels());
} else {
// Copying from the archetype default rol.
$archetyperoleid = $DB->get_field(
'role',
'id',
array('shortname' => $record['archetype'], 'archetype' => $record['archetype'])
);
$contextlevels = get_role_contextlevels($archetyperoleid);
}
set_role_contextlevels($newroleid, $contextlevels);
if ($record['archetype']) {
// We copy all the roles the archetype can assign, override and switch to.
if ($record['archetype']) {
$types = array('assign', 'override', 'switch');
foreach ($types as $type) {
$rolestocopy = get_default_role_archetype_allows($type, $record['archetype']);
foreach ($rolestocopy as $tocopy) {
$functionname = 'allow_' . $type;
$functionname($newroleid, $tocopy);
}
}
}
// Copying the archetype capabilities.
$sourcerole = $DB->get_record('role', array('id' => $archetyperoleid));
role_cap_duplicate($sourcerole, $newroleid);
}
return $newroleid;
}
/**
* Helper method which combines $defaults with the values specified in $record.
* If $record is an object, it is converted to an array.

View File

@ -130,6 +130,10 @@ class behat_data_generators extends behat_base {
'cohorts' => array(
'datagenerator' => 'cohort',
'required' => array('idnumber')
),
'roles' => array(
'datagenerator' => 'role',
'required' => array('shortname')
)
);
@ -377,6 +381,22 @@ class behat_data_generators extends behat_base {
$this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
}
/**
* Creates a role.
*
* @param array $data
* @return void
*/
protected function process_role($data) {
// We require the user to fill the role shortname.
if (empty($data['shortname'])) {
throw new Exception('\'role\' requires the field \'shortname\' to be specified');
}
$this->datagenerator->create_role($data);
}
/**
* Gets the user id from it's username.
* @throws Exception
@ -402,7 +422,7 @@ class behat_data_generators extends behat_base {
global $DB;
if (!$id = $DB->get_field('role', 'id', array('shortname' => $roleshortname))) {
throw new Exception('The specified role with shortname"' . $roleshortname . '" does not exist');
throw new Exception('The specified role with shortname "' . $roleshortname . '" does not exist');
}
return $id;
@ -439,7 +459,7 @@ class behat_data_generators extends behat_base {
global $DB;
if (!$id = $DB->get_field('course', 'id', array('shortname' => $shortname))) {
throw new Exception('The specified course with shortname"' . $shortname . '" does not exist');
throw new Exception('The specified course with shortname "' . $shortname . '" does not exist');
}
return $id;
}