Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Handler / FieldFieldAccessTestBase.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Handler;
4
5 use Drupal\user\Entity\Role;
6 use Drupal\user\Entity\User;
7 use Drupal\views\Entity\View;
8 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
9 use Drupal\views\Views;
10
11 /**
12  * Provides a base class for base field access in views.
13  */
14 abstract class FieldFieldAccessTestBase extends ViewsKernelTestBase {
15
16   /**
17    * Stores an user entity with access to fields.
18    *
19    * @var \Drupal\user\UserInterface
20    */
21   protected $userWithAccess;
22
23   /**
24    * Stores an user entity without access to fields.
25    *
26    * @var \Drupal\user\UserInterface
27    */
28   protected $userWithoutAccess;
29
30   /**
31    * {@inheritdoc}
32    */
33   public static $modules = ['user'];
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp($import_test_views = TRUE) {
39     parent::setUp($import_test_views);
40
41     $this->installEntitySchema('user');
42
43     $role_with_access = Role::create([
44       'id' => 'with_access',
45       'permissions' => ['view test entity field'],
46     ]);
47     $role_with_access->save();
48     $role_without_access = Role::create([
49       'id' => 'without_access',
50       'permissions' => [],
51     ]);
52     $role_without_access->save();
53
54     $this->userWithAccess = User::create([
55       'name' => $this->randomMachineName(),
56       'roles' => [$role_with_access->id()],
57     ]);
58     $this->userWithAccess->save();
59     $this->userWithoutAccess = User::create([
60       'name' => $this->randomMachineName(),
61       'roles' => [$role_without_access->id()],
62     ]);
63     $this->userWithoutAccess->save();
64   }
65
66   /**
67    * Checks views field access for a given entity type and field name.
68    *
69    * To use this method, set up an entity of type $entity_type_id, with field
70    * $field_name. Create an entity instance that contains content $field_content
71    * in that field.
72    *
73    * This method will check that a user with permission can see the content in a
74    * view, and a user without access permission on that field cannot.
75    *
76    * @param string $entity_type_id
77    *   The entity type ID.
78    * @param string $field_name
79    *   The field name.
80    * @param string $field_content
81    *   The expected field content.
82    */
83   protected function assertFieldAccess($entity_type_id, $field_name, $field_content) {
84     \Drupal::state()->set('views_field_access_test-field', $field_name);
85
86     $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
87     $view_id = $this->randomMachineName();
88     $data_table = $entity_type->getDataTable();
89     // Use the data table as long as the field is not 'uuid'. This is the only
90     // column that can only be obtained from the base table.
91     $base_table = ($data_table && ($field_name !== 'uuid')) ? $data_table : $entity_type->getBaseTable();
92     $entity = View::create([
93       'id' => $view_id,
94       'base_table' => $base_table,
95       'display' => [
96         'default' => [
97           'display_plugin' => 'default',
98           'id' => 'default',
99           'display_options' => [
100             'fields' => [
101               $field_name => [
102                 'table' => $base_table,
103                 'field' => $field_name,
104                 'id' => $field_name,
105                 'plugin_id' => 'field',
106               ],
107             ],
108           ],
109         ],
110       ],
111     ]);
112     $entity->save();
113
114     /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
115     $account_switcher = \Drupal::service('account_switcher');
116
117     /** @var \Drupal\Core\Render\RendererInterface $renderer */
118     $renderer = \Drupal::service('renderer');
119
120     $account_switcher->switchTo($this->userWithAccess);
121     $executable = Views::getView($view_id);
122     $build = $executable->preview();
123     $this->setRawContent($renderer->renderRoot($build));
124
125     $this->assertText($field_content);
126     $this->assertTrue(isset($executable->field[$field_name]));
127
128     $account_switcher->switchTo($this->userWithoutAccess);
129     $executable = Views::getView($view_id);
130     $build = $executable->preview();
131     $this->setRawContent($renderer->renderRoot($build));
132
133     $this->assertNoText($field_content);
134     $this->assertFalse(isset($executable->field[$field_name]));
135
136     \Drupal::state()->delete('views_field_access_test-field');
137   }
138
139 }