You are here

public function AccessTest::testUserFlag in Flag 8.4

Tests specific UserFlagType permissions.

File

tests/src/Kernel/AccessTest.php, line 164

Class

AccessTest
Tests related to access to flags.

Namespace

Drupal\Tests\flag\Kernel

Code

public function testUserFlag() {

  // A flag that shows on users profiles.
  $flag = Flag::create([
    'id' => 'A flag about users',
    'label' => $this
      ->randomString(),
    'entity_type' => 'user',
    'flag_type' => 'entity:user',
    'link_type' => 'reload',
    'flagTypeConfig' => [
      'extra_permissions' => [
        'owner',
      ],
    ],
    'linkTypeConfig' => [],
  ]);
  $flag
    ->save();
  $flag_id = $flag
    ->id();

  // Create a user who may flag her own user account.
  $user_alice = $this
    ->createUser([
    "flag {$flag_id} own user account",
    "unflag {$flag_id} own user account",
  ]);

  // Create a user who may flag the work of others.
  $user_bob = $this
    ->createUser([
    "flag {$flag_id} other user accounts",
    "unflag {$flag_id} other user accounts",
  ]);

  // For Alice selfies are permitted.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_alice, $user_alice)
    ->isAllowed());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_alice, $user_alice)
    ->isAllowed());

  // For Bob selfies are banned.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_bob, $user_bob)
    ->isNeutral());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_bob, $user_bob)
    ->isNeutral());

  // For alice flagging other people's profiles is banned.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_alice, $user_bob)
    ->isNeutral());
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_alice, $user_bob)
    ->isNeutral());

  // For Bob flagging other people's profiles is permitted.
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_bob, $user_alice)
    ->isAllowed());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_bob, $user_alice)
    ->isAllowed());

  // When no flaggable is supplied UserFlagType::actionAccess() tests are
  // bypassed.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_alice)
    ->isNeutral());
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_bob)
    ->isNeutral());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_alice)
    ->isNeutral());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_bob)
    ->isNeutral());
}