function AccessRoleTest::testAccessRole
Same name in other branches
- 8.9.x core/modules/user/tests/src/Functional/Views/AccessRoleTest.php \Drupal\Tests\user\Functional\Views\AccessRoleTest::testAccessRole()
- 10 core/modules/user/tests/src/Functional/Views/AccessRoleTest.php \Drupal\Tests\user\Functional\Views\AccessRoleTest::testAccessRole()
- 11.x 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 43
Class
- AccessRoleTest
- Tests views role access plugin.
Namespace
Drupal\Tests\user\Functional\ViewsCode
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->assertSame($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->assertFalse($executable->display_handler
->access($this->webUser));
$this->assertTrue($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->assertSame($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');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.