function FieldSqlStorageTest::testUpdateFieldSchemaWithData
Tests trying to update a field with data.
File
- 
              core/tests/ Drupal/ KernelTests/ Core/ Entity/ FieldSqlStorageTest.php, line 320 
Class
- FieldSqlStorageTest
- Tests Field SQL Storage .
Namespace
Drupal\KernelTests\Core\EntityCode
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.
