You are here

public function AccessRoleTest::testAccessRole in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/user/tests/src/Functional/Views/AccessRoleTest.php \Drupal\Tests\user\Functional\Views\AccessRoleTest::testAccessRole()

Tests role access plugin.

File

core/modules/user/tests/src/Functional/Views/AccessRoleTest.php, line 36

Class

AccessRoleTest
Tests views role access plugin.

Namespace

Drupal\Tests\user\Functional\Views

Code

public function testAccessRole() {

  /** @var \Drupal\views\ViewEntityInterface $view */
  $view = \Drupal::entityTypeManager()
    ->getStorage('view')
    ->load('test_access_role');
  $display =& $view
    ->getDisplay('default');
  $display['display_options']['access']['options']['role'] = [
    $this->normalRole => $this->normalRole,
  ];
  $view
    ->save();
  $this->container
    ->get('router.builder')
    ->rebuildIfNeeded();
  $expected = [
    'config' => [
      'user.role.' . $this->normalRole,
    ],
    'module' => [
      'user',
      'views_test_data',
    ],
  ];
  $this
    ->assertIdentical($expected, $view
    ->calculateDependencies()
    ->getDependencies());
  $executable = Views::executableFactory()
    ->get($view);
  $executable
    ->setDisplay('page_1');
  $access_plugin = $executable->display_handler
    ->getPlugin('access');
  $this
    ->assertInstanceOf(Role::class, $access_plugin);

  // Test the access() method on the access plugin.
  $this
    ->assertSame(FALSE, $executable->display_handler
    ->access($this->webUser));
  $this
    ->assertSame(TRUE, $executable->display_handler
    ->access($this->normalUser));
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet('test-role');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->assertCacheContext('user.roles');
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('test-role');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertCacheContext('user.roles');

  // Test allowing multiple roles.
  $view = Views::getView('test_access_role')->storage;
  $display =& $view
    ->getDisplay('default');
  $display['display_options']['access']['options']['role'] = [
    $this->normalRole => $this->normalRole,
    'anonymous' => 'anonymous',
  ];
  $view
    ->save();
  $this->container
    ->get('router.builder')
    ->rebuildIfNeeded();

  // Ensure that the list of roles is sorted correctly, if the generated role
  // ID comes before 'anonymous', see https://www.drupal.org/node/2398259.
  $roles = [
    'user.role.anonymous',
    'user.role.' . $this->normalRole,
  ];
  sort($roles);
  $expected = [
    'config' => $roles,
    'module' => [
      'user',
      'views_test_data',
    ],
  ];
  $this
    ->assertIdentical($expected, $view
    ->calculateDependencies()
    ->getDependencies());
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet('test-role');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->assertCacheContext('user.roles');
  $this
    ->drupalLogout();
  $this
    ->drupalGet('test-role');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertCacheContext('user.roles');
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('test-role');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertCacheContext('user.roles');
}