function EntityDefinitionUpdateTest::testBundleFieldCreateDeleteWithExistingEntities

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testBundleFieldCreateDeleteWithExistingEntities()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testBundleFieldCreateDeleteWithExistingEntities()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php \Drupal\KernelTests\Core\Entity\EntityDefinitionUpdateTest::testBundleFieldCreateDeleteWithExistingEntities()

Tests creating and deleting a bundle field if entities exist.

This tests deletion when there are existing entities, but non-existent data for the field being deleted.

See also

testBundleFieldDeleteWithExistingData()

File

core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php, line 459

Class

EntityDefinitionUpdateTest
Tests EntityDefinitionUpdateManager functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testBundleFieldCreateDeleteWithExistingEntities() : void {
    // Save an entity.
    $name = $this->randomString();
    $storage = $this->entityTypeManager
        ->getStorage('entity_test_update');
    $entity = $storage->create([
        'name' => $name,
    ]);
    $entity->save();
    // Add a bundle field and run the update. Ensure the bundle field's table
    // is created and the prior saved entity data is still there.
    $this->addBundleField();
    $this->applyEntityUpdates();
    $schema_handler = $this->database
        ->schema();
    $this->assertTrue($schema_handler->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table created for new_bundle_field.');
    $entity = $this->entityTypeManager
        ->getStorage('entity_test_update')
        ->load($entity->id());
    $this->assertSame($name, $entity->name->value, 'Entity data preserved during field creation.');
    // Remove the base field and run the update. Ensure the bundle field's
    // table is deleted and the prior saved entity data is still there.
    $this->removeBundleField();
    $this->applyEntityUpdates();
    $this->assertFalse($schema_handler->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table deleted for new_bundle_field.');
    $entity = $this->entityTypeManager
        ->getStorage('entity_test_update')
        ->load($entity->id());
    $this->assertSame($name, $entity->name->value, 'Entity data preserved during field deletion.');
    // Test that required columns are created as 'not null'.
    $this->addBundleField('shape_required');
    $this->applyEntityUpdates();
    $message = 'The new_bundle_field_shape column is not nullable.';
    $values = [
        'bundle' => $entity->bundle(),
        'deleted' => 0,
        'entity_id' => $entity->id(),
        'revision_id' => $entity->id(),
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'delta' => 0,
        'new_bundle_field_color' => $this->randomString(),
    ];
    try {
        // Try to insert a record without providing a value for the 'not null'
        // column. This should fail.
        $this->database
            ->insert('entity_test_update__new_bundle_field')
            ->fields($values)
            ->execute();
        $this->fail($message);
    } catch (IntegrityConstraintViolationException) {
        // Now provide a value for the 'not null' column. This is expected to
        // succeed.
        $values['new_bundle_field_shape'] = $this->randomString();
        $this->database
            ->insert('entity_test_update__new_bundle_field')
            ->fields($values)
            ->execute();
    }
}

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