3 namespace Drupal\Tests\user\Functional;
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\file\Entity\File;
8 use Drupal\Tests\TestFileCreationTrait;
11 * Tests user picture functionality.
15 class UserPictureTest extends BrowserTestBase {
17 use TestFileCreationTrait {
18 getTestFiles as drupalGetTestFiles;
22 * The profile to install as a basis for testing.
24 * Using the standard profile to test user picture config provided by the
29 protected $profile = 'standard';
34 * @var \Drupal\user\UserInterface
38 protected function setUp() {
41 // This test expects unused managed files to be marked temporary and then
42 // cleaned up by file_cron().
43 $this->config('file.settings')
44 ->set('make_unused_managed_files_temporary', TRUE)
47 $this->webUser = $this->drupalCreateUser([
51 'skip comment approval',
56 * Tests creation, display, and deletion of user pictures.
58 public function testCreateDeletePicture() {
59 $this->drupalLogin($this->webUser);
61 // Save a new picture.
62 $image = current($this->drupalGetTestFiles('image'));
63 $file = $this->saveUserPicture($image);
65 // Verify that the image is displayed on the user account page.
66 $this->drupalGet('user');
67 $this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on user account page.');
69 // Delete the picture.
71 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Remove'));
72 $this->drupalPostForm(NULL, [], t('Save'));
74 // Call file_cron() to clean up the file. Make sure the timestamp
75 // of the file is older than the system.file.temporary_maximum_age
76 // configuration value.
77 db_update('file_managed')
79 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
81 ->condition('fid', $file->id())
83 \Drupal::service('cron')->run();
85 // Verify that the image has been deleted.
86 $this->assertFalse(File::load($file->id()), 'File was removed from the database.');
87 // Clear out PHP's file stat cache so we see the current value.
88 clearstatcache(TRUE, $file->getFileUri());
89 $this->assertFalse(is_file($file->getFileUri()), 'File was removed from the file system.');
93 * Tests embedded users on node pages.
95 public function testPictureOnNodeComment() {
96 $this->drupalLogin($this->webUser);
98 // Save a new picture.
99 $image = current($this->drupalGetTestFiles('image'));
100 $file = $this->saveUserPicture($image);
102 $node = $this->drupalCreateNode(['type' => 'article']);
104 // Enable user pictures on nodes.
105 $this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();
107 $image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style');
108 $style = ImageStyle::load($image_style_id);
109 $image_url = file_url_transform_relative($style->buildUrl($file->getfileUri()));
110 $alt_text = 'Profile picture for user ' . $this->webUser->getUsername();
112 // Verify that the image is displayed on the node page.
113 $this->drupalGet('node/' . $node->id());
114 $elements = $this->cssSelect('.node__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
115 $this->assertEqual(count($elements), 1, 'User picture with alt text found on node page.');
117 // Enable user pictures on comments, instead of nodes.
118 $this->config('system.theme.global')
119 ->set('features.node_user_picture', FALSE)
120 ->set('features.comment_user_picture', TRUE)
124 'comment_body[0][value]' => $this->randomString(),
126 $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save'));
127 $elements = $this->cssSelect('.comment__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
128 $this->assertEqual(count($elements), 1, 'User picture with alt text found on the comment.');
130 // Disable user pictures on comments and nodes.
131 $this->config('system.theme.global')
132 ->set('features.node_user_picture', FALSE)
133 ->set('features.comment_user_picture', FALSE)
136 $this->drupalGet('node/' . $node->id());
137 $this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.');
141 * Edits the user picture for the test user.
143 public function saveUserPicture($image) {
144 $edit = ['files[user_picture_0]' => \Drupal::service('file_system')->realpath($image->uri)];
145 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Save'));
147 // Load actual user data from database.
148 $user_storage = $this->container->get('entity.manager')->getStorage('user');
149 $user_storage->resetCache([$this->webUser->id()]);
150 $account = $user_storage->load($this->webUser->id());
151 return File::load($account->user_picture->target_id);