You are here

public function BlockContentCreationTest::testFailedBlockCreation in Zircon Profile 8.0

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

Verifies that a transaction rolls back the failed creation.

File

core/modules/block_content/src/Tests/BlockContentCreationTest.php, line 193
Contains \Drupal\block_content\Tests\BlockContentCreationTest.

Class

BlockContentCreationTest
Create a block and test saving it.

Namespace

Drupal\block_content\Tests

Code

public function testFailedBlockCreation() {

  // Create a block.
  try {
    $this
      ->createBlockContent('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('block_content_field_data', '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('block_content_field_data', '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.');
  }
}