function FieldSqlStorageTest::testFieldUpdateFailure

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

Tests that failure to create fields is handled gracefully.

File

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

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldUpdateFailure() : void {
  // 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), 'Table $table_name exists.');
  }
}

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