function BulkDeleteTest::testPurgeFieldStorage

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::testPurgeFieldStorage()
  2. 8.9.x core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::testPurgeFieldStorage()
  3. 11.x core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::testPurgeFieldStorage()

Tests purging field storages.

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 387

Class

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

Namespace

Drupal\Tests\field\Kernel

Code

public function testPurgeFieldStorage() : void {
    // 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->assertArrayHasKey($field->uuid(), $fields);
    $this->assertTrue($fields[$field->uuid()]
        ->isDeleted());
    // 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->assertArrayHasKey($field_storage->uuid(), $storages);
    $this->assertFalse($storages[$field_storage->uuid()]
        ->isDeleted());
    // 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->assertArrayHasKey($field->uuid(), $fields);
    $this->assertTrue($fields[$field->uuid()]
        ->isDeleted());
    $storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')
        ->loadByProperties([
        'uuid' => $field_storage->uuid(),
        'include_deleted' => TRUE,
    ]);
    $this->assertArrayHasKey($field_storage->uuid(), $storages);
    $this->assertTrue($storages[$field_storage->uuid()]
        ->isDeleted());
    // 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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.