You are here

protected function ViewsFieldAccessTest::assertFieldAccess in Field Permissions 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/ViewsFieldAccessTest.php \Drupal\Tests\field_permissions\Kernel\ViewsFieldAccessTest::assertFieldAccess()

Checks views field access on the test entity and field.

3 calls to ViewsFieldAccessTest::assertFieldAccess()
ViewsFieldAccessTest::testCustomPermissions in tests/src/Kernel/ViewsFieldAccessTest.php
Tests custom permissions.
ViewsFieldAccessTest::testPrivatePermissions in tests/src/Kernel/ViewsFieldAccessTest.php
Tests private permissions.
ViewsFieldAccessTest::testPublicPermissions in tests/src/Kernel/ViewsFieldAccessTest.php
Test default (public) permissions.

File

tests/src/Kernel/ViewsFieldAccessTest.php, line 184

Class

ViewsFieldAccessTest
Test that custom and private field access works with views.

Namespace

Drupal\Tests\field_permissions\Kernel

Code

protected function assertFieldAccess() {

  /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
  $entity_type = $this->container
    ->get('entity_type.manager')
    ->getDefinition('entity_test');
  $view_id = $this
    ->randomMachineName();
  $data_table = $entity_type
    ->getDataTable();
  $base_table = $data_table ?: $entity_type
    ->getBaseTable();
  $field_name = $this->fieldStorage
    ->getName();
  $field_content = $this->entity->{$field_name}->value;
  $entity = View::create([
    'id' => $view_id,
    'base_table' => $base_table,
    'display' => [
      'default' => [
        'display_plugin' => 'default',
        'id' => 'default',
        'display_options' => [
          'fields' => [
            $field_name => [
              'table' => $base_table . '__' . $field_name,
              'field' => $field_name,
              'id' => $field_name,
              'plugin_id' => 'field',
              'type' => 'text_default',
            ],
          ],
        ],
      ],
    ],
  ]);
  $entity
    ->save();

  /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
  $account_switcher = $this->container
    ->get('account_switcher');

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $account_switcher
    ->switchTo($this->userWithAccess);
  $executable = Views::getView($view_id);
  $build = $executable
    ->preview();
  $this
    ->setRawContent($renderer
    ->renderRoot($build));
  $this
    ->assertText($field_content);
  $this
    ->assertArrayHasKey($field_name, $executable->field);
  $account_switcher
    ->switchTo($this->userWithoutAccess);
  $executable = Views::getView($view_id);
  $build = $executable
    ->preview();
  $this
    ->setRawContent($renderer
    ->renderRoot($build));

  // If this is a public permission, then the no access user can see it too.
  if ($this->fieldStorage
    ->getThirdPartySetting('field_permissions', 'permission_type') === FieldPermissionTypeInterface::ACCESS_PUBLIC) {
    $this
      ->assertText($field_content);
  }
  else {
    $this
      ->assertNoText($field_content);
  }
}