function FieldSqlStorageTestCase::testFieldUpdateFailure

Test that failure to create fields is handled gracefully.

File

modules/field/modules/field_sql_storage/field_sql_storage.test, line 403

Class

FieldSqlStorageTestCase
Tests field storage.

Code

function testFieldUpdateFailure() {
    // Create a text field.
    $field = array(
        'field_name' => 'test_text',
        'type' => 'text',
        'settings' => array(
            'max_length' => 255,
        ),
    );
    $field = field_create_field($field);
    // 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 = $field;
    $field['settings']['max_length'] = '-1)';
    try {
        field_update_field($field);
        $this->fail(t('Update succeeded.'));
    } catch (Exception $e) {
        $this->pass(t('Update properly failed.'));
    }
    // Ensure that the field tables are still there.
    foreach (_field_sql_storage_schema($prior_field) as $table_name => $table_info) {
        $this->assertTrue(db_table_exists($table_name), format_string('Table %table exists.', array(
            '%table' => $table_name,
        )));
    }
}

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