function DriverSpecificTransactionTestBase::testRootTransactionEndCallbackFailureUponDdlAndRollbackForNonTransactionalDdlDatabase

Tests post-transaction rollback failure after a DDL statement.

For database servers that support transactional DDL, a rollback of a transaction including DDL statements is not possible, since a commit happened already. We cannot decide what should be the status of the callback, an exception is thrown.

@todo In drupal:12.0.0, rollBack will throw a TransactionOutOfOrderException. Adjust the test accordingly.

File

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php, line 926

Class

DriverSpecificTransactionTestBase
Tests the transaction abstraction system.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testRootTransactionEndCallbackFailureUponDdlAndRollbackForNonTransactionalDdlDatabase() : void {
    if ($this->connection
        ->supportsTransactionalDDL()) {
        $this->markTestSkipped('This test only works for database that do not support 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');
    set_error_handler(static function (int $errno, string $errstr) : bool {
        throw new \ErrorException($errstr);
    });
    try {
        $transaction->rollBack();
    } catch (\ErrorException $e) {
        $this->assertSame('Transaction::rollBack() failed because of a prior execution of a DDL statement.', $e->getMessage());
    } finally {
        restore_error_handler();
    }
    unset($transaction);
    // The post-transaction callback should now have inserted a 'rtcRollback'
    // row.
    $this->assertSame('rtcRollback', $this->postTransactionCallbackAction);
    $this->assertRowAbsent('rtcCommit');
    $this->assertRowPresent('rtcRollback');
    $manager = $this->connection
        ->transactionManager();
    $this->assertSame(0, $manager->stackDepth());
    $reflectedTransactionState = new \ReflectionMethod($manager, 'getConnectionTransactionState');
    $this->assertSame(ClientConnectionTransactionState::RollbackFailed, $reflectedTransactionState->invoke($manager));
    $this->assertRowPresent('row');
}

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