Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserPictureTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\file\Entity\File;
8 use Drupal\Tests\TestFileCreationTrait;
9
10 /**
11  * Tests user picture functionality.
12  *
13  * @group user
14  */
15 class UserPictureTest extends BrowserTestBase {
16
17   use TestFileCreationTrait {
18     getTestFiles as drupalGetTestFiles;
19   }
20
21   /**
22    * The profile to install as a basis for testing.
23    *
24    * Using the standard profile to test user picture config provided by the
25    * standard profile.
26    *
27    * @var string
28    */
29   protected $profile = 'standard';
30
31   /**
32    * A regular user.
33    *
34    * @var \Drupal\user\UserInterface
35    */
36   protected $webUser;
37
38   protected function setUp() {
39     parent::setUp();
40
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)
45       ->save();
46
47     $this->webUser = $this->drupalCreateUser([
48       'access content',
49       'access comments',
50       'post comments',
51       'skip comment approval',
52     ]);
53   }
54
55   /**
56    * Tests creation, display, and deletion of user pictures.
57    */
58   public function testCreateDeletePicture() {
59     $this->drupalLogin($this->webUser);
60
61     // Save a new picture.
62     $image = current($this->drupalGetTestFiles('image'));
63     $file = $this->saveUserPicture($image);
64
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.');
68
69     // Delete the picture.
70     $edit = [];
71     $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Remove'));
72     $this->drupalPostForm(NULL, [], t('Save'));
73
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')
78       ->fields([
79         'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
80       ])
81       ->condition('fid', $file->id())
82       ->execute();
83     \Drupal::service('cron')->run();
84
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.');
90   }
91
92   /**
93    * Tests embedded users on node pages.
94    */
95   public function testPictureOnNodeComment() {
96     $this->drupalLogin($this->webUser);
97
98     // Save a new picture.
99     $image = current($this->drupalGetTestFiles('image'));
100     $file = $this->saveUserPicture($image);
101
102     $node = $this->drupalCreateNode(['type' => 'article']);
103
104     // Enable user pictures on nodes.
105     $this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();
106
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();
111
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.');
116
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)
121       ->save();
122
123     $edit = [
124       'comment_body[0][value]' => $this->randomString(),
125     ];
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.');
129
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)
134       ->save();
135
136     $this->drupalGet('node/' . $node->id());
137     $this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.');
138   }
139
140   /**
141    * Edits the user picture for the test user.
142    */
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'));
146
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);
152   }
153
154 }