function FileTestCase::createFile

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

Parameters

$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.

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

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

Return value

File object.

30 calls to FileTestCase::createFile()
FileCopyTest::testExistingError in modules/simpletest/tests/file.test
Test that copying over an existing file fails when FILE_EXISTS_ERROR is specified.
FileCopyTest::testExistingRename in modules/simpletest/tests/file.test
Test renaming when copying over a file that already exists.
FileCopyTest::testExistingReplace in modules/simpletest/tests/file.test
Test replacement when copying over a file that already exists.
FileCopyTest::testNormal in modules/simpletest/tests/file.test
Test file copying in the normal, base case.
FileDeleteTest::testInUse in modules/simpletest/tests/file.test
Tries deleting a file that is in use.

... See full list

File

modules/simpletest/tests/file.test, line 201

Class

FileTestCase
Base class for file tests that adds some additional file specific assertions and helper functions.

Code

function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {
  if (!isset($filepath)) {
    // Prefix with non-latin characters to ensure that all file-related
    // tests work with international filenames.
    $filepath = 'Файл для тестирования ' . $this->randomName();
  }
  if (!isset($scheme)) {
    $scheme = file_default_scheme();
  }
  $filepath = $scheme . '://' . $filepath;
  if (!isset($contents)) {
    $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
  }
  file_put_contents($filepath, $contents);
  $this->assertTrue(is_file($filepath), 'The test file exists on the disk.', 'Create test file');
  $file = new stdClass();
  $file->uri = $filepath;
  $file->filename = drupal_basename($file->uri);
  $file->filemime = 'text/plain';
  $file->uid = 1;
  $file->timestamp = REQUEST_TIME;
  $file->filesize = filesize($file->uri);
  $file->status = 0;
  // Write the record directly rather than calling file_save() so we don't
  // invoke the hooks.
  $this->assertNotIdentical(drupal_write_record('file_managed', $file), FALSE, 'The file was added to the database.', 'Create test file');
  return $file;
}

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