You are here

public function FileFieldSourcesTestBase::createTemporaryFile in FileField Sources 8

Create temporary file.

Return value

object Permanent file object.

4 calls to FileFieldSourcesTestBase::createTemporaryFile()
AttachSourceTest::testCopyFileFromAbsolutePath in tests/src/Functional/AttachSourceTest.php
Tests copy file from absolute path.
AttachSourceTest::testMoveFilesFromRelativePath in tests/src/Functional/AttachSourceTest.php
Tests move relative files with different names.
FileFieldSourcesTestBase::createTemporaryFileEntity in tests/src/Functional/FileFieldSourcesTestBase.php
Create temporary file entity.
MultipleValuesTest::setUp in tests/src/Functional/MultipleValuesTest.php
Sets up for multiple values test case.

File

tests/src/Functional/FileFieldSourcesTestBase.php, line 176

Class

FileFieldSourcesTestBase
Base class for File Field Sources test cases.

Namespace

Drupal\Tests\filefield_sources\Functional

Code

public function createTemporaryFile($path = '', $filename = NULL) {
  if (is_null($filename)) {
    $filename = $this
      ->randomMachineName() . '.txt';
  }
  if (empty($path)) {
    $path = \Drupal::config('system.file')
      ->get('default_scheme') . '://';
  }
  $uri = $path . $filename;
  $contents = $this
    ->randomString();

  // Change mode so that we can create files.
  \Drupal::service('file_system')
    ->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
  \Drupal::getContainer()
    ->get('file_system')
    ->chmod($path, FileSystem::CHMOD_DIRECTORY);
  file_put_contents($uri, $contents);
  $this
    ->assertTrue(is_file($uri), 'The temporary file has been created.');

  // Change mode so that we can delete created file.
  \Drupal::getContainer()
    ->get('file_system')
    ->chmod($uri, FileSystem::CHMOD_FILE);

  // Return object similar to file_scan_directory().
  $file = new \stdClass();
  $file->uri = $uri;
  $file->filename = $filename;
  $file->name = pathinfo($filename, PATHINFO_FILENAME);
  return $file;
}