function AccessRoleTest::testRenderCaching

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()
  2. 8.9.x core/modules/user/tests/src/Functional/Views/AccessRoleTest.php \Drupal\Tests\user\Functional\Views\AccessRoleTest::testRenderCaching()
  3. 11.x 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 116

Class

AccessRoleTest
Tests views role access plugin.

Namespace

Drupal\Tests\user\Functional\Views

Code

public function testRenderCaching() : void {
  $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->renderInIsolation($build);
  $this->assertContains('user.roles', $build['#cache']['contexts']);
  $this->assertEquals([
    'config:views.view.test_access_role',
  ], $build['#cache']['tags']);
  $this->assertEquals(Cache::PERMANENT, $build['#cache']['max-age']);
  $this->assertNotSame('', $result);
  // Then without access.
  $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
  $account_switcher->switchTo($this->webUser);
  $result = $renderer->renderInIsolation($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->assertEquals([], $build['#cache']['tags']);
  $this->assertEquals(Cache::PERMANENT, $build['#cache']['max-age']);
  $this->assertEquals('', $result);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.