annotate core/modules/user/tests/src/Functional/UserPictureTest.php @ 5:12f9dff5fda9 tip

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