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. 8.9.x core/modules/user/tests/src/Functional/UserPictureTest.php \Drupal\Tests\user\Functional\UserPictureTest::testPictureOnNodeComment()
  3. 10 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 111

Class

UserPictureTest
Tests user picture functionality.

Namespace

Drupal\Tests\user\Functional

Code

public function testPictureOnNodeComment() : void {
    $this->drupalLogin($this->webUser);
    $this->drupalCreateContentType([
        'type' => 'article',
        'name' => 'Article',
    ]);
    $this->addDefaultCommentField('node', 'article');
    // 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 > 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()));
}

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