FileSaveDataTest.php 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace Drupal\KernelTests\Core\File;
  3. use Drupal\Core\File\FileSystemInterface;
  4. /**
  5. * Tests the file_unmanaged_save_data() function.
  6. *
  7. * @group File
  8. */
  9. class FileSaveDataTest extends FileTestBase {
  10. /**
  11. * Test the file_unmanaged_save_data() function.
  12. */
  13. public function testFileSaveData() {
  14. $contents = $this->randomMachineName(8);
  15. $this->setSetting('file_chmod_file', 0777);
  16. // No filename.
  17. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  18. $file_system = \Drupal::service('file_system');
  19. // Provide a filename.
  20. $filepath = $file_system->saveData($contents, 'public://asdf.txt', FileSystemInterface::EXISTS_REPLACE);
  21. $this->assertNotFalse($filepath, 'Unnamed file saved correctly.');
  22. $this->assertEqual('asdf.txt', \Drupal::service('file_system')->basename($filepath), 'File was named correctly.');
  23. $this->assertEqual($contents, file_get_contents($filepath), 'Contents of the file are correct.');
  24. $this->assertFilePermissions($filepath, 0777);
  25. }
  26. }