function AliasTest::testWhitelist

Same name in other branches
  1. 9 core/modules/path_alias/tests/src/Kernel/AliasTest.php \Drupal\Tests\path_alias\Kernel\AliasTest::testWhitelist()
  2. 8.9.x core/modules/path_alias/tests/src/Kernel/AliasTest.php \Drupal\Tests\path_alias\Kernel\AliasTest::testWhitelist()

Tests the alias whitelist.

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 testWhitelist() : void {
    $memoryCounterBackend = new MemoryCounterBackend(\Drupal::service(TimeInterface::class));
    // Create AliasManager and Path object.
    $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container
        ->get('lock'), $this->container
        ->get('state'), $this->container
        ->get('path_alias.repository'));
    $aliasManager = new AliasManager($this->container
        ->get('path_alias.repository'), $whitelist, $this->container
        ->get('language_manager'), $memoryCounterBackend, $this->container
        ->get(TimeInterface::class));
    // No alias for user and admin yet, so should be NULL.
    $this->assertNull($whitelist->get('user'));
    $this->assertNull($whitelist->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($whitelist->get($this->randomMachineName()));
    // Add an alias for user/1, user should get whitelisted now.
    $this->createPathAlias('/user/1', '/' . $this->randomMachineName());
    $aliasManager->cacheClear();
    $this->assertTrue($whitelist->get('user'));
    $this->assertNull($whitelist->get('admin'));
    $this->assertNull($whitelist->get($this->randomMachineName()));
    // Add an alias for admin, both should get whitelisted now.
    $this->createPathAlias('/admin/something', '/' . $this->randomMachineName());
    $aliasManager->cacheClear();
    $this->assertTrue($whitelist->get('user'));
    $this->assertTrue($whitelist->get('admin'));
    $this->assertNull($whitelist->get($this->randomMachineName()));
    // Remove the user alias again, whitelist 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($whitelist->get('user'));
    $this->assertTrue($whitelist->get('admin'));
    $this->assertNull($whitelist->get($this->randomMachineName()));
    // Destruct the whitelist so that the caches are written.
    $whitelist->destruct();
    $this->assertEquals(1, $memoryCounterBackend->getCounter('set', 'path_alias_whitelist'));
    $memoryCounterBackend->resetCounter();
    // Re-initialize the whitelist using the same cache backend, should load
    // from cache.
    $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container
        ->get('lock'), $this->container
        ->get('state'), $this->container
        ->get('path_alias.repository'));
    $this->assertNull($whitelist->get('user'));
    $this->assertTrue($whitelist->get('admin'));
    $this->assertNull($whitelist->get($this->randomMachineName()));
    $this->assertEquals(1, $memoryCounterBackend->getCounter('get', 'path_alias_whitelist'));
    $this->assertEquals(0, $memoryCounterBackend->getCounter('set', 'path_alias_whitelist'));
    // Destruct the whitelist, should not attempt to write the cache again.
    $whitelist->destruct();
    $this->assertEquals(1, $memoryCounterBackend->getCounter('get', 'path_alias_whitelist'));
    $this->assertEquals(0, $memoryCounterBackend->getCounter('set', 'path_alias_whitelist'));
}

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