You are here

public function LikeAndDislikeTest::testVoting in Like & Dislike 8

Asserts module voting.

Test that voting (liking and disliking) properly works, including removing a like or dislike (if enabled) and changing a vote.

File

tests/src/FunctionalJavascript/LikeAndDislikeTest.php, line 364

Class

LikeAndDislikeTest
Provides a web test for like_and_dislike module.

Namespace

Drupal\Tests\like_and_dislike\FunctionalJavascript

Code

public function testVoting() {

  // Create a node and add a comment to it.
  $node = Node::create([
    'title' => 'Test node title',
    'type' => 'article',
  ]);
  $node
    ->save();
  $node_id = $node
    ->id();
  $comment = Comment::create([
    'subject' => 'Test subject',
    'comment_body' => 'Test body',
    'entity_id' => $node_id,
    'entity_type' => 'node',
    'node_type' => 'article',
    'field_name' => 'test_comment_field',
    'status' => CommentInterface::PUBLISHED,
    'uid' => $this->adminUser
      ->id(),
  ]);
  $comment
    ->save();
  $comment_id = $comment
    ->id();

  // Enable like and dislike for nodes and comments (test_comment_type)
  // and enable vote cancellation.
  $edit = [
    'enabled_types[node][enabled]' => TRUE,
    'enabled_types[node][bundle_info][bundles][article]' => TRUE,
    'enabled_types[comment][enabled]' => TRUE,
    'enabled_types[comment][bundle_info][bundles][test_comment_type]' => TRUE,
    'allow_cancel_vote' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/config/search/votingapi/like_and_dislike', $edit, t('Save configuration'));
  $this
    ->assertSession()
    ->pageTextContains('The configuration options have been saved.');

  // Update user with voting permissions.
  $user_roles = $this->adminUser
    ->getRoles();
  $user_role = end($user_roles);
  $edit = [
    $user_role . '[add or remove like votes on article of node]' => TRUE,
    $user_role . '[add or remove dislike votes on article of node]' => TRUE,
    $user_role . '[add or remove like votes on test_comment_type of comment]' => TRUE,
    $user_role . '[add or remove dislike votes on test_comment_type of comment]' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/people/permissions', $edit, 'Save permissions');

  // Toggle on visibility of the extra fields.
  $this
    ->drupalGet('admin/structure/types/manage/article/display');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Show row weights');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('fields[like_and_dislike][region]', 'content');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('fields[like_and_dislike][region]', 'content')
    ->isSelected());
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');
  $this
    ->drupalGet('admin/structure/comment/manage/test_comment_type/display');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('fields[like_and_dislike][region]', 'content');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('fields[like_and_dislike][region]', 'content')
    ->isSelected());
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // Verify that the node and comment don't have any like or dislike.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertLikesAndDislikes('node', $node_id);
  $this
    ->assertLikesAndDislikes('comment', $comment_id);

  // Add likes and dislikes and verify that the count increments.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->vote('like', 'node', $node_id, 'Your like vote was added.');
  $xpath = $this
    ->xpath('//*[@id="like-container-node-' . $node_id . '"]/a')[0];
  $this
    ->assertTrue($xpath
    ->hasAttribute('class'));
  $this
    ->assertLikesAndDislikes('node', $node_id, '1');
  $this
    ->assertLikesAndDislikes('comment', $comment_id);
  $this
    ->vote('dislike', 'comment', $comment_id, 'Your dislike vote was added.');
  $xpath = $this
    ->xpath('//*[@id="like-container-comment-' . $comment_id . '"]/a')[0];
  $this
    ->assertFalse($xpath
    ->hasAttribute('class'));
  $this
    ->assertLikesAndDislikes('node', $node_id, '1');
  $this
    ->assertLikesAndDislikes('comment', $comment_id, '0', '1');

  // Login as different users to further increment votes.
  $user2_permissions = [
    'access comments',
    'add or remove like votes on article of node',
    'add or remove dislike votes on article of node',
    'add or remove like votes on test_comment_type of comment',
    'add or remove dislike votes on test_comment_type of comment',
  ];
  $user2 = $this
    ->drupalCreateUser($user2_permissions);
  $this
    ->drupalLogin($user2);

  // Assert that icons are not marked as "voted".
  $this
    ->drupalGet('node/' . $node_id);
  $xpath = $this
    ->xpath('//*[@id="like-container-node-' . $node_id . '"]/a')[0];
  $this
    ->assertFalse($xpath
    ->hasAttribute('class'));
  $xpath = $this
    ->xpath('//*[@id="dislike-container-comment-' . $comment_id . '"]/a')[0];
  $this
    ->assertFalse($xpath
    ->hasAttribute('class'));
  $this
    ->vote('like', 'node', $node_id);
  $this
    ->assertLikesAndDislikes('node', $node_id, '2');
  $this
    ->assertLikesAndDislikes('comment', $comment_id, '0', '1');
  $this
    ->vote('like', 'comment', $comment_id);
  $this
    ->assertLikesAndDislikes('node', $node_id, '2');
  $this
    ->assertLikesAndDislikes('comment', $comment_id, '1', '1');

  // Vote the opposite, to swap the votes.
  $this
    ->vote('dislike', 'node', $node_id);
  $xpath = $this
    ->xpath('//*[@id="like-container-node-' . $node_id . '"]/a')[0];
  $this
    ->assertEquals('', $xpath
    ->getAttribute('class'));
  $this
    ->assertLikesAndDislikes('node', $node_id, '1', '1');
  $this
    ->assertLikesAndDislikes('comment', $comment_id, '1', '1');

  // Vote the same again to cancel the votes. At this point, user 2 voted to
  // dislike the article and like the comment.
  $this
    ->vote('dislike', 'node', $node_id, NULL, TRUE);
  $xpath = $this
    ->xpath('//*[@id="like-container-node-' . $node_id . '"]/a')[0];
  $this
    ->assertEquals('', $xpath
    ->getAttribute('class'));
  $this
    ->assertLikesAndDislikes('node', $node_id, '1');
  $this
    ->assertLikesAndDislikes('comment', $comment_id, '1', '1');

  // Disable vote cancellation.
  $this
    ->drupalLogin($this->adminUser);
  $edit = [
    'enabled_types[node][enabled]' => TRUE,
    'enabled_types[comment][enabled]' => TRUE,
    'enabled_types[comment][bundle_info][bundles][test_comment_type]' => TRUE,
    'allow_cancel_vote' => FALSE,
  ];
  $this
    ->drupalPostForm('admin/config/search/votingapi/like_and_dislike', $edit, t('Save configuration'));
  $this
    ->assertSession()
    ->pageTextContains('The configuration options have been saved.');
  $this
    ->drupalLogin($user2);

  // Unsuccessfully try to cancel the comment like vote.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->vote('like', 'comment', $comment_id, 'You are not allowed to vote the same way multiple times.', TRUE);
  $this
    ->assertLikesAndDislikes('node', $node_id, '1');
  $this
    ->assertLikesAndDislikes('comment', $comment_id, '1', '1');

  // Login as a user without permission to add or remove votes.
  $user3_permissions = [
    'access comments',
  ];
  $user3 = $this
    ->drupalCreateUser($user3_permissions);
  $this
    ->drupalLogin($user3);

  // Verify that the votes are correctly displayed, but are not links.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertLikesAndDislikes('node', $node_id, '1', '0', TRUE);
  $this
    ->assertLikesAndDislikes('comment', $comment_id, '1', '1', TRUE);
}