You are here

public function EntityReferenceFieldTest::testAutocreateApi in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::testAutocreateApi()

Tests all the possible ways to autocreate an entity via the API.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php, line 246

Class

EntityReferenceFieldTest
Tests for the entity reference field.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testAutocreateApi() {
  $entity = $this->entityTypeManager
    ->getStorage($this->entityType)
    ->create([
    'name' => $this
      ->randomString(),
  ]);

  // Test content entity autocreation.
  $this
    ->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
    $entity
      ->set('user_id', $user);
  });
  $this
    ->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
    $entity
      ->set('user_id', $user, FALSE);
  });
  $this
    ->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
    $entity->user_id
      ->setValue($user);
  });
  $this
    ->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
    $entity->user_id[0]
      ->get('entity')
      ->setValue($user);
  });
  $this
    ->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
    $entity->user_id
      ->setValue([
      'entity' => $user,
      'target_id' => NULL,
    ]);
  });
  try {
    $message = 'Setting both the entity and an invalid target_id property fails.';
    $this
      ->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
      $user
        ->save();
      $entity->user_id
        ->setValue([
        'entity' => $user,
        'target_id' => $this
          ->generateRandomEntityId(),
      ]);
    });
    $this
      ->fail($message);
  } catch (\InvalidArgumentException $e) {

    // Expected exception; just continue testing.
  }
  $this
    ->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
    $entity->user_id = $user;
  });
  $this
    ->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
    $entity->user_id->entity = $user;
  });

  // Test config entity autocreation.
  $this
    ->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
    $entity
      ->set('user_role', $role);
  });
  $this
    ->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
    $entity
      ->set('user_role', $role, FALSE);
  });
  $this
    ->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
    $entity->user_role
      ->setValue($role);
  });
  $this
    ->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
    $entity->user_role[0]
      ->get('entity')
      ->setValue($role);
  });
  $this
    ->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
    $entity->user_role
      ->setValue([
      'entity' => $role,
      'target_id' => NULL,
    ]);
  });
  try {
    $message = 'Setting both the entity and an invalid target_id property fails.';
    $this
      ->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
      $role
        ->save();
      $entity->user_role
        ->setValue([
        'entity' => $role,
        'target_id' => $this
          ->generateRandomEntityId(TRUE),
      ]);
    });
    $this
      ->fail($message);
  } catch (\InvalidArgumentException $e) {

    // Expected exception; just continue testing.
  }
  $this
    ->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
    $entity->user_role = $role;
  });
  $this
    ->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
    $entity->user_role->entity = $role;
  });

  // Test target entity saving after setting it as new.
  $storage = $this->entityTypeManager
    ->getStorage('user');
  $user_id = $this
    ->generateRandomEntityId();
  $user = $storage
    ->create([
    'uid' => $user_id,
    'name' => $this
      ->randomString(),
  ]);
  $entity->user_id = $user;
  $user
    ->save();
  $entity
    ->save();
  $this
    ->assertEqual($entity->user_id->target_id, $user
    ->id());
}