View source
<?php
namespace Drupal\Tests\sqlsrv\Kernel;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\Database\IntegrityConstraintViolationException;
use Drupal\Driver\Database\sqlsrv\PDO\DoomedTransactionException;
use Drupal\KernelTests\Core\Database\DatabaseTestBase;
class SchemaTestExtended extends DatabaseTestBase {
protected $table = [];
protected $schema;
protected function setUp() {
parent::setUp();
$schema = $this->connection
->schema();
$this->schema = $schema;
$this->table = [
'description' => 'New Comment',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'name' => [
'description' => "A person's name",
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
'binary' => TRUE,
],
'age' => [
'description' => "The person's age",
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'job' => [
'description' => "The person's job",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => 'Undefined',
],
],
'primary key' => [
'id',
],
'unique keys' => [
'name' => [
'name',
],
],
'indexes' => [
'ages' => [
'age',
],
],
];
}
public function testPrimaryKeyHandling() {
$table_spec = array(
'fields' => array(
'id' => array(
'type' => 'int',
'not null' => TRUE,
),
),
);
$database = \Drupal::database();
$database
->schema()
->createTable('test_table', $table_spec);
$this
->assertTrue($database
->schema()
->tableExists('test_table'), t('Creating a table without a primary key works.'));
$database
->schema()
->addPrimaryKey('test_table', array(
'id',
));
$this
->pass(t('Adding a primary key should work when the table has no data.'));
$database
->insert('test_table')
->fields(array(
'id' => 1,
))
->execute();
try {
$database
->insert('test_table')
->fields(array(
'id' => 1,
))
->execute();
$this
->fail(t('Duplicate values in the table should not be allowed when the primary key is there.'));
} catch (IntegrityConstraintViolationException $e) {
}
$database
->schema()
->dropPrimaryKey('test_table');
$this
->pass(t('Removing a primary key should work.'));
$database
->insert('test_table')
->fields(array(
'id' => 1,
))
->execute();
$this
->pass(t('Adding a duplicate row should work without the primary key.'));
try {
$database
->schema()
->addPrimaryKey('test_table', array(
'id',
));
$this
->fail(t('Trying to add a primary key should fail with duplicate rows in the table.'));
} catch (IntegrityConstraintViolationException $e) {
}
}
public function testPrimaryKeyAlter() {
$table_spec = array(
'fields' => array(
'id' => array(
'type' => 'int',
'not null' => TRUE,
),
),
'primary key' => array(
'id',
),
);
$this->connection
->schema()
->createTable('test_table', $table_spec);
$this
->assertTrue($this->connection
->schema()
->tableExists('test_table'));
$this->connection
->schema()
->changeField('test_table', 'id', 'id', array(
'type' => 'int',
'not null' => TRUE,
'default' => 1,
));
}
public function testUnsignedField() {
$table_spec = array(
'fields' => array(
'id' => array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
),
);
$schema = $this->connection
->schema();
$schema
->createTable('test_table', $table_spec);
try {
$this->connection
->insert('test_table')
->fields(array(
'id' => -1,
))
->execute();
$failed = FALSE;
} catch (DatabaseException $e) {
$failed = TRUE;
}
$this
->assertTrue($failed, t('Inserting a negative value in an unsigned field failed.'));
$this
->assertUnsignedField('test_table', 'id');
try {
$this->connection
->insert('test_table')
->fields(array(
'id' => 1,
))
->execute();
$failed = FALSE;
} catch (DatabaseException $e) {
$failed = TRUE;
}
$this
->assertFalse($failed, t('Inserting a positive value in an unsigned field succeeded.'));
$schema
->changeField('test_table', 'id', 'id', array(
'type' => 'int',
'not null' => TRUE,
));
$this
->assertSignedField('test_table', 'id');
$schema
->changeField('test_table', 'id', 'id', array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
));
$this
->assertUnsignedField('test_table', 'id');
}
protected function assertUnsignedField($table, $field_name) {
try {
$this->connection
->insert($table)
->fields(array(
$field_name => -1,
))
->execute();
$success = TRUE;
} catch (DatabaseException $e) {
$success = FALSE;
}
$this
->assertFalse($success, t('Inserting a negative value in an unsigned field failed.'));
try {
$this->connection
->insert($table)
->fields(array(
$field_name => 1,
))
->execute();
$success = TRUE;
} catch (DatabaseException $e) {
$success = FALSE;
}
$this
->assertTrue($success, t('Inserting a positive value in an unsigned field succeeded.'));
$this->connection
->delete($table)
->execute();
}
protected function assertSignedField($table, $field_name) {
try {
$this->connection
->insert($table)
->fields(array(
$field_name => -1,
))
->execute();
$success = TRUE;
} catch (DatabaseException $e) {
$success = FALSE;
}
$this
->assertTrue($success, t('Inserting a negative value in a signed field succeeded.'));
try {
$this->connection
->insert($table)
->fields(array(
$field_name => 1,
))
->execute();
$success = TRUE;
} catch (DatabaseException $e) {
$success = FALSE;
}
$this
->assertTrue($success, t('Inserting a positive value in a signed field succeeded.'));
$this->connection
->delete($table)
->execute();
}
public function testAddChangeWithIndex() {
$table_spec = array(
'fields' => array(
'id' => array(
'type' => 'int',
'not null' => TRUE,
),
),
'primary key' => array(
'id',
),
);
$this->connection
->schema()
->createTable('test_table', $table_spec);
$this->connection
->schema()
->addField('test_table', 'test', array(
'type' => 'int',
'not null' => TRUE,
'default' => 1,
), array(
'indexes' => array(
'id_test' => array(
'id, test',
),
),
));
$this
->assertTrue($this->connection
->schema()
->indexExists('test_table', 'id_test'), t('The index has been created by db_add_field().'));
$this->connection
->schema()
->dropIndex('test_table', 'id_test');
$this
->assertFalse($this->connection
->schema()
->indexExists('test_table', 'id_test'), t('The index has been dropped.'));
$this->connection
->schema()
->changeField('test_table', 'test', 'test', array(
'type' => 'int',
'not null' => TRUE,
'default' => 1,
), array(
'indexes' => array(
'id_test' => array(
'id, test',
),
),
));
$this
->assertTrue($this->connection
->schema()
->indexExists('test_table', 'id_test'), t('The index has been recreated by db_change_field().'));
}
private function AddChangeWithBinarySearchHelper(array $results, string $type, string $fieldtype) {
foreach ($results as $search => $result) {
$count = $this->connection
->query('SELECT COUNT(*) FROM {test_table_binary} WHERE name = :name', [
':name' => $search,
])
->fetchField();
$this
->assertEqual($count, $result[$type], "Returned the correct number of total rows for a {$type} search on fieldtype {$fieldtype}");
}
}
public function testNumericFieldPrecision() {
$table_spec = array(
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'name' => array(
'type' => 'numeric',
'precision' => 400,
'scale' => 2,
),
),
'primary key' => array(
'id',
),
);
$schema = $this->connection
->schema();
$success = FALSE;
try {
$schema
->createTable('test_table_binary', $table_spec);
$success = TRUE;
} catch (Exception $error) {
$success = FALSE;
}
$this
->assertTrue($success, t('Able to create a numeric field with an out of bounds precision.'));
}
public function testInsertBadCharsIntoNonExistingTable() {
$schema = $this->connection
->schema();
try {
$query = $this->connection
->insert('GHOST_TABLE');
$query
->fields(array(
'FIELD' => gzcompress('compresing this string into zip!'),
));
$query
->execute();
} catch (\Exception $e) {
if (!$e instanceof \Drupal\Core\Database\SchemaObjectDoesNotExistException) {
$this
->fail('Inserting into a non existent table does not trigger the right type of Exception.');
}
else {
$this
->pass('Proper exception type thrown.');
}
}
try {
$query = $this->connection
->update('GHOST_TABLE');
$query
->fields(array(
'FIELD' => gzcompress('compresing this string into zip!'),
));
$query
->execute();
} catch (\Exception $e) {
if (!$e instanceof \Drupal\Core\Database\SchemaObjectDoesNotExistException) {
$this
->fail('Updating into a non existent table does not trigger the right type of Exception.');
}
else {
$this
->pass('Proper exception type thrown.');
}
}
}
public function testTransactionDoomed() {
$table_spec = array(
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'binary' => false,
),
),
'primary key' => array(
'id',
),
);
$schema = $this->connection
->schema();
$schema
->createTable('test_table', $table_spec);
$query = $this->connection
->insert('test_table');
$query
->fields(array(
'name' => 'JUAN',
));
$id = $query
->execute();
$transaction = $this->connection
->startTransaction();
$this->connection
->query('UPDATE {test_table} SET name = :p0 WHERE id = :p1', array(
':p0' => 'DAVID',
':p1' => $id,
));
$name = $this->connection
->query('SELECT TOP(1) NAME from {test_table}')
->fetchField();
$this
->assertEqual($name, 'DAVID');
try {
$name = $this->connection
->query('SELECT COUNT(*) FROM THIS_TABLE_DOES_NOT_EXIST')
->fetchField();
} catch (\Exception $e) {
}
$name = $this->connection
->query('SELECT TOP(1) NAME from {test_table}')
->fetchField();
$this
->assertEqual($name, 'DAVID');
try {
$this->connection
->query('UPDATE {test_table} SET name = :p0 WHERE id = :p1', array(
':p0' => 'DAVID',
':p1' => 'THIS IS NOT AND WILL NEVER BE A NUMBER',
));
} catch (\Exception $e) {
}
$failed = FALSE;
try {
$name = $this->connection
->query('SELECT TOP(1) NAME from {test_table}')
->fetchField();
$this
->assertEqual($name, 'DAVID');
} catch (\Exception $e) {
if (!$e instanceof DoomedTransactionException) {
$this
->fail('Wrong exception when testing doomed transactions.');
}
$failed = TRUE;
}
$this
->assertTrue($failed, 'Operating on the database after the transaction is doomed throws an exception.');
$failed = FALSE;
try {
unset($transaction);
} catch (\Exception $e) {
if (!$e instanceof DoomedTransactionException) {
$this
->fail('Wrong exception when testing doomed transactions.');
}
$failed = TRUE;
}
$this
->assertTrue($failed, 'Trying to commit a doomed transaction throws an Exception.');
}
}