function LinksetControllerTest::testUserAccountMenu

Same name in other branches
  1. 11.x core/modules/system/tests/src/Functional/Menu/LinksetControllerTest.php \Drupal\Tests\system\Functional\Menu\LinksetControllerTest::testUserAccountMenu()

Tests that the user account menu behaves as it should.

The account menu is a good test case because it provides a restricted, YAML-defined link ("My account") and a dynamic code-defined link ("Log in/out")

File

core/modules/system/tests/src/Functional/Menu/LinksetControllerTest.php, line 279

Class

LinksetControllerTest
Tests the behavior of the linkset controller.

Namespace

Drupal\Tests\system\Functional\Menu

Code

public function testUserAccountMenu() : void {
    $this->enableEndpoint(TRUE);
    $expected_cacheability = new CacheableMetadata();
    $expected_cacheability->addCacheContexts([
        'user.roles:authenticated',
    ]);
    $expected_cacheability->addCacheTags([
        'config:system.menu.account',
        'http_response',
    ]);
    $response = $this->doRequest('GET', Url::fromUri('base:/system/menu/account/linkset'));
    $this->assertDrupalResponseCacheability('MISS', $expected_cacheability, $response);
    $link_items = Json::decode((string) $response->getBody())['linkset'][0]['item'];
    $titles = array_column($link_items, 'title');
    $this->assertContains('Log in', $titles);
    $this->assertNotContains('Log out', $titles);
    $this->assertNotContains('My account', $titles);
    // Redo the request, but with an authenticated user.
    $response = $this->doRequest('GET', Url::fromUri('base:/system/menu/account/linkset'), 200, $this->authorAccount);
    // The expected cache tags must be updated.
    $expected_cacheability->setCacheTags([
        'config:system.menu.account',
        'http_response',
    ]);
    // Authenticated requests do not use the page cache, so a "HIT" or "MISS"
    // isn't expected either.
    $this->assertDrupalResponseCacheability(FALSE, $expected_cacheability, $response);
    $link_items = Json::decode((string) $response->getBody())['linkset'][0]['item'];
    $titles = array_column($link_items, 'title');
    $this->assertContains('Log out', $titles);
    $this->assertContains('My account', $titles);
    $this->assertNotContains('Log in', $titles);
}

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