function DriverSpecificTransactionTestBase::testRollbackAfterDdlStatementForNonTransactionalDdlDatabase

Tests rollback after a DDL statement when no transactional DDL supported.

@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 439

Class

DriverSpecificTransactionTestBase
Tests the transaction abstraction system.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testRollbackAfterDdlStatementForNonTransactionalDdlDatabase() : void {
    if ($this->connection
        ->supportsTransactionalDDL()) {
        $this->markTestSkipped('This test only works for database that do not support transactional DDL.');
    }
    // For database servers that do not support transactional DDL,
    // the DDL statement should commit the transaction stack.
    $this->cleanUp();
    $transaction = $this->createRootTransaction('', FALSE);
    $reflectionMethod = new \ReflectionMethod(get_class($this->connection
        ->transactionManager()), 'getConnectionTransactionState');
    $this->assertSame(1, $this->connection
        ->transactionManager()
        ->stackDepth());
    $this->assertEquals(ClientConnectionTransactionState::Active, $reflectionMethod->invoke($this->connection
        ->transactionManager()));
    $this->insertRow('row');
    $this->executeDDLStatement();
    // Try to rollback the root transaction. Since the DDL already committed
    // it, it should fail.
    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);
    $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.