You are here

public function BlockContentCreationTest::testFailedBlockCreation in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php \Drupal\Tests\block_content\Functional\BlockContentCreationTest::testFailedBlockCreation()

Verifies that a transaction rolls back the failed creation.

File

core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php, line 200

Class

BlockContentCreationTest
Create a block and test saving it.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testFailedBlockCreation() {

  // Create a block.
  try {
    $this
      ->createBlockContent('fail_creation');
    $this
      ->fail('Expected exception has not been thrown.');
  } catch (\Exception $e) {

    // Expected exception; just continue testing.
  }
  $connection = Database::getConnection();
  if ($connection
    ->supportsTransactions()) {

    // Check that the block does not exist in the database.
    $id = $connection
      ->select('block_content_field_data', 'b')
      ->fields('b', [
      '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 = $connection
      ->select('block_content_field_data', 'b')
      ->fields('b', [
      '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 = $connection
      ->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.');
  }
}