You are here

protected function FileFieldWidgetTest::createTemporaryFile in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::createTemporaryFile()
  2. 9 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::createTemporaryFile()

Creates a temporary file, for a specific user.

Parameters

string $data: A string containing the contents of the file.

\Drupal\user\UserInterface $user: The user of the file owner.

Return value

\Drupal\file\FileInterface A file object, or FALSE on error.

File

core/modules/file/tests/src/Functional/FileFieldWidgetTest.php, line 57

Class

FileFieldWidgetTest
Tests the file field widget with public and private files.

Namespace

Drupal\Tests\file\Functional

Code

protected function createTemporaryFile($data, UserInterface $user = NULL) {

  /** @var \Drupal\file\FileRepositoryInterface $file_repository */
  $file_repository = \Drupal::service('file.repository');
  $file = $file_repository
    ->writeData($data, "public://");
  if ($file) {
    if ($user) {
      $file
        ->setOwner($user);
    }
    else {
      $file
        ->setOwner($this->adminUser);
    }

    // Change the file status to be temporary.
    $file
      ->setTemporary();

    // Save the changes.
    $file
      ->save();
  }
  return $file;
}