function MenuAccessTest::assertUserRoutesAccess

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

Asserts which routes a user has access to.

Parameters

\Drupal\Core\Session\AccountInterface $user: The user account for which to check access.

array $expectedAccessibleRoutes: The routes the user should have access to.

array $allRoutes: The routes to check.

1 call to MenuAccessTest::assertUserRoutesAccess()
MenuAccessTest::testSystemAdminMenuBlockAccessCheck in core/modules/system/tests/src/Functional/Menu/MenuAccessTest.php
Test routes implementing _access_admin_menu_block_page.

File

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

Class

MenuAccessTest
Tests the route access checks on menu links.

Namespace

Drupal\Tests\system\Functional\Menu

Code

private function assertUserRoutesAccess(AccountInterface $user, array $expectedAccessibleRoutes, array $allRoutes) : void {
    $this->drupalLogin($user);
    $expectedInaccessibleRoutes = array_diff($allRoutes, $expectedAccessibleRoutes);
    $this->assertEmpty(array_diff($expectedAccessibleRoutes, $allRoutes));
    $actualAccessibleRoutes = [];
    $actualInaccessibleRoutes = [];
    foreach ($allRoutes as $route) {
        $this->drupalGet(Url::fromRoute($route));
        $requestStatus = $this->getSession()
            ->getStatusCode();
        switch ($requestStatus) {
            case 200:
                $actualAccessibleRoutes[] = $route;
                break;
            case 403:
                $actualInaccessibleRoutes[] = $route;
                break;
            default:
                throw new \UnexpectedValueException("Unexpected status code {$requestStatus} for route {$route}");
        }
    }
    $debug = fn($accessibleRoutes, $inaccessibleRoutes) => "\nAccessible routes: " . implode(', ', $accessibleRoutes) . "\nInaccessible routes: " . implode(', ', $inaccessibleRoutes);
    $expected = $debug($expectedAccessibleRoutes, $expectedInaccessibleRoutes);
    $actual = $debug($actualAccessibleRoutes, $actualInaccessibleRoutes);
    $this->assertSession()
        ->assert($expected === $actual, "Routes do not match. \nExpected routes:{$expected}\nActual routes: {$actual}");
}

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