You are here

public function NameUserTest::testUserHooks in Name Field 8

Tests the user hooks.

File

tests/src/Kernel/NameUserTest.php, line 49

Class

NameUserTest
Tests the integration with user module.

Namespace

Drupal\Tests\name\Kernel

Code

public function testUserHooks() {
  FieldStorageConfig::create([
    'field_name' => 'field_text',
    'type' => 'string',
    'entity_type' => 'user',
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field_text',
    'type' => 'string',
    'entity_type' => 'user',
    'bundle' => 'user',
  ])
    ->save();
  $this
    ->assertIdentical('', \Drupal::config('name.settings')
    ->get('user_preferred'));
  FieldStorageConfig::create([
    'field_name' => 'field_name_test',
    'type' => 'name',
    'entity_type' => 'user',
  ])
    ->save();
  FieldStorageConfig::create([
    'field_name' => 'field_name_test2',
    'type' => 'name',
    'entity_type' => 'user',
  ])
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'field_name_test',
    'type' => 'name',
    'entity_type' => 'user',
    'bundle' => 'user',
  ]);
  $field
    ->save();
  $field2 = FieldConfig::create([
    'field_name' => 'field_name_test2',
    'type' => 'name',
    'entity_type' => 'user',
    'bundle' => 'user',
  ]);
  $field2
    ->save();
  $this
    ->assertEquals($field
    ->getName(), \Drupal::config('name.settings')
    ->get('user_preferred'));
  \Drupal::configFactory()
    ->getEditable('name.settings')
    ->set('user_preferred', $field2
    ->getName())
    ->save();
  $field2
    ->delete();
  $this
    ->assertEquals('', \Drupal::config('name.settings')
    ->get('user_preferred'));
  \Drupal::configFactory()
    ->getEditable('name.settings')
    ->set('user_preferred', $field
    ->getName())
    ->save();
  $account = User::create([
    'name' => 'test',
  ]);
  $account->field_name_test[0] = [
    'given' => 'Max',
    'family' => 'Mustermann',
  ];
  $account
    ->save();
  $account = User::load($account
    ->id());
  $this
    ->assertEquals('Max Mustermann', $account->realname);
  $this
    ->assertEquals('Max Mustermann', $account
    ->label());
  $this
    ->assertEquals('test', $account
    ->getAccountName());
  $this
    ->assertEquals('Max Mustermann', $account
    ->getDisplayName());
}