You are here

public function UserPictureTest::testCreateDeletePicture in Drupal 8

Same name and namespace in other branches
  1. 9 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
    ->assertRaw(StreamWrapperManager::getTarget($file
    ->getFileUri()), 'User picture found on user account page.');

  // Delete the picture.
  $edit = [];
  $this
    ->drupalPostForm('user/' . $this->webUser
    ->id() . '/edit', $edit, t('Remove'));
  $this
    ->drupalPostForm(NULL, [], t('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
    ->assertFileNotExists($file
    ->getFileUri());
}