function AliasTest::testPrefixList

Tests the alias prefix.

File

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

Class

AliasTest
Tests path alias CRUD and lookup functionality.

Namespace

Drupal\Tests\path_alias\Kernel

Code

public function testPrefixList() : void {
    $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'));
    $aliasManager = new AliasManager($this->container
        ->get('path_alias.repository'), $prefix_list, $this->container
        ->get('language_manager'), $memoryCounterBackend, $this->container
        ->get(TimeInterface::class));
    // No alias for user and admin yet, so should be NULL.
    $this->assertNull($prefix_list->get('user'));
    $this->assertNull($prefix_list->get('admin'));
    // Non-existing path roots should be NULL too. Use a length of 7 to avoid
    // possible conflict with random aliases below.
    $this->assertNull($prefix_list->get($this->randomMachineName()));
    // Add an alias for user/1, user should get cached now.
    $this->createPathAlias('/user/1', '/' . $this->randomMachineName());
    $aliasManager->cacheClear();
    $this->assertTrue($prefix_list->get('user'));
    $this->assertNull($prefix_list->get('admin'));
    $this->assertNull($prefix_list->get($this->randomMachineName()));
    // Add an alias for admin, both should get cached now.
    $this->createPathAlias('/admin/something', '/' . $this->randomMachineName());
    $aliasManager->cacheClear();
    $this->assertTrue($prefix_list->get('user'));
    $this->assertTrue($prefix_list->get('admin'));
    $this->assertNull($prefix_list->get($this->randomMachineName()));
    // Remove the user alias again, prefix list entry should be removed.
    $path_alias_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('path_alias');
    $entities = $path_alias_storage->loadByProperties([
        'path' => '/user/1',
    ]);
    $path_alias_storage->delete($entities);
    $aliasManager->cacheClear();
    $this->assertNull($prefix_list->get('user'));
    $this->assertTrue($prefix_list->get('admin'));
    $this->assertNull($prefix_list->get($this->randomMachineName()));
    // Destruct the prefix list so that the caches are written.
    $prefix_list->destruct();
    $this->assertEquals(1, $memoryCounterBackend->getCounter('set', 'path_alias_prefix_list'));
    $memoryCounterBackend->resetCounter();
    // Re-initialize the prefix list using the same cache backend, should load
    // from 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->assertNull($prefix_list->get('user'));
    $this->assertTrue($prefix_list->get('admin'));
    $this->assertNull($prefix_list->get($this->randomMachineName()));
    $this->assertEquals(1, $memoryCounterBackend->getCounter('get', 'path_alias_prefix_list'));
    $this->assertEquals(0, $memoryCounterBackend->getCounter('set', 'path_alias_prefix_list'));
    // Destruct the prefix list, should not attempt to write the cache again.
    $prefix_list->destruct();
    $this->assertEquals(1, $memoryCounterBackend->getCounter('get', 'path_alias_prefix_list'));
    $this->assertEquals(0, $memoryCounterBackend->getCounter('set', 'path_alias_prefix_list'));
}

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