Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/src/Tests/UserPictureTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 test expects unused managed files to be marked temporary and then | |
37 // cleaned up by file_cron(). | |
38 $this->config('file.settings') | |
39 ->set('make_unused_managed_files_temporary', TRUE) | |
40 ->save(); | |
41 | |
42 $this->webUser = $this->drupalCreateUser([ | |
43 'access content', | |
44 'access comments', | |
45 'post comments', | |
46 'skip comment approval', | |
47 ]); | |
48 } | |
49 | |
50 /** | |
51 * Tests creation, display, and deletion of user pictures. | |
52 */ | |
53 public function testCreateDeletePicture() { | |
54 $this->drupalLogin($this->webUser); | |
55 | |
56 // Save a new picture. | |
57 $image = current($this->drupalGetTestFiles('image')); | |
58 $file = $this->saveUserPicture($image); | |
59 | |
60 // Verify that the image is displayed on the user account page. | |
61 $this->drupalGet('user'); | |
62 $this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on user account page.'); | |
63 | |
64 // Delete the picture. | |
65 $edit = []; | |
66 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Remove')); | |
67 $this->drupalPostForm(NULL, [], t('Save')); | |
68 | |
69 // Call file_cron() to clean up the file. Make sure the timestamp | |
70 // of the file is older than the system.file.temporary_maximum_age | |
71 // configuration value. | |
72 db_update('file_managed') | |
73 ->fields([ | |
74 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1), | |
75 ]) | |
76 ->condition('fid', $file->id()) | |
77 ->execute(); | |
78 \Drupal::service('cron')->run(); | |
79 | |
80 // Verify that the image has been deleted. | |
81 $this->assertFalse(File::load($file->id()), 'File was removed from the database.'); | |
82 // Clear out PHP's file stat cache so we see the current value. | |
83 clearstatcache(TRUE, $file->getFileUri()); | |
84 $this->assertFalse(is_file($file->getFileUri()), 'File was removed from the file system.'); | |
85 } | |
86 | |
87 /** | |
88 * Tests embedded users on node pages. | |
89 */ | |
90 public function testPictureOnNodeComment() { | |
91 $this->drupalLogin($this->webUser); | |
92 | |
93 // Save a new picture. | |
94 $image = current($this->drupalGetTestFiles('image')); | |
95 $file = $this->saveUserPicture($image); | |
96 | |
97 $node = $this->drupalCreateNode(['type' => 'article']); | |
98 | |
99 // Enable user pictures on nodes. | |
100 $this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save(); | |
101 | |
102 $image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style'); | |
103 $style = ImageStyle::load($image_style_id); | |
104 $image_url = file_url_transform_relative($style->buildUrl($file->getfileUri())); | |
105 $alt_text = 'Profile picture for user ' . $this->webUser->getUsername(); | |
106 | |
107 // Verify that the image is displayed on the node page. | |
108 $this->drupalGet('node/' . $node->id()); | |
109 $elements = $this->cssSelect('.node__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]'); | |
110 $this->assertEqual(count($elements), 1, 'User picture with alt text found on node page.'); | |
111 | |
112 // Enable user pictures on comments, instead of nodes. | |
113 $this->config('system.theme.global') | |
114 ->set('features.node_user_picture', FALSE) | |
115 ->set('features.comment_user_picture', TRUE) | |
116 ->save(); | |
117 | |
118 $edit = [ | |
119 'comment_body[0][value]' => $this->randomString(), | |
120 ]; | |
121 $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save')); | |
122 $elements = $this->cssSelect('.comment__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]'); | |
123 $this->assertEqual(count($elements), 1, 'User picture with alt text found on the comment.'); | |
124 | |
125 // Disable user pictures on comments and nodes. | |
126 $this->config('system.theme.global') | |
127 ->set('features.node_user_picture', FALSE) | |
128 ->set('features.comment_user_picture', FALSE) | |
129 ->save(); | |
130 | |
131 $this->drupalGet('node/' . $node->id()); | |
132 $this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.'); | |
133 } | |
134 | |
135 /** | |
136 * Edits the user picture for the test user. | |
137 */ | |
138 public function saveUserPicture($image) { | |
139 $edit = ['files[user_picture_0]' => drupal_realpath($image->uri)]; | |
140 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Save')); | |
141 | |
142 // Load actual user data from database. | |
143 $user_storage = $this->container->get('entity.manager')->getStorage('user'); | |
144 $user_storage->resetCache([$this->webUser->id()]); | |
145 $account = $user_storage->load($this->webUser->id()); | |
146 return File::load($account->user_picture->target_id); | |
147 } | |
148 | |
149 } |