You are here

function FileManagedUnitTestBase::createFile in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/file/src/Tests/FileManagedUnitTestBase.php \Drupal\file\Tests\FileManagedUnitTestBase::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.

21 calls to FileManagedUnitTestBase::createFile()
CopyTest::testExistingError in core/modules/file/src/Tests/CopyTest.php
Test that copying over an existing file fails when FILE_EXISTS_ERROR is specified.
CopyTest::testExistingRename in core/modules/file/src/Tests/CopyTest.php
Test renaming when copying over a file that already exists.
CopyTest::testExistingReplace in core/modules/file/src/Tests/CopyTest.php
Test replacement when copying over a file that already exists.
CopyTest::testNormal in core/modules/file/src/Tests/CopyTest.php
Test file copying in the normal, base case.
DeleteTest::testInUse in core/modules/file/src/Tests/DeleteTest.php
Tries deleting a file that is in use.

... See full list

File

core/modules/file/src/Tests/FileManagedUnitTestBase.php, line 164
Contains \Drupal\file\Tests\FileManagedUnitTestBase.

Class

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

Namespace

Drupal\file\Tests

Code

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 = entity_create('file', array(
    '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;
}