You are here

public function AccessTest::testDefault in Flag 8.4

Tests default hook_flag_action_access() mechanism.

File

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

Class

AccessTest
Tests related to access to flags.

Namespace

Drupal\Tests\flag\Kernel

Code

public function testDefault() {

  // Create a flag.
  $flag = Flag::create([
    'id' => 'example',
    'label' => $this
      ->randomString(),
    'entity_type' => 'node',
    'bundles' => [
      'article',
    ],
    'flag_type' => 'entity:node',
    'link_type' => 'reload',
    'flagTypeConfig' => [],
    'linkTypeConfig' => [],
  ]);
  $flag
    ->save();

  // Create a user who may flag and unflag.
  $user_alice = $this
    ->createUser([
    'administer flags',
    'flag example',
    'unflag example',
  ]);

  // Create a user who may only flag.
  $user_jill = $this
    ->createUser([
    'administer flags',
    'flag example',
  ]);

  // Create a user who may not flag or unflag.
  $user_bob = $this
    ->createUser();
  $article = Node::create([
    'type' => 'article',
    'title' => 'Article node',
  ]);
  $article
    ->save();

  // Test with both permissions.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_alice, $article)
    ->isAllowed(), 'Alice can flag.');
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_alice, $article)
    ->isAllowed(), 'Alice can unflag.');

  // Test with only flag permission.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_jill, $article)
    ->isAllowed(), 'Jill can flag.');
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_jill, $article)
    ->isNeutral(), 'Jill cannot unflag.');

  // Test without permissions.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_bob, $article)
    ->isNeutral(), 'Bob cannot flag.');
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_bob, $article)
    ->isNeutral(), 'Bob cannot unflag..');
}