You are here

function SaveDataTest::testExistingRename 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::testExistingRename()

Test file_save_data() when renaming around an existing file.

File

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

Class

SaveDataTest
Tests the file_save_data() function.

Namespace

Drupal\file\Tests

Code

function testExistingRename() {

  // Setup a file to overwrite.
  $existing = $this
    ->createFile();
  $contents = $this
    ->randomMachineName(8);
  $result = file_save_data($contents, $existing
    ->getFileUri(), FILE_EXISTS_RENAME);
  $this
    ->assertTrue($result, 'File saved successfully.');
  $this
    ->assertEqual('public', file_uri_scheme($result
    ->getFileUri()), "File was placed in Drupal's files directory.");
  $this
    ->assertEqual($result
    ->getFilename(), $existing
    ->getFilename(), 'Filename was set to the basename of the source, rather than that of the renamed file.');
  $this
    ->assertEqual($contents, file_get_contents($result
    ->getFileUri()), 'Contents of the file are correct.');
  $this
    ->assertEqual($result
    ->getMimeType(), 'application/octet-stream', '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',
  ));

  // Ensure that the existing file wasn't overwritten.
  $this
    ->assertDifferentFile($existing, $result);
  $this
    ->assertFileUnchanged($existing, File::load($existing
    ->id()));

  // Verify that was returned is what's in the database.
  $this
    ->assertFileUnchanged($result, File::load($result
    ->id()));
}