Index: modules/contact/contact.test =================================================================== RCS file: /cvs/drupal/drupal/modules/contact/contact.test,v retrieving revision 1.13 diff -u -p -r1.13 contact.test --- modules/contact/contact.test 25 Nov 2008 13:14:26 -0000 1.13 +++ modules/contact/contact.test 27 Nov 2008 19:23:34 -0000 @@ -22,7 +22,7 @@ class ContactSitewideTestCase extends Dr */ function testSiteWideContact() { // Create and login administative user. - $admin_user = $this->drupalCreateUser(array('administer site-wide contact form', 'administer permissions')); + $admin_user = $this->drupalCreateUser(array('administer site-wide contact form')); $this->drupalLogin($admin_user); // Set settings. @@ -38,7 +38,7 @@ class ContactSitewideTestCase extends Dr $this->deleteCategories(); // Ensure that the contact form won't be shown without categories. - $this->setPermission('anonymous user', array('access site-wide contact form' => TRUE)); + $this->drupalSetPermissions('anonymous user', array('access site-wide contact form')); $this->drupalLogout(); $this->drupalGet('contact'); $this->assertText(t('The contact form has not been configured.'), t('Contact form will not work without categories configured.')); @@ -73,7 +73,7 @@ class ContactSitewideTestCase extends Dr $this->assertRaw(t('Category %category has been updated.', array('%category' => $category)), t('Category successfully updated.')); // Ensure that the contact form is shown without a category selection input. - $this->setPermission('anonymous user', array('access site-wide contact form' => TRUE)); + $this->drupalSetPermissions('anonymous user', array('access site-wide contact form')); $this->drupalLogout(); $this->drupalGet('contact'); $this->assertText($contact_form_information, t('Contact form is shown when there is one category.')); @@ -91,17 +91,14 @@ class ContactSitewideTestCase extends Dr $this->assertTrue(db_query('DELETE FROM {flood}'), t('Flood table emptied.')); // Check to see that anonymous user cannot see contact page without permission. - $this->setPermission('anonymous user', array('access site-wide contact form' => FALSE)); + $this->drupalSetPermissions('anonymous user', array()); $this->drupalLogout(); $this->drupalGet('contact'); $this->assertResponse(403, t('Access denied to anonymous user without permission.')); // Give anonymous user permission and see that page is viewable. - $this->drupalLogin($admin_user); - $this->setPermission('anonymous user', array('access site-wide contact form' => TRUE)); - $this->drupalLogout(); - + $this->drupalSetPermissions('anonymous user', array('access site-wide contact form')); $this->drupalGet('contact'); $this->assertResponse(200, t('Access granted to anonymous user with permission.')); @@ -227,29 +224,6 @@ class ContactSitewideTestCase extends Dr } return $categories; } - - /** - * Set permission. - * - * @param string $role User role to set permissions for. - * @param array $permissions Key-value array of permissions to set. - */ - function setPermission($role, $permissions) { - // Get role id (rid) for specified role. - $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array($role))); - if ($rid === FALSE) { - $this->fail(t(' [permission] Role "' . $role . '" not found.')); - } - - // Create edit array from permission. - $edit = array(); - foreach ($permissions as $name => $value) { - $edit[$rid . '[' . $name . ']'] = $value; - } - - $this->drupalPost('admin/user/permissions', $edit, t('Save permissions')); - $this->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.')); - } } /** Index: modules/simpletest/drupal_web_test_case.php =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v retrieving revision 1.62 diff -u -p -r1.62 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 27 Nov 2008 07:07:01 -0000 1.62 +++ modules/simpletest/drupal_web_test_case.php 27 Nov 2008 19:23:35 -0000 @@ -617,7 +617,7 @@ class DrupalWebTestCase { */ protected function drupalCreateUser($permissions = NULL) { // Create a role with the given permission set. - if (!($rid = $this->_drupalCreateRole($permissions))) { + if (!($rid = $this->drupalCreateRole($permissions))) { return FALSE; } @@ -642,14 +642,23 @@ class DrupalWebTestCase { } /** - * Internal helper function; Create a role with specified permissions. + * Assign permissions to a user role. * + * @param $role + * The user role to assign permissions. * @param $permissions * Array of permission names to assign to role. * @return - * Role ID of newly created role, or FALSE if role creation failed. + * TRUE if the role is a valid role and the permissions have been + * assigned, otherwise FALSE. */ - protected function _drupalCreateRole(Array $permissions = NULL) { + protected function drupalSetPermissions($role, $permissions = NULL) { + $rid = db_query("SELECT rid FROM {role} WHERE name = :name", array(':name' => $role))->fetchField(); + if ($rid === FALSE) { + $this->fail(t('Role %role not found.', array('%role' => $role))); + return FALSE; + } + // Generate string version of permissions list. if ($permissions === NULL) { $permissions = array('access comments', 'access content', 'post comments', 'post comments without approval'); @@ -659,18 +668,42 @@ class DrupalWebTestCase { return FALSE; } + // Clear permissions for role. + db_delete('role_permission') + ->condition('rid', $rid) + ->execute(); + + // Assign permissions to role. + foreach ($permissions as $permission_string) { + db_insert('role_permission') + ->fields(array( + 'rid' => $rid, + 'permission' => $permission_string, + )) + ->execute(); + } + $count = db_query("SELECT COUNT(*) FROM {role_permission} WHERE rid = :rid", array(':rid' => $rid))->fetchField(); + $result = $count == count($permissions); + $this->assertEqual($result, t('Assigned role %role permissions: @perms', array('%role' => $role, '@perms' => implode(', ', $permissions))), t('Role')); + + return $result; + } + + /** + * Create a role with specified permissions. + * + * @param $permissions + * Array of permission names to assign to role. + * @return + * Role ID of newly created role, or FALSE if role creation failed. + */ + protected function drupalCreateRole($permissions = NULL) { // Create new role. $role_name = $this->randomName(); db_query("INSERT INTO {role} (name) VALUES ('%s')", $role_name); $role = db_fetch_object(db_query("SELECT * FROM {role} WHERE name = '%s'", $role_name)); $this->assertTrue($role, t('Created role of name: @role_name, id: @rid', array('@role_name' => $role_name, '@rid' => (isset($role->rid) ? $role->rid : t('-n/a-')))), t('Role')); - if ($role && !empty($role->rid)) { - // Assign permissions to role and mark it for clean-up. - foreach ($permissions as $permission_string) { - db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", $role->rid, $permission_string); - } - $count = db_result(db_query("SELECT COUNT(*) FROM {role_permission} WHERE rid = %d", $role->rid)); - $this->assertTrue($count == count($permissions), t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role')); + if ($role && !empty($role->rid) && $this->drupalSetPermissions($role->name, $permissions)) { return $role->rid; } else { @@ -722,7 +755,7 @@ class DrupalWebTestCase { } if (!isset($user)) { - $user = $this->_drupalCreateRole(); + $user = $this->drupalCreateRole(); } $edit = array(