You are here

protected function EntityValidationTest::createTestEntity in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::createTestEntity()

Creates a test entity.

Parameters

string $entity_type: An entity type.

Return value

\Drupal\Core\Entity\EntityInterface The created test entity.

3 calls to EntityValidationTest::createTestEntity()
EntityValidationTest::checkValidation in core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php
Executes the validation test set for a defined entity type.
EntityValidationTest::testCompositeConstraintValidation in core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php
Tests composite constraints.
EntityValidationTest::testEntityChangedConstraintOnConcurrentMultilingualEditing in core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php
Tests the EntityChangedConstraintValidator with multiple translations.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php, line 71

Class

EntityValidationTest
Tests the Entity Validation API.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function createTestEntity($entity_type) {
  $this->entityName = $this
    ->randomMachineName();
  $this->entityUser = $this
    ->createUser();

  // Pass in the value of the name field when creating. With the user
  // field we test setting a field after creation.
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->create();
  $entity->user_id->target_id = $this->entityUser
    ->id();
  $entity->name->value = $this->entityName;

  // Set a value for the test field.
  if ($entity
    ->hasField('field_test_text')) {
    $this->entityFieldText = $this
      ->randomMachineName();
    $entity->field_test_text->value = $this->entityFieldText;
  }
  return $entity;
}