class FileSaveDataTest

Same name and namespace in other branches
  1. 7.x modules/simpletest/tests/file.test \FileSaveDataTest
  2. 11.x core/tests/Drupal/KernelTests/Core/File/FileSaveDataTest.php \Drupal\KernelTests\Core\File\FileSaveDataTest
  3. 10 core/tests/Drupal/KernelTests/Core/File/FileSaveDataTest.php \Drupal\KernelTests\Core\File\FileSaveDataTest
  4. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileSaveDataTest.php \Drupal\KernelTests\Core\File\FileSaveDataTest

Tests the file_unmanaged_save_data() function.

@group File

Hierarchy

Expanded class hierarchy of FileSaveDataTest

File

core/tests/Drupal/KernelTests/Core/File/FileSaveDataTest.php, line 12

Namespace

Drupal\KernelTests\Core\File
View source
class FileSaveDataTest extends FileTestBase {
  
  /**
   * Tests the file_unmanaged_save_data() function.
   */
  public function testFileSaveData() {
    $contents = $this->randomMachineName(8);
    $this->setSetting('file_chmod_file', 0777);
    // No filename.
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    // Provide a filename.
    $filepath = $file_system->saveData($contents, 'public://asdf.txt', FileSystemInterface::EXISTS_REPLACE);
    $this->assertNotFalse($filepath, 'Unnamed file saved correctly.');
    $this->assertEquals('asdf.txt', \Drupal::service('file_system')->basename($filepath), 'File was named correctly.');
    $this->assertEquals($contents, file_get_contents($filepath), 'Contents of the file are correct.');
    $this->assertFilePermissions($filepath, 0777);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.