You are here

function DeleteTruncateTest::testSubselectDelete in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/DeleteTruncateTest.php \Drupal\system\Tests\Database\DeleteTruncateTest::testSubselectDelete()

Confirms that we can use a subselect in a delete successfully.

File

core/modules/system/src/Tests/Database/DeleteTruncateTest.php, line 28
Contains \Drupal\system\Tests\Database\DeleteTruncateTest.

Class

DeleteTruncateTest
Tests delete and truncate queries.

Namespace

Drupal\system\Tests\Database

Code

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.');
}