function EndOfTransactionQueriesTest::testEntitySave
Same name in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php \Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySave()
- 10 core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php \Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySave()
- 11.x core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php \Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySave()
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Cache/ EndOfTransactionQueriesTest.php, line 66
Class
- EndOfTransactionQueriesTest
- Tests that cache tag invalidation queries are delayed to the end of transactions.
Namespace
Drupal\KernelTests\Core\CacheCode
public function testEntitySave() {
\Drupal::cache()->set('test_cache_pretransaction_foobar', 'something', Cache::PERMANENT, [
'foobar',
]);
\Drupal::cache()->set('test_cache_pretransaction_entity_test_list', 'something', Cache::PERMANENT, [
'entity_test_list',
]);
$entity = EntityTest::create([
'name' => $this->randomString(),
]);
\Drupal::database()->resetLoggedStatements();
$entity->save();
$executed_statements = \Drupal::database()->getLoggedStatements();
$last_statement_index = max(array_keys($executed_statements));
$cachetag_statements = array_keys($this->getStatementsForTable(\Drupal::database()->getLoggedStatements(), 'cachetags'));
$this->assertSame($last_statement_index - count($cachetag_statements) + 1, min($cachetag_statements), 'All of the last queries in the transaction are for the "cachetags" table.');
// Verify that a nested entity save occurred.
$this->assertSame('johndoe', User::load(1)->getAccountName());
// Cache reads occurring during a transaction that DO NOT depend on
// invalidated cache tags result in cache HITs. Similarly, cache writes that
// DO NOT depend on invalidated cache tags DO get written. Of course, if we
// read either one now, outside of the context of the transaction, we expect
// the same.
$this->assertNotEmpty(\Drupal::state()->get('delay_cache_tags_invalidation_entity_test_insert__pretransaction_foobar'));
$this->assertNotEmpty(\Drupal::cache()->get('delay_cache_tags_invalidation_entity_test_insert__during_transaction_foobar'));
$this->assertNotEmpty(\Drupal::state()->get('delay_cache_tags_invalidation_user_insert__during_transaction_foobar'));
$this->assertNotEmpty(\Drupal::cache()->get('test_cache_pretransaction_foobar'));
// Cache reads occurring during a transaction that DO depend on invalidated
// cache tags result in cache MISSes. Similarly, cache writes that DO depend
// on invalidated cache tags DO NOT get written. Of course, if we read
// either one now, outside of the context of the transaction, we expect the
// same.
$this->assertFalse(\Drupal::state()->get('delay_cache_tags_invalidation_entity_test_insert__pretransaction_entity_test_list'));
$this->assertFalse(\Drupal::cache()->get('delay_cache_tags_invalidation_entity_test_insert__during_transaction_entity_test_list'));
$this->assertFalse(\Drupal::state()->get('delay_cache_tags_invalidation_user_insert__during_transaction_entity_test_list'));
$this->assertFalse(\Drupal::cache()->get('test_cache_pretransaction_entity_test_list'));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.