function PathAliasTest::testPathCache

Same name in other branches
  1. 9 core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testPathCache()
  2. 10 core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testPathCache()
  3. 11.x core/modules/path/tests/src/Functional/PathAliasTest.php \Drupal\Tests\path\Functional\PathAliasTest::testPathCache()

Tests the path cache.

File

core/modules/path/tests/src/Functional/PathAliasTest.php, line 46

Class

PathAliasTest
Add, edit, delete, and change alias and verify its consistency in the database.

Namespace

Drupal\Tests\path\Functional

Code

public function testPathCache() {
    // Create test node.
    $node1 = $this->drupalCreateNode();
    // Create alias.
    $edit = [];
    $edit['path[0][value]'] = '/node/' . $node1->id();
    $edit['alias[0][value]'] = '/' . $this->randomMachineName(8);
    $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
    // Check the path alias whitelist cache.
    $whitelist = \Drupal::cache('bootstrap')->get('path_alias_whitelist');
    $this->assertTrue($whitelist->data['node']);
    $this->assertFalse($whitelist->data['admin']);
    // Visit the system path for the node and confirm a cache entry is
    // created.
    \Drupal::cache('data')->deleteAll();
    // Make sure the path is not converted to the alias.
    $this->drupalGet(trim($edit['path[0][value]'], '/'), [
        'alias' => TRUE,
    ]);
    $this->assertNotEmpty(\Drupal::cache('data')->get('preload-paths:' . $edit['path[0][value]']), 'Cache entry was created.');
    // Visit the alias for the node and confirm a cache entry is created.
    \Drupal::cache('data')->deleteAll();
    // @todo Remove this once https://www.drupal.org/node/2480077 lands.
    Cache::invalidateTags([
        'rendered',
    ]);
    $this->drupalGet(trim($edit['alias[0][value]'], '/'));
    $this->assertNotEmpty(\Drupal::cache('data')->get('preload-paths:' . $edit['path[0][value]']), 'Cache entry was created.');
}

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