You are here

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

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

File

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

Class

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

Namespace

Drupal\Tests\field\Kernel

Code

public 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
    ->assertCount(0, $mem, '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 = [];
  $hooks['field_test_field_delete'] = $this->entitiesByBundles[$bundle];
  $this
    ->checkHooksInvocations($hooks, $actual_hooks);

  // The field still exists, deleted.
  $fields = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->loadByProperties([
    '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 = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->loadByProperties([
    'uuid' => $field
      ->uuid(),
    'include_deleted' => TRUE,
  ]);
  $this
    ->assertCount(0, $fields, 'The field is purged.');

  // The field storage still exists, not deleted.
  $storages = \Drupal::entityTypeManager()
    ->getStorage('field_storage_config')
    ->loadByProperties([
    '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
    ->assertCount(0, $mem, '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 = [];
  $hooks['field_test_field_delete'] = $this->entitiesByBundles[$bundle];
  $this
    ->checkHooksInvocations($hooks, $actual_hooks);

  // The field and the storage still exist, deleted.
  $fields = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->loadByProperties([
    'uuid' => $field
      ->uuid(),
    'include_deleted' => TRUE,
  ]);
  $this
    ->assertTrue(isset($fields[$field
    ->uuid()]) && $fields[$field
    ->uuid()]
    ->isDeleted(), 'The field exists and is deleted');
  $storages = \Drupal::entityTypeManager()
    ->getStorage('field_storage_config')
    ->loadByProperties([
    '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 = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->loadByProperties([
    'uuid' => $field
      ->uuid(),
    'include_deleted' => TRUE,
  ]);
  $this
    ->assertCount(0, $fields, 'The field is purged.');
  $storages = \Drupal::entityTypeManager()
    ->getStorage('field_storage_config')
    ->loadByProperties([
    'uuid' => $field_storage
      ->uuid(),
    'include_deleted' => TRUE,
  ]);
  $this
    ->assertCount(0, $storages, 'The field storage is purged.');
}