Pull merge.
[yaffs-website] / web / core / modules / user / tests / src / Functional / Views / AccessRoleTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional\Views;
4
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;
10
11 /**
12  * Tests views role access plugin.
13  *
14  * @group user
15  * @see \Drupal\user\Plugin\views\access\Role
16  */
17 class AccessRoleTest extends AccessTestBase {
18
19   use AssertPageCacheContextsAndTagsTrait;
20
21   /**
22    * Views used by this test.
23    *
24    * @var array
25    */
26   public static $testViews = ['test_access_role'];
27
28   /**
29    * Tests role access plugin.
30    */
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,
37     ];
38     $view->save();
39     $this->container->get('router.builder')->rebuildIfNeeded();
40     $expected = [
41       'config' => ['user.role.' . $this->normalRole],
42       'module' => ['user', 'views_test_data'],
43     ];
44     $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
45
46     $executable = Views::executableFactory()->get($view);
47     $executable->setDisplay('page_1');
48
49     $access_plugin = $executable->display_handler->getPlugin('access');
50     $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.');
51
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));
55
56     $this->drupalLogin($this->webUser);
57     $this->drupalGet('test-role');
58     $this->assertResponse(403);
59     $this->assertCacheContext('user.roles');
60
61     $this->drupalLogin($this->normalUser);
62     $this->drupalGet('test-role');
63     $this->assertResponse(200);
64     $this->assertCacheContext('user.roles');
65
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',
72     ];
73     $view->save();
74     $this->container->get('router.builder')->rebuildIfNeeded();
75
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];
79     sort($roles);
80     $expected = [
81       'config' => $roles,
82       'module' => ['user', 'views_test_data'],
83     ];
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');
97   }
98
99   /**
100    * Tests access on render caching.
101    */
102   public function testRenderCaching() {
103     $view = Views::getView('test_access_role');
104     $display = &$view->storage->getDisplay('default');
105     $display['display_options']['cache'] = [
106       'type' => 'tag',
107     ];
108     $display['display_options']['access']['options']['role'] = [
109       $this->normalRole => $this->normalRole,
110     ];
111     $view->save();
112
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');
117
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, '');
126
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, '');
138   }
139
140 }