diff --git a/core/includes/update.inc b/core/includes/update.inc index e19ea56..77d4c4a 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -227,9 +227,36 @@ function update_prepare_d8_language() { } /** - * Prepare a minimal working D8 role name system. This is required for session bootstrapping during an upgrade from D7. + * Prepare a minimal working D8 role name system. + * + * This is required for session bootstrapping during an upgrade from D7. */ function update_prepare_d8_role_names() { + if (!db_field_exists('role', 'label')) { + // Add the 'label' column to the role table. + $label_column = array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + 'description' => 'Role label.', + 'translatable' => TRUE, + ); + db_add_field('role', 'label', $label_column); + + // Copy the old role name to the label. + db_query("UPDATE {role} SET label = name"); + // Copy the role id to the name column, the rid is our new machine name. The + // only exception are the anonymous and authenticated user roles. + db_query('UPDATE {role} SET name = rid WHERE rid NOT IN (1, 2)'); + db_update('role') + ->fields(array('name' => DRUPAL_ANONYMOUS_ROLE)) + ->condition('rid', 1) + ->execute(); + db_update('role') + ->fields(array('name' => DRUPAL_AUTHENTICATED_ROLE)) + ->condition('rid', 2) + ->execute(); + } if (!db_field_exists('role_permission', 'role_name')) { // Add the 'role_name' column to the role_permission table. $column = array( @@ -238,6 +265,16 @@ function update_prepare_d8_role_names() { 'description' => 'Foreign Key: {role}.name.', ); db_add_field('role_permission', 'role_name', $column); + + db_query('UPDATE {role_permission} SET role_name = rid WHERE rid NOT IN (1, 2)'); + db_update('role_permission') + ->fields(array('role_name' => DRUPAL_ANONYMOUS_ROLE)) + ->condition('rid', 1) + ->execute(); + db_update('role_permission') + ->fields(array('role_name' => DRUPAL_AUTHENTICATED_ROLE)) + ->condition('rid', 2) + ->execute(); } if (!db_field_exists('users_roles', 'role_name')) { // Add the 'role_name' column to the users_roles table. @@ -248,79 +285,16 @@ function update_prepare_d8_role_names() { 'description' => 'Primary Key: {role}.name for role.', ); db_add_field('users_roles', 'role_name', $column); - } - if (!db_field_exists('role', 'label')) { - // Add the 'label' column to the role table. - $label_column = array( - 'type' => 'varchar', - 'length' => 255, - 'default' => '', - 'description' => 'Role label.', - 'translatable' => TRUE, - ); - db_add_field('role', 'label', $label_column); - - // Copy the old role name to the label. - db_query('UPDATE {role} SET label = name'); - - // Load the mapping of rids to role_names. - $rid_mapping = db_select('role', 'r') - ->fields('r', array('rid', 'name')) - ->execute() - ->fetchAllKeyed(0, 1); - - // Convert the old name to a valid machine name by replacing white spaces and - // upper case letters. - foreach ($rid_mapping as $rid => $role_name) { - $new_name = drupal_strtolower($role_name); - $new_name = preg_replace('/[^a-z0-9]/u', '_', $new_name); - // Check if the new name already exists. - do { - $exists = db_select('role', 'r') - ->fields('r', array('name')) - ->condition('name', $role_name, '!=') - ->condition('name', $new_name) - ->execute() - ->fetchCol(); - if (!empty($exists)) { - if (drupal_strlen($new_name) < 64) { - // Try again with an additional '_' until this name is unique. - $new_name = $new_name . '_'; - } - else { - // In the rare case that this is a long name and still not unique we - // just replace a random character at a random position. - $chars = 'abcdefghijklmnopqrstuvwxyz0123456789_'; - $new_name = substr_replace( - $new_name, - $chars[rand(0, strlen($chars) - 1)], - rand(0, strlen($new_name) - 1), - 1 - ); - - } - } - } while (!empty($exists)); - - // Replace the old name with the new name. - db_update('role') - ->fields(array('name' => $new_name)) - ->condition('name', $role_name) - ->execute(); - - // Fill in role_name data in the role_permission table. - db_update('role_permission') - ->fields(array('role_name' => $new_name)) - ->condition('rid', $rid) - ->execute(); - - // Fill in role_name data in the users_roles table. - db_update('users_roles') - ->fields(array('role_name' => $new_name)) - ->condition('rid', $rid) - ->execute(); - } + db_query('UPDATE {users_roles} SET role_name = rid WHERE rid NOT IN (1, 2)'); + db_update('users_roles') + ->fields(array('role_name' => DRUPAL_ANONYMOUS_ROLE)) + ->condition('rid', 1) + ->execute(); + db_update('users_roles') + ->fields(array('role_name' => DRUPAL_AUTHENTICATED_ROLE)) + ->condition('rid', 2) + ->execute(); } } diff --git a/core/modules/block/block.install b/core/modules/block/block.install index ab9bcef..b1e7fcb 100644 --- a/core/modules/block/block.install +++ b/core/modules/block/block.install @@ -280,18 +280,15 @@ function block_update_8002() { ); db_add_field('block_role', 'role_name', $column); - // Load the mapping of rids to role_names. - $rid_mapping = db_select('role', 'r') - ->fields('r', array('rid', 'name')) - ->execute() - ->fetchAllKeyed(0, 1); - foreach ($rid_mapping as $rid => $role_name) { - // Fill in role_name data in the block_role table. - db_update('block_role') - ->fields(array('role_name' => $role_name)) - ->condition('rid', $rid) - ->execute(); - } + db_query('UPDATE {block_role} SET role_name = rid WHERE rid NOT IN (1, 2)'); + db_update('block_role') + ->fields(array('role_name' => DRUPAL_ANONYMOUS_ROLE)) + ->condition('rid', 1) + ->execute(); + db_update('block_role') + ->fields(array('role_name' => DRUPAL_AUTHENTICATED_ROLE)) + ->condition('rid', 2) + ->execute(); // The role name field must not be NULL. $column['not null'] = TRUE; diff --git a/core/modules/system/tests/upgrade/upgrade.roles.test b/core/modules/system/tests/upgrade/upgrade.roles.test index d42d77e..c1fcef5 100644 --- a/core/modules/system/tests/upgrade/upgrade.roles.test +++ b/core/modules/system/tests/upgrade/upgrade.roles.test @@ -34,18 +34,23 @@ class RoleUpgradePathTestCase extends UpgradePathTestCase { public function testRoleUpgrade() { $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); - // Check that "gärtner" has been converted to "g_rtner" and that the role + // Check that "gärtner" has been converted to "4" and that the role // edit page for it exists. - $this->drupalGet('admin/people/permissions/roles/edit/g_rtner'); - $this->assertResponse(200, 'Role edit page for "g_rtner" was found.'); + $this->drupalGet('admin/people/permissions/roles/edit/4'); + $this->assertResponse(200, 'Role edit page for "gärtner" was found.'); // Check that the permission for "gärtner" still exists. - $this->drupalGet('admin/people/permissions/g_rtner'); - $this->assertFieldChecked('edit-g-rtner-edit-own-comments', 'Edit own comments permission for "g_rtner" is set correctly.'); + $this->drupalGet('admin/people/permissions/4'); + $this->assertFieldChecked('edit-4-edit-own-comments', 'Edit own comments permission for "gärtner" is set correctly.'); // Check that the role visibility setting for the who's online block still // exists. $this->drupalGet('admin/structure/block/manage/user/online/configure'); - $this->assertFieldChecked('edit-roles-very-long-role-name-that-has-exactly-sixty-four-characters-in-it', "Who's online block visibility setting is correctly set for the long role name."); + $this->assertFieldChecked('edit-roles-5', "Who's online block visibility setting is correctly set for the long role name."); + // Check that the role name to label conversion was successful and that the + // labels are displayed. + $this->assertText('gärtner', 'Role label is displayed on block visibility settings.'); + $this->assertText('very long role name that has exactly sixty-four characters in it', 'Role label is displayed on block visibility settings.'); + $this->assertText('very_long role name that has exactly sixty-four characters in it', 'Role label is displayed on block visibility settings.'); } } diff --git a/core/modules/user/user.install b/core/modules/user/user.install index 53c7321..b8e040c 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -400,9 +400,8 @@ function user_update_8002() { db_drop_primary_key('users_roles'); db_add_primary_key('users_roles', array('uid', 'role_name')); - // We cannot just drop the rid column in the role table because other modules - // might need the information for their upgrades (example: block module). So - // we just remove the auto increment and the primary key setting. + // Remove the auto increment and the primary key setting so that it is + // possible to change the primary key of the role table. $column = array( 'type' => 'int', 'unsigned' => TRUE, @@ -413,6 +412,7 @@ function user_update_8002() { db_drop_primary_key('role'); db_add_primary_key('role', array('name')); + db_drop_field('role', 'rid'); db_drop_field('users_roles', 'rid'); db_drop_field('role_permission', 'rid'); }