3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the file_save_data() function.
12 class SaveDataTest extends FileManagedUnitTestBase {
15 * Test the file_save_data() function when no filename is provided.
17 public function testWithoutFilename() {
18 $contents = $this->randomMachineName(8);
20 $result = file_save_data($contents);
21 $this->assertTrue($result, 'Unnamed file saved correctly.');
23 $this->assertEqual(file_default_scheme(), file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
24 $this->assertEqual($result->getFilename(), drupal_basename($result->getFileUri()), "Filename was set to the file's basename.");
25 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
26 $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
27 $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
29 // Check that the correct hooks were called.
30 $this->assertFileHooksCalled(['insert']);
32 // Verify that what was returned is what's in the database.
33 $this->assertFileUnchanged($result, File::load($result->id()));
37 * Test the file_save_data() function when a filename is provided.
39 public function testWithFilename() {
40 $contents = $this->randomMachineName(8);
42 // Using filename with non-latin characters.
43 $filename = 'Текстовый файл.txt';
45 $result = file_save_data($contents, 'public://' . $filename);
46 $this->assertTrue($result, 'Unnamed file saved correctly.');
48 $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
49 $this->assertEqual($filename, drupal_basename($result->getFileUri()), 'File was named correctly.');
50 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
51 $this->assertEqual($result->getMimeType(), 'text/plain', 'A MIME type was set.');
52 $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
54 // Check that the correct hooks were called.
55 $this->assertFileHooksCalled(['insert']);
57 // Verify that what was returned is what's in the database.
58 $this->assertFileUnchanged($result, File::load($result->id()));
62 * Test file_save_data() when renaming around an existing file.
64 public function testExistingRename() {
65 // Setup a file to overwrite.
66 $existing = $this->createFile();
67 $contents = $this->randomMachineName(8);
69 $result = file_save_data($contents, $existing->getFileUri(), FILE_EXISTS_RENAME);
70 $this->assertTrue($result, 'File saved successfully.');
72 $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
73 $this->assertEqual($result->getFilename(), $existing->getFilename(), 'Filename was set to the basename of the source, rather than that of the renamed file.');
74 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
75 $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
76 $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
78 // Check that the correct hooks were called.
79 $this->assertFileHooksCalled(['insert']);
81 // Ensure that the existing file wasn't overwritten.
82 $this->assertDifferentFile($existing, $result);
83 $this->assertFileUnchanged($existing, File::load($existing->id()));
85 // Verify that was returned is what's in the database.
86 $this->assertFileUnchanged($result, File::load($result->id()));
90 * Test file_save_data() when replacing an existing file.
92 public function testExistingReplace() {
93 // Setup a file to overwrite.
94 $existing = $this->createFile();
95 $contents = $this->randomMachineName(8);
97 $result = file_save_data($contents, $existing->getFileUri(), FILE_EXISTS_REPLACE);
98 $this->assertTrue($result, 'File saved successfully.');
100 $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
101 $this->assertEqual($result->getFilename(), $existing->getFilename(), 'Filename was set to the basename of the existing file, rather than preserving the original name.');
102 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
103 $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
104 $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
106 // Check that the correct hooks were called.
107 $this->assertFileHooksCalled(['load', 'update']);
109 // Verify that the existing file was re-used.
110 $this->assertSameFile($existing, $result);
112 // Verify that what was returned is what's in the database.
113 $this->assertFileUnchanged($result, File::load($result->id()));
117 * Test that file_save_data() fails overwriting an existing file.
119 public function testExistingError() {
120 $contents = $this->randomMachineName(8);
121 $existing = $this->createFile(NULL, $contents);
123 // Check the overwrite error.
124 $result = file_save_data('asdf', $existing->getFileUri(), FILE_EXISTS_ERROR);
125 $this->assertFalse($result, 'Overwriting a file fails when FILE_EXISTS_ERROR is specified.');
126 $this->assertEqual($contents, file_get_contents($existing->getFileUri()), 'Contents of existing file were unchanged.');
128 // Check that no hooks were called while failing.
129 $this->assertFileHooksCalled([]);
131 // Ensure that the existing file wasn't overwritten.
132 $this->assertFileUnchanged($existing, File::load($existing->id()));