You are here

public function VoteDeletionTest::testVoteDeletion in Voting API 8.3

Tests deleting a vote.

File

tests/src/Functional/VoteDeletionTest.php, line 57

Class

VoteDeletionTest
Tests the deletion of votes.

Namespace

Drupal\Tests\votingapi\Functional

Code

public function testVoteDeletion() {
  $session = $this
    ->assertSession();
  $vote_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('vote');

  // Save a few votes.
  $values = [
    3 => 'source_1',
    4 => 'source_2',
    5 => 'source_2',
  ];
  foreach ($values as $value => $source) {
    $vote_storage
      ->create([
      'type' => 'vote',
      'entity_id' => $this->node
        ->id(),
      'entity_type' => 'node',
      'user_id' => 0,
      'value' => $value,
      'vote_source' => $source,
    ])
      ->save();
  }

  // Get vote id.
  $vote_id = \Drupal::entityQuery('vote')
    ->condition('vote_source', 'source_1')
    ->execute();
  $vote = $vote_storage
    ->load(reset($vote_id));
  $vote_owner = $vote
    ->getOwner()
    ->getDisplayName();
  $entity_type = $this->node
    ->getEntityType()
    ->getSingularLabel();
  $label = $this->node
    ->label();

  // Delete a vote.
  $this
    ->drupalGet('admin/vote/' . reset($vote_id) . '/delete');
  $session
    ->pageTextContains(t('You are about to delete a vote by @user on @entity-type @label. This action cannot be undone.', [
    '@user' => $vote_owner,
    '@entity-type' => $entity_type,
    '@label' => $label,
  ]));
  $this
    ->submitForm([], 'Delete');
  $session
    ->pageTextContains(t('The vote by @user on @entity-type @label has been deleted.', [
    '@user' => $vote_owner,
    '@entity-type' => $entity_type,
    '@label' => $label,
  ]));

  // Assert that the vote got deleted and other votes remain.
  $source_1_votes = $vote_storage
    ->getUserVotes(0, 'vote', 'node', 1, 'source_1');
  $this
    ->assertCount(0, $source_1_votes);
  $source_2_votes = $vote_storage
    ->getUserVotes(0, 'vote', 'node', 1, 'source_2');
  $this
    ->assertCount(2, $source_2_votes);
}