You are here

public function LikeAndDislikeTest::testUserEntity in Like & Dislike 8

Asserts likes and dislikes for users.

File

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

Class

LikeAndDislikeTest
Provides a web test for like_and_dislike module.

Namespace

Drupal\Tests\like_and_dislike\FunctionalJavascript

Code

public function testUserEntity() {

  // Enable likes and dislikes for users.
  $edit = [
    'enabled_types[user][enabled]' => 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
    ->assertFieldChecked('edit-enabled-types-user-enabled');

  // Make "like and dislike" component visible.
  $this
    ->drupalGet('admin/config/people/accounts/display');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Show row weights');
  $this
    ->assertSession()
    ->waitForElementVisible('css', '[name="fields[like_and_dislike][region]"]');
  $this
    ->submitForm([
    'fields[like_and_dislike][region]' => 'content',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // Go to user profile.
  $user_id = $this->adminUser
    ->id();
  $this
    ->drupalGet('user/' . $user_id);

  // Likes and dislikes are displayed but the user has no permission to vote.
  $this
    ->assertLikesAndDislikes('user', $user_id, '0', '0', TRUE);

  // Allow user "like" permission only and assert the links.
  $user_roles = $this->adminUser
    ->getRoles();
  $user_role = end($user_roles);
  $edit = [
    $user_role . '[add or remove like votes on user]' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/people/permissions', $edit, 'Save permissions');
  $this
    ->drupalGet('user/' . $user_id);

  // Assert user is able to like, but not to dislike.
  $xpath = $this
    ->xpath('//*[@id="like-container-user-' . $user_id . '"]/a')[0];
  $this
    ->assertFalse($xpath
    ->hasAttribute('class'));
  $this
    ->assertEquals($this
    ->cssSelect('#dislike-container-user-' . $user_id . ' a[class]')[0]
    ->getAttribute('class'), 'disable-status');

  // Assert that enabled_types is an empty Array.
  $enabled_types = \Drupal::config('like_and_dislike.settings')
    ->get('enabled_types');
  $this
    ->assertEquals($enabled_types['user'], []);
}