Skip to content

Commit

Permalink
Merge branch 'MDL-42326_25' of git://github.com/dmonllao/moodle into …
Browse files Browse the repository at this point in the history
…MOODLE_25_STABLE
  • Loading branch information
danpoltawski committed Nov 4, 2013
2 parents 824105f + fb8ea65 commit d06720b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
31 changes: 31 additions & 0 deletions admin/tool/behat/tests/behat/data_generators.feature
Expand Up @@ -65,6 +65,37 @@ Feature: Set up contextual data for tests
And I should see "Grouping 1"
And I should see "Grouping 2"

@javascript
Scenario: Role overrides
Given the following "users" exists:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@asd.com |
| student1 | Student | 1 | student1@asd.com |
And the following "categories" exists:
| name | category | idnumber |
| Cat 1 | 0 | CAT1 |
And the following "courses" exists:
| fullname | shortname |
| Course 1 | C1 |
And the following "course enrolments" exists:
| user | course | role |
| student1 | C1 | student |
| teacher1 | C1 | editingteacher |
And the following "permission overrides" exists:
| capability | permission | role | contextlevel | reference |
| mod/forum:editanypost | Allow | student | Course | C1 |
| mod/forum:replynews | Prevent | editingteacher | Course | C1 |
When I log in as "admin"
And I follow "Course 1"
And I expand "Users" node
And I follow "Permissions"
And I select "Student (1)" from "Advanced role override"
Then the "mod/forum:editanypost" field should match "1" value
And I press "Cancel"
And I select "Teacher (1)" from "Advanced role override"
And the "mod/forum:replynews" field should match "-1" value
And I press "Cancel"

Scenario: Add course enrolments
Given the following "users" exists:
| username | firstname | lastname | email |
Expand Down
43 changes: 43 additions & 0 deletions lib/tests/behat/behat_data_generators.php
Expand Up @@ -50,6 +50,10 @@
*/
class behat_data_generators extends behat_base {

const cap_allow = 'Allow';
const cap_prevent = 'Prevent';
const cap_prohibit = 'Prohibit';

/**
* @var testing_data_generator
*/
Expand Down Expand Up @@ -93,6 +97,11 @@ class behat_data_generators extends behat_base {
'switchids' => array('user' => 'userid', 'course' => 'courseid', 'role' => 'roleid')

),
'permission overrides' => array(
'datagenerator' => 'permission_override',
'required' => array('capability', 'permission', 'role', 'contextlevel', 'reference'),
'switchids' => array('role' => 'roleid')
),
'system role assigns' => array(
'datagenerator' => 'system_role_assign',
'required' => array('user', 'role'),
Expand Down Expand Up @@ -277,6 +286,40 @@ protected function process_enrol_user($data) {

}

/**
* Allows/denies a capability at the specified context
*
* @throws Exception
* @param array $data
* @return void
*/
protected function process_permission_override($data) {

// Will throw an exception if it does not exist.
$context = $this->get_context($data['contextlevel'], $data['reference']);

switch ($data['permission']) {
case self::cap_allow:
$permission = CAP_ALLOW;
break;
case self::cap_prevent:
$permission = CAP_PREVENT;
break;
case self::cap_prohibit:
$permission = CAP_PROHIBIT;
break;
default:
throw new Exception('The \'' . $data['permission'] . '\' permission does not exist');
break;
}

if (is_null(get_capability_info($data['capability']))) {
throw new Exception('The \'' . $data['capability'] . '\' capability does not exist');
}

role_change_permission($data['roleid'], $context, $data['capability'], $permission);
}

/**
* Assigns a role to a user at system context
*
Expand Down

0 comments on commit d06720b

Please sign in to comment.