function UserPictureTest::testPictureOnNodeComment

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserPictureTest.php \Drupal\Tests\user\Functional\UserPictureTest::testPictureOnNodeComment()
  2. 10 core/modules/user/tests/src/Functional/UserPictureTest.php \Drupal\Tests\user\Functional\UserPictureTest::testPictureOnNodeComment()
  3. 11.x 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 104

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 = file_url_transform_relative($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('.node__meta .field--name-user-picture 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->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save'));
    $elements = $this->cssSelect('.comment__meta .field--name-user-picture 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->assertNoRaw(StreamWrapperManager::getTarget($file->getFileUri()), 'User picture not found on node and comment.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.