function FieldSqlStorageTest::testUpdateFieldSchemaWithData

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

Test trying to update a field with data.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php, line 316

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testUpdateFieldSchemaWithData() {
    $entity_type = 'entity_test_rev';
    // Create a decimal 5.2 field and add some data.
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'decimal52',
        'entity_type' => $entity_type,
        'type' => 'decimal',
        'settings' => [
            'precision' => 5,
            'scale' => 2,
        ],
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => $entity_type,
    ]);
    $field->save();
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        'id' => 0,
        'revision_id' => 0,
    ]);
    $entity->decimal52->value = '1.235';
    $entity->save();
    // Attempt to update the field in a way that would work without data.
    $field_storage->setSetting('scale', 3);
    $this->expectException(FieldStorageDefinitionUpdateForbiddenException::class);
    $field_storage->save();
}

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