? 371235-patched-benchmarks.txt ? 371235-unpatched-benchmarks.txt ? includes/database/install.inc ? sites/all/modules/cvs Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.320 diff -u -p -r1.320 form.inc --- includes/form.inc 3 Feb 2009 18:55:29 -0000 1.320 +++ includes/form.inc 14 Feb 2009 23:36:29 -0000 @@ -1972,7 +1972,7 @@ function theme_checkboxes($element) { $class .= ' ' . $element['#attributes']['class']; } $element['#children'] = '
' . (!empty($element['#children']) ? $element['#children'] : '') . '
'; - + return $element['#children']; } Index: modules/comment/comment.test =================================================================== RCS file: /cvs/drupal/drupal/modules/comment/comment.test,v retrieving revision 1.25 diff -u -p -r1.25 comment.test --- modules/comment/comment.test 7 Feb 2009 20:10:40 -0000 1.25 +++ modules/comment/comment.test 14 Feb 2009 23:36:38 -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', 'administer blocks')); + $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer blocks')); $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)); } /** @@ -162,23 +159,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. @@ -373,7 +353,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(); @@ -433,15 +413,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('
', t('Comments were not displayed.')); $this->assertNoLink('Add new comment', t('Link to add comment was found.')); @@ -470,7 +449,7 @@ class CommentApprovalTest extends Commen $this->drupalLogin($this->admin_user); // Set anonymous comments to require approval. - $this->setAnonymousUserComment(TRUE, FALSE); + $this->drupalSetPermissions('anonymous user', array('access content', 'access comments', 'post comments')); $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info. $this->drupalLogout(); @@ -504,7 +483,7 @@ class CommentApprovalTest extends Commen $this->drupalLogin($this->admin_user); // Set anonymous comments to require approval. - $this->setAnonymousUserComment(TRUE, FALSE); + $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.14 diff -u -p -r1.14 contact.test --- modules/contact/contact.test 30 Dec 2008 16:43:16 -0000 1.14 +++ modules/contact/contact.test 14 Feb 2009 23:36:45 -0000 @@ -22,7 +22,7 @@ class ContactSitewideTestCase extends Dr */ function testSiteWideContact() { // Create and login administrative 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.83 diff -u -p -r1.83 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 6 Feb 2009 00:30:36 -0000 1.83 +++ modules/simpletest/drupal_web_test_case.php 14 Feb 2009 23:37:17 -0000 @@ -651,9 +651,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; } @@ -678,36 +678,56 @@ 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. - */ - 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'); + * @param $merge + * A boolean if TRUE, will only add permissions instead of clearing all + * existing permissions. + * @return + * TRUE if the role is a valid role and the permissions have been + * assigned, otherwise FALSE. + */ + protected function drupalSetPermissions($role, array $permissions = array(), $merge = FALSE) { + $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, $merge); + + $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; @@ -1999,7 +2019,7 @@ class DrupalWebTestCase { $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code; return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser')); } - + /** * TODO write documentation. * @param $type @@ -2017,13 +2037,13 @@ class DrupalWebTestCase { ); $field_definition += $settings; field_create_field($field_definition); - + $field = field_read_field($field_name); $this->assertTrue($field, t('Created field @field_name of type @type.', array('@field_name' => $field_name, '@type' => $type))); - + return $field; } - + /** * TODO write documentation. * @param $field_name @@ -2046,11 +2066,10 @@ class DrupalWebTestCase { ), ); field_create_instance($instance_definition); - + $instance = field_read_instance($field_name, $bundle); $this->assertTrue($instance, t('Created instance of field @field_name on bundle @bundle.', array('@field_name' => $field_name, '@bundle' => $bundle))); - + return $instance; } } - Index: modules/simpletest/simpletest.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.test,v retrieving revision 1.16 diff -u -p -r1.16 simpletest.test --- modules/simpletest/simpletest.test 22 Jan 2009 12:46:06 -0000 1.16 +++ modules/simpletest/simpletest.test 14 Feb 2009 23:37:19 -0000 @@ -137,7 +137,8 @@ class SimpleTestFunctionalTest extends D $this->assertAssertion($this->pass, 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); $this->assertAssertion($this->fail, 'Other', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); - $this->assertAssertion(t('Created permissions: @perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); + $this->assertAssertion(t('Assigned permissions to role'), 'Role', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); + $this->assertAssertion(t('@perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); $this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), 'Role', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); // Check that a warning is catched by simpletest. Index: modules/system/system.install =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.install,v retrieving revision 1.306 diff -u -p -r1.306 system.install --- modules/system/system.install 3 Feb 2009 12:30:14 -0000 1.306 +++ modules/system/system.install 14 Feb 2009 23:38:33 -0000 @@ -354,18 +354,9 @@ function system_install() { // This sets uid 1 (superuser). We skip uid 2 but that's not a big problem. db_query("UPDATE {users} SET uid = 1 WHERE name = '%s'", 'placeholder-for-uid-1'); - // Built-in roles. - db_query("INSERT INTO {role} (name) VALUES ('%s')", 'anonymous user'); - db_query("INSERT INTO {role} (name) VALUES ('%s')", 'authenticated user'); - - // Anonymous role permissions. - db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", 1, 'access content'); - - // Authenticated role permissions. - db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", 2, 'access comments'); - db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", 2, 'access content'); - db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", 2, 'post comments'); - db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", 2, 'post comments without approval'); + // Add built-in roles. + user_add_role('anonymous user'); + user_add_role('authenticated user'); db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", 'theme_default', 's:7:"garland";'); db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name = '%s'", 1, 'theme', 'garland'); Index: modules/user/user.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.admin.inc,v retrieving revision 1.37 diff -u -p -r1.37 user.admin.inc --- modules/user/user.admin.inc 3 Feb 2009 18:55:32 -0000 1.37 +++ modules/user/user.admin.inc 14 Feb 2009 23:38:53 -0000 @@ -598,13 +598,9 @@ function user_admin_perm($form_state, $r * @see user_admin_perm */ 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); - } + foreach (array_keys($form_state['values']['role_names']) as $rid) { + $permissions = array_filter($form_state['values'][$rid]); + user_set_permissions($rid, $permissions); } drupal_set_message(t('The changes have been saved.')); @@ -710,14 +706,14 @@ function user_admin_role() { } function user_admin_role_validate($form, &$form_state) { - if ($form_state['values']['name']) { + if (trim($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']))); } } @@ -733,15 +729,11 @@ function user_admin_role_submit($form, & drupal_set_message(t('The role has been renamed.')); } elseif ($form_state['values']['op'] == t('Delete role')) { - db_query('DELETE FROM {role} WHERE rid = %d', $form_state['values']['rid']); - db_query('DELETE FROM {role_permission} WHERE rid = %d', $form_state['values']['rid']); - // Update the users who have this role set: - db_query('DELETE FROM {users_roles} WHERE rid = %d', $form_state['values']['rid']); - + user_delete_role($form_state['values']['rid']); 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.963 diff -u -p -r1.963 user.module --- modules/user/user.module 9 Feb 2009 07:36:15 -0000 1.963 +++ modules/user/user.module 14 Feb 2009 23:39:21 -0000 @@ -2784,3 +2784,120 @@ function _user_forms(&$edit, $account, $ return empty($groups) ? FALSE : $groups; } +/** + * Get the role ID of a role name. + * + * @param $role_name + * 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_name) { + return db_query("SELECT rid FROM {role} WHERE name = :name", array(':name' => $role_name))->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; +} + +/** + * Delete a user role. + * + * @param $role + * A string with the role name, or an integer with the role ID. + * @return + * TRUE on success, FALSE otherwise. + */ +function user_delete_role($role) { + if (!is_numeric($role)) { + // Fetch the role ID if $role is a role name. + $role = user_get_role_id($role); + } + + if (!$role) { + return FALSE; + } + + db_delete('role')->condition('rid', $role)->execute(); + db_delete('role_permission')->condition('rid', $role)->execute(); + db_delete('users_roles')->condition('rid', $role)->execute(); + + // Clear the user access cache. + user_access(NULL, NULL, TRUE); + + return TRUE; +} + +/** + * 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. + * @param $merge + * A boolean if TRUE, will only add permissions instead of clearing all + * existing permissions. + * @return + * TRUE on success, FALSE otherwise. + */ +function user_set_permissions($role, array $permissions = array(), $merge = FALSE) { + if (!is_numeric($role)) { + // Fetch the role ID if $role is a role name. + $role = user_get_role_id($role); + } + + if (!$role) { + return FALSE; + } + + if ($merge) { + // Find only the permissions the user role currently does not have. + $current = db_query("SELECT permission FROM {role_permission} WHERE rid = :rid", array(':rid' => $role))->fetchCol(); + $permissions = array_diff($permissions, $current); + } + else { + // 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(); + } + + // Clear the user access cache. + user_access(NULL, NULL, TRUE); + + return TRUE; +} Index: modules/user/user.test =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.test,v retrieving revision 1.28 diff -u -p -r1.28 user.test --- modules/user/user.test 9 Feb 2009 07:36:15 -0000 1.28 +++ modules/user/user.test 14 Feb 2009 23:39:32 -0000 @@ -699,55 +699,111 @@ class UserPictureTestCase extends Drupal } } - -class UserPermissionsTestCase extends DrupalWebTestCase { - protected $admin_user; +/** + * Test the user role and permission functionality. + */ +class UserRolesPermissionsFunctionalTest extends DrupalWebTestCase { + protected $test_user; protected $rid; function getInfo() { return array( - 'name' => t('Role permissions'), - 'description' => t('Verify that role permissions can be added and removed via the permissions page.'), - 'group' => t('User') + 'name' => t('User roles and permissions'), + 'description' => t('Test the user roles and permissions functionality.'), + 'group' => t('User'), ); } - function setUp() { - parent::setUp(); - - $this->admin_user = $this->drupalCreateUser(array('administer permissions', 'access user profiles')); - - // Find the new role ID - it must be the maximum. - $all_rids = array_keys($this->admin_user->roles); - sort($all_rids); - $this->rid = array_pop($all_rids); - } - /** - * Change user permissions and check user_access(). + * Test user role and permission interfaces. */ - function testUserPermissionChanges() { - $this->drupalLogin($this->admin_user); - $rid = $this->rid; - $account = $this->admin_user; + function testRolesAndPermissions() { + $test_user = $this->drupalCreateUser(); + $admin_user = $this->drupalCreateUser(array('administer permissions', 'administer users')); + $this->drupalLogin($admin_user); + $perm = 'access user profiles'; + + // Add a new user role and fetch it's role ID. + $role = $this->getRandomRoleName(); + $this->drupalPost('admin/user/roles', array('name' => $role), t('Add role')); + $this->assertText(t('The role has been added.')); + $role_id = user_get_role_id($role); + + // Try to add another role with duplicate names. + $this->drupalPost('admin/user/roles', array('name' => 'anonymous user'), t('Add role')); + $this->assertRaw(t('The role name %role already exists. Please choose another role name.', array('%role' => 'anonymous user')), t('Could not add a duplicate role name.')); + $this->drupalPost('admin/user/roles/edit/'. $role_id, array('name' => 'anonymous user'), t('Save role')); + $this->assertRaw(t('The role name %role already exists. Please choose another role name.', array('%role' => 'anonymous user')), t('Could not add a duplicate role name.')); + + // Try to add an invalid role name. + $this->drupalPost('admin/user/roles', array('name' => ' '), t('Add role')); + $this->assertText(t('You must specify a valid role name.')); + + // Try to edit the locked anonymous and authenticated user roles. + foreach (array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID) as $invalid_rid) { + $this->drupalGet('admin/user/roles/edit/' . $invalid_rid); + $this->assertEqual(url('admin/user/roles', array('absolute' => TRUE)), $this->getUrl(), t('Cannot edit locked user role.')); + } + + // Rename the new role. + $this->clickLink(t('edit role'), 0); + $role = $this->getRandomRoleName(); + $this->drupalPost(NULL, array('name' => $role), t('Save role')); + $this->assertText(t('The role has been renamed.')); + $this->assertEqual($role_id, user_get_role_id($role), t('Role ID unchanged.')); - // Add a permission. - $this->assertFalse(user_access('administer nodes', $account, TRUE), t('User does not have "administer nodes" permission.')); + // Add a permission to the new role. + $this->clickLink(t('edit permissions'), 2); $edit = array(); - $edit[$rid . '[administer nodes]'] = TRUE; + $edit[$role_id . '[' . $perm . ']'] = TRUE; $this->drupalPost('admin/user/permissions', $edit, t('Save permissions')); - $this->assertText(t('The changes have been saved.'), t('Successful save message displayed.')); - $this->assertTrue(user_access('administer nodes', $account, TRUE), t('User now has "administer nodes" permission.')); + $this->assertText(t('The changes have been saved.')); + + // Assign a user to the new role. + $this->drupalGet('user/' . $test_user->uid . '/edit'); + $this->assertFieldByXPath('//input[@type="checkbox" and @name="roles[' . DRUPAL_AUTHENTICATED_RID . ']" and @disabled="disabled" and @checked="checked"]', TRUE, t('The authenticated user role checkbox is checked but disabled.')); + $edit = array(); + // For some reason, the FormAPI will show an 'invalid choice selected' + // without the following line. DrupalWTF? It's a disabled checkbox... + $edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = FALSE; + $edit['roles[' . $role_id . ']'] = TRUE; + $this->drupalPost(NULL, $edit, t('Save')); + $this->assertText(t('The changes have been saved.')); + + // Test that the user has the assigned role permissions. + $test_user = user_load($test_user->uid); + $this->assertTrue(user_access($perm, $test_user, TRUE), t('User has "access user profiles" permission.')); + + // Delete the role and reload the user since it's roles have changed. + $this->drupalPost('admin/user/roles/edit/' . $role_id, array(), t('Delete role')); + $this->assertText(t('The role has been deleted.')); + $test_user = user_load($test_user->uid); + + // Test that the user no longer has the assigned role permission. + $this->assertFalse(user_access($perm, $test_user, TRUE), t('User does not have "access user profiles" permission.')); + $this->assertFalse(user_access($perm, drupal_anonymous_user()), t('Anonymous user does not have "access user profiles" permission.')); - // Remove a permission. - $this->assertTrue(user_access('access user profiles', $account, TRUE), t('User has "access user profiles" permission.')); + // Assign a permission to the anonymous and authenticated user role. $edit = array(); - $edit[$rid . '[access user profiles]'] = FALSE; + $edit[DRUPAL_ANONYMOUS_RID . '[' . $perm . ']'] = TRUE; + $edit[DRUPAL_AUTHENTICATED_RID . '[' . $perm . ']'] = TRUE; $this->drupalPost('admin/user/permissions', $edit, t('Save permissions')); - $this->assertText(t('The changes have been saved.'), t('Successful save message displayed.')); - $this->assertFalse(user_access('access user profiles', $account, TRUE), t('User no longer has "access user profiles" permission.')); + $this->assertText(t('The changes have been saved.')); + + // Test that both anonymous and authenticated users have the permission. + $this->assertTrue(user_access($perm, $test_user, TRUE), t('User has "access user profiles" permission.')); + $this->assertTrue(user_access($perm, drupal_anonymous_user()), t('Anonymous user has "access user profiles" permission.')); } + /** + * Generate a new role name and test to make sure it is not already used. + */ + private function getRandomRoleName() { + do { + $role = $this->randomName(4, 'role_'); + } while (in_array($role, user_roles())); + return $role; + } } class UserAdminTestCase extends DrupalWebTestCase { Index: profiles/default/default.profile =================================================================== RCS file: /cvs/drupal/drupal/profiles/default/default.profile,v retrieving revision 1.37 diff -u -p -r1.37 default.profile --- profiles/default/default.profile 3 Feb 2009 12:30:14 -0000 1.37 +++ profiles/default/default.profile 14 Feb 2009 23:39:38 -0000 @@ -90,7 +90,11 @@ function default_profile_task_list() { * modify the $task, otherwise discarded. */ function default_profile_tasks(&$task, $url) { - + + // Enable default permissions. + user_set_permissions(DRUPAL_ANONYMOUS_RID, array('access content')); + user_set_permissions(DRUPAL_AUTHENTICATED_RID, array('access content', 'access comments', 'post comments', 'post comments without approval')); + // Enable 3 standard blocks. db_query("INSERT INTO {block} (module, delta, theme, status, weight, region, pages, cache) VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s', %d)", 'user', 'login', 'garland', 1, 0, 'left', '', -1); db_query("INSERT INTO {block} (module, delta, theme, status, weight, region, pages, cache) VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s', %d)", 'user', 'navigation', 'garland', 1, 0, 'left', '', -1);