You are here

public function UserPictureTest::testCreateDeletePicture in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserPictureTest.php \Drupal\Tests\user\Functional\UserPictureTest::testCreateDeletePicture()

Tests creation, display, and deletion of user pictures.

File

core/modules/user/tests/src/Functional/UserPictureTest.php, line 66

Class

UserPictureTest
Tests user picture functionality.

Namespace

Drupal\Tests\user\Functional

Code

public function testCreateDeletePicture() {
  $this
    ->drupalLogin($this->webUser);

  // Save a new picture.
  $image = current($this
    ->drupalGetTestFiles('image'));
  $file = $this
    ->saveUserPicture($image);

  // Verify that the image is displayed on the user account page.
  $this
    ->drupalGet('user');
  $this
    ->assertSession()
    ->responseContains(StreamWrapperManager::getTarget($file
    ->getFileUri()));

  // Delete the picture.
  $edit = [];
  $this
    ->drupalGet('user/' . $this->webUser
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Remove');
  $this
    ->submitForm([], 'Save');

  // Call file_cron() to clean up the file. Make sure the timestamp
  // of the file is older than the system.file.temporary_maximum_age
  // configuration value. We use an UPDATE statement because using the API
  // would set the timestamp.
  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();

  // Verify that the image has been deleted.
  $this
    ->assertNull(File::load($file
    ->id()), 'File was removed from the database.');

  // Clear out PHP's file stat cache so we see the current value.
  clearstatcache(TRUE, $file
    ->getFileUri());
  $this
    ->assertFileDoesNotExist($file
    ->getFileUri());
}