function BulkDeleteTest::setUp

Same name and namespace in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::setUp()
  2. 10 core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::setUp()
  3. 11.x core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::setUp()

Overrides FieldKernelTestBase::setUp

File

core/modules/field/tests/src/Kernel/BulkDeleteTest.php, line 94

Class

BulkDeleteTest
Bulk delete storages and fields, and clean up afterwards.

Namespace

Drupal\Tests\field\Kernel

Code

protected function setUp() : void {
    parent::setUp();
    $this->fieldStorages = [];
    $this->entities = [];
    $this->entitiesByBundles = [];
    // Create two bundles.
    $this->bundles = [
        'bb_1' => 'bb_1',
        'bb_2' => 'bb_2',
    ];
    foreach ($this->bundles as $name => $desc) {
        entity_test_create_bundle($name, $desc);
    }
    // Create two field storages.
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'bf_1',
        'entity_type' => $this->entityTypeId,
        'type' => 'test_field',
        'cardinality' => 1,
    ]);
    $field_storage->save();
    $this->fieldStorages[] = $field_storage;
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'bf_2',
        'entity_type' => $this->entityTypeId,
        'type' => 'test_field',
        'cardinality' => 4,
    ]);
    $field_storage->save();
    $this->fieldStorages[] = $field_storage;
    // For each bundle, create each field, and 10 entities with values for the
    // fields.
    foreach ($this->bundles as $bundle) {
        foreach ($this->fieldStorages as $field_storage) {
            FieldConfig::create([
                'field_storage' => $field_storage,
                'bundle' => $bundle,
            ])->save();
        }
        for ($i = 0; $i < 10; $i++) {
            $entity = $this->container
                ->get('entity_type.manager')
                ->getStorage($this->entityTypeId)
                ->create([
                'type' => $bundle,
            ]);
            foreach ($this->fieldStorages as $field_storage) {
                $entity->{$field_storage->getName()}
                    ->setValue($this->_generateTestFieldValues($field_storage->getCardinality()));
            }
            $entity->save();
        }
    }
    $this->entities = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->entityTypeId)
        ->loadMultiple();
    foreach ($this->entities as $entity) {
        // This test relies on the entities having stale field definitions
        // so that the deleted field can be accessed on them. Access the field
        // now, so that they are always loaded.
        $entity->bf_1->value;
        // Also keep track of the entities per bundle.
        $this->entitiesByBundles[$entity->bundle()][$entity->id()] = $entity;
    }
}

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