You are here

public function FlagActionTest::testFlaggingDeleteAction in Flag 8.4

Tests the flagging delete action.

File

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

Class

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

Namespace

Drupal\Tests\flag\Kernel

Code

public function testFlaggingDeleteAction() {

  // Action should be available upon install.

  /** @var \Drupal\system\ActionConfigEntityInterface $action */
  $action = $this->container
    ->get('entity_type.manager')
    ->getStorage('action')
    ->load('flag_delete_flagging');
  $plugin = $action
    ->getPlugin();
  $this
    ->assertInstanceOf(DeleteFlaggingAction::class, $plugin);

  /** @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();

  // Flag the entity.
  $test_entity = EntityTest::create();
  $test_entity
    ->save();
  $this->flagService
    ->flag($entity_flag, $test_entity);
  $flaggings = $this->flagService
    ->getEntityFlaggings($entity_flag, $test_entity);
  $flagging = reset($flaggings);

  // Verify plugin access for other user is false.
  $other_user = $this
    ->createUser();
  $this
    ->assertFalse($plugin
    ->access($flagging, $other_user));
  $access = $plugin
    ->access($flagging, $other_user, TRUE);
  $this
    ->assertFalse($access
    ->isAllowed());

  // Access for flag owner should be true.
  $this
    ->assertFalse($plugin
    ->access($flagging));
  $access = $plugin
    ->access($flagging, NULL, TRUE);
  $this
    ->assertFalse($access
    ->isAllowed());

  // Execute and verify the flagging is gone.
  $plugin
    ->execute($flagging);
  $this
    ->assertEmpty($this->flagService
    ->getEntityFlaggings($entity_flag, $test_entity));
}