View source
<?php
namespace Drupal\KernelTests\Core\Cache;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\DatabaseBackendFactory;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\Reference;
class EndOfTransactionQueriesTest extends KernelTestBase {
protected static $modules = [
'delay_cache_tags_invalidation',
'entity_test',
'system',
'user',
];
protected function setUp() : void {
parent::setUp();
if (!class_exists($this
->getDatabaseConnectionInfo()['default']['namespace'] . '\\Connection')) {
$this
->markTestSkipped(sprintf('No logging override exists for the %s database driver. Create it, subclass this test class and override ::getDatabaseConnectionInfo().', $this
->getDatabaseConnectionInfo()['default']['driver']));
}
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('user');
Cache::invalidateTags([
$this
->randomString(),
]);
}
public function register(ContainerBuilder $container) {
parent::register($container);
$container
->register('cache_factory', DatabaseBackendFactory::class)
->addArgument(new Reference('database'))
->addArgument(new Reference('cache_tags.invalidator.checksum'))
->addArgument(new Reference('settings'));
}
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.');
$this
->assertSame('johndoe', User::load(1)
->getAccountName());
$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'));
$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'));
}
public function testEntitySaveRollback() {
\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());
}
$this
->assertNotEmpty(\Drupal::cache()
->get('test_cache_pretransaction_entity_test_list'));
$this
->assertNotEmpty(\Drupal::cache()
->get('test_cache_pretransaction_user_list'));
User::create([
'name' => 'johndoe',
'status' => 1,
])
->save();
$this
->assertNotEmpty(\Drupal::cache()
->get('test_cache_pretransaction_entity_test_list'));
$this
->assertFalse(\Drupal::cache()
->get('test_cache_pretransaction_user_list'));
}
protected function getStatementsForTable(array $statements, $table_name) {
$tables = array_filter(array_map([
$this,
'statementToTableName',
], $statements));
return array_filter($tables, function ($table_for_statement) use ($table_name) {
return $table_for_statement === $table_name;
});
}
protected static function statementToTableName($statement) {
if (preg_match('/.*\\{([^\\}]+)\\}.*/', $statement, $matches)) {
return $matches[1];
}
else {
return NULL;
}
}
protected function getDatabaseConnectionInfo() {
$info = parent::getDatabaseConnectionInfo();
$info['default']['namespace'] = '\\Drupal\\database_statement_monitoring_test\\' . $info['default']['driver'];
return $info;
}
}