function TransactionTest::testRootTransactionEndCallbackCalledAfterDdlAndRollbackForTransactionalDdlDatabase

Tests post-transaction rollback executes after a DDL statement.

For database servers that support transactional DDL, a rollback of a transaction including DDL statements is possible.

File

core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php, line 1137

Class

TransactionTest
Tests the transactions, using the explicit ::commitOrRelease method.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testRootTransactionEndCallbackCalledAfterDdlAndRollbackForTransactionalDdlDatabase() : void {
  if (!$this->connection
    ->supportsTransactionalDDL()) {
    $this->markTestSkipped('This test only works for database supporting transactional DDL.');
  }
  $transaction = $this->createRootTransaction('', FALSE);
  $this->connection
    ->transactionManager()
    ->addPostTransactionCallback([
    $this,
    'rootTransactionCallback',
  ]);
  $this->insertRow('row');
  $this->assertNull($this->postTransactionCallbackAction);
  // Callbacks are processed only when destructing the transaction.
  // Executing a DDL statement is not sufficient itself.
  // We cannot use truncate here, since it has protective code to fall back
  // to a transactional delete when in transaction. We drop an unrelated
  // table instead.
  $this->connection
    ->schema()
    ->dropTable('test_people');
  $this->assertNull($this->postTransactionCallbackAction);
  $this->assertRowAbsent('rtcCommit');
  $this->assertRowAbsent('rtcRollback');
  $this->assertRowPresent('row');
  // Callbacks are processed only when destructing the transaction.
  // Executing the rollback is not sufficient by itself.
  $transaction->rollBack();
  $this->assertNull($this->postTransactionCallbackAction);
  $this->assertRowAbsent('rtcCommit');
  $this->assertRowAbsent('rtcRollback');
  $this->assertRowAbsent('row');
  // Destruct the transaction.
  unset($transaction);
  // The post-transaction callback should now have inserted a 'rtcRollback'
  // row.
  $this->assertSame('rtcRollback', $this->postTransactionCallbackAction);
  $this->assertRowAbsent('rtcCommit');
  $this->assertRowPresent('rtcRollback');
  $this->assertRowAbsent('row');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.