You are here

protected function FieldFieldAccessTestBase::assertFieldAccess in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/FieldFieldAccessTestBase.php \Drupal\views\Tests\Handler\FieldFieldAccessTestBase::assertFieldAccess()

Checks views field access for a given entity type and field name.

To use this method, set up an entity of type $entity_type_id, with field $field_name. Create an entity instance that contains content $field_content in that field.

This method will check that a user with permission can see the content in a view, and a user without access permission on that field cannot.

Parameters

string $entity_type_id: The entity type ID.

string $field_name: The field name.

string $field_content: The expected field content.

8 calls to FieldFieldAccessTestBase::assertFieldAccess()
AggregatorFeedViewsFieldAccessTest::testAggregatorFeedFields in core/modules/aggregator/src/Tests/Views/AggregatorFeedViewsFieldAccessTest.php
Checks access for aggregator_feed fields.
AggregatorItemViewsFieldAccessTest::testAggregatorItemFields in core/modules/aggregator/src/Tests/Views/AggregatorItemViewsFieldAccessTest.php
Checks access for aggregator_item fields.
CommentViewsFieldAccessTest::testCommentFields in core/modules/comment/src/Tests/Views/CommentViewsFieldAccessTest.php
Check access for comment fields.
EntityTestViewsFieldAccessTest::testEntityTestFields in core/modules/views/src/Tests/Handler/EntityTestViewsFieldAccessTest.php
FileViewsFieldAccessTest::testFileFields in core/modules/file/src/Tests/Views/FileViewsFieldAccessTest.php
Check access for file fields.

... See full list

File

core/modules/views/src/Tests/Handler/FieldFieldAccessTestBase.php, line 88
Contains \Drupal\views\Tests\Handler\FieldFieldAccessTestBase.

Class

FieldFieldAccessTestBase
Provides a base class for base field access in views.

Namespace

Drupal\views\Tests\Handler

Code

protected function assertFieldAccess($entity_type_id, $field_name, $field_content) {
  \Drupal::state()
    ->set('views_field_access_test-field', $field_name);
  $entity_type = \Drupal::entityManager()
    ->getDefinition($entity_type_id);
  $view_id = $this
    ->randomMachineName();
  $data_table = $entity_type
    ->getDataTable();

  // Use the data table as long as the field is not 'uuid'. This is the only
  // column that can only be obtained from the base table.
  $base_table = $data_table && $field_name !== 'uuid' ? $data_table : $entity_type
    ->getBaseTable();
  $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' => $field_name,
              'id' => $field_name,
              'plugin_id' => 'field',
            ],
          ],
        ],
      ],
    ],
  ]);
  $entity
    ->save();

  /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
  $account_switcher = \Drupal::service('account_switcher');

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $account_switcher
    ->switchTo($this->userWithAccess);
  $executable = Views::getView($view_id);
  $build = $executable
    ->preview();
  $this
    ->setRawContent($renderer
    ->renderRoot($build));
  $this
    ->assertText($field_content);
  $this
    ->assertTrue(isset($executable->field[$field_name]));
  $account_switcher
    ->switchTo($this->userWithoutAccess);
  $executable = Views::getView($view_id);
  $build = $executable
    ->preview();
  $this
    ->setRawContent($renderer
    ->renderRoot($build));
  $this
    ->assertNoText($field_content);
  $this
    ->assertFalse(isset($executable->field[$field_name]));
  \Drupal::state()
    ->delete('views_field_access_test-field');
}