You are here

protected function AccessTest::setTeamRolePermissionsOnUi in Apigee Edge 8

Sets team role permissions.

The team role permission admin UI is tested properly while changing the permissions.

Parameters

string $role_name: The ID of a team role.

array $permissions: Team role permissions to enable.

2 calls to AccessTest::setTeamRolePermissionsOnUi()
AccessTest::teamAccessTest in modules/apigee_edge_teams/tests/src/Functional/AccessTest.php
Tests team, team membership level and admin permissions.
AccessTest::teamRoleAccessTest in modules/apigee_edge_teams/tests/src/Functional/AccessTest.php
Tests team roles related UIs, permissions.

File

modules/apigee_edge_teams/tests/src/Functional/AccessTest.php, line 661

Class

AccessTest
Teams module access test.

Namespace

Drupal\Tests\apigee_edge_teams\Functional

Code

protected function setTeamRolePermissionsOnUi(string $role_name, array $permissions) {

  // Save the original logged in user if there is any.
  // Note: The account switcher service is not working as it is expected this
  // is the reason why we need this workaround.
  $oldNotRootLoggedInUser = NULL;
  if ($this->loggedInUser && $this->loggedInUser
    ->id() != $this->rootUser
    ->id()) {
    $oldNotRootLoggedInUser = clone $this->loggedInUser;
  }

  // Save permissions with admin user.
  if ($oldNotRootLoggedInUser === NULL || $oldNotRootLoggedInUser
    ->id() !== $this->rootUser
    ->id()) {
    $this
      ->drupalLogin($this->rootUser);
  }
  $permission_changes = [];
  foreach (array_keys(self::TEAM_MEMBER_PERMISSION_MATRIX) as $permission) {
    $permission_changes["{$role_name}[{$permission}]"] = in_array($permission, $permissions);
  }
  $this
    ->drupalPostForm(Url::fromRoute('apigee_edge_teams.settings.team.permissions'), $permission_changes, 'Save permissions');

  // Dump permission configuration to the HTML output.
  $this
    ->drupalGet(Url::fromRoute('apigee_edge_teams.settings.team.permissions'));

  // Because changes made on the UI therefore _this_ instance of the team role
  // storage must be cleared manually.
  $this->teamRoleStorage
    ->resetCache([
    $role_name,
  ]);

  // Log back in with the old, not root user.
  if ($oldNotRootLoggedInUser) {
    if ($oldNotRootLoggedInUser
      ->id() === $this->account
      ->id()) {
      $this
        ->drupalLogin($this->account);
    }
    else {
      throw new \Exception("Unable to switch back to the originally logged user because it was neither the root user nor the simple authenticated user. Its user id: {$oldNotRootLoggedInUser->id()}.");
    }
  }
}