You are here

public function AccessTest::testOwnersAccess in Flag 8.4

Tests owners access to flaggables.

Authors own articles - and can only flag their own work. Editors own articles - but can only flag the work of others.

File

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

Class

AccessTest
Tests related to access to flags.

Namespace

Drupal\Tests\flag\Kernel

Code

public function testOwnersAccess() {

  // A review flag with extra permissions set.
  $flag = Flag::create([
    'id' => 'me_myself_and_I',
    'label' => 'Self Review Flag',
    'entity_type' => 'node',
    'bundles' => [
      'article',
    ],
    'flag_type' => 'entity:node',
    'link_type' => 'reload',
    'flagTypeConfig' => [
      'extra_permissions' => [
        'owner',
      ],
    ],
    'linkTypeConfig' => [],
  ]);
  $flag
    ->save();
  $flag_id = $flag
    ->id();

  // Give authors permission to flag their own work.
  $user_author = $this
    ->createUser([
    "flag {$flag_id} own items",
    "unflag {$flag_id} own items",
  ]);

  // Editors get permission.
  $user_editor = $this
    ->createUser([
    "flag {$flag_id} other items",
    "unflag {$flag_id} other items",
  ]);

  // Article is owned by Author.
  $article_by_author = Node::create([
    'type' => 'article',
    'title' => 'Article node',
  ]);
  $article_by_author
    ->setOwner($user_author);
  $article_by_author
    ->save();

  // Article owned by editor (which NO one can flag or unflag).
  $article_by_editor = Node::create([
    'type' => 'article',
    'title' => 'Article node',
  ]);
  $article_by_editor
    ->setOwner($user_editor);
  $article_by_editor
    ->save();

  // Author can self review his or her own work.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_author, $article_by_author)
    ->isAllowed());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_author, $article_by_author)
    ->isAllowed());

  // Author can review others work.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_author, $article_by_editor)
    ->isNeutral());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_author, $article_by_editor)
    ->isNeutral());

  // Editors should be able to access work that is not their own.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_editor, $article_by_author)
    ->isAllowed());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_editor, $article_by_author)
    ->isAllowed());

  // Editors should not get access to the self review flag.
  $this
    ->assertTrue($flag
    ->actionAccess('flag', $user_editor, $article_by_editor)
    ->isNeutral());
  $this
    ->assertTrue($flag
    ->actionAccess('unflag', $user_editor, $article_by_editor)
    ->isNeutral());

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