function MenuActiveTrailTest::provider

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Menu/MenuActiveTrailTest.php \Drupal\Tests\Core\Menu\MenuActiveTrailTest::provider()
  2. 10 core/tests/Drupal/Tests/Core/Menu/MenuActiveTrailTest.php \Drupal\Tests\Core\Menu\MenuActiveTrailTest::provider()
  3. 11.x core/tests/Drupal/Tests/Core/Menu/MenuActiveTrailTest.php \Drupal\Tests\Core\Menu\MenuActiveTrailTest::provider()

Provides test data for all test methods.

Return value

array Returns a list of test data of which each is an array containing the following elements:

  • request: A request object.
  • links: An array of menu links keyed by ID.
  • menu_name: The active menu name.
  • expected_link: The expected active link for the given menu.

File

core/tests/Drupal/Tests/Core/Menu/MenuActiveTrailTest.php, line 96

Class

MenuActiveTrailTest
Tests the active menu trail service.

Namespace

Drupal\Tests\Core\Menu

Code

public function provider() {
    $data = [];
    $mock_route = new Route('');
    $request = new Request();
    $request->attributes
        ->set(RouteObjectInterface::ROUTE_NAME, 'baby_llama');
    $request->attributes
        ->set(RouteObjectInterface::ROUTE_OBJECT, $mock_route);
    $request->attributes
        ->set('_raw_variables', new ParameterBag([]));
    $link_1 = MenuLinkMock::create([
        'id' => 'baby_llama_link_1',
        'route_name' => 'baby_llama',
        'title' => 'Baby llama',
        'parent' => 'mama_llama_link',
    ]);
    $link_2 = MenuLinkMock::create([
        'id' => 'baby_llama_link_2',
        'route_name' => 'baby_llama',
        'title' => 'Baby llama',
        'parent' => 'papa_llama_link',
    ]);
    // @see \Drupal\Core\Menu\MenuLinkManagerInterface::getParentIds()
    $link_1_parent_ids = [
        'baby_llama_link_1',
        'mama_llama_link',
        '',
    ];
    $empty_active_trail = [
        '',
    ];
    // No active link is returned when zero links match the current route.
    $data[] = [
        $request,
        [],
        $this->randomMachineName(),
        NULL,
        $empty_active_trail,
    ];
    // The first (and only) matching link is returned when one link matches the
    // current route.
    $data[] = [
        $request,
        [
            'baby_llama_link_1' => $link_1,
        ],
        $this->randomMachineName(),
        $link_1,
        $link_1_parent_ids,
    ];
    // The first of multiple matching links is returned when multiple links
    // match the current route, where "first" is determined by sorting by key.
    $data[] = [
        $request,
        [
            'baby_llama_link_1' => $link_1,
            'baby_llama_link_2' => $link_2,
        ],
        $this->randomMachineName(),
        $link_1,
        $link_1_parent_ids,
    ];
    // No active link is returned in case of a 403.
    $request = new Request();
    $request->attributes
        ->set('_exception_statuscode', 403);
    $data[] = [
        $request,
        FALSE,
        $this->randomMachineName(),
        NULL,
        $empty_active_trail,
    ];
    // No active link is returned when the route name is missing.
    $request = new Request();
    $data[] = [
        $request,
        FALSE,
        $this->randomMachineName(),
        NULL,
        $empty_active_trail,
    ];
    return $data;
}

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