function MigrateUserTest::testUser

Same name in this branch
  1. 10 core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d6\MigrateUserTest::testUser()
Same name in other branches
  1. 9 core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d6\MigrateUserTest::testUser()
  2. 9 core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserTest::testUser()
  3. 8.9.x core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d6\MigrateUserTest::testUser()
  4. 8.9.x core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserTest::testUser()
  5. 11.x core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d6\MigrateUserTest::testUser()
  6. 11.x 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 146

Class

MigrateUserTest
Users migration.

Namespace

Drupal\Tests\user\Kernel\Migrate\d7

Code

public function testUser() : void {
    $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, (int) $source->created, (int) $source->access, (int) $source->login, (bool) $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());
    }
    // Tests the Drupal 7 user entity translations to Drupal 8 migration.
    $manager = $this->container
        ->get('content_translation.manager');
    // Get the user and its translations.
    $user = User::load(2);
    $user_fr = $user->getTranslation('fr');
    $user_is = $user->getTranslation('is');
    // Test that fields translated with Entity Translation are migrated.
    $this->assertSame('99', $user->field_integer->value);
    $this->assertSame('9', $user_fr->field_integer->value);
    $this->assertSame('1', $user_is->field_integer->value);
    // Test that the French translation metadata is correctly migrated.
    $metadata_fr = $manager->getTranslationMetadata($user_fr);
    $this->assertSame('en', $metadata_fr->getSource());
    $this->assertSame('1', $metadata_fr->getAuthor()->uid->value);
    $this->assertSame('1531663916', $metadata_fr->getCreatedTime());
    $this->assertFalse($metadata_fr->isOutdated());
    $this->assertFalse($metadata_fr->isPublished());
    // Test that the Icelandic translation metadata is correctly migrated.
    $metadata_is = $manager->getTranslationMetadata($user_is);
    $this->assertSame('en', $metadata_is->getSource());
    $this->assertSame('2', $metadata_is->getAuthor()->uid->value);
    $this->assertSame('1531663925', $metadata_is->getCreatedTime());
    $this->assertTrue($metadata_is->isOutdated());
    $this->assertTrue($metadata_is->isPublished());
    // Test that untranslatable properties are the same as the source language.
    $this->assertSame($user->label(), $user_fr->label());
    $this->assertSame($user->label(), $user_is->label());
    $this->assertSame($user->getEmail(), $user_fr->getEmail());
    $this->assertSame($user->getEmail(), $user_is->getEmail());
    $this->assertSame($user->getPassword(), $user_fr->getPassword());
    $this->assertSame($user->getPassword(), $user_is->getPassword());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.