You are here

function AliasTest::testWhitelist in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Path/AliasTest.php \Drupal\system\Tests\Path\AliasTest::testWhitelist()

Tests the alias whitelist.

File

core/modules/system/src/Tests/Path/AliasTest.php, line 163
Contains \Drupal\system\Tests\Path\AliasTest.

Class

AliasTest
Tests path alias CRUD and lookup functionality.

Namespace

Drupal\system\Tests\Path

Code

function testWhitelist() {

  // Prepare database table.
  $connection = Database::getConnection();
  $this->fixtures
    ->createTables($connection);
  $memoryCounterBackend = new MemoryCounterBackend('default');

  // Create AliasManager and Path object.
  $aliasStorage = new AliasStorage($connection, $this->container
    ->get('module_handler'));
  $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container
    ->get('lock'), $this->container
    ->get('state'), $aliasStorage);
  $aliasManager = new AliasManager($aliasStorage, $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.
  $aliasStorage
    ->save('/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.
  $aliasStorage
    ->save('/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.
  $aliasStorage
    ->delete(array(
    'source' => '/user/1',
  ));
  $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'), $aliasStorage);
  $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);
}