You are here

protected function BulkDeleteTest::setUp in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/BulkDeleteTest.php \Drupal\field\Tests\BulkDeleteTest::setUp()

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

Overrides FieldUnitTestBase::setUp

File

core/modules/field/src/Tests/BulkDeleteTest.php, line 94
Contains \Drupal\field\Tests\BulkDeleteTest.

Class

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

Namespace

Drupal\field\Tests

Code

protected function setUp() {
  parent::setUp();
  $this->fieldStorages = array();
  $this->entities = array();
  $this->entitiesByBundles = array();

  // Create two bundles.
  $this->bundles = array(
    '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 = entity_create('field_storage_config', array(
    'field_name' => 'bf_1',
    'entity_type' => $this->entityTypeId,
    'type' => 'test_field',
    'cardinality' => 1,
  ));
  $field_storage
    ->save();
  $this->fieldStorages[] = $field_storage;
  $field_storage = entity_create('field_storage_config', array(
    '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) {
      entity_create('field_config', array(
        'field_storage' => $field_storage,
        'bundle' => $bundle,
      ))
        ->save();
    }
    for ($i = 0; $i < 10; $i++) {
      $entity = entity_create($this->entityTypeId, array(
        'type' => $bundle,
      ));
      foreach ($this->fieldStorages as $field_storage) {
        $entity->{$field_storage
          ->getName()}
          ->setValue($this
          ->_generateTestFieldValues($field_storage
          ->getCardinality()));
      }
      $entity
        ->save();
    }
  }
  $this->entities = entity_load_multiple($this->entityTypeId);
  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;
  }
}