MDL-42326 behat: Data generator for permission overrides

This commit is contained in:
David Monllao
2013-10-28 11:33:17 +08:00
committed by David Monllao
parent 67d4599aff
commit 3b51b66e2b
+43
View File
@@ -50,6 +50,10 @@ use Behat\Behat\Exception\PendingException as PendingException;
*/
class behat_data_generators extends behat_base {
const cap_allow = 'Allow';
const cap_prevent = 'Prevent';
const cap_prohibit = 'Prohibit';
/**
* @var testing_data_generator
*/
@@ -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'),
@@ -277,6 +286,40 @@ class behat_data_generators extends behat_base {
}
/**
* 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
*