function EntityReferenceFieldTest::assertUserAutocreate

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::assertUserAutocreate()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::assertUserAutocreate()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::assertUserAutocreate()

Asserts that the setter callback performs autocreation for users.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The referencing entity.

$setter_callback: A callback setting the target entity on the referencing entity.

Return value

bool TRUE if the user was autocreated, FALSE otherwise.

1 call to EntityReferenceFieldTest::assertUserAutocreate()
EntityReferenceFieldTest::testAutocreateApi in core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php
Tests all the possible ways to autocreate an entity via the API.

File

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

Class

EntityReferenceFieldTest
Tests for the entity reference field.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function assertUserAutocreate(EntityInterface $entity, $setter_callback) {
    $storage = $this->entityTypeManager
        ->getStorage('user');
    $user_id = $this->generateRandomEntityId();
    $user = $storage->create([
        'uid' => $user_id,
        'name' => $this->randomString(),
    ]);
    $setter_callback($entity, $user);
    $entity->save();
    $storage->resetCache();
    $user = User::load($user_id);
    return $this->assertEqual($entity->user_id->target_id, $user->id());
}

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