Merge branch 'MDL-42324_25' of git://github.com/dmonllao/moodle into MOODLE_25_STABLE

This commit is contained in:
Dan Poltawski
2013-10-23 17:14:37 +08:00
3 changed files with 205 additions and 4 deletions
@@ -79,6 +79,49 @@ 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"
Scenario: Add modules
Given the following "courses" exists:
| fullname | shortname |
| Course 1 | C1 |
And the following "activities" exists:
| activity | name | intro | course | idnumber |
| assign | Test assignment name | Test assignment description | C1 | assign1 |
| data | Test database name | Test database description | C1 | data1 |
When I log in as "admin"
And I follow "Course 1"
Then I should see "Test assignment name"
And I should see "Test database name"
And I follow "Test assignment name"
And I should see "Test assignment description"
@javascript
Scenario: Add relations between users and groups
Given the following "users" exists:
+28
View File
@@ -690,6 +690,34 @@ EOD;
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);
}
}
/**
+134 -4
View File
@@ -94,10 +94,20 @@ 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')
),
'activities' => array(
'datagenerator' => 'activity',
'required' => array('activity', 'idnumber', 'course'),
'switchids' => array('course' => 'course')
),
'group members' => array(
'datagenerator' => 'group_member',
'required' => array('user', 'group'),
@@ -198,6 +208,26 @@ class behat_data_generators extends behat_base {
return $data;
}
/**
* Adapter to modules generator
* @throws Exception Custom exception for test writers
* @param array $data
* @return void
*/
protected function process_activity($data) {
// The the_following_exists() method checks that the field exists.
$activityname = $data['activity'];
unset($data['activity']);
// Custom exception.
try {
$this->datagenerator->create_module($activityname, $data);
} catch (coding_exception $e) {
throw new Exception('\'' . $activityname . '\' activity can not be added using this step,' .
' use the step \'I add a "ACTIVITY_OR_RESOURCE_NAME_STRING" to section "SECTION_NUMBER"\' instead');
}
}
/**
* Adapter to enrol_user() data generator.
@@ -238,12 +268,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 +289,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 +420,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;
}
}