You are here

function UserPictureTest::testCreateDeletePicture in Zircon Profile 8

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

Tests creation, display, and deletion of user pictures.

File

core/modules/user/src/Tests/UserPictureTest.php, line 52
Contains \Drupal\user\Tests\UserPictureTest.

Class

UserPictureTest
Tests user picture functionality.

Namespace

Drupal\user\Tests

Code

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(file_uri_target($file
    ->getFileUri()), 'User picture found on user account page.');

  // Delete the picture.
  $edit = array();
  $this
    ->drupalPostForm('user/' . $this->webUser
    ->id() . '/edit', $edit, t('Remove'));
  $this
    ->drupalPostForm(NULL, array(), 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.
  db_update('file_managed')
    ->fields(array(
    '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
    ->assertFalse(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
    ->assertFalse(is_file($file
    ->getFileUri()), 'File was removed from the file system.');
}