function EndOfTransactionQueriesTest::testEntitySaveRollback

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php \Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySaveRollback()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php \Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySaveRollback()
  3. 11.x core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php \Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySaveRollback()

Tests an entity save rollback.

File

core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php, line 119

Class

EndOfTransactionQueriesTest
Tests delaying of cache tag invalidation queries to the end of transactions.

Namespace

Drupal\KernelTests\Core\Cache

Code

public function testEntitySaveRollback() : void {
    \Drupal::cache()->set('test_cache_pretransaction_entity_test_list', 'something', Cache::PERMANENT, [
        'entity_test_list',
    ]);
    \Drupal::cache()->set('test_cache_pretransaction_user_list', 'something', Cache::PERMANENT, [
        'user_list',
    ]);
    \Drupal::state()->set('delay_cache_tags_invalidation_exception', TRUE);
    try {
        EntityTest::create([
            'name' => $this->randomString(),
        ])
            ->save();
        $this->fail('Exception not thrown');
    } catch (\Exception $e) {
        $this->assertEquals('Abort entity save to trigger transaction rollback.', $e->getMessage());
    }
    // The cache has not been invalidated.
    $this->assertNotEmpty(\Drupal::cache()->get('test_cache_pretransaction_entity_test_list'));
    $this->assertNotEmpty(\Drupal::cache()->get('test_cache_pretransaction_user_list'));
    // Save a user, that should invalidate the cache tagged with user_list but
    // not the one with entity_test_list.
    User::create([
        'name' => 'john doe',
        'status' => 1,
    ])->save();
    $this->assertNotEmpty(\Drupal::cache()->get('test_cache_pretransaction_entity_test_list'));
    $this->assertFalse(\Drupal::cache()->get('test_cache_pretransaction_user_list'));
}

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