Backup of db before drupal security update
[yaffs-website] / web / core / modules / user / src / Tests / UserPictureTest.php
1 <?php
2
3 namespace Drupal\user\Tests;
4
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\file\Entity\File;
8
9 /**
10  * Tests user picture functionality.
11  *
12  * @group user
13  */
14 class UserPictureTest extends WebTestBase {
15
16   /**
17    * The profile to install as a basis for testing.
18    *
19    * Using the standard profile to test user picture config provided by the
20    * standard profile.
21    *
22    * @var string
23    */
24   protected $profile = 'standard';
25
26   /**
27    * A regular user.
28    *
29    * @var \Drupal\user\UserInterface
30    */
31   protected $webUser;
32
33   protected function setUp() {
34     parent::setUp();
35
36     $this->webUser = $this->drupalCreateUser([
37       'access content',
38       'access comments',
39       'post comments',
40       'skip comment approval',
41     ]);
42   }
43
44   /**
45    * Tests creation, display, and deletion of user pictures.
46    */
47   public function testCreateDeletePicture() {
48     $this->drupalLogin($this->webUser);
49
50     // Save a new picture.
51     $image = current($this->drupalGetTestFiles('image'));
52     $file = $this->saveUserPicture($image);
53
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.');
57
58     // Delete the picture.
59     $edit = [];
60     $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Remove'));
61     $this->drupalPostForm(NULL, [], t('Save'));
62
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')
67       ->fields([
68         'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
69       ])
70       ->condition('fid', $file->id())
71       ->execute();
72     \Drupal::service('cron')->run();
73
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.');
79   }
80
81   /**
82    * Tests embedded users on node pages.
83    */
84   public function testPictureOnNodeComment() {
85     $this->drupalLogin($this->webUser);
86
87     // Save a new picture.
88     $image = current($this->drupalGetTestFiles('image'));
89     $file = $this->saveUserPicture($image);
90
91     $node = $this->drupalCreateNode(['type' => 'article']);
92
93     // Enable user pictures on nodes.
94     $this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();
95
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();
100
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.');
105
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)
110       ->save();
111
112     $edit = [
113       'comment_body[0][value]' => $this->randomString(),
114     ];
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.');
118
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)
123       ->save();
124
125     $this->drupalGet('node/' . $node->id());
126     $this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.');
127   }
128
129   /**
130    * Edits the user picture for the test user.
131    */
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'));
135
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);
141   }
142
143 }