You are here

public function LikeAndDislikeTest::testVisibility in Like & Dislike 8

Tests likes visibility.

Test that visibility of likes an dislikes can be correctly changed using the settings form and the extra field visibility setting.

File

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

Class

LikeAndDislikeTest
Provides a web test for like_and_dislike module.

Namespace

Drupal\Tests\like_and_dislike\FunctionalJavascript

Code

public function testVisibility() {

  // Create a node.
  $node = Node::create([
    'title' => 'Test node title',
    'type' => 'article',
  ]);
  $node
    ->save();

  // Enable custom display settings for the teaser view mode.
  $this
    ->drupalGet('admin/structure/types/manage/article/display');
  $this
    ->click('#edit-modes summary');
  $edit = [
    'display_modes_custom[teaser]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // Enable like an dislike for our article node type, disable for our
  // 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,
    'hide_vote_widget' => FALSE,
  ];
  $this
    ->drupalPostForm('admin/config/search/votingapi/like_and_dislike', $edit, t('Save configuration'));
  $this
    ->assertSession()
    ->pageTextContains('The configuration options have been saved.');
  $this
    ->assertFieldChecked('edit-enabled-types-node-enabled');
  $this
    ->assertFieldChecked('edit-enabled-types-comment-enabled');
  $this
    ->assertFieldChecked('edit-enabled-types-node-bundle-info-bundles-article');
  $this
    ->assertNoFieldChecked('edit-enabled-types-comment-bundle-info-bundles-comment');
  $this
    ->assertFieldChecked('edit-enabled-types-comment-bundle-info-bundles-test-comment-type');
  $this
    ->assertFieldChecked('edit-allow-cancel-vote');
  $this
    ->assertNoFieldChecked('edit-hide-vote-widget');

  // Verify there are new like and dislike permissions.
  $this
    ->drupalGet('admin/people/permissions');
  $this
    ->assertSession()
    ->pageTextContains('Content (Article): add/remove Like vote');
  $this
    ->assertSession()
    ->pageTextContains('Content (Article): add/remove Dislike vote');
  $this
    ->assertSession()
    ->pageTextNotContains('Comment (Default comments): add/remove Like vote');
  $this
    ->assertSession()
    ->pageTextNotContains('Comment (Default comments): add/remove Dislike vote');
  $this
    ->assertSession()
    ->pageTextContains('Comment (Test_comment_type): add/remove Like vote');
  $this
    ->assertSession()
    ->pageTextContains('Comment (Test_comment_type): add/remove Dislike vote');

  // Update the user with like and dislike 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(NULL, $edit, 'Save permissions');

  // Verify that like and dislike field is showing up as a field for default
  // view mode and that it is disabled by default.
  $this
    ->drupalGet('admin/structure/types/manage/article/display');
  $this
    ->assertSession()
    ->pageTextContains('Like and dislike');
  $this
    ->assertOptionSelected('edit-fields-like-and-dislike-region', 'hidden');

  // Same for teaser view mode.
  $this
    ->drupalGet('admin/structure/types/manage/article/display/teaser');
  $this
    ->assertSession()
    ->pageTextContains('Like and dislike');
  $this
    ->assertOptionSelected('edit-fields-like-and-dislike-region', 'hidden');

  // Toggle on visibility of the extra field for default view mode.
  $this
    ->drupalGet('admin/structure/types/manage/article/display');
  $this
    ->assertSession()
    ->waitForElementVisible('css', '[name="fields[like_and_dislike][region]"]');
  $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
    ->assertFalse($this
    ->assertSession()
    ->optionExists('edit-fields-like-and-dislike-region', 'hidden')
    ->isSelected());

  // Verify that like and dislike are properly displayed as links.
  $node_id = $node
    ->id();
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertLikesAndDislikes('node', $node_id);

  // Verify that like and dislike aren't showing up on teaser view mode.
  $teaser_render_array = $this
    ->drupalBuildEntityView($node, 'teaser');
  $this
    ->assertFalse(isset($teaser_render_array['like_and_dislike']));

  // Toggle off visibility of like and dislike for default view mode and on
  // for teaser mode, for nodes.
  $this
    ->drupalGet('admin/structure/types/manage/article/display');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('fields[like_and_dislike][region]', 'hidden');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('fields[like_and_dislike][region]', 'hidden')
    ->isSelected());
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');
  $this
    ->assertOptionSelected('edit-fields-like-and-dislike-region', 'hidden');
  $this
    ->drupalPostForm('admin/structure/types/manage/article/display/teaser', [
    'fields[like_and_dislike][region]' => 'content',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('edit-fields-like-and-dislike-region', 'hidden')
    ->isSelected());

  // Verify that like and dislike are no longer showing up on default view
  // mode.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertSession()
    ->pageTextNotContains('Like');
  $this
    ->assertSession()
    ->pageTextNotContains('Dislike');

  // Verify that like and dislike are now showing on teaser view mode.
  $teaser_render_array = $this
    ->drupalBuildEntityView($node, 'teaser');
  $this
    ->assertTrue(isset($teaser_render_array['like_and_dislike']));

  // Add a comment to this node.
  $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();

  // Verify that like and dislike are not showing up for the comment.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertSession()
    ->pageTextNotContains('Like');
  $this
    ->assertSession()
    ->pageTextNotContains('Dislike');

  // Disable like and dislike for nodes and enable for comments.
  $edit = [
    'enabled_types[node][enabled]' => FALSE,
    '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.');

  // Verify that like and dislike are no longer showing up for nodes.
  $teaser_render_array = $this
    ->drupalBuildEntityView($node, 'teaser');
  $this
    ->assertFalse(isset($teaser_render_array['like_and_dislike']));

  // Verify that like an dislike are not showing up for comments yet.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertSession()
    ->pageTextNotContains('Like');
  $this
    ->assertSession()
    ->pageTextNotContains('Dislike');

  // Toggle on visibility of like and dislike for the default view mode for
  // comments.
  $this
    ->drupalGet('admin/structure/comment/manage/test_comment_type/display');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('fields[like_and_dislike][region]', 'content');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // Verify that like and dislike are now showing for the comment.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertLikesAndDislikes('comment', $comment_id);

  // Enable and toggle on visibility of like and dislike for both nodes and
  // comments.
  $edit = [
    'enabled_types[node][enabled]' => TRUE,
    'enabled_types[node][bundle_info][bundles][article]' => 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.');
  $this
    ->drupalGet('admin/structure/types/manage/article/display');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('fields[like_and_dislike][region]', 'content');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // Verify that both are showing up on the default view mode.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertLikesAndDislikes('node', $node_id);
  $this
    ->assertLikesAndDislikes('comment', $comment_id);

  // Turn on hide vote widget permission.
  $edit = [
    'hide_vote_widget' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/config/search/votingapi/like_and_dislike', $edit, t('Save configuration'));
  $this
    ->assertSession()
    ->pageTextContains('The configuration options have been saved.');
  $this
    ->assertFieldChecked('edit-hide-vote-widget');

  // Turn off dislike permission for node and comment.
  $this
    ->drupalGet('admin/people/permissions');
  $edit = [
    $user_role . '[add or remove dislike votes on article of node]' => FALSE,
    $user_role . '[add or remove dislike votes on test_comment_type of comment]' => FALSE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save permissions');

  // Verify that dislike icon is not showed in default view mode.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertVotingIconExistence('node', $node_id, 'like', TRUE);
  $this
    ->assertVotingIconExistence('node', $node_id, 'dislike', FALSE);
  $this
    ->assertVotingIconExistence('comment', $comment_id, 'like', TRUE);
  $this
    ->assertVotingIconExistence('comment', $comment_id, 'dislike', FALSE);

  // Turn off like permission for node and comment.
  $this
    ->drupalGet('admin/people/permissions');
  $edit = [
    $user_role . '[add or remove like votes on article of node]' => FALSE,
    $user_role . '[add or remove like votes on test_comment_type of comment]' => FALSE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save permissions');

  // Verify that both like and dislike icons are not showed in default view
  // mode.
  $this
    ->drupalGet('node/' . $node_id);
  $this
    ->assertVotingIconExistence('node', $node_id, 'like', FALSE);
  $this
    ->assertVotingIconExistence('node', $node_id, 'dislike', FALSE);
  $this
    ->assertVotingIconExistence('node', $comment_id, 'dislike', FALSE);
  $this
    ->assertVotingIconExistence('comment', $comment_id, 'dislike', FALSE);
}