function MigrateUserTest::assertEntity

Same name in other branches
  1. 9 core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserTest::assertEntity()
  2. 8.9.x core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserTest::assertEntity()
  3. 11.x core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserTest::assertEntity()

Asserts various aspects of a user account.

@internal

Parameters

string $id: The user ID.

string $label: The username.

string $mail: The user's email address.

string $password: The password for this user.

int $created: The user's creation time.

int $access: The last access time.

int $login: The last login time.

bool $blocked: Whether or not the account is blocked.

string $entity_langcode: The user entity language code.

string $prefered_langcode: The user prefered language code.

string $timezone: The user account's timezone name.

string $init: The user's initial email address.

string[] $roles: Role IDs the user account is expected to have.

array|null $field_integer: The value of the integer field.

int|false $field_file_target_id: (optional) The target ID of the file field.

bool $has_picture: (optional) Whether the user is expected to have a picture attached.

1 call to MigrateUserTest::assertEntity()
MigrateUserTest::testUser in core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php
Tests the Drupal 7 user to Drupal 8 migration.

File

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

Class

MigrateUserTest
Users migration.

Namespace

Drupal\Tests\user\Kernel\Migrate\d7

Code

protected function assertEntity(string $id, string $label, string $mail, string $password, int $created, int $access, int $login, bool $blocked, string $entity_langcode, string $prefered_langcode, string $timezone, string $init, array $roles, ?array $field_integer, $field_file_target_id = FALSE, bool $has_picture = FALSE) : void {
    
    /** @var \Drupal\user\UserInterface $user */
    $user = User::load($id);
    $this->assertInstanceOf(UserInterface::class, $user);
    $this->assertSame($label, $user->label());
    $this->assertSame($mail, $user->getEmail());
    $this->assertSame($password, $user->getPassword());
    $this->assertSame($created, (int) $user->getCreatedTime());
    $this->assertSame($access, (int) $user->getLastAccessedTime());
    $this->assertSame($login, (int) $user->getLastLoginTime());
    $this->assertNotSame($blocked, (bool) $user->isBlocked());
    // Ensure the user's langcode, preferred_langcode and
    // preferred_admin_langcode are valid.
    // $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.
    $language_manager = $this->container
        ->get('language_manager');
    $default_langcode = $language_manager->getDefaultLanguage()
        ->getId();
    if ($prefered_langcode == '') {
        $this->assertSame('en', $user->langcode->value);
        $this->assertSame($default_langcode, $user->preferred_langcode->value);
        $this->assertSame($default_langcode, $user->preferred_admin_langcode->value);
    }
    elseif ($language_manager->getLanguage($prefered_langcode) === NULL) {
        $this->assertSame($default_langcode, $user->langcode->value);
        $this->assertSame($default_langcode, $user->preferred_langcode->value);
        $this->assertSame($default_langcode, $user->preferred_admin_langcode->value);
    }
    else {
        $this->assertSame($entity_langcode, $user->langcode->value);
        $this->assertSame($prefered_langcode, $user->preferred_langcode->value);
        $this->assertSame($prefered_langcode, $user->preferred_admin_langcode->value);
    }
    $this->assertSame($timezone, $user->getTimeZone());
    $this->assertSame($init, $user->getInitialEmail());
    $this->assertSame($roles, $user->getRoles());
    $this->assertSame($has_picture, !$user->user_picture
        ->isEmpty());
    if (!is_null($field_integer)) {
        $this->assertTrue($user->hasField('field_integer'));
        $this->assertEquals($field_integer[0], $user->field_integer->value);
    }
    if (!empty($field_file_target_id)) {
        $this->assertTrue($user->hasField('field_file'));
        $this->assertSame($field_file_target_id, $user->field_file->target_id);
    }
}

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