function EndOfTransactionQueriesTest::testEntitySave

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

Tests an entity save.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Cache

Code

public function testEntitySave() : void {
    \Drupal::cache()->set('test_cache_pre-transaction_foobar', 'something', Cache::PERMANENT, [
        'foobar',
    ]);
    \Drupal::cache()->set('test_cache_pre-transaction_entity_test_list', 'something', Cache::PERMANENT, [
        'entity_test_list',
    ]);
    $entity = EntityTest::create([
        'name' => $this->randomString(),
    ]);
    Database::startLog('testEntitySave');
    $entity->save();
    // Entity save should have deferred cache invalidation to after transaction
    // completion for the "entity_test_list", "entity_test_list:entity_test"
    // and "4xx-response" tags. Since cache invalidation is a MERGE database
    // operation, and in core drivers each MERGE is split in two SELECT and
    // INSERT|UPDATE operations, we expect the last 6 logged database queries
    // to be related to the {cachetags} table.
    $expected_tail_length = 6;
    $executed_statements = [];
    foreach (Database::getLog('testEntitySave') as $log) {
        // Exclude transaction related statements from the log.
        if (str_starts_with($log['query'], 'ROLLBACK TO SAVEPOINT ') || str_starts_with($log['query'], 'RELEASE SAVEPOINT ') || str_starts_with($log['query'], 'SAVEPOINT ')) {
            continue;
        }
        $executed_statements[] = $log['query'];
    }
    $expected_post_transaction_statements = array_keys(array_fill(array_key_last($executed_statements) - $expected_tail_length + 1, $expected_tail_length, TRUE));
    $cachetag_statements = $this->getStatementsForTable($executed_statements, 'cachetags');
    $tail_cachetag_statements = array_keys(array_slice($cachetag_statements, count($cachetag_statements) - $expected_tail_length, $expected_tail_length, TRUE));
    $this->assertSame($expected_post_transaction_statements, $tail_cachetag_statements);
    // Verify that a nested entity save occurred.
    $this->assertSame('john doe', 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__pre-transaction_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_pre-transaction_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__pre-transaction_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_pre-transaction_entity_test_list'));
}

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