You are here

protected function BulkDeleteTest::setUp in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Set the default field storage backend for fields created during tests.

Overrides FieldKernelTestBase::setUp

File

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

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