AccessRoleTest.php 4.89 KB
Newer Older
1 2
<?php

3
namespace Drupal\user\Tests\Views;
4

5
use Drupal\Core\Cache\Cache;
6
use Drupal\user\Plugin\views\access\Role;
7
use Drupal\views\Plugin\views\display\DisplayPluginBase;
8
use Drupal\views\Views;
9

10 11 12
/**
 * Tests views role access plugin.
 *
13
 * @group user
14
 * @see \Drupal\user\Plugin\views\access\Role
15 16 17
 */
class AccessRoleTest extends AccessTestBase {

18 19 20 21 22
  /**
   * Views used by this test.
   *
   * @var array
   */
23
  public static $testViews = ['test_access_role'];
24

25 26 27
  /**
   * Tests role access plugin.
   */
28
  public function testAccessRole() {
29
    /** @var \Drupal\views\ViewEntityInterface $view */
30
    $view = \Drupal::entityManager()->getStorage('view')->load('test_access_role');
31
    $display = &$view->getDisplay('default');
32
    $display['display_options']['access']['options']['role'] = [
33
      $this->normalRole => $this->normalRole,
34
    ];
35
    $view->save();
36
    $this->container->get('router.builder')->rebuildIfNeeded();
37 38 39 40
    $expected = [
      'config' => ['user.role.' . $this->normalRole],
      'module' => ['user'],
    ];
41
    $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
42 43 44

    $executable = Views::executableFactory()->get($view);
    $executable->setDisplay('page_1');
45

46
    $access_plugin = $executable->display_handler->getPlugin('access');
47
    $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.');
48

49 50 51 52 53 54 55
    // Test the access() method on the access plugin.
    $this->assertFalse($executable->display_handler->access($this->webUser));
    $this->assertTrue($executable->display_handler->access($this->normalUser));

    $this->drupalLogin($this->webUser);
    $this->drupalGet('test-role');
    $this->assertResponse(403);
56
    $this->assertCacheContext('user.roles');
57 58 59 60

    $this->drupalLogin($this->normalUser);
    $this->drupalGet('test-role');
    $this->assertResponse(200);
61
    $this->assertCacheContext('user.roles');
62 63 64 65

    // Test allowing multiple roles.
    $view = Views::getView('test_access_role')->storage;
    $display = &$view->getDisplay('default');
66
    $display['display_options']['access']['options']['role'] = [
67 68
      $this->normalRole => $this->normalRole,
      'anonymous' => 'anonymous',
69
    ];
70
    $view->save();
71
    $this->container->get('router.builder')->rebuildIfNeeded();
72 73 74 75 76

    // Ensure that the list of roles is sorted correctly, if the generated role
    // ID comes before 'anonymous', see https://www.drupal.org/node/2398259.
    $roles = ['user.role.anonymous', 'user.role.' . $this->normalRole];
    sort($roles);
77
    $expected = [
78
      'config' => $roles,
79 80
      'module' => ['user'],
    ];
81
    $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
82 83 84
    $this->drupalLogin($this->webUser);
    $this->drupalGet('test-role');
    $this->assertResponse(403);
85
    $this->assertCacheContext('user.roles');
86 87 88
    $this->drupalLogout();
    $this->drupalGet('test-role');
    $this->assertResponse(200);
89
    $this->assertCacheContext('user.roles');
90 91 92
    $this->drupalLogin($this->normalUser);
    $this->drupalGet('test-role');
    $this->assertResponse(200);
93
    $this->assertCacheContext('user.roles');
94 95
  }

96 97 98 99 100 101 102 103 104
  /**
   * Tests access on render caching.
   */
  public function testRenderCaching() {
    $view = Views::getView('test_access_role');
    $display = &$view->storage->getDisplay('default');
    $display['display_options']['cache'] = [
      'type' => 'tag',
    ];
105
    $display['display_options']['access']['options']['role'] = [
106
      $this->normalRole => $this->normalRole,
107
    ];
108 109 110 111 112 113 114
    $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');

115
    // First access as user with access.
116 117 118
    $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
    $account_switcher->switchTo($this->normalUser);
    $result = $renderer->renderPlain($build);
119 120 121
    $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']);
122 123
    $this->assertNotEqual($result, '');

124
    // Then without access.
125 126 127
    $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
    $account_switcher->switchTo($this->webUser);
    $result = $renderer->renderPlain($build);
128
    // @todo Fix this in https://www.drupal.org/node/2551037,
alexpott's avatar
alexpott committed
129
    // DisplayPluginBase::applyDisplayCacheabilityMetadata() is not invoked when
130
    // using buildBasicRenderable() and a Views access plugin returns FALSE.
131 132
    // $this->assertTrue(in_array('user.roles', $build['#cache']['contexts']));
    // $this->assertEqual([], $build['#cache']['tags']);
133
    $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
134 135 136
    $this->assertEqual($result, '');
  }

137
}