You are here

function FieldSqlStorageTest::testUpdateFieldSchemaWithData in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php \Drupal\system\Tests\Entity\FieldSqlStorageTest::testUpdateFieldSchemaWithData()

Test trying to update a field with data.

File

core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php, line 312
Contains \Drupal\system\Tests\Entity\FieldSqlStorageTest.

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\system\Tests\Entity

Code

function testUpdateFieldSchemaWithData() {
  $entity_type = 'entity_test_rev';

  // Create a decimal 5.2 field and add some data.
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => 'decimal52',
    'entity_type' => $entity_type,
    'type' => 'decimal',
    'settings' => array(
      'precision' => 5,
      'scale' => 2,
    ),
  ));
  $field_storage
    ->save();
  $field = entity_create('field_config', array(
    'field_storage' => $field_storage,
    'bundle' => $entity_type,
  ));
  $field
    ->save();
  $entity = entity_create($entity_type, array(
    '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);
  try {
    $field_storage
      ->save();
    $this
      ->fail(t('Cannot update field schema with data.'));
  } catch (FieldStorageDefinitionUpdateForbiddenException $e) {
    $this
      ->pass(t('Cannot update field schema with data.'));
  }
}