function NavigationUserBlockTest::testNavigationUserBlock

Test output of user navigation block with regards to contents.

File

core/modules/navigation/tests/src/FunctionalJavascript/NavigationUserBlockTest.php, line 72

Class

NavigationUserBlockTest
Tests for \Drupal\navigation\Plugin\NavigationBlock\NavigationUserBlock.

Namespace

Drupal\Tests\navigation\FunctionalJavascript

Code

public function testNavigationUserBlock() : void {
  $test_page_url = Url::fromRoute('test_page_test.test_page');
  // Login as a limited access user, and verify that the username is displayed
  // correctly.
  $this->drupalLogin($this->normalUser);
  $this->drupalGet($test_page_url);
  // Wait for the default 'My Account' text to be replaced.
  $this->getSession()
    ->getPage()
    ->waitFor(10, function (Element $page) {
    return $page->find('css', '[aria-controls="navigation-link-navigationuser-linksuserwrapper"] > .toolbar-button__label')
      ->getText() !== 'My Account';
  });
  // We should see the users name in the navigation menu.
  $rendered_user_name = $this->cssSelect('[aria-controls="navigation-link-navigationuser-linksuserwrapper"] > .toolbar-button__label')[0]
    ->getText();
  $this->assertEquals($this->normalUser
    ->getDisplayName(), $rendered_user_name);
  // Login as an admin access user, and verify that the username is displayed
  // correctly.
  $this->drupalLogin($this->adminUser);
  $this->drupalGet($test_page_url);
  // Wait for the default 'My Account' text to be replaced.
  $this->getSession()
    ->getPage()
    ->waitFor(10, function (Element $page) {
    return $page->find('css', '[aria-controls="navigation-link-navigationuser-linksuserwrapper"] > .toolbar-button__label')
      ->getText() !== 'My Account';
  });
  // We should see the users name in the navigation menu.
  $rendered_user_name = $this->cssSelect('[aria-controls="navigation-link-navigationuser-linksuserwrapper"] > .toolbar-button__label')[0]
    ->getText();
  $this->assertEquals($this->adminUser
    ->getDisplayName(), $rendered_user_name);
  // Change the users name, assert that the changes reflect in the navigation.
  $new_username = $this->randomMachineName();
  $this->drupalGet('user/' . $this->adminUser
    ->id() . '/edit');
  $this->submitForm([
    'name' => $new_username,
  ], 'Save');
  // Wait for the default 'My Account' text to be replaced.
  $this->getSession()
    ->getPage()
    ->waitFor(10, function (Element $page) {
    return $page->find('css', '[aria-controls="navigation-link-navigationuser-linksuserwrapper"] > .toolbar-button__label')
      ->getText() !== 'My Account';
  });
  // We should see the users name in the navigation menu.
  $rendered_user_name = $this->cssSelect('[aria-controls="navigation-link-navigationuser-linksuserwrapper"] > .toolbar-button__label')[0]
    ->getText();
  $this->assertEquals($new_username, $rendered_user_name);
}

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