AccessPermissionTest.php in Drupal 8
File
core/modules/user/tests/src/Kernel/Views/AccessPermissionTest.php
View source
<?php
namespace Drupal\Tests\user\Kernel\Views;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Plugin\views\access\Permission;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class AccessPermissionTest extends KernelTestBase {
use UserCreationTrait;
protected static $modules = [
'node',
'system',
'user',
'user_test_views',
'views',
'views_test_data',
];
public static $testViews = [
'test_access_perm',
];
protected $webUser;
protected $normalUser;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->createUser([], NULL, FALSE, [
'uid' => 1,
]);
$this->webUser = $this
->createUser();
$this->normalUser = $this
->createUser([
'views_test_data test permission',
]);
ViewTestData::createTestViews(get_class($this), [
'user_test_views',
]);
}
public function testAccessPerm() {
$view = Views::getView('test_access_perm');
$view
->setDisplay();
$access_plugin = $view->display_handler
->getPlugin('access');
$this
->assertInstanceOf(Permission::class, $access_plugin);
$this
->assertEquals('Permission', $access_plugin
->pluginTitle());
$this
->assertFalse($view->display_handler
->access($this->webUser));
$this
->assertTrue($view->display_handler
->access($this->normalUser));
}
public function testRenderCaching() {
$view = Views::getView('test_access_perm');
$display =& $view->storage
->getDisplay('default');
$display['display_options']['cache'] = [
'type' => 'tag',
];
$renderer = $this->container
->get('renderer');
$account_switcher = $this->container
->get('account_switcher');
$build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default');
$account_switcher
->switchTo($this->webUser);
$this
->assertEmpty($renderer
->renderPlain($build));
$build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default');
$account_switcher
->switchTo($this->normalUser);
$this
->assertNotEmpty($renderer
->renderPlain($build));
}
}