annotate core/modules/user/tests/src/Functional/UserPictureTest.php @ 19:fa3358dc1485 tip

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