function MenuAccessTest::testMenuBlockLinksAccessCheck

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Menu/MenuAccessTest.php \Drupal\Tests\system\Functional\Menu\MenuAccessTest::testMenuBlockLinksAccessCheck()
  2. 10 core/modules/system/tests/src/Functional/Menu/MenuAccessTest.php \Drupal\Tests\system\Functional\Menu\MenuAccessTest::testMenuBlockLinksAccessCheck()
  3. 11.x core/modules/system/tests/src/Functional/Menu/MenuAccessTest.php \Drupal\Tests\system\Functional\Menu\MenuAccessTest::testMenuBlockLinksAccessCheck()

Tests menu link for route with access check.

See also

\Drupal\menu_test\Access\AccessCheck::access()

File

core/modules/system/tests/src/Functional/Menu/MenuAccessTest.php, line 41

Class

MenuAccessTest
Tests the route access checks on menu links.

Namespace

Drupal\Tests\system\Functional\Menu

Code

public function testMenuBlockLinksAccessCheck() {
    $this->drupalPlaceBlock('system_menu_block:account');
    // Test that there's link rendered on the route.
    $this->drupalGet('menu_test_access_check_session');
    $this->assertSession()
        ->linkExists('Test custom route access check');
    // Page is still accessible but there should be no menu link.
    $this->drupalGet('menu_test_access_check_session');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->linkNotExists('Test custom route access check');
    // Test that page is no more accessible.
    $this->drupalGet('menu_test_access_check_session');
    $this->assertSession()
        ->statusCodeEquals(403);
    // Check for access to a restricted local task from a default local task.
    $this->drupalGet('foo/asdf');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertLinkByHref('foo/asdf');
    $this->assertLinkByHref('foo/asdf/b');
    $this->assertNoLinkByHref('foo/asdf/c');
    // Attempt to access a restricted local task.
    $this->drupalGet('foo/asdf/c');
    $this->assertSession()
        ->statusCodeEquals(403);
    $elements = $this->xpath('//ul[@class=:class]/li/a[@href=:href]', [
        ':class' => 'tabs primary',
        ':href' => Url::fromRoute('menu_test.router_test1', [
            'bar' => 'asdf',
        ])->toString(),
    ]);
    $this->assertTrue(empty($elements), 'No tab linking to foo/asdf found');
    $this->assertNoLinkByHref('foo/asdf/b');
    $this->assertNoLinkByHref('foo/asdf/c');
}

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