Commit 2231e592 authored by catch's avatar catch
Browse files

Issue #3101664 by bvoynick, claudiu.cristea, Matroskeen, Josh Waihi,...

Issue #3101664 by bvoynick, claudiu.cristea, Matroskeen, Josh Waihi, smustgrave, Wim Leers: UserRolesCacheContext reports wrong user.roles
parent ad25f53a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ public function getContext($role = NULL) {
      return implode(',', $this->user->getRoles());
    }
    else {
      return (in_array($role, $this->user->getRoles()) ? '0' : '1');
      return (in_array($role, $this->user->getRoles()) ? 'true' : 'false');
    }
  }

+28 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\Core\Cache\Context;

use Drupal\Core\Cache\Context\UserRolesCacheContext;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\Core\Cache\Context\UserRolesCacheContext
 * @group Cache
 */
class UserRolesCacheContextTest extends UnitTestCase {

  /**
   * @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'));
  }

}