You are here

function FieldAttachStorageTest::testFieldAttachDelete in Zircon Profile 8.0

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

Test entity deletion.

File

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

Class

FieldAttachStorageTest
Tests storage-related Field Attach API functions.

Namespace

Drupal\field\Tests

Code

function testFieldAttachDelete() {
  $entity_type = 'entity_test_rev';
  $this
    ->createFieldWithStorage('', $entity_type);
  $cardinality = $this->fieldTestData->field_storage
    ->getCardinality();
  $entity = entity_create($entity_type, array(
    'type' => $this->fieldTestData->field
      ->getTargetBundle(),
  ));
  $vids = array();

  // Create revision 0
  $values = $this
    ->_generateTestFieldValues($cardinality);
  $entity->{$this->fieldTestData->field_name} = $values;
  $entity
    ->save();
  $vids[] = $entity
    ->getRevisionId();

  // Create revision 1
  $entity
    ->setNewRevision();
  $entity
    ->save();
  $vids[] = $entity
    ->getRevisionId();

  // Create revision 2
  $entity
    ->setNewRevision();
  $entity
    ->save();
  $vids[] = $entity
    ->getRevisionId();
  $controller = $this->container
    ->get('entity.manager')
    ->getStorage($entity
    ->getEntityTypeId());
  $controller
    ->resetCache();

  // Confirm each revision loads
  foreach ($vids as $vid) {
    $revision = $controller
      ->loadRevision($vid);
    $this
      ->assertEqual(count($revision->{$this->fieldTestData->field_name}), $cardinality, "The test entity revision {$vid} has {$cardinality} values.");
  }

  // Delete revision 1, confirm the other two still load.
  $controller
    ->deleteRevision($vids[1]);
  $controller
    ->resetCache();
  foreach (array(
    0,
    2,
  ) as $key) {
    $vid = $vids[$key];
    $revision = $controller
      ->loadRevision($vid);
    $this
      ->assertEqual(count($revision->{$this->fieldTestData->field_name}), $cardinality, "The test entity revision {$vid} has {$cardinality} values.");
  }

  // Confirm the current revision still loads
  $controller
    ->resetCache();
  $current = $controller
    ->load($entity
    ->id());
  $this
    ->assertEqual(count($current->{$this->fieldTestData->field_name}), $cardinality, "The test entity current revision has {$cardinality} values.");

  // Delete all field data, confirm nothing loads
  $entity
    ->delete();
  $controller
    ->resetCache();
  foreach (array(
    0,
    1,
    2,
  ) as $vid) {
    $revision = $controller
      ->loadRevision($vid);
    $this
      ->assertFalse($revision);
  }
  $this
    ->assertFalse($controller
    ->load($entity
    ->id()));
}