Commit 0b47db51 authored by larowlan's avatar larowlan

Issue #2998335 by tim.plunkett: [regression]...

Issue #2998335 by tim.plunkett: [regression] \Drupal\user\ContextProvider\CurrentUserContext broken for anonymous users
parent bbc3495c
......@@ -53,9 +53,14 @@ public function getRuntimeContexts(array $unqualified_context_ids) {
// @todo Do not validate protected fields to avoid bug in TypedData,
// remove this in https://www.drupal.org/project/drupal/issues/2934192.
$current_user->_skipProtectedUserFieldConstraint = TRUE;
$context = EntityContext::fromEntity($current_user, $this->t('Current user'));
}
else {
// If not user is available, provide an empty context object.
$context = EntityContext::fromEntityTypeId('user', $this->t('Current user'));
}
$context = EntityContext::fromEntity($current_user, $this->t('Current user'));
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['user']);
$context->addCacheableDependency($cacheability);
......
<?php
namespace Drupal\Tests\user\Kernel\ContextProvider;
use Drupal\Core\Session\AccountInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
/**
* @coversDefaultClass \Drupal\user\ContextProvider\CurrentUserContext
*
* @group user
*/
class CurrentUserContextTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['system', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['sequences']);
$this->installEntitySchema('user');
}
/**
* @covers ::getAvailableContexts
*/
public function testGetAvailableContexts() {
$context_repository = $this->container->get('context.repository');
// Test an authenticated account.
$authenticated = User::create([
'name' => $this->randomMachineName(),
]);
$authenticated->save();
$authenticated = User::load($authenticated->id());
$this->container->get('current_user')->setAccount($authenticated);
$contexts = $context_repository->getAvailableContexts();
$this->assertArrayHasKey('@user.current_user_context:current_user', $contexts);
$this->assertSame('entity:user', $contexts['@user.current_user_context:current_user']->getContextDefinition()->getDataType());
$this->assertTrue($contexts['@user.current_user_context:current_user']->hasContextValue());
$this->assertTrue($contexts['@user.current_user_context:current_user']->getContextValue());
// Test an anonymous account.
$anonymous = $this->prophesize(AccountInterface::class);
$anonymous->id()->willReturn(0);
$this->container->get('current_user')->setAccount($anonymous->reveal());
$contexts = $context_repository->getAvailableContexts();
$this->assertArrayHasKey('@user.current_user_context:current_user', $contexts);
$this->assertSame('entity:user', $contexts['@user.current_user_context:current_user']->getContextDefinition()->getDataType());
$this->assertFalse($contexts['@user.current_user_context:current_user']->hasContextValue());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment