3 namespace Drupal\user\Tests;
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\file\Entity\File;
10 * Tests user picture functionality.
14 class UserPictureTest extends WebTestBase {
17 * The profile to install as a basis for testing.
19 * Using the standard profile to test user picture config provided by the
24 protected $profile = 'standard';
29 * @var \Drupal\user\UserInterface
33 protected function setUp() {
36 $this->webUser = $this->drupalCreateUser([
40 'skip comment approval',
45 * Tests creation, display, and deletion of user pictures.
47 public function testCreateDeletePicture() {
48 $this->drupalLogin($this->webUser);
50 // Save a new picture.
51 $image = current($this->drupalGetTestFiles('image'));
52 $file = $this->saveUserPicture($image);
54 // Verify that the image is displayed on the user account page.
55 $this->drupalGet('user');
56 $this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on user account page.');
58 // Delete the picture.
60 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Remove'));
61 $this->drupalPostForm(NULL, [], t('Save'));
63 // Call file_cron() to clean up the file. Make sure the timestamp
64 // of the file is older than the system.file.temporary_maximum_age
65 // configuration value.
66 db_update('file_managed')
68 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
70 ->condition('fid', $file->id())
72 \Drupal::service('cron')->run();
74 // Verify that the image has been deleted.
75 $this->assertFalse(File::load($file->id()), 'File was removed from the database.');
76 // Clear out PHP's file stat cache so we see the current value.
77 clearstatcache(TRUE, $file->getFileUri());
78 $this->assertFalse(is_file($file->getFileUri()), 'File was removed from the file system.');
82 * Tests embedded users on node pages.
84 public function testPictureOnNodeComment() {
85 $this->drupalLogin($this->webUser);
87 // Save a new picture.
88 $image = current($this->drupalGetTestFiles('image'));
89 $file = $this->saveUserPicture($image);
91 $node = $this->drupalCreateNode(['type' => 'article']);
93 // Enable user pictures on nodes.
94 $this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();
96 $image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style');
97 $style = ImageStyle::load($image_style_id);
98 $image_url = file_url_transform_relative($style->buildUrl($file->getfileUri()));
99 $alt_text = 'Profile picture for user ' . $this->webUser->getUsername();
101 // Verify that the image is displayed on the node page.
102 $this->drupalGet('node/' . $node->id());
103 $elements = $this->cssSelect('.node__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
104 $this->assertEqual(count($elements), 1, 'User picture with alt text found on node page.');
106 // Enable user pictures on comments, instead of nodes.
107 $this->config('system.theme.global')
108 ->set('features.node_user_picture', FALSE)
109 ->set('features.comment_user_picture', TRUE)
113 'comment_body[0][value]' => $this->randomString(),
115 $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save'));
116 $elements = $this->cssSelect('.comment__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
117 $this->assertEqual(count($elements), 1, 'User picture with alt text found on the comment.');
119 // Disable user pictures on comments and nodes.
120 $this->config('system.theme.global')
121 ->set('features.node_user_picture', FALSE)
122 ->set('features.comment_user_picture', FALSE)
125 $this->drupalGet('node/' . $node->id());
126 $this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.');
130 * Edits the user picture for the test user.
132 public function saveUserPicture($image) {
133 $edit = ['files[user_picture_0]' => drupal_realpath($image->uri)];
134 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Save'));
136 // Load actual user data from database.
137 $user_storage = $this->container->get('entity.manager')->getStorage('user');
138 $user_storage->resetCache([$this->webUser->id()]);
139 $account = $user_storage->load($this->webUser->id());
140 return File::load($account->user_picture->target_id);