You are here

function FileSaveDataTest::testWithFilename in Drupal 7

Test the file_save_data() function when a filename is provided.

File

modules/simpletest/tests/file.test, line 2415
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

FileSaveDataTest
Tests the file_save_data() function.

Code

function testWithFilename() {
  $contents = $this
    ->randomName(8);

  // Using filename with non-latin characters.
  $filename = 'Текстовый файл.txt';
  $result = file_save_data($contents, 'public://' . $filename);
  $this
    ->assertTrue($result, 'Unnamed file saved correctly.');
  $this
    ->assertEqual('public', file_uri_scheme($result->uri), "File was placed in Drupal's files directory.");
  $this
    ->assertEqual($filename, drupal_basename($result->uri), 'File was named correctly.');
  $this
    ->assertEqual($contents, file_get_contents($result->uri), 'Contents of the file are correct.');
  $this
    ->assertEqual($result->filemime, 'text/plain', 'A MIME type was set.');
  $this
    ->assertEqual($result->status, FILE_STATUS_PERMANENT, "The file's status was set to permanent.");

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled(array(
    'insert',
  ));

  // Verify that what was returned is what's in the database.
  $this
    ->assertFileUnchanged($result, file_load($result->fid, TRUE));
}