function EntityReferenceFieldTest::testAutocreateApi

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::testAutocreateApi()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::testAutocreateApi()
  3. 10 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 247

Class

EntityReferenceFieldTest
Tests for the entity reference field.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testAutocreateApi() : void {
    $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) {
        // 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) {
        // 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->assertEquals($user->id(), $entity->user_id->target_id);
}

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