function EntityCrudHookTest::testEntityNGRollback

Tests rollback from failed insert in EntityNG.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php, line 525
Definition of Drupal\system\Tests\Entity\EntityCrudHookTest.

Class

EntityCrudHookTest
Tests invocation of hooks when performing an action.

Namespace

Drupal\system\Tests\Entity

Code

function testEntityNGRollback() {

  // Create a block.
  try {
    $entity = 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.');
  }
}