InsertDefaultsTest.php in Zircon Profile 8
File
core/modules/system/src/Tests/Database/InsertDefaultsTest.php
View source
<?php
namespace Drupal\system\Tests\Database;
use Drupal\Core\Database\Query\NoFieldsException;
class InsertDefaultsTest extends DatabaseTestBase {
function testDefaultInsert() {
$query = db_insert('test')
->useDefaults(array(
'job',
));
$id = $query
->execute();
$schema = drupal_get_module_schema('database_test', 'test');
$job = db_query('SELECT job FROM {test} WHERE id = :id', array(
':id' => $id,
))
->fetchField();
$this
->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.');
}
function testDefaultEmptyInsert() {
$num_records_before = (int) db_query('SELECT COUNT(*) FROM {test}')
->fetchField();
try {
db_insert('test')
->execute();
$this
->fail('Expected exception NoFieldsException has not been thrown.');
} catch (NoFieldsException $e) {
$this
->pass('Expected exception NoFieldsException has been thrown.');
}
$num_records_after = (int) db_query('SELECT COUNT(*) FROM {test}')
->fetchField();
$this
->assertIdentical($num_records_before, $num_records_after, 'Do nothing as no fields are specified.');
}
function testDefaultInsertWithFields() {
$query = db_insert('test')
->fields(array(
'name' => 'Bob',
))
->useDefaults(array(
'job',
));
$id = $query
->execute();
$schema = drupal_get_module_schema('database_test', 'test');
$job = db_query('SELECT job FROM {test} WHERE id = :id', array(
':id' => $id,
))
->fetchField();
$this
->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.');
}
}