function CurrentUserContextTest::testGetAvailableContexts

Same name in other branches
  1. 9 core/modules/user/tests/src/Kernel/ContextProvider/CurrentUserContextTest.php \Drupal\Tests\user\Kernel\ContextProvider\CurrentUserContextTest::testGetAvailableContexts()
  2. 8.9.x core/modules/user/tests/src/Kernel/ContextProvider/CurrentUserContextTest.php \Drupal\Tests\user\Kernel\ContextProvider\CurrentUserContextTest::testGetAvailableContexts()
  3. 10 core/modules/user/tests/src/Kernel/ContextProvider/CurrentUserContextTest.php \Drupal\Tests\user\Kernel\ContextProvider\CurrentUserContextTest::testGetAvailableContexts()

@covers ::getAvailableContexts

File

core/modules/user/tests/src/Kernel/ContextProvider/CurrentUserContextTest.php, line 35

Class

CurrentUserContextTest
@coversDefaultClass \Drupal\user\ContextProvider\CurrentUserContext

Namespace

Drupal\Tests\user\Kernel\ContextProvider

Code

public function testGetAvailableContexts() : void {
    $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->assertNotNull($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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.