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