You are here

public function ProfileRoleAccessTest::testProfileCreate in Profile 8

Tests profile create role access checks.

File

tests/src/Kernel/ProfileRoleAccessTest.php, line 119

Class

ProfileRoleAccessTest
Tests profile role access handling.

Namespace

Drupal\Tests\profile\Kernel

Code

public function testProfileCreate() {
  $user = $this
    ->createUser([], [
    "create {$this->type1->id()} profile",
    "create {$this->type2->id()} profile",
    "create {$this->type3->id()} profile",
  ]);

  // The user initially has no roles, so they can only access the first
  // profile type, which isn't restricted by role.
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type1
    ->id(), $user, [
    'profile_owner' => $user,
  ]));
  $this
    ->assertFalse($this->accessHandler
    ->createAccess($this->type2
    ->id(), $user, [
    'profile_owner' => $user,
  ]));
  $this
    ->assertFalse($this->accessHandler
    ->createAccess($this->type3
    ->id(), $user, [
    'profile_owner' => $user,
  ]));

  // No role check is performed when the profile_owner isn't passed.
  $this->accessHandler
    ->resetCache();
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type1
    ->id(), $user));
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type2
    ->id(), $user));
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type3
    ->id(), $user));

  // With role1, the user can access the first and the third profile type.
  $this->accessHandler
    ->resetCache();
  $user
    ->addRole($this->role1
    ->id());
  $user
    ->save();
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type1
    ->id(), $user, [
    'profile_owner' => $user,
  ]));
  $this
    ->assertFalse($this->accessHandler
    ->createAccess($this->type2
    ->id(), $user, [
    'profile_owner' => $user,
  ]));
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type3
    ->id(), $user, [
    'profile_owner' => $user,
  ]));

  // With role2, the user can access all three profile types.
  $this->accessHandler
    ->resetCache();
  $user
    ->addRole($this->role2
    ->id());
  $user
    ->save();
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type1
    ->id(), $user, [
    'profile_owner' => $user,
  ]));
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type2
    ->id(), $user, [
    'profile_owner' => $user,
  ]));
  $this
    ->assertTrue($this->accessHandler
    ->createAccess($this->type3
    ->id(), $user, [
    'profile_owner' => $user,
  ]));
}