View source
<?php
namespace Drupal\system\Tests\Database;
class DeleteTruncateTest extends DatabaseTestBase {
function testSubselectDelete() {
$num_records_before = db_query('SELECT COUNT(*) FROM {test_task}')
->fetchField();
$pid_to_delete = db_query("SELECT * FROM {test_task} WHERE task = 'sleep'")
->fetchField();
$subquery = db_select('test', 't')
->fields('t', array(
'id',
))
->condition('t.id', array(
$pid_to_delete,
), 'IN');
$delete = db_delete('test_task')
->condition('task', 'sleep')
->condition('pid', $subquery, 'IN');
$num_deleted = $delete
->execute();
$this
->assertEqual($num_deleted, 1, 'Deleted 1 record.');
$num_records_after = db_query('SELECT COUNT(*) FROM {test_task}')
->fetchField();
$this
->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
}
function testSimpleDelete() {
$num_records_before = db_query('SELECT COUNT(*) FROM {test}')
->fetchField();
$num_deleted = db_delete('test')
->condition('id', 1)
->execute();
$this
->assertIdentical($num_deleted, 1, 'Deleted 1 record.');
$num_records_after = db_query('SELECT COUNT(*) FROM {test}')
->fetchField();
$this
->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
}
function testTruncate() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test}")
->fetchField();
$this
->assertTrue($num_records_before > 0, 'The table is not empty.');
db_truncate('test')
->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test}")
->fetchField();
$this
->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
}
function testSpecialColumnDelete() {
$num_records_before = db_query('SELECT COUNT(*) FROM {test_special_columns}')
->fetchField();
$num_deleted = db_delete('test_special_columns')
->condition('id', 1)
->execute();
$this
->assertIdentical($num_deleted, 1, 'Deleted 1 special column record.');
$num_records_after = db_query('SELECT COUNT(*) FROM {test_special_columns}')
->fetchField();
$this
->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
}
}