annotate core/modules/user/tests/src/Functional/UserPictureTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 namespace Drupal\Tests\user\Functional;
Chris@14 4
Chris@14 5 use Drupal\image\Entity\ImageStyle;
Chris@14 6 use Drupal\Tests\BrowserTestBase;
Chris@14 7 use Drupal\file\Entity\File;
Chris@14 8 use Drupal\Tests\TestFileCreationTrait;
Chris@14 9
Chris@14 10 /**
Chris@14 11 * Tests user picture functionality.
Chris@14 12 *
Chris@14 13 * @group user
Chris@14 14 */
Chris@14 15 class UserPictureTest extends BrowserTestBase {
Chris@14 16
Chris@14 17 use TestFileCreationTrait {
Chris@14 18 getTestFiles as drupalGetTestFiles;
Chris@14 19 }
Chris@14 20
Chris@14 21 /**
Chris@14 22 * The profile to install as a basis for testing.
Chris@14 23 *
Chris@14 24 * Using the standard profile to test user picture config provided by the
Chris@14 25 * standard profile.
Chris@14 26 *
Chris@14 27 * @var string
Chris@14 28 */
Chris@14 29 protected $profile = 'standard';
Chris@14 30
Chris@14 31 /**
Chris@14 32 * A regular user.
Chris@14 33 *
Chris@14 34 * @var \Drupal\user\UserInterface
Chris@14 35 */
Chris@14 36 protected $webUser;
Chris@14 37
Chris@14 38 protected function setUp() {
Chris@14 39 parent::setUp();
Chris@14 40
Chris@14 41 // This test expects unused managed files to be marked temporary and then
Chris@14 42 // cleaned up by file_cron().
Chris@14 43 $this->config('file.settings')
Chris@14 44 ->set('make_unused_managed_files_temporary', TRUE)
Chris@14 45 ->save();
Chris@14 46
Chris@14 47 $this->webUser = $this->drupalCreateUser([
Chris@14 48 'access content',
Chris@14 49 'access comments',
Chris@14 50 'post comments',
Chris@14 51 'skip comment approval',
Chris@14 52 ]);
Chris@14 53 }
Chris@14 54
Chris@14 55 /**
Chris@14 56 * Tests creation, display, and deletion of user pictures.
Chris@14 57 */
Chris@14 58 public function testCreateDeletePicture() {
Chris@14 59 $this->drupalLogin($this->webUser);
Chris@14 60
Chris@14 61 // Save a new picture.
Chris@14 62 $image = current($this->drupalGetTestFiles('image'));
Chris@14 63 $file = $this->saveUserPicture($image);
Chris@14 64
Chris@14 65 // Verify that the image is displayed on the user account page.
Chris@14 66 $this->drupalGet('user');
Chris@14 67 $this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on user account page.');
Chris@14 68
Chris@14 69 // Delete the picture.
Chris@14 70 $edit = [];
Chris@14 71 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Remove'));
Chris@14 72 $this->drupalPostForm(NULL, [], t('Save'));
Chris@14 73
Chris@14 74 // Call file_cron() to clean up the file. Make sure the timestamp
Chris@14 75 // of the file is older than the system.file.temporary_maximum_age
Chris@14 76 // configuration value.
Chris@14 77 db_update('file_managed')
Chris@14 78 ->fields([
Chris@14 79 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
Chris@14 80 ])
Chris@14 81 ->condition('fid', $file->id())
Chris@14 82 ->execute();
Chris@14 83 \Drupal::service('cron')->run();
Chris@14 84
Chris@14 85 // Verify that the image has been deleted.
Chris@14 86 $this->assertFalse(File::load($file->id()), 'File was removed from the database.');
Chris@14 87 // Clear out PHP's file stat cache so we see the current value.
Chris@14 88 clearstatcache(TRUE, $file->getFileUri());
Chris@14 89 $this->assertFalse(is_file($file->getFileUri()), 'File was removed from the file system.');
Chris@14 90 }
Chris@14 91
Chris@14 92 /**
Chris@14 93 * Tests embedded users on node pages.
Chris@14 94 */
Chris@14 95 public function testPictureOnNodeComment() {
Chris@14 96 $this->drupalLogin($this->webUser);
Chris@14 97
Chris@14 98 // Save a new picture.
Chris@14 99 $image = current($this->drupalGetTestFiles('image'));
Chris@14 100 $file = $this->saveUserPicture($image);
Chris@14 101
Chris@14 102 $node = $this->drupalCreateNode(['type' => 'article']);
Chris@14 103
Chris@14 104 // Enable user pictures on nodes.
Chris@14 105 $this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();
Chris@14 106
Chris@14 107 $image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style');
Chris@14 108 $style = ImageStyle::load($image_style_id);
Chris@14 109 $image_url = file_url_transform_relative($style->buildUrl($file->getfileUri()));
Chris@14 110 $alt_text = 'Profile picture for user ' . $this->webUser->getUsername();
Chris@14 111
Chris@14 112 // Verify that the image is displayed on the node page.
Chris@14 113 $this->drupalGet('node/' . $node->id());
Chris@14 114 $elements = $this->cssSelect('.node__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
Chris@14 115 $this->assertEqual(count($elements), 1, 'User picture with alt text found on node page.');
Chris@14 116
Chris@14 117 // Enable user pictures on comments, instead of nodes.
Chris@14 118 $this->config('system.theme.global')
Chris@14 119 ->set('features.node_user_picture', FALSE)
Chris@14 120 ->set('features.comment_user_picture', TRUE)
Chris@14 121 ->save();
Chris@14 122
Chris@14 123 $edit = [
Chris@14 124 'comment_body[0][value]' => $this->randomString(),
Chris@14 125 ];
Chris@14 126 $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save'));
Chris@14 127 $elements = $this->cssSelect('.comment__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
Chris@14 128 $this->assertEqual(count($elements), 1, 'User picture with alt text found on the comment.');
Chris@14 129
Chris@14 130 // Disable user pictures on comments and nodes.
Chris@14 131 $this->config('system.theme.global')
Chris@14 132 ->set('features.node_user_picture', FALSE)
Chris@14 133 ->set('features.comment_user_picture', FALSE)
Chris@14 134 ->save();
Chris@14 135
Chris@14 136 $this->drupalGet('node/' . $node->id());
Chris@14 137 $this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.');
Chris@14 138 }
Chris@14 139
Chris@14 140 /**
Chris@14 141 * Edits the user picture for the test user.
Chris@14 142 */
Chris@14 143 public function saveUserPicture($image) {
Chris@14 144 $edit = ['files[user_picture_0]' => \Drupal::service('file_system')->realpath($image->uri)];
Chris@14 145 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Save'));
Chris@14 146
Chris@14 147 // Load actual user data from database.
Chris@14 148 $user_storage = $this->container->get('entity.manager')->getStorage('user');
Chris@14 149 $user_storage->resetCache([$this->webUser->id()]);
Chris@14 150 $account = $user_storage->load($this->webUser->id());
Chris@14 151 return File::load($account->user_picture->target_id);
Chris@14 152 }
Chris@14 153
Chris@14 154 }