You are here

public function FlagActionTest::testFlagActions in Flag 8.4

Tests direct use of the action plugins.

File

tests/src/Kernel/FlagActionTest.php, line 88

Class

FlagActionTest
Test flag actions are added/removed when flags are added/deleted.

Namespace

Drupal\Tests\flag\Kernel

Code

public function testFlagActions() {

  /** @var \Drupal\flag\FlagInterface $entity_flag */
  $entity_flag = Flag::create([
    'id' => mb_strtolower($this
      ->randomMachineName()),
    'label' => $this
      ->randomString(),
    'entity_type' => 'entity_test',
    'flag_type' => 'entity:entity_test',
    'link_type' => 'reload',
    'flagTypeConfig' => [],
    'linkTypeConfig' => [],
  ]);
  $entity_flag
    ->save();
  $test_entity = EntityTest::create();
  $test_entity
    ->save();

  /** @var \Drupal\system\ActionConfigEntityInterface $action */
  $action = $this->container
    ->get('entity_type.manager')
    ->getStorage('action')
    ->load('flag_action.' . $entity_flag
    ->id() . '_flag');
  $plugin = $action
    ->getPlugin();
  $plugin
    ->execute($test_entity);
  $this
    ->assertTrue($entity_flag
    ->isFlagged($test_entity, $this->account));

  // Access should be false for this user.
  $this
    ->assertFalse($plugin
    ->access($test_entity, $this->account));

  // Admin should have access.
  $this
    ->assertTrue($plugin
    ->access($test_entity, $this->admin));

  // Unflag.
  $this->entityTypeManager
    ->getStorage('flagging')
    ->resetCache();
  $action = $this->entityTypeManager
    ->getStorage('action')
    ->load('flag_action.' . $entity_flag
    ->id() . '_unflag');
  $plugin = $action
    ->getPlugin();
  $plugin
    ->execute($test_entity);

  // @todo Flagging cache cannot be cleared, so this check cannot happen.
  // @see https://www.drupal.org/node/2801423
  // $this->assertFalse($entity_flag->isFlagged($test_entity, $this->account));
  // Access should be false for this user.
  $this
    ->assertFalse($plugin
    ->access($test_entity, $this->account));

  // Admin should have access.
  $this
    ->assertTrue($plugin
    ->access($test_entity, $this->admin));
}