You are here

function BulkDeleteTest::testPurgeField 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::testPurgeField()

Verify that field data items and fields are purged when a field storage is deleted.

File

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

Class

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

Namespace

Drupal\field\Tests

Code

function testPurgeField() {

  // Start recording hook invocations.
  field_test_memorize();
  $bundle = reset($this->bundles);
  $field_storage = reset($this->fieldStorages);
  $field_name = $field_storage
    ->getName();

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

  // No field hooks were called.
  $mem = field_test_memorize();
  $this
    ->assertEqual(count($mem), 0, 'No field hooks were called');
  $batch_size = 2;
  for ($count = 8; $count >= 0; $count -= $batch_size) {

    // Purge two entities.
    field_purge_batch($batch_size);

    // There are $count deleted entities left.
    $found = \Drupal::entityQuery('entity_test')
      ->condition('type', $bundle)
      ->condition($field_name . '.deleted', 1)
      ->execute();
    $this
      ->assertEqual(count($found), $count, 'Correct number of entities found after purging 2');
  }

  // Check hooks invocations.
  // FieldItemInterface::delete() should have been called once for each entity in the
  // bundle.
  $actual_hooks = field_test_memorize();
  $hooks = array();
  $entities = $this->entitiesByBundles[$bundle];
  foreach ($entities as $id => $entity) {
    $hooks['field_test_field_delete'][] = $entity;
  }
  $this
    ->checkHooksInvocations($hooks, $actual_hooks);

  // The field still exists, deleted.
  $fields = entity_load_multiple_by_properties('field_config', array(
    'field_storage_uuid' => $field_storage
      ->uuid(),
    'deleted' => TRUE,
    'include_deleted' => TRUE,
  ));
  $this
    ->assertEqual(count($fields), 1, 'There is one deleted field');

  // Purge the field.
  field_purge_batch($batch_size);

  // The field is gone.
  $fields = entity_load_multiple_by_properties('field_config', array(
    'field_storage_uuid' => $field_storage
      ->uuid(),
    'deleted' => TRUE,
    'include_deleted' => TRUE,
  ));
  $this
    ->assertEqual(count($fields), 0, 'The field is gone');

  // The field storage still exists, not deleted, because it has a second
  // field.
  $storages = entity_load_multiple_by_properties('field_storage_config', array(
    'uuid' => $field_storage
      ->uuid(),
    'include_deleted' => TRUE,
  ));
  $this
    ->assertTrue(isset($storages[$field_storage
    ->uuid()]), 'The field storage exists and is not deleted');
}