function FieldAttachStorageTest::testFieldAttachDelete

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

Tests entity deletion.

File

core/modules/field/tests/src/Kernel/FieldAttachStorageTest.php, line 231

Class

FieldAttachStorageTest
Tests storage-related Field Attach API functions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testFieldAttachDelete() {
    $entity_type = 'entity_test_rev';
    $this->createFieldWithStorage('', $entity_type);
    $cardinality = $this->fieldTestData->field_storage
        ->getCardinality();
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        'type' => $this->fieldTestData->field
            ->getTargetBundle(),
    ]);
    $vids = [];
    // 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_type.manager')
        ->getStorage($entity->getEntityTypeId());
    $controller->resetCache();
    // Confirm each revision loads
    foreach ($vids as $vid) {
        $revision = $controller->loadRevision($vid);
        $this->assertCount($cardinality, $revision->{$this->fieldTestData->field_name}, "The test entity revision {$vid} has {$cardinality} values.");
    }
    // Delete revision 1, confirm the other two still load.
    $controller->deleteRevision($vids[1]);
    $controller->resetCache();
    foreach ([
        0,
        2,
    ] as $key) {
        $vid = $vids[$key];
        $revision = $controller->loadRevision($vid);
        $this->assertCount($cardinality, $revision->{$this->fieldTestData->field_name}, "The test entity revision {$vid} has {$cardinality} values.");
    }
    // Confirm the current revision still loads
    $controller->resetCache();
    $current = $controller->load($entity->id());
    $this->assertCount($cardinality, $current->{$this->fieldTestData->field_name}, "The test entity current revision has {$cardinality} values.");
    // Delete all field data, confirm nothing loads
    $entity->delete();
    $controller->resetCache();
    foreach ([
        0,
        1,
        2,
    ] as $vid) {
        $revision = $controller->loadRevision($vid);
        $this->assertNull($revision);
    }
    $this->assertNull($controller->load($entity->id()));
}

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