1 database_test.test protected DatabaseTransactionTestCase::transactionInnerLayer($suffix, $rollback = FALSE, $ddl_statement = FALSE)

Helper method for transaction unit tests. This "inner layer" transaction is either used alone or nested inside of the "outer layer" transaction.

Parameters

$suffix: Suffix to add to field values to differentiate tests.

$rollback: Whether or not to try rolling back the transaction when we're done.

$ddl_statement: Whether to execute a DDL statement during the transaction.

File

core/modules/simpletest/tests/database_test.test, line 3348
Database tests.

Class

DatabaseTransactionTestCase
Test transaction support, particularly nesting.

Code

protected function transactionInnerLayer($suffix, $rollback = FALSE, $ddl_statement = FALSE) {
  $connection = Database::getConnection();

  $depth = $connection->transactionDepth();
  // Start a transaction. If we're being called from ->transactionOuterLayer,
  // then we're already in a transaction. Normally, that would make starting
  // a transaction here dangerous, but the database API handles this problem
  // for us by tracking the nesting and avoiding the danger.
  $txn = db_transaction();

  $depth2 = $connection->transactionDepth();
  $this->assertTrue($depth < $depth2, 'Transaction depth is has increased with new transaction.');

  // Insert a single row into the testing table.
  db_insert('test')
    ->fields(array(
      'name' => 'Daniel' . $suffix,
      'age' => '19',
    ))
    ->execute();

  $this->assertTrue($connection->inTransaction(), 'In transaction inside nested transaction.');

  if ($ddl_statement) {
    $table = array(
      'fields' => array(
        'id' => array(
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
      ),
      'primary key' => array('id'),
    );
    db_create_table('database_test_1', $table);

    $this->assertTrue($connection->inTransaction(), 'In transaction inside nested transaction.');
  }

  if ($rollback) {
    // Roll back the transaction, if requested.
    // This rollback should propagate to the last savepoint.
    $txn->rollback();
    $this->assertTrue(($connection->transactionDepth() == $depth), 'Transaction has rolled back to the last savepoint after calling rollback().');
  }
}