function DatabaseLegacyTest::testSchemaFieldDefaultChange
Tests Schema::fieldSetDefault and Schema::fieldSetNoDefault.
@expectedDeprecation fieldSetDefault() is deprecated in drupal:8.7.0 and will be removed before drupal:9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035 @expectedDeprecation fieldSetNoDefault() is deprecated in drupal:8.7.0 and will be removed before drupal:9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Database/ DatabaseLegacyTest.php, line 164
Class
- DatabaseLegacyTest
- Deprecation tests cases for the database layer.
Namespace
Drupal\KernelTests\Core\DatabaseCode
public function testSchemaFieldDefaultChange() {
// Create a table.
$table_specification = [
'description' => 'Schema table description.',
'fields' => [
'id' => [
'type' => 'int',
'default' => NULL,
],
'test_field' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Test field',
],
],
];
$this->connection
->schema()
->createTable('test_table', $table_specification);
// An insert without a value for the column 'test_field' should fail.
try {
$this->connection
->insert('test_table')
->fields([
'id' => 1,
])
->execute();
$this->fail('Expected DatabaseException, none was thrown.');
} catch (DatabaseException $e) {
$this->assertEquals(0, $this->connection
->select('test_table')
->countQuery()
->execute()
->fetchField());
}
// Add a default value to the column.
$this->connection
->schema()
->fieldSetDefault('test_table', 'test_field', 0);
// The insert should now succeed.
$this->connection
->insert('test_table')
->fields([
'id' => 1,
])
->execute();
$this->assertEquals(1, $this->connection
->select('test_table')
->countQuery()
->execute()
->fetchField());
// Remove the default.
$this->connection
->schema()
->fieldSetNoDefault('test_table', 'test_field');
// The insert should fail again.
try {
$this->connection
->insert('test_table')
->fields([
'id' => 2,
])
->execute();
$this->fail('Expected DatabaseException, none was thrown.');
} catch (DatabaseException $e) {
$this->assertEquals(1, $this->connection
->select('test_table')
->countQuery()
->execute()
->fetchField());
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.