function UserNavigationBlockTest::testUserNavigationBlock

Same name in other branches
  1. 11.x core/modules/navigation/tests/src/Functional/UserNavigationBlockTest.php \Drupal\Tests\navigation\Functional\UserNavigationBlockTest::testUserNavigationBlock()

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

File

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

Class

UserNavigationBlockTest
Tests for \Drupal\navigation\Plugin\NavigationBlock\UserNavigationBlock.

Namespace

Drupal\Tests\navigation\Functional

Code

public function testUserNavigationBlock() {
    // 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 see the users name in the navigation menu.
    $rendered_user_name = $this->cssSelect('[aria-controls="admin-toolbar-user-menu"] > .toolbar-button__label')[0]
        ->getText();
    $this->assertEquals((string) $this->normalUser
        ->getDisplayName(), $rendered_user_name);
    // We should see all three user links in the page.
    $link_labels = [
        'View profile',
        'Edit profile',
        'Log out',
    ];
    $block = $this->assertSession()
        ->elementExists('css', sprintf('.toolbar-block:contains("%s")', $rendered_user_name));
    foreach ($link_labels as $link_label) {
        $links = $block->findAll('named', [
            'link',
            $link_label,
        ]);
        $this->assertCount(1, $links, sprintf('Found %s links with label %s.', count($links), $link_label));
    }
    // The Edit profile link should link to the users edit profile page.
    $links = $this->getSession()
        ->getPage()
        ->findAll('named', [
        'link',
        'Edit profile',
    ]);
    $this->assertStringContainsString(sprintf('/user/%s/edit', $this->normalUser
        ->id()), $links[0]->getAttribute('href'));
    // Login as a different user, UI should update.
    $this->drupalLogin($this->adminUser);
    $this->verifyDynamicPageCache($test_page_url, 'MISS');
    $this->verifyDynamicPageCache($test_page_url, 'HIT');
    $rendered_user_name = $this->cssSelect('[aria-controls="admin-toolbar-user-menu"] > .toolbar-button__label')[0]
        ->getText();
    $this->assertEquals((string) $this->adminUser
        ->getDisplayName(), $rendered_user_name);
    // The Edit profile link should link to the users edit profile page.
    $links = $this->getSession()
        ->getPage()
        ->findAll('named', [
        'link',
        'Edit profile',
    ]);
    $this->assertStringContainsString(sprintf('/user/%s/edit', $this->adminUser
        ->id()), $links[0]->getAttribute('href'));
}

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