function SaveDataTest::testExistingRename

Test file_save_data() when renaming around an existing file.

File

drupal/core/modules/file/lib/Drupal/file/Tests/SaveDataTest.php, line 72
Definition of 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
    ->randomName(8);
  $result = file_save_data($contents, $existing->uri, FILE_EXISTS_RENAME);
  $this
    ->assertTrue($result, t("File saved successfully."));
  $this
    ->assertEqual('public', file_uri_scheme($result->uri), t("File was placed in Drupal's files directory."));
  $this
    ->assertEqual($result->filename, $existing->filename, t("Filename was set to the basename of the source, rather than that of the renamed file."));
  $this
    ->assertEqual($contents, file_get_contents($result->uri), t("Contents of the file are correct."));
  $this
    ->assertEqual($result->filemime, 'application/octet-stream', t("A MIME type was set."));
  $this
    ->assertEqual($result->status, FILE_STATUS_PERMANENT, t("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->fid, TRUE));

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