Commit caa2e7e1 authored by Dries's avatar Dries
Browse files

- Patch #267308 by boombatower: check permissions to make sure they are avlid.

parent 89f115c4
......@@ -191,8 +191,7 @@ function randomName($number = 4, $prefix = 'simpletest_') {
*/
function drupalCreateUser($permissions = NULL) {
// Create a role with the given permission set.
$rid = $this->_drupalCreateRole($permissions);
if (!$rid) {
if (!($rid = $this->_drupalCreateRole($permissions))) {
return FALSE;
}
......@@ -228,6 +227,10 @@ private function _drupalCreateRole($permissions = NULL) {
$permissions = array('access comments', 'access content', 'post comments', 'post comments without approval');
}
if (!$this->checkPermissions($permissions)) {
return FALSE;
}
// Create new role.
$role_name = $this->randomName();
db_query("INSERT INTO {role} (name) VALUES ('%s')", $role_name);
......@@ -247,6 +250,30 @@ private function _drupalCreateRole($permissions = NULL) {
}
}
/**
* Check to make sure that the array of permissions are valid.
*
* @param array $permissions Permissions to check.
* @param boolean $reset Reset cached available permissions.
* @return boolean Valid.
*/
private function checkPermissions(array $permissions, $reset = FALSE) {
static $available;
if (!isset($available) || $reset) {
$available = array_keys(module_invoke_all('perm'));
}
$valid = TRUE;
foreach ($permissions as $permission) {
if (!in_array($permission, $available)) {
$this->fail(t('Invalid permission %permission.', array('%permission' => $permission)), t('Role'));
$valid = FALSE;
}
}
return $valid;
}
/**
* Logs in a user with the internal browser. If already logged in then logs the current
* user out before logging in the specified user. If no user is specified then a new
......@@ -331,6 +358,7 @@ function setUp() {
actions_synchronize();
_drupal_flush_css_js();
$this->refreshVariables();
$this->checkPermissions(array(), TRUE);
// Restore necessary variables.
variable_set('install_profile', 'default');
......
......@@ -22,11 +22,16 @@ class SimpleTestTestCase extends DrupalWebTestCase {
* Implementation of setUp().
*/
function setUp() {
parent::setUp('simpletest');
if (!$this->inCURL()) {
parent::setUp('simpletest');
// Create and login user
$admin_user = $this->drupalCreateUser(array('administer unit tests'));
$this->drupalLogin($admin_user);
// Create and login user
$admin_user = $this->drupalCreateUser(array('administer unit tests'));
$this->drupalLogin($admin_user);
}
else {
parent::setUp();
}
}
/**
......@@ -47,6 +52,8 @@ class SimpleTestTestCase extends DrupalWebTestCase {
function testWebTestRunner() {
$this->pass = t('SimpleTest pass.');
$this->fail = t('SimpleTest fail.');
$this->valid_permission = 'access content';
$this->invalid_permission = 'invalid permission';
if ($this->inCURL()) {
// Only run following code if this test is running itself through a CURL request.
......@@ -73,6 +80,9 @@ class SimpleTestTestCase extends DrupalWebTestCase {
function stubTest() {
$this->pass($this->pass);
$this->fail($this->fail);
$this->drupalCreateUser(array($this->valid_permission));
$this->drupalCreateUser(array($this->invalid_permission));
}
/**
......@@ -81,6 +91,9 @@ class SimpleTestTestCase extends DrupalWebTestCase {
function confirmStubTestResults() {
$this->assertAssertion($this->pass, '[Other]', 'Pass');
$this->assertAssertion($this->fail, '[Other]', 'Fail');
$this->assertAssertion(t('Created permissions: @perms', array('@perms' => $this->valid_permission)), '[Role]', 'Pass');
$this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), '[Role]', 'Fail');
}
/**
......@@ -93,6 +106,7 @@ class SimpleTestTestCase extends DrupalWebTestCase {
* @return Assertion result.
*/
function assertAssertion($message, $type, $status) {
$message = trim(strip_tags($message));
$found = FALSE;
foreach ($this->results['assertions'] as $assertion) {
if ($assertion['message'] == $message &&
......@@ -154,7 +168,7 @@ class SimpleTestTestCase extends DrupalWebTestCase {
* @return string Extracted text.
*/
function asText(SimpleXMLElement $element) {
return strip_tags($element->asXML());
return trim(strip_tags($element->asXML()));
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment