addda24f278cf3c324db35369d9e6ef5f3f76fd1
[yaffs-website] / web / core / modules / user / src / Tests / Views / AccessRoleTest.php
1 <?php
2
3 namespace Drupal\user\Tests\Views;
4
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;
9
10 /**
11  * Tests views role access plugin.
12  *
13  * @group user
14  * @see \Drupal\user\Plugin\views\access\Role
15  */
16 class AccessRoleTest extends AccessTestBase {
17
18   /**
19    * Views used by this test.
20    *
21    * @var array
22    */
23   public static $testViews = ['test_access_role'];
24
25   /**
26    * Tests role access plugin.
27    */
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,
34     ];
35     $view->save();
36     $this->container->get('router.builder')->rebuildIfNeeded();
37     $expected = [
38       'config' => ['user.role.' . $this->normalRole],
39       'module' => ['user'],
40     ];
41     $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
42
43     $executable = Views::executableFactory()->get($view);
44     $executable->setDisplay('page_1');
45
46     $access_plugin = $executable->display_handler->getPlugin('access');
47     $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.');
48
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));
52
53     $this->drupalLogin($this->webUser);
54     $this->drupalGet('test-role');
55     $this->assertResponse(403);
56     $this->assertCacheContext('user.roles');
57
58     $this->drupalLogin($this->normalUser);
59     $this->drupalGet('test-role');
60     $this->assertResponse(200);
61     $this->assertCacheContext('user.roles');
62
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',
69     ];
70     $view->save();
71     $this->container->get('router.builder')->rebuildIfNeeded();
72
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];
76     sort($roles);
77     $expected = [
78       'config' => $roles,
79       'module' => ['user'],
80     ];
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');
94   }
95
96   /**
97    * Tests access on render caching.
98    */
99   public function testRenderCaching() {
100     $view = Views::getView('test_access_role');
101     $display = &$view->storage->getDisplay('default');
102     $display['display_options']['cache'] = [
103       'type' => 'tag',
104     ];
105     $display['display_options']['access']['options']['role'] = [
106       $this->normalRole => $this->normalRole,
107     ];
108     $view->save();
109
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');
114
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, '');
123
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, '');
135   }
136
137 }