You are here

public function ProfileTest::testProfile in Profile 8

Tests the profile entity and its methods.

File

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

Class

ProfileTest
Tests basic functionality of profiles.

Namespace

Drupal\Tests\profile\Kernel

Code

public function testProfile() {
  $types_data = [
    'profile_type_0' => [
      'label' => $this
        ->randomMachineName(),
    ],
    'profile_type_1' => [
      'label' => $this
        ->randomMachineName(),
    ],
  ];

  /** @var \Drupal\profile\Entity\ProfileTypeInterface[] $types */
  $types = [];
  foreach ($types_data as $id => $values) {
    $types[$id] = ProfileType::create([
      'id' => $id,
    ] + $values);
    $types[$id]
      ->save();
  }

  // Create a new profile.

  /** @var \Drupal\profile\Entity\ProfileInterface $profile */
  $profile = $this->profileStorage
    ->create([
    'type' => $types['profile_type_0']
      ->id(),
  ]);
  $profile
    ->setOwnerId($this->user1
    ->id());
  $this
    ->assertEquals($this->user1
    ->id(), $profile
    ->getOwnerId());
  $profile
    ->setCreatedTime('1554159046');
  $this
    ->assertEquals('1554159046', $profile
    ->getCreatedTime());
  $profile
    ->setChangedTime('1554159090');
  $this
    ->assertEquals('1554159090', $profile
    ->getChangedTime());
  $this
    ->assertEquals('default', $profile
    ->getData('test', 'default'));
  $profile
    ->setData('test', 'value');
  $this
    ->assertEquals('value', $profile
    ->getData('test', 'default'));
  $profile
    ->unsetData('test');
  $this
    ->assertNull($profile
    ->getData('test'));
  $this
    ->assertEquals('default', $profile
    ->getData('test', 'default'));

  // Save the profile.
  $profile
    ->save();
  $expected_label = new TranslatableMarkup('@type #@id', [
    '@type' => $types['profile_type_0']
      ->label(),
    '@id' => $profile
      ->id(),
  ]);
  $this
    ->assertEquals($expected_label, $profile
    ->label());

  // List profiles for the user and verify that the new profile appears.
  $list = $this->profileStorage
    ->loadByProperties([
    'uid' => $this->user1
      ->id(),
  ]);
  $list_ids = array_keys($list);
  $this
    ->assertEquals($list_ids, [
    $profile
      ->id(),
  ]);

  // Create a second profile.
  $user1_profile1 = $profile;
  $user1_profile = $this->profileStorage
    ->create([
    'type' => $types['profile_type_0']
      ->id(),
    'uid' => $this->user1
      ->id(),
  ]);
  $user1_profile
    ->save();

  // List profiles for the user and verify that both profiles appear.
  $list = $this->profileStorage
    ->loadByProperties([
    'uid' => $this->user1
      ->id(),
  ]);
  $list_ids = array_keys($list);
  $this
    ->assertEquals($list_ids, [
    $user1_profile1
      ->id(),
    $user1_profile
      ->id(),
  ]);

  // Delete the second profile and verify that the first still exists.
  $user1_profile
    ->delete();
  $list = $this->profileStorage
    ->loadByProperties([
    'uid' => $this->user1
      ->id(),
  ]);
  $list_ids = array_keys($list);
  $this
    ->assertEquals($list_ids, [
    $user1_profile1
      ->id(),
  ]);

  // Create a profile for the second user.
  $user2_profile1 = $this->profileStorage
    ->create([
    'type' => $types['profile_type_0']
      ->id(),
    'uid' => $this->user2
      ->id(),
  ]);
  $user2_profile1
    ->save();

  // Delete the first user and verify that all of its profiles are deleted.
  $this->user1
    ->delete();
  $list = $this->profileStorage
    ->loadByProperties([
    'uid' => $this->user1
      ->id(),
  ]);
  $list_ids = array_keys($list);
  $this
    ->assertEquals($list_ids, []);

  // List profiles for the second user and verify that they still exist.
  $list = $this->profileStorage
    ->loadByProperties([
    'uid' => $this->user2
      ->id(),
  ]);
  $list_ids = array_keys($list);
  $this
    ->assertEquals($list_ids, [
    $user2_profile1
      ->id(),
  ]);
}