You are here

function SaveDataTest::testWithFilename in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/SaveDataTest.php \Drupal\file\Tests\SaveDataTest::testWithFilename()

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

File

core/modules/file/src/Tests/SaveDataTest.php, line 43
Contains \Drupal\file\Tests\SaveDataTest.

Class

SaveDataTest
Tests the file_save_data() function.

Namespace

Drupal\file\Tests

Code

function testWithFilename() {
  $contents = $this
    ->randomMachineName(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
    ->getFileUri()), "File was placed in Drupal's files directory.");
  $this
    ->assertEqual($filename, drupal_basename($result
    ->getFileUri()), 'File was named correctly.');
  $this
    ->assertEqual($contents, file_get_contents($result
    ->getFileUri()), 'Contents of the file are correct.');
  $this
    ->assertEqual($result
    ->getMimeType(), 'text/plain', 'A MIME type was set.');
  $this
    ->assertTrue($result
    ->isPermanent(), "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
    ->id()));
}