function NavigationTopBarTest::assertActionsWeight

Asserts that top bar actions respect local tasks weights.

Parameters

\Behat\Mink\Element\NodeElement $toolbar_links: Action links to assert.

1 call to NavigationTopBarTest::assertActionsWeight()
NavigationTopBarTest::testTopBarVisibility in core/modules/navigation/tests/src/Functional/NavigationTopBarTest.php
Tests the top bar visibility.

File

core/modules/navigation/tests/src/Functional/NavigationTopBarTest.php, line 125

Class

NavigationTopBarTest
Tests the top bar functionality.

Namespace

Drupal\Tests\navigation\Functional

Code

protected function assertActionsWeight(NodeElement $toolbar_links) : void {
  // Displayed action links in the top bar.
  $displayed_links = array_map(fn($link) => $link->getText(), $toolbar_links->findAll('css', 'li'));
  // Extract the route name from the URL.
  $current_url = $this->getSession()
    ->getCurrentUrl();
  // Convert alias to system path.
  $path = parse_url($current_url, PHP_URL_PATH);
  if ($GLOBALS['base_path'] !== '/') {
    $path = str_replace($GLOBALS['base_path'], '/', $path);
  }
  // Get local tasks for the current route.
  $entity_local_tasks = \Drupal::service('plugin.manager.menu.local_task')->getLocalTasks(Url::fromUserInput($path)->getRouteName());
  // Sort order of tabs based on their weights.
  uasort($entity_local_tasks['tabs'], [
    SortArray::class,
    'sortByWeightProperty',
  ]);
  // Extract the expected order based on sorted weights.
  $expected_order = array_values(array_map(fn($task) => $task['#link']['title'], $entity_local_tasks['tabs']));
  // Filter out elements not in displayed_links.
  $expected_order = array_values(array_filter($expected_order, fn($title) => in_array($title, $displayed_links, TRUE)));
  // Ensure that the displayed links match the expected order.
  $this->assertSame($expected_order, $displayed_links, 'Local tasks are displayed in the correct order based on their weights.');
}

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