Skip to content
AccessRoleTest.php 2.88 KiB
Newer Older
 * Contains \Drupal\user\Tests\Views\AccessRoleTest.
use Drupal\views\Views;
use Symfony\Component\HttpFoundation\Request;
 * @see \Drupal\user\Plugin\views\access\Role
 */
class AccessRoleTest extends AccessTestBase {

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_access_role');

  /**
   * Tests role access plugin.
   */
  function testAccessRole() {
    /** @var \Drupal\views\ViewEntityInterface $view */
    $view = \Drupal::entityManager()->getStorage('view')->load('test_access_role');
    $display = &$view->getDisplay('default');
    $display['display_options']['access']['options']['role'] = array(
      $this->normalRole => $this->normalRole,
    );
    $this->container->get('router.builder')->rebuildIfNeeded();
    $expected = [
      'config' => ['user.role.' . $this->normalRole],
      'module' => ['user'],
    ];
    $this->assertIdentical($expected, $view->calculateDependencies());

    $executable = Views::executableFactory()->get($view);
    $executable->setDisplay('page_1');
    $access_plugin = $executable->display_handler->getPlugin('access');
    $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.');
    // 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);

    $this->drupalLogin($this->normalUser);
    $this->drupalGet('test-role');
    $this->assertResponse(200);

    // Test allowing multiple roles.
    $view = Views::getView('test_access_role')->storage;
    $display = &$view->getDisplay('default');
    $display['display_options']['access']['options']['role'] = array(
      $this->normalRole => $this->normalRole,
      'anonymous' => 'anonymous',
    );
    $view->save();
    $this->container->get('router.builder')->rebuildIfNeeded();

    // 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);
      'module' => ['user'],
    ];
    $this->assertIdentical($expected, $view->calculateDependencies());
    $this->drupalLogin($this->webUser);
    $this->drupalGet('test-role');
    $this->assertResponse(403);
    $this->drupalLogout();
    $this->drupalGet('test-role');
    $this->assertResponse(200);
    $this->drupalLogin($this->normalUser);
    $this->drupalGet('test-role');
    $this->assertResponse(200);