You are here

public function MigrateUserTest::testUser in Zircon Profile 8.0

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

Tests the Drupal6 user to Drupal 8 migration.

File

core/modules/user/src/Tests/Migrate/d6/MigrateUserTest.php, line 72
Contains \Drupal\user\Tests\Migrate\d6\MigrateUserTest.

Class

MigrateUserTest
Users migration.

Namespace

Drupal\user\Tests\Migrate\d6

Code

public function testUser() {
  $users = Database::getConnection('default', 'migrate')
    ->select('users', 'u')
    ->fields('u')
    ->condition('uid', 0, '>')
    ->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,
    );
    $id_map = Migration::load('d6_user_role')
      ->getIdMap();
    foreach ($rids as $rid) {
      $role = $id_map
        ->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 to Drupal >= 7 for non-admin users.
    if ($user
      ->id() != 1) {
      $this
        ->assertTrue(\Drupal::service('password')
        ->check($source->pass_plain, $user
        ->getPassword()));
    }
  }

  // Rollback the migration and make sure everything is deleted but uid 1.
  (new MigrateExecutable($this->migration, $this))
    ->rollback();
  $users = Database::getConnection('default', 'migrate')
    ->select('users', 'u')
    ->fields('u', [
    'uid',
  ])
    ->condition('uid', 0, '>')
    ->execute()
    ->fetchCol();
  foreach ($users as $uid) {
    $account = User::load($uid);
    if ($uid == 1) {
      $this
        ->assertNotNull($account, 'User 1 was preserved after rollback');
    }
    else {
      $this
        ->assertNull($account);
    }
  }
}