function MenuLinkTreeTest::testUserLoginAndUserLogoutLinks

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Menu/MenuLinkTreeTest.php \Drupal\KernelTests\Core\Menu\MenuLinkTreeTest::testUserLoginAndUserLogoutLinks()

Tests user/login and user/logout links.

File

core/tests/Drupal/KernelTests/Core/Menu/MenuLinkTreeTest.php, line 142

Class

MenuLinkTreeTest
Tests the menu link tree.

Namespace

Drupal\KernelTests\Core\Menu

Code

public function testUserLoginAndUserLogoutLinks() : void {
    $account_switcher = $this->container
        ->get('account_switcher');
    $login_menu_link = MenuLinkMock::create([
        'id' => 'user_login_example',
        'route_name' => 'user.login',
    ]);
    $logout_menu_link = MenuLinkMock::create([
        'id' => 'user_logout_example',
        'route_name' => 'user.logout',
    ]);
    $this->menuLinkManager
        ->addDefinition($login_menu_link->getPluginId(), $login_menu_link->getPluginDefinition());
    $this->menuLinkManager
        ->addDefinition($logout_menu_link->getPluginId(), $logout_menu_link->getPluginDefinition());
    // Returns the accessible links from transformed 'mock' menu tree.
    $get_accessible_links = function () {
        $parameters = new MenuTreeParameters();
        $manipulators = [
            [
                'callable' => 'menu.default_tree_manipulators:checkAccess',
            ],
        ];
        $tree = $this->linkTree
            ->load('mock', $parameters);
        $this->linkTree
            ->transform($tree, $manipulators);
        return array_keys(array_filter($tree, function (MenuLinkTreeElement $element) {
            return !$element->link instanceof InaccessibleMenuLink;
        }));
    };
    // Check that anonymous can only access the login link.
    $account_switcher->switchTo(new AnonymousUserSession());
    $this->assertSame([
        'user_login_example',
    ], $get_accessible_links());
    // Ensure that also user 1 does not see the login link.
    $account_switcher->switchTo(new UserSession([
        'uid' => 1,
    ]));
    $this->assertSame([
        'user_logout_example',
    ], $get_accessible_links());
}

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