FileSaveDataTest::testExistingReplace

7 file.test FileSaveDataTest::testExistingReplace()
8 file.test FileSaveDataTest::testExistingReplace()

Test file_save_data() when replacing an existing file.

File

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

Code

function testExistingReplace() {
  // Setup a file to overwrite.
  $existing = $this->createFile();
  $contents = $this->randomName(8);

  $result = file_save_data($contents, $existing->uri, FILE_EXISTS_REPLACE);
  $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 existing file, rather than preserving the original name.'));
  $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('load', 'update'));

  // Verify that the existing file was re-used.
  $this->assertSameFile($existing, $result);

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