You are here

public function UserFlagTypeTest::testFlagSelf in Flag 8.4

Tests that user can flag themselves when and only when appropriate.

File

tests/src/Functional/UserFlagTypeTest.php, line 38

Class

UserFlagTypeTest
Tests user flag type integrations.

Namespace

Drupal\Tests\flag\Functional

Code

public function testFlagSelf() {
  $flag = $this
    ->createFlagFromArray([
    'link_type' => 'reload',
    'entity_type' => 'user',
    'bundles' => array_keys(\Drupal::service('entity_type.bundle.info')
      ->getBundleInfo('user')),
    'flag_type' => $this
      ->getFlagType('user'),
    'show_on_profile' => TRUE,
    'flagTypeConfig' => [
      // Create extra permissions to self flag.
      'extra_permissions' => [
        'owner',
      ],
    ],
  ]);

  // User can flag their own work.
  $user = $this
    ->createUser([
    'flag ' . $flag
      ->id() . ' own user account',
    'unflag ' . $flag
      ->id() . ' own user account',
    'administer flags',
    'administer flagging display',
    'administer flagging fields',
    'administer node display',
    'administer nodes',
  ]);
  $this
    ->drupalLogin($user);

  // Check the state of the extra permssions checkbox.
  $this
    ->drupalGet('admin/structure/flags/manage/' . $flag
    ->id());
  $this
    ->assertFieldChecked('edit-extra-permissions-owner');

  // Assert flag appears on the profile page.
  $this
    ->drupalGet('user/' . $user
    ->id());
  $this
    ->assertLink($flag
    ->getShortText('flag'));

  // Uncheck extra permssions.
  $edit = [
    'extra_permissions[owner]' => FALSE,
  ];
  $this
    ->drupalPostForm('admin/structure/flags/manage/' . $flag
    ->id(), $edit, $this
    ->t('Save Flag'));

  // Confirm extra permissions is unchecked.
  $this
    ->drupalGet('admin/structure/flags/manage/' . $flag
    ->id());
  $this
    ->assertNoFieldChecked('edit-extra-permissions-owner');

  // Assert the flag disapears from the profile page.
  $this
    ->drupalGet('user/' . $user
    ->id());
  $this
    ->assertNoLink($flag
    ->getShortText('flag'));
}