function NavigationLinkBlockTest::testNavigationLinkBlockCache

Test output of the link navigation with regards to caching and contents.

File

core/modules/navigation/tests/src/Functional/NavigationLinkBlockTest.php, line 68

Class

NavigationLinkBlockTest
Tests for \Drupal\navigation\Plugin\Block\NavigationLinkBlockTest.

Namespace

Drupal\Tests\navigation\Functional

Code

public function testNavigationLinkBlockCache() : void {
    $label = 'Admin Main Page';
    $link_title = 'Navigation Settings';
    $link_uri = '/admin/config/user-interface/navigation/settings';
    $link_icon = 'admin-link';
    $this->appendNavigationLinkBlock($label, $link_title, 'internal:' . $link_uri, $link_icon);
    // Verify some basic cacheability metadata. Ensures that we're not doing
    // anything so egregious as to upset expected caching behavior. In this
    // case, as an anonymous user, we should have zero effect on the page.
    $test_page_url = Url::fromRoute('test_page_test.test_page');
    $this->verifyPageCache($test_page_url, 'MISS');
    $this->verifyPageCache($test_page_url, 'HIT');
    // Login as a limited access user, and verify that the dynamic page cache
    // is working as expected.
    $this->drupalLogin($this->normalUser);
    $this->verifyDynamicPageCache($test_page_url, 'MISS');
    $this->verifyDynamicPageCache($test_page_url, 'HIT');
    // We should not see the admin page link in the page.
    $link_selector = '.admin-toolbar__item .toolbar-button--icon--' . $link_icon;
    $this->assertSession()
        ->elementNotExists('css', $link_selector);
    $this->assertSession()
        ->pageTextNotContains($link_title);
    $this->assertSession()
        ->pageTextNotContains($label);
    // Login as a different user, UI should update.
    $this->drupalLogin($this->adminUser);
    $this->verifyDynamicPageCache($test_page_url, 'MISS');
    $this->verifyDynamicPageCache($test_page_url, 'HIT');
    $this->drupalGet(Url::fromRoute('navigation.settings'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->elementExists('css', $link_selector);
    $this->assertSession()
        ->pageTextContains($link_title);
    $this->assertSession()
        ->pageTextContains($label);
    $this->assertSession()
        ->elementTextContains('css', $link_selector, $link_title);
    // The link should link to the admin page.
    $link = $this->getSession()
        ->getPage()
        ->find('named', [
        'link',
        $link_title,
    ]);
    $this->assertStringContainsString('/admin/config/user-interface/navigation/settings', $link->getAttribute('href'));
}

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