function EntityCrudHookTest::testEntityRollback

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testEntityRollback()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testEntityRollback()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testEntityRollback()

Tests rollback from failed entity save.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php, line 547

Class

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

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityRollback() {
    // Create a block.
    try {
        EntityTest::create([
            'name' => 'fail_insert',
        ])->save();
        $this->fail('Expected exception has not been thrown.');
    } catch (\Exception $e) {
        // Expected exception; just continue testing.
    }
    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.');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.