You are here

function FieldSqlStorageTest::testFieldUpdateFailure 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::testFieldUpdateFailure()

Test that failure to create fields is handled gracefully.

File

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

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\system\Tests\Entity

Code

function testFieldUpdateFailure() {

  // Create a text field.
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => 'test_text',
    'entity_type' => 'entity_test_rev',
    'type' => 'text',
    'settings' => array(
      'max_length' => 255,
    ),
  ));
  $field_storage
    ->save();

  // Attempt to update the field in a way that would break the storage. The
  // parenthesis suffix is needed because SQLite has *very* relaxed rules for
  // data types, so we actually need to provide an invalid SQL syntax in order
  // to break it.
  // @see https://www.sqlite.org/datatype3.html
  $prior_field_storage = $field_storage;
  $field_storage
    ->setSetting('max_length', '-1)');
  try {
    $field_storage
      ->save();
    $this
      ->fail(t('Update succeeded.'));
  } catch (\Exception $e) {
    $this
      ->pass(t('Update properly failed.'));
  }

  // Ensure that the field tables are still there.
  $tables = array(
    $this->tableMapping
      ->getDedicatedDataTableName($prior_field_storage),
    $this->tableMapping
      ->getDedicatedRevisionTableName($prior_field_storage),
  );
  foreach ($tables as $table_name) {
    $this
      ->assertTrue(db_table_exists($table_name), t('Table %table exists.', array(
      '%table' => $table_name,
    )));
  }
}