function FileFieldWidgetTest::createTemporaryFile

Same name in other branches
  1. 8.9.x core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::createTemporaryFile()
  2. 10 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::createTemporaryFile()
  3. 11.x 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.

1 call to FileFieldWidgetTest::createTemporaryFile()
FileFieldWidgetTest::doTestTemporaryFileRemovalExploit in core/modules/file/tests/src/Functional/FileFieldWidgetTest.php
Helper for testing exploiting the temporary file removal using fid.

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.