You are here

public function UserPictureTest::testPictureOnNodeComment in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserPictureTest.php \Drupal\Tests\user\Functional\UserPictureTest::testPictureOnNodeComment()
  2. 9 core/modules/user/tests/src/Functional/UserPictureTest.php \Drupal\Tests\user\Functional\UserPictureTest::testPictureOnNodeComment()

Tests embedded users on node pages.

File

core/modules/user/tests/src/Functional/UserPictureTest.php, line 105

Class

UserPictureTest
Tests user picture functionality.

Namespace

Drupal\Tests\user\Functional

Code

public function testPictureOnNodeComment() {
  $this
    ->drupalLogin($this->webUser);

  // Save a new picture.
  $image = current($this
    ->drupalGetTestFiles('image'));
  $file = $this
    ->saveUserPicture($image);
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);

  // Enable user pictures on nodes.
  $this
    ->config('system.theme.global')
    ->set('features.node_user_picture', TRUE)
    ->save();
  $image_style_id = $this
    ->config('core.entity_view_display.user.user.compact')
    ->get('content.user_picture.settings.image_style');
  $style = ImageStyle::load($image_style_id);
  $image_url = \Drupal::service('file_url_generator')
    ->transformRelative($style
    ->buildUrl($file
    ->getfileUri()));
  $alt_text = 'Profile picture for user ' . $this->webUser
    ->getAccountName();

  // Verify that the image is displayed on the node page.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $elements = $this
    ->cssSelect('article[role="article"] > footer img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
  $this
    ->assertCount(1, $elements, 'User picture with alt text found on node page.');

  // Enable user pictures on comments, instead of nodes.
  $this
    ->config('system.theme.global')
    ->set('features.node_user_picture', FALSE)
    ->set('features.comment_user_picture', TRUE)
    ->save();
  $edit = [
    'comment_body[0][value]' => $this
      ->randomString(),
  ];
  $this
    ->drupalGet('comment/reply/node/' . $node
    ->id() . '/comment');
  $this
    ->submitForm($edit, 'Save');
  $elements = $this
    ->cssSelect('#comment-1 img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
  $this
    ->assertCount(1, $elements, 'User picture with alt text found on the comment.');

  // Disable user pictures on comments and nodes.
  $this
    ->config('system.theme.global')
    ->set('features.node_user_picture', FALSE)
    ->set('features.comment_user_picture', FALSE)
    ->save();
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseNotContains(StreamWrapperManager::getTarget($file
    ->getFileUri()));
}