You are here

public function UrlIntegrationTest::testAccess in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Routing/UrlIntegrationTest.php \Drupal\system\Tests\Routing\UrlIntegrationTest::testAccess()

Ensures that the access() method on \Drupal\Core\Url objects works.

File

core/modules/system/src/Tests/Routing/UrlIntegrationTest.php, line 41
Contains \Drupal\system\Tests\Routing\UrlIntegrationTest.

Class

UrlIntegrationTest
Tests the URL object integration into the access system.

Namespace

Drupal\system\Tests\Routing

Code

public function testAccess() {
  \Drupal::service('router.builder')
    ->rebuild();

  /** @var \Drupal\user\RoleInterface $role_with_access */
  $role_with_access = Role::create([
    'id' => 'role_with_access',
  ]);
  $role_with_access
    ->grantPermission('administer users');
  $role_with_access
    ->save();

  /** @var \Drupal\user\RoleInterface $role_without_access */
  $role_without_access = Role::create([
    'id' => 'role_without_access',
  ]);
  $role_without_access
    ->save();
  $user_with_access = User::create([
    'roles' => [
      'role_with_access',
    ],
  ]);
  $user_without_access = User::create([
    'roles' => [
      'role_without_access',
    ],
  ]);
  $url_always_access = new Url('router_test.1');
  $this
    ->assertTrue($url_always_access
    ->access($user_with_access));
  $this
    ->assertTrue($url_always_access
    ->access($user_without_access));
  $url_none_access = new Url('router_test.15');
  $this
    ->assertFalse($url_none_access
    ->access($user_with_access));
  $this
    ->assertFalse($url_none_access
    ->access($user_without_access));
  $url_access = new Url('router_test.16');
  $this
    ->assertTrue($url_access
    ->access($user_with_access));
  $this
    ->assertFalse($url_access
    ->access($user_without_access));
}