You are here

public function ProfileTest::testPopulate in Profile 8

Tests populating a profile using another profile's field values.

File

tests/src/Kernel/ProfileTest.php, line 205

Class

ProfileTest
Tests basic functionality of profiles.

Namespace

Drupal\Tests\profile\Kernel

Code

public function testPopulate() {
  $profile_type = ProfileType::create([
    'id' => 'customer',
    'label' => 'Customer',
  ]);
  $profile_type
    ->save();
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_fullname',
    'entity_type' => 'profile',
    'type' => 'text',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $profile_type
      ->id(),
    'label' => 'Full name',
  ]);
  $field
    ->save();
  $first_profile = Profile::create([
    'type' => 'customer',
    'uid' => 1,
    'field_fullname' => 'John Smith',
    'status' => FALSE,
  ]);
  $second_profile = Profile::create([
    'type' => 'customer',
    'uid' => 1,
    'field_fullname' => '',
    'status' => FALSE,
  ]);
  $third_profile = Profile::create([
    'type' => 'customer',
    'uid' => 2,
    'field_fullname' => 'Jane Smith',
    'status' => TRUE,
  ]);
  $third_profile
    ->populateFromProfile($second_profile, [
    'field_fullname',
  ]);

  // Confirm that the configurable field was transferred.
  $this
    ->assertEmpty($third_profile
    ->get('field_fullname')->value);

  // Confirm that the base fields were not changed.
  $this
    ->assertEquals(2, $third_profile
    ->getOwnerId());
  $this
    ->assertTrue($third_profile
    ->isPublished());
  $third_profile
    ->populateFromProfile($first_profile);

  // Confirm that the configurable field was transferred.
  $this
    ->assertEquals('John Smith', $third_profile
    ->get('field_fullname')->value);

  // Confirm that the base fields were not changed.
  $this
    ->assertEquals(2, $third_profile
    ->getOwnerId());
  $this
    ->assertTrue($third_profile
    ->isPublished());
}