View source
<?php
namespace Drupal\Tests\user\Kernel\Condition;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
class UserRoleConditionTest extends KernelTestBase {
protected $manager;
protected $anonymous;
protected $authenticated;
protected $role;
protected static $modules = [
'system',
'user',
'field',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('user');
$this->manager = $this->container
->get('plugin.manager.condition');
Role::create([
'id' => RoleInterface::ANONYMOUS_ID,
'label' => 'Anonymous user',
])
->save();
Role::create([
'id' => RoleInterface::AUTHENTICATED_ID,
'label' => 'Authenticated user',
])
->save();
$rid = strtolower($this
->randomMachineName(8));
$label = $this
->randomString(8);
$role = Role::create([
'id' => $rid,
'label' => $label,
]);
$role
->save();
$this->role = $role;
$this->anonymous = User::create([
'name' => '',
'uid' => 0,
]);
$this->anonymous
->save();
$this->anonymous = User::load($this->anonymous
->id());
$this->authenticated = User::create([
'name' => $this
->randomMachineName(),
]);
$this->authenticated
->save();
$this->authenticated
->addRole($this->role
->id());
}
public function testConditions() {
$condition = $this->manager
->createInstance('user_role')
->setConfig('roles', [
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
])
->setContextValue('user', $this->anonymous);
$this
->assertFalse($condition
->execute(), 'Anonymous users fail role checks for authenticated.');
$this
->assertEquals('The user is a member of Authenticated user', $condition
->summary());
$condition
->setConfig('roles', [
RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID,
]);
$this
->assertTrue($condition
->execute(), 'Anonymous users pass role checks for anonymous.');
$this
->assertEquals('The user is a member of Anonymous user', $condition
->summary());
$condition
->setConfig('roles', [
RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
]);
$this
->assertTrue($condition
->execute(), 'Anonymous users pass role checks for anonymous or authenticated.');
$this
->assertEquals('The user is a member of Anonymous user, Authenticated user', $condition
->summary());
$condition
->setContextValue('user', $this->authenticated);
$this
->assertTrue($condition
->execute(), 'Authenticated users pass role checks for anonymous or authenticated.');
$condition
->setConfig('roles', [
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
]);
$this
->assertTrue($condition
->execute(), 'Authenticated users pass role checks for authenticated.');
$condition
->setConfig('negate', TRUE);
$this
->assertEquals('The user is not a member of Authenticated user', $condition
->summary());
$condition
->setConfig('roles', [
RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
]);
$this
->assertEquals('The user is not a member of Anonymous user, Authenticated user', $condition
->summary());
$condition
->setConfig('roles', [
$this->role
->id() => $this->role
->id(),
]);
$condition
->setConfig('negate', FALSE);
$this
->assertTrue($condition
->execute(), 'Authenticated user is a member of the custom role.');
$this
->assertEquals(new FormattableMarkup('The user is a member of @roles', [
'@roles' => $this->role
->label(),
]), $condition
->summary());
}
public function testLegacy() {
$this
->expectDeprecation('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980');
$condition = $this->manager
->createInstance('user_role', [
'roles' => [
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
],
'context' => [
'user' => $this->authenticated,
],
]);
$this
->assertTrue($condition
->execute(), 'Constructor injection of context and configuration working as anticipated.');
}
}