View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\Core\Database\Database;
class NodeAccessGrantsCacheContextTest extends NodeTestBase {
protected static $modules = [
'node_access_test',
];
protected $defaultTheme = 'stark';
protected $accessUser;
protected $noAccessUser;
protected function setUp() : void {
parent::setUp();
node_access_rebuild();
$this
->drupalCreateNode();
$this
->drupalCreateNode();
$this
->drupalCreateNode();
$this
->drupalCreateNode();
$this->accessUser = $this
->drupalCreateUser([
'access content overview',
'access content',
'node test view',
]);
$this->noAccessUser = $this
->drupalCreateUser([
'access content overview',
'access content',
]);
$this->noAccessUser2 = $this
->drupalCreateUser([
'access content overview',
'access content',
]);
$this->userMapping = [
1 => $this->rootUser,
2 => $this->accessUser,
3 => $this->noAccessUser,
];
}
protected function assertUserCacheContext(array $expected) {
foreach ($expected as $uid => $context) {
if ($uid > 0) {
$this
->drupalLogin($this->userMapping[$uid]);
}
$this
->assertSame($context, $this->container
->get('cache_context.user.node_grants')
->getContext('view'));
}
$this
->drupalLogout();
}
public function testCacheContext() {
$this
->assertUserCacheContext([
0 => 'view.all:0;node_access_test_author:0;node_access_all:0',
1 => 'all',
2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
3 => 'view.all:0;node_access_test_author:3',
]);
$record = [
'nid' => 0,
'gid' => 0,
'realm' => 'node_access_all',
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
];
Database::getConnection()
->insert('node_access')
->fields($record)
->execute();
\Drupal::state()
->set('node_access_test.no_access_uid', 0);
drupal_static_reset('node_access_view_all_nodes');
$this
->assertUserCacheContext([
0 => 'view.all',
1 => 'all',
2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
3 => 'view.all:0;node_access_test_author:3',
]);
\Drupal::state()
->set('node_access_test.no_access_uid', $this->accessUser
->id());
drupal_static_reset('node_access_view_all_nodes');
$this
->assertUserCacheContext([
0 => 'view.all:0;node_access_test_author:0',
1 => 'all',
2 => 'view.all',
3 => 'view.all:0;node_access_test_author:3',
]);
\Drupal::state()
->set('node_access_test.no_access_uid', $this->noAccessUser
->id());
drupal_static_reset('node_access_view_all_nodes');
$this
->assertUserCacheContext([
0 => 'view.all:0;node_access_test_author:0',
1 => 'all',
2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
3 => 'view.all',
]);
$this->container
->get('module_installer')
->uninstall([
'node_access_test',
]);
drupal_static_reset('node_access_view_all_nodes');
$this
->assertUserCacheContext([
0 => 'view.all',
1 => 'all',
2 => 'view.all',
3 => 'view.all',
]);
}
}