function UserRoleConditionTest::setUp

Same name in other branches
  1. 9 core/modules/user/tests/src/Kernel/Condition/UserRoleConditionTest.php \Drupal\Tests\user\Kernel\Condition\UserRoleConditionTest::setUp()
  2. 8.9.x core/modules/user/tests/src/Kernel/Condition/UserRoleConditionTest.php \Drupal\Tests\user\Kernel\Condition\UserRoleConditionTest::setUp()
  3. 11.x core/modules/user/tests/src/Kernel/Condition/UserRoleConditionTest.php \Drupal\Tests\user\Kernel\Condition\UserRoleConditionTest::setUp()

Overrides KernelTestBase::setUp

File

core/modules/user/tests/src/Kernel/Condition/UserRoleConditionTest.php, line 56

Class

UserRoleConditionTest
Tests the user role condition.

Namespace

Drupal\Tests\user\Kernel\Condition

Code

protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('user');
    $this->manager = $this->container
        ->get('plugin.manager.condition');
    // Set up the authenticated and anonymous roles.
    Role::create([
        'id' => RoleInterface::ANONYMOUS_ID,
        'label' => 'Anonymous user',
    ])->save();
    Role::create([
        'id' => RoleInterface::AUTHENTICATED_ID,
        'label' => 'Authenticated user',
    ])->save();
    // Create new role.
    $rid = $this->randomMachineName(8);
    $label = $this->randomString(8);
    $role = Role::create([
        'id' => $rid,
        'label' => $label,
    ]);
    $role->save();
    $this->role = $role;
    // Setup an anonymous user for our tests.
    $this->anonymous = User::create([
        'name' => '',
        'uid' => 0,
    ]);
    $this->anonymous
        ->save();
    // Loading the anonymous user adds the correct role.
    $this->anonymous = User::load($this->anonymous
        ->id());
    // Setup an authenticated user for our tests.
    $this->authenticated = User::create([
        'name' => $this->randomMachineName(),
    ]);
    $this->authenticated
        ->save();
    // Add the custom role.
    $this->authenticated
        ->addRole($this->role
        ->id());
}

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