You are here

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

Verify that field storages are preserved and purged correctly as multiple fields are deleted and purged.

File

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

Class

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

Namespace

Drupal\field\Tests

Code

function testPurgeFieldStorage() {

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

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

  // Assert that FieldItemInterface::delete() was not called yet.
  $mem = field_test_memorize();
  $this
    ->assertEqual(count($mem), 0, 'No field hooks were called.');

  // Purge the data.
  field_purge_batch(10);

  // 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(
    'uuid' => $field
      ->uuid(),
    'include_deleted' => TRUE,
  ));
  $this
    ->assertTrue(isset($fields[$field
    ->uuid()]) && $fields[$field
    ->uuid()]
    ->isDeleted(), 'The field exists and is deleted');

  // Purge again to purge the field.
  field_purge_batch(0);

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

  // The field storage still exists, not deleted.
  $storages = entity_load_multiple_by_properties('field_storage_config', array(
    'uuid' => $field_storage
      ->uuid(),
    'include_deleted' => TRUE,
  ));
  $this
    ->assertTrue(isset($storages[$field_storage
    ->uuid()]) && !$storages[$field_storage
    ->uuid()]
    ->isDeleted(), 'The field storage exists and is not deleted');

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

  // Assert that FieldItemInterface::delete() was not called yet.
  $mem = field_test_memorize();
  $this
    ->assertEqual(count($mem), 0, 'No field hooks were called.');

  // Purge the data.
  field_purge_batch(10);

  // Check hooks invocations (same as above, for the 2nd 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 and the storage still exist, deleted.
  $fields = entity_load_multiple_by_properties('field_config', array(
    'uuid' => $field
      ->uuid(),
    'include_deleted' => TRUE,
  ));
  $this
    ->assertTrue(isset($fields[$field
    ->uuid()]) && $fields[$field
    ->uuid()]
    ->isDeleted(), 'The field exists and is deleted');
  $storages = entity_load_multiple_by_properties('field_storage_config', array(
    'uuid' => $field_storage
      ->uuid(),
    'include_deleted' => TRUE,
  ));
  $this
    ->assertTrue(isset($storages[$field_storage
    ->uuid()]) && $storages[$field_storage
    ->uuid()]
    ->isDeleted(), 'The field storage exists and is deleted');

  // Purge again to purge the field and the storage.
  field_purge_batch(0);

  // The field and the storage are gone.
  $fields = entity_load_multiple_by_properties('field_config', array(
    'uuid' => $field
      ->uuid(),
    'include_deleted' => TRUE,
  ));
  $this
    ->assertEqual(count($fields), 0, 'The field is purged.');
  $storages = entity_load_multiple_by_properties('field_storage_config', array(
    'uuid' => $field_storage
      ->uuid(),
    'include_deleted' => TRUE,
  ));
  $this
    ->assertEqual(count($storages), 0, 'The field storage is purged.');
}