You are here

function EntityCrudHookTest::testEntityRollback in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityCrudHookTest.php \Drupal\system\Tests\Entity\EntityCrudHookTest::testEntityRollback()

Tests rollback from failed entity save.

File

core/modules/system/src/Tests/Entity/EntityCrudHookTest.php, line 541
Contains \Drupal\system\Tests\Entity\EntityCrudHookTest.

Class

EntityCrudHookTest
Tests the invocation of hooks when creating, inserting, loading, updating or deleting an entity.

Namespace

Drupal\system\Tests\Entity

Code

function testEntityRollback() {

  // Create a block.
  try {
    entity_create('entity_test', array(
      'name' => 'fail_insert',
    ))
      ->save();
    $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.
    $ids = \Drupal::entityQuery('entity_test')
      ->condition('name', 'fail_insert')
      ->execute();
    $this
      ->assertTrue(empty($ids), 'Transactions supported, and entity not found in database.');
  }
  else {

    // Check that the block exists in the database.
    $ids = \Drupal::entityQuery('entity_test')
      ->condition('name', 'fail_insert')
      ->execute();
    $this
      ->assertFalse(empty($ids), 'Transactions not supported, and entity found in database.');
  }
}