You are here

public function AccessRoleTest::testRenderCaching in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/user/tests/src/Functional/Views/AccessRoleTest.php \Drupal\Tests\user\Functional\Views\AccessRoleTest::testRenderCaching()

Tests access on render caching.

File

core/modules/user/tests/src/Functional/Views/AccessRoleTest.php, line 107

Class

AccessRoleTest
Tests views role access plugin.

Namespace

Drupal\Tests\user\Functional\Views

Code

public function testRenderCaching() {
  $view = Views::getView('test_access_role');
  $display =& $view->storage
    ->getDisplay('default');
  $display['display_options']['cache'] = [
    'type' => 'tag',
  ];
  $display['display_options']['access']['options']['role'] = [
    $this->normalRole => $this->normalRole,
  ];
  $view
    ->save();

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');

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

  // First access as user with access.
  $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
  $account_switcher
    ->switchTo($this->normalUser);
  $result = $renderer
    ->renderPlain($build);
  $this
    ->assertContains('user.roles', $build['#cache']['contexts']);
  $this
    ->assertEqual([
    'config:views.view.test_access_role',
  ], $build['#cache']['tags']);
  $this
    ->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
  $this
    ->assertNotEqual($result, '');

  // Then without access.
  $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
  $account_switcher
    ->switchTo($this->webUser);
  $result = $renderer
    ->renderPlain($build);

  // @todo Fix this in https://www.drupal.org/node/2551037,
  // DisplayPluginBase::applyDisplayCacheabilityMetadata() is not invoked when
  // using buildBasicRenderable() and a Views access plugin returns FALSE.
  // $this->assertContains('user.roles', $build['#cache']['contexts']);
  // $this->assertEqual([], $build['#cache']['tags']);
  $this
    ->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
  $this
    ->assertEqual($result, '');
}