UserRolesCacheContextTest.php

Same filename and directory in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Cache/Context/UserRolesCacheContextTest.php
  2. 10 core/tests/Drupal/Tests/Core/Cache/Context/UserRolesCacheContextTest.php

Namespace

Drupal\Tests\Core\Cache\Context

File

core/tests/Drupal/Tests/Core/Cache/Context/UserRolesCacheContextTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Cache\Context;

use Drupal\Core\Cache\Context\UserRolesCacheContext;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests Drupal\Core\Cache\Context\UserRolesCacheContext.
 */
class UserRolesCacheContextTest extends UnitTestCase {
  
  /**
   * Tests calculated role.
   *
   * @legacy-covers ::getContext
   */
  public function testCalculatedRole() : void {
    $current_user = $this->prophesize(AccountInterface::class);
    // Ensure the ID is not 1. This cache context gives user 1 a special
    // superuser value.
    $current_user->id()
      ->willReturn(2);
    $current_user->getRoles()
      ->willReturn([
      'role1',
      'role2',
    ]);
    $cache_context = new UserRolesCacheContext($current_user->reveal());
    $this->assertSame('true', $cache_context->getContext('role1'));
    $this->assertSame('false', $cache_context->getContext('role-not-held'));
  }

}

Classes

Title Deprecated Summary
UserRolesCacheContextTest Tests Drupal\Core\Cache\Context\UserRolesCacheContext.

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