You are here

public function FieldSqlStorageTest::testFieldUpdateFailure in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldUpdateFailure()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testFieldUpdateFailure()

Test that failure to create fields is handled gracefully.

File

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

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldUpdateFailure() {

  // Create a text field.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'test_text',
    'entity_type' => 'entity_test_rev',
    'type' => 'text',
    'settings' => [
      '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('Update succeeded.');
  } catch (\Exception $e) {

    // Expected exception; just continue testing.
  }

  // Ensure that the field tables are still there.
  $tables = [
    $this->tableMapping
      ->getDedicatedDataTableName($prior_field_storage),
    $this->tableMapping
      ->getDedicatedRevisionTableName($prior_field_storage),
  ];
  $schema = Database::getConnection()
    ->schema();
  foreach ($tables as $table_name) {
    $this
      ->assertTrue($schema
      ->tableExists($table_name), t('Table %table exists.', [
      '%table' => $table_name,
    ]));
  }
}