You are here

function AccessRoleTest::testAccessRole in Zircon Profile 8.0

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

Tests role access plugin.

File

core/modules/user/src/Tests/Views/AccessRoleTest.php, line 33
Contains \Drupal\user\Tests\Views\AccessRoleTest.

Class

AccessRoleTest
Tests views role access plugin.

Namespace

Drupal\user\Tests\Views

Code

function testAccessRole() {

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

  // Test the access() method on the access plugin.
  $this
    ->assertFalse($executable->display_handler
    ->access($this->webUser));
  $this
    ->assertTrue($executable->display_handler
    ->access($this->normalUser));
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet('test-role');
  $this
    ->assertResponse(403);
  $this
    ->assertCacheContext('user.roles');
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('test-role');
  $this
    ->assertResponse(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'] = array(
    $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',
    ],
  ];
  $this
    ->assertIdentical($expected, $view
    ->calculateDependencies()
    ->getDependencies());
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet('test-role');
  $this
    ->assertResponse(403);
  $this
    ->assertCacheContext('user.roles');
  $this
    ->drupalLogout();
  $this
    ->drupalGet('test-role');
  $this
    ->assertResponse(200);
  $this
    ->assertCacheContext('user.roles');
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('test-role');
  $this
    ->assertResponse(200);
  $this
    ->assertCacheContext('user.roles');
}