diff --git a/core/modules/field/src/Plugin/migrate/source/d6/FieldInstance.php b/core/modules/field/src/Plugin/migrate/source/d7/FieldInstance.php old mode 100644 new mode 100755 index bddfc27..42c6289 --- a/core/modules/field/src/Plugin/migrate/source/d6/FieldInstance.php +++ b/core/modules/field/src/Plugin/migrate/source/d7/FieldInstance.php @@ -2,20 +2,19 @@ /** * @file - * Contains \Drupal\field\Plugin\migrate\source\d6\FieldInstance. + * Contains \Drupal\field\Plugin\migrate\source\d7\FieldInstance. */ -namespace Drupal\field\Plugin\migrate\source\d6; +namespace Drupal\field\Plugin\migrate\source\d7; use Drupal\migrate\Row; use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase; /** - * Drupal 6 field instances source from database. + * Drupal 7 field instances source from database. * * @MigrateSource( - * id = "d6_field_instance", - * source_provider = "content" + * id = "d7_field_instance", * ) */ class FieldInstance extends DrupalSqlBase { @@ -24,12 +23,22 @@ class FieldInstance extends DrupalSqlBase { * {@inheritdoc} */ public function query() { - $query = $this->select('content_node_field_instance', 'cnfi')->fields('cnfi'); - if (isset($this->configuration['node_type'])) { - $query->condition('cnfi.type_name', $this->configuration['node_type']); + $query = $this->select('field_config_instance', 'fci') + ->fields('fci') + ->condition('fci.deleted', 0) + ->condition('fc.active', 1) + ->condition('fc.deleted', 0) + ->condition('fc.storage_active', 1); + $query->innerJoin('field_config', 'fc', 'fci.field_id = fc.id'); + + // Optionally filter by entity type and bundle. + if (isset($this->configuration['entity_type'])) { + $query->condition('entity_type', $this->configuration['entity_type']); + + if (isset($this->configuration['bundle'])) { + $query->condition('bundle', $this->configuration['bundle']); + } } - $query->join('content_node_field', 'cnf', 'cnf.field_name = cnfi.field_name'); - $query->fields('cnf'); return $query; } @@ -40,16 +49,12 @@ public function query() { public function fields() { return array( 'field_name' => $this->t('The machine name of field.'), - 'type_name' => $this->t('Content type where is used this field.'), - 'weight' => $this->t('Weight.'), - 'label' => $this->t('A name to show.'), - 'widget_type' => $this->t('Widget type.'), - 'widget_settings' => $this->t('Serialize data with widget settings.'), - 'display_settings' => $this->t('Serialize data with display settings.'), - 'description' => $this->t('A description of field.'), - 'widget_module' => $this->t('Module that implements widget.'), - 'widget_active' => $this->t('Status of widget'), - 'module' => $this->t('The module that provides the field.'), + 'entity_type' => $this->t('The entity type.'), + 'bundle' => $this->t('The entity bundle.'), + 'default_value' => $this->t('Default value'), + 'instance_settings' => $this->t('Field instance settings.'), + 'widget_settings' => $this->t('Widget settings.'), + 'display_settings' => $this->t('Display settings.'), ); } @@ -57,13 +62,24 @@ public function fields() { * {@inheritdoc} */ public function prepareRow(Row $row) { - // Unserialize data. - $widget_settings = unserialize($row->getSourceProperty('widget_settings')); - $display_settings = unserialize($row->getSourceProperty('display_settings')); - $global_settings = unserialize($row->getSourceProperty('global_settings')); - $row->setSourceProperty('widget_settings', $widget_settings); - $row->setSourceProperty('display_settings', $display_settings); - $row->setSourceProperty('global_settings', $global_settings); + $data = unserialize($row->getSourceProperty('data')); + + $row->setSourceProperty('label', $data['label']); + $row->setSourceProperty('description', $data['description']); + $row->setSourceProperty('required', $data['required']); + + $default_value = !empty($data['default_value']) ? $data['default_value'] : array(); + if ($data['widget']['type'] == 'email_textfield' && $default_value) { + $default_value[0]['value'] = $default_value[0]['email']; + unset($default_value[0]['email']); + } + $row->setSourceProperty('default_value', $default_value); + + // Settings. + $row->setSourceProperty('instance_settings', $data['settings']); + $row->setSourceProperty('widget_settings', $data['widget']); + $row->setSourceProperty('display_settings', $data['display']); + return parent::prepareRow($row); } @@ -71,16 +87,19 @@ public function prepareRow(Row $row) { * {@inheritdoc} */ public function getIds() { - $ids = array( - 'field_name' => array( + return array( + 'entity_type' => array( + 'type' => 'string', + 'alias' => 'fci', + ), + 'bundle' => array( 'type' => 'string', - 'alias' => 'cnfi', + 'alias' => 'fci', ), - 'type_name' => array( + 'field_name' => array( 'type' => 'string', + 'alias' => 'fci', ), ); - return $ids; } - } diff --git a/core/modules/user/migration_templates/d6_user.yml b/core/modules/user/migration_templates/d7_user.yml old mode 100644 new mode 100755 index 05ff4a0..07df192 --- a/core/modules/user/migration_templates/d6_user.yml +++ b/core/modules/user/migration_templates/d7_user.yml @@ -1,9 +1,11 @@ -id: d6_user -label: Drupal 6 user accounts +id: d7_user +label: Drupal 7 user accounts migration_tags: - - Drupal 6 + - Drupal 7 +builder: + plugin: d7_user source: - plugin: d6_user + plugin: d7_user process: uid: uid name: name @@ -13,25 +15,17 @@ process: access: access login: login status: status - timezone: - plugin: user_update_7002 - source: timezone + timezone: timezone + langcode: language preferred_langcode: language + preferred_admin_langcode: language init: init roles: plugin: migration - migration: d6_user_role + migration: d7_user_role source: roles - user_picture: - plugin: d6_user_picture - source: uid destination: plugin: entity:user - md5_passwords: true migration_dependencies: required: - - d6_user_role - optional: - - d6_user_picture_file - - d6_user_picture_entity_display - - d6_user_picture_entity_form_display + - d7_user_role diff --git a/core/modules/user/migration_templates/d6_user_mail.yml b/core/modules/user/migration_templates/d7_user_mail.yml index b06b6ea..0c5e74c 100644 --- a/core/modules/user/migration_templates/d6_user_mail.yml +++ b/core/modules/user/migration_templates/d7_user_mail.yml @@ -1,7 +1,7 @@ -id: d6_user_mail +id: d7_user_mail label: User mail configuration migration_tags: - - Drupal 6 + - Drupal 7 source: plugin: variable variables: @@ -9,8 +9,8 @@ source: - user_mail_status_activated_body - user_mail_password_reset_subject - user_mail_password_reset_body - - user_mail_status_deleted_subject - - user_mail_status_deleted_body + - user_mail_status_canceled_subject + - user_mail_status_canceled_body - user_mail_register_admin_created_subject - user_mail_register_admin_created_body - user_mail_register_no_approval_required_subject @@ -20,48 +20,20 @@ source: - user_mail_status_blocked_subject - user_mail_status_blocked_body process: - 'status_activated/subject': - plugin: convert_tokens - source: user_mail_status_activated_subject - 'status_activated/body': - plugin: convert_tokens - source: user_mail_status_activated_body - 'password_reset/subject': - plugin: convert_tokens - source: user_mail_password_reset_subject - 'password_reset/body': - plugin: convert_tokens - source: user_mail_password_reset_body - 'cancel_confirm/subject': - plugin: convert_tokens - source: user_mail_status_deleted_subject - 'cancel_confirm/body': - plugin: convert_tokens - source: user_mail_status_deleted_body - 'register_admin_created/subject': - plugin: convert_tokens - source: user_mail_register_admin_created_subject - 'register_admin_created/body': - plugin: convert_tokens - source: user_mail_register_admin_created_body - 'register_no_approval_required/subject': - plugin: convert_tokens - source: user_mail_register_no_approval_required_subject - 'register_no_approval_required/body': - plugin: convert_tokens - source: user_mail_register_no_approval_required_body - 'register_pending_approval/subject': - plugin: convert_tokens - source: user_mail_register_pending_approval_subject - 'register_pending_approval/body': - plugin: convert_tokens - source: user_mail_register_pending_approval_body - 'status_blocked/subject': - plugin: convert_tokens - source: user_mail_status_blocked_subject - 'status_blocked/body': - plugin: convert_tokens - source: user_mail_status_blocked_body + 'status_activated/subject': user_mail_status_activated_subject + 'status_activated/body': user_mail_status_activated_body + 'password_reset/subject': user_mail_password_reset_subject + 'password_reset/body': user_mail_password_reset_body + 'cancel_confirm/subject': user_mail_status_canceled_subject + 'cancel_confirm/body': user_mail_status_canceled_body + 'register_admin_created/subject': user_mail_register_admin_created_subject + 'register_admin_created/body': user_mail_register_admin_created_body + 'register_no_approval_required/subject': user_mail_register_no_approval_required_subject + 'register_no_approval_required/body': user_mail_register_no_approval_required_body + 'register_pending_approval/subject': user_mail_register_pending_approval_subject + 'register_pending_approval/body': user_mail_register_pending_approval_body + 'status_blocked/subject': user_mail_status_blocked_subject + 'status_blocked/body': user_mail_status_blocked_body destination: plugin: config config_name: user.mail diff --git a/core/modules/user/migration_templates/d6_user_role.yml b/core/modules/user/migration_templates/d7_user_role.yml old mode 100644 new mode 100755 index e15058b..6688083 --- a/core/modules/user/migration_templates/d6_user_role.yml +++ b/core/modules/user/migration_templates/d7_user_role.yml @@ -1,9 +1,9 @@ -id: d6_user_role -label: Drupal 6 user roles +id: d7_user_role +label: Drupal 7 user roles migration_tags: - - Drupal 6 + - Drupal 7 source: - plugin: d6_user_role + plugin: d7_user_role process: id: - @@ -35,12 +35,10 @@ process: 'delete own forum topics' : 'delete own forum content' 'edit any forum topic' : 'edit any forum content' 'edit own forum topics' : 'edit own forum content' - - plugin: system_update_7000 - - plugin: node_update_7008 - plugin: flatten - - plugin: filter_format_permission + weight: weight destination: plugin: entity:user_role migration_dependencies: - required: - - d6_filter_format + optional: + - d7_filter_format diff --git a/core/modules/user/src/Plugin/migrate/source/d6/Role.php b/core/modules/user/src/Plugin/migrate/source/d7/Role.php old mode 100644 new mode 100755 index cc6bcaa..4f84290 --- a/core/modules/user/src/Plugin/migrate/source/d6/Role.php +++ b/core/modules/user/src/Plugin/migrate/source/d7/Role.php @@ -2,38 +2,28 @@ /** * @file - * Contains \Drupal\user\Plugin\migrate\source\d6\Role. + * Contains \Drupal\user\Plugin\migrate\source\d7\Role. */ -namespace Drupal\user\Plugin\migrate\source\d6; +namespace Drupal\user\Plugin\migrate\source\d7; use Drupal\migrate\Row; use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase; /** - * Drupal 6 role source from database. + * Drupal 7 role source from database. * * @MigrateSource( - * id = "d6_user_role" + * id = "d7_user_role" * ) */ class Role extends DrupalSqlBase { /** - * List of filter IDs per role IDs. - * - * @var array - */ - protected $filterPermissions = array(); - - /** * {@inheritdoc} */ public function query() { - $query = $this->select('role', 'r') - ->fields('r', array('rid', 'name')) - ->orderBy('rid'); - return $query; + return $this->select('role', 'r')->fields('r'); } /** @@ -43,41 +33,21 @@ public function fields() { return array( 'rid' => $this->t('Role ID.'), 'name' => $this->t('The name of the user role.'), + 'weight' => $this->t('The weight of the role.'), ); } /** * {@inheritdoc} */ - protected function initializeIterator() { - $filter_roles = $this->select('filter_formats', 'f') - ->fields('f', array('format', 'roles')) - ->execute() - ->fetchAllKeyed(); - foreach ($filter_roles as $format => $roles) { - // Drupal 6 code: $roles = ','. implode(',', $roles) .','; - // Remove the beginning and ending comma. - foreach (explode(',', trim($roles, ',')) as $rid) { - $this->filterPermissions[$rid][] = $format; - } - } - return parent::initializeIterator(); - } - - /** - * {@inheritdoc} - */ public function prepareRow(Row $row) { - $rid = $row->getSourceProperty('rid'); - $permissions = $this->select('permission', 'p') - ->fields('p', array('perm')) - ->condition('rid', $rid) + $permissions = $this->select('role_permission', 'rp') + ->fields('rp', ['permission']) + ->condition('rid', $row->getSourceProperty('rid')) ->execute() - ->fetchField(); - $row->setSourceProperty('permissions', explode(', ', $permissions)); - if (isset($this->filterPermissions[$rid])) { - $row->setSourceProperty("filter_permissions:$rid", $this->filterPermissions[$rid]); - } + ->fetchCol(); + $row->setSourceProperty('permissions', $permissions); + return parent::prepareRow($row); } diff --git a/core/modules/user/src/Plugin/migrate/source/d6/User.php b/core/modules/user/src/Plugin/migrate/source/d7/User.php old mode 100644 new mode 100755 index c347149..e3b2c3a --- a/core/modules/user/src/Plugin/migrate/source/d6/User.php +++ b/core/modules/user/src/Plugin/migrate/source/d7/User.php @@ -2,30 +2,29 @@ /** * @file - * Contains \Drupal\user\Plugin\migrate\source\d6\User. + * Contains \Drupal\user\Plugin\migrate\source\d7\User. */ -namespace Drupal\user\Plugin\migrate\source\d6; +namespace Drupal\user\Plugin\migrate\source\d7; -use Drupal\migrate\Plugin\SourceEntityInterface; use Drupal\migrate\Row; -use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase; +use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity; /** - * Drupal 6 user source from database. + * Drupal 7 user source from database. * * @MigrateSource( - * id = "d6_user" + * id = "d7_user" * ) */ -class User extends DrupalSqlBase implements SourceEntityInterface { +class User extends FieldableEntity { /** * {@inheritdoc} */ public function query() { return $this->select('users', 'u') - ->fields('u', array_keys($this->baseFields())) + ->fields('u') ->condition('uid', 1, '>'); } @@ -33,10 +32,24 @@ public function query() { * {@inheritdoc} */ public function fields() { - $fields = $this->baseFields(); - - // Add roles field. - $fields['roles'] = $this->t('Roles'); + $fields = array( + 'uid' => $this->t('User ID'), + 'name' => $this->t('Username'), + 'pass' => $this->t('Password'), + 'mail' => $this->t('Email address'), + 'signature' => $this->t('Signature'), + 'signature_format' => $this->t('Signature format'), + 'created' => $this->t('Registered timestamp'), + 'access' => $this->t('Last access timestamp'), + 'login' => $this->t('Last login timestamp'), + 'status' => $this->t('Status'), + 'timezone' => $this->t('Timezone'), + 'language' => $this->t('Language'), + 'picture' => $this->t('Picture'), + 'init' => $this->t('Init'), + 'data' => $this->t('User data'), + 'roles' => $this->t('Roles'), + ); // Profile fields. if ($this->moduleExists('profile')) { @@ -53,32 +66,19 @@ public function fields() { * {@inheritdoc} */ public function prepareRow(Row $row) { - // User roles. - $roles = $this->select('users_roles', 'ur') - ->fields('ur', array('rid')) - ->condition('ur.uid', $row->getSourceProperty('uid')) - ->execute() - ->fetchCol(); - $row->setSourceProperty('roles', $roles); - - // We are adding here the Event contributed module column. - // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7 - if ($row->hasSourceProperty('timezone_id') && $row->getSourceProperty('timezone_id')) { - if ($this->getDatabase()->schema()->tableExists('event_timezones')) { - $event_timezone = $this->select('event_timezones', 'e') - ->fields('e', array('name')) - ->condition('e.timezone', $row->getSourceProperty('timezone_id')) - ->execute() - ->fetchField(); - if ($event_timezone) { - $row->setSourceProperty('event_timezone', $event_timezone); - } - } - } + $roles = $this->select('role', 'r') + ->fields('r', ['rid', 'name']); + $roles->join('users_roles', 'ur', 'ur.rid = r.rid'); + $roles->condition('ur.uid', $row->getSourceProperty('uid')); + $row->setSourceProperty('roles', $roles->execute()->fetchAllKeyed()); - // Unserialize Data. $row->setSourceProperty('data', unserialize($row->getSourceProperty('data'))); + // Get Field API field values. + foreach (array_keys($this->getFields('user')) as $field) { + $row->setSourceProperty($field, $this->getFieldValues('user', $field, $row->getSourceProperty('uid'))); + } + return parent::prepareRow($row); } @@ -94,58 +94,4 @@ public function getIds() { ); } - /** - * Returns the user base fields to be migrated. - * - * @return array - * Associative array having field name as key and description as value. - */ - protected function baseFields() { - $fields = array( - 'uid' => $this->t('User ID'), - 'name' => $this->t('Username'), - 'pass' => $this->t('Password'), - 'mail' => $this->t('Email address'), - 'signature' => $this->t('Signature'), - 'signature_format' => $this->t('Signature format'), - 'created' => $this->t('Registered timestamp'), - 'access' => $this->t('Last access timestamp'), - 'login' => $this->t('Last login timestamp'), - 'status' => $this->t('Status'), - 'timezone' => $this->t('Timezone'), - 'language' => $this->t('Language'), - 'picture' => $this->t('Picture'), - 'init' => $this->t('Init'), - 'data' => $this->t('User data'), - ); - - // Possible field added by Date contributed module. - // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7 - if ($this->getDatabase()->schema()->fieldExists('users', 'timezone_name')) { - $fields['timezone_name'] = $this->t('Timezone (Date)'); - } - - // Possible field added by Event contributed module. - // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7 - if ($this->getDatabase()->schema()->fieldExists('users', 'timezone_id')) { - $fields['timezone_id'] = $this->t('Timezone (Event)'); - } - - return $fields; - } - - /** - * {@inheritdoc} - */ - public function bundleMigrationRequired() { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function entityTypeId() { - return 'user'; - } - } diff --git a/core/modules/user/src/Tests/Migrate/d6/MigrateUserRoleTest.php b/core/modules/user/src/Tests/Migrate/d7/MigrateUserRoleTest.php index 4b86da2..51f8f97 100644 --- a/core/modules/user/src/Tests/Migrate/d6/MigrateUserRoleTest.php +++ b/core/modules/user/src/Tests/Migrate/d7/MigrateUserRoleTest.php @@ -2,90 +2,72 @@ /** * @file - * Contains \Drupal\user\Tests\Migrate\d6\MigrateUserRoleTest. + * Contains \Drupal\user\Tests\Migrate\d7\MigrateUserRoleTest. */ -namespace Drupal\user\Tests\Migrate\d6; +namespace Drupal\user\Tests\Migrate\d7; +use Drupal\Core\Database\Database; +use Drupal\migrate_drupal\Tests\d7\MigrateDrupal7TestBase; use Drupal\user\Entity\Role; -use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase; +use Drupal\user\RoleInterface; /** * Upgrade user roles to user.role.*.yml. * - * @group migrate_drupal_6 + * @group user */ -class MigrateUserRoleTest extends MigrateDrupal6TestBase { +class MigrateUserRoleTest extends MigrateDrupal7TestBase { /** * The modules to be enabled during the test. * * @var array */ - static $modules = array('filter', 'node'); + static $modules = array('user'); /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - // We need some sample data so we can use the Migration process plugin. - $id_mappings = array( - 'd6_filter_format' => array( - array(array(1), array('filtered_html')), - array(array(2), array('full_html')) - ), - ); - $this->prepareMigrations($id_mappings); - $this->executeMigration('d6_user_role'); + $this->executeMigration('d7_user_role'); + } + + /** + * Asserts aspects of a user role config entity. + * + * @param string $id + * The role ID. + * @param string $label + * The role's expected label. + * @param int|NULL $original_rid + * The original (integer) ID of the role, to check permissions. + */ + protected function assertEntity($id, $label, $original_rid) { + /** @var \Drupal\user\RoleInterface $entity */ + $entity = Role::load($id); + $this->assertTrue($entity instanceof RoleInterface); + $this->assertIdentical($label, $entity->label()); + + if (isset($original_rid)) { + $permissions = Database::getConnection('default', 'migrate') + ->select('role_permission', 'rp') + ->fields('rp', ['permission']) + ->condition('rid', $original_rid) + ->execute() + ->fetchCol(); + $this->assertIdentical($permissions, $entity->getPermissions()); + } } /** * Tests user role migration. */ public function testUserRole() { - /** @var \Drupal\migrate\entity\Migration $migration */ - $migration = entity_load('migration', 'd6_user_role'); - $rid = 'anonymous'; - $anonymous = Role::load($rid); - $this->assertIdentical($rid, $anonymous->id()); - $this->assertIdentical(array('migrate test anonymous permission', 'use text format filtered_html'), $anonymous->getPermissions()); - $this->assertIdentical(array($rid), $migration->getIdMap()->lookupDestinationId(array(1))); - $rid = 'authenticated'; - $authenticated = Role::load($rid); - $this->assertIdentical($rid, $authenticated->id()); - $this->assertIdentical(array('migrate test authenticated permission', 'use text format filtered_html'), $authenticated->getPermissions()); - $this->assertIdentical(array($rid), $migration->getIdMap()->lookupDestinationId(array(2))); - $rid = 'migrate_test_role_1'; - $migrate_test_role_1 = Role::load($rid); - $this->assertIdentical($rid, $migrate_test_role_1->id()); - $this->assertIdentical(array(0 => 'migrate test role 1 test permission', 'use text format full_html'), $migrate_test_role_1->getPermissions()); - $this->assertIdentical(array($rid), $migration->getIdMap()->lookupDestinationId(array(3))); - $rid = 'migrate_test_role_2'; - $migrate_test_role_2 = Role::load($rid); - $this->assertIdentical(array( - 'migrate test role 2 test permission', - 'use PHP for settings', - 'administer contact forms', - 'skip comment approval', - 'edit own blog content', - 'edit any blog content', - 'delete own blog content', - 'delete any blog content', - 'create forum content', - 'delete any forum content', - 'delete own forum content', - 'edit any forum content', - 'edit own forum content', - 'administer nodes', - 'access content overview', - ), $migrate_test_role_2->getPermissions()); - $this->assertIdentical($rid, $migrate_test_role_2->id()); - $this->assertIdentical(array($rid), $migration->getIdMap()->lookupDestinationId(array(4))); - $rid = 'migrate_test_role_3_that_is_long'; - $migrate_test_role_3 = Role::load($rid); - $this->assertIdentical($rid, $migrate_test_role_3->id()); - $this->assertIdentical(array($rid), $migration->getIdMap()->lookupDestinationId(array(5))); + $this->assertEntity('anonymous', 'anonymous user', 1); + $this->assertEntity('authenticated', 'authenticated user', 2); + $this->assertEntity('administrator', 'administrator', 3); } } diff --git a/core/modules/user/src/Tests/Migrate/d6/MigrateUserTest.php b/core/modules/user/src/Tests/Migrate/d7/MigrateUserTest.php index 9980ba0..3b70a08 100644 --- a/core/modules/user/src/Tests/Migrate/d6/MigrateUserTest.php +++ b/core/modules/user/src/Tests/Migrate/d7/MigrateUserTest.php @@ -2,171 +2,85 @@ /** * @file - * Contains \Drupal\user\Tests\Migrate\d6\MigrateUserTest. + * Contains \Drupal\user\Tests\Migrate\d7\MigrateUserTest. */ -namespace Drupal\user\Tests\Migrate\d6; +namespace Drupal\user\Tests\Migrate\d7; +use Drupal\migrate_drupal\Tests\d7\MigrateDrupal7TestBase; use Drupal\user\Entity\User; -use Drupal\file\Entity\File; -use Drupal\Core\Database\Database; use Drupal\user\RoleInterface; -use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase; +use Drupal\user\UserInterface; /** * Users migration. * - * @group migrate_drupal_6 + * @group user */ -class MigrateUserTest extends MigrateDrupal6TestBase { +class MigrateUserTest extends MigrateDrupal7TestBase { /** * The modules to be enabled during the test. * * @var array */ - static $modules = array( - 'link', - 'options', - 'datetime', - 'text', - 'file', - 'image', - ); + static $modules = array('user'); /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); + $this->executeMigration('d7_user_role'); + $this->executeMigration('d7_user'); + } - $this->installEntitySchema('file'); - $this->installSchema('file', ['file_usage']); - - // Create the user profile field and instance. - entity_create('field_storage_config', array( - 'entity_type' => 'user', - 'field_name' => 'user_picture', - 'type' => 'image', - 'translatable' => '0', - ))->save(); - entity_create('field_config', array( - 'label' => 'User Picture', - 'description' => '', - 'field_name' => 'user_picture', - 'entity_type' => 'user', - 'bundle' => 'user', - 'required' => 0, - ))->save(); - - $file = entity_create('file', array( - 'fid' => 2, - 'uid' => 2, - 'filename' => 'image-test.jpg', - 'uri' => "public://image-test.jpg", - 'filemime' => 'image/jpeg', - 'created' => 1, - 'changed' => 1, - 'status' => FILE_STATUS_PERMANENT, - )); - $file->enforceIsNew(); - file_put_contents($file->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); - $file->save(); - - $file = entity_create('file', array( - 'fid' => 8, - 'uid' => 8, - 'filename' => 'image-test.png', - 'uri' => "public://image-test.png", - 'filemime' => 'image/png', - 'created' => 1, - 'changed' => 1, - 'status' => FILE_STATUS_PERMANENT, - )); - $file->enforceIsNew(); - file_put_contents($file->getFileUri(), file_get_contents('core/modules/simpletest/files/image-2.jpg')); - $file->save(); - - $id_mappings = array( - 'd6_user_role' => array( - array(array(1), array('anonymous user')), - array(array(2), array('authenticated user')), - array(array(3), array('migrate test role 1')), - array(array(4), array('migrate test role 2')), - array(array(5), array('migrate test role 3')), - ), - 'user_picture_entity_display' => array( - array(array(1), array('user', 'user', 'default', 'user_picture')), - ), - 'user_picture_entity_form_display' => array( - array(array(1), array('user', 'user', 'default', 'user_picture')), - ), - 'd6_user_picture_file' => array( - array(array(2), array(2)), - array(array(8), array(8)), - ), - ); - - $this->prepareMigrations($id_mappings); - - $this->executeMigration('d6_user'); + /** + * Asserts various aspects of a user account. + * + * @param string $id + * The user ID. + * @param string $label + * The username. + * @param string $mail + * The user's e-mail address. + * @param int $access + * The last access time. + * @param int $login + * The last login time. + * @param bool $blocked + * Whether or not the account is blocked. + * @param string $langcode + * The user account's language code. + * @param string $init + * The user's initial e-mail address. + * @param string[] $roles + * Role IDs the user account is expected to have. + */ + protected function assertEntity($id, $label, $mail, $access, $login, $blocked, $langcode, $init, array $roles = [RoleInterface::AUTHENTICATED_ID]) { + /** @var \Drupal\user\UserInterface $user */ + $user = User::load($id); + $this->assertTrue($user instanceof UserInterface); + $this->assertIdentical($label, $user->label()); + $this->assertIdentical($mail, $user->getEmail()); + $this->assertIdentical($access, $user->getLastAccessedTime()); + $this->assertIdentical($login, $user->getLastLoginTime()); + $this->assertIdentical($blocked, $user->isBlocked()); + // $user->getPreferredLangcode() might fallback to default language if the + // user preferred language is not configured on the site. We just want to + // test if the value was imported correctly. + $this->assertIdentical($langcode, $user->langcode->value); + $this->assertIdentical($langcode, $user->preferred_langcode->value); + $this->assertIdentical($langcode, $user->preferred_admin_langcode->value); + $this->assertIdentical($init, $user->getInitialEmail()); + $this->assertIdentical($roles, $user->getRoles()); } /** - * Tests the Drupal6 user to Drupal 8 migration. + * Tests the Drupal 7 user to Drupal 8 migration. */ public function testUser() { - $users = Database::getConnection('default', 'migrate') - ->select('users', 'u') - ->fields('u') - ->execute() - ->fetchAll(); - - foreach ($users as $source) { - // Get roles directly from the source. - $rids = Database::getConnection('default', 'migrate') - ->select('users_roles', 'ur') - ->fields('ur', array('rid')) - ->condition('ur.uid', $source->uid) - ->execute() - ->fetchCol(); - $roles = array(RoleInterface::AUTHENTICATED_ID); - $migration_role = entity_load('migration', 'd6_user_role'); - foreach ($rids as $rid) { - $role = $migration_role->getIdMap()->lookupDestinationId(array($rid)); - $roles[] = reset($role); - } - - /** @var \Drupal\user\UserInterface $user */ - $user = User::load($source->uid); - $this->assertIdentical($source->uid, $user->id()); - $this->assertIdentical($source->name, $user->label()); - $this->assertIdentical($source->mail, $user->getEmail()); - $this->assertIdentical($source->created, $user->getCreatedTime()); - $this->assertIdentical($source->access, $user->getLastAccessedTime()); - $this->assertIdentical($source->login, $user->getLastLoginTime()); - $is_blocked = $source->status == 0; - $this->assertIdentical($is_blocked, $user->isBlocked()); - // $user->getPreferredLangcode() might fallback to default language if the - // user preferred language is not configured on the site. We just want to - // test if the value was imported correctly. - $this->assertIdentical($source->language, $user->preferred_langcode->value); - $expected_timezone_name = $source->timezone_name ?: $this->config('system.date')->get('timezone.default'); - $this->assertIdentical($expected_timezone_name, $user->getTimeZone()); - $this->assertIdentical($source->init, $user->getInitialEmail()); - $this->assertIdentical($roles, $user->getRoles()); - - // We have one empty picture in the data so don't try load that. - if (!empty($source->picture)) { - // Test the user picture. - $file = File::load($user->user_picture->target_id); - $this->assertIdentical(basename($source->picture), $file->getFilename()); - } - - // Use the API to check if the password has been salted and re-hashed to - // conform the Drupal >= 7. - $this->assertTrue(\Drupal::service('password')->check($source->pass_plain, $user->getPassword())); - } + $this->assertEntity(2, 'Odo', 'odo@local.host', '0', '0', FALSE, '', 'odo@local.host'); } } diff --git a/core/modules/user/tests/src/Unit/Migrate/d6/RoleTest.php b/core/modules/user/tests/src/Unit/Plugin/migrate/source/d7/RoleTest.php index cf0a4a5..3fcde05 100644 --- a/core/modules/user/tests/src/Unit/Migrate/d6/RoleTest.php +++ b/core/modules/user/tests/src/Unit/Plugin/migrate/source/d7/RoleTest.php @@ -2,34 +2,26 @@ /** * @file - * Contains \Drupal\Tests\user\Unit\Migrate\d6\RoleTest. + * Contains \Drupal\Tests\user\Unit\Plugin\migrate\source\d7\RoleTest. */ -namespace Drupal\Tests\user\Unit\Migrate\d6; +namespace Drupal\Tests\user\Unit\Plugin\migrate\source\d7; use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase; /** - * Tests D6 role source plugin. + * Tests D7 role source plugin. * * @group user */ class RoleTest extends MigrateSqlSourceTestCase { - // The plugin system is not working during unit testing so the source plugin - // class needs to be manually specified. - const PLUGIN_CLASS = 'Drupal\user\Plugin\migrate\source\d6\Role'; + const PLUGIN_CLASS = 'Drupal\user\Plugin\migrate\source\d7\Role'; - // The fake Migration configuration entity. protected $migrationConfiguration = array( - // The ID of the entity, can be any string. 'id' => 'test', - // Leave it empty for now. - 'idlist' => array(), - // This needs to be the identifier of the actual key: cid for comment, nid - // for node and so on. 'source' => array( - 'plugin' => 'd6_user_role', + 'plugin' => 'd7_user_role', ), ); @@ -71,17 +63,15 @@ class RoleTest extends MigrateSqlSourceTestCase { */ protected function setUp() { foreach ($this->expectedResults as $row) { - $this->databaseContents['permission'][] = array( - 'perm' => implode(', ', $row['permissions']), - 'rid' => $row['rid'], - ); + foreach ($row['permissions'] as $permission) { + $this->databaseContents['role_permission'][] = array( + 'permission' => $permission, + 'rid' => $row['rid'], + ); + } unset($row['permissions']); $this->databaseContents['role'][] = $row; } - $this->databaseContents['filter_formats'][] = array( - 'format' => 1, - 'roles' => '', - ); parent::setUp(); } diff --git a/core/modules/user/tests/src/Unit/Migrate/d6/UserTest.php b/core/modules/user/tests/src/Unit/Plugin/migrate/source/d7/UserTest.php index 78fad1b..c7e35e1 100644 --- a/core/modules/user/tests/src/Unit/Migrate/d6/UserTest.php +++ b/core/modules/user/tests/src/Unit/Plugin/migrate/source/d7/UserTest.php @@ -2,87 +2,117 @@ /** * @file - * Contains \Drupal\Tests\user\Unit\Migrate\d6\UserTest. + * Contains \Drupal\Tests\user\Unit\Plugin\migrate\source\d7\UserTest. */ -namespace Drupal\Tests\user\Unit\Migrate\d6; +namespace Drupal\Tests\user\Unit\Plugin\migrate\source\d7; use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase; /** - * Tests D6 user source plugin. + * Tests D7 user source plugin. * * @group user */ class UserTest extends MigrateSqlSourceTestCase { - const PLUGIN_CLASS = 'Drupal\user\Plugin\migrate\source\d6\User'; + const PLUGIN_CLASS = 'Drupal\user\Plugin\migrate\source\d7\User'; - protected $migrationConfiguration = array( + protected $migrationConfiguration = [ 'id' => 'test', - 'idlist' => array(), - 'source' => array( - 'plugin' => 'd6_user', - ), - ); + 'source' => [ + 'plugin' => 'd7_user', + ], + ]; - protected $expectedResults = array( - array( - 'uid' => 2, - 'name' => 'admin', - // @todo d6 hash? - 'pass' => '1234', - 'mail' => 'admin@example.com', - 'theme' => '', - 'signature' => '', - 'signature_format' => 0, - 'created' => 1279402616, - 'access' => 1322981278, - 'login' => 1322699994, - 'status' => 0, - 'timezone' => 'America/Lima', - 'language' => 'en', - // @todo Add the file when needed. - 'picture' => 'sites/default/files/pictures/picture-1.jpg', - 'init' => 'admin@example.com', - 'data' => NULL, - ), - array( - 'uid' => 4, - 'name' => 'alice', - // @todo d6 hash? - 'pass' => '1234', - 'mail' => 'alice@example.com', - 'theme' => '', + protected $expectedResults = [ + [ + 'uid' => '2', + 'name' => 'Odo', + 'pass' => '$S$DVpvPItXvnsmF3giVEe7Jy2lG.SCoEs8uKwpHsyPvdeNAaNZYxZ8', + 'mail' => 'odo@local.host', 'signature' => '', - 'signature_format' => 0, - 'created' => 1322981368, - 'access' => 1322982419, - 'login' => 132298140, - 'status' => 0, - 'timezone' => 'America/Lima', - 'language' => 'en', - 'picture' => '', - 'init' => 'alice@example.com', - 'data' => NULL, - ), - ); + 'signature_format' => 'filtered_html', + 'created' => '1432750741', + 'access' => '0', + 'login' => '0', + 'status' => '1', + 'timezone' => 'America/Chicago', + 'language' => '', + 'picture' => '0', + 'init' => 'odo@local.host', + 'roles' => [ + 2 => 'authenticated user', + ], + 'data' => [ + 'contact' => 1, + ], + 'field_file' => [ + [ + 'fid' => 99, + 'display' => 1, + 'description' => 'None', + ], + ], + ], + ]; /** * {@inheritdoc} */ protected function setUp() { - foreach ($this->expectedResults as $k => $row) { - $this->databaseContents['users'][$k] = $row; - } - // getDatabase() will not create empty tables, so we need to insert data - // even if it's irrelevant to the test. - $this->databaseContents['users_roles'] = array( - array( - 'uid' => 99, - 'rid' => 99, - ), - ); + $this->databaseContents['users'][] = [ + 'uid' => '2', + 'name' => 'Odo', + 'pass' => '$S$DVpvPItXvnsmF3giVEe7Jy2lG.SCoEs8uKwpHsyPvdeNAaNZYxZ8', + 'mail' => 'odo@local.host', + 'theme' => '', + 'signature' => '', + 'signature_format' => 'filtered_html', + 'created' => '1432750741', + 'access' => '0', + 'login' => '0', + 'status' => '1', + 'timezone' => 'America/Chicago', + 'language' => '', + 'picture' => '0', + 'init' => 'odo@local.host', + 'data' => 'a:1:{s:7:"contact";i:1;}', + ]; + $this->databaseContents['users_roles'][] = [ + 'uid' => 2, + 'rid' => 2, + ]; + $this->databaseContents['role'][] = [ + 'rid' => 2, + 'name' => 'authenticated user', + 'weight' => 0, + ]; + $this->databaseContents['field_config_instance'] = [ + [ + 'id' => '33', + 'field_id' => '11', + 'field_name' => 'field_file', + 'entity_type' => 'user', + 'bundle' => 'user', + 'data' => 'a:0:{}', + 'deleted' => '0', + ], + ]; + $this->databaseContents['field_data_field_file'] = [ + [ + 'entity_type' => 'user', + 'bundle' => 'user', + 'deleted' => 0, + 'entity_id' => 2, + 'revision_id' => NULL, + 'language' => 'und', + 'delta' => 0, + 'field_file_fid' => 99, + 'field_file_display' => 1, + 'field_file_description' => 'None', + ], + ]; parent::setUp(); }