You are here

public function BulkDeleteTest::testDeleteField in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::testDeleteField()
  2. 10 core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::testDeleteField()

Verify that deleting a field leaves the field data items in the database and that the appropriate Field API functions can operate on the deleted data and field definition.

This tests how EntityFieldQuery interacts with field deletion and could be moved to FieldCrudTestCase, but depends on this class's setUp().

File

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

Class

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

Namespace

Drupal\Tests\field\Kernel

Code

public function testDeleteField() {
  $bundle = reset($this->bundles);
  $field_storage = reset($this->fieldStorages);
  $field_name = $field_storage
    ->getName();
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_test');

  // There are 10 entities of this bundle.
  $found = $storage
    ->getQuery()
    ->condition('type', $bundle)
    ->execute();
  $this
    ->assertCount(10, $found, 'Correct number of entities found before deleting');

  // Delete the field.
  $field = FieldConfig::loadByName($this->entityTypeId, $bundle, $field_name);
  $field
    ->delete();

  // The field still exists, deleted.
  $fields = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->loadByProperties([
    'field_storage_uuid' => $field_storage
      ->uuid(),
    'deleted' => TRUE,
    'include_deleted' => TRUE,
  ]);
  $this
    ->assertCount(1, $fields, 'There is one deleted field');
  $field = $fields[$field
    ->uuid()];
  $this
    ->assertEqual($field
    ->getTargetBundle(), $bundle, 'The deleted field is for the correct bundle');

  // Check that the actual stored content did not change during delete.

  /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  $table_mapping = $storage
    ->getTableMapping();
  $table = $table_mapping
    ->getDedicatedDataTableName($field_storage);
  $column = $table_mapping
    ->getFieldColumnName($field_storage, 'value');
  $result = Database::getConnection()
    ->select($table, 't')
    ->fields('t')
    ->execute();
  foreach ($result as $row) {
    $this
      ->assertEqual($this->entities[$row->entity_id]->{$field_name}->value, $row->{$column});
  }

  // There are 0 entities of this bundle with non-deleted data.
  $found = $storage
    ->getQuery()
    ->condition('type', $bundle)
    ->condition("{$field_name}.deleted", 0)
    ->execute();
  $this
    ->assertEmpty($found, 'No entities found after deleting');

  // There are 10 entities of this bundle when deleted fields are allowed, and
  // their values are correct.
  $found = $storage
    ->getQuery()
    ->condition('type', $bundle)
    ->condition("{$field_name}.deleted", 1)
    ->sort('id')
    ->execute();
  $this
    ->assertCount(10, $found, 'Correct number of entities found after deleting');
  $this
    ->assertEmpty(array_diff($found, array_keys($this->entities)));
}