You are here

protected function PrivateFileSchemeHandlerTest::createFileEntity in Acquia Content Hub 8.2

Saves and returns a file entity based on the file name and scheme.

Optionally override every values by using the $values parameter.

Parameters

string $file_name: The file name. Used to generate uri.

string $scheme: The applicable scheme.

array $values: Values to override file entity fields.

Return value

\Drupal\file\FileInterface The newly created file entity.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to PrivateFileSchemeHandlerTest::createFileEntity()
PrivateFileSchemeHandlerTest::testAddAttributes in tests/src/Kernel/PrivateFileSchemeHandlerTest.php
@covers ::addAttributes

File

tests/src/Kernel/PrivateFileSchemeHandlerTest.php, line 83

Class

PrivateFileSchemeHandlerTest
Tests the Private File Scheme Handler.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

protected function createFileEntity(string $file_name, string $scheme, array $values = []) : FileInterface {
  $path = explode('/', $file_name);
  $data = [
    'uuid' => \Drupal::service('uuid')
      ->generate(),
    'langcode' => 'en',
    'uid' => 1,
    'filename' => end($path),
    'uri' => sprintf("{$scheme}://%s", implode('/', $path)),
    'filemime' => mimetype_from_extension($file_name),
    'filesize' => rand(1000, 5000),
    'status' => 1,
    'created' => time(),
  ];
  $data = array_merge($data, $values);
  $file = File::create($data);
  $file
    ->save();
  return $file;
}