You are here

function FileUnmanagedSaveDataTest::testFileSaveData in SimpleTest 7

Test the file_unmanaged_save_data() function.

File

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

Class

FileUnmanagedSaveDataTest
Tests the file_unmanaged_save_data() function.

Code

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

  // No filename.
  $filepath = file_unmanaged_save_data($contents);
  $this
    ->assertTrue($filepath, t('Unnamed file saved correctly.'));
  $this
    ->assertEqual(file_directory_path('public'), file_directory_path(file_stream_wrapper_valid_scheme($filepath)), t("File was placed in Drupal's files directory."));
  $this
    ->assertEqual($contents, file_get_contents(drupal_realpath($filepath)), t('Contents of the file are correct.'));

  // Provide a filename.
  $filepath = file_unmanaged_save_data($contents, 'public://asdf.txt', FILE_EXISTS_REPLACE);
  $this
    ->assertTrue($filepath, t('Unnamed file saved correctly.'));
  $this
    ->assertEqual(file_directory_path('public'), file_directory_path(file_uri_scheme($filepath)), t("File was placed in Drupal's files directory."));
  $this
    ->assertEqual('asdf.txt', basename($filepath), t('File was named correctly.'));
  $this
    ->assertEqual($contents, file_get_contents(drupal_realpath($filepath)), t('Contents of the file are correct.'));
  $this
    ->assertFilePermissions($filepath, variable_get('file_chmod_file', 0664));
}