View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;
class UserPictureTest extends BrowserTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected $profile = 'standard';
protected $defaultTheme = 'stark';
protected $webUser;
protected function setUp() : void {
parent::setUp();
$this
->config('file.settings')
->set('make_unused_managed_files_temporary', TRUE)
->save();
$this->webUser = $this
->drupalCreateUser([
'access content',
'access comments',
'post comments',
'skip comment approval',
]);
}
public function testCreateDeletePicture() {
$this
->drupalLogin($this->webUser);
$image = current($this
->drupalGetTestFiles('image'));
$file = $this
->saveUserPicture($image);
$this
->drupalGet('user');
$this
->assertSession()
->responseContains(StreamWrapperManager::getTarget($file
->getFileUri()));
$edit = [];
$this
->drupalGet('user/' . $this->webUser
->id() . '/edit');
$this
->submitForm($edit, 'Remove');
$this
->submitForm([], 'Save');
Database::getConnection()
->update('file_managed')
->fields([
'changed' => REQUEST_TIME - ($this
->config('system.file')
->get('temporary_maximum_age') + 1),
])
->condition('fid', $file
->id())
->execute();
\Drupal::service('cron')
->run();
$this
->assertNull(File::load($file
->id()), 'File was removed from the database.');
clearstatcache(TRUE, $file
->getFileUri());
$this
->assertFileDoesNotExist($file
->getFileUri());
}
public function testPictureOnNodeComment() {
$this
->drupalLogin($this->webUser);
$image = current($this
->drupalGetTestFiles('image'));
$file = $this
->saveUserPicture($image);
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$this
->config('system.theme.global')
->set('features.node_user_picture', TRUE)
->save();
$image_style_id = $this
->config('core.entity_view_display.user.user.compact')
->get('content.user_picture.settings.image_style');
$style = ImageStyle::load($image_style_id);
$image_url = \Drupal::service('file_url_generator')
->transformRelative($style
->buildUrl($file
->getfileUri()));
$alt_text = 'Profile picture for user ' . $this->webUser
->getAccountName();
$this
->drupalGet('node/' . $node
->id());
$elements = $this
->cssSelect('article[role="article"] > footer img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
$this
->assertCount(1, $elements, 'User picture with alt text found on node page.');
$this
->config('system.theme.global')
->set('features.node_user_picture', FALSE)
->set('features.comment_user_picture', TRUE)
->save();
$edit = [
'comment_body[0][value]' => $this
->randomString(),
];
$this
->drupalGet('comment/reply/node/' . $node
->id() . '/comment');
$this
->submitForm($edit, 'Save');
$elements = $this
->cssSelect('#comment-1 img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
$this
->assertCount(1, $elements, 'User picture with alt text found on the comment.');
$this
->config('system.theme.global')
->set('features.node_user_picture', FALSE)
->set('features.comment_user_picture', FALSE)
->save();
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseNotContains(StreamWrapperManager::getTarget($file
->getFileUri()));
}
public function saveUserPicture($image) {
$edit = [
'files[user_picture_0]' => \Drupal::service('file_system')
->realpath($image->uri),
];
$this
->drupalGet('user/' . $this->webUser
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$user_storage = $this->container
->get('entity_type.manager')
->getStorage('user');
$user_storage
->resetCache([
$this->webUser
->id(),
]);
$account = $user_storage
->load($this->webUser
->id());
return File::load($account->user_picture->target_id);
}
public function testUserViewAlter() {
\Drupal::service('module_installer')
->install([
'image_module_test',
]);
EntityViewDisplay::load('user.user.default')
->setComponent('user_picture', [
'region' => 'content',
'type' => 'dummy_image_formatter',
])
->save();
$this
->drupalLogin($this->webUser);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Dummy');
}
}