function FileManagedUnitTestBase::createFile

Same name in other branches
  1. 8.9.x core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php \Drupal\Tests\file\Kernel\FileManagedUnitTestBase::createFile()
  2. 10 core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php \Drupal\Tests\file\Kernel\FileManagedUnitTestBase::createFile()
  3. 11.x core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php \Drupal\Tests\file\Kernel\FileManagedUnitTestBase::createFile()

Creates and saves a file, asserting that it was saved.

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.

33 calls to FileManagedUnitTestBase::createFile()
CopyTest::testEntityStorageException in core/modules/file/tests/src/Kernel/CopyTest.php
Tests for entity storage exception.
CopyTest::testExistingError in core/modules/file/tests/src/Kernel/CopyTest.php
Tests that copying over an existing file fails when instructed to do so.
CopyTest::testExistingRename in core/modules/file/tests/src/Kernel/CopyTest.php
Tests renaming when copying over a file that already exists.
CopyTest::testExistingReplace in core/modules/file/tests/src/Kernel/CopyTest.php
Tests replacement when copying over a file that already exists.
CopyTest::testInvalidStreamWrapper in core/modules/file/tests/src/Kernel/CopyTest.php
Tests for an invalid stream wrapper.

... See full list

File

core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php, line 163

Class

FileManagedUnitTestBase
Provides a base class for testing file uploads and hook invocations.

Namespace

Drupal\Tests\file\Kernel

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.
    // Verify that the file was added to the database.
    $this->assertGreaterThan(0, $file->id());
    \Drupal::state()->set('file_test.count_hook_invocations', TRUE);
    return $file;
}

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