? ab.php
? log_node.txt
? placeholder-benchmarks.txt
? modules/simpletest/tests/343502.test
? modules/simpletest/tests/343502.test.txt
Index: modules/comment/comment.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.test,v
retrieving revision 1.21
diff -u -p -r1.21 comment.test
--- modules/comment/comment.test	16 Dec 2008 23:57:32 -0000	1.21
+++ modules/comment/comment.test	18 Dec 2008 18:01:42 -0000
@@ -9,12 +9,9 @@ class CommentHelperCase extends DrupalWe
   function setUp() {
     parent::setUp('comment');
     // Create users.
-    $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer permissions'));
+    $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments'));
     $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content'));
-
-    $this->drupalLogin($this->web_user);
-    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
-    $this->drupalLogout();
+    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->uid));
   }
 
   /**
@@ -160,23 +157,6 @@ class CommentHelperCase extends DrupalWe
   }
 
   /**
-   * Set anonymous comment setting.
-   *
-   * @param boolean $enabled
-   *   Allow anonymous commenting.
-   * @param boolean $without_approval
-   *   Allow anonymous commenting without approval.
-   */
-  function setAnonymousUserComment($enabled, $without_approval) {
-    $edit = array();
-    $edit['1[access comments]'] = $enabled;
-    $edit['1[post comments]'] = $enabled;
-    $edit['1[post comments without approval]'] = $without_approval;
-    $this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
-    $this->assertText(t('The changes have been saved.'), t('Anonymous user comments ' . ($enabled ? 'enabled' : 'disabled') . '.'));
-  }
-
-  /**
    * Check for contact info.
    *
    * @return boolean Contact info is available.
@@ -387,7 +367,7 @@ class CommentAnonymous extends CommentHe
   function testAnonymous() {
     $this->drupalLogin($this->admin_user);
     // Enabled anonymous user comments.
-    $this->setAnonymousUserComment(TRUE, TRUE);
+    $this->drupalSetPermissions('anonymous user', array('access content', 'access comments', 'post comments', 'post comments without approval'));
     $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
     $this->drupalLogout();
 
@@ -447,15 +427,14 @@ class CommentAnonymous extends CommentHe
 
     $this->drupalGet('admin/content/comment');
     $this->assertNoRaw('comments[' . $anonymous_comment3->id . ']', t('Comment was deleted.'));
+    $this->drupalLogout();
 
-    // Reset.
-    $this->drupalLogin($this->admin_user);
-    $this->setAnonymousUserComment(FALSE, FALSE);
+    // Reset permissions.
+    $this->drupalSetPermissions('anonymous user', array('access content'));
 
     // Attempt to view comments while disallowed.
     // NOTE: if authenticated user has permission to post comments, then a
     // "Login or register to post comments" type link may be shown.
-    $this->drupalLogout();
     $this->drupalGet('node/' . $this->node->nid);
     $this->assertNoRaw('<div id="comments">', t('Comments were not displayed.'));
     $this->assertNoLink('Add new comment', t('Link to add comment was found.'));
@@ -483,8 +462,8 @@ class CommentApprovalTest extends Commen
   function testApprovalAdminInterface() {
     $this->drupalLogin($this->admin_user);
 
-    // Set anonymouse comments to require approval.
-    $this->setAnonymousUserComment(TRUE, FALSE);
+    // Set anonymous comments to require approval.
+    $this->drupalSetPermissions('anonymous user', array('access content', 'access comments', 'post comments'));
     $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
     $this->drupalLogout();
 
@@ -517,8 +496,8 @@ class CommentApprovalTest extends Commen
   function testApprovalNodeInterface() {
     $this->drupalLogin($this->admin_user);
 
-    // Set anonymouse comments to require approval.
-    $this->setAnonymousUserComment(TRUE, FALSE);
+    // Set anonymous comments to require approval.
+    $this->drupalSetPermissions('anonymous user', array('access content', 'access comments', 'post comments'));
     $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
     $this->drupalLogout();
 
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	18 Dec 2008 18:01:42 -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');
     $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.75
diff -u -p -r1.75 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	18 Dec 2008 00:42:55 -0000	1.75
+++ modules/simpletest/drupal_web_test_case.php	18 Dec 2008 18:01:43 -0000
@@ -645,9 +645,9 @@ class DrupalWebTestCase {
    *   A fully loaded user object with pass_raw property, or FALSE if account
    *   creation fails.
    */
