function AccessPolicyProcessorInFibersTest::testAccessPolicyProcessorInFibers
Same name and namespace in other branches
- main core/tests/Drupal/KernelTests/Core/Session/AccessPolicyProcessorInFibersTest.php \Drupal\KernelTests\Core\Session\AccessPolicyProcessorInFibersTest::testAccessPolicyProcessorInFibers()
Tests the behavior of the access policy processor running inside fibers.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Session/ AccessPolicyProcessorInFibersTest.php, line 36
Class
- AccessPolicyProcessorInFibersTest
- Tests the behavior of the access policy processor running inside fibers.
Namespace
Drupal\KernelTests\Core\SessionCode
public function testAccessPolicyProcessorInFibers() : void {
// Create a role and then empty the static cache, so that it will need to be
// loaded from storage.
$this->createRole([
'administer modules',
], 'test_role');
\Drupal::entityTypeManager()->getStorage('user_role')
->resetCache();
// Create a render array with two elements that have lazy builders. The
// first lazy builder prints the ID of the current user. The second lazy
// builder checks the permissions of a different user, which results in a
// call to AccountPolicyProcessor::processAccessPolicies(). In that method,
// if the current user ID is different from the ID of the account being
// processed, the current user is temporarily switched to that account.
// This is done in order to make sure the correct user's data is used when
// saving to the variation cache.
//
// Note that for the purposes of this test, the lazy builder that accesses
// the current user ID has to come before the other lazy builder in the
// render array. Ordering the array this way results in the second lazy
// builder starting to run before the first. This happens because as render
// contexts are updated as they are bubbled, the BubbleableMetadata object
// associated with the render element merges its attached placeholder to the
// front of the list to be processed.
$build = [
[
'#lazy_builder' => [
self::class . '::lazyBuilderCheckCurrentUserCallback',
[],
],
'#create_placeholder' => TRUE,
],
[
// Add a space between placeholders.
'#markup' => ' ',
],
[
'#lazy_builder' => [
self::class . '::lazyBuilderCheckAccessCallback',
[],
],
'#create_placeholder' => TRUE,
],
];
$user2 = new UserSession([
'uid' => 2,
]);
$this->setCurrentUser($user2);
$expected = 'The current user id is 2. User 3 can administer modules.';
$output = (string) \Drupal::service(RendererInterface::class)->renderRoot($build);
$this->assertSame($expected, $output);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.