3 namespace Drupal\Tests\user\Functional\Views;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
7 use Drupal\user\Plugin\views\access\Role;
8 use Drupal\views\Plugin\views\display\DisplayPluginBase;
9 use Drupal\views\Views;
12 * Tests views role access plugin.
15 * @see \Drupal\user\Plugin\views\access\Role
17 class AccessRoleTest extends AccessTestBase {
19 use AssertPageCacheContextsAndTagsTrait;
22 * Views used by this test.
26 public static $testViews = ['test_access_role'];
29 * Tests role access plugin.
31 public function testAccessRole() {
32 /** @var \Drupal\views\ViewEntityInterface $view */
33 $view = \Drupal::entityManager()->getStorage('view')->load('test_access_role');
34 $display = &$view->getDisplay('default');
35 $display['display_options']['access']['options']['role'] = [
36 $this->normalRole => $this->normalRole,
39 $this->container->get('router.builder')->rebuildIfNeeded();
41 'config' => ['user.role.' . $this->normalRole],
42 'module' => ['user', 'views_test_data'],
44 $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
46 $executable = Views::executableFactory()->get($view);
47 $executable->setDisplay('page_1');
49 $access_plugin = $executable->display_handler->getPlugin('access');
50 $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.');
52 // Test the access() method on the access plugin.
53 $this->assertFalse($executable->display_handler->access($this->webUser));
54 $this->assertTrue($executable->display_handler->access($this->normalUser));
56 $this->drupalLogin($this->webUser);
57 $this->drupalGet('test-role');
58 $this->assertResponse(403);
59 $this->assertCacheContext('user.roles');
61 $this->drupalLogin($this->normalUser);
62 $this->drupalGet('test-role');
63 $this->assertResponse(200);
64 $this->assertCacheContext('user.roles');
66 // Test allowing multiple roles.
67 $view = Views::getView('test_access_role')->storage;
68 $display = &$view->getDisplay('default');
69 $display['display_options']['access']['options']['role'] = [
70 $this->normalRole => $this->normalRole,
71 'anonymous' => 'anonymous',
74 $this->container->get('router.builder')->rebuildIfNeeded();
76 // Ensure that the list of roles is sorted correctly, if the generated role
77 // ID comes before 'anonymous', see https://www.drupal.org/node/2398259.
78 $roles = ['user.role.anonymous', 'user.role.' . $this->normalRole];
82 'module' => ['user', 'views_test_data'],
84 $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
85 $this->drupalLogin($this->webUser);
86 $this->drupalGet('test-role');
87 $this->assertResponse(403);
88 $this->assertCacheContext('user.roles');
89 $this->drupalLogout();
90 $this->drupalGet('test-role');
91 $this->assertResponse(200);
92 $this->assertCacheContext('user.roles');
93 $this->drupalLogin($this->normalUser);
94 $this->drupalGet('test-role');
95 $this->assertResponse(200);
96 $this->assertCacheContext('user.roles');
100 * Tests access on render caching.
102 public function testRenderCaching() {
103 $view = Views::getView('test_access_role');
104 $display = &$view->storage->getDisplay('default');
105 $display['display_options']['cache'] = [
108 $display['display_options']['access']['options']['role'] = [
109 $this->normalRole => $this->normalRole,
113 /** @var \Drupal\Core\Render\RendererInterface $renderer */
114 $renderer = \Drupal::service('renderer');
115 /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
116 $account_switcher = \Drupal::service('account_switcher');
118 // First access as user with access.
119 $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
120 $account_switcher->switchTo($this->normalUser);
121 $result = $renderer->renderPlain($build);
122 $this->assertTrue(in_array('user.roles', $build['#cache']['contexts']));
123 $this->assertEqual(['config:views.view.test_access_role'], $build['#cache']['tags']);
124 $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
125 $this->assertNotEqual($result, '');
127 // Then without access.
128 $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
129 $account_switcher->switchTo($this->webUser);
130 $result = $renderer->renderPlain($build);
131 // @todo Fix this in https://www.drupal.org/node/2551037,
132 // DisplayPluginBase::applyDisplayCacheabilityMetadata() is not invoked when
133 // using buildBasicRenderable() and a Views access plugin returns FALSE.
134 // $this->assertTrue(in_array('user.roles', $build['#cache']['contexts']));
135 // $this->assertEqual([], $build['#cache']['tags']);
136 $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
137 $this->assertEqual($result, '');