You are here

public function AccessRoleTest::testRenderCaching in Zircon Profile 8

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

Tests access on render caching.

File

core/modules/user/src/Tests/Views/AccessRoleTest.php, line 104
Contains \Drupal\user\Tests\Views\AccessRoleTest.

Class

AccessRoleTest
Tests views role access plugin.

Namespace

Drupal\user\Tests\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'] = array(
    $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
    ->assertTrue(in_array('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::applyDisplayCachablityMetadata() is not invoked when
  // using buildBasicRenderable() and a Views access plugin returns FALSE.

  //$this->assertTrue(in_array('user.roles', $build['#cache']['contexts']));

  //$this->assertEqual([], $build['#cache']['tags']);
  $this
    ->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
  $this
    ->assertEqual($result, '');
}