-  protected function drupalCreateUser($permissions = NULL) {
+  protected function drupalCreateUser(array $permissions = array('access comments', 'access content', 'post comments', 'post comments without approval')) {
     // Create a role with the given permission set.
-    if (!($rid = $this->_drupalCreateRole($permissions))) {
+    if (!($rid = $this->drupalCreateRole($permissions))) {
       return FALSE;
     }
 
@@ -672,36 +672,53 @@ 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) {
-    // Generate string version of permissions list.
-    if ($permissions === NULL) {
-      $permissions = array('access comments', 'access content', 'post comments', 'post comments without approval');
+  protected function drupalSetPermissions($role, array $permissions = array()) {
+    $rid = user_get_role_id($role);
+    if (!$rid) {
+      $this->fail(t('Role %role not found.', array('%role' => $role)));
+      return FALSE;
     }
 
+    // Check the all the permissions strings are valid.
     if (!$this->checkPermissions($permissions)) {
       return FALSE;
     }
 
+    // Assign the user permissions.
+    user_set_permissions($rid, $permissions);
+
+    $count = db_query("SELECT COUNT(*) FROM {role_permission} WHERE rid = :rid", array(':rid' => $rid))->fetchField();
+    $result = $count == count($permissions);
+    $this->assertTrue($result, t('Assigned permissions to role %role: @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(array $permissions = array()) {
     // 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'));
-      return $role->rid;
+    $rid = user_add_role($role_name);
+    $this->assertTrue($rid, t('Created role of name: @role_name, id: @rid', array('@role_name' => $role_name, '@rid' => $rid)), t('Role'));
+    if ($rid && $this->drupalSetPermissions($role_name, $permissions)) {
+      return $rid;
     }
     else {
       return FALSE;
Index: modules/simpletest/simpletest.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.test,v
retrieving revision 1.13
diff -u -p -r1.13 simpletest.test
--- modules/simpletest/simpletest.test	11 Dec 2008 20:11:40 -0000	1.13
+++ modules/simpletest/simpletest.test	18 Dec 2008 18:01:44 -0000
@@ -137,7 +137,8 @@ class SimpleTestTestCase extends DrupalW
     $this->assertAssertion($this->pass, 'Other', 'Pass', 'simpletest.test', 'SimpleTestTestCase->stubTest()');
     $this->assertAssertion($this->fail, 'Other', 'Fail', 'simpletest.test', 'SimpleTestTestCase->stubTest()');
 
-    $this->assertAssertion(t('Created permissions: @perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'simpletest.test', 'SimpleTestTestCase->stubTest()');
+    $this->assertAssertion(t('Assigned permissions to role'), 'Role', 'Pass', 'simpletest.test', 'SimpleTestTestCase->stubTest()');
+    $this->assertAssertion(t('@perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'simpletest.test', 'SimpleTestTestCase->stubTest()');
     $this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), 'Role', 'Fail', 'simpletest.test', 'SimpleTestTestCase->stubTest()');
 
     // Check that a warning is catched by simpletest.
Index: modules/user/user.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.admin.inc,v
retrieving revision 1.33
diff -u -p -r1.33 user.admin.inc
--- modules/user/user.admin.inc	16 Nov 2008 15:10:49 -0000	1.33
+++ modules/user/user.admin.inc	18 Dec 2008 18:01:44 -0000
@@ -550,12 +550,8 @@ function user_admin_perm($form_state, $r
  */
 function user_admin_perm_submit($form, &$form_state) {
   foreach ($form_state['values']['role_names'] as $rid => $name) {
-    $checked = array_filter($form_state['values'][$rid]);
-    // Delete existing permissions for the role. This handles "unchecking" checkboxes.
-    db_query("DELETE FROM {role_permission} WHERE rid = %d", $rid);
-    foreach ($checked as $permission) {
-      db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", $rid, $permission);
-    }
+    $permissions = array_filter($form_state['values'][$rid]);
+    user_set_permissions($rid, $permissions);
   }
 
   drupal_set_message(t('The changes have been saved.'));
@@ -663,12 +659,12 @@ function user_admin_role() {
 function user_admin_role_validate($form, &$form_state) {
   if ($form_state['values']['name']) {
     if ($form_state['values']['op'] == t('Save role')) {
-      if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s' AND rid != %d", $form_state['values']['name'], $form_state['values']['rid']))) {
+      if (($rid = user_get_role_id($form_state['values']['name'])) && $rid != $form_state['values']['rid']) {
         form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
       }
     }
     elseif ($form_state['values']['op'] == t('Add role')) {
-      if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s'", $form_state['values']['name']))) {
+      if (user_get_role_id($form_state['values']['name'])) {
         form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
       }
     }
@@ -692,7 +688,7 @@ function user_admin_role_submit($form, &
     drupal_set_message(t('The role has been deleted.'));
   }
   elseif ($form_state['values']['op'] == t('Add role')) {
-    db_query("INSERT INTO {role} (name) VALUES ('%s')", $form_state['values']['name']);
+    user_add_role($form_state['values']['name']);
     drupal_set_message(t('The role has been added.'));
   }
   $form_state['redirect'] = 'admin/user/roles';
Index: modules/user/user.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.module,v
retrieving revision 1.946
diff -u -p -r1.946 user.module
--- modules/user/user.module	16 Dec 2008 23:57:33 -0000	1.946
+++ modules/user/user.module	18 Dec 2008 18:01:47 -0000
@@ -2444,3 +2444,79 @@ function _user_forms(&$edit, $account, $
   return empty($groups) ? FALSE : $groups;
 }
 
+/**
+ * Get the role ID of a role name.
+ *
+ * @param $role
+ *   The name of the role.
+ * @return
+ *   An integer of the role's ID, or FALSE if the role was not found.
+ */
+function user_get_role_id($role) {
+  return db_query("SELECT rid FROM {role} WHERE name = :name", array(':name' => $role))->fetchField();
+}
+
+/**
+ * Create a new user role.
+ *
+ * @param $name
+ *   The name of the new user role.
+ * @param $permissions
+ *   An array of permissions to assign to the role.
+ * @return
+ *   The role ID of the new role, or FALSE if a role with $name already exists.
+ */
+function user_add_role($name, array $permissions = array()) {
+  // Check if the user role already exists.
+  if (user_get_role_id($name)) {
+    return FALSE;
+  }
+
+  // Add role to the database.
+  $rid = db_insert('role')
+    ->fields(array('name' => $name))
+    ->execute();
+
+  // Assign the permissions to the role.
+  user_set_permissions($rid, $permissions);
+
+  return $rid;
+}
+
+/**
+ * Assign an array of permissions to a role.
+ *
+ * @param $role
+ *   A string with the role name, or an integer with the role ID.
+ * @param $permissions
+ *   An array of permissions strings.
+ * @return
+ *   TRUE on success, FALSE otherwise.
+ */
+function user_set_permissions($role, array $permissions = array()) {
+  if (!is_numeric($role)) {
+    // Get the role ID if $role is not an integer.
+    $role = user_get_role_id($role);
+  }
+
+  if (!$role) {
+    return FALSE;
+  }
+
+  // Delete existing permissions for the role.
+  db_delete('role_permission')
+    ->condition('rid', $role)
+    ->execute();
+
+  // Assign the new permissions for the role.
+  foreach ($permissions as $permission_string) {
+    db_insert('role_permission')
+      ->fields(array(
+        'rid' => $role,
+        'permission' => $permission_string,
+      ))
+      ->execute();
+  }
+
+  return TRUE;
+}
