function CrudTest::testCreateFieldFail

Test failure to create a field.

File

drupal/core/modules/field/lib/Drupal/field/Tests/CrudTest.php, line 157
Definition of Drupal\field\Tests\CrudTest.

Class

CrudTest

Namespace

Drupal\field\Tests

Code

function testCreateFieldFail() {
  $field_name = 'duplicate';
  $field_definition = array(
    'field_name' => $field_name,
    'type' => 'test_field',
    'storage' => array(
      'type' => 'field_test_storage_failure',
    ),
  );
  $query = db_select('field_config')
    ->condition('field_name', $field_name)
    ->countQuery();

  // The field does not appear in field_config.
  $count = $query
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 0, 'A field_config row for the field does not exist.');

  // Try to create the field.
  try {
    $field = field_create_field($field_definition);
    $this
      ->assertTrue(FALSE, 'Field creation (correctly) fails.');
  } catch (Exception $e) {
    $this
      ->assertTrue(TRUE, 'Field creation (correctly) fails.');
  }

  // The field does not appear in field_config.
  $count = $query
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 0, 'A field_config row for the field does not exist.');
}