function FieldAttachStorageTest::testEntityCreateBundle

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testEntityCreateBundle()
  2. 10 core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php \Drupal\Tests\field\Kernel\FieldAttachStorageTest::testEntityCreateBundle()
  3. 11.x 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 291

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");
}

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