function ToolbarActiveTrailTest::testToolbarActiveTrail

Same name and namespace in other branches
  1. 11.x core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarActiveTrailTest.php \Drupal\Tests\toolbar\FunctionalJavascript\ToolbarActiveTrailTest::testToolbarActiveTrail()

Tests that the active trail is maintained even when traversed deeper.

@testWith ["vertical"]
["horizontal"]

Parameters

string $orientation: The toolbar orientation.

Throws

\Behat\Mink\Exception\ElementNotFoundException

File

core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarActiveTrailTest.php, line 51

Class

ToolbarActiveTrailTest
Tests that the active trail is maintained in the toolbar.

Namespace

Drupal\Tests\toolbar\FunctionalJavascript

Code

public function testToolbarActiveTrail(string $orientation) : void {
  $page = $this->getSession()
    ->getPage();
  $assert_session = $this->assertSession();
  $this->drupalGet('<front>');
  $this->assertNotEmpty($this->assertSession()
    ->waitForElement('css', 'body.toolbar-horizontal'));
  $this->assertNotEmpty($this->assertSession()
    ->waitForElementVisible('css', '.toolbar-tray'));
  $this->assertSession()
    ->waitForElementRemoved('css', '.toolbar-loading');
  $this->assertNotEmpty($assert_session->waitForElementVisible('css', '#toolbar-item-administration.is-active'));
  // If testing for vertical orientation of the toolbar then switch to it.
  if ($orientation === 'vertical') {
    $page->pressButton('Vertical orientation');
  }
  // Traverse deeper.
  $this->clickLink('Structure');
  $this->clickLink('Content types');
  $this->clickLink('Manage fields');
  $this->clickLink('Edit');
  if ($orientation === 'vertical') {
    $this->assertNotEmpty($assert_session->waitForElementVisible('named', [
      'link',
      'Structure',
    ]));
    // Assert that menu-item--active-trail was maintained.
    $this->assertTrue($assert_session->waitForElementVisible('named', [
      'link',
      'Structure',
    ])
      ->getParent()
      ->getParent()
      ->hasClass('menu-item--active-trail'));
    $this->assertTrue($assert_session->waitForElementVisible('named', [
      'link',
      'Content types',
    ])
      ->getParent()
      ->getParent()
      ->hasClass('menu-item--active-trail'));
    // Change orientation and check focus is maintained.
    $page->pressButton('Horizontal orientation');
    $this->assertTrue($assert_session->waitForElementVisible('css', '#toolbar-link-system-admin_structure')
      ->hasClass('is-active'));
  }
  else {
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '#toolbar-link-system-admin_structure'));
    // Assert that is-active was maintained.
    $this->assertTrue($assert_session->waitForElementVisible('css', '#toolbar-link-system-admin_structure')
      ->hasClass('is-active'));
    // Change orientation and check focus is maintained.
    $page->pressButton('Vertical orientation');
    // Introduce a delay to let the focus load.
    $this->getSession()
      ->wait(150);
    $this->assertTrue($assert_session->waitForElementVisible('named', [
      'link',
      'Structure',
    ])
      ->getParent()
      ->getParent()
      ->hasClass('menu-item--active-trail'));
    $this->assertTrue($assert_session->waitForElementVisible('named', [
      'link',
      'Content types',
    ])
      ->getParent()
      ->getParent()
      ->hasClass('menu-item--active-trail'));
  }
}

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