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. 10 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 116

Class

AliasTest
Tests path alias CRUD and lookup functionality.

Namespace

Drupal\Tests\path_alias\Kernel

Code

public function testWhitelist() {
    $memoryCounterBackend = new MemoryCounterBackend();
    // 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);
    // 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->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 1);
    $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->assertEqual($memoryCounterBackend->getCounter('get', 'path_alias_whitelist'), 1);
    $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);
    // Destruct the whitelist, should not attempt to write the cache again.
    $whitelist->destruct();
    $this->assertEqual($memoryCounterBackend->getCounter('get', 'path_alias_whitelist'), 1);
    $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);
}

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