3 namespace Drupal\user\Tests\Views;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\user\Plugin\views\access\Role;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\Views;
11 * Tests views role access plugin.
14 * @see \Drupal\user\Plugin\views\access\Role
16 class AccessRoleTest extends AccessTestBase {
19 * Views used by this test.
23 public static $testViews = ['test_access_role'];
26 * Tests role access plugin.
28 public function testAccessRole() {
29 /** @var \Drupal\views\ViewEntityInterface $view */
30 $view = \Drupal::entityManager()->getStorage('view')->load('test_access_role');
31 $display = &$view->getDisplay('default');
32 $display['display_options']['access']['options']['role'] = [
33 $this->normalRole => $this->normalRole,
36 $this->container->get('router.builder')->rebuildIfNeeded();
38 'config' => ['user.role.' . $this->normalRole],
41 $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
43 $executable = Views::executableFactory()->get($view);
44 $executable->setDisplay('page_1');
46 $access_plugin = $executable->display_handler->getPlugin('access');
47 $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.');
49 // Test the access() method on the access plugin.
50 $this->assertFalse($executable->display_handler->access($this->webUser));
51 $this->assertTrue($executable->display_handler->access($this->normalUser));
53 $this->drupalLogin($this->webUser);
54 $this->drupalGet('test-role');
55 $this->assertResponse(403);
56 $this->assertCacheContext('user.roles');
58 $this->drupalLogin($this->normalUser);
59 $this->drupalGet('test-role');
60 $this->assertResponse(200);
61 $this->assertCacheContext('user.roles');
63 // Test allowing multiple roles.
64 $view = Views::getView('test_access_role')->storage;
65 $display = &$view->getDisplay('default');
66 $display['display_options']['access']['options']['role'] = [
67 $this->normalRole => $this->normalRole,
68 'anonymous' => 'anonymous',
71 $this->container->get('router.builder')->rebuildIfNeeded();
73 // Ensure that the list of roles is sorted correctly, if the generated role
74 // ID comes before 'anonymous', see https://www.drupal.org/node/2398259.
75 $roles = ['user.role.anonymous', 'user.role.' . $this->normalRole];
81 $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
82 $this->drupalLogin($this->webUser);
83 $this->drupalGet('test-role');
84 $this->assertResponse(403);
85 $this->assertCacheContext('user.roles');
86 $this->drupalLogout();
87 $this->drupalGet('test-role');
88 $this->assertResponse(200);
89 $this->assertCacheContext('user.roles');
90 $this->drupalLogin($this->normalUser);
91 $this->drupalGet('test-role');
92 $this->assertResponse(200);
93 $this->assertCacheContext('user.roles');
97 * Tests access on render caching.
99 public function testRenderCaching() {
100 $view = Views::getView('test_access_role');
101 $display = &$view->storage->getDisplay('default');
102 $display['display_options']['cache'] = [
105 $display['display_options']['access']['options']['role'] = [
106 $this->normalRole => $this->normalRole,
110 /** @var \Drupal\Core\Render\RendererInterface $renderer */
111 $renderer = \Drupal::service('renderer');
112 /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
113 $account_switcher = \Drupal::service('account_switcher');
115 // First access as user with access.
116 $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
117 $account_switcher->switchTo($this->normalUser);
118 $result = $renderer->renderPlain($build);
119 $this->assertTrue(in_array('user.roles', $build['#cache']['contexts']));
120 $this->assertEqual(['config:views.view.test_access_role'], $build['#cache']['tags']);
121 $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
122 $this->assertNotEqual($result, '');
124 // Then without access.
125 $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
126 $account_switcher->switchTo($this->webUser);
127 $result = $renderer->renderPlain($build);
128 // @todo Fix this in https://www.drupal.org/node/2551037,
129 // DisplayPluginBase::applyDisplayCacheabilityMetadata() is not invoked when
130 // using buildBasicRenderable() and a Views access plugin returns FALSE.
131 // $this->assertTrue(in_array('user.roles', $build['#cache']['contexts']));
132 // $this->assertEqual([], $build['#cache']['tags']);
133 $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
134 $this->assertEqual($result, '');