You are here

public function LinkTypeConfirmFormTest::doFlagUnflagNode in Flag 8.4

Create a node, flag it and unflag it.

1 call to LinkTypeConfirmFormTest::doFlagUnflagNode()
LinkTypeConfirmFormTest::testCreateConfirmFlag in tests/src/Functional/LinkTypeConfirmFormTest.php
Test the confirm form link type.

File

tests/src/Functional/LinkTypeConfirmFormTest.php, line 53

Class

LinkTypeConfirmFormTest
Tests the confirm form link type.

Namespace

Drupal\Tests\flag\Functional

Code

public function doFlagUnflagNode() {
  $node = $this
    ->drupalCreateNode([
    'type' => $this->nodeType,
  ]);
  $node_id = $node
    ->id();
  $flag_id = $this->flag
    ->id();

  // Grant the flag permissions to the authenticated role, so that both
  // users have the same roles and share the render cache.
  $this
    ->grantFlagPermissions($this->flag);

  // Create and login a new user.
  $user_1 = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user_1);

  // Get the flag count before the flagging, querying the database directly.
  $flag_count_pre = \Drupal::database()
    ->query('SELECT count FROM {flag_counts}
      WHERE flag_id = :flag_id AND entity_type = :entity_type AND entity_id = :entity_id', [
    ':flag_id' => $flag_id,
    ':entity_type' => 'node',
    ':entity_id' => $node_id,
  ])
    ->fetchField();

  // Click the flag link.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->clickLink($this->flag
    ->getShortText('flag'));

  // Check if we have the confirm form message displayed.
  $this
    ->assertSession()
    ->pageTextContains($this->flagConfirmMessage);

  // Submit the confirm form.
  $this
    ->drupalPostForm('flag/confirm/flag/' . $flag_id . '/' . $node_id, [], $this->createButtonText);

  // Check that the node is flagged.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertLink($this->flag
    ->getShortText('unflag'));

  // Check the flag count was incremented.
  $flag_count_flagged = \Drupal::database()
    ->query('SELECT count FROM {flag_counts}
      WHERE flag_id = :flag_id AND entity_type = :entity_type AND entity_id = :entity_id', [
    ':flag_id' => $flag_id,
    ':entity_type' => 'node',
    ':entity_id' => $node_id,
  ])
    ->fetchField();
  $this
    ->assertEqual($flag_count_flagged, $flag_count_pre + 1, "The flag count was incremented.");

  // Unflag the node.
  $this
    ->clickLink($this->flag
    ->getShortText('unflag'));

  // Check if we have the confirm form message displayed.
  $this
    ->assertSession()
    ->pageTextContains($this->unflagConfirmMessage);

  // Submit the confirm form.
  $this
    ->drupalPostForm(NULL, [], $this->deleteButtonText);

  // Check that the node is no longer flagged.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertLink($this->flag
    ->getShortText('flag'));

  // Check the flag count was decremented.
  $flag_count_unflagged = \Drupal::database()
    ->query('SELECT count FROM {flag_counts}
      WHERE flag_id = :flag_id AND entity_type = :entity_type AND entity_id = :entity_id', [
    ':flag_id' => $flag_id,
    ':entity_type' => 'node',
    ':entity_id' => $node_id,
  ])
    ->fetchField();
  $this
    ->assertEqual($flag_count_unflagged, $flag_count_flagged - 1, "The flag count was decremented.");
}