You are here

public function FieldAttachStorageTest::testEntityCreateBundle in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testEntityCreateBundle()

Tests entity_bundle_create().

File

core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php, line 288

Class

FieldAttachStorageTest
Tests storage-related Field Attach API functions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testEntityCreateBundle() {
  $entity_type = 'entity_test_rev';
  $this
    ->createFieldWithStorage('', $entity_type);
  $cardinality = $this->fieldTestData->field_storage
    ->getCardinality();

  // Create a new bundle.
  $new_bundle = 'test_bundle_' . mb_strtolower($this
    ->randomMachineName());
  entity_test_create_bundle($new_bundle, NULL, $entity_type);

  // Add a field to that bundle.
  $this->fieldTestData->field_definition['bundle'] = $new_bundle;
  FieldConfig::create($this->fieldTestData->field_definition)
    ->save();

  // Save an entity with data in the field.
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type)
    ->create([
    'type' => $this->fieldTestData->field
      ->getTargetBundle(),
  ]);
  $values = $this
    ->_generateTestFieldValues($cardinality);
  $entity->{$this->fieldTestData->field_name} = $values;

  // Verify the field data is present on load.
  $entity = $this
    ->entitySaveReload($entity);
  $this
    ->assertCount($cardinality, $entity->{$this->fieldTestData->field_name}, "Data is retrieved for the new bundle");
}