You are here

function FieldAttachStorageTest::testEntityDeleteBundle in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldAttachStorageTest.php \Drupal\field\Tests\FieldAttachStorageTest::testEntityDeleteBundle()

Test entity_bundle_delete().

File

core/modules/field/src/Tests/FieldAttachStorageTest.php, line 306
Contains \Drupal\field\Tests\FieldAttachStorageTest.

Class

FieldAttachStorageTest
Tests storage-related Field Attach API functions.

Namespace

Drupal\field\Tests

Code

function testEntityDeleteBundle() {
  $entity_type = 'entity_test_rev';
  $this
    ->createFieldWithStorage('', $entity_type);

  // Create a new bundle.
  $new_bundle = 'test_bundle_' . Unicode::strtolower($this
    ->randomMachineName());
  entity_test_create_bundle($new_bundle, NULL, $entity_type);

  // Add a field to that bundle.
  $this->fieldTestData->field_definition['bundle'] = $new_bundle;
  entity_create('field_config', $this->fieldTestData->field_definition)
    ->save();

  // Create a second field for the test bundle
  $field_name = Unicode::strtolower($this
    ->randomMachineName() . '_field_name');
  $field_storage = array(
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'type' => 'test_field',
    'cardinality' => 1,
  );
  entity_create('field_storage_config', $field_storage)
    ->save();
  $field = array(
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'bundle' => $this->fieldTestData->field
      ->getTargetBundle(),
    'label' => $this
      ->randomMachineName() . '_label',
    'description' => $this
      ->randomMachineName() . '_description',
    'weight' => mt_rand(0, 127),
  );
  entity_create('field_config', $field)
    ->save();

  // Save an entity with data for both fields
  $entity = entity_create($entity_type, array(
    'type' => $this->fieldTestData->field
      ->getTargetBundle(),
  ));
  $values = $this
    ->_generateTestFieldValues($this->fieldTestData->field_storage
    ->getCardinality());
  $entity->{$this->fieldTestData->field_name} = $values;
  $entity->{$field_name} = $this
    ->_generateTestFieldValues(1);
  $entity = $this
    ->entitySaveReload($entity);

  // Verify the fields are present on load
  $this
    ->assertEqual(count($entity->{$this->fieldTestData->field_name}), 4, 'First field got loaded');
  $this
    ->assertEqual(count($entity->{$field_name}), 1, 'Second field got loaded');

  // Delete the bundle.
  entity_test_delete_bundle($this->fieldTestData->field
    ->getTargetBundle(), $entity_type);

  // Verify no data gets loaded
  $controller = $this->container
    ->get('entity.manager')
    ->getStorage($entity
    ->getEntityTypeId());
  $controller
    ->resetCache();
  $entity = $controller
    ->load($entity
    ->id());
  $this
    ->assertTrue(empty($entity->{$this->fieldTestData->field_name}), 'No data for first field');
  $this
    ->assertTrue(empty($entity->{$field_name}), 'No data for second field');

  // Verify that the fields are gone.
  $this
    ->assertFalse(FieldConfig::load('entity_test.' . $this->fieldTestData->field
    ->getTargetBundle() . '.' . $this->fieldTestData->field_name), "First field is deleted");
  $this
    ->assertFalse(FieldConfig::load('entity_test.' . $field['bundle'] . '.' . $field_name), "Second field is deleted");
}