function BulkDeleteTest::testDeleteField in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/field/src/Tests/BulkDeleteTest.php \Drupal\field\Tests\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/ src/ Tests/ BulkDeleteTest.php, line 162 - Contains \Drupal\field\Tests\BulkDeleteTest.
Class
- BulkDeleteTest
- Bulk delete storages and fields, and clean up afterwards.
Namespace
Drupal\field\TestsCode
function testDeleteField() {
$bundle = reset($this->bundles);
$field_storage = reset($this->fieldStorages);
$field_name = $field_storage
->getName();
$factory = \Drupal::service('entity.query');
// There are 10 entities of this bundle.
$found = $factory
->get('entity_test')
->condition('type', $bundle)
->execute();
$this
->assertEqual(count($found), 10, '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 = 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');
$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.
$storage = \Drupal::entityManager()
->getStorage($this->entityTypeId);
/** @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 = db_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 = $factory
->get('entity_test')
->condition('type', $bundle)
->condition("{$field_name}.deleted", 0)
->execute();
$this
->assertFalse($found, 'No entities found after deleting');
// There are 10 entities of this bundle when deleted fields are allowed, and
// their values are correct.
$found = $factory
->get('entity_test')
->condition('type', $bundle)
->condition("{$field_name}.deleted", 1)
->sort('id')
->execute();
$this
->assertEqual(count($found), 10, 'Correct number of entities found after deleting');
$this
->assertFalse(array_diff($found, array_keys($this->entities)));
}