AccessTest.php in Drupal 9
File
core/modules/views/tests/src/Functional/Plugin/AccessTest.php
View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class AccessTest extends ViewTestBase {
public static $testViews = [
'test_access_none',
'test_access_static',
'test_access_dynamic',
];
protected static $modules = [
'node',
];
protected $defaultTheme = 'stark';
protected $webUser;
protected $normalUser;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->enableViewsTestModule();
ViewTestData::createTestViews(static::class, [
'views_test_data',
]);
$this->webUser = $this
->drupalCreateUser();
$normal_role = $this
->drupalCreateRole([]);
$this->normalUser = $this
->drupalCreateUser([
'views_test_data test permission',
]);
$this->normalUser
->addRole($normal_role);
}
public function testAccessNone() {
$view = Views::getView('test_access_none');
$view
->setDisplay();
$this
->assertTrue($view->display_handler
->access($this->webUser));
$this
->assertTrue($view->display_handler
->access($this->normalUser));
}
public function testStaticAccessPlugin() {
$view = Views::getView('test_access_static');
$view
->setDisplay();
$access_plugin = $view->display_handler
->getPlugin('access');
$this
->assertFalse($access_plugin
->access($this->normalUser));
$this
->drupalGet('test_access_static');
$this
->assertSession()
->statusCodeEquals(403);
$display =& $view->storage
->getDisplay('default');
$display['display_options']['access']['options']['access'] = TRUE;
$access_plugin->options['access'] = TRUE;
$view
->save();
$this->container
->get('router.builder')
->rebuildIfNeeded();
$this
->assertTrue($access_plugin
->access($this->normalUser));
$this
->drupalGet('test_access_static');
$this
->assertSession()
->statusCodeEquals(200);
}
}