Newer
Older
<?php
Tim Plunkett
committed
/**
* @file
catch
committed
* Contains \Drupal\user\Tests\Views\AccessRoleTest.
Tim Plunkett
committed
*/
catch
committed
namespace Drupal\user\Tests\Views;
Tim Plunkett
committed
use Drupal\user\Plugin\views\access\Role;
use Drupal\views\Views;
use Symfony\Component\HttpFoundation\Request;
Tim Plunkett
committed
/**
* Tests views role access plugin.
*
* @group user
Alex Pott
committed
* @see \Drupal\user\Plugin\views\access\Role
*/
class AccessRoleTest extends AccessTestBase {
catch
committed
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_access_role');
/**
* Tests role access plugin.
*/
function testAccessRole() {
Alex Pott
committed
/** @var \Drupal\views\ViewEntityInterface $view */
catch
committed
$view = \Drupal::entityManager()->getStorage('view')->load('test_access_role');
$display = &$view->getDisplay('default');
$display['display_options']['access']['options']['role'] = array(
$this->normalRole => $this->normalRole,
);
$view->save();
Alex Pott
committed
$this->container->get('router.builder')->rebuildIfNeeded();
Alex Pott
committed
$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');
Tim Plunkett
committed
$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();
Alex Pott
committed
$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);
Alex Pott
committed
$expected = [
'config' => $roles,
Alex Pott
committed
'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);