You are here

public function FileManagedTestBase::createFile in Drupal 8

Same name in this branch
  1. 8 core/modules/file/src/Tests/FileManagedTestBase.php \Drupal\file\Tests\FileManagedTestBase::createFile()
  2. 8 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::createFile()
Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::createFile()
  2. 10 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::createFile()

Create a file and save it to the files table and assert that it occurs correctly.

Parameters

string $filepath: Optional string specifying the file path. If none is provided then a randomly named file will be created in the site's files directory.

string $contents: Optional contents to save into the file. If a NULL value is provided an arbitrary string will be used.

string $scheme: Optional string indicating the stream scheme to use. Drupal core includes public, private, and temporary. The public wrapper is the default.

Return value

\Drupal\file\FileInterface File entity.

3 calls to FileManagedTestBase::createFile()
DownloadTest::checkUrl in core/modules/file/tests/src/Functional/DownloadTest.php
Download a file from the URL generated by file_create_url().
DownloadTest::doPrivateFileTransferTest in core/modules/file/tests/src/Functional/DownloadTest.php
Test the private file transfer system.
DownloadTest::testPublicFileTransfer in core/modules/file/tests/src/Functional/DownloadTest.php
Test the public file transfer system.

File

core/modules/file/tests/src/Functional/FileManagedTestBase.php, line 150

Class

FileManagedTestBase
Base class for file tests that use the file_test module to test uploads and hooks.

Namespace

Drupal\Tests\file\Functional

Code

public function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {

  // Don't count hook invocations caused by creating the file.
  \Drupal::state()
    ->set('file_test.count_hook_invocations', FALSE);
  $file = File::create([
    'uri' => $this
      ->createUri($filepath, $contents, $scheme),
    'uid' => 1,
  ]);
  $file
    ->save();

  // Write the record directly rather than using the API so we don't invoke
  // the hooks.
  $this
    ->assertTrue($file
    ->id() > 0, 'The file was added to the database.', 'Create test file');
  \Drupal::state()
    ->set('file_test.count_hook_invocations', TRUE);
  return $file;
}