function AliasTest::testPrefixListCacheDeletionMidRequest

Tests situation where the prefix list cache is deleted mid-request.

File

core/modules/path_alias/tests/src/Kernel/AliasTest.php, line 424

Class

AliasTest
Tests path alias CRUD and lookup functionality.

Namespace

Drupal\Tests\path_alias\Kernel

Code

public function testPrefixListCacheDeletionMidRequest() {
    $memoryCounterBackend = new MemoryCounterBackend(\Drupal::service(TimeInterface::class));
    // Create AliasManager and Path object.
    $prefix_list = new AliasPrefixList('path_alias_prefix_list', $memoryCounterBackend, $this->container
        ->get('lock'), $this->container
        ->get('state'), $this->container
        ->get('path_alias.repository'));
    // Prefix list cache should not exist at all yet.
    $this->assertFalse($memoryCounterBackend->get('path_alias_prefix_list'));
    // Add some aliases for both menu routes we have.
    $this->createPathAlias('/admin/something', '/' . $this->randomMachineName());
    $this->createPathAlias('/user/something', '/' . $this->randomMachineName());
    // Lookup admin path in prefix list. It will query the DB and figure out
    // that it indeed has an alias, and add it to the internal prefix list and
    // flag it to be persisted to cache.
    $this->assertTrue($prefix_list->get('admin'));
    // Destruct the prefix list so it persists its cache.
    $prefix_list->destruct();
    $this->assertEquals(1, $memoryCounterBackend->getCounter('set', 'path_alias_prefix_list'));
    // Cache data should have data for 'user' and 'admin', even though just
    // 'admin' was looked up. This is because the cache is primed with all
    // menu router base paths.
    $this->assertEquals([
        'user' => FALSE,
        'admin' => TRUE,
    ], $memoryCounterBackend->get('path_alias_prefix_list')->data);
    $memoryCounterBackend->resetCounter();
    // Re-initialize the prefix list and lookup an alias for the 'user' path.
    // Prefix list should load data from its cache, see that it hasn't done a
    // check for 'user' yet, perform the check, then mark the result to be
    // persisted to cache.
    $prefix_list = new AliasPrefixList('path_alias_prefix_list', $memoryCounterBackend, $this->container
        ->get('lock'), $this->container
        ->get('state'), $this->container
        ->get('path_alias.repository'));
    $this->assertTrue($prefix_list->get('user'));
    // Delete the prefix list cache. This could happen from an outside process,
    // like a code deployment that performs a cache rebuild.
    $memoryCounterBackend->delete('path_alias_prefix_list');
    // Destruct prefix list so it attempts to save the prefix list data to
    // cache. However it should recognize that the previous cache entry was
    // deleted from underneath it and not save anything to cache, to protect
    // from cache corruption.
    $prefix_list->destruct();
    $this->assertEquals(0, $memoryCounterBackend->getCounter('set', 'path_alias_prefix_list'));
    $this->assertFalse($memoryCounterBackend->get('path_alias_prefix_list'));
    $memoryCounterBackend->resetCounter();
}

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