You are here

public function MigrateUserTest::testUser in Drupal 8

Same name in this branch
  1. 8 core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d6\MigrateUserTest::testUser()
  2. 8 core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserTest::testUser()
Same name and namespace in other branches
  1. 9 core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserTest::testUser()

Tests the Drupal 7 user to Drupal 8 migration.

File

core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php, line 140

Class

MigrateUserTest
Users migration.

Namespace

Drupal\Tests\user\Kernel\Migrate\d7

Code

public function testUser() {
  $users = Database::getConnection('default', 'migrate')
    ->select('users', 'u')
    ->fields('u')
    ->condition('uid', 1, '>')
    ->execute()
    ->fetchAll();
  foreach ($users as $source) {
    $rids = Database::getConnection('default', 'migrate')
      ->select('users_roles', 'ur')
      ->fields('ur', [
      'rid',
    ])
      ->condition('ur.uid', $source->uid)
      ->execute()
      ->fetchCol();
    $roles = [
      RoleInterface::AUTHENTICATED_ID,
    ];
    $id_map = $this
      ->getMigration('d7_user_role')
      ->getIdMap();
    foreach ($rids as $rid) {
      $role = $id_map
        ->lookupDestinationIds([
        $rid,
      ])[0];
      $roles[] = reset($role);
    }
    $entity_translation = Database::getConnection('default', 'migrate')
      ->select('entity_translation', 'et')
      ->fields('et', [
      'language',
    ])
      ->condition('et.entity_type', 'user')
      ->condition('et.entity_id', $source->uid)
      ->condition('et.source', '')
      ->execute()
      ->fetchField();
    $entity_language = $entity_translation ?: $source->language;
    $field_integer = Database::getConnection('default', 'migrate')
      ->select('field_data_field_integer', 'fi')
      ->fields('fi', [
      'field_integer_value',
    ])
      ->condition('fi.entity_id', $source->uid)
      ->condition('fi.language', $entity_language)
      ->execute()
      ->fetchCol();
    $field_integer = !empty($field_integer) ? $field_integer : NULL;
    $field_file = Database::getConnection('default', 'migrate')
      ->select('field_data_field_file', 'ff')
      ->fields('ff', [
      'field_file_fid',
    ])
      ->condition('ff.entity_id', $source->uid)
      ->execute()
      ->fetchField();
    $this
      ->assertEntity($source->uid, $source->name, $source->mail, $source->pass, $source->created, $source->access, $source->login, $source->status, $entity_language, $source->language, $source->timezone, $source->init, $roles, $field_integer, $field_file);

    // Ensure that the user can authenticate.
    $this
      ->assertEquals($source->uid, $this->container
      ->get('user.auth')
      ->authenticate($source->name, 'a password'));

    // After authenticating the password will be rehashed because the password
    // stretching iteration count has changed from 15 in Drupal 7 to 16 in
    // Drupal 8.
    $user = User::load($source->uid);
    $rehash = $user
      ->getPassword();
    $this
      ->assertNotEquals($source->pass, $rehash);

    // Authenticate again and there should be no re-hash.
    $this
      ->assertEquals($source->uid, $this->container
      ->get('user.auth')
      ->authenticate($source->name, 'a password'));
    $user = User::load($source->uid);
    $this
      ->assertEquals($rehash, $user
      ->getPassword());
  }
}