public function CustomBlockCreationTest::testFailedBlockCreation

Verifies that a transaction rolls back the failed creation.

File

drupal/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php, line 72
Contains \Drupal\custom_block\Tests\CustomBlockCreationTest.

Class

CustomBlockCreationTest
Tests creating and saving a block.

Namespace

Drupal\custom_block\Tests

Code

public function testFailedBlockCreation() {

  // Create a block.
  try {
    $this
      ->createCustomBlock('fail_creation');
    $this
      ->fail('Expected exception has not been thrown.');
  } catch (\Exception $e) {
    $this
      ->pass('Expected exception has been thrown.');
  }
  if (Database::getConnection()
    ->supportsTransactions()) {

    // Check that the block does not exist in the database.
    $id = db_select('custom_block', 'b')
      ->fields('b', array(
      'id',
    ))
      ->condition('info', 'fail_creation')
      ->execute()
      ->fetchField();
    $this
      ->assertFalse($id, 'Transactions supported, and block not found in database.');
  }
  else {

    // Check that the block exists in the database.
    $id = db_select('custom_block', 'b')
      ->fields('b', array(
      'id',
    ))
      ->condition('info', 'fail_creation')
      ->execute()
      ->fetchField();
    $this
      ->assertTrue($id, 'Transactions not supported, and block found in database.');

    // Check that the failed rollback was logged.
    $records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")
      ->fetchAll();
    $this
      ->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
  }
}