public function DeleteTruncateTest::testTruncateInTransaction in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Database/DeleteTruncateTest.php \Drupal\KernelTests\Core\Database\DeleteTruncateTest::testTruncateInTransaction()
Confirms that we can truncate a whole table while in transaction.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Database/ DeleteTruncateTest.php, line 74
Class
- DeleteTruncateTest
- Tests delete and truncate queries.
Namespace
Drupal\KernelTests\Core\DatabaseCode
public function testTruncateInTransaction() {
$num_records_before = $this->connection
->select('test')
->countQuery()
->execute()
->fetchField();
$this
->assertGreaterThan(0, $num_records_before, 'The table is not empty.');
$transaction = $this->connection
->startTransaction('test_truncate_in_transaction');
$this->connection
->insert('test')
->fields([
'name' => 'Freddie',
'age' => 45,
'job' => 'Great singer',
])
->execute();
$num_records_after_insert = $this->connection
->select('test')
->countQuery()
->execute()
->fetchField();
$this
->assertEquals($num_records_before + 1, $num_records_after_insert);
$this->connection
->truncate('test')
->execute();
// Checks that there are no records left in the table, and transaction is
// still active.
$this
->assertTrue($this->connection
->inTransaction());
$num_records_after = $this->connection
->select('test')
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(0, $num_records_after);
// Close the transaction, and check that there are still no records in the
// table.
$transaction = NULL;
$this
->assertFalse($this->connection
->inTransaction());
$num_records_after = $this->connection
->select('test')
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(0, $num_records_after);
}