You are here

protected function UserRoleConditionTest::setUp in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/Condition/UserRoleConditionTest.php \Drupal\user\Tests\Condition\UserRoleConditionTest::setUp()

Performs setup tasks before each individual test method is run.

Overrides KernelTestBase::setUp

File

core/modules/user/src/Tests/Condition/UserRoleConditionTest.php, line 61
Contains \Drupal\user\Tests\Condition\UserRoleConditionTest.

Class

UserRoleConditionTest
Tests the user role condition.

Namespace

Drupal\user\Tests\Condition

Code

protected function setUp() {
  parent::setUp();
  $this
    ->installSchema('system', 'sequences');
  $this
    ->installEntitySchema('user');
  $this->manager = $this->container
    ->get('plugin.manager.condition');

  // Set up the authenticated and anonymous roles.
  Role::create(array(
    'id' => RoleInterface::ANONYMOUS_ID,
    'label' => 'Anonymous user',
  ))
    ->save();
  Role::create(array(
    'id' => RoleInterface::AUTHENTICATED_ID,
    'label' => 'Authenticated user',
  ))
    ->save();

  // Create new role.
  $rid = strtolower($this
    ->randomMachineName(8));
  $label = $this
    ->randomString(8);
  $role = Role::create(array(
    'id' => $rid,
    'label' => $label,
  ));
  $role
    ->save();
  $this->role = $role;

  // Setup an anonymous user for our tests.
  $this->anonymous = User::create(array(
    '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(array(
    'name' => $this
      ->randomMachineName(),
  ));
  $this->authenticated
    ->save();

  // Add the custom role.
  $this->authenticated
    ->addRole($this->role
    ->id());
}