function EntityReferenceFieldTest::assertUserRoleAutocreate

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

Asserts that the setter callback performs autocreation for user roles.

@internal

Parameters

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

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

1 call to EntityReferenceFieldTest::assertUserRoleAutocreate()
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 361

Class

EntityReferenceFieldTest
Tests for the entity reference field.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function assertUserRoleAutocreate(EntityInterface $entity, callable $setter_callback) : void {
    $storage = $this->entityTypeManager
        ->getStorage('user_role');
    $role_id = $this->generateRandomEntityId(TRUE);
    $role = $storage->create([
        'id' => $role_id,
        'label' => $this->randomString(),
    ]);
    $setter_callback($entity, $role);
    $entity->save();
    $storage->resetCache();
    $role = Role::load($role_id);
    $this->assertEquals($entity->user_role->target_id, $role->id());
}

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