function SiteContextTest::testGetAvailableContexts

@covers ::getAvailableContexts

File

tests/src/Kernel/ContextProvider/SiteContextTest.php, line 39

Class

SiteContextTest
@coversDefaultClass \Drupal\user\ContextProvider\CurrentUserContext

Namespace

Drupal\Tests\rules\Kernel\ContextProvider

Code

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('@rules.site_context:site', $contexts);
    $this->assertSame('site', $contexts['@rules.site_context:site']->getContextDefinition()
        ->getDataType());
    $this->assertTrue($contexts['@rules.site_context:site']->hasContextValue());
    $this->assertNotNull($contexts['@rules.site_context:site']->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('@rules.site_context:site', $contexts);
    $this->assertSame('site', $contexts['@rules.site_context:site']->getContextDefinition()
        ->getDataType());
    $this->assertTrue($contexts['@rules.site_context:site']->hasContextValue());
